blob: 81d1c43dd97fbcab03a0b3070be20017078ff3c8 [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>
Paul Gortmaker056075c2011-07-03 13:58:33 -040047#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110048#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070049#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110050#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110051#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/raid/md_p.h>
53#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110055#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110056#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown01f96c02011-09-21 15:30:20 +100062/* pers_list is a list of registered personalities protected
63 * by pers_lock.
64 * pers_lock does extra service to protect accesses to
65 * mddev->thread when the mutex cannot be held.
66 */
NeilBrown2604b702006-01-06 00:20:36 -080067static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_SPINLOCK(pers_lock);
69
Adrian Bunk5e563412006-06-26 00:27:42 -070070static void md_print_devices(void);
71
Bernd Schubert90b08712008-05-23 13:04:38 -070072static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020073static struct workqueue_struct *md_wq;
74static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070075
Adrian Bunk5e563412006-06-26 00:27:42 -070076#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Robert Becker1e509152009-12-14 12:49:58 +110079 * Default number of read corrections we'll attempt on an rdev
80 * before ejecting it from the array. We divide the read error
81 * count by 2 for every hour elapsed between read errors.
82 */
83#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
86 * is 1000 KB/sec, so the extra system load does not show up that much.
87 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070088 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * subsystem is idle. There is also an 'absolute maximum' reconstruction
90 * speed limit - in case reconstruction slows down your system despite
91 * idle IO detection.
92 *
93 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080094 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96
97static int sysctl_speed_limit_min = 1000;
98static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110099static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800100{
101 return mddev->sync_speed_min ?
102 mddev->sync_speed_min : sysctl_speed_limit_min;
103}
104
NeilBrownfd01b882011-10-11 16:47:53 +1100105static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800106{
107 return mddev->sync_speed_max ?
108 mddev->sync_speed_max : sysctl_speed_limit_max;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static struct ctl_table_header *raid_table_header;
112
113static ctl_table raid_table[] = {
114 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .procname = "speed_limit_min",
116 .data = &sysctl_speed_limit_min,
117 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700118 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800119 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 },
121 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .procname = "speed_limit_max",
123 .data = &sysctl_speed_limit_max,
124 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700125 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800126 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800128 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131static ctl_table raid_dir_table[] = {
132 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .procname = "raid",
134 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700135 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .child = raid_table,
137 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800138 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141static ctl_table raid_root_table[] = {
142 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .procname = "dev",
144 .maxlen = 0,
145 .mode = 0555,
146 .child = raid_dir_table,
147 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800148 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700151static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
NeilBrownf91de922005-11-08 21:39:36 -0800153static int start_readonly;
154
NeilBrowna167f662010-10-26 18:31:13 +1100155/* bio_clone_mddev
156 * like bio_clone, but with a local bio set
157 */
158
159static void mddev_bio_destructor(struct bio *bio)
160{
NeilBrownfd01b882011-10-11 16:47:53 +1100161 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100162
163 mddevp = (void*)bio;
164 mddev = mddevp[-1];
165
166 bio_free(bio, mddev->bio_set);
167}
168
169struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100170 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100171{
172 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100173 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100174
175 if (!mddev || !mddev->bio_set)
176 return bio_alloc(gfp_mask, nr_iovecs);
177
178 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
179 mddev->bio_set);
180 if (!b)
181 return NULL;
182 mddevp = (void*)b;
183 mddevp[-1] = mddev;
184 b->bi_destructor = mddev_bio_destructor;
185 return b;
186}
187EXPORT_SYMBOL_GPL(bio_alloc_mddev);
188
189struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100190 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100191{
192 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100193 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100194
195 if (!mddev || !mddev->bio_set)
196 return bio_clone(bio, gfp_mask);
197
198 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
199 mddev->bio_set);
200 if (!b)
201 return NULL;
202 mddevp = (void*)b;
203 mddevp[-1] = mddev;
204 b->bi_destructor = mddev_bio_destructor;
205 __bio_clone(b, bio);
206 if (bio_integrity(bio)) {
207 int ret;
208
209 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
210
211 if (ret < 0) {
212 bio_put(b);
213 return NULL;
214 }
215 }
216
217 return b;
218}
219EXPORT_SYMBOL_GPL(bio_clone_mddev);
220
NeilBrownd2eb35a2011-07-28 11:31:48 +1000221void md_trim_bio(struct bio *bio, int offset, int size)
222{
223 /* 'bio' is a cloned bio which we need to trim to match
224 * the given offset and size.
225 * This requires adjusting bi_sector, bi_size, and bi_io_vec
226 */
227 int i;
228 struct bio_vec *bvec;
229 int sofar = 0;
230
231 size <<= 9;
232 if (offset == 0 && size == bio->bi_size)
233 return;
234
235 bio->bi_sector += offset;
236 bio->bi_size = size;
237 offset <<= 9;
238 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
239
240 while (bio->bi_idx < bio->bi_vcnt &&
241 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
242 /* remove this whole bio_vec */
243 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
244 bio->bi_idx++;
245 }
246 if (bio->bi_idx < bio->bi_vcnt) {
247 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
248 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
249 }
250 /* avoid any complications with bi_idx being non-zero*/
251 if (bio->bi_idx) {
252 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
253 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
254 bio->bi_vcnt -= bio->bi_idx;
255 bio->bi_idx = 0;
256 }
257 /* Make sure vcnt and last bv are not too big */
258 bio_for_each_segment(bvec, bio, i) {
259 if (sofar + bvec->bv_len > size)
260 bvec->bv_len = size - sofar;
261 if (bvec->bv_len == 0) {
262 bio->bi_vcnt = i;
263 break;
264 }
265 sofar += bvec->bv_len;
266 }
267}
268EXPORT_SYMBOL_GPL(md_trim_bio);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
NeilBrownd7603b72006-01-06 00:20:30 -0800271 * We have a system wide 'event count' that is incremented
272 * on any 'interesting' event, and readers of /proc/mdstat
273 * can use 'poll' or 'select' to find out when the event
274 * count increases.
275 *
276 * Events are:
277 * start array, stop array, error, add device, remove device,
278 * start build, activate spare
279 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800280static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800281static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100282void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800283{
284 atomic_inc(&md_event_count);
285 wake_up(&md_event_waiters);
286}
NeilBrown29269552006-03-27 01:18:10 -0800287EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800288
NeilBrownc331eb02006-05-30 21:27:13 -0700289/* Alternate version that can be called from interrupts
290 * when calling sysfs_notify isn't needed.
291 */
NeilBrownfd01b882011-10-11 16:47:53 +1100292static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700293{
294 atomic_inc(&md_event_count);
295 wake_up(&md_event_waiters);
296}
297
NeilBrownd7603b72006-01-06 00:20:30 -0800298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * Enables to iterate over all existing md arrays
300 * all_mddevs_lock protects this list.
301 */
302static LIST_HEAD(all_mddevs);
303static DEFINE_SPINLOCK(all_mddevs_lock);
304
305
306/*
307 * iterates through all used mddevs in the system.
308 * We take care to grab the all_mddevs_lock whenever navigating
309 * the list, and to always hold a refcount when unlocked.
310 * Any code which breaks out of this loop while own
311 * a reference to the current mddev and must mddev_put it.
312 */
NeilBrownfd01b882011-10-11 16:47:53 +1100313#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 \
315 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100316 _tmp = all_mddevs.next; \
317 _mddev = NULL;}); \
318 ({ if (_tmp != &all_mddevs) \
319 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100321 if (_mddev) mddev_put(_mddev); \
322 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
323 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100325 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 )
327
328
NeilBrown409c57f2009-03-31 14:39:39 +1100329/* Rather than calling directly into the personality make_request function,
330 * IO requests come here first so that we can check if the device is
331 * being suspended pending a reconfiguration.
332 * We hold a refcount over the call to ->make_request. By the time that
333 * call has finished, the bio has been linked into some internal structure
334 * and so is visible to ->quiesce(), so we don't need the refcount any more.
335 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200336static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
NeilBrown49077322010-03-25 16:20:56 +1100338 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100339 struct mddev *mddev = q->queuedata;
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);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200346 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100347 }
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);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200371 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
NeilBrown9e35b992010-04-06 14:23:02 +1000382/* mddev_suspend makes sure no new requests are submitted
383 * to the device, and that any requests that have been submitted
384 * are completely handled.
385 * Once ->stop is called and completes, the module will be completely
386 * unused.
387 */
NeilBrownfd01b882011-10-11 16:47:53 +1100388void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100389{
390 BUG_ON(mddev->suspended);
391 mddev->suspended = 1;
392 synchronize_rcu();
393 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
394 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100395}
NeilBrown390ee602010-06-01 19:37:27 +1000396EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100397
NeilBrownfd01b882011-10-11 16:47:53 +1100398void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100399{
400 mddev->suspended = 0;
401 wake_up(&mddev->sb_wait);
402 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500403
404 md_wakeup_thread(mddev->thread);
405 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100406}
NeilBrown390ee602010-06-01 19:37:27 +1000407EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100408
NeilBrownfd01b882011-10-11 16:47:53 +1100409int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000410{
411 return mddev->suspended;
412}
413EXPORT_SYMBOL(mddev_congested);
414
NeilBrowna2826aa2009-12-14 12:49:49 +1100415/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 */
418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420{
NeilBrown3cb03002011-10-11 16:45:26 +1100421 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100422 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100423
424 rdev_dec_pending(rdev, mddev);
425
426 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200428 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429 }
430 bio_put(bio);
431}
432
NeilBrowna7a07e62010-12-09 16:04:25 +1100433static void md_submit_flush_data(struct work_struct *ws);
434
NeilBrowna035fc32010-12-09 16:17:51 +1100435static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100436{
NeilBrownfd01b882011-10-11 16:47:53 +1100437 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100438 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100439
NeilBrowna7a07e62010-12-09 16:04:25 +1100440 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
441 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100442 rcu_read_lock();
443 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
444 if (rdev->raid_disk >= 0 &&
445 !test_bit(Faulty, &rdev->flags)) {
446 /* Take two references, one is dropped
447 * when request finishes, one after
448 * we reclaim rcu_read_lock
449 */
450 struct bio *bi;
451 atomic_inc(&rdev->nr_pending);
452 atomic_inc(&rdev->nr_pending);
453 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100454 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200455 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100456 bi->bi_private = rdev;
457 bi->bi_bdev = rdev->bdev;
458 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200459 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100460 rcu_read_lock();
461 rdev_dec_pending(rdev, mddev);
462 }
463 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100464 if (atomic_dec_and_test(&mddev->flush_pending))
465 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100466}
467
Tejun Heoe9c74692010-09-03 11:56:18 +0200468static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100469{
NeilBrownfd01b882011-10-11 16:47:53 +1100470 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200471 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100472
Tejun Heoe9c74692010-09-03 11:56:18 +0200473 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 /* an empty barrier - all done */
475 bio_endio(bio, 0);
476 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200477 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200478 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100479 }
NeilBrown2b74e122010-12-09 15:59:01 +1100480
481 mddev->flush_bio = NULL;
482 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100483}
484
NeilBrownfd01b882011-10-11 16:47:53 +1100485void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100486{
487 spin_lock_irq(&mddev->write_lock);
488 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200489 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100490 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200491 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100492 spin_unlock_irq(&mddev->write_lock);
493
NeilBrowna035fc32010-12-09 16:17:51 +1100494 INIT_WORK(&mddev->flush_work, submit_flushes);
495 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100496}
Tejun Heoe9c74692010-09-03 11:56:18 +0200497EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100498
NeilBrown97658cd2011-04-18 18:25:42 +1000499/* Support for plugging.
500 * This mirrors the plugging support in request_queue, but does not
501 * require having a whole queue or request structures.
502 * We allocate an md_plug_cb for each md device and each thread it gets
503 * plugged on. This links tot the private plug_handle structure in the
504 * personality data where we keep a count of the number of outstanding
505 * plugs so other code can see if a plug is active.
506 */
507struct md_plug_cb {
508 struct blk_plug_cb cb;
NeilBrownfd01b882011-10-11 16:47:53 +1100509 struct mddev *mddev;
NeilBrown97658cd2011-04-18 18:25:42 +1000510};
511
512static void plugger_unplug(struct blk_plug_cb *cb)
513{
514 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
515 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
516 md_wakeup_thread(mdcb->mddev->thread);
517 kfree(mdcb);
518}
519
520/* Check that an unplug wakeup will come shortly.
521 * If not, wakeup the md thread immediately
522 */
NeilBrownfd01b882011-10-11 16:47:53 +1100523int mddev_check_plugged(struct mddev *mddev)
NeilBrown97658cd2011-04-18 18:25:42 +1000524{
525 struct blk_plug *plug = current->plug;
526 struct md_plug_cb *mdcb;
527
528 if (!plug)
529 return 0;
530
531 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
532 if (mdcb->cb.callback == plugger_unplug &&
533 mdcb->mddev == mddev) {
534 /* Already on the list, move to top */
535 if (mdcb != list_first_entry(&plug->cb_list,
536 struct md_plug_cb,
537 cb.list))
538 list_move(&mdcb->cb.list, &plug->cb_list);
539 return 1;
540 }
541 }
542 /* Not currently on the callback list */
543 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
544 if (!mdcb)
545 return 0;
546
547 mdcb->mddev = mddev;
548 mdcb->cb.callback = plugger_unplug;
549 atomic_inc(&mddev->plug_cnt);
550 list_add(&mdcb->cb.list, &plug->cb_list);
551 return 1;
552}
553EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000554
NeilBrownfd01b882011-10-11 16:47:53 +1100555static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 atomic_inc(&mddev->active);
558 return mddev;
559}
560
Dan Williams5fd3a172009-03-04 00:57:25 -0700561static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100562
NeilBrownfd01b882011-10-11 16:47:53 +1100563static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
NeilBrowna167f662010-10-26 18:31:13 +1100565 struct bio_set *bs = NULL;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
568 return;
NeilBrownd3374822009-01-09 08:31:10 +1100569 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100570 mddev->ctime == 0 && !mddev->hold_active) {
571 /* Array is not configured at all, and not held active,
572 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100573 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100574 bs = mddev->bio_set;
575 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100576 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200577 /* We did a probe so need to clean up. Call
578 * queue_work inside the spinlock so that
579 * flush_workqueue() after mddev_find will
580 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100581 */
582 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200583 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100584 } else
585 kfree(mddev);
586 }
587 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100588 if (bs)
589 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
NeilBrownfd01b882011-10-11 16:47:53 +1100592void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100593{
594 mutex_init(&mddev->open_mutex);
595 mutex_init(&mddev->reconfig_mutex);
596 mutex_init(&mddev->bitmap_info.mutex);
597 INIT_LIST_HEAD(&mddev->disks);
598 INIT_LIST_HEAD(&mddev->all_mddevs);
599 init_timer(&mddev->safemode_timer);
600 atomic_set(&mddev->active, 1);
601 atomic_set(&mddev->openers, 0);
602 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000603 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100604 spin_lock_init(&mddev->write_lock);
605 atomic_set(&mddev->flush_pending, 0);
606 init_waitqueue_head(&mddev->sb_wait);
607 init_waitqueue_head(&mddev->recovery_wait);
608 mddev->reshape_position = MaxSector;
609 mddev->resync_min = 0;
610 mddev->resync_max = MaxSector;
611 mddev->level = LEVEL_NONE;
612}
NeilBrown390ee602010-06-01 19:37:27 +1000613EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100614
NeilBrownfd01b882011-10-11 16:47:53 +1100615static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
NeilBrownfd01b882011-10-11 16:47:53 +1100617 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
NeilBrown8f5f02c2011-02-16 13:58:51 +1100619 if (unit && MAJOR(unit) != MD_MAJOR)
620 unit &= ~((1<<MdpMinorShift)-1);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 retry:
623 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
NeilBrownefeb53c2009-01-09 08:31:10 +1100625 if (unit) {
626 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
627 if (mddev->unit == unit) {
628 mddev_get(mddev);
629 spin_unlock(&all_mddevs_lock);
630 kfree(new);
631 return mddev;
632 }
633
634 if (new) {
635 list_add(&new->all_mddevs, &all_mddevs);
636 spin_unlock(&all_mddevs_lock);
637 new->hold_active = UNTIL_IOCTL;
638 return new;
639 }
640 } else if (new) {
641 /* find an unused unit number */
642 static int next_minor = 512;
643 int start = next_minor;
644 int is_free = 0;
645 int dev = 0;
646 while (!is_free) {
647 dev = MKDEV(MD_MAJOR, next_minor);
648 next_minor++;
649 if (next_minor > MINORMASK)
650 next_minor = 0;
651 if (next_minor == start) {
652 /* Oh dear, all in use. */
653 spin_unlock(&all_mddevs_lock);
654 kfree(new);
655 return NULL;
656 }
657
658 is_free = 1;
659 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
660 if (mddev->unit == dev) {
661 is_free = 0;
662 break;
663 }
664 }
665 new->unit = dev;
666 new->md_minor = MINOR(dev);
667 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 list_add(&new->all_mddevs, &all_mddevs);
669 spin_unlock(&all_mddevs_lock);
670 return new;
671 }
672 spin_unlock(&all_mddevs_lock);
673
NeilBrown9ffae0c2006-01-06 00:20:32 -0800674 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!new)
676 return NULL;
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 new->unit = unit;
679 if (MAJOR(unit) == MD_MAJOR)
680 new->md_minor = MINOR(unit);
681 else
682 new->md_minor = MINOR(unit) >> MdpMinorShift;
683
NeilBrownfafd7fb2010-04-01 15:55:30 +1100684 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto retry;
687}
688
NeilBrownfd01b882011-10-11 16:47:53 +1100689static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800691 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
NeilBrownfd01b882011-10-11 16:47:53 +1100694static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100695{
696 return mutex_is_locked(&mddev->reconfig_mutex);
697}
698
NeilBrownfd01b882011-10-11 16:47:53 +1100699static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800701 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
NeilBrownb6eb1272010-04-15 10:13:47 +1000704static struct attribute_group md_redundancy_group;
705
NeilBrownfd01b882011-10-11 16:47:53 +1100706static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
NeilBrowna64c8762010-04-14 17:15:37 +1000708 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000709 /* These cannot be removed under reconfig_mutex as
710 * an access to the files will try to take reconfig_mutex
711 * while holding the file unremovable, which leads to
712 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000713 * So hold set sysfs_active while the remove in happeing,
714 * and anything else which might set ->to_remove or my
715 * otherwise change the sysfs namespace will fail with
716 * -EBUSY if sysfs_active is still set.
717 * We set sysfs_active under reconfig_mutex and elsewhere
718 * test it under the same mutex to ensure its correct value
719 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000720 */
NeilBrowna64c8762010-04-14 17:15:37 +1000721 struct attribute_group *to_remove = mddev->to_remove;
722 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000723 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000724 mutex_unlock(&mddev->reconfig_mutex);
725
NeilBrown00bcb4a2010-06-01 19:37:23 +1000726 if (mddev->kobj.sd) {
727 if (to_remove != &md_redundancy_group)
728 sysfs_remove_group(&mddev->kobj, to_remove);
729 if (mddev->pers == NULL ||
730 mddev->pers->sync_request == NULL) {
731 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
732 if (mddev->sysfs_action)
733 sysfs_put(mddev->sysfs_action);
734 mddev->sysfs_action = NULL;
735 }
NeilBrowna64c8762010-04-14 17:15:37 +1000736 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000737 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000738 } else
739 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Chris Dunlop751e67c2011-10-19 16:48:26 +1100741 /* As we've dropped the mutex we need a spinlock to
742 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000743 */
744 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700745 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000746 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
NeilBrownfd01b882011-10-11 16:47:53 +1100749static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
NeilBrown3cb03002011-10-11 16:45:26 +1100751 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100753 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (rdev->desc_nr == nr)
755 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return NULL;
758}
759
NeilBrownfd01b882011-10-11 16:47:53 +1100760static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
NeilBrown3cb03002011-10-11 16:45:26 +1100762 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100764 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (rdev->bdev->bd_dev == dev)
766 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return NULL;
769}
770
NeilBrown84fc4b52011-10-11 16:49:58 +1100771static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800772{
NeilBrown84fc4b52011-10-11 16:49:58 +1100773 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800774 list_for_each_entry(pers, &pers_list, list) {
775 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800776 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800777 if (strcmp(pers->name, clevel)==0)
778 return pers;
779 }
NeilBrown2604b702006-01-06 00:20:36 -0800780 return NULL;
781}
782
Andre Nollb73df2d2008-07-11 22:02:23 +1000783/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100784static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100786 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000787 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
NeilBrown3cb03002011-10-11 16:45:26 +1100790static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 if (rdev->sb_page)
793 MD_BUG();
794
795 rdev->sb_page = alloc_page(GFP_KERNEL);
796 if (!rdev->sb_page) {
797 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000798 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801 return 0;
802}
803
NeilBrown3cb03002011-10-11 16:45:26 +1100804static void free_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800807 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 rdev->sb_loaded = 0;
809 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000810 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100811 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
NeilBrown2699b672011-07-28 11:31:47 +1000813 if (rdev->bb_page) {
814 put_page(rdev->bb_page);
815 rdev->bb_page = NULL;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819
NeilBrown6712ecf2007-09-27 12:47:43 +0200820static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700821{
NeilBrown3cb03002011-10-11 16:45:26 +1100822 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100823 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700824
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700825 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
826 printk("md: super_written gets error=%d, uptodate=%d\n",
827 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
828 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800829 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700830 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700831
NeilBrowna9701a32005-11-08 21:39:34 -0800832 if (atomic_dec_and_test(&mddev->pending_writes))
833 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700834 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700835}
836
NeilBrownfd01b882011-10-11 16:47:53 +1100837void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700838 sector_t sector, int size, struct page *page)
839{
840 /* write first size bytes of page to sector of rdev
841 * Increment mddev->pending_writes before returning
842 * and decrement it on completion, waking up sb_wait
843 * if zero is reached.
844 * If an error occurred, call md_error
845 */
NeilBrowna167f662010-10-26 18:31:13 +1100846 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700847
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100848 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700849 bio->bi_sector = sector;
850 bio_add_page(bio, page, size, 0);
851 bio->bi_private = rdev;
852 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800853
NeilBrown7bfa19f2005-06-21 17:17:28 -0700854 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000855 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800856}
857
NeilBrownfd01b882011-10-11 16:47:53 +1100858void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800859{
Tejun Heoe9c74692010-09-03 11:56:18 +0200860 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800861 DEFINE_WAIT(wq);
862 for(;;) {
863 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
864 if (atomic_read(&mddev->pending_writes)==0)
865 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800866 schedule();
867 }
868 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700869}
870
NeilBrown6712ecf2007-09-27 12:47:43 +0200871static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
NeilBrown3cb03002011-10-11 16:45:26 +1100876int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100877 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
NeilBrowna167f662010-10-26 18:31:13 +1100879 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct completion event;
881 int ret;
882
Jens Axboe721a9602011-03-09 11:56:30 +0100883 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100885 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
886 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100887 if (metadata_op)
888 bio->bi_sector = sector + rdev->sb_start;
889 else
890 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 bio_add_page(bio, page, size, 0);
892 init_completion(&event);
893 bio->bi_private = &event;
894 bio->bi_end_io = bi_complete;
895 submit_bio(rw, bio);
896 wait_for_completion(&event);
897
898 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
899 bio_put(bio);
900 return ret;
901}
NeilBrowna8745db2006-01-06 00:20:34 -0800902EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
NeilBrown3cb03002011-10-11 16:45:26 +1100904static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 char b[BDEVNAME_SIZE];
907 if (!rdev->sb_page) {
908 MD_BUG();
909 return -EINVAL;
910 }
911 if (rdev->sb_loaded)
912 return 0;
913
914
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100915 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 goto fail;
917 rdev->sb_loaded = 1;
918 return 0;
919
920fail:
921 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
922 bdevname(rdev->bdev,b));
923 return -EINVAL;
924}
925
926static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
927{
Andre Noll05710462008-07-11 22:02:20 +1000928 return sb1->set_uuid0 == sb2->set_uuid0 &&
929 sb1->set_uuid1 == sb2->set_uuid1 &&
930 sb1->set_uuid2 == sb2->set_uuid2 &&
931 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
935{
936 int ret;
937 mdp_super_t *tmp1, *tmp2;
938
939 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
940 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
941
942 if (!tmp1 || !tmp2) {
943 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100944 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto abort;
946 }
947
948 *tmp1 = *sb1;
949 *tmp2 = *sb2;
950
951 /*
952 * nr_disks is not constant
953 */
954 tmp1->nr_disks = 0;
955 tmp2->nr_disks = 0;
956
Andre Nollce0c8e02008-07-11 22:02:20 +1000957 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700959 kfree(tmp1);
960 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return ret;
962}
963
NeilBrown4d167f02007-05-09 02:35:37 -0700964
965static u32 md_csum_fold(u32 csum)
966{
967 csum = (csum & 0xffff) + (csum >> 16);
968 return (csum & 0xffff) + (csum >> 16);
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static unsigned int calc_sb_csum(mdp_super_t * sb)
972{
NeilBrown4d167f02007-05-09 02:35:37 -0700973 u64 newcsum = 0;
974 u32 *sb32 = (u32*)sb;
975 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned int disk_csum, csum;
977
978 disk_csum = sb->sb_csum;
979 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700980
981 for (i = 0; i < MD_SB_BYTES/4 ; i++)
982 newcsum += sb32[i];
983 csum = (newcsum & 0xffffffff) + (newcsum>>32);
984
985
986#ifdef CONFIG_ALPHA
987 /* This used to use csum_partial, which was wrong for several
988 * reasons including that different results are returned on
989 * different architectures. It isn't critical that we get exactly
990 * the same return value as before (we always csum_fold before
991 * testing, and that removes any differences). However as we
992 * know that csum_partial always returned a 16bit value on
993 * alphas, do a fold to maximise conformity to previous behaviour.
994 */
995 sb->sb_csum = md_csum_fold(disk_csum);
996#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return csum;
1000}
1001
1002
1003/*
1004 * Handle superblock details.
1005 * We want to be able to handle multiple superblock formats
1006 * so we have a common interface to them all, and an array of
1007 * different handlers.
1008 * We rely on user-space to write the initial superblock, and support
1009 * reading and updating of superblocks.
1010 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001011 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * loads and validates a superblock on dev.
1013 * if refdev != NULL, compare superblocks on both devices
1014 * Return:
1015 * 0 - dev has a superblock that is compatible with refdev
1016 * 1 - dev has a superblock that is compatible and newer than refdev
1017 * so dev should be used as the refdev in future
1018 * -EINVAL superblock incompatible or invalid
1019 * -othererror e.g. -EIO
1020 *
NeilBrownfd01b882011-10-11 16:47:53 +11001021 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * Verify that dev is acceptable into mddev.
1023 * The first time, mddev->raid_disks will be 0, and data from
1024 * dev should be merged in. Subsequent calls check that dev
1025 * is new enough. Return 0 or -EINVAL
1026 *
NeilBrownfd01b882011-10-11 16:47:53 +11001027 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * Update the superblock for rdev with data in mddev
1029 * This does not write to disc.
1030 *
1031 */
1032
1033struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001034 char *name;
1035 struct module *owner;
NeilBrown3cb03002011-10-11 16:45:26 +11001036 int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001037 int minor_version);
NeilBrownfd01b882011-10-11 16:47:53 +11001038 int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev);
1039 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001040 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001041 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042};
1043
1044/*
Andre Noll0894cc32009-06-18 08:49:23 +10001045 * Check that the given mddev has no bitmap.
1046 *
1047 * This function is called from the run method of all personalities that do not
1048 * support bitmaps. It prints an error message and returns non-zero if mddev
1049 * has a bitmap. Otherwise, it returns 0.
1050 *
1051 */
NeilBrownfd01b882011-10-11 16:47:53 +11001052int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001053{
NeilBrownc3d97142009-12-14 12:49:52 +11001054 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001055 return 0;
1056 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1057 mdname(mddev), mddev->pers->name);
1058 return 1;
1059}
1060EXPORT_SYMBOL(md_check_no_bitmap);
1061
1062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * load_super for 0.90.0
1064 */
NeilBrown3cb03002011-10-11 16:45:26 +11001065static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1068 mdp_super_t *sb;
1069 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /*
Andre Noll0f420352008-07-11 22:02:23 +10001072 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * it's at the end of the disk.
1074 *
1075 * It also happens to be a multiple of 4Kb.
1076 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001077 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
NeilBrown0002b272005-09-09 16:23:53 -07001079 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (ret) return ret;
1081
1082 ret = -EINVAL;
1083
1084 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001085 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 if (sb->md_magic != MD_SB_MAGIC) {
1088 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1089 b);
1090 goto abort;
1091 }
1092
1093 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001094 sb->minor_version < 90 ||
1095 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1097 sb->major_version, sb->minor_version,
1098 b);
1099 goto abort;
1100 }
1101
1102 if (sb->raid_disks <= 0)
1103 goto abort;
1104
NeilBrown4d167f02007-05-09 02:35:37 -07001105 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1107 b);
1108 goto abort;
1109 }
1110
1111 rdev->preferred_minor = sb->md_minor;
1112 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001113 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001114 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (sb->level == LEVEL_MULTIPATH)
1117 rdev->desc_nr = -1;
1118 else
1119 rdev->desc_nr = sb->this_disk.number;
1120
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001121 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001125 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!uuid_equal(refsb, sb)) {
1127 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1128 b, bdevname(refdev->bdev,b2));
1129 goto abort;
1130 }
1131 if (!sb_equal(refsb, sb)) {
1132 printk(KERN_WARNING "md: %s has same UUID"
1133 " but different superblock to %s\n",
1134 b, bdevname(refdev->bdev, b2));
1135 goto abort;
1136 }
1137 ev1 = md_event(sb);
1138 ev2 = md_event(refsb);
1139 if (ev1 > ev2)
1140 ret = 1;
1141 else
1142 ret = 0;
1143 }
NeilBrown8190e752009-06-18 08:48:58 +10001144 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001145 /* Limit to 4TB as metadata cannot record more than that */
1146 if (rdev->sectors >= (2ULL << 32))
1147 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
NeilBrown27a7b262011-09-10 17:21:28 +10001149 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001150 /* "this cannot possibly happen" ... */
1151 ret = -EINVAL;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 abort:
1154 return ret;
1155}
1156
1157/*
1158 * validate_super for 0.90.0
1159 */
NeilBrownfd01b882011-10-11 16:47:53 +11001160static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001163 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001164 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
NeilBrown41158c72005-06-21 17:17:25 -07001166 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001167 clear_bit(Faulty, &rdev->flags);
1168 clear_bit(In_sync, &rdev->flags);
1169 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (mddev->raid_disks == 0) {
1172 mddev->major_version = 0;
1173 mddev->minor_version = sb->minor_version;
1174 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001175 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001176 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 mddev->ctime = sb->ctime;
1178 mddev->utime = sb->utime;
1179 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001180 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 mddev->layout = sb->layout;
1182 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001183 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001184 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001185 mddev->bitmap_info.offset = 0;
1186 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
NeilBrownf6705572006-03-27 01:18:11 -08001188 if (mddev->minor_version >= 91) {
1189 mddev->reshape_position = sb->reshape_position;
1190 mddev->delta_disks = sb->delta_disks;
1191 mddev->new_level = sb->new_level;
1192 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001193 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001194 } else {
1195 mddev->reshape_position = MaxSector;
1196 mddev->delta_disks = 0;
1197 mddev->new_level = mddev->level;
1198 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001199 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (sb->state & (1<<MD_SB_CLEAN))
1203 mddev->recovery_cp = MaxSector;
1204 else {
1205 if (sb->events_hi == sb->cp_events_hi &&
1206 sb->events_lo == sb->cp_events_lo) {
1207 mddev->recovery_cp = sb->recovery_cp;
1208 } else
1209 mddev->recovery_cp = 0;
1210 }
1211
1212 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1213 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1214 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1215 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1216
1217 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001218
1219 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001220 mddev->bitmap_info.file == NULL)
1221 mddev->bitmap_info.offset =
1222 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001223
NeilBrown41158c72005-06-21 17:17:25 -07001224 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001225 /* Insist on good event counter while assembling, except
1226 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001228 if (sb->disks[rdev->desc_nr].state & (
1229 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1230 if (ev1 < mddev->events)
1231 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001232 } else if (mddev->bitmap) {
1233 /* if adding to array with a bitmap, then we can accept an
1234 * older device ... but not too old.
1235 */
NeilBrown41158c72005-06-21 17:17:25 -07001236 if (ev1 < mddev->bitmap->events_cleared)
1237 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001238 } else {
1239 if (ev1 < mddev->events)
1240 /* just a hot-add of a new device, leave raid_disk at -1 */
1241 return 0;
1242 }
NeilBrown41158c72005-06-21 17:17:25 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 desc = sb->disks + rdev->desc_nr;
1246
1247 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001248 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001249 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1250 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001251 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1254 /* active but not in sync implies recovery up to
1255 * reshape position. We don't know exactly where
1256 * that is, so set to zero for now */
1257 if (mddev->minor_version >= 91) {
1258 rdev->recovery_offset = 0;
1259 rdev->raid_disk = desc->raid_disk;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001262 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1263 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001264 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001265 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
1268
1269/*
1270 * sync_super for 0.90.0
1271 */
NeilBrownfd01b882011-10-11 16:47:53 +11001272static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001275 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* make rdev->sb match mddev data..
1280 *
1281 * 1/ zero out disks
1282 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1283 * 3/ any empty disks < next_spare become removed
1284 *
1285 * disks[0] gets initialised to REMOVED because
1286 * we cannot be sure from other fields if it has
1287 * been initialised or not.
1288 */
1289 int i;
1290 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1291
NeilBrown61181562005-09-09 16:24:02 -07001292 rdev->sb_size = MD_SB_BYTES;
1293
Namhyung Kim65a06f062011-07-27 11:00:36 +10001294 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 memset(sb, 0, sizeof(*sb));
1297
1298 sb->md_magic = MD_SB_MAGIC;
1299 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 sb->patch_version = mddev->patch_version;
1301 sb->gvalid_words = 0; /* ignored */
1302 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1303 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1304 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1305 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1306
1307 sb->ctime = mddev->ctime;
1308 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001309 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sb->raid_disks = mddev->raid_disks;
1311 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001312 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 sb->utime = mddev->utime;
1314 sb->state = 0;
1315 sb->events_hi = (mddev->events>>32);
1316 sb->events_lo = (u32)mddev->events;
1317
NeilBrownf6705572006-03-27 01:18:11 -08001318 if (mddev->reshape_position == MaxSector)
1319 sb->minor_version = 90;
1320 else {
1321 sb->minor_version = 91;
1322 sb->reshape_position = mddev->reshape_position;
1323 sb->new_level = mddev->new_level;
1324 sb->delta_disks = mddev->delta_disks;
1325 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001326 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001327 }
1328 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (mddev->in_sync)
1330 {
1331 sb->recovery_cp = mddev->recovery_cp;
1332 sb->cp_events_hi = (mddev->events>>32);
1333 sb->cp_events_lo = (u32)mddev->events;
1334 if (mddev->recovery_cp == MaxSector)
1335 sb->state = (1<< MD_SB_CLEAN);
1336 } else
1337 sb->recovery_cp = 0;
1338
1339 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001340 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
NeilBrownc3d97142009-12-14 12:49:52 +11001342 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001343 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001346 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001348 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001349 int is_active = test_bit(In_sync, &rdev2->flags);
1350
1351 if (rdev2->raid_disk >= 0 &&
1352 sb->minor_version >= 91)
1353 /* we have nowhere to store the recovery_offset,
1354 * but if it is not below the reshape_position,
1355 * we can piggy-back on that.
1356 */
1357 is_active = 1;
1358 if (rdev2->raid_disk < 0 ||
1359 test_bit(Faulty, &rdev2->flags))
1360 is_active = 0;
1361 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001362 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001364 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001365 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 d = &sb->disks[rdev2->desc_nr];
1367 nr_disks++;
1368 d->number = rdev2->desc_nr;
1369 d->major = MAJOR(rdev2->bdev->bd_dev);
1370 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001371 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 d->raid_disk = rdev2->raid_disk;
1373 else
1374 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001375 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001377 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001379 if (test_bit(In_sync, &rdev2->flags))
1380 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 active++;
1382 working++;
1383 } else {
1384 d->state = 0;
1385 spare++;
1386 working++;
1387 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001388 if (test_bit(WriteMostly, &rdev2->flags))
1389 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* now set the "removed" and "faulty" bits on any missing devices */
1392 for (i=0 ; i < mddev->raid_disks ; i++) {
1393 mdp_disk_t *d = &sb->disks[i];
1394 if (d->state == 0 && d->number == 0) {
1395 d->number = i;
1396 d->raid_disk = i;
1397 d->state = (1<<MD_DISK_REMOVED);
1398 d->state |= (1<<MD_DISK_FAULTY);
1399 failed++;
1400 }
1401 }
1402 sb->nr_disks = nr_disks;
1403 sb->active_disks = active;
1404 sb->working_disks = working;
1405 sb->failed_disks = failed;
1406 sb->spare_disks = spare;
1407
1408 sb->this_disk = sb->disks[rdev->desc_nr];
1409 sb->sb_csum = calc_sb_csum(sb);
1410}
1411
1412/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001413 * rdev_size_change for 0.90.0
1414 */
1415static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001416super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001417{
Andre Noll58c0fed2009-03-31 14:33:13 +11001418 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001419 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001420 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001421 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001422 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001423 if (!num_sectors || num_sectors > rdev->sb_start)
1424 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001425 /* Limit to 4TB as metadata cannot record more than that.
1426 * 4TB == 2^32 KB, or 2*2^32 sectors.
1427 */
1428 if (num_sectors >= (2ULL << 32))
1429 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001430 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001431 rdev->sb_page);
1432 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001433 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001434}
1435
1436
1437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * version 1 superblock
1439 */
1440
NeilBrown1c05b4b2006-10-21 10:24:08 -07001441static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001443 __le32 disk_csum;
1444 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 unsigned long long newcsum;
1446 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001447 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int i;
1449
1450 disk_csum = sb->sb_csum;
1451 sb->sb_csum = 0;
1452 newcsum = 0;
1453 for (i=0; size>=4; size -= 4 )
1454 newcsum += le32_to_cpu(*isuper++);
1455
1456 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001457 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1460 sb->sb_csum = disk_csum;
1461 return cpu_to_le32(csum);
1462}
1463
NeilBrown2699b672011-07-28 11:31:47 +10001464static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1465 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001466static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 struct mdp_superblock_1 *sb;
1469 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001470 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001472 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /*
Andre Noll0f420352008-07-11 22:02:23 +10001475 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * It is always aligned to a 4K boundary and
1477 * depeding on minor_version, it can be:
1478 * 0: At least 8K, but less than 12K, from end of device
1479 * 1: At start of device
1480 * 2: 4K from start of device.
1481 */
1482 switch(minor_version) {
1483 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001484 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001485 sb_start -= 8*2;
1486 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
1488 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001489 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001492 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 default:
1495 return -EINVAL;
1496 }
Andre Noll0f420352008-07-11 22:02:23 +10001497 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
NeilBrown0002b272005-09-09 16:23:53 -07001499 /* superblock is rarely larger than 1K, but it can be larger,
1500 * and it is safe to read 4k, so we do that
1501 */
1502 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (ret) return ret;
1504
1505
Namhyung Kim65a06f062011-07-27 11:00:36 +10001506 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1509 sb->major_version != cpu_to_le32(1) ||
1510 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001511 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001512 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return -EINVAL;
1514
1515 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1516 printk("md: invalid superblock checksum on %s\n",
1517 bdevname(rdev->bdev,b));
1518 return -EINVAL;
1519 }
1520 if (le64_to_cpu(sb->data_size) < 10) {
1521 printk("md: data_size too small on %s\n",
1522 bdevname(rdev->bdev,b));
1523 return -EINVAL;
1524 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 rdev->preferred_minor = 0xffff;
1527 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001528 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
NeilBrown0002b272005-09-09 16:23:53 -07001530 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001531 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001532 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001533 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1534
1535 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001536 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001537 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001538
NeilBrown31b65a02006-07-10 04:44:14 -07001539 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1540 rdev->desc_nr = -1;
1541 else
1542 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1543
NeilBrown2699b672011-07-28 11:31:47 +10001544 if (!rdev->bb_page) {
1545 rdev->bb_page = alloc_page(GFP_KERNEL);
1546 if (!rdev->bb_page)
1547 return -ENOMEM;
1548 }
1549 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1550 rdev->badblocks.count == 0) {
1551 /* need to load the bad block list.
1552 * Currently we limit it to one page.
1553 */
1554 s32 offset;
1555 sector_t bb_sector;
1556 u64 *bbp;
1557 int i;
1558 int sectors = le16_to_cpu(sb->bblog_size);
1559 if (sectors > (PAGE_SIZE / 512))
1560 return -EINVAL;
1561 offset = le32_to_cpu(sb->bblog_offset);
1562 if (offset == 0)
1563 return -EINVAL;
1564 bb_sector = (long long)offset;
1565 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1566 rdev->bb_page, READ, true))
1567 return -EIO;
1568 bbp = (u64 *)page_address(rdev->bb_page);
1569 rdev->badblocks.shift = sb->bblog_shift;
1570 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1571 u64 bb = le64_to_cpu(*bbp);
1572 int count = bb & (0x3ff);
1573 u64 sector = bb >> 10;
1574 sector <<= sb->bblog_shift;
1575 count <<= sb->bblog_shift;
1576 if (bb + 1 == 0)
1577 break;
1578 if (md_set_badblocks(&rdev->badblocks,
1579 sector, count, 1) == 0)
1580 return -EINVAL;
1581 }
1582 } else if (sb->bblog_offset == 0)
1583 rdev->badblocks.shift = -1;
1584
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001585 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001586 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001587 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001589 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1592 sb->level != refsb->level ||
1593 sb->layout != refsb->layout ||
1594 sb->chunksize != refsb->chunksize) {
1595 printk(KERN_WARNING "md: %s has strangely different"
1596 " superblock to %s\n",
1597 bdevname(rdev->bdev,b),
1598 bdevname(refdev->bdev,b2));
1599 return -EINVAL;
1600 }
1601 ev1 = le64_to_cpu(sb->events);
1602 ev2 = le64_to_cpu(refsb->events);
1603
1604 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001605 ret = 1;
1606 else
1607 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
NeilBrowna1801f82008-03-04 14:29:31 -08001609 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001610 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001611 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001613 rdev->sectors = rdev->sb_start;
1614 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001616 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001617 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001618 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001619 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
NeilBrownfd01b882011-10-11 16:47:53 +11001622static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001624 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001625 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
NeilBrown41158c72005-06-21 17:17:25 -07001627 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001628 clear_bit(Faulty, &rdev->flags);
1629 clear_bit(In_sync, &rdev->flags);
1630 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 if (mddev->raid_disks == 0) {
1633 mddev->major_version = 1;
1634 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001635 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001636 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1638 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1639 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001640 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 mddev->layout = le32_to_cpu(sb->layout);
1642 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001643 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001644 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001645 mddev->bitmap_info.offset = 0;
1646 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1649 memcpy(mddev->uuid, sb->set_uuid, 16);
1650
1651 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001652
NeilBrown71c08052005-09-09 16:23:51 -07001653 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001654 mddev->bitmap_info.file == NULL )
1655 mddev->bitmap_info.offset =
1656 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001657
NeilBrownf6705572006-03-27 01:18:11 -08001658 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1659 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1660 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1661 mddev->new_level = le32_to_cpu(sb->new_level);
1662 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001663 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001664 } else {
1665 mddev->reshape_position = MaxSector;
1666 mddev->delta_disks = 0;
1667 mddev->new_level = mddev->level;
1668 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001669 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001670 }
1671
NeilBrown41158c72005-06-21 17:17:25 -07001672 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001673 /* Insist of good event counter while assembling, except for
1674 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001676 if (rdev->desc_nr >= 0 &&
1677 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1678 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1679 if (ev1 < mddev->events)
1680 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001681 } else if (mddev->bitmap) {
1682 /* If adding to array with a bitmap, then we can accept an
1683 * older device, but not too old.
1684 */
NeilBrown41158c72005-06-21 17:17:25 -07001685 if (ev1 < mddev->bitmap->events_cleared)
1686 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001687 } else {
1688 if (ev1 < mddev->events)
1689 /* just a hot-add of a new device, leave raid_disk at -1 */
1690 return 0;
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (mddev->level != LEVEL_MULTIPATH) {
1693 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001694 if (rdev->desc_nr < 0 ||
1695 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1696 role = 0xffff;
1697 rdev->desc_nr = -1;
1698 } else
1699 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 switch(role) {
1701 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 break;
1703 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001704 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001707 if ((le32_to_cpu(sb->feature_map) &
1708 MD_FEATURE_RECOVERY_OFFSET))
1709 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1710 else
1711 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 rdev->raid_disk = role;
1713 break;
1714 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001715 if (sb->devflags & WriteMostly1)
1716 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001717 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1718 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001719 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001720 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return 0;
1723}
1724
NeilBrownfd01b882011-10-11 16:47:53 +11001725static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001728 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int max_dev, i;
1730 /* make rdev->sb match mddev and rdev data. */
1731
Namhyung Kim65a06f062011-07-27 11:00:36 +10001732 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 sb->feature_map = 0;
1735 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001736 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(sb->pad3, 0, sizeof(sb->pad3));
1739
1740 sb->utime = cpu_to_le64((__u64)mddev->utime);
1741 sb->events = cpu_to_le64(mddev->events);
1742 if (mddev->in_sync)
1743 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1744 else
1745 sb->resync_offset = cpu_to_le64(0);
1746
NeilBrown1c05b4b2006-10-21 10:24:08 -07001747 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001748
NeilBrownf0ca3402006-02-02 14:28:04 -08001749 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001750 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001751 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001752 sb->level = cpu_to_le32(mddev->level);
1753 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001754
NeilBrownaeb9b2112011-08-25 14:43:08 +10001755 if (test_bit(WriteMostly, &rdev->flags))
1756 sb->devflags |= WriteMostly1;
1757 else
1758 sb->devflags &= ~WriteMostly1;
1759
NeilBrownc3d97142009-12-14 12:49:52 +11001760 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1761 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001762 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001763 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001764
1765 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001766 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001767 sb->feature_map |=
1768 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1769 sb->recovery_offset =
1770 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001771 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001772 if (test_bit(Replacement, &rdev->flags))
1773 sb->feature_map |=
1774 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775
NeilBrownf6705572006-03-27 01:18:11 -08001776 if (mddev->reshape_position != MaxSector) {
1777 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1778 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1779 sb->new_layout = cpu_to_le32(mddev->new_layout);
1780 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1781 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001782 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001783 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001784
NeilBrown2699b672011-07-28 11:31:47 +10001785 if (rdev->badblocks.count == 0)
1786 /* Nothing to do for bad blocks*/ ;
1787 else if (sb->bblog_offset == 0)
1788 /* Cannot record bad blocks on this device */
1789 md_error(mddev, rdev);
1790 else {
1791 struct badblocks *bb = &rdev->badblocks;
1792 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1793 u64 *p = bb->page;
1794 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1795 if (bb->changed) {
1796 unsigned seq;
1797
1798retry:
1799 seq = read_seqbegin(&bb->lock);
1800
1801 memset(bbp, 0xff, PAGE_SIZE);
1802
1803 for (i = 0 ; i < bb->count ; i++) {
1804 u64 internal_bb = *p++;
1805 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1806 | BB_LEN(internal_bb));
1807 *bbp++ = cpu_to_le64(store_bb);
1808 }
1809 if (read_seqretry(&bb->lock, seq))
1810 goto retry;
1811
1812 bb->sector = (rdev->sb_start +
1813 (int)le32_to_cpu(sb->bblog_offset));
1814 bb->size = le16_to_cpu(sb->bblog_size);
1815 bb->changed = 0;
1816 }
1817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001820 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (rdev2->desc_nr+1 > max_dev)
1822 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001823
NeilBrown70471da2009-08-03 10:59:57 +10001824 if (max_dev > le32_to_cpu(sb->max_dev)) {
1825 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001826 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001827 rdev->sb_size = max_dev * 2 + 256;
1828 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1829 if (rdev->sb_size & bmask)
1830 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001831 } else
1832 max_dev = le32_to_cpu(sb->max_dev);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 for (i=0; i<max_dev;i++)
1835 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1836
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001837 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001839 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001841 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001843 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001844 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 else
1846 sb->dev_roles[i] = cpu_to_le16(0xffff);
1847 }
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 sb->sb_csum = calc_sb_1_csum(sb);
1850}
1851
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001853super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854{
1855 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001856 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001857 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001859 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001860 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001861 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001862 max_sectors -= rdev->data_offset;
1863 if (!num_sectors || num_sectors > max_sectors)
1864 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001865 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001866 /* minor version 0 with bitmap we can't move */
1867 return 0;
1868 } else {
1869 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001870 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001871 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001872 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001873 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001874 if (!num_sectors || num_sectors > max_sectors)
1875 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001876 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001878 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001879 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001880 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001882 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883 rdev->sb_page);
1884 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001885 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Adrian Bunk75c96f82005-05-05 16:16:09 -07001888static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 [0] = {
1890 .name = "0.90.0",
1891 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001892 .load_super = super_90_load,
1893 .validate_super = super_90_validate,
1894 .sync_super = super_90_sync,
1895 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 },
1897 [1] = {
1898 .name = "md-1",
1899 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 .load_super = super_1_load,
1901 .validate_super = super_1_validate,
1902 .sync_super = super_1_sync,
1903 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 },
1905};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
NeilBrownfd01b882011-10-11 16:47:53 +11001907static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001908{
1909 if (mddev->sync_super) {
1910 mddev->sync_super(mddev, rdev);
1911 return;
1912 }
1913
1914 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1915
1916 super_types[mddev->major_version].sync_super(mddev, rdev);
1917}
1918
NeilBrownfd01b882011-10-11 16:47:53 +11001919static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
NeilBrown3cb03002011-10-11 16:45:26 +11001921 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
NeilBrown4b809912008-07-21 17:05:25 +10001923 rcu_read_lock();
1924 rdev_for_each_rcu(rdev, mddev1)
1925 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001926 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001927 rdev2->bdev->bd_contains) {
1928 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001929 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001930 }
1931 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return 0;
1933}
1934
1935static LIST_HEAD(pending_raid_disks);
1936
Andre Nollac5e7112009-08-03 10:59:47 +10001937/*
1938 * Try to register data integrity profile for an mddev
1939 *
1940 * This is called when an array is started and after a disk has been kicked
1941 * from the array. It only succeeds if all working and active component devices
1942 * are integrity capable with matching profiles.
1943 */
NeilBrownfd01b882011-10-11 16:47:53 +11001944int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001945{
NeilBrown3cb03002011-10-11 16:45:26 +11001946 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001947
Andre Nollac5e7112009-08-03 10:59:47 +10001948 if (list_empty(&mddev->disks))
1949 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001950 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1951 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001952 list_for_each_entry(rdev, &mddev->disks, same_set) {
1953 /* skip spares and non-functional disks */
1954 if (test_bit(Faulty, &rdev->flags))
1955 continue;
1956 if (rdev->raid_disk < 0)
1957 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001958 if (!reference) {
1959 /* Use the first rdev as the reference */
1960 reference = rdev;
1961 continue;
1962 }
1963 /* does this rdev's profile match the reference profile? */
1964 if (blk_integrity_compare(reference->bdev->bd_disk,
1965 rdev->bdev->bd_disk) < 0)
1966 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001967 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001968 if (!reference || !bdev_get_integrity(reference->bdev))
1969 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001970 /*
1971 * All component devices are integrity capable and have matching
1972 * profiles, register the common profile for the md device.
1973 */
1974 if (blk_integrity_register(mddev->gendisk,
1975 bdev_get_integrity(reference->bdev)) != 0) {
1976 printk(KERN_ERR "md: failed to register integrity for %s\n",
1977 mdname(mddev));
1978 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001979 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001980 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1981 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1982 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1983 mdname(mddev));
1984 return -EINVAL;
1985 }
Andre Nollac5e7112009-08-03 10:59:47 +10001986 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001987}
Andre Nollac5e7112009-08-03 10:59:47 +10001988EXPORT_SYMBOL(md_integrity_register);
1989
1990/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001991void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001992{
1993 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1994 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1995
1996 if (!bi_mddev) /* nothing to do */
1997 return;
1998 if (rdev->raid_disk < 0) /* skip spares */
1999 return;
2000 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2001 rdev->bdev->bd_disk) >= 0)
2002 return;
2003 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2004 blk_integrity_unregister(mddev->gendisk);
2005}
2006EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002007
NeilBrownfd01b882011-10-11 16:47:53 +11002008static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002010 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002011 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002012 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002013 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if (rdev->mddev) {
2016 MD_BUG();
2017 return -EINVAL;
2018 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002019
2020 /* prevent duplicates */
2021 if (find_rdev(mddev, rdev->bdev->bd_dev))
2022 return -EEXIST;
2023
Andre Nolldd8ac332009-03-31 14:33:13 +11002024 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2025 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2026 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002027 if (mddev->pers) {
2028 /* Cannot change size, so fail
2029 * If mddev->level <= 0, then we don't care
2030 * about aligning sizes (e.g. linear)
2031 */
2032 if (mddev->level > 0)
2033 return -ENOSPC;
2034 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002035 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* Verify rdev->desc_nr is unique.
2039 * If it is -1, assign a free number, else
2040 * check number is not in use
2041 */
2042 if (rdev->desc_nr < 0) {
2043 int choice = 0;
2044 if (mddev->pers) choice = mddev->raid_disks;
2045 while (find_rdev_nr(mddev, choice))
2046 choice++;
2047 rdev->desc_nr = choice;
2048 } else {
2049 if (find_rdev_nr(mddev, rdev->desc_nr))
2050 return -EBUSY;
2051 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002052 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2053 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2054 mdname(mddev), mddev->max_disks);
2055 return -EBUSY;
2056 }
NeilBrown19133a42005-11-08 21:39:35 -08002057 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002058 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002059 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002062 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002063
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002064 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002065 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002066
Tejun Heo0762b8b2008-08-25 19:56:12 +09002067 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002068 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2069 /* failure here is OK */;
2070 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002071
NeilBrown4b809912008-07-21 17:05:25 +10002072 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002073 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002074
2075 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002076 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002079
2080 fail:
2081 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2082 b, mdname(mddev));
2083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
NeilBrown177a99b2008-02-06 01:39:56 -08002086static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002087{
NeilBrown3cb03002011-10-11 16:45:26 +11002088 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002089 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002090 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002091}
2092
NeilBrown3cb03002011-10-11 16:45:26 +11002093static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 char b[BDEVNAME_SIZE];
2096 if (!rdev->mddev) {
2097 MD_BUG();
2098 return;
2099 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002100 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002101 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2103 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002104 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002105 sysfs_put(rdev->sysfs_state);
2106 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002107 kfree(rdev->badblocks.page);
2108 rdev->badblocks.count = 0;
2109 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002110 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002111 * writing to 'remove' to "dev/state". We also need
2112 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002113 */
NeilBrown4b809912008-07-21 17:05:25 +10002114 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002115 INIT_WORK(&rdev->del_work, md_delayed_delete);
2116 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002117 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/*
2121 * prevent the device from being mounted, repartitioned or
2122 * otherwise reused by a RAID array (or any other kernel
2123 * subsystem), by bd_claiming the device.
2124 */
NeilBrown3cb03002011-10-11 16:45:26 +11002125static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 int err = 0;
2128 struct block_device *bdev;
2129 char b[BDEVNAME_SIZE];
2130
Tejun Heod4d77622010-11-13 11:55:18 +01002131 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002132 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (IS_ERR(bdev)) {
2134 printk(KERN_ERR "md: could not open %s.\n",
2135 __bdevname(dev, b));
2136 return PTR_ERR(bdev);
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 rdev->bdev = bdev;
2139 return err;
2140}
2141
NeilBrown3cb03002011-10-11 16:45:26 +11002142static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 struct block_device *bdev = rdev->bdev;
2145 rdev->bdev = NULL;
2146 if (!bdev)
2147 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002148 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151void md_autodetect_dev(dev_t dev);
2152
NeilBrown3cb03002011-10-11 16:45:26 +11002153static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 char b[BDEVNAME_SIZE];
2156 printk(KERN_INFO "md: export_rdev(%s)\n",
2157 bdevname(rdev->bdev,b));
2158 if (rdev->mddev)
2159 MD_BUG();
2160 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002162 if (test_bit(AutoDetected, &rdev->flags))
2163 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164#endif
2165 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002166 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
NeilBrown3cb03002011-10-11 16:45:26 +11002169static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 unbind_rdev_from_array(rdev);
2172 export_rdev(rdev);
2173}
2174
NeilBrownfd01b882011-10-11 16:47:53 +11002175static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
NeilBrown3cb03002011-10-11 16:45:26 +11002177 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
NeilBrownd089c6a2008-02-06 01:39:59 -08002179 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!rdev->mddev) {
2181 MD_BUG();
2182 continue;
2183 }
2184 kick_rdev_from_array(rdev);
2185 }
2186 if (!list_empty(&mddev->disks))
2187 MD_BUG();
2188 mddev->raid_disks = 0;
2189 mddev->major_version = 0;
2190}
2191
2192static void print_desc(mdp_disk_t *desc)
2193{
2194 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2195 desc->major,desc->minor,desc->raid_disk,desc->state);
2196}
2197
Cheng Renquancd2ac932009-01-09 08:31:08 +11002198static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 int i;
2201
2202 printk(KERN_INFO
2203 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2204 sb->major_version, sb->minor_version, sb->patch_version,
2205 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2206 sb->ctime);
2207 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2208 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2209 sb->md_minor, sb->layout, sb->chunk_size);
2210 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2211 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2212 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2213 sb->failed_disks, sb->spare_disks,
2214 sb->sb_csum, (unsigned long)sb->events_lo);
2215
2216 printk(KERN_INFO);
2217 for (i = 0; i < MD_SB_DISKS; i++) {
2218 mdp_disk_t *desc;
2219
2220 desc = sb->disks + i;
2221 if (desc->number || desc->major || desc->minor ||
2222 desc->raid_disk || (desc->state && (desc->state != 4))) {
2223 printk(" D %2d: ", i);
2224 print_desc(desc);
2225 }
2226 }
2227 printk(KERN_INFO "md: THIS: ");
2228 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
Cheng Renquancd2ac932009-01-09 08:31:08 +11002231static void print_sb_1(struct mdp_superblock_1 *sb)
2232{
2233 __u8 *uuid;
2234
2235 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002236 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002237 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002238 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002239 le32_to_cpu(sb->major_version),
2240 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002241 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002242 sb->set_name,
2243 (unsigned long long)le64_to_cpu(sb->ctime)
2244 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2245
2246 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002247 printk(KERN_INFO
2248 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002249 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002250 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002251 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2252 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002253 le32_to_cpu(sb->level),
2254 (unsigned long long)le64_to_cpu(sb->size),
2255 le32_to_cpu(sb->raid_disks),
2256 le32_to_cpu(sb->layout),
2257 le32_to_cpu(sb->chunksize),
2258 (unsigned long long)le64_to_cpu(sb->data_offset),
2259 (unsigned long long)le64_to_cpu(sb->data_size),
2260 (unsigned long long)le64_to_cpu(sb->super_offset),
2261 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2262 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002263 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002264 sb->devflags,
2265 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2266 (unsigned long long)le64_to_cpu(sb->events),
2267 (unsigned long long)le64_to_cpu(sb->resync_offset),
2268 le32_to_cpu(sb->sb_csum),
2269 le32_to_cpu(sb->max_dev)
2270 );
2271}
2272
NeilBrown3cb03002011-10-11 16:45:26 +11002273static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002276 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2277 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002278 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2279 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002281 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2282 switch (major_version) {
2283 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002284 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002285 break;
2286 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002287 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002288 break;
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 } else
2291 printk(KERN_INFO "md: no rdev superblock!\n");
2292}
2293
Adrian Bunk5e563412006-06-26 00:27:42 -07002294static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002296 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002297 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002298 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 char b[BDEVNAME_SIZE];
2300
2301 printk("\n");
2302 printk("md: **********************************\n");
2303 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2304 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002305 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
NeilBrown32a76272005-06-21 17:17:14 -07002307 if (mddev->bitmap)
2308 bitmap_print_sb(mddev->bitmap);
2309 else
2310 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002311 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 printk("<%s>", bdevname(rdev->bdev,b));
2313 printk("\n");
2314
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002315 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002316 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318 printk("md: **********************************\n");
2319 printk("\n");
2320}
2321
2322
NeilBrownfd01b882011-10-11 16:47:53 +11002323static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
NeilBrown42543762006-06-26 00:27:57 -07002325 /* Update each superblock (in-memory image), but
2326 * if we are allowed to, skip spares which already
2327 * have the right event counter, or have one earlier
2328 * (which would mean they aren't being marked as dirty
2329 * with the rest of the array)
2330 */
NeilBrown3cb03002011-10-11 16:45:26 +11002331 struct md_rdev *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002332 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002333 if (rdev->sb_events == mddev->events ||
2334 (nospares &&
2335 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002336 rdev->sb_events+1 == mddev->events)) {
2337 /* Don't update this superblock */
2338 rdev->sb_loaded = 2;
2339 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002340 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002341 rdev->sb_loaded = 1;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344}
2345
NeilBrownfd01b882011-10-11 16:47:53 +11002346static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
NeilBrown3cb03002011-10-11 16:45:26 +11002348 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002349 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002350 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002351 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002354 /* First make sure individual recovery_offsets are correct */
2355 list_for_each_entry(rdev, &mddev->disks, same_set) {
2356 if (rdev->raid_disk >= 0 &&
2357 mddev->delta_disks >= 0 &&
2358 !test_bit(In_sync, &rdev->flags) &&
2359 mddev->curr_resync_completed > rdev->recovery_offset)
2360 rdev->recovery_offset = mddev->curr_resync_completed;
2361
2362 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002363 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002364 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002365 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002366 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002367 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002368 list_for_each_entry(rdev, &mddev->disks, same_set) {
2369 if (rdev->badblocks.changed) {
2370 md_ack_all_badblocks(&rdev->badblocks);
2371 md_error(mddev, rdev);
2372 }
2373 clear_bit(Blocked, &rdev->flags);
2374 clear_bit(BlockedBadBlocks, &rdev->flags);
2375 wake_up(&rdev->blocked_wait);
2376 }
2377 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002378 wake_up(&mddev->sb_wait);
2379 return;
2380 }
2381
NeilBrowna9701a32005-11-08 21:39:34 -08002382 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002383
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002384 mddev->utime = get_seconds();
2385
NeilBrown850b2b422006-10-03 01:15:46 -07002386 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2387 force_change = 1;
2388 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2389 /* just a clean<-> dirty transition, possibly leave spares alone,
2390 * though if events isn't the right even/odd, we will have to do
2391 * spares after all
2392 */
2393 nospares = 1;
2394 if (force_change)
2395 nospares = 0;
2396 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002397 /* If the array is degraded, then skipping spares is both
2398 * dangerous and fairly pointless.
2399 * Dangerous because a device that was removed from the array
2400 * might have a event_count that still looks up-to-date,
2401 * so it can be re-added without a resync.
2402 * Pointless because if there are any spares to skip,
2403 * then a recovery will happen and soon that array won't
2404 * be degraded any more and the spare can go back to sleep then.
2405 */
NeilBrown850b2b422006-10-03 01:15:46 -07002406 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002407
NeilBrown06d91a52005-06-21 17:17:12 -07002408 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002409
2410 /* If this is just a dirty<->clean transition, and the array is clean
2411 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002412 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002413 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002414 && mddev->can_decrease_events
2415 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002416 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002417 mddev->can_decrease_events = 0;
2418 } else {
NeilBrown42543762006-06-26 00:27:57 -07002419 /* otherwise we have to go forward and ... */
2420 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002421 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 if (!mddev->events) {
2425 /*
2426 * oops, this 64-bit counter should never wrap.
2427 * Either we are in around ~1 trillion A.C., assuming
2428 * 1 reboot per second, or we have a bug:
2429 */
2430 MD_BUG();
2431 mddev->events --;
2432 }
NeilBrown2699b672011-07-28 11:31:47 +10002433
NeilBrownde393cd2011-07-28 11:31:48 +10002434 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002435 if (rdev->badblocks.changed)
2436 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002437 if (test_bit(Faulty, &rdev->flags))
2438 set_bit(FaultRecorded, &rdev->flags);
2439 }
NeilBrown2699b672011-07-28 11:31:47 +10002440
NeilBrowne6910632008-02-06 01:39:51 -08002441 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002442 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
NeilBrown36a4e1f2011-10-07 14:23:17 +11002444 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2445 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
NeilBrown4ad13662007-07-17 04:06:13 -07002447 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002448 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002450
NeilBrown42543762006-06-26 00:27:57 -07002451 if (rdev->sb_loaded != 1)
2452 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002454 if (!test_bit(Faulty, &rdev->flags) &&
2455 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002456 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002457 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002458 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002459 pr_debug("md: (write) %s's sb offset: %llu\n",
2460 bdevname(rdev->bdev, b),
2461 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002462 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002463 if (rdev->badblocks.size) {
2464 md_super_write(mddev, rdev,
2465 rdev->badblocks.sector,
2466 rdev->badblocks.size << 9,
2467 rdev->bb_page);
2468 rdev->badblocks.size = 0;
2469 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002470
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002471 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002472 pr_debug("md: %s (skipping faulty)\n",
2473 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002474 else
2475 pr_debug("(skipping incremental s/r ");
2476
NeilBrown7bfa19f2005-06-21 17:17:28 -07002477 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* only need to write one superblock... */
2479 break;
2480 }
NeilBrowna9701a32005-11-08 21:39:34 -08002481 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002482 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002483
NeilBrowna9701a32005-11-08 21:39:34 -08002484 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002485 if (mddev->in_sync != sync_req ||
2486 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002487 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002488 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002489 goto repeat;
2490 }
NeilBrown850b2b422006-10-03 01:15:46 -07002491 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002492 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002493 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002494 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2495 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002496
NeilBrownde393cd2011-07-28 11:31:48 +10002497 list_for_each_entry(rdev, &mddev->disks, same_set) {
2498 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2499 clear_bit(Blocked, &rdev->flags);
2500
2501 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002502 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002503 clear_bit(BlockedBadBlocks, &rdev->flags);
2504 wake_up(&rdev->blocked_wait);
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Andre Noll7f6ce762008-03-23 18:34:54 +01002508/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002509 * We want to accept with case. For this we use cmd_match.
2510 */
2511static int cmd_match(const char *cmd, const char *str)
2512{
2513 /* See if cmd, written into a sysfs file, matches
2514 * str. They must either be the same, or cmd can
2515 * have a trailing newline
2516 */
2517 while (*cmd && *str && *cmd == *str) {
2518 cmd++;
2519 str++;
2520 }
2521 if (*cmd == '\n')
2522 cmd++;
2523 if (*str || *cmd)
2524 return 0;
2525 return 1;
2526}
2527
NeilBrown86e6ffd2005-11-08 21:39:24 -08002528struct rdev_sysfs_entry {
2529 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002530 ssize_t (*show)(struct md_rdev *, char *);
2531 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002532};
2533
2534static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002535state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002536{
2537 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002538 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002539
NeilBrownde393cd2011-07-28 11:31:48 +10002540 if (test_bit(Faulty, &rdev->flags) ||
2541 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002542 len+= sprintf(page+len, "%sfaulty",sep);
2543 sep = ",";
2544 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002545 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002546 len += sprintf(page+len, "%sin_sync",sep);
2547 sep = ",";
2548 }
NeilBrownf6556752006-06-26 00:28:01 -07002549 if (test_bit(WriteMostly, &rdev->flags)) {
2550 len += sprintf(page+len, "%swrite_mostly",sep);
2551 sep = ",";
2552 }
NeilBrownde393cd2011-07-28 11:31:48 +10002553 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002554 (rdev->badblocks.unacked_exist
2555 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002556 len += sprintf(page+len, "%sblocked", sep);
2557 sep = ",";
2558 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002559 if (!test_bit(Faulty, &rdev->flags) &&
2560 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561 len += sprintf(page+len, "%sspare", sep);
2562 sep = ",";
2563 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002564 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2565 len += sprintf(page+len, "%swrite_error", sep);
2566 sep = ",";
2567 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002568 if (test_bit(WantReplacement, &rdev->flags)) {
2569 len += sprintf(page+len, "%swant_replacement", sep);
2570 sep = ",";
2571 }
2572 if (test_bit(Replacement, &rdev->flags)) {
2573 len += sprintf(page+len, "%sreplacement", sep);
2574 sep = ",";
2575 }
2576
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577 return len+sprintf(page+len, "\n");
2578}
2579
NeilBrown45dc2de2006-06-26 00:27:58 -07002580static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002581state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002582{
2583 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002584 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002585 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002586 * writemostly - sets write_mostly
2587 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002588 * blocked - sets the Blocked flags
2589 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002590 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002591 * write_error - sets WriteErrorSeen
2592 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002593 */
2594 int err = -EINVAL;
2595 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2596 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002597 if (test_bit(Faulty, &rdev->flags))
2598 err = 0;
2599 else
2600 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002601 } else if (cmd_match(buf, "remove")) {
2602 if (rdev->raid_disk >= 0)
2603 err = -EBUSY;
2604 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002605 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002606 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002607 if (mddev->pers)
2608 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002609 md_new_event(mddev);
2610 err = 0;
2611 }
NeilBrownf6556752006-06-26 00:28:01 -07002612 } else if (cmd_match(buf, "writemostly")) {
2613 set_bit(WriteMostly, &rdev->flags);
2614 err = 0;
2615 } else if (cmd_match(buf, "-writemostly")) {
2616 clear_bit(WriteMostly, &rdev->flags);
2617 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002618 } else if (cmd_match(buf, "blocked")) {
2619 set_bit(Blocked, &rdev->flags);
2620 err = 0;
2621 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002622 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002623 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002624 /* metadata handler doesn't understand badblocks,
2625 * so we need to fail the device
2626 */
2627 md_error(rdev->mddev, rdev);
2628 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002629 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002630 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002631 wake_up(&rdev->blocked_wait);
2632 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2633 md_wakeup_thread(rdev->mddev->thread);
2634
2635 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002636 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2637 set_bit(In_sync, &rdev->flags);
2638 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002639 } else if (cmd_match(buf, "write_error")) {
2640 set_bit(WriteErrorSeen, &rdev->flags);
2641 err = 0;
2642 } else if (cmd_match(buf, "-write_error")) {
2643 clear_bit(WriteErrorSeen, &rdev->flags);
2644 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002645 } else if (cmd_match(buf, "want_replacement")) {
2646 /* Any non-spare device that is not a replacement can
2647 * become want_replacement at any time, but we then need to
2648 * check if recovery is needed.
2649 */
2650 if (rdev->raid_disk >= 0 &&
2651 !test_bit(Replacement, &rdev->flags))
2652 set_bit(WantReplacement, &rdev->flags);
2653 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2654 md_wakeup_thread(rdev->mddev->thread);
2655 err = 0;
2656 } else if (cmd_match(buf, "-want_replacement")) {
2657 /* Clearing 'want_replacement' is always allowed.
2658 * Once replacements starts it is too late though.
2659 */
2660 err = 0;
2661 clear_bit(WantReplacement, &rdev->flags);
2662 } else if (cmd_match(buf, "replacement")) {
2663 /* Can only set a device as a replacement when array has not
2664 * yet been started. Once running, replacement is automatic
2665 * from spares, or by assigning 'slot'.
2666 */
2667 if (rdev->mddev->pers)
2668 err = -EBUSY;
2669 else {
2670 set_bit(Replacement, &rdev->flags);
2671 err = 0;
2672 }
2673 } else if (cmd_match(buf, "-replacement")) {
2674 /* Similarly, can only clear Replacement before start */
2675 if (rdev->mddev->pers)
2676 err = -EBUSY;
2677 else {
2678 clear_bit(Replacement, &rdev->flags);
2679 err = 0;
2680 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002681 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002682 if (!err)
2683 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002684 return err ? err : len;
2685}
NeilBrown80ca3a42006-07-10 04:44:18 -07002686static struct rdev_sysfs_entry rdev_state =
2687__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002688
2689static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002690errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002691{
2692 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2693}
2694
2695static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002696errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002697{
2698 char *e;
2699 unsigned long n = simple_strtoul(buf, &e, 10);
2700 if (*buf && (*e == 0 || *e == '\n')) {
2701 atomic_set(&rdev->corrected_errors, n);
2702 return len;
2703 }
2704 return -EINVAL;
2705}
2706static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002707__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002708
NeilBrown014236d2006-01-06 00:20:55 -08002709static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002710slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002711{
2712 if (rdev->raid_disk < 0)
2713 return sprintf(page, "none\n");
2714 else
2715 return sprintf(page, "%d\n", rdev->raid_disk);
2716}
2717
2718static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002719slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002720{
2721 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002722 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002723 int slot = simple_strtoul(buf, &e, 10);
2724 if (strncmp(buf, "none", 4)==0)
2725 slot = -1;
2726 else if (e==buf || (*e && *e!= '\n'))
2727 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002728 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002729 /* Setting 'slot' on an active array requires also
2730 * updating the 'rd%d' link, and communicating
2731 * with the personality with ->hot_*_disk.
2732 * For now we only support removing
2733 * failed/spare devices. This normally happens automatically,
2734 * but not when the metadata is externally managed.
2735 */
NeilBrownc303da62008-02-06 01:39:51 -08002736 if (rdev->raid_disk == -1)
2737 return -EEXIST;
2738 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002739 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002740 return -EINVAL;
2741 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002742 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002743 if (err)
2744 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002745 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002746 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002747 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2748 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002749 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002750 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002751 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002752 */
2753
2754 if (rdev->raid_disk != -1)
2755 return -EBUSY;
2756
NeilBrownc6751b22011-02-02 11:57:13 +11002757 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2758 return -EBUSY;
2759
Neil Brown6c2fce22008-06-28 08:31:31 +10002760 if (rdev->mddev->pers->hot_add_disk == NULL)
2761 return -EINVAL;
2762
NeilBrownba1b41b2011-01-14 09:14:34 +11002763 if (slot >= rdev->mddev->raid_disks &&
2764 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2765 return -ENOSPC;
2766
Neil Brown6c2fce22008-06-28 08:31:31 +10002767 rdev->raid_disk = slot;
2768 if (test_bit(In_sync, &rdev->flags))
2769 rdev->saved_raid_disk = slot;
2770 else
2771 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002772 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002773 err = rdev->mddev->pers->
2774 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002775 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 return err;
Neil Brown52664732008-06-28 08:31:44 +10002778 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002779 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002780 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002781 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002782 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002783 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002784 if (slot >= rdev->mddev->raid_disks &&
2785 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002786 return -ENOSPC;
2787 rdev->raid_disk = slot;
2788 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002789 clear_bit(Faulty, &rdev->flags);
2790 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002791 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002792 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002793 }
NeilBrown014236d2006-01-06 00:20:55 -08002794 return len;
2795}
2796
2797
2798static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002799__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002800
NeilBrown93c8cad2006-01-06 00:20:56 -08002801static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002802offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002803{
Andrew Morton6961ece2006-01-06 00:20:59 -08002804 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002805}
2806
2807static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002808offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002809{
2810 char *e;
2811 unsigned long long offset = simple_strtoull(buf, &e, 10);
2812 if (e==buf || (*e && *e != '\n'))
2813 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002814 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002815 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002816 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002817 /* Must set offset before size, so overlap checks
2818 * can be sane */
2819 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002820 rdev->data_offset = offset;
2821 return len;
2822}
2823
2824static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002825__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002826
NeilBrown83303b62006-01-06 00:21:06 -08002827static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002828rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002829{
Andre Nolldd8ac332009-03-31 14:33:13 +11002830 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002831}
2832
NeilBrownc5d79ad2008-02-06 01:39:54 -08002833static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2834{
2835 /* check if two start/length pairs overlap */
2836 if (s1+l1 <= s2)
2837 return 0;
2838 if (s2+l2 <= s1)
2839 return 0;
2840 return 1;
2841}
2842
Dan Williamsb522adc2009-03-31 15:00:31 +11002843static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2844{
2845 unsigned long long blocks;
2846 sector_t new;
2847
2848 if (strict_strtoull(buf, 10, &blocks) < 0)
2849 return -EINVAL;
2850
2851 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2852 return -EINVAL; /* sector conversion overflow */
2853
2854 new = blocks * 2;
2855 if (new != blocks * 2)
2856 return -EINVAL; /* unsigned long long to sector_t overflow */
2857
2858 *sectors = new;
2859 return 0;
2860}
2861
NeilBrown83303b62006-01-06 00:21:06 -08002862static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002863rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002864{
NeilBrownfd01b882011-10-11 16:47:53 +11002865 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002866 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002867 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002868
Dan Williamsb522adc2009-03-31 15:00:31 +11002869 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002870 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002871 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002872 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002873 sectors = super_types[my_mddev->major_version].
2874 rdev_size_change(rdev, sectors);
2875 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002876 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002877 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002878 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002879 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002880 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002881 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002882 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002883
Andre Nolldd8ac332009-03-31 14:33:13 +11002884 rdev->sectors = sectors;
2885 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002886 /* need to check that all other rdevs with the same ->bdev
2887 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002888 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002889 * we have to change it back, we will have the lock again.
2890 */
NeilBrownfd01b882011-10-11 16:47:53 +11002891 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002892 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002893 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002894
NeilBrown27c529b2008-03-04 14:29:33 -08002895 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002896 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002897 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002898
2899 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002900 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002901 if (rdev->bdev == rdev2->bdev &&
2902 rdev != rdev2 &&
2903 overlaps(rdev->data_offset, rdev->sectors,
2904 rdev2->data_offset,
2905 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002906 overlap = 1;
2907 break;
2908 }
2909 mddev_unlock(mddev);
2910 if (overlap) {
2911 mddev_put(mddev);
2912 break;
2913 }
2914 }
NeilBrown27c529b2008-03-04 14:29:33 -08002915 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002916 if (overlap) {
2917 /* Someone else could have slipped in a size
2918 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002919 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002920 * safe, and trust userspace not to race with
2921 * itself
2922 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002923 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002924 return -EBUSY;
2925 }
2926 }
NeilBrown83303b62006-01-06 00:21:06 -08002927 return len;
2928}
2929
2930static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002931__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002932
Dan Williams06e3c812009-12-12 21:17:12 -07002933
NeilBrown3cb03002011-10-11 16:45:26 +11002934static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002935{
2936 unsigned long long recovery_start = rdev->recovery_offset;
2937
2938 if (test_bit(In_sync, &rdev->flags) ||
2939 recovery_start == MaxSector)
2940 return sprintf(page, "none\n");
2941
2942 return sprintf(page, "%llu\n", recovery_start);
2943}
2944
NeilBrown3cb03002011-10-11 16:45:26 +11002945static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002946{
2947 unsigned long long recovery_start;
2948
2949 if (cmd_match(buf, "none"))
2950 recovery_start = MaxSector;
2951 else if (strict_strtoull(buf, 10, &recovery_start))
2952 return -EINVAL;
2953
2954 if (rdev->mddev->pers &&
2955 rdev->raid_disk >= 0)
2956 return -EBUSY;
2957
2958 rdev->recovery_offset = recovery_start;
2959 if (recovery_start == MaxSector)
2960 set_bit(In_sync, &rdev->flags);
2961 else
2962 clear_bit(In_sync, &rdev->flags);
2963 return len;
2964}
2965
2966static struct rdev_sysfs_entry rdev_recovery_start =
2967__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2968
NeilBrown16c791a2011-07-28 11:31:47 +10002969
2970static ssize_t
2971badblocks_show(struct badblocks *bb, char *page, int unack);
2972static ssize_t
2973badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2974
NeilBrown3cb03002011-10-11 16:45:26 +11002975static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002976{
2977 return badblocks_show(&rdev->badblocks, page, 0);
2978}
NeilBrown3cb03002011-10-11 16:45:26 +11002979static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002980{
NeilBrownde393cd2011-07-28 11:31:48 +10002981 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2982 /* Maybe that ack was all we needed */
2983 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2984 wake_up(&rdev->blocked_wait);
2985 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002986}
2987static struct rdev_sysfs_entry rdev_bad_blocks =
2988__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2989
2990
NeilBrown3cb03002011-10-11 16:45:26 +11002991static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002992{
2993 return badblocks_show(&rdev->badblocks, page, 1);
2994}
NeilBrown3cb03002011-10-11 16:45:26 +11002995static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002996{
2997 return badblocks_store(&rdev->badblocks, page, len, 1);
2998}
2999static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3000__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3001
NeilBrown86e6ffd2005-11-08 21:39:24 -08003002static struct attribute *rdev_default_attrs[] = {
3003 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003004 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003005 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003006 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003007 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003008 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003009 &rdev_bad_blocks.attr,
3010 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003011 NULL,
3012};
3013static ssize_t
3014rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3015{
3016 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003017 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003018 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003019 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003020
3021 if (!entry->show)
3022 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003023
3024 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3025 if (!rv) {
3026 if (rdev->mddev == NULL)
3027 rv = -EBUSY;
3028 else
3029 rv = entry->show(rdev, page);
3030 mddev_unlock(mddev);
3031 }
3032 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003033}
3034
3035static ssize_t
3036rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3037 const char *page, size_t length)
3038{
3039 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003040 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003041 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003042 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003043
3044 if (!entry->store)
3045 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003046 if (!capable(CAP_SYS_ADMIN))
3047 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003048 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003049 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003050 if (rdev->mddev == NULL)
3051 rv = -EBUSY;
3052 else
3053 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003054 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003055 }
3056 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003057}
3058
3059static void rdev_free(struct kobject *ko)
3060{
NeilBrown3cb03002011-10-11 16:45:26 +11003061 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003062 kfree(rdev);
3063}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003064static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003065 .show = rdev_attr_show,
3066 .store = rdev_attr_store,
3067};
3068static struct kobj_type rdev_ktype = {
3069 .release = rdev_free,
3070 .sysfs_ops = &rdev_sysfs_ops,
3071 .default_attrs = rdev_default_attrs,
3072};
3073
NeilBrown3cb03002011-10-11 16:45:26 +11003074int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003075{
3076 rdev->desc_nr = -1;
3077 rdev->saved_raid_disk = -1;
3078 rdev->raid_disk = -1;
3079 rdev->flags = 0;
3080 rdev->data_offset = 0;
3081 rdev->sb_events = 0;
3082 rdev->last_read_error.tv_sec = 0;
3083 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003084 rdev->sb_loaded = 0;
3085 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003086 atomic_set(&rdev->nr_pending, 0);
3087 atomic_set(&rdev->read_errors, 0);
3088 atomic_set(&rdev->corrected_errors, 0);
3089
3090 INIT_LIST_HEAD(&rdev->same_set);
3091 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003092
3093 /* Add space to store bad block list.
3094 * This reserves the space even on arrays where it cannot
3095 * be used - I wonder if that matters
3096 */
3097 rdev->badblocks.count = 0;
3098 rdev->badblocks.shift = 0;
3099 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3100 seqlock_init(&rdev->badblocks.lock);
3101 if (rdev->badblocks.page == NULL)
3102 return -ENOMEM;
3103
3104 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003105}
3106EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107/*
3108 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3109 *
3110 * mark the device faulty if:
3111 *
3112 * - the device is nonexistent (zero size)
3113 * - the device has no valid superblock
3114 *
3115 * a faulty rdev _never_ has rdev->sb set.
3116 */
NeilBrown3cb03002011-10-11 16:45:26 +11003117static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 char b[BDEVNAME_SIZE];
3120 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003121 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 sector_t size;
3123
NeilBrown9ffae0c2006-01-06 00:20:32 -08003124 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (!rdev) {
3126 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3127 return ERR_PTR(-ENOMEM);
3128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
NeilBrown2230dfe2011-07-28 11:31:46 +10003130 err = md_rdev_init(rdev);
3131 if (err)
3132 goto abort_free;
3133 err = alloc_disk_sb(rdev);
3134 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 goto abort_free;
3136
NeilBrownc5d79ad2008-02-06 01:39:54 -08003137 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (err)
3139 goto abort_free;
3140
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003141 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003142
Mike Snitzer77304d22010-11-08 14:39:12 +01003143 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (!size) {
3145 printk(KERN_WARNING
3146 "md: %s has zero or unknown size, marking faulty!\n",
3147 bdevname(rdev->bdev,b));
3148 err = -EINVAL;
3149 goto abort_free;
3150 }
3151
3152 if (super_format >= 0) {
3153 err = super_types[super_format].
3154 load_super(rdev, NULL, super_minor);
3155 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003156 printk(KERN_WARNING
3157 "md: %s does not have a valid v%d.%d "
3158 "superblock, not importing!\n",
3159 bdevname(rdev->bdev,b),
3160 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto abort_free;
3162 }
3163 if (err < 0) {
3164 printk(KERN_WARNING
3165 "md: could not read %s's sb, not importing!\n",
3166 bdevname(rdev->bdev,b));
3167 goto abort_free;
3168 }
3169 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003170 if (super_format == -1)
3171 /* hot-add for 0.90, or non-persistent: so no badblocks */
3172 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003173
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return rdev;
3175
3176abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003177 if (rdev->bdev)
3178 unlock_rdev(rdev);
3179 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003180 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 kfree(rdev);
3182 return ERR_PTR(err);
3183}
3184
3185/*
3186 * Check a full RAID array for plausibility
3187 */
3188
3189
NeilBrownfd01b882011-10-11 16:47:53 +11003190static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
3192 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003193 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 char b[BDEVNAME_SIZE];
3195
3196 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003197 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 switch (super_types[mddev->major_version].
3199 load_super(rdev, freshest, mddev->minor_version)) {
3200 case 1:
3201 freshest = rdev;
3202 break;
3203 case 0:
3204 break;
3205 default:
3206 printk( KERN_ERR \
3207 "md: fatal superblock inconsistency in %s"
3208 " -- removing from array\n",
3209 bdevname(rdev->bdev,b));
3210 kick_rdev_from_array(rdev);
3211 }
3212
3213
3214 super_types[mddev->major_version].
3215 validate_super(mddev, freshest);
3216
3217 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003218 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003219 if (mddev->max_disks &&
3220 (rdev->desc_nr >= mddev->max_disks ||
3221 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003222 printk(KERN_WARNING
3223 "md: %s: %s: only %d devices permitted\n",
3224 mdname(mddev), bdevname(rdev->bdev, b),
3225 mddev->max_disks);
3226 kick_rdev_from_array(rdev);
3227 continue;
3228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (rdev != freshest)
3230 if (super_types[mddev->major_version].
3231 validate_super(mddev, rdev)) {
3232 printk(KERN_WARNING "md: kicking non-fresh %s"
3233 " from array!\n",
3234 bdevname(rdev->bdev,b));
3235 kick_rdev_from_array(rdev);
3236 continue;
3237 }
3238 if (mddev->level == LEVEL_MULTIPATH) {
3239 rdev->desc_nr = i++;
3240 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003241 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003242 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003243 rdev->raid_disk = -1;
3244 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
NeilBrown72e02072009-12-14 12:49:55 +11003249/* Read a fixed-point number.
3250 * Numbers in sysfs attributes should be in "standard" units where
3251 * possible, so time should be in seconds.
3252 * However we internally use a a much smaller unit such as
3253 * milliseconds or jiffies.
3254 * This function takes a decimal number with a possible fractional
3255 * component, and produces an integer which is the result of
3256 * multiplying that number by 10^'scale'.
3257 * all without any floating-point arithmetic.
3258 */
3259int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3260{
3261 unsigned long result = 0;
3262 long decimals = -1;
3263 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3264 if (*cp == '.')
3265 decimals = 0;
3266 else if (decimals < scale) {
3267 unsigned int value;
3268 value = *cp - '0';
3269 result = result * 10 + value;
3270 if (decimals >= 0)
3271 decimals++;
3272 }
3273 cp++;
3274 }
3275 if (*cp == '\n')
3276 cp++;
3277 if (*cp)
3278 return -EINVAL;
3279 if (decimals < 0)
3280 decimals = 0;
3281 while (decimals < scale) {
3282 result *= 10;
3283 decimals ++;
3284 }
3285 *res = result;
3286 return 0;
3287}
3288
3289
NeilBrown19052c02008-08-05 15:54:13 +10003290static void md_safemode_timeout(unsigned long data);
3291
NeilBrowneae17012005-11-08 21:39:23 -08003292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003293safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003294{
3295 int msec = (mddev->safemode_delay*1000)/HZ;
3296 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3297}
3298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003299safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003300{
NeilBrown16f17b32006-06-26 00:27:37 -07003301 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003302
NeilBrown72e02072009-12-14 12:49:55 +11003303 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003304 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003305 if (msec == 0)
3306 mddev->safemode_delay = 0;
3307 else {
NeilBrown19052c02008-08-05 15:54:13 +10003308 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003309 mddev->safemode_delay = (msec*HZ)/1000;
3310 if (mddev->safemode_delay == 0)
3311 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003312 if (mddev->safemode_delay < old_delay)
3313 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003314 }
3315 return len;
3316}
3317static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003318__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003319
3320static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003321level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003322{
NeilBrown84fc4b52011-10-11 16:49:58 +11003323 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003324 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003325 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003326 else if (mddev->clevel[0])
3327 return sprintf(page, "%s\n", mddev->clevel);
3328 else if (mddev->level != LEVEL_NONE)
3329 return sprintf(page, "%d\n", mddev->level);
3330 else
3331 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003332}
3333
NeilBrownd9d166c2006-01-06 00:20:51 -08003334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003335level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003336{
Dan Williamsf2859af2010-05-02 10:04:16 -07003337 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003338 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003339 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003340 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003341 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003342 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003343
3344 if (mddev->pers == NULL) {
3345 if (len == 0)
3346 return 0;
3347 if (len >= sizeof(mddev->clevel))
3348 return -ENOSPC;
3349 strncpy(mddev->clevel, buf, len);
3350 if (mddev->clevel[len-1] == '\n')
3351 len--;
3352 mddev->clevel[len] = 0;
3353 mddev->level = LEVEL_NONE;
3354 return rv;
3355 }
3356
3357 /* request to change the personality. Need to ensure:
3358 * - array is not engaged in resync/recovery/reshape
3359 * - old personality can be suspended
3360 * - new personality will access other array.
3361 */
3362
NeilBrownbb4f1e92010-08-08 21:18:03 +10003363 if (mddev->sync_thread ||
3364 mddev->reshape_position != MaxSector ||
3365 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003366 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003367
3368 if (!mddev->pers->quiesce) {
3369 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3370 mdname(mddev), mddev->pers->name);
3371 return -EINVAL;
3372 }
3373
3374 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003375 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003376 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003377 strncpy(clevel, buf, len);
3378 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003379 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003380 clevel[len] = 0;
3381 if (strict_strtol(clevel, 10, &level))
3382 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003383
Dan Williamsf2859af2010-05-02 10:04:16 -07003384 if (request_module("md-%s", clevel) != 0)
3385 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003386 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003387 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003388 if (!pers || !try_module_get(pers->owner)) {
3389 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003390 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003391 return -EINVAL;
3392 }
3393 spin_unlock(&pers_lock);
3394
3395 if (pers == mddev->pers) {
3396 /* Nothing to do! */
3397 module_put(pers->owner);
3398 return rv;
3399 }
3400 if (!pers->takeover) {
3401 module_put(pers->owner);
3402 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003403 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003404 return -EINVAL;
3405 }
3406
NeilBrowne93f68a2010-06-15 09:36:03 +01003407 list_for_each_entry(rdev, &mddev->disks, same_set)
3408 rdev->new_raid_disk = rdev->raid_disk;
3409
NeilBrown245f46c2009-03-31 14:39:39 +11003410 /* ->takeover must set new_* and/or delta_disks
3411 * if it succeeds, and may set them when it fails.
3412 */
3413 priv = pers->takeover(mddev);
3414 if (IS_ERR(priv)) {
3415 mddev->new_level = mddev->level;
3416 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003417 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003418 mddev->raid_disks -= mddev->delta_disks;
3419 mddev->delta_disks = 0;
3420 module_put(pers->owner);
3421 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003422 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003423 return PTR_ERR(priv);
3424 }
3425
3426 /* Looks like we have a winner */
3427 mddev_suspend(mddev);
3428 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003429
3430 if (mddev->pers->sync_request == NULL &&
3431 pers->sync_request != NULL) {
3432 /* need to add the md_redundancy_group */
3433 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3434 printk(KERN_WARNING
3435 "md: cannot register extra attributes for %s\n",
3436 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003437 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003438 }
3439 if (mddev->pers->sync_request != NULL &&
3440 pers->sync_request == NULL) {
3441 /* need to remove the md_redundancy_group */
3442 if (mddev->to_remove == NULL)
3443 mddev->to_remove = &md_redundancy_group;
3444 }
3445
Trela Maciej54071b32010-03-08 16:02:42 +11003446 if (mddev->pers->sync_request == NULL &&
3447 mddev->external) {
3448 /* We are converting from a no-redundancy array
3449 * to a redundancy array and metadata is managed
3450 * externally so we need to be sure that writes
3451 * won't block due to a need to transition
3452 * clean->dirty
3453 * until external management is started.
3454 */
3455 mddev->in_sync = 0;
3456 mddev->safemode_delay = 0;
3457 mddev->safemode = 0;
3458 }
3459
NeilBrowne93f68a2010-06-15 09:36:03 +01003460 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003461 if (rdev->raid_disk < 0)
3462 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003463 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003464 rdev->new_raid_disk = -1;
3465 if (rdev->new_raid_disk == rdev->raid_disk)
3466 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003467 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003468 }
3469 list_for_each_entry(rdev, &mddev->disks, same_set) {
3470 if (rdev->raid_disk < 0)
3471 continue;
3472 if (rdev->new_raid_disk == rdev->raid_disk)
3473 continue;
3474 rdev->raid_disk = rdev->new_raid_disk;
3475 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003476 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003477 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003478 if (sysfs_link_rdev(mddev, rdev))
3479 printk(KERN_WARNING "md: cannot register rd%d"
3480 " for %s after level change\n",
3481 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003482 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003483 }
3484
3485 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003486 mddev->pers = pers;
3487 mddev->private = priv;
3488 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3489 mddev->level = mddev->new_level;
3490 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003491 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003492 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003493 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003494 if (mddev->pers->sync_request == NULL) {
3495 /* this is now an array without redundancy, so
3496 * it must always be in_sync
3497 */
3498 mddev->in_sync = 1;
3499 del_timer_sync(&mddev->safemode_timer);
3500 }
NeilBrown245f46c2009-03-31 14:39:39 +11003501 pers->run(mddev);
3502 mddev_resume(mddev);
3503 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3504 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3505 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003506 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003507 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003508 return rv;
3509}
3510
3511static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003512__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003513
NeilBrownd4dbd022006-06-26 00:27:59 -07003514
3515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003516layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003517{
3518 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003519 if (mddev->reshape_position != MaxSector &&
3520 mddev->layout != mddev->new_layout)
3521 return sprintf(page, "%d (%d)\n",
3522 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003523 return sprintf(page, "%d\n", mddev->layout);
3524}
3525
3526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003527layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003528{
3529 char *e;
3530 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003531
3532 if (!*buf || (*e && *e != '\n'))
3533 return -EINVAL;
3534
NeilBrownb3546032009-03-31 14:56:41 +11003535 if (mddev->pers) {
3536 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003537 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003538 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003539 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003540 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003541 if (err) {
3542 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003543 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003544 }
NeilBrownb3546032009-03-31 14:56:41 +11003545 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003546 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003547 if (mddev->reshape_position == MaxSector)
3548 mddev->layout = n;
3549 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003550 return len;
3551}
3552static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003553__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003554
3555
NeilBrowneae17012005-11-08 21:39:23 -08003556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003557raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003558{
NeilBrownbb636542005-11-08 21:39:45 -08003559 if (mddev->raid_disks == 0)
3560 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003561 if (mddev->reshape_position != MaxSector &&
3562 mddev->delta_disks != 0)
3563 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3564 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003565 return sprintf(page, "%d\n", mddev->raid_disks);
3566}
3567
NeilBrownfd01b882011-10-11 16:47:53 +11003568static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003569
3570static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003571raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003572{
NeilBrownda943b992006-01-06 00:20:54 -08003573 char *e;
3574 int rv = 0;
3575 unsigned long n = simple_strtoul(buf, &e, 10);
3576
3577 if (!*buf || (*e && *e != '\n'))
3578 return -EINVAL;
3579
3580 if (mddev->pers)
3581 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003582 else if (mddev->reshape_position != MaxSector) {
3583 int olddisks = mddev->raid_disks - mddev->delta_disks;
3584 mddev->delta_disks = n - olddisks;
3585 mddev->raid_disks = n;
3586 } else
NeilBrownda943b992006-01-06 00:20:54 -08003587 mddev->raid_disks = n;
3588 return rv ? rv : len;
3589}
3590static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003591__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003592
NeilBrown24dd4692005-11-08 21:39:26 -08003593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003594chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003595{
NeilBrown08a02ec2007-05-09 02:35:38 -07003596 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003597 mddev->chunk_sectors != mddev->new_chunk_sectors)
3598 return sprintf(page, "%d (%d)\n",
3599 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003600 mddev->chunk_sectors << 9);
3601 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003602}
3603
3604static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003605chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003606{
NeilBrown3b343802006-01-06 00:20:47 -08003607 char *e;
3608 unsigned long n = simple_strtoul(buf, &e, 10);
3609
NeilBrown3b343802006-01-06 00:20:47 -08003610 if (!*buf || (*e && *e != '\n'))
3611 return -EINVAL;
3612
NeilBrownb3546032009-03-31 14:56:41 +11003613 if (mddev->pers) {
3614 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003615 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003616 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003617 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003618 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003619 if (err) {
3620 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003621 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003622 }
NeilBrownb3546032009-03-31 14:56:41 +11003623 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003624 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003625 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003626 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003627 }
NeilBrown3b343802006-01-06 00:20:47 -08003628 return len;
3629}
3630static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003631__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003632
NeilBrowna94213b2006-06-26 00:28:00 -07003633static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003634resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003635{
NeilBrownd1a7c502009-03-31 15:24:32 +11003636 if (mddev->recovery_cp == MaxSector)
3637 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003638 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3639}
3640
3641static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003642resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003643{
NeilBrowna94213b2006-06-26 00:28:00 -07003644 char *e;
3645 unsigned long long n = simple_strtoull(buf, &e, 10);
3646
NeilBrownb0986362011-05-11 15:52:21 +10003647 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003648 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003649 if (cmd_match(buf, "none"))
3650 n = MaxSector;
3651 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003652 return -EINVAL;
3653
3654 mddev->recovery_cp = n;
3655 return len;
3656}
3657static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003658__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003659
NeilBrown9e653b62006-06-26 00:27:58 -07003660/*
3661 * The array state can be:
3662 *
3663 * clear
3664 * No devices, no size, no level
3665 * Equivalent to STOP_ARRAY ioctl
3666 * inactive
3667 * May have some settings, but array is not active
3668 * all IO results in error
3669 * When written, doesn't tear down array, but just stops it
3670 * suspended (not supported yet)
3671 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003672 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003673 * readonly
3674 * no resync can happen. no superblocks get written.
3675 * write requests fail
3676 * read-auto
3677 * like readonly, but behaves like 'clean' on a write request.
3678 *
3679 * clean - no pending writes, but otherwise active.
3680 * When written to inactive array, starts without resync
3681 * If a write request arrives then
3682 * if metadata is known, mark 'dirty' and switch to 'active'.
3683 * if not known, block and switch to write-pending
3684 * If written to an active array that has pending writes, then fails.
3685 * active
3686 * fully active: IO and resync can be happening.
3687 * When written to inactive array, starts with resync
3688 *
3689 * write-pending
3690 * clean, but writes are blocked waiting for 'active' to be written.
3691 *
3692 * active-idle
3693 * like active, but no writes have been seen for a while (100msec).
3694 *
3695 */
3696enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3697 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003698static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003699 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3700 "write-pending", "active-idle", NULL };
3701
3702static int match_word(const char *word, char **list)
3703{
3704 int n;
3705 for (n=0; list[n]; n++)
3706 if (cmd_match(word, list[n]))
3707 break;
3708 return n;
3709}
3710
3711static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003712array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003713{
3714 enum array_state st = inactive;
3715
3716 if (mddev->pers)
3717 switch(mddev->ro) {
3718 case 1:
3719 st = readonly;
3720 break;
3721 case 2:
3722 st = read_auto;
3723 break;
3724 case 0:
3725 if (mddev->in_sync)
3726 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003727 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003728 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003729 else if (mddev->safemode)
3730 st = active_idle;
3731 else
3732 st = active;
3733 }
3734 else {
3735 if (list_empty(&mddev->disks) &&
3736 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003737 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003738 st = clear;
3739 else
3740 st = inactive;
3741 }
3742 return sprintf(page, "%s\n", array_states[st]);
3743}
3744
NeilBrownfd01b882011-10-11 16:47:53 +11003745static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3746static int md_set_readonly(struct mddev * mddev, int is_open);
3747static int do_md_run(struct mddev * mddev);
3748static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003749
3750static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003751array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003752{
3753 int err = -EINVAL;
3754 enum array_state st = match_word(buf, array_states);
3755 switch(st) {
3756 case bad_word:
3757 break;
3758 case clear:
3759 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003760 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003761 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003762 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003763 break;
3764 case inactive:
3765 /* stopping an active array */
3766 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003767 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003768 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003769 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003770 } else
3771 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003772 break;
3773 case suspended:
3774 break; /* not supported yet */
3775 case readonly:
3776 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003777 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003778 else {
3779 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003780 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003781 err = do_md_run(mddev);
3782 }
3783 break;
3784 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003785 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003786 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003787 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003788 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003789 err = restart_array(mddev);
3790 if (err == 0) {
3791 mddev->ro = 2;
3792 set_disk_ro(mddev->gendisk, 0);
3793 }
NeilBrown9e653b62006-06-26 00:27:58 -07003794 } else {
3795 mddev->ro = 2;
3796 err = do_md_run(mddev);
3797 }
3798 break;
3799 case clean:
3800 if (mddev->pers) {
3801 restart_array(mddev);
3802 spin_lock_irq(&mddev->write_lock);
3803 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003804 if (mddev->in_sync == 0) {
3805 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003806 if (mddev->safemode == 1)
3807 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003808 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003809 }
3810 err = 0;
3811 } else
3812 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003813 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003814 } else
3815 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003816 break;
3817 case active:
3818 if (mddev->pers) {
3819 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003820 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003821 wake_up(&mddev->sb_wait);
3822 err = 0;
3823 } else {
3824 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003825 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003826 err = do_md_run(mddev);
3827 }
3828 break;
3829 case write_pending:
3830 case active_idle:
3831 /* these cannot be set */
3832 break;
3833 }
3834 if (err)
3835 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003836 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003837 if (mddev->hold_active == UNTIL_IOCTL)
3838 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003839 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003840 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003841 }
NeilBrown9e653b62006-06-26 00:27:58 -07003842}
NeilBrown80ca3a42006-07-10 04:44:18 -07003843static struct md_sysfs_entry md_array_state =
3844__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003845
NeilBrown6d7ff7382006-01-06 00:21:16 -08003846static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003847max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003848 return sprintf(page, "%d\n",
3849 atomic_read(&mddev->max_corr_read_errors));
3850}
3851
3852static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003853max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003854{
3855 char *e;
3856 unsigned long n = simple_strtoul(buf, &e, 10);
3857
3858 if (*buf && (*e == 0 || *e == '\n')) {
3859 atomic_set(&mddev->max_corr_read_errors, n);
3860 return len;
3861 }
3862 return -EINVAL;
3863}
3864
3865static struct md_sysfs_entry max_corr_read_errors =
3866__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3867 max_corrected_read_errors_store);
3868
3869static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003870null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003871{
3872 return -EINVAL;
3873}
3874
3875static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003876new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003877{
3878 /* buf must be %d:%d\n? giving major and minor numbers */
3879 /* The new device is added to the array.
3880 * If the array has a persistent superblock, we read the
3881 * superblock to initialise info and check validity.
3882 * Otherwise, only checking done is that in bind_rdev_to_array,
3883 * which mainly checks size.
3884 */
3885 char *e;
3886 int major = simple_strtoul(buf, &e, 10);
3887 int minor;
3888 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003889 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003890 int err;
3891
3892 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3893 return -EINVAL;
3894 minor = simple_strtoul(e+1, &e, 10);
3895 if (*e && *e != '\n')
3896 return -EINVAL;
3897 dev = MKDEV(major, minor);
3898 if (major != MAJOR(dev) ||
3899 minor != MINOR(dev))
3900 return -EOVERFLOW;
3901
3902
3903 if (mddev->persistent) {
3904 rdev = md_import_device(dev, mddev->major_version,
3905 mddev->minor_version);
3906 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003907 struct md_rdev *rdev0
3908 = list_entry(mddev->disks.next,
3909 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003910 err = super_types[mddev->major_version]
3911 .load_super(rdev, rdev0, mddev->minor_version);
3912 if (err < 0)
3913 goto out;
3914 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003915 } else if (mddev->external)
3916 rdev = md_import_device(dev, -2, -1);
3917 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003918 rdev = md_import_device(dev, -1, -1);
3919
3920 if (IS_ERR(rdev))
3921 return PTR_ERR(rdev);
3922 err = bind_rdev_to_array(rdev, mddev);
3923 out:
3924 if (err)
3925 export_rdev(rdev);
3926 return err ? err : len;
3927}
3928
3929static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003930__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003931
3932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003933bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003934{
3935 char *end;
3936 unsigned long chunk, end_chunk;
3937
3938 if (!mddev->bitmap)
3939 goto out;
3940 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3941 while (*buf) {
3942 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3943 if (buf == end) break;
3944 if (*end == '-') { /* range */
3945 buf = end + 1;
3946 end_chunk = simple_strtoul(buf, &end, 0);
3947 if (buf == end) break;
3948 }
3949 if (*end && !isspace(*end)) break;
3950 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003951 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003952 }
3953 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3954out:
3955 return len;
3956}
3957
3958static struct md_sysfs_entry md_bitmap =
3959__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3960
3961static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003962size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003963{
Andre Noll58c0fed2009-03-31 14:33:13 +11003964 return sprintf(page, "%llu\n",
3965 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003966}
3967
NeilBrownfd01b882011-10-11 16:47:53 +11003968static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003969
3970static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003971size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003972{
3973 /* If array is inactive, we can reduce the component size, but
3974 * not increase it (except from 0).
3975 * If array is active, we can try an on-line resize
3976 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003977 sector_t sectors;
3978 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003979
Andre Noll58c0fed2009-03-31 14:33:13 +11003980 if (err < 0)
3981 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003982 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003983 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003984 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003985 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003986 if (mddev->dev_sectors == 0 ||
3987 mddev->dev_sectors > sectors)
3988 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003989 else
3990 err = -ENOSPC;
3991 }
3992 return err ? err : len;
3993}
3994
3995static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003996__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003997
NeilBrown8bb93aa2006-01-06 00:20:50 -08003998
3999/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004000 * This is one of
4001 * 'none' for arrays with no metadata (good luck...)
4002 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004003 * or N.M for internally known formats
4004 */
4005static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004006metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004007{
4008 if (mddev->persistent)
4009 return sprintf(page, "%d.%d\n",
4010 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004011 else if (mddev->external)
4012 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004013 else
4014 return sprintf(page, "none\n");
4015}
4016
4017static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004018metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004019{
4020 int major, minor;
4021 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004022 /* Changing the details of 'external' metadata is
4023 * always permitted. Otherwise there must be
4024 * no devices attached to the array.
4025 */
4026 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4027 ;
4028 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004029 return -EBUSY;
4030
4031 if (cmd_match(buf, "none")) {
4032 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004033 mddev->external = 0;
4034 mddev->major_version = 0;
4035 mddev->minor_version = 90;
4036 return len;
4037 }
4038 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004039 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004040 if (namelen >= sizeof(mddev->metadata_type))
4041 namelen = sizeof(mddev->metadata_type)-1;
4042 strncpy(mddev->metadata_type, buf+9, namelen);
4043 mddev->metadata_type[namelen] = 0;
4044 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4045 mddev->metadata_type[--namelen] = 0;
4046 mddev->persistent = 0;
4047 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004048 mddev->major_version = 0;
4049 mddev->minor_version = 90;
4050 return len;
4051 }
4052 major = simple_strtoul(buf, &e, 10);
4053 if (e==buf || *e != '.')
4054 return -EINVAL;
4055 buf = e+1;
4056 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004057 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004058 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004059 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004060 return -ENOENT;
4061 mddev->major_version = major;
4062 mddev->minor_version = minor;
4063 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004064 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004065 return len;
4066}
4067
4068static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004069__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004070
NeilBrowna35b0d62006-01-06 00:20:49 -08004071static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004072action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004073{
NeilBrown7eec3142005-11-08 21:39:44 -08004074 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004075 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4076 type = "frozen";
4077 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004078 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004079 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4080 type = "reshape";
4081 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004082 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4083 type = "resync";
4084 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4085 type = "check";
4086 else
4087 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004088 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004089 type = "recover";
4090 }
4091 return sprintf(page, "%s\n", type);
4092}
4093
NeilBrownfd01b882011-10-11 16:47:53 +11004094static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004095
NeilBrown24dd4692005-11-08 21:39:26 -08004096static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004097action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004098{
NeilBrown7eec3142005-11-08 21:39:44 -08004099 if (!mddev->pers || !mddev->pers->sync_request)
4100 return -EINVAL;
4101
NeilBrownb6a9ce62009-05-26 09:41:17 +10004102 if (cmd_match(page, "frozen"))
4103 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4104 else
4105 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4106
4107 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004108 if (mddev->sync_thread) {
4109 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004110 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004111 }
NeilBrown03c902e2006-01-06 00:20:46 -08004112 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4113 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004114 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004115 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004116 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004117 else if (cmd_match(page, "recover")) {
4118 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4119 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4120 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004121 int err;
4122 if (mddev->pers->start_reshape == NULL)
4123 return -EINVAL;
4124 err = mddev->pers->start_reshape(mddev);
4125 if (err)
4126 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004127 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004128 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004129 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004130 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004131 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004132 return -EINVAL;
4133 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4134 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004135 }
NeilBrown03c902e2006-01-06 00:20:46 -08004136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004137 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004138 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004139 return len;
4140}
4141
NeilBrown9d888832005-11-08 21:39:26 -08004142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004143mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004144{
4145 return sprintf(page, "%llu\n",
4146 (unsigned long long) mddev->resync_mismatches);
4147}
4148
NeilBrown80ca3a42006-07-10 04:44:18 -07004149static struct md_sysfs_entry md_scan_mode =
4150__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004151
NeilBrown96de1e62005-11-08 21:39:39 -08004152
NeilBrown80ca3a42006-07-10 04:44:18 -07004153static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004154
NeilBrown88202a02006-01-06 00:21:36 -08004155static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004156sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004157{
4158 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4159 mddev->sync_speed_min ? "local": "system");
4160}
4161
4162static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004163sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004164{
4165 int min;
4166 char *e;
4167 if (strncmp(buf, "system", 6)==0) {
4168 mddev->sync_speed_min = 0;
4169 return len;
4170 }
4171 min = simple_strtoul(buf, &e, 10);
4172 if (buf == e || (*e && *e != '\n') || min <= 0)
4173 return -EINVAL;
4174 mddev->sync_speed_min = min;
4175 return len;
4176}
4177
4178static struct md_sysfs_entry md_sync_min =
4179__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4180
4181static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004182sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004183{
4184 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4185 mddev->sync_speed_max ? "local": "system");
4186}
4187
4188static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004189sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004190{
4191 int max;
4192 char *e;
4193 if (strncmp(buf, "system", 6)==0) {
4194 mddev->sync_speed_max = 0;
4195 return len;
4196 }
4197 max = simple_strtoul(buf, &e, 10);
4198 if (buf == e || (*e && *e != '\n') || max <= 0)
4199 return -EINVAL;
4200 mddev->sync_speed_max = max;
4201 return len;
4202}
4203
4204static struct md_sysfs_entry md_sync_max =
4205__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4206
Iustin Popd7f3d292007-10-16 23:30:54 -07004207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004209{
4210 return sprintf(page, "%d\n", mddev->degraded);
4211}
4212static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004213
4214static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004215sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004216{
4217 return sprintf(page, "%d\n", mddev->parallel_resync);
4218}
4219
4220static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004221sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004222{
4223 long n;
4224
4225 if (strict_strtol(buf, 10, &n))
4226 return -EINVAL;
4227
4228 if (n != 0 && n != 1)
4229 return -EINVAL;
4230
4231 mddev->parallel_resync = n;
4232
4233 if (mddev->sync_thread)
4234 wake_up(&resync_wait);
4235
4236 return len;
4237}
4238
4239/* force parallel resync, even with shared block devices */
4240static struct md_sysfs_entry md_sync_force_parallel =
4241__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4242 sync_force_parallel_show, sync_force_parallel_store);
4243
4244static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004245sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004246{
4247 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004248 if (mddev->curr_resync == 0)
4249 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004250 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4251 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004252 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004253 db = resync - mddev->resync_mark_cnt;
4254 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004255}
4256
NeilBrown80ca3a42006-07-10 04:44:18 -07004257static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004258
4259static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004260sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004261{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004262 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004263
NeilBrownacb180b2009-04-14 16:28:34 +10004264 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4265 return sprintf(page, "none\n");
4266
NeilBrown88202a02006-01-06 00:21:36 -08004267 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004268 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004269 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004270 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004271
NeilBrownacb180b2009-04-14 16:28:34 +10004272 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004273 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004274}
4275
NeilBrown80ca3a42006-07-10 04:44:18 -07004276static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004277
NeilBrowne464eaf2006-03-27 01:18:14 -08004278static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004279min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004280{
4281 return sprintf(page, "%llu\n",
4282 (unsigned long long)mddev->resync_min);
4283}
4284static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004285min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004286{
4287 unsigned long long min;
4288 if (strict_strtoull(buf, 10, &min))
4289 return -EINVAL;
4290 if (min > mddev->resync_max)
4291 return -EINVAL;
4292 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4293 return -EBUSY;
4294
4295 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004296 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004297 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004298 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004299 return -EINVAL;
4300 }
4301 mddev->resync_min = min;
4302
4303 return len;
4304}
4305
4306static struct md_sysfs_entry md_min_sync =
4307__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004311{
4312 if (mddev->resync_max == MaxSector)
4313 return sprintf(page, "max\n");
4314 else
4315 return sprintf(page, "%llu\n",
4316 (unsigned long long)mddev->resync_max);
4317}
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004320{
4321 if (strncmp(buf, "max", 3) == 0)
4322 mddev->resync_max = MaxSector;
4323 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004324 unsigned long long max;
4325 if (strict_strtoull(buf, 10, &max))
4326 return -EINVAL;
4327 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004328 return -EINVAL;
4329 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004330 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004331 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4332 return -EBUSY;
4333
4334 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004335 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004336 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004337 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004338 return -EINVAL;
4339 }
4340 mddev->resync_max = max;
4341 }
4342 wake_up(&mddev->recovery_wait);
4343 return len;
4344}
4345
4346static struct md_sysfs_entry md_max_sync =
4347__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4348
4349static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004350suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004351{
4352 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4353}
4354
4355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004357{
4358 char *e;
4359 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004360 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004361
NeilBrownb8d966e2009-07-01 11:14:04 +10004362 if (mddev->pers == NULL ||
4363 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004364 return -EINVAL;
4365 if (buf == e || (*e && *e != '\n'))
4366 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004367
4368 mddev->suspend_lo = new;
4369 if (new >= old)
4370 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004371 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004372 else {
4373 /* Expanding suspended region - need to wait */
4374 mddev->pers->quiesce(mddev, 1);
4375 mddev->pers->quiesce(mddev, 0);
4376 }
4377 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004378}
4379static struct md_sysfs_entry md_suspend_lo =
4380__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4381
4382
4383static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004384suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004385{
4386 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4387}
4388
4389static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004390suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004391{
4392 char *e;
4393 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004394 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004395
NeilBrownb8d966e2009-07-01 11:14:04 +10004396 if (mddev->pers == NULL ||
4397 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004398 return -EINVAL;
4399 if (buf == e || (*e && *e != '\n'))
4400 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004401
4402 mddev->suspend_hi = new;
4403 if (new <= old)
4404 /* Shrinking suspended region */
4405 mddev->pers->quiesce(mddev, 2);
4406 else {
4407 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004408 mddev->pers->quiesce(mddev, 1);
4409 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004410 }
4411 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004412}
4413static struct md_sysfs_entry md_suspend_hi =
4414__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4415
NeilBrown08a02ec2007-05-09 02:35:38 -07004416static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004417reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004418{
4419 if (mddev->reshape_position != MaxSector)
4420 return sprintf(page, "%llu\n",
4421 (unsigned long long)mddev->reshape_position);
4422 strcpy(page, "none\n");
4423 return 5;
4424}
4425
4426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004427reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004428{
4429 char *e;
4430 unsigned long long new = simple_strtoull(buf, &e, 10);
4431 if (mddev->pers)
4432 return -EBUSY;
4433 if (buf == e || (*e && *e != '\n'))
4434 return -EINVAL;
4435 mddev->reshape_position = new;
4436 mddev->delta_disks = 0;
4437 mddev->new_level = mddev->level;
4438 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004439 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004440 return len;
4441}
4442
4443static struct md_sysfs_entry md_reshape_position =
4444__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4445 reshape_position_store);
4446
Dan Williamsb522adc2009-03-31 15:00:31 +11004447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004448array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004449{
4450 if (mddev->external_size)
4451 return sprintf(page, "%llu\n",
4452 (unsigned long long)mddev->array_sectors/2);
4453 else
4454 return sprintf(page, "default\n");
4455}
4456
4457static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004458array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004459{
4460 sector_t sectors;
4461
4462 if (strncmp(buf, "default", 7) == 0) {
4463 if (mddev->pers)
4464 sectors = mddev->pers->size(mddev, 0, 0);
4465 else
4466 sectors = mddev->array_sectors;
4467
4468 mddev->external_size = 0;
4469 } else {
4470 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4471 return -EINVAL;
4472 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004473 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004474
4475 mddev->external_size = 1;
4476 }
4477
4478 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004479 if (mddev->pers) {
4480 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004481 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004482 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004483 return len;
4484}
4485
4486static struct md_sysfs_entry md_array_size =
4487__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4488 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004489
NeilBrowneae17012005-11-08 21:39:23 -08004490static struct attribute *md_default_attrs[] = {
4491 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004492 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004493 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004494 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004495 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004496 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004497 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004498 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004499 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004500 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004501 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004502 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004503 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004504 NULL,
4505};
4506
4507static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004508 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004509 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004510 &md_sync_min.attr,
4511 &md_sync_max.attr,
4512 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004513 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004514 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004515 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004516 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004517 &md_suspend_lo.attr,
4518 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004519 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004520 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004521 NULL,
4522};
NeilBrown411036f2005-11-08 21:39:40 -08004523static struct attribute_group md_redundancy_group = {
4524 .name = NULL,
4525 .attrs = md_redundancy_attrs,
4526};
4527
NeilBrowneae17012005-11-08 21:39:23 -08004528
4529static ssize_t
4530md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4531{
4532 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004533 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004534 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004535
4536 if (!entry->show)
4537 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004538 spin_lock(&all_mddevs_lock);
4539 if (list_empty(&mddev->all_mddevs)) {
4540 spin_unlock(&all_mddevs_lock);
4541 return -EBUSY;
4542 }
4543 mddev_get(mddev);
4544 spin_unlock(&all_mddevs_lock);
4545
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004546 rv = mddev_lock(mddev);
4547 if (!rv) {
4548 rv = entry->show(mddev, page);
4549 mddev_unlock(mddev);
4550 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004551 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004552 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004553}
4554
4555static ssize_t
4556md_attr_store(struct kobject *kobj, struct attribute *attr,
4557 const char *page, size_t length)
4558{
4559 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004560 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004561 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004562
4563 if (!entry->store)
4564 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004565 if (!capable(CAP_SYS_ADMIN))
4566 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004567 spin_lock(&all_mddevs_lock);
4568 if (list_empty(&mddev->all_mddevs)) {
4569 spin_unlock(&all_mddevs_lock);
4570 return -EBUSY;
4571 }
4572 mddev_get(mddev);
4573 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004574 rv = mddev_lock(mddev);
4575 if (!rv) {
4576 rv = entry->store(mddev, page, length);
4577 mddev_unlock(mddev);
4578 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004579 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004580 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004581}
4582
4583static void md_free(struct kobject *ko)
4584{
NeilBrownfd01b882011-10-11 16:47:53 +11004585 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004586
4587 if (mddev->sysfs_state)
4588 sysfs_put(mddev->sysfs_state);
4589
4590 if (mddev->gendisk) {
4591 del_gendisk(mddev->gendisk);
4592 put_disk(mddev->gendisk);
4593 }
4594 if (mddev->queue)
4595 blk_cleanup_queue(mddev->queue);
4596
NeilBrowneae17012005-11-08 21:39:23 -08004597 kfree(mddev);
4598}
4599
Emese Revfy52cf25d2010-01-19 02:58:23 +01004600static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004601 .show = md_attr_show,
4602 .store = md_attr_store,
4603};
4604static struct kobj_type md_ktype = {
4605 .release = md_free,
4606 .sysfs_ops = &md_sysfs_ops,
4607 .default_attrs = md_default_attrs,
4608};
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610int mdp_major = 0;
4611
Dan Williams5fd3a172009-03-04 00:57:25 -07004612static void mddev_delayed_delete(struct work_struct *ws)
4613{
NeilBrownfd01b882011-10-11 16:47:53 +11004614 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004615
NeilBrown43a70502009-12-14 12:49:55 +11004616 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004617 kobject_del(&mddev->kobj);
4618 kobject_put(&mddev->kobj);
4619}
4620
NeilBrownefeb53c2009-01-09 08:31:10 +11004621static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004623 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004624 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004626 int partitioned;
4627 int shift;
4628 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004629 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004632 return -ENODEV;
4633
4634 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4635 shift = partitioned ? MdpMinorShift : 0;
4636 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Tejun Heoe804ac72010-10-15 15:36:08 +02004638 /* wait for any previous instance of this device to be
4639 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004640 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004641 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004642
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004643 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004644 error = -EEXIST;
4645 if (mddev->gendisk)
4646 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004647
4648 if (name) {
4649 /* Need to ensure that 'name' is not a duplicate.
4650 */
NeilBrownfd01b882011-10-11 16:47:53 +11004651 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004652 spin_lock(&all_mddevs_lock);
4653
4654 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4655 if (mddev2->gendisk &&
4656 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4657 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004658 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004659 }
4660 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 }
NeilBrown8b765392009-01-09 08:31:08 +11004662
NeilBrown0909dc42009-07-01 12:27:21 +10004663 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004664 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004665 if (!mddev->queue)
4666 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004667 mddev->queue->queuedata = mddev;
4668
NeilBrown409c57f2009-03-31 14:39:39 +11004669 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 disk = alloc_disk(1 << shift);
4672 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004673 blk_cleanup_queue(mddev->queue);
4674 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004675 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004677 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004679 if (name)
4680 strcpy(disk->disk_name, name);
4681 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004683 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 disk->fops = &md_fops;
4686 disk->private_data = mddev;
4687 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004688 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004689 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004690 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004691 * remove it now.
4692 */
4693 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004695 /* As soon as we call add_disk(), another thread could get
4696 * through to md_open, so make sure it doesn't get too far
4697 */
4698 mutex_lock(&mddev->open_mutex);
4699 add_disk(disk);
4700
Tejun Heoed9e1982008-08-25 19:56:05 +09004701 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4702 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004703 if (error) {
4704 /* This isn't possible, but as kobject_init_and_add is marked
4705 * __must_check, we must do something with the result
4706 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004707 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4708 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004709 error = 0;
4710 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004711 if (mddev->kobj.sd &&
4712 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004713 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004714 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004715 abort:
4716 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004717 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004718 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004719 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004720 }
NeilBrownd3374822009-01-09 08:31:10 +11004721 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004722 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004723}
4724
4725static struct kobject *md_probe(dev_t dev, int *part, void *data)
4726{
4727 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 return NULL;
4729}
4730
NeilBrownefeb53c2009-01-09 08:31:10 +11004731static int add_named_array(const char *val, struct kernel_param *kp)
4732{
4733 /* val must be "md_*" where * is not all digits.
4734 * We allocate an array with a large free minor number, and
4735 * set the name to val. val must not already be an active name.
4736 */
4737 int len = strlen(val);
4738 char buf[DISK_NAME_LEN];
4739
4740 while (len && val[len-1] == '\n')
4741 len--;
4742 if (len >= DISK_NAME_LEN)
4743 return -E2BIG;
4744 strlcpy(buf, val, len+1);
4745 if (strncmp(buf, "md_", 3) != 0)
4746 return -EINVAL;
4747 return md_alloc(0, buf);
4748}
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750static void md_safemode_timeout(unsigned long data)
4751{
NeilBrownfd01b882011-10-11 16:47:53 +11004752 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Neil Brown0fd62b82008-06-28 08:31:36 +10004754 if (!atomic_read(&mddev->writes_pending)) {
4755 mddev->safemode = 1;
4756 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004757 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 md_wakeup_thread(mddev->thread);
4760}
4761
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004762static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
NeilBrownfd01b882011-10-11 16:47:53 +11004764int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
NeilBrown2604b702006-01-06 00:20:36 -08004766 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004767 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004768 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
NeilBrowna757e642005-04-16 15:26:42 -07004770 if (list_empty(&mddev->disks))
4771 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 if (mddev->pers)
4775 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004776 /* Cannot run until previous stop completes properly */
4777 if (mddev->sysfs_active)
4778 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 /*
4781 * Analyze all RAID superblock(s)
4782 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004783 if (!mddev->raid_disks) {
4784 if (!mddev->persistent)
4785 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004786 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
NeilBrownd9d166c2006-01-06 00:20:51 -08004789 if (mddev->level != LEVEL_NONE)
4790 request_module("md-level-%d", mddev->level);
4791 else if (mddev->clevel[0])
4792 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 /*
4795 * Drop all container device buffers, from now on
4796 * the only valid external interface is through the md
4797 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004799 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004800 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 continue;
4802 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004803 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004804
4805 /* perform some consistency tests on the device.
4806 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004807 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004808 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004809 if (rdev->meta_bdev) {
4810 /* Nothing to check */;
4811 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004812 if (mddev->dev_sectors &&
4813 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004814 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004815 printk("md: %s: data overlaps metadata\n",
4816 mdname(mddev));
4817 return -EINVAL;
4818 }
4819 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004820 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004821 > rdev->data_offset) {
4822 printk("md: %s: metadata overlaps data\n",
4823 mdname(mddev));
4824 return -EINVAL;
4825 }
4826 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004827 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 }
4829
NeilBrowna167f662010-10-26 18:31:13 +11004830 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004831 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004832 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004835 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004836 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004838 if (mddev->level != LEVEL_NONE)
4839 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4840 mddev->level);
4841 else
4842 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4843 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return -EINVAL;
4845 }
NeilBrown2604b702006-01-06 00:20:36 -08004846 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004848 if (mddev->level != pers->level) {
4849 mddev->level = pers->level;
4850 mddev->new_level = pers->level;
4851 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004852 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
NeilBrownf6705572006-03-27 01:18:11 -08004854 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004855 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004856 /* This personality cannot handle reshaping... */
4857 mddev->pers = NULL;
4858 module_put(pers->owner);
4859 return -EINVAL;
4860 }
4861
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004862 if (pers->sync_request) {
4863 /* Warn if this is a potentially silly
4864 * configuration.
4865 */
4866 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004867 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004868 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004869
4870 list_for_each_entry(rdev, &mddev->disks, same_set)
4871 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004872 if (rdev < rdev2 &&
4873 rdev->bdev->bd_contains ==
4874 rdev2->bdev->bd_contains) {
4875 printk(KERN_WARNING
4876 "%s: WARNING: %s appears to be"
4877 " on the same physical disk as"
4878 " %s.\n",
4879 mdname(mddev),
4880 bdevname(rdev->bdev,b),
4881 bdevname(rdev2->bdev,b2));
4882 warned = 1;
4883 }
4884 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004885
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004886 if (warned)
4887 printk(KERN_WARNING
4888 "True protection against single-disk"
4889 " failure might be compromised.\n");
4890 }
4891
NeilBrown657390d2005-08-26 18:34:16 -07004892 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004893 /* may be over-ridden by personality */
4894 mddev->resync_max_sectors = mddev->dev_sectors;
4895
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004896 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
NeilBrown0f9552b52009-12-30 12:08:50 +11004898 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004899 mddev->ro = 2; /* read-only, but switch on first write */
4900
NeilBrownb15c2e52006-01-06 00:20:16 -08004901 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004902 if (err)
4903 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004904 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4905 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4906 " but 'external_size' not in effect?\n", __func__);
4907 printk(KERN_ERR
4908 "md: invalid array_size %llu > default size %llu\n",
4909 (unsigned long long)mddev->array_sectors / 2,
4910 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4911 err = -EINVAL;
4912 mddev->pers->stop(mddev);
4913 }
4914 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004915 err = bitmap_create(mddev);
4916 if (err) {
4917 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4918 mdname(mddev), err);
4919 mddev->pers->stop(mddev);
4920 }
4921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 module_put(mddev->pers->owner);
4924 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004925 bitmap_destroy(mddev);
4926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004928 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004929 if (mddev->kobj.sd &&
4930 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004931 printk(KERN_WARNING
4932 "md: cannot register extra attributes for %s\n",
4933 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004934 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004935 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004936 mddev->ro = 0;
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004939 atomic_set(&mddev->max_corr_read_errors,
4940 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 mddev->safemode = 0;
4942 mddev->safemode_timer.function = md_safemode_timeout;
4943 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004944 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004946 smp_wmb();
4947 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004948 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004949 if (rdev->raid_disk >= 0)
4950 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004951 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4954
NeilBrown850b2b422006-10-03 01:15:46 -07004955 if (mddev->flags)
4956 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
NeilBrownd7603b72006-01-06 00:20:30 -08004958 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004959 sysfs_notify_dirent_safe(mddev->sysfs_state);
4960 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004961 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return 0;
4963}
NeilBrown390ee602010-06-01 19:37:27 +10004964EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
NeilBrownfd01b882011-10-11 16:47:53 +11004966static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004967{
4968 int err;
4969
4970 err = md_run(mddev);
4971 if (err)
4972 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004973 err = bitmap_load(mddev);
4974 if (err) {
4975 bitmap_destroy(mddev);
4976 goto out;
4977 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004978
4979 md_wakeup_thread(mddev->thread);
4980 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4981
NeilBrownfe60b012010-03-29 11:10:42 +11004982 set_capacity(mddev->gendisk, mddev->array_sectors);
4983 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004984 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004985 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4986out:
4987 return err;
4988}
4989
NeilBrownfd01b882011-10-11 16:47:53 +11004990static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Andre Noll80fab1d2008-07-11 22:02:21 +10004994 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004996 return -ENXIO;
4997 if (!mddev->pers)
4998 return -EINVAL;
4999 if (!mddev->ro)
5000 return -EBUSY;
5001 mddev->safemode = 0;
5002 mddev->ro = 0;
5003 set_disk_ro(disk, 0);
5004 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5005 mdname(mddev));
5006 /* Kick recovery or resync if necessary */
5007 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5008 md_wakeup_thread(mddev->thread);
5009 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005010 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
NeilBrownacc55e22006-06-26 00:27:47 -07005014/* similar to deny_write_access, but accounts for our holding a reference
5015 * to the file ourselves */
5016static int deny_bitmap_write_access(struct file * file)
5017{
5018 struct inode *inode = file->f_mapping->host;
5019
5020 spin_lock(&inode->i_lock);
5021 if (atomic_read(&inode->i_writecount) > 1) {
5022 spin_unlock(&inode->i_lock);
5023 return -ETXTBSY;
5024 }
5025 atomic_set(&inode->i_writecount, -1);
5026 spin_unlock(&inode->i_lock);
5027
5028 return 0;
5029}
5030
NeilBrown43a70502009-12-14 12:49:55 +11005031void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005032{
5033 struct inode *inode = file->f_mapping->host;
5034
5035 spin_lock(&inode->i_lock);
5036 atomic_set(&inode->i_writecount, 1);
5037 spin_unlock(&inode->i_lock);
5038}
5039
NeilBrownfd01b882011-10-11 16:47:53 +11005040static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005041{
5042 mddev->array_sectors = 0;
5043 mddev->external_size = 0;
5044 mddev->dev_sectors = 0;
5045 mddev->raid_disks = 0;
5046 mddev->recovery_cp = 0;
5047 mddev->resync_min = 0;
5048 mddev->resync_max = MaxSector;
5049 mddev->reshape_position = MaxSector;
5050 mddev->external = 0;
5051 mddev->persistent = 0;
5052 mddev->level = LEVEL_NONE;
5053 mddev->clevel[0] = 0;
5054 mddev->flags = 0;
5055 mddev->ro = 0;
5056 mddev->metadata_type[0] = 0;
5057 mddev->chunk_sectors = 0;
5058 mddev->ctime = mddev->utime = 0;
5059 mddev->layout = 0;
5060 mddev->max_disks = 0;
5061 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005062 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005063 mddev->delta_disks = 0;
5064 mddev->new_level = LEVEL_NONE;
5065 mddev->new_layout = 0;
5066 mddev->new_chunk_sectors = 0;
5067 mddev->curr_resync = 0;
5068 mddev->resync_mismatches = 0;
5069 mddev->suspend_lo = mddev->suspend_hi = 0;
5070 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5071 mddev->recovery = 0;
5072 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005073 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005074 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005075 mddev->safemode = 0;
5076 mddev->bitmap_info.offset = 0;
5077 mddev->bitmap_info.default_offset = 0;
5078 mddev->bitmap_info.chunksize = 0;
5079 mddev->bitmap_info.daemon_sleep = 0;
5080 mddev->bitmap_info.max_write_behind = 0;
5081}
5082
NeilBrownfd01b882011-10-11 16:47:53 +11005083static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005084{
5085 if (mddev->sync_thread) {
5086 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5087 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005088 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005089 }
5090
5091 del_timer_sync(&mddev->safemode_timer);
5092
5093 bitmap_flush(mddev);
5094 md_super_wait(mddev);
5095
5096 if (!mddev->in_sync || mddev->flags) {
5097 /* mark array as shutdown cleanly */
5098 mddev->in_sync = 1;
5099 md_update_sb(mddev, 1);
5100 }
5101}
NeilBrowndefad612011-01-14 09:14:33 +11005102
NeilBrownfd01b882011-10-11 16:47:53 +11005103void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005104{
5105 mddev_lock(mddev);
5106 __md_stop_writes(mddev);
5107 mddev_unlock(mddev);
5108}
NeilBrown390ee602010-06-01 19:37:27 +10005109EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005110
NeilBrownfd01b882011-10-11 16:47:53 +11005111void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005112{
NeilBrown0ca69882011-01-14 09:14:33 +11005113 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005114 mddev->pers->stop(mddev);
5115 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5116 mddev->to_remove = &md_redundancy_group;
5117 module_put(mddev->pers->owner);
5118 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005119 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005120}
NeilBrown390ee602010-06-01 19:37:27 +10005121EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005122
NeilBrownfd01b882011-10-11 16:47:53 +11005123static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005124{
5125 int err = 0;
5126 mutex_lock(&mddev->open_mutex);
5127 if (atomic_read(&mddev->openers) > is_open) {
5128 printk("md: %s still in use.\n",mdname(mddev));
5129 err = -EBUSY;
5130 goto out;
5131 }
5132 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005133 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005134
5135 err = -ENXIO;
5136 if (mddev->ro==1)
5137 goto out;
5138 mddev->ro = 1;
5139 set_disk_ro(mddev->gendisk, 1);
5140 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005141 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005142 err = 0;
5143 }
5144out:
5145 mutex_unlock(&mddev->open_mutex);
5146 return err;
5147}
5148
NeilBrown9e653b62006-06-26 00:27:58 -07005149/* mode:
5150 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005151 * 2 - stop but do not disassemble array
5152 */
NeilBrownfd01b882011-10-11 16:47:53 +11005153static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005156 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
NeilBrownc8c00a62009-08-10 12:50:52 +10005158 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005159 if (atomic_read(&mddev->openers) > is_open ||
5160 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005161 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005162 mutex_unlock(&mddev->open_mutex);
5163 return -EBUSY;
5164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
NeilBrown6e17b022010-08-07 21:41:19 +10005166 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005167 if (mddev->ro)
5168 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005169
NeilBrowndefad612011-01-14 09:14:33 +11005170 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005171 md_stop(mddev);
5172 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005173 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005174
NeilBrowna4bd82d2010-03-29 13:23:10 +11005175 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005176 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005177
NeilBrowna4bd82d2010-03-29 13:23:10 +11005178 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005179 if (rdev->raid_disk >= 0)
5180 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005181
NeilBrowna4bd82d2010-03-29 13:23:10 +11005182 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005183 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005184 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005185 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005186
NeilBrowna4bd82d2010-03-29 13:23:10 +11005187 if (mddev->ro)
5188 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005189 } else
5190 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 /*
5192 * Free resources if final stop
5193 */
NeilBrown9e653b62006-06-26 00:27:58 -07005194 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5196
NeilBrown978f9462006-02-02 14:28:05 -08005197 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005198 if (mddev->bitmap_info.file) {
5199 restore_bitmap_write_access(mddev->bitmap_info.file);
5200 fput(mddev->bitmap_info.file);
5201 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005202 }
NeilBrownc3d97142009-12-14 12:49:52 +11005203 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 export_array(mddev);
5206
NeilBrown6177b472010-03-29 11:37:13 +11005207 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005208 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005209 if (mddev->hold_active == UNTIL_STOP)
5210 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005211 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005212 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005213 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005214 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216}
5217
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005218#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005219static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
NeilBrown3cb03002011-10-11 16:45:26 +11005221 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 int err;
5223
NeilBrowna757e642005-04-16 15:26:42 -07005224 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
5227 printk(KERN_INFO "md: running: ");
5228
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005229 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 char b[BDEVNAME_SIZE];
5231 printk("<%s>", bdevname(rdev->bdev,b));
5232 }
5233 printk("\n");
5234
NeilBrownd710e132008-10-13 11:55:12 +11005235 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (err) {
5237 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005238 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 }
5240}
5241
5242/*
5243 * lets try to run arrays based on all disks that have arrived
5244 * until now. (those are in pending_raid_disks)
5245 *
5246 * the method: pick the first pending disk, collect all disks with
5247 * the same UUID, remove all from the pending list and put them into
5248 * the 'same_array' list. Then order this list based on superblock
5249 * update time (freshest comes first), kick out 'old' disks and
5250 * compare superblocks. If everything's fine then run it.
5251 *
5252 * If "unit" is allocated, then bump its reference count
5253 */
5254static void autorun_devices(int part)
5255{
NeilBrown3cb03002011-10-11 16:45:26 +11005256 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005257 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 char b[BDEVNAME_SIZE];
5259
5260 printk(KERN_INFO "md: autorun ...\n");
5261 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005262 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005264 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005266 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 printk(KERN_INFO "md: considering %s ...\n",
5269 bdevname(rdev0->bdev,b));
5270 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005271 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 if (super_90_load(rdev, rdev0, 0) >= 0) {
5273 printk(KERN_INFO "md: adding %s ...\n",
5274 bdevname(rdev->bdev,b));
5275 list_move(&rdev->same_set, &candidates);
5276 }
5277 /*
5278 * now we have a set of devices, with all of them having
5279 * mostly sane superblocks. It's time to allocate the
5280 * mddev.
5281 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005282 if (part) {
5283 dev = MKDEV(mdp_major,
5284 rdev0->preferred_minor << MdpMinorShift);
5285 unit = MINOR(dev) >> MdpMinorShift;
5286 } else {
5287 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5288 unit = MINOR(dev);
5289 }
5290 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5292 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5293 break;
5294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 md_probe(dev, NULL, NULL);
5297 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005298 if (!mddev || !mddev->gendisk) {
5299 if (mddev)
5300 mddev_put(mddev);
5301 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 "md: cannot allocate memory for md drive.\n");
5303 break;
5304 }
5305 if (mddev_lock(mddev))
5306 printk(KERN_WARNING "md: %s locked, cannot run\n",
5307 mdname(mddev));
5308 else if (mddev->raid_disks || mddev->major_version
5309 || !list_empty(&mddev->disks)) {
5310 printk(KERN_WARNING
5311 "md: %s already running, cannot run %s\n",
5312 mdname(mddev), bdevname(rdev0->bdev,b));
5313 mddev_unlock(mddev);
5314 } else {
5315 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005316 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005317 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 list_del_init(&rdev->same_set);
5319 if (bind_rdev_to_array(rdev, mddev))
5320 export_rdev(rdev);
5321 }
5322 autorun_array(mddev);
5323 mddev_unlock(mddev);
5324 }
5325 /* on success, candidates will be empty, on error
5326 * it won't...
5327 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005328 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005329 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 mddev_put(mddev);
5333 }
5334 printk(KERN_INFO "md: ... autorun DONE.\n");
5335}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005336#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338static int get_version(void __user * arg)
5339{
5340 mdu_version_t ver;
5341
5342 ver.major = MD_MAJOR_VERSION;
5343 ver.minor = MD_MINOR_VERSION;
5344 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5345
5346 if (copy_to_user(arg, &ver, sizeof(ver)))
5347 return -EFAULT;
5348
5349 return 0;
5350}
5351
NeilBrownfd01b882011-10-11 16:47:53 +11005352static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005355 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005356 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
NeilBrowna9f326e2009-09-23 18:06:41 +10005358 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005359 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005361 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 failed++;
5363 else {
5364 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005365 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005366 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 else
5368 spare++;
5369 }
5370 }
5371
5372 info.major_version = mddev->major_version;
5373 info.minor_version = mddev->minor_version;
5374 info.patch_version = MD_PATCHLEVEL_VERSION;
5375 info.ctime = mddev->ctime;
5376 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005377 info.size = mddev->dev_sectors / 2;
5378 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005379 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 info.nr_disks = nr;
5381 info.raid_disks = mddev->raid_disks;
5382 info.md_minor = mddev->md_minor;
5383 info.not_persistent= !mddev->persistent;
5384
5385 info.utime = mddev->utime;
5386 info.state = 0;
5387 if (mddev->in_sync)
5388 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005389 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005390 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005391 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 info.working_disks = working;
5393 info.failed_disks = failed;
5394 info.spare_disks = spare;
5395
5396 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005397 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 if (copy_to_user(arg, &info, sizeof(info)))
5400 return -EFAULT;
5401
5402 return 0;
5403}
5404
NeilBrownfd01b882011-10-11 16:47:53 +11005405static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005406{
5407 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5408 char *ptr, *buf = NULL;
5409 int err = -ENOMEM;
5410
Dan Williamsb5470dc2008-06-27 21:44:04 -07005411 if (md_allow_write(mddev))
5412 file = kmalloc(sizeof(*file), GFP_NOIO);
5413 else
5414 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005415
NeilBrown32a76272005-06-21 17:17:14 -07005416 if (!file)
5417 goto out;
5418
5419 /* bitmap disabled, zero the first byte and copy out */
5420 if (!mddev->bitmap || !mddev->bitmap->file) {
5421 file->pathname[0] = '\0';
5422 goto copy_out;
5423 }
5424
5425 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5426 if (!buf)
5427 goto out;
5428
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005429 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5430 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005431 goto out;
5432
5433 strcpy(file->pathname, ptr);
5434
5435copy_out:
5436 err = 0;
5437 if (copy_to_user(arg, file, sizeof(*file)))
5438 err = -EFAULT;
5439out:
5440 kfree(buf);
5441 kfree(file);
5442 return err;
5443}
5444
NeilBrownfd01b882011-10-11 16:47:53 +11005445static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
5447 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005448 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
5450 if (copy_from_user(&info, arg, sizeof(info)))
5451 return -EFAULT;
5452
Andre Noll26ef3792008-07-11 22:02:21 +10005453 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (rdev) {
5455 info.major = MAJOR(rdev->bdev->bd_dev);
5456 info.minor = MINOR(rdev->bdev->bd_dev);
5457 info.raid_disk = rdev->raid_disk;
5458 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005459 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005461 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 info.state |= (1<<MD_DISK_ACTIVE);
5463 info.state |= (1<<MD_DISK_SYNC);
5464 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005465 if (test_bit(WriteMostly, &rdev->flags))
5466 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 } else {
5468 info.major = info.minor = 0;
5469 info.raid_disk = -1;
5470 info.state = (1<<MD_DISK_REMOVED);
5471 }
5472
5473 if (copy_to_user(arg, &info, sizeof(info)))
5474 return -EFAULT;
5475
5476 return 0;
5477}
5478
NeilBrownfd01b882011-10-11 16:47:53 +11005479static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
5481 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005482 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 dev_t dev = MKDEV(info->major,info->minor);
5484
5485 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5486 return -EOVERFLOW;
5487
5488 if (!mddev->raid_disks) {
5489 int err;
5490 /* expecting a device which has a superblock */
5491 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5492 if (IS_ERR(rdev)) {
5493 printk(KERN_WARNING
5494 "md: md_import_device returned %ld\n",
5495 PTR_ERR(rdev));
5496 return PTR_ERR(rdev);
5497 }
5498 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005499 struct md_rdev *rdev0
5500 = list_entry(mddev->disks.next,
5501 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005502 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 .load_super(rdev, rdev0, mddev->minor_version);
5504 if (err < 0) {
5505 printk(KERN_WARNING
5506 "md: %s has different UUID to %s\n",
5507 bdevname(rdev->bdev,b),
5508 bdevname(rdev0->bdev,b2));
5509 export_rdev(rdev);
5510 return -EINVAL;
5511 }
5512 }
5513 err = bind_rdev_to_array(rdev, mddev);
5514 if (err)
5515 export_rdev(rdev);
5516 return err;
5517 }
5518
5519 /*
5520 * add_new_disk can be used once the array is assembled
5521 * to add "hot spares". They must already have a superblock
5522 * written
5523 */
5524 if (mddev->pers) {
5525 int err;
5526 if (!mddev->pers->hot_add_disk) {
5527 printk(KERN_WARNING
5528 "%s: personality does not support diskops!\n",
5529 mdname(mddev));
5530 return -EINVAL;
5531 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005532 if (mddev->persistent)
5533 rdev = md_import_device(dev, mddev->major_version,
5534 mddev->minor_version);
5535 else
5536 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 if (IS_ERR(rdev)) {
5538 printk(KERN_WARNING
5539 "md: md_import_device returned %ld\n",
5540 PTR_ERR(rdev));
5541 return PTR_ERR(rdev);
5542 }
NeilBrown1a855a02010-12-09 16:36:28 +11005543 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005544 if (!mddev->persistent) {
5545 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005546 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005547 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005548 set_bit(In_sync, &rdev->flags);
5549 } else
NeilBrown41158c72005-06-21 17:17:25 -07005550 rdev->raid_disk = -1;
5551 } else
5552 super_types[mddev->major_version].
5553 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005554 if ((info->state & (1<<MD_DISK_SYNC)) &&
5555 (!test_bit(In_sync, &rdev->flags) ||
5556 rdev->raid_disk != info->raid_disk)) {
5557 /* This was a hot-add request, but events doesn't
5558 * match, so reject it.
5559 */
5560 export_rdev(rdev);
5561 return -EINVAL;
5562 }
5563
NeilBrown1a855a02010-12-09 16:36:28 +11005564 if (test_bit(In_sync, &rdev->flags))
5565 rdev->saved_raid_disk = rdev->raid_disk;
5566 else
5567 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005568
NeilBrownb2d444d2005-11-08 21:39:31 -08005569 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005570 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5571 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005572 else
5573 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 rdev->raid_disk = -1;
5576 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005577 if (!err && !mddev->pers->hot_remove_disk) {
5578 /* If there is hot_add_disk but no hot_remove_disk
5579 * then added disks for geometry changes,
5580 * and should be added immediately.
5581 */
5582 super_types[mddev->major_version].
5583 validate_super(mddev, rdev);
5584 err = mddev->pers->hot_add_disk(mddev, rdev);
5585 if (err)
5586 unbind_rdev_from_array(rdev);
5587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (err)
5589 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005590 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005591 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005592
NeilBrown17571282006-12-10 02:20:52 -08005593 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005594 if (mddev->degraded)
5595 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005597 if (!err)
5598 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005599 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 return err;
5601 }
5602
5603 /* otherwise, add_new_disk is only allowed
5604 * for major_version==0 superblocks
5605 */
5606 if (mddev->major_version != 0) {
5607 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5608 mdname(mddev));
5609 return -EINVAL;
5610 }
5611
5612 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5613 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005614 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 if (IS_ERR(rdev)) {
5616 printk(KERN_WARNING
5617 "md: error, md_import_device() returned %ld\n",
5618 PTR_ERR(rdev));
5619 return PTR_ERR(rdev);
5620 }
5621 rdev->desc_nr = info->number;
5622 if (info->raid_disk < mddev->raid_disks)
5623 rdev->raid_disk = info->raid_disk;
5624 else
5625 rdev->raid_disk = -1;
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005628 if (info->state & (1<<MD_DISK_SYNC))
5629 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005631 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5632 set_bit(WriteMostly, &rdev->flags);
5633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 if (!mddev->persistent) {
5635 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005636 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5637 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005638 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005639 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
NeilBrown2bf071b2006-01-06 00:20:55 -08005641 err = bind_rdev_to_array(rdev, mddev);
5642 if (err) {
5643 export_rdev(rdev);
5644 return err;
5645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
5647
5648 return 0;
5649}
5650
NeilBrownfd01b882011-10-11 16:47:53 +11005651static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
5653 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005654 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 rdev = find_rdev(mddev, dev);
5657 if (!rdev)
5658 return -ENXIO;
5659
5660 if (rdev->raid_disk >= 0)
5661 goto busy;
5662
5663 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005664 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005665 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 return 0;
5668busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005669 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 bdevname(rdev->bdev,b), mdname(mddev));
5671 return -EBUSY;
5672}
5673
NeilBrownfd01b882011-10-11 16:47:53 +11005674static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 char b[BDEVNAME_SIZE];
5677 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005678 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
5680 if (!mddev->pers)
5681 return -ENODEV;
5682
5683 if (mddev->major_version != 0) {
5684 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5685 " version-0 superblocks.\n",
5686 mdname(mddev));
5687 return -EINVAL;
5688 }
5689 if (!mddev->pers->hot_add_disk) {
5690 printk(KERN_WARNING
5691 "%s: personality does not support diskops!\n",
5692 mdname(mddev));
5693 return -EINVAL;
5694 }
5695
NeilBrownd710e132008-10-13 11:55:12 +11005696 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 if (IS_ERR(rdev)) {
5698 printk(KERN_WARNING
5699 "md: error, md_import_device() returned %ld\n",
5700 PTR_ERR(rdev));
5701 return -EINVAL;
5702 }
5703
5704 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005705 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005707 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
NeilBrown8190e752009-06-18 08:48:58 +10005709 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
NeilBrownb2d444d2005-11-08 21:39:31 -08005711 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 printk(KERN_WARNING
5713 "md: can not hot-add faulty %s disk to %s!\n",
5714 bdevname(rdev->bdev,b), mdname(mddev));
5715 err = -EINVAL;
5716 goto abort_export;
5717 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005718 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005720 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005721 err = bind_rdev_to_array(rdev, mddev);
5722 if (err)
5723 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
5725 /*
5726 * The rest should better be atomic, we can have disk failures
5727 * noticed in interrupt contexts ...
5728 */
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 rdev->raid_disk = -1;
5731
NeilBrown850b2b422006-10-03 01:15:46 -07005732 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
5734 /*
5735 * Kick recovery, maybe this spare has to be added to the
5736 * array immediately.
5737 */
5738 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5739 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005740 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 return 0;
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743abort_export:
5744 export_rdev(rdev);
5745 return err;
5746}
5747
NeilBrownfd01b882011-10-11 16:47:53 +11005748static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005749{
5750 int err;
5751
NeilBrown36fa3062005-09-09 16:23:45 -07005752 if (mddev->pers) {
5753 if (!mddev->pers->quiesce)
5754 return -EBUSY;
5755 if (mddev->recovery || mddev->sync_thread)
5756 return -EBUSY;
5757 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005758 }
5759
NeilBrown36fa3062005-09-09 16:23:45 -07005760
5761 if (fd >= 0) {
5762 if (mddev->bitmap)
5763 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005764 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005765
NeilBrownc3d97142009-12-14 12:49:52 +11005766 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005767 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5768 mdname(mddev));
5769 return -EBADF;
5770 }
5771
NeilBrownc3d97142009-12-14 12:49:52 +11005772 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005773 if (err) {
5774 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5775 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005776 fput(mddev->bitmap_info.file);
5777 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005778 return err;
5779 }
NeilBrownc3d97142009-12-14 12:49:52 +11005780 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005781 } else if (mddev->bitmap == NULL)
5782 return -ENOENT; /* cannot remove what isn't there */
5783 err = 0;
5784 if (mddev->pers) {
5785 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005786 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005787 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005788 if (!err)
5789 err = bitmap_load(mddev);
5790 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005791 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005792 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005793 fd = -1; /* make sure to put the file */
5794 }
NeilBrown36fa3062005-09-09 16:23:45 -07005795 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005796 }
5797 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005798 if (mddev->bitmap_info.file) {
5799 restore_bitmap_write_access(mddev->bitmap_info.file);
5800 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005801 }
NeilBrownc3d97142009-12-14 12:49:52 +11005802 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005803 }
5804
NeilBrown32a76272005-06-21 17:17:14 -07005805 return err;
5806}
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808/*
5809 * set_array_info is used two different ways
5810 * The original usage is when creating a new array.
5811 * In this usage, raid_disks is > 0 and it together with
5812 * level, size, not_persistent,layout,chunksize determine the
5813 * shape of the array.
5814 * This will always create an array with a type-0.90.0 superblock.
5815 * The newer usage is when assembling an array.
5816 * In this case raid_disks will be 0, and the major_version field is
5817 * use to determine which style super-blocks are to be found on the devices.
5818 * The minor and patch _version numbers are also kept incase the
5819 * super_block handler wishes to interpret them.
5820 */
NeilBrownfd01b882011-10-11 16:47:53 +11005821static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
5823
5824 if (info->raid_disks == 0) {
5825 /* just setting version number for superblock loading */
5826 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005827 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 super_types[info->major_version].name == NULL) {
5829 /* maybe try to auto-load a module? */
5830 printk(KERN_INFO
5831 "md: superblock version %d not known\n",
5832 info->major_version);
5833 return -EINVAL;
5834 }
5835 mddev->major_version = info->major_version;
5836 mddev->minor_version = info->minor_version;
5837 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005838 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005839 /* ensure mddev_put doesn't delete this now that there
5840 * is some minimal configuration.
5841 */
5842 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return 0;
5844 }
5845 mddev->major_version = MD_MAJOR_VERSION;
5846 mddev->minor_version = MD_MINOR_VERSION;
5847 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5848 mddev->ctime = get_seconds();
5849
5850 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005851 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005852 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 mddev->raid_disks = info->raid_disks;
5854 /* don't set md_minor, it is determined by which /dev/md* was
5855 * openned
5856 */
5857 if (info->state & (1<<MD_SB_CLEAN))
5858 mddev->recovery_cp = MaxSector;
5859 else
5860 mddev->recovery_cp = 0;
5861 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005862 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
5864 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005865 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 mddev->max_disks = MD_SB_DISKS;
5868
NeilBrowne6910632008-02-06 01:39:51 -08005869 if (mddev->persistent)
5870 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005871 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
NeilBrownc3d97142009-12-14 12:49:52 +11005873 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5874 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005875
NeilBrownf6705572006-03-27 01:18:11 -08005876 mddev->reshape_position = MaxSector;
5877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 /*
5879 * Generate a 128 bit UUID
5880 */
5881 get_random_bytes(mddev->uuid, 16);
5882
NeilBrownf6705572006-03-27 01:18:11 -08005883 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005884 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005885 mddev->new_layout = mddev->layout;
5886 mddev->delta_disks = 0;
5887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return 0;
5889}
5890
NeilBrownfd01b882011-10-11 16:47:53 +11005891void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005892{
Dan Williamsb522adc2009-03-31 15:00:31 +11005893 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5894
5895 if (mddev->external_size)
5896 return;
5897
Dan Williams1f403622009-03-31 14:59:03 +11005898 mddev->array_sectors = array_sectors;
5899}
5900EXPORT_SYMBOL(md_set_array_sectors);
5901
NeilBrownfd01b882011-10-11 16:47:53 +11005902static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005903{
NeilBrown3cb03002011-10-11 16:45:26 +11005904 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005905 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005906 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005907
5908 if (mddev->pers->resize == NULL)
5909 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005910 /* The "num_sectors" is the number of sectors of each device that
5911 * is used. This can only make sense for arrays with redundancy.
5912 * linear and raid0 always use whatever space is available. We can only
5913 * consider changing this number if no resync or reconstruction is
5914 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005915 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005916 * of each device. If num_sectors is zero, we find the largest size
5917 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005918 */
5919 if (mddev->sync_thread)
5920 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005921 if (mddev->bitmap)
5922 /* Sorry, cannot grow a bitmap yet, just remove it,
5923 * grow, and re-add.
5924 */
5925 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005926 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005927 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005928
Andre Nolld71f9f82008-07-11 22:02:22 +10005929 if (fit && (num_sectors == 0 || num_sectors > avail))
5930 num_sectors = avail;
5931 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005932 return -ENOSPC;
5933 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005934 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005935 if (!rv)
5936 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005937 return rv;
5938}
5939
NeilBrownfd01b882011-10-11 16:47:53 +11005940static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08005941{
5942 int rv;
5943 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005944 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005945 return -EINVAL;
5946 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005947 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005948 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005949 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005950 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005951 mddev->delta_disks = raid_disks - mddev->raid_disks;
5952
5953 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005954 if (rv < 0)
5955 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005956 return rv;
5957}
5958
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960/*
5961 * update_array_info is used to change the configuration of an
5962 * on-line array.
5963 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5964 * fields in the info are checked against the array.
5965 * Any differences that cannot be handled will cause an error.
5966 * Normally, only one change can be managed at a time.
5967 */
NeilBrownfd01b882011-10-11 16:47:53 +11005968static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
5970 int rv = 0;
5971 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005972 int state = 0;
5973
5974 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005975 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005976 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
5978 if (mddev->major_version != info->major_version ||
5979 mddev->minor_version != info->minor_version ||
5980/* mddev->patch_version != info->patch_version || */
5981 mddev->ctime != info->ctime ||
5982 mddev->level != info->level ||
5983/* mddev->layout != info->layout || */
5984 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005985 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005986 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5987 ((state^info->state) & 0xfffffe00)
5988 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 return -EINVAL;
5990 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005991 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5992 cnt++;
5993 if (mddev->raid_disks != info->raid_disks)
5994 cnt++;
5995 if (mddev->layout != info->layout)
5996 cnt++;
5997 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5998 cnt++;
5999 if (cnt == 0)
6000 return 0;
6001 if (cnt > 1)
6002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 if (mddev->layout != info->layout) {
6005 /* Change layout
6006 * we don't need to do anything at the md level, the
6007 * personality will take care of it all.
6008 */
NeilBrown50ac1682009-06-18 08:47:55 +10006009 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006011 else {
6012 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006013 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006014 if (rv)
6015 mddev->new_layout = mddev->layout;
6016 return rv;
6017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006019 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006020 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
NeilBrownda943b992006-01-06 00:20:54 -08006022 if (mddev->raid_disks != info->raid_disks)
6023 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
NeilBrown36fa3062005-09-09 16:23:45 -07006025 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6026 if (mddev->pers->quiesce == NULL)
6027 return -EINVAL;
6028 if (mddev->recovery || mddev->sync_thread)
6029 return -EBUSY;
6030 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6031 /* add the bitmap */
6032 if (mddev->bitmap)
6033 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006034 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006035 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006036 mddev->bitmap_info.offset =
6037 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07006038 mddev->pers->quiesce(mddev, 1);
6039 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006040 if (!rv)
6041 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006042 if (rv)
6043 bitmap_destroy(mddev);
6044 mddev->pers->quiesce(mddev, 0);
6045 } else {
6046 /* remove the bitmap */
6047 if (!mddev->bitmap)
6048 return -ENOENT;
6049 if (mddev->bitmap->file)
6050 return -EINVAL;
6051 mddev->pers->quiesce(mddev, 1);
6052 bitmap_destroy(mddev);
6053 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006054 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006055 }
6056 }
NeilBrown850b2b422006-10-03 01:15:46 -07006057 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 return rv;
6059}
6060
NeilBrownfd01b882011-10-11 16:47:53 +11006061static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
NeilBrown3cb03002011-10-11 16:45:26 +11006063 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
6065 if (mddev->pers == NULL)
6066 return -ENODEV;
6067
6068 rdev = find_rdev(mddev, dev);
6069 if (!rdev)
6070 return -ENODEV;
6071
6072 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006073 if (!test_bit(Faulty, &rdev->flags))
6074 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return 0;
6076}
6077
Andre Noll2f9618c2008-04-25 18:57:58 +02006078/*
6079 * We have a problem here : there is no easy way to give a CHS
6080 * virtual geometry. We currently pretend that we have a 2 heads
6081 * 4 sectors (with a BIG number of cylinders...). This drives
6082 * dosfs just mad... ;-)
6083 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006084static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6085{
NeilBrownfd01b882011-10-11 16:47:53 +11006086 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006087
6088 geo->heads = 2;
6089 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006090 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006091 return 0;
6092}
6093
Al Viroa39907f2008-03-02 10:31:15 -05006094static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 unsigned int cmd, unsigned long arg)
6096{
6097 int err = 0;
6098 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006099 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006100 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
NeilBrown506c9e42011-12-23 10:17:26 +11006102 switch (cmd) {
6103 case RAID_VERSION:
6104 case GET_ARRAY_INFO:
6105 case GET_DISK_INFO:
6106 break;
6107 default:
6108 if (!capable(CAP_SYS_ADMIN))
6109 return -EACCES;
6110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
6112 /*
6113 * Commands dealing with the RAID driver but not any
6114 * particular array:
6115 */
6116 switch (cmd)
6117 {
6118 case RAID_VERSION:
6119 err = get_version(argp);
6120 goto done;
6121
6122 case PRINT_RAID_DEBUG:
6123 err = 0;
6124 md_print_devices();
6125 goto done;
6126
6127#ifndef MODULE
6128 case RAID_AUTORUN:
6129 err = 0;
6130 autostart_arrays(arg);
6131 goto done;
6132#endif
6133 default:;
6134 }
6135
6136 /*
6137 * Commands creating/starting a new array:
6138 */
6139
Al Viroa39907f2008-03-02 10:31:15 -05006140 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
6142 if (!mddev) {
6143 BUG();
6144 goto abort;
6145 }
6146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 err = mddev_lock(mddev);
6148 if (err) {
6149 printk(KERN_INFO
6150 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6151 err, cmd);
6152 goto abort;
6153 }
6154
6155 switch (cmd)
6156 {
6157 case SET_ARRAY_INFO:
6158 {
6159 mdu_array_info_t info;
6160 if (!arg)
6161 memset(&info, 0, sizeof(info));
6162 else if (copy_from_user(&info, argp, sizeof(info))) {
6163 err = -EFAULT;
6164 goto abort_unlock;
6165 }
6166 if (mddev->pers) {
6167 err = update_array_info(mddev, &info);
6168 if (err) {
6169 printk(KERN_WARNING "md: couldn't update"
6170 " array info. %d\n", err);
6171 goto abort_unlock;
6172 }
6173 goto done_unlock;
6174 }
6175 if (!list_empty(&mddev->disks)) {
6176 printk(KERN_WARNING
6177 "md: array %s already has disks!\n",
6178 mdname(mddev));
6179 err = -EBUSY;
6180 goto abort_unlock;
6181 }
6182 if (mddev->raid_disks) {
6183 printk(KERN_WARNING
6184 "md: array %s already initialised!\n",
6185 mdname(mddev));
6186 err = -EBUSY;
6187 goto abort_unlock;
6188 }
6189 err = set_array_info(mddev, &info);
6190 if (err) {
6191 printk(KERN_WARNING "md: couldn't set"
6192 " array info. %d\n", err);
6193 goto abort_unlock;
6194 }
6195 }
6196 goto done_unlock;
6197
6198 default:;
6199 }
6200
6201 /*
6202 * Commands querying/configuring an existing array:
6203 */
NeilBrown32a76272005-06-21 17:17:14 -07006204 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006205 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006206 if ((!mddev->raid_disks && !mddev->external)
6207 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6208 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6209 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 err = -ENODEV;
6211 goto abort_unlock;
6212 }
6213
6214 /*
6215 * Commands even a read-only array can execute:
6216 */
6217 switch (cmd)
6218 {
6219 case GET_ARRAY_INFO:
6220 err = get_array_info(mddev, argp);
6221 goto done_unlock;
6222
NeilBrown32a76272005-06-21 17:17:14 -07006223 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006224 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006225 goto done_unlock;
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 case GET_DISK_INFO:
6228 err = get_disk_info(mddev, argp);
6229 goto done_unlock;
6230
6231 case RESTART_ARRAY_RW:
6232 err = restart_array(mddev);
6233 goto done_unlock;
6234
6235 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006236 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 goto done_unlock;
6238
6239 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006240 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 goto done_unlock;
6242
Dan Williamse2218352010-05-12 08:25:37 +10006243 case BLKROSET:
6244 if (get_user(ro, (int __user *)(arg))) {
6245 err = -EFAULT;
6246 goto done_unlock;
6247 }
6248 err = -EINVAL;
6249
6250 /* if the bdev is going readonly the value of mddev->ro
6251 * does not matter, no writes are coming
6252 */
6253 if (ro)
6254 goto done_unlock;
6255
6256 /* are we are already prepared for writes? */
6257 if (mddev->ro != 1)
6258 goto done_unlock;
6259
6260 /* transitioning to readauto need only happen for
6261 * arrays that call md_write_start
6262 */
6263 if (mddev->pers) {
6264 err = restart_array(mddev);
6265 if (err == 0) {
6266 mddev->ro = 2;
6267 set_disk_ro(mddev->gendisk, 0);
6268 }
6269 }
6270 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 }
6272
6273 /*
6274 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006275 * superblock, so we do not allow them on read-only arrays.
6276 * However non-MD ioctls (e.g. get-size) will still come through
6277 * here and hit the 'default' below, so only disallow
6278 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006280 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006281 if (mddev->ro == 2) {
6282 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006283 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006284 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6285 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006286 } else {
6287 err = -EROFS;
6288 goto abort_unlock;
6289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 }
6291
6292 switch (cmd)
6293 {
6294 case ADD_NEW_DISK:
6295 {
6296 mdu_disk_info_t info;
6297 if (copy_from_user(&info, argp, sizeof(info)))
6298 err = -EFAULT;
6299 else
6300 err = add_new_disk(mddev, &info);
6301 goto done_unlock;
6302 }
6303
6304 case HOT_REMOVE_DISK:
6305 err = hot_remove_disk(mddev, new_decode_dev(arg));
6306 goto done_unlock;
6307
6308 case HOT_ADD_DISK:
6309 err = hot_add_disk(mddev, new_decode_dev(arg));
6310 goto done_unlock;
6311
6312 case SET_DISK_FAULTY:
6313 err = set_disk_faulty(mddev, new_decode_dev(arg));
6314 goto done_unlock;
6315
6316 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006317 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 goto done_unlock;
6319
NeilBrown32a76272005-06-21 17:17:14 -07006320 case SET_BITMAP_FILE:
6321 err = set_bitmap_file(mddev, (int)arg);
6322 goto done_unlock;
6323
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 err = -EINVAL;
6326 goto abort_unlock;
6327 }
6328
6329done_unlock:
6330abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006331 if (mddev->hold_active == UNTIL_IOCTL &&
6332 err != -EINVAL)
6333 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 mddev_unlock(mddev);
6335
6336 return err;
6337done:
6338 if (err)
6339 MD_BUG();
6340abort:
6341 return err;
6342}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006343#ifdef CONFIG_COMPAT
6344static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6345 unsigned int cmd, unsigned long arg)
6346{
6347 switch (cmd) {
6348 case HOT_REMOVE_DISK:
6349 case HOT_ADD_DISK:
6350 case SET_DISK_FAULTY:
6351 case SET_BITMAP_FILE:
6352 /* These take in integer arg, do not convert */
6353 break;
6354 default:
6355 arg = (unsigned long)compat_ptr(arg);
6356 break;
6357 }
6358
6359 return md_ioctl(bdev, mode, cmd, arg);
6360}
6361#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Al Viroa39907f2008-03-02 10:31:15 -05006363static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364{
6365 /*
6366 * Succeed if we can lock the mddev, which confirms that
6367 * it isn't being stopped right now.
6368 */
NeilBrownfd01b882011-10-11 16:47:53 +11006369 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 int err;
6371
NeilBrownd3374822009-01-09 08:31:10 +11006372 if (mddev->gendisk != bdev->bd_disk) {
6373 /* we are racing with mddev_put which is discarding this
6374 * bd_disk.
6375 */
6376 mddev_put(mddev);
6377 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006378 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006379 /* Then retry the open from the top */
6380 return -ERESTARTSYS;
6381 }
6382 BUG_ON(mddev != bdev->bd_disk->private_data);
6383
NeilBrownc8c00a62009-08-10 12:50:52 +10006384 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 goto out;
6386
6387 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006388 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006389 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006391 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 out:
6393 return err;
6394}
6395
Al Viroa39907f2008-03-02 10:31:15 -05006396static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397{
NeilBrownfd01b882011-10-11 16:47:53 +11006398 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006400 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006401 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 mddev_put(mddev);
6403
6404 return 0;
6405}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006406
6407static int md_media_changed(struct gendisk *disk)
6408{
NeilBrownfd01b882011-10-11 16:47:53 +11006409 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006410
6411 return mddev->changed;
6412}
6413
6414static int md_revalidate(struct gendisk *disk)
6415{
NeilBrownfd01b882011-10-11 16:47:53 +11006416 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006417
6418 mddev->changed = 0;
6419 return 0;
6420}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006421static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422{
6423 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006424 .open = md_open,
6425 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006426 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006427#ifdef CONFIG_COMPAT
6428 .compat_ioctl = md_compat_ioctl,
6429#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006430 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006431 .media_changed = md_media_changed,
6432 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433};
6434
Adrian Bunk75c96f82005-05-05 16:16:09 -07006435static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436{
NeilBrown2b8bf342011-10-11 16:48:23 +11006437 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 /*
6440 * md_thread is a 'system-thread', it's priority should be very
6441 * high. We avoid resource deadlocks individually in each
6442 * raid personality. (RAID5 does preallocation) We also use RR and
6443 * the very same RT priority as kswapd, thus we will never get
6444 * into a priority inversion deadlock.
6445 *
6446 * we definitely have to have equal or higher priority than
6447 * bdflush, otherwise bdflush will deadlock if there are too
6448 * many dirty RAID5 blocks.
6449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
NeilBrown6985c432005-10-19 21:23:47 -07006451 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006452 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
NeilBrown93588e22005-11-15 00:09:12 -08006454 /* We need to wait INTERRUPTIBLE so that
6455 * we don't add to the load-average.
6456 * That means we need to be sure no signals are
6457 * pending
6458 */
6459 if (signal_pending(current))
6460 flush_signals(current);
6461
6462 wait_event_interruptible_timeout
6463 (thread->wqueue,
6464 test_bit(THREAD_WAKEUP, &thread->flags)
6465 || kthread_should_stop(),
6466 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
NeilBrown6c987912011-01-14 09:13:53 +11006468 clear_bit(THREAD_WAKEUP, &thread->flags);
6469 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006470 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006472
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 return 0;
6474}
6475
NeilBrown2b8bf342011-10-11 16:48:23 +11006476void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477{
6478 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006479 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 set_bit(THREAD_WAKEUP, &thread->flags);
6481 wake_up(&thread->wqueue);
6482 }
6483}
6484
NeilBrown2b8bf342011-10-11 16:48:23 +11006485struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 const char *name)
6487{
NeilBrown2b8bf342011-10-11 16:48:23 +11006488 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
NeilBrown2b8bf342011-10-11 16:48:23 +11006490 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 if (!thread)
6492 return NULL;
6493
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 init_waitqueue_head(&thread->wqueue);
6495
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 thread->run = run;
6497 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006498 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006499 thread->tsk = kthread_run(md_thread, thread,
6500 "%s_%s",
6501 mdname(thread->mddev),
6502 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006503 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 kfree(thread);
6505 return NULL;
6506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 return thread;
6508}
6509
NeilBrown2b8bf342011-10-11 16:48:23 +11006510void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511{
NeilBrown2b8bf342011-10-11 16:48:23 +11006512 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006513 if (!thread)
6514 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006515 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006516 /* Locking ensures that mddev_unlock does not wake_up a
6517 * non-existent thread
6518 */
6519 spin_lock(&pers_lock);
6520 *threadp = NULL;
6521 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006522
6523 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 kfree(thread);
6525}
6526
NeilBrownfd01b882011-10-11 16:47:53 +11006527void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
6529 if (!mddev) {
6530 MD_BUG();
6531 return;
6532 }
6533
NeilBrownb2d444d2005-11-08 21:39:31 -08006534 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006536
NeilBrownde393cd2011-07-28 11:31:48 +10006537 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 return;
6539 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006540 if (mddev->degraded)
6541 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006542 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6544 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6545 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006546 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006547 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006548 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549}
6550
6551/* seq_file implementation /proc/mdstat */
6552
6553static void status_unused(struct seq_file *seq)
6554{
6555 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006556 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
6558 seq_printf(seq, "unused devices: ");
6559
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006560 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 char b[BDEVNAME_SIZE];
6562 i++;
6563 seq_printf(seq, "%s ",
6564 bdevname(rdev->bdev,b));
6565 }
6566 if (!i)
6567 seq_printf(seq, "<none>");
6568
6569 seq_printf(seq, "\n");
6570}
6571
6572
NeilBrownfd01b882011-10-11 16:47:53 +11006573static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574{
NeilBrowndd71cf62009-05-07 12:49:35 +10006575 sector_t max_sectors, resync, res;
6576 unsigned long dt, db;
6577 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006578 int scale;
6579 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
NeilBrowndd71cf62009-05-07 12:49:35 +10006581 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
6583 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006584 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006586 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
6588 /*
6589 * Should not happen.
6590 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006591 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 MD_BUG();
6593 return;
6594 }
NeilBrown4588b422006-03-27 01:18:04 -08006595 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006596 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006597 * u32, as those are the requirements for sector_div.
6598 * Thus 'scale' must be at least 10
6599 */
6600 scale = 10;
6601 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006602 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006603 scale++;
6604 }
6605 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006606 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006607
6608 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 {
NeilBrown4588b422006-03-27 01:18:04 -08006610 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 seq_printf(seq, "[");
6612 for (i = 0; i < x; i++)
6613 seq_printf(seq, "=");
6614 seq_printf(seq, ">");
6615 for (i = 0; i < y; i++)
6616 seq_printf(seq, ".");
6617 seq_printf(seq, "] ");
6618 }
NeilBrown4588b422006-03-27 01:18:04 -08006619 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006620 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6621 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006622 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6623 "check" :
6624 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6625 "resync" : "recovery"))),
6626 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006627 (unsigned long long) resync/2,
6628 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
6630 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 * dt: time from mark until now
6632 * db: blocks written from mark until now
6633 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006634 *
6635 * rt is a sector_t, so could be 32bit or 64bit.
6636 * So we divide before multiply in case it is 32bit and close
6637 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006638 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006639 * near the end of resync when the number of remaining sectors
6640 * is close to 'db'.
6641 * We then divide rt by 32 after multiplying by db to compensate.
6642 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 */
6644 dt = ((jiffies - mddev->resync_mark) / HZ);
6645 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006646 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6647 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
NeilBrowndd71cf62009-05-07 12:49:35 +10006649 rt = max_sectors - resync; /* number of remaining sectors */
6650 sector_div(rt, db/32+1);
6651 rt *= dt;
6652 rt >>= 5;
6653
6654 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6655 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
NeilBrownff4e8d92006-07-10 04:44:16 -07006657 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658}
6659
6660static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6661{
6662 struct list_head *tmp;
6663 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006664 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
6666 if (l >= 0x10000)
6667 return NULL;
6668 if (!l--)
6669 /* header */
6670 return (void*)1;
6671
6672 spin_lock(&all_mddevs_lock);
6673 list_for_each(tmp,&all_mddevs)
6674 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006675 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 mddev_get(mddev);
6677 spin_unlock(&all_mddevs_lock);
6678 return mddev;
6679 }
6680 spin_unlock(&all_mddevs_lock);
6681 if (!l--)
6682 return (void*)2;/* tail */
6683 return NULL;
6684}
6685
6686static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6687{
6688 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006689 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
6691 ++*pos;
6692 if (v == (void*)2)
6693 return NULL;
6694
6695 spin_lock(&all_mddevs_lock);
6696 if (v == (void*)1)
6697 tmp = all_mddevs.next;
6698 else
6699 tmp = mddev->all_mddevs.next;
6700 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006701 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 else {
6703 next_mddev = (void*)2;
6704 *pos = 0x10000;
6705 }
6706 spin_unlock(&all_mddevs_lock);
6707
6708 if (v != (void*)1)
6709 mddev_put(mddev);
6710 return next_mddev;
6711
6712}
6713
6714static void md_seq_stop(struct seq_file *seq, void *v)
6715{
NeilBrownfd01b882011-10-11 16:47:53 +11006716 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
6718 if (mddev && v != (void*)1 && v != (void*)2)
6719 mddev_put(mddev);
6720}
6721
6722static int md_seq_show(struct seq_file *seq, void *v)
6723{
NeilBrownfd01b882011-10-11 16:47:53 +11006724 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006725 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006726 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006727 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
6729 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006730 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 seq_printf(seq, "Personalities : ");
6732 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006733 list_for_each_entry(pers, &pers_list, list)
6734 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
6736 spin_unlock(&pers_lock);
6737 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006738 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 return 0;
6740 }
6741 if (v == (void*)2) {
6742 status_unused(seq);
6743 return 0;
6744 }
6745
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006746 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6750 seq_printf(seq, "%s : %sactive", mdname(mddev),
6751 mddev->pers ? "" : "in");
6752 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006753 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006755 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006756 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 seq_printf(seq, " %s", mddev->pers->name);
6758 }
6759
Andre Nolldd8ac332009-03-31 14:33:13 +11006760 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006761 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 char b[BDEVNAME_SIZE];
6763 seq_printf(seq, " %s[%d]",
6764 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006765 if (test_bit(WriteMostly, &rdev->flags))
6766 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006767 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 seq_printf(seq, "(F)");
6769 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006770 }
6771 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006772 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006773 if (test_bit(Replacement, &rdev->flags))
6774 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006775 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 }
6777
6778 if (!list_empty(&mddev->disks)) {
6779 if (mddev->pers)
6780 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006781 (unsigned long long)
6782 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 else
6784 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006785 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006787 if (mddev->persistent) {
6788 if (mddev->major_version != 0 ||
6789 mddev->minor_version != 90) {
6790 seq_printf(seq," super %d.%d",
6791 mddev->major_version,
6792 mddev->minor_version);
6793 }
NeilBrowne6910632008-02-06 01:39:51 -08006794 } else if (mddev->external)
6795 seq_printf(seq, " super external:%s",
6796 mddev->metadata_type);
6797 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006798 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
6800 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006801 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006803 if (mddev->pers->sync_request) {
6804 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006805 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006806 seq_printf(seq, "\n ");
6807 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6808 seq_printf(seq, "\tresync=DELAYED\n ");
6809 else if (mddev->recovery_cp < MaxSector)
6810 seq_printf(seq, "\tresync=PENDING\n ");
6811 }
NeilBrown32a76272005-06-21 17:17:14 -07006812 } else
6813 seq_printf(seq, "\n ");
6814
6815 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006816 unsigned long chunk_kb;
6817 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006818 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006819 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006820 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6821 "%lu%s chunk",
6822 bitmap->pages - bitmap->missing_pages,
6823 bitmap->pages,
6824 (bitmap->pages - bitmap->missing_pages)
6825 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006826 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006827 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006828 if (bitmap->file) {
6829 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006830 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006831 }
NeilBrown78d742d2005-06-21 17:17:15 -07006832
NeilBrown32a76272005-06-21 17:17:14 -07006833 seq_printf(seq, "\n");
6834 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 }
6836
6837 seq_printf(seq, "\n");
6838 }
6839 mddev_unlock(mddev);
6840
6841 return 0;
6842}
6843
Jan Engelhardt110518b2009-05-07 12:49:37 +10006844static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 .start = md_seq_start,
6846 .next = md_seq_next,
6847 .stop = md_seq_stop,
6848 .show = md_seq_show,
6849};
6850
6851static int md_seq_open(struct inode *inode, struct file *file)
6852{
Kay Sieversf1514632011-07-12 20:48:39 +02006853 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 int error;
6855
6856 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006857 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006858 return error;
6859
6860 seq = file->private_data;
6861 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 return error;
6863}
6864
NeilBrownd7603b72006-01-06 00:20:30 -08006865static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6866{
Kay Sieversf1514632011-07-12 20:48:39 +02006867 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006868 int mask;
6869
6870 poll_wait(filp, &md_event_waiters, wait);
6871
6872 /* always allow read */
6873 mask = POLLIN | POLLRDNORM;
6874
Kay Sieversf1514632011-07-12 20:48:39 +02006875 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006876 mask |= POLLERR | POLLPRI;
6877 return mask;
6878}
6879
Arjan van de Venfa027c22007-02-12 00:55:33 -08006880static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006881 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 .open = md_seq_open,
6883 .read = seq_read,
6884 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006885 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006886 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887};
6888
NeilBrown84fc4b52011-10-11 16:49:58 +11006889int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006892 list_add_tail(&p->list, &pers_list);
6893 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 spin_unlock(&pers_lock);
6895 return 0;
6896}
6897
NeilBrown84fc4b52011-10-11 16:49:58 +11006898int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899{
NeilBrown2604b702006-01-06 00:20:36 -08006900 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006902 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 spin_unlock(&pers_lock);
6904 return 0;
6905}
6906
NeilBrownfd01b882011-10-11 16:47:53 +11006907static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908{
NeilBrown3cb03002011-10-11 16:45:26 +11006909 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006911 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
6913 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006914 rcu_read_lock();
6915 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006917 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6918 (int)part_stat_read(&disk->part0, sectors[1]) -
6919 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006920 /* sync IO will cause sync_io to increase before the disk_stats
6921 * as sync_io is counted when a request starts, and
6922 * disk_stats is counted when it completes.
6923 * So resync activity will cause curr_events to be smaller than
6924 * when there was no such activity.
6925 * non-sync IO will cause disk_stat to increase without
6926 * increasing sync_io so curr_events will (eventually)
6927 * be larger than it was before. Once it becomes
6928 * substantially larger, the test below will cause
6929 * the array to appear non-idle, and resync will slow
6930 * down.
6931 * If there is a lot of outstanding resync activity when
6932 * we set last_event to curr_events, then all that activity
6933 * completing might cause the array to appear non-idle
6934 * and resync will be slowed down even though there might
6935 * not have been non-resync activity. This will only
6936 * happen once though. 'last_events' will soon reflect
6937 * the state where there is little or no outstanding
6938 * resync requests, and further resync activity will
6939 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006940 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006942 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 rdev->last_events = curr_events;
6944 idle = 0;
6945 }
6946 }
NeilBrown4b809912008-07-21 17:05:25 +10006947 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 return idle;
6949}
6950
NeilBrownfd01b882011-10-11 16:47:53 +11006951void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952{
6953 /* another "blocks" (512byte) blocks have been synced */
6954 atomic_sub(blocks, &mddev->recovery_active);
6955 wake_up(&mddev->recovery_wait);
6956 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 md_wakeup_thread(mddev->thread);
6959 // stop recovery, signal do_sync ....
6960 }
6961}
6962
6963
NeilBrown06d91a52005-06-21 17:17:12 -07006964/* md_write_start(mddev, bi)
6965 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006966 * in superblock) before writing, schedule a superblock update
6967 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006968 */
NeilBrownfd01b882011-10-11 16:47:53 +11006969void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970{
Neil Brown0fd62b82008-06-28 08:31:36 +10006971 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006972 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006973 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006974
NeilBrownf91de922005-11-08 21:39:36 -08006975 BUG_ON(mddev->ro == 1);
6976 if (mddev->ro == 2) {
6977 /* need to switch to read/write */
6978 mddev->ro = 0;
6979 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6980 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006981 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006982 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006983 }
NeilBrown06d91a52005-06-21 17:17:12 -07006984 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006985 if (mddev->safemode == 1)
6986 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006987 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006988 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006989 if (mddev->in_sync) {
6990 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006991 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006992 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006993 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006994 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006995 }
NeilBrowna9701a32005-11-08 21:39:34 -08006996 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006997 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006998 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006999 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007000 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007001 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002}
7003
NeilBrownfd01b882011-10-11 16:47:53 +11007004void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005{
7006 if (atomic_dec_and_test(&mddev->writes_pending)) {
7007 if (mddev->safemode == 2)
7008 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007009 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7011 }
7012}
7013
NeilBrown2a2275d2007-01-26 00:57:11 -08007014/* md_allow_write(mddev)
7015 * Calling this ensures that the array is marked 'active' so that writes
7016 * may proceed without blocking. It is important to call this before
7017 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7018 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007019 *
7020 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7021 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007022 */
NeilBrownfd01b882011-10-11 16:47:53 +11007023int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007024{
7025 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007026 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007027 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007028 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007029 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007030 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007031
7032 spin_lock_irq(&mddev->write_lock);
7033 if (mddev->in_sync) {
7034 mddev->in_sync = 0;
7035 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007036 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007037 if (mddev->safemode_delay &&
7038 mddev->safemode == 0)
7039 mddev->safemode = 1;
7040 spin_unlock_irq(&mddev->write_lock);
7041 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007042 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007043 } else
7044 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007045
NeilBrown070dc6d2010-08-30 17:33:34 +10007046 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007047 return -EAGAIN;
7048 else
7049 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007050}
7051EXPORT_SYMBOL_GPL(md_allow_write);
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053#define SYNC_MARKS 10
7054#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007055void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056{
NeilBrownfd01b882011-10-11 16:47:53 +11007057 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 unsigned int currspeed = 0,
7059 window;
NeilBrown57afd892005-06-21 17:17:13 -07007060 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 unsigned long mark[SYNC_MARKS];
7062 sector_t mark_cnt[SYNC_MARKS];
7063 int last_mark,m;
7064 struct list_head *tmp;
7065 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007066 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007067 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007068 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
7070 /* just incase thread restarts... */
7071 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7072 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007073 if (mddev->ro) /* never try to sync a read-only array */
7074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
NeilBrown61df9d92006-10-03 01:15:57 -07007076 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7077 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7078 desc = "data-check";
7079 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7080 desc = "requested-resync";
7081 else
7082 desc = "resync";
7083 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7084 desc = "reshape";
7085 else
7086 desc = "recovery";
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 /* we overload curr_resync somewhat here.
7089 * 0 == not engaged in resync at all
7090 * 2 == checking that there is no conflict with another sync
7091 * 1 == like 2, but have yielded to allow conflicting resync to
7092 * commense
7093 * other == active in resync - this many blocks
7094 *
7095 * Before starting a resync we must have set curr_resync to
7096 * 2, and then checked that every "conflicting" array has curr_resync
7097 * less than ours. When we find one that is the same or higher
7098 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7099 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7100 * This will mean we have to start checking from the beginning again.
7101 *
7102 */
7103
7104 do {
7105 mddev->curr_resync = 2;
7106
7107 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007108 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007109 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007110
7111 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007113 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 if (mddev2 == mddev)
7115 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007116 if (!mddev->parallel_resync
7117 && mddev2->curr_resync
7118 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 DEFINE_WAIT(wq);
7120 if (mddev < mddev2 && mddev->curr_resync == 2) {
7121 /* arbitrarily yield */
7122 mddev->curr_resync = 1;
7123 wake_up(&resync_wait);
7124 }
7125 if (mddev > mddev2 && mddev->curr_resync == 1)
7126 /* no need to wait here, we can wait the next
7127 * time 'round when curr_resync == 2
7128 */
7129 continue;
NeilBrown97441972008-09-19 11:49:54 +10007130 /* We need to wait 'interruptible' so as not to
7131 * contribute to the load average, and not to
7132 * be caught by 'softlockup'
7133 */
7134 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007135 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007136 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007137 printk(KERN_INFO "md: delaying %s of %s"
7138 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007140 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007142 if (signal_pending(current))
7143 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 schedule();
7145 finish_wait(&resync_wait, &wq);
7146 goto try_again;
7147 }
7148 finish_wait(&resync_wait, &wq);
7149 }
7150 }
7151 } while (mddev->curr_resync < 2);
7152
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007153 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007154 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007156 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 */
7158 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007159 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007160 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007161 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7162 j = mddev->resync_min;
7163 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007164 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007165
NeilBrownccfcc3c2006-03-27 01:18:09 -08007166 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007167 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007168 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007170 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007171 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007172 rcu_read_lock();
7173 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007174 if (rdev->raid_disk >= 0 &&
7175 !test_bit(Faulty, &rdev->flags) &&
7176 !test_bit(In_sync, &rdev->flags) &&
7177 rdev->recovery_offset < j)
7178 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007179 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181
NeilBrown61df9d92006-10-03 01:15:57 -07007182 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7183 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7184 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007185 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007186 "(but not more than %d KB/sec) for %s.\n",
7187 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
NeilBrowneea1bf32009-03-31 14:27:02 +11007189 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007190
NeilBrown57afd892005-06-21 17:17:13 -07007191 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 for (m = 0; m < SYNC_MARKS; m++) {
7193 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007194 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 }
7196 last_mark = 0;
7197 mddev->resync_mark = mark[last_mark];
7198 mddev->resync_mark_cnt = mark_cnt[last_mark];
7199
7200 /*
7201 * Tune reconstruction:
7202 */
7203 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007204 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7205 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
7207 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 last_check = 0;
7209
7210 if (j>2) {
7211 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007212 "md: resuming %s of %s from checkpoint.\n",
7213 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 mddev->curr_resync = j;
7215 }
NeilBrown75d3da42011-01-14 09:14:34 +11007216 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217
7218 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007219 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
NeilBrown57afd892005-06-21 17:17:13 -07007221 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007222
NeilBrown7a91ee12009-05-26 12:57:21 +10007223 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7224 ((mddev->curr_resync > mddev->curr_resync_completed &&
7225 (mddev->curr_resync - mddev->curr_resync_completed)
7226 > (max_sectors >> 4)) ||
7227 (j - mddev->curr_resync_completed)*2
7228 >= mddev->resync_max - mddev->curr_resync_completed
7229 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007230 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007231 wait_event(mddev->recovery_wait,
7232 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007233 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007234 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007235 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007236 }
NeilBrownacb180b2009-04-14 16:28:34 +10007237
NeilBrowne62e58a2009-07-01 13:15:35 +10007238 while (j >= mddev->resync_max && !kthread_should_stop()) {
7239 /* As this condition is controlled by user-space,
7240 * we can block indefinitely, so use '_interruptible'
7241 * to avoid triggering warnings.
7242 */
7243 flush_signals(current); /* just in case */
7244 wait_event_interruptible(mddev->recovery_wait,
7245 mddev->resync_max > j
7246 || kthread_should_stop());
7247 }
NeilBrownacb180b2009-04-14 16:28:34 +10007248
7249 if (kthread_should_stop())
7250 goto interrupted;
7251
NeilBrown57afd892005-06-21 17:17:13 -07007252 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007253 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007254 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007255 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 goto out;
7257 }
NeilBrown57afd892005-06-21 17:17:13 -07007258
7259 if (!skipped) { /* actual IO requested */
7260 io_sectors += sectors;
7261 atomic_add(sectors, &mddev->recovery_active);
7262 }
7263
NeilBrowne875ece2011-07-28 11:39:24 +10007264 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7265 break;
7266
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 j += sectors;
7268 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007269 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007270 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007271 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007272 * visible in /proc/mdstat
7273 */
7274 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007275
7276 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 continue;
7278
NeilBrown57afd892005-06-21 17:17:13 -07007279 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 repeat:
7281 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7282 /* step marks */
7283 int next = (last_mark+1) % SYNC_MARKS;
7284
7285 mddev->resync_mark = mark[next];
7286 mddev->resync_mark_cnt = mark_cnt[next];
7287 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007288 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 last_mark = next;
7290 }
7291
7292
NeilBrownc6207272008-02-06 01:39:52 -08007293 if (kthread_should_stop())
7294 goto interrupted;
7295
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 /*
7298 * this loop exits only if either when we are slower than
7299 * the 'hard' speed limit, or the system was IO-idle for
7300 * a jiffy.
7301 * the system might be non-idle CPU-wise, but we only care
7302 * about not overloading the IO subsystem. (things like an
7303 * e2fsck being done on the RAID array should execute fast)
7304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 cond_resched();
7306
NeilBrown57afd892005-06-21 17:17:13 -07007307 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7308 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
NeilBrown88202a02006-01-06 00:21:36 -08007310 if (currspeed > speed_min(mddev)) {
7311 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007312 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007313 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 goto repeat;
7315 }
7316 }
7317 }
NeilBrown61df9d92006-10-03 01:15:57 -07007318 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 /*
7320 * this also signals 'finished resyncing' to md_stop
7321 */
7322 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7324
7325 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007326 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
NeilBrowndfc70642008-05-23 13:04:39 -07007328 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007329 mddev->curr_resync > 2) {
7330 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7331 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7332 if (mddev->curr_resync >= mddev->recovery_cp) {
7333 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007334 "md: checkpointing %s of %s.\n",
7335 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007336 mddev->recovery_cp =
7337 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007338 }
7339 } else
7340 mddev->recovery_cp = MaxSector;
7341 } else {
7342 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7343 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007344 rcu_read_lock();
7345 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007346 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007347 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007348 !test_bit(Faulty, &rdev->flags) &&
7349 !test_bit(In_sync, &rdev->flags) &&
7350 rdev->recovery_offset < mddev->curr_resync)
7351 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007352 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007355 skip:
NeilBrown17571282006-12-10 02:20:52 -08007356 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
NeilBrownc07b70a2009-12-14 12:49:48 +11007358 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7359 /* We completed so min/max setting can be forgotten if used. */
7360 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7361 mddev->resync_min = 0;
7362 mddev->resync_max = MaxSector;
7363 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7364 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 mddev->curr_resync = 0;
7366 wake_up(&resync_wait);
7367 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7368 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007369 return;
7370
7371 interrupted:
7372 /*
7373 * got a signal, exit.
7374 */
7375 printk(KERN_INFO
7376 "md: md_do_sync() got signal ... exiting\n");
7377 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7378 goto out;
7379
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380}
NeilBrown29269552006-03-27 01:18:10 -08007381EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
NeilBrownfd01b882011-10-11 16:47:53 +11007383static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007384{
NeilBrown3cb03002011-10-11 16:45:26 +11007385 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007386 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007387 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007388
NeilBrown97e4f422009-03-31 14:33:13 +11007389 mddev->curr_resync_completed = 0;
7390
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007391 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007392 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007393 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007394 (test_bit(Faulty, &rdev->flags) ||
7395 ! test_bit(In_sync, &rdev->flags)) &&
7396 atomic_read(&rdev->nr_pending)==0) {
7397 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007398 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007399 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007400 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007401 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007402 }
7403 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007404 if (removed)
7405 sysfs_notify(&mddev->kobj, NULL,
7406 "degraded");
7407
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007408
NeilBrown7bfec5f2011-12-23 10:17:53 +11007409 list_for_each_entry(rdev, &mddev->disks, same_set) {
7410 if (rdev->raid_disk >= 0 &&
7411 !test_bit(In_sync, &rdev->flags) &&
7412 !test_bit(Faulty, &rdev->flags))
7413 spares++;
7414 if (rdev->raid_disk < 0
7415 && !test_bit(Faulty, &rdev->flags)) {
7416 rdev->recovery_offset = 0;
7417 if (mddev->pers->
7418 hot_add_disk(mddev, rdev) == 0) {
7419 if (sysfs_link_rdev(mddev, rdev))
7420 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007421 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007422 md_new_event(mddev);
7423 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007424 }
NeilBrowndfc70642008-05-23 13:04:39 -07007425 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007426 }
7427 return spares;
7428}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007429
NeilBrownfd01b882011-10-11 16:47:53 +11007430static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007431{
NeilBrown3cb03002011-10-11 16:45:26 +11007432 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007433
7434 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007435 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007436 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7437 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7438 /* success...*/
7439 /* activate any spares */
7440 if (mddev->pers->spare_active(mddev))
7441 sysfs_notify(&mddev->kobj, NULL,
7442 "degraded");
7443 }
7444 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7445 mddev->pers->finish_reshape)
7446 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007447
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007448 /* If array is no-longer degraded, then any saved_raid_disk
7449 * information must be scrapped. Also if any device is now
7450 * In_sync we must scrape the saved_raid_disk for that device
7451 * do the superblock for an incrementally recovered device
7452 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007453 */
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007454 list_for_each_entry(rdev, &mddev->disks, same_set)
7455 if (!mddev->degraded ||
7456 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007457 rdev->saved_raid_disk = -1;
7458
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007459 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007460 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7461 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7462 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7463 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7464 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7465 /* flag recovery needed just to double check */
7466 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7467 sysfs_notify_dirent_safe(mddev->sysfs_action);
7468 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007469 if (mddev->event_work.func)
7470 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007471}
7472
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473/*
7474 * This routine is regularly called by all per-raid-array threads to
7475 * deal with generic issues like resync and super-block update.
7476 * Raid personalities that don't have a thread (linear/raid0) do not
7477 * need this as they never do any recovery or update the superblock.
7478 *
7479 * It does not do any resync itself, but rather "forks" off other threads
7480 * to do that as needed.
7481 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7482 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007483 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 * and wakeups up this thread which will reap the thread and finish up.
7485 * This thread also removes any faulty devices (with nr_pending == 0).
7486 *
7487 * The overall approach is:
7488 * 1/ if the superblock needs updating, update it.
7489 * 2/ If a recovery thread is running, don't do anything else.
7490 * 3/ If recovery has finished, clean up, possibly marking spares active.
7491 * 4/ If there are any faulty devices, remove them.
7492 * 5/ If array is degraded, try to add spares devices
7493 * 6/ If array has spares or is not in-sync, start a resync thread.
7494 */
NeilBrownfd01b882011-10-11 16:47:53 +11007495void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007497 if (mddev->suspended)
7498 return;
7499
NeilBrown5f404022005-06-21 17:17:16 -07007500 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007501 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
NeilBrownfca4d842005-06-21 17:17:11 -07007503 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007504 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007505 printk(KERN_INFO "md: %s in immediate safe mode\n",
7506 mdname(mddev));
7507 mddev->safemode = 2;
7508 }
7509 flush_signals(current);
7510 }
7511
NeilBrownc89a8ee2008-08-05 15:54:13 +10007512 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7513 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007515 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007517 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007518 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007519 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7520 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 ))
7522 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007523
NeilBrowndf5b89b2006-03-27 01:18:20 -08007524 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007525 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007526
NeilBrownc89a8ee2008-08-05 15:54:13 +10007527 if (mddev->ro) {
7528 /* Only thing we do on a ro array is remove
7529 * failed devices.
7530 */
NeilBrown3cb03002011-10-11 16:45:26 +11007531 struct md_rdev *rdev;
NeilBrowna8c42c72011-01-31 13:47:13 +11007532 list_for_each_entry(rdev, &mddev->disks, same_set)
7533 if (rdev->raid_disk >= 0 &&
7534 !test_bit(Blocked, &rdev->flags) &&
7535 test_bit(Faulty, &rdev->flags) &&
7536 atomic_read(&rdev->nr_pending)==0) {
7537 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007538 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007539 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007540 rdev->raid_disk = -1;
7541 }
7542 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007543 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7544 goto unlock;
7545 }
7546
NeilBrown31a59e32008-04-30 00:52:30 -07007547 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007548 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007549 spin_lock_irq(&mddev->write_lock);
7550 if (mddev->safemode &&
7551 !atomic_read(&mddev->writes_pending) &&
7552 !mddev->in_sync &&
7553 mddev->recovery_cp == MaxSector) {
7554 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007555 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007556 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007557 }
7558 if (mddev->safemode == 1)
7559 mddev->safemode = 0;
7560 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007561 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007562 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007563 }
NeilBrownfca4d842005-06-21 17:17:11 -07007564
NeilBrown850b2b422006-10-03 01:15:46 -07007565 if (mddev->flags)
7566 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007567
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7569 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7570 /* resync/recovery still happening */
7571 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7572 goto unlock;
7573 }
7574 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007575 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 goto unlock;
7577 }
Neil Brown72a23c22008-06-28 08:31:41 +10007578 /* Set RUNNING before clearing NEEDED to avoid
7579 * any transients in the value of "sync_action".
7580 */
7581 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7582 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007583 /* Clear some bits that don't mean anything, but
7584 * might be left set
7585 */
NeilBrown24dd4692005-11-08 21:39:26 -08007586 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7587 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007589 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7590 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 /* no recovery is running.
7592 * remove any failed drives, then
7593 * add spares if possible.
7594 * Spare are also removed and re-added, to allow
7595 * the personality to fail the re-add.
7596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007598 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007599 if (mddev->pers->check_reshape == NULL ||
7600 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007601 /* Cannot proceed */
7602 goto unlock;
7603 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007604 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007605 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007606 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7607 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007608 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007609 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007610 } else if (mddev->recovery_cp < MaxSector) {
7611 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007612 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007613 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7614 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007616
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007618 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7619 /* We are adding a device or devices to an array
7620 * which has the bitmap stored on all devices.
7621 * So make sure all bitmap pages get written
7622 */
7623 bitmap_write_all(mddev->bitmap);
7624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 mddev->sync_thread = md_register_thread(md_do_sync,
7626 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007627 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 if (!mddev->sync_thread) {
7629 printk(KERN_ERR "%s: could not start resync"
7630 " thread...\n",
7631 mdname(mddev));
7632 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007633 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7634 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7635 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7636 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7637 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007638 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007640 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007641 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 }
7643 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007644 if (!mddev->sync_thread) {
7645 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7646 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7647 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007648 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007649 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 mddev_unlock(mddev);
7652 }
7653}
7654
NeilBrownfd01b882011-10-11 16:47:53 +11007655void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007656{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007657 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007658 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007659 !test_bit(Blocked, &rdev->flags) &&
7660 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007661 msecs_to_jiffies(5000));
7662 rdev_dec_pending(rdev, mddev);
7663}
7664EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7665
NeilBrown2230dfe2011-07-28 11:31:46 +10007666
7667/* Bad block management.
7668 * We can record which blocks on each device are 'bad' and so just
7669 * fail those blocks, or that stripe, rather than the whole device.
7670 * Entries in the bad-block table are 64bits wide. This comprises:
7671 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7672 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7673 * A 'shift' can be set so that larger blocks are tracked and
7674 * consequently larger devices can be covered.
7675 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7676 *
7677 * Locking of the bad-block table uses a seqlock so md_is_badblock
7678 * might need to retry if it is very unlucky.
7679 * We will sometimes want to check for bad blocks in a bi_end_io function,
7680 * so we use the write_seqlock_irq variant.
7681 *
7682 * When looking for a bad block we specify a range and want to
7683 * know if any block in the range is bad. So we binary-search
7684 * to the last range that starts at-or-before the given endpoint,
7685 * (or "before the sector after the target range")
7686 * then see if it ends after the given start.
7687 * We return
7688 * 0 if there are no known bad blocks in the range
7689 * 1 if there are known bad block which are all acknowledged
7690 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7691 * plus the start/length of the first bad section we overlap.
7692 */
7693int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7694 sector_t *first_bad, int *bad_sectors)
7695{
7696 int hi;
7697 int lo = 0;
7698 u64 *p = bb->page;
7699 int rv = 0;
7700 sector_t target = s + sectors;
7701 unsigned seq;
7702
7703 if (bb->shift > 0) {
7704 /* round the start down, and the end up */
7705 s >>= bb->shift;
7706 target += (1<<bb->shift) - 1;
7707 target >>= bb->shift;
7708 sectors = target - s;
7709 }
7710 /* 'target' is now the first block after the bad range */
7711
7712retry:
7713 seq = read_seqbegin(&bb->lock);
7714
7715 hi = bb->count;
7716
7717 /* Binary search between lo and hi for 'target'
7718 * i.e. for the last range that starts before 'target'
7719 */
7720 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7721 * are known not to be the last range before target.
7722 * VARIANT: hi-lo is the number of possible
7723 * ranges, and decreases until it reaches 1
7724 */
7725 while (hi - lo > 1) {
7726 int mid = (lo + hi) / 2;
7727 sector_t a = BB_OFFSET(p[mid]);
7728 if (a < target)
7729 /* This could still be the one, earlier ranges
7730 * could not. */
7731 lo = mid;
7732 else
7733 /* This and later ranges are definitely out. */
7734 hi = mid;
7735 }
7736 /* 'lo' might be the last that started before target, but 'hi' isn't */
7737 if (hi > lo) {
7738 /* need to check all range that end after 's' to see if
7739 * any are unacknowledged.
7740 */
7741 while (lo >= 0 &&
7742 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7743 if (BB_OFFSET(p[lo]) < target) {
7744 /* starts before the end, and finishes after
7745 * the start, so they must overlap
7746 */
7747 if (rv != -1 && BB_ACK(p[lo]))
7748 rv = 1;
7749 else
7750 rv = -1;
7751 *first_bad = BB_OFFSET(p[lo]);
7752 *bad_sectors = BB_LEN(p[lo]);
7753 }
7754 lo--;
7755 }
7756 }
7757
7758 if (read_seqretry(&bb->lock, seq))
7759 goto retry;
7760
7761 return rv;
7762}
7763EXPORT_SYMBOL_GPL(md_is_badblock);
7764
7765/*
7766 * Add a range of bad blocks to the table.
7767 * This might extend the table, or might contract it
7768 * if two adjacent ranges can be merged.
7769 * We binary-search to find the 'insertion' point, then
7770 * decide how best to handle it.
7771 */
7772static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7773 int acknowledged)
7774{
7775 u64 *p;
7776 int lo, hi;
7777 int rv = 1;
7778
7779 if (bb->shift < 0)
7780 /* badblocks are disabled */
7781 return 0;
7782
7783 if (bb->shift) {
7784 /* round the start down, and the end up */
7785 sector_t next = s + sectors;
7786 s >>= bb->shift;
7787 next += (1<<bb->shift) - 1;
7788 next >>= bb->shift;
7789 sectors = next - s;
7790 }
7791
7792 write_seqlock_irq(&bb->lock);
7793
7794 p = bb->page;
7795 lo = 0;
7796 hi = bb->count;
7797 /* Find the last range that starts at-or-before 's' */
7798 while (hi - lo > 1) {
7799 int mid = (lo + hi) / 2;
7800 sector_t a = BB_OFFSET(p[mid]);
7801 if (a <= s)
7802 lo = mid;
7803 else
7804 hi = mid;
7805 }
7806 if (hi > lo && BB_OFFSET(p[lo]) > s)
7807 hi = lo;
7808
7809 if (hi > lo) {
7810 /* we found a range that might merge with the start
7811 * of our new range
7812 */
7813 sector_t a = BB_OFFSET(p[lo]);
7814 sector_t e = a + BB_LEN(p[lo]);
7815 int ack = BB_ACK(p[lo]);
7816 if (e >= s) {
7817 /* Yes, we can merge with a previous range */
7818 if (s == a && s + sectors >= e)
7819 /* new range covers old */
7820 ack = acknowledged;
7821 else
7822 ack = ack && acknowledged;
7823
7824 if (e < s + sectors)
7825 e = s + sectors;
7826 if (e - a <= BB_MAX_LEN) {
7827 p[lo] = BB_MAKE(a, e-a, ack);
7828 s = e;
7829 } else {
7830 /* does not all fit in one range,
7831 * make p[lo] maximal
7832 */
7833 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7834 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7835 s = a + BB_MAX_LEN;
7836 }
7837 sectors = e - s;
7838 }
7839 }
7840 if (sectors && hi < bb->count) {
7841 /* 'hi' points to the first range that starts after 's'.
7842 * Maybe we can merge with the start of that range */
7843 sector_t a = BB_OFFSET(p[hi]);
7844 sector_t e = a + BB_LEN(p[hi]);
7845 int ack = BB_ACK(p[hi]);
7846 if (a <= s + sectors) {
7847 /* merging is possible */
7848 if (e <= s + sectors) {
7849 /* full overlap */
7850 e = s + sectors;
7851 ack = acknowledged;
7852 } else
7853 ack = ack && acknowledged;
7854
7855 a = s;
7856 if (e - a <= BB_MAX_LEN) {
7857 p[hi] = BB_MAKE(a, e-a, ack);
7858 s = e;
7859 } else {
7860 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7861 s = a + BB_MAX_LEN;
7862 }
7863 sectors = e - s;
7864 lo = hi;
7865 hi++;
7866 }
7867 }
7868 if (sectors == 0 && hi < bb->count) {
7869 /* we might be able to combine lo and hi */
7870 /* Note: 's' is at the end of 'lo' */
7871 sector_t a = BB_OFFSET(p[hi]);
7872 int lolen = BB_LEN(p[lo]);
7873 int hilen = BB_LEN(p[hi]);
7874 int newlen = lolen + hilen - (s - a);
7875 if (s >= a && newlen < BB_MAX_LEN) {
7876 /* yes, we can combine them */
7877 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7878 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7879 memmove(p + hi, p + hi + 1,
7880 (bb->count - hi - 1) * 8);
7881 bb->count--;
7882 }
7883 }
7884 while (sectors) {
7885 /* didn't merge (it all).
7886 * Need to add a range just before 'hi' */
7887 if (bb->count >= MD_MAX_BADBLOCKS) {
7888 /* No room for more */
7889 rv = 0;
7890 break;
7891 } else {
7892 int this_sectors = sectors;
7893 memmove(p + hi + 1, p + hi,
7894 (bb->count - hi) * 8);
7895 bb->count++;
7896
7897 if (this_sectors > BB_MAX_LEN)
7898 this_sectors = BB_MAX_LEN;
7899 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7900 sectors -= this_sectors;
7901 s += this_sectors;
7902 }
7903 }
7904
7905 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007906 if (!acknowledged)
7907 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007908 write_sequnlock_irq(&bb->lock);
7909
7910 return rv;
7911}
7912
NeilBrown3cb03002011-10-11 16:45:26 +11007913int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007914 int acknowledged)
7915{
7916 int rv = md_set_badblocks(&rdev->badblocks,
7917 s + rdev->data_offset, sectors, acknowledged);
7918 if (rv) {
7919 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007920 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007921 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7922 md_wakeup_thread(rdev->mddev->thread);
7923 }
7924 return rv;
7925}
7926EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7927
7928/*
7929 * Remove a range of bad blocks from the table.
7930 * This may involve extending the table if we spilt a region,
7931 * but it must not fail. So if the table becomes full, we just
7932 * drop the remove request.
7933 */
7934static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7935{
7936 u64 *p;
7937 int lo, hi;
7938 sector_t target = s + sectors;
7939 int rv = 0;
7940
7941 if (bb->shift > 0) {
7942 /* When clearing we round the start up and the end down.
7943 * This should not matter as the shift should align with
7944 * the block size and no rounding should ever be needed.
7945 * However it is better the think a block is bad when it
7946 * isn't than to think a block is not bad when it is.
7947 */
7948 s += (1<<bb->shift) - 1;
7949 s >>= bb->shift;
7950 target >>= bb->shift;
7951 sectors = target - s;
7952 }
7953
7954 write_seqlock_irq(&bb->lock);
7955
7956 p = bb->page;
7957 lo = 0;
7958 hi = bb->count;
7959 /* Find the last range that starts before 'target' */
7960 while (hi - lo > 1) {
7961 int mid = (lo + hi) / 2;
7962 sector_t a = BB_OFFSET(p[mid]);
7963 if (a < target)
7964 lo = mid;
7965 else
7966 hi = mid;
7967 }
7968 if (hi > lo) {
7969 /* p[lo] is the last range that could overlap the
7970 * current range. Earlier ranges could also overlap,
7971 * but only this one can overlap the end of the range.
7972 */
7973 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7974 /* Partial overlap, leave the tail of this range */
7975 int ack = BB_ACK(p[lo]);
7976 sector_t a = BB_OFFSET(p[lo]);
7977 sector_t end = a + BB_LEN(p[lo]);
7978
7979 if (a < s) {
7980 /* we need to split this range */
7981 if (bb->count >= MD_MAX_BADBLOCKS) {
7982 rv = 0;
7983 goto out;
7984 }
7985 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7986 bb->count++;
7987 p[lo] = BB_MAKE(a, s-a, ack);
7988 lo++;
7989 }
7990 p[lo] = BB_MAKE(target, end - target, ack);
7991 /* there is no longer an overlap */
7992 hi = lo;
7993 lo--;
7994 }
7995 while (lo >= 0 &&
7996 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7997 /* This range does overlap */
7998 if (BB_OFFSET(p[lo]) < s) {
7999 /* Keep the early parts of this range. */
8000 int ack = BB_ACK(p[lo]);
8001 sector_t start = BB_OFFSET(p[lo]);
8002 p[lo] = BB_MAKE(start, s - start, ack);
8003 /* now low doesn't overlap, so.. */
8004 break;
8005 }
8006 lo--;
8007 }
8008 /* 'lo' is strictly before, 'hi' is strictly after,
8009 * anything between needs to be discarded
8010 */
8011 if (hi - lo > 1) {
8012 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8013 bb->count -= (hi - lo - 1);
8014 }
8015 }
8016
8017 bb->changed = 1;
8018out:
8019 write_sequnlock_irq(&bb->lock);
8020 return rv;
8021}
8022
NeilBrown3cb03002011-10-11 16:45:26 +11008023int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10008024{
8025 return md_clear_badblocks(&rdev->badblocks,
8026 s + rdev->data_offset,
8027 sectors);
8028}
8029EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8030
8031/*
8032 * Acknowledge all bad blocks in a list.
8033 * This only succeeds if ->changed is clear. It is used by
8034 * in-kernel metadata updates
8035 */
8036void md_ack_all_badblocks(struct badblocks *bb)
8037{
8038 if (bb->page == NULL || bb->changed)
8039 /* no point even trying */
8040 return;
8041 write_seqlock_irq(&bb->lock);
8042
8043 if (bb->changed == 0) {
8044 u64 *p = bb->page;
8045 int i;
8046 for (i = 0; i < bb->count ; i++) {
8047 if (!BB_ACK(p[i])) {
8048 sector_t start = BB_OFFSET(p[i]);
8049 int len = BB_LEN(p[i]);
8050 p[i] = BB_MAKE(start, len, 1);
8051 }
8052 }
NeilBrownde393cd2011-07-28 11:31:48 +10008053 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008054 }
8055 write_sequnlock_irq(&bb->lock);
8056}
8057EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8058
NeilBrown16c791a2011-07-28 11:31:47 +10008059/* sysfs access to bad-blocks list.
8060 * We present two files.
8061 * 'bad-blocks' lists sector numbers and lengths of ranges that
8062 * are recorded as bad. The list is truncated to fit within
8063 * the one-page limit of sysfs.
8064 * Writing "sector length" to this file adds an acknowledged
8065 * bad block list.
8066 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8067 * been acknowledged. Writing to this file adds bad blocks
8068 * without acknowledging them. This is largely for testing.
8069 */
8070
8071static ssize_t
8072badblocks_show(struct badblocks *bb, char *page, int unack)
8073{
8074 size_t len;
8075 int i;
8076 u64 *p = bb->page;
8077 unsigned seq;
8078
8079 if (bb->shift < 0)
8080 return 0;
8081
8082retry:
8083 seq = read_seqbegin(&bb->lock);
8084
8085 len = 0;
8086 i = 0;
8087
8088 while (len < PAGE_SIZE && i < bb->count) {
8089 sector_t s = BB_OFFSET(p[i]);
8090 unsigned int length = BB_LEN(p[i]);
8091 int ack = BB_ACK(p[i]);
8092 i++;
8093
8094 if (unack && ack)
8095 continue;
8096
8097 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8098 (unsigned long long)s << bb->shift,
8099 length << bb->shift);
8100 }
NeilBrownde393cd2011-07-28 11:31:48 +10008101 if (unack && len == 0)
8102 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008103
8104 if (read_seqretry(&bb->lock, seq))
8105 goto retry;
8106
8107 return len;
8108}
8109
8110#define DO_DEBUG 1
8111
8112static ssize_t
8113badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8114{
8115 unsigned long long sector;
8116 int length;
8117 char newline;
8118#ifdef DO_DEBUG
8119 /* Allow clearing via sysfs *only* for testing/debugging.
8120 * Normally only a successful write may clear a badblock
8121 */
8122 int clear = 0;
8123 if (page[0] == '-') {
8124 clear = 1;
8125 page++;
8126 }
8127#endif /* DO_DEBUG */
8128
8129 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8130 case 3:
8131 if (newline != '\n')
8132 return -EINVAL;
8133 case 2:
8134 if (length <= 0)
8135 return -EINVAL;
8136 break;
8137 default:
8138 return -EINVAL;
8139 }
8140
8141#ifdef DO_DEBUG
8142 if (clear) {
8143 md_clear_badblocks(bb, sector, length);
8144 return len;
8145 }
8146#endif /* DO_DEBUG */
8147 if (md_set_badblocks(bb, sector, length, !unack))
8148 return len;
8149 else
8150 return -ENOSPC;
8151}
8152
Adrian Bunk75c96f82005-05-05 16:16:09 -07008153static int md_notify_reboot(struct notifier_block *this,
8154 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155{
8156 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008157 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008158 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008159
8160 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8161
8162 printk(KERN_INFO "md: stopping all md devices.\n");
8163
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008164 for_each_mddev(mddev, tmp) {
Neil Brownc71d4882006-05-26 10:39:25 +10008165 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008166 /* Force a switch to readonly even array
8167 * appears to still be in use. Hence
8168 * the '100'.
8169 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008170 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008171 mddev_unlock(mddev);
8172 }
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008173 need_delay = 1;
8174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 /*
8176 * certain more exotic SCSI devices are known to be
8177 * volatile wrt too early system reboots. While the
8178 * right place to handle this issue is the given
8179 * driver, we do want to have a safe RAID driver ...
8180 */
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008181 if (need_delay)
8182 mdelay(1000*1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 }
8184 return NOTIFY_DONE;
8185}
8186
Adrian Bunk75c96f82005-05-05 16:16:09 -07008187static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 .notifier_call = md_notify_reboot,
8189 .next = NULL,
8190 .priority = INT_MAX, /* before any real devices */
8191};
8192
8193static void md_geninit(void)
8194{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008195 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008197 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198}
8199
Adrian Bunk75c96f82005-05-05 16:16:09 -07008200static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201{
Tejun Heoe804ac72010-10-15 15:36:08 +02008202 int ret = -ENOMEM;
8203
Tejun Heoada609e2011-01-25 14:35:54 +01008204 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008205 if (!md_wq)
8206 goto err_wq;
8207
8208 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8209 if (!md_misc_wq)
8210 goto err_misc_wq;
8211
8212 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8213 goto err_md;
8214
8215 if ((ret = register_blkdev(0, "mdp")) < 0)
8216 goto err_mdp;
8217 mdp_major = ret;
8218
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008219 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008220 md_probe, NULL, NULL);
8221 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222 md_probe, NULL, NULL);
8223
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008225 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008226
8227 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
Tejun Heoe804ac72010-10-15 15:36:08 +02008230err_mdp:
8231 unregister_blkdev(MD_MAJOR, "md");
8232err_md:
8233 destroy_workqueue(md_misc_wq);
8234err_misc_wq:
8235 destroy_workqueue(md_wq);
8236err_wq:
8237 return ret;
8238}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
8240#ifndef MODULE
8241
8242/*
8243 * Searches all registered partitions for autorun RAID arrays
8244 * at boot time.
8245 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008246
8247static LIST_HEAD(all_detected_devices);
8248struct detected_devices_node {
8249 struct list_head list;
8250 dev_t dev;
8251};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252
8253void md_autodetect_dev(dev_t dev)
8254{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008255 struct detected_devices_node *node_detected_dev;
8256
8257 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8258 if (node_detected_dev) {
8259 node_detected_dev->dev = dev;
8260 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8261 } else {
8262 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8263 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265}
8266
8267
8268static void autostart_arrays(int part)
8269{
NeilBrown3cb03002011-10-11 16:45:26 +11008270 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008271 struct detected_devices_node *node_detected_dev;
8272 dev_t dev;
8273 int i_scanned, i_passed;
8274
8275 i_scanned = 0;
8276 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
8278 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8279
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008280 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8281 i_scanned++;
8282 node_detected_dev = list_entry(all_detected_devices.next,
8283 struct detected_devices_node, list);
8284 list_del(&node_detected_dev->list);
8285 dev = node_detected_dev->dev;
8286 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008287 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 if (IS_ERR(rdev))
8289 continue;
8290
NeilBrownb2d444d2005-11-08 21:39:31 -08008291 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 MD_BUG();
8293 continue;
8294 }
NeilBrownd0fae182008-03-04 14:29:31 -08008295 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008297 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008299
8300 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8301 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008302
8303 autorun_devices(part);
8304}
8305
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008306#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307
8308static __exit void md_exit(void)
8309{
NeilBrownfd01b882011-10-11 16:47:53 +11008310 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008312
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008313 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008314 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008316 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317 unregister_blkdev(mdp_major, "mdp");
8318 unregister_reboot_notifier(&md_notifier);
8319 unregister_sysctl_table(raid_table_header);
8320 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008321 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008322 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008323 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008325 destroy_workqueue(md_misc_wq);
8326 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327}
8328
Dan Williams685784a2007-07-09 11:56:42 -07008329subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330module_exit(md_exit)
8331
NeilBrownf91de922005-11-08 21:39:36 -08008332static int get_ro(char *buffer, struct kernel_param *kp)
8333{
8334 return sprintf(buffer, "%d", start_readonly);
8335}
8336static int set_ro(const char *val, struct kernel_param *kp)
8337{
8338 char *e;
8339 int num = simple_strtoul(val, &e, 10);
8340 if (*val && (*e == '\0' || *e == '\n')) {
8341 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008342 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008343 }
8344 return -EINVAL;
8345}
8346
NeilBrown80ca3a42006-07-10 04:44:18 -07008347module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8348module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008349
NeilBrownefeb53c2009-01-09 08:31:10 +11008350module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008351
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352EXPORT_SYMBOL(register_md_personality);
8353EXPORT_SYMBOL(unregister_md_personality);
8354EXPORT_SYMBOL(md_error);
8355EXPORT_SYMBOL(md_done_sync);
8356EXPORT_SYMBOL(md_write_start);
8357EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358EXPORT_SYMBOL(md_register_thread);
8359EXPORT_SYMBOL(md_unregister_thread);
8360EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361EXPORT_SYMBOL(md_check_recovery);
8362MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008363MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008364MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008365MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);