blob: 114ba155af87dc69e7576c4e8f803968bfbd5726 [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);
NeilBrown41158c72005-06-21 17:17:25 -07001717 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001718 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return 0;
1721}
1722
NeilBrownfd01b882011-10-11 16:47:53 +11001723static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001726 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int max_dev, i;
1728 /* make rdev->sb match mddev and rdev data. */
1729
Namhyung Kim65a06f062011-07-27 11:00:36 +10001730 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 sb->feature_map = 0;
1733 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001734 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 memset(sb->pad3, 0, sizeof(sb->pad3));
1737
1738 sb->utime = cpu_to_le64((__u64)mddev->utime);
1739 sb->events = cpu_to_le64(mddev->events);
1740 if (mddev->in_sync)
1741 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1742 else
1743 sb->resync_offset = cpu_to_le64(0);
1744
NeilBrown1c05b4b2006-10-21 10:24:08 -07001745 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001746
NeilBrownf0ca3402006-02-02 14:28:04 -08001747 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001748 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001749 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001750 sb->level = cpu_to_le32(mddev->level);
1751 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001752
NeilBrownaeb9b2112011-08-25 14:43:08 +10001753 if (test_bit(WriteMostly, &rdev->flags))
1754 sb->devflags |= WriteMostly1;
1755 else
1756 sb->devflags &= ~WriteMostly1;
1757
NeilBrownc3d97142009-12-14 12:49:52 +11001758 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1759 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001760 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001761 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001762
1763 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001764 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001765 sb->feature_map |=
1766 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1767 sb->recovery_offset =
1768 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001769 }
1770
NeilBrownf6705572006-03-27 01:18:11 -08001771 if (mddev->reshape_position != MaxSector) {
1772 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1773 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1774 sb->new_layout = cpu_to_le32(mddev->new_layout);
1775 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1776 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001777 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001778 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001779
NeilBrown2699b672011-07-28 11:31:47 +10001780 if (rdev->badblocks.count == 0)
1781 /* Nothing to do for bad blocks*/ ;
1782 else if (sb->bblog_offset == 0)
1783 /* Cannot record bad blocks on this device */
1784 md_error(mddev, rdev);
1785 else {
1786 struct badblocks *bb = &rdev->badblocks;
1787 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1788 u64 *p = bb->page;
1789 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1790 if (bb->changed) {
1791 unsigned seq;
1792
1793retry:
1794 seq = read_seqbegin(&bb->lock);
1795
1796 memset(bbp, 0xff, PAGE_SIZE);
1797
1798 for (i = 0 ; i < bb->count ; i++) {
1799 u64 internal_bb = *p++;
1800 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1801 | BB_LEN(internal_bb));
1802 *bbp++ = cpu_to_le64(store_bb);
1803 }
1804 if (read_seqretry(&bb->lock, seq))
1805 goto retry;
1806
1807 bb->sector = (rdev->sb_start +
1808 (int)le32_to_cpu(sb->bblog_offset));
1809 bb->size = le16_to_cpu(sb->bblog_size);
1810 bb->changed = 0;
1811 }
1812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001815 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (rdev2->desc_nr+1 > max_dev)
1817 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001818
NeilBrown70471da2009-08-03 10:59:57 +10001819 if (max_dev > le32_to_cpu(sb->max_dev)) {
1820 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001821 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001822 rdev->sb_size = max_dev * 2 + 256;
1823 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1824 if (rdev->sb_size & bmask)
1825 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001826 } else
1827 max_dev = le32_to_cpu(sb->max_dev);
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 for (i=0; i<max_dev;i++)
1830 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1831
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001832 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001834 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001836 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001838 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001839 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 else
1841 sb->dev_roles[i] = cpu_to_le16(0xffff);
1842 }
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 sb->sb_csum = calc_sb_1_csum(sb);
1845}
1846
Chris Webb0cd17fe2008-06-28 08:31:46 +10001847static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001848super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849{
1850 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001851 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001852 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001854 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001856 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001857 max_sectors -= rdev->data_offset;
1858 if (!num_sectors || num_sectors > max_sectors)
1859 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001860 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 /* minor version 0 with bitmap we can't move */
1862 return 0;
1863 } else {
1864 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001865 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001866 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001867 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001868 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001869 if (!num_sectors || num_sectors > max_sectors)
1870 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001871 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001872 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001873 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001874 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001875 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001876 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001877 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 rdev->sb_page);
1879 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001880 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Adrian Bunk75c96f82005-05-05 16:16:09 -07001883static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 [0] = {
1885 .name = "0.90.0",
1886 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887 .load_super = super_90_load,
1888 .validate_super = super_90_validate,
1889 .sync_super = super_90_sync,
1890 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 },
1892 [1] = {
1893 .name = "md-1",
1894 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001895 .load_super = super_1_load,
1896 .validate_super = super_1_validate,
1897 .sync_super = super_1_sync,
1898 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 },
1900};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
NeilBrownfd01b882011-10-11 16:47:53 +11001902static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001903{
1904 if (mddev->sync_super) {
1905 mddev->sync_super(mddev, rdev);
1906 return;
1907 }
1908
1909 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1910
1911 super_types[mddev->major_version].sync_super(mddev, rdev);
1912}
1913
NeilBrownfd01b882011-10-11 16:47:53 +11001914static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
NeilBrown3cb03002011-10-11 16:45:26 +11001916 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
NeilBrown4b809912008-07-21 17:05:25 +10001918 rcu_read_lock();
1919 rdev_for_each_rcu(rdev, mddev1)
1920 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001921 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001922 rdev2->bdev->bd_contains) {
1923 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001924 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001925 }
1926 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
1928}
1929
1930static LIST_HEAD(pending_raid_disks);
1931
Andre Nollac5e7112009-08-03 10:59:47 +10001932/*
1933 * Try to register data integrity profile for an mddev
1934 *
1935 * This is called when an array is started and after a disk has been kicked
1936 * from the array. It only succeeds if all working and active component devices
1937 * are integrity capable with matching profiles.
1938 */
NeilBrownfd01b882011-10-11 16:47:53 +11001939int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001940{
NeilBrown3cb03002011-10-11 16:45:26 +11001941 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001942
Andre Nollac5e7112009-08-03 10:59:47 +10001943 if (list_empty(&mddev->disks))
1944 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001945 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1946 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001947 list_for_each_entry(rdev, &mddev->disks, same_set) {
1948 /* skip spares and non-functional disks */
1949 if (test_bit(Faulty, &rdev->flags))
1950 continue;
1951 if (rdev->raid_disk < 0)
1952 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001953 if (!reference) {
1954 /* Use the first rdev as the reference */
1955 reference = rdev;
1956 continue;
1957 }
1958 /* does this rdev's profile match the reference profile? */
1959 if (blk_integrity_compare(reference->bdev->bd_disk,
1960 rdev->bdev->bd_disk) < 0)
1961 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001962 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001963 if (!reference || !bdev_get_integrity(reference->bdev))
1964 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001965 /*
1966 * All component devices are integrity capable and have matching
1967 * profiles, register the common profile for the md device.
1968 */
1969 if (blk_integrity_register(mddev->gendisk,
1970 bdev_get_integrity(reference->bdev)) != 0) {
1971 printk(KERN_ERR "md: failed to register integrity for %s\n",
1972 mdname(mddev));
1973 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001974 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001975 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1976 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1977 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1978 mdname(mddev));
1979 return -EINVAL;
1980 }
Andre Nollac5e7112009-08-03 10:59:47 +10001981 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001982}
Andre Nollac5e7112009-08-03 10:59:47 +10001983EXPORT_SYMBOL(md_integrity_register);
1984
1985/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001986void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001987{
1988 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1989 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1990
1991 if (!bi_mddev) /* nothing to do */
1992 return;
1993 if (rdev->raid_disk < 0) /* skip spares */
1994 return;
1995 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1996 rdev->bdev->bd_disk) >= 0)
1997 return;
1998 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1999 blk_integrity_unregister(mddev->gendisk);
2000}
2001EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002002
NeilBrownfd01b882011-10-11 16:47:53 +11002003static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002005 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002006 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002007 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002008 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (rdev->mddev) {
2011 MD_BUG();
2012 return -EINVAL;
2013 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002014
2015 /* prevent duplicates */
2016 if (find_rdev(mddev, rdev->bdev->bd_dev))
2017 return -EEXIST;
2018
Andre Nolldd8ac332009-03-31 14:33:13 +11002019 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2020 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2021 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002022 if (mddev->pers) {
2023 /* Cannot change size, so fail
2024 * If mddev->level <= 0, then we don't care
2025 * about aligning sizes (e.g. linear)
2026 */
2027 if (mddev->level > 0)
2028 return -ENOSPC;
2029 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002030 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 /* Verify rdev->desc_nr is unique.
2034 * If it is -1, assign a free number, else
2035 * check number is not in use
2036 */
2037 if (rdev->desc_nr < 0) {
2038 int choice = 0;
2039 if (mddev->pers) choice = mddev->raid_disks;
2040 while (find_rdev_nr(mddev, choice))
2041 choice++;
2042 rdev->desc_nr = choice;
2043 } else {
2044 if (find_rdev_nr(mddev, rdev->desc_nr))
2045 return -EBUSY;
2046 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002047 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2048 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2049 mdname(mddev), mddev->max_disks);
2050 return -EBUSY;
2051 }
NeilBrown19133a42005-11-08 21:39:35 -08002052 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002053 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002054 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002057 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002058
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002059 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002060 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061
Tejun Heo0762b8b2008-08-25 19:56:12 +09002062 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002063 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2064 /* failure here is OK */;
2065 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002066
NeilBrown4b809912008-07-21 17:05:25 +10002067 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002068 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002069
2070 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002071 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002074
2075 fail:
2076 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2077 b, mdname(mddev));
2078 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
NeilBrown177a99b2008-02-06 01:39:56 -08002081static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002082{
NeilBrown3cb03002011-10-11 16:45:26 +11002083 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002084 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002085 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002086}
2087
NeilBrown3cb03002011-10-11 16:45:26 +11002088static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 char b[BDEVNAME_SIZE];
2091 if (!rdev->mddev) {
2092 MD_BUG();
2093 return;
2094 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002095 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002096 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2098 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002099 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002100 sysfs_put(rdev->sysfs_state);
2101 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002102 kfree(rdev->badblocks.page);
2103 rdev->badblocks.count = 0;
2104 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002105 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002106 * writing to 'remove' to "dev/state". We also need
2107 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002108 */
NeilBrown4b809912008-07-21 17:05:25 +10002109 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002110 INIT_WORK(&rdev->del_work, md_delayed_delete);
2111 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002112 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115/*
2116 * prevent the device from being mounted, repartitioned or
2117 * otherwise reused by a RAID array (or any other kernel
2118 * subsystem), by bd_claiming the device.
2119 */
NeilBrown3cb03002011-10-11 16:45:26 +11002120static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 int err = 0;
2123 struct block_device *bdev;
2124 char b[BDEVNAME_SIZE];
2125
Tejun Heod4d77622010-11-13 11:55:18 +01002126 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002127 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (IS_ERR(bdev)) {
2129 printk(KERN_ERR "md: could not open %s.\n",
2130 __bdevname(dev, b));
2131 return PTR_ERR(bdev);
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rdev->bdev = bdev;
2134 return err;
2135}
2136
NeilBrown3cb03002011-10-11 16:45:26 +11002137static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct block_device *bdev = rdev->bdev;
2140 rdev->bdev = NULL;
2141 if (!bdev)
2142 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002143 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146void md_autodetect_dev(dev_t dev);
2147
NeilBrown3cb03002011-10-11 16:45:26 +11002148static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 char b[BDEVNAME_SIZE];
2151 printk(KERN_INFO "md: export_rdev(%s)\n",
2152 bdevname(rdev->bdev,b));
2153 if (rdev->mddev)
2154 MD_BUG();
2155 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002157 if (test_bit(AutoDetected, &rdev->flags))
2158 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#endif
2160 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002161 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
NeilBrown3cb03002011-10-11 16:45:26 +11002164static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 unbind_rdev_from_array(rdev);
2167 export_rdev(rdev);
2168}
2169
NeilBrownfd01b882011-10-11 16:47:53 +11002170static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
NeilBrown3cb03002011-10-11 16:45:26 +11002172 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
NeilBrownd089c6a2008-02-06 01:39:59 -08002174 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!rdev->mddev) {
2176 MD_BUG();
2177 continue;
2178 }
2179 kick_rdev_from_array(rdev);
2180 }
2181 if (!list_empty(&mddev->disks))
2182 MD_BUG();
2183 mddev->raid_disks = 0;
2184 mddev->major_version = 0;
2185}
2186
2187static void print_desc(mdp_disk_t *desc)
2188{
2189 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2190 desc->major,desc->minor,desc->raid_disk,desc->state);
2191}
2192
Cheng Renquancd2ac932009-01-09 08:31:08 +11002193static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 int i;
2196
2197 printk(KERN_INFO
2198 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2199 sb->major_version, sb->minor_version, sb->patch_version,
2200 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2201 sb->ctime);
2202 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2203 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2204 sb->md_minor, sb->layout, sb->chunk_size);
2205 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2206 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2207 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2208 sb->failed_disks, sb->spare_disks,
2209 sb->sb_csum, (unsigned long)sb->events_lo);
2210
2211 printk(KERN_INFO);
2212 for (i = 0; i < MD_SB_DISKS; i++) {
2213 mdp_disk_t *desc;
2214
2215 desc = sb->disks + i;
2216 if (desc->number || desc->major || desc->minor ||
2217 desc->raid_disk || (desc->state && (desc->state != 4))) {
2218 printk(" D %2d: ", i);
2219 print_desc(desc);
2220 }
2221 }
2222 printk(KERN_INFO "md: THIS: ");
2223 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Cheng Renquancd2ac932009-01-09 08:31:08 +11002226static void print_sb_1(struct mdp_superblock_1 *sb)
2227{
2228 __u8 *uuid;
2229
2230 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002231 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002232 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002233 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002234 le32_to_cpu(sb->major_version),
2235 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002236 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002237 sb->set_name,
2238 (unsigned long long)le64_to_cpu(sb->ctime)
2239 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2240
2241 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002242 printk(KERN_INFO
2243 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002244 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002245 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002246 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2247 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002248 le32_to_cpu(sb->level),
2249 (unsigned long long)le64_to_cpu(sb->size),
2250 le32_to_cpu(sb->raid_disks),
2251 le32_to_cpu(sb->layout),
2252 le32_to_cpu(sb->chunksize),
2253 (unsigned long long)le64_to_cpu(sb->data_offset),
2254 (unsigned long long)le64_to_cpu(sb->data_size),
2255 (unsigned long long)le64_to_cpu(sb->super_offset),
2256 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2257 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002258 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002259 sb->devflags,
2260 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2261 (unsigned long long)le64_to_cpu(sb->events),
2262 (unsigned long long)le64_to_cpu(sb->resync_offset),
2263 le32_to_cpu(sb->sb_csum),
2264 le32_to_cpu(sb->max_dev)
2265 );
2266}
2267
NeilBrown3cb03002011-10-11 16:45:26 +11002268static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002271 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2272 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002273 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2274 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002276 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2277 switch (major_version) {
2278 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002279 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002280 break;
2281 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002282 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002283 break;
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 } else
2286 printk(KERN_INFO "md: no rdev superblock!\n");
2287}
2288
Adrian Bunk5e563412006-06-26 00:27:42 -07002289static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002291 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002292 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002293 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 char b[BDEVNAME_SIZE];
2295
2296 printk("\n");
2297 printk("md: **********************************\n");
2298 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2299 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002300 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
NeilBrown32a76272005-06-21 17:17:14 -07002302 if (mddev->bitmap)
2303 bitmap_print_sb(mddev->bitmap);
2304 else
2305 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002306 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 printk("<%s>", bdevname(rdev->bdev,b));
2308 printk("\n");
2309
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002310 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002311 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 printk("md: **********************************\n");
2314 printk("\n");
2315}
2316
2317
NeilBrownfd01b882011-10-11 16:47:53 +11002318static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
NeilBrown42543762006-06-26 00:27:57 -07002320 /* Update each superblock (in-memory image), but
2321 * if we are allowed to, skip spares which already
2322 * have the right event counter, or have one earlier
2323 * (which would mean they aren't being marked as dirty
2324 * with the rest of the array)
2325 */
NeilBrown3cb03002011-10-11 16:45:26 +11002326 struct md_rdev *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002327 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002328 if (rdev->sb_events == mddev->events ||
2329 (nospares &&
2330 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002331 rdev->sb_events+1 == mddev->events)) {
2332 /* Don't update this superblock */
2333 rdev->sb_loaded = 2;
2334 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002335 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002336 rdev->sb_loaded = 1;
2337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339}
2340
NeilBrownfd01b882011-10-11 16:47:53 +11002341static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
NeilBrown3cb03002011-10-11 16:45:26 +11002343 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002344 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002345 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002346 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002349 /* First make sure individual recovery_offsets are correct */
2350 list_for_each_entry(rdev, &mddev->disks, same_set) {
2351 if (rdev->raid_disk >= 0 &&
2352 mddev->delta_disks >= 0 &&
2353 !test_bit(In_sync, &rdev->flags) &&
2354 mddev->curr_resync_completed > rdev->recovery_offset)
2355 rdev->recovery_offset = mddev->curr_resync_completed;
2356
2357 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002358 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002359 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002360 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002361 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002362 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002363 list_for_each_entry(rdev, &mddev->disks, same_set) {
2364 if (rdev->badblocks.changed) {
2365 md_ack_all_badblocks(&rdev->badblocks);
2366 md_error(mddev, rdev);
2367 }
2368 clear_bit(Blocked, &rdev->flags);
2369 clear_bit(BlockedBadBlocks, &rdev->flags);
2370 wake_up(&rdev->blocked_wait);
2371 }
2372 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002373 wake_up(&mddev->sb_wait);
2374 return;
2375 }
2376
NeilBrowna9701a32005-11-08 21:39:34 -08002377 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002378
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002379 mddev->utime = get_seconds();
2380
NeilBrown850b2b422006-10-03 01:15:46 -07002381 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2382 force_change = 1;
2383 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2384 /* just a clean<-> dirty transition, possibly leave spares alone,
2385 * though if events isn't the right even/odd, we will have to do
2386 * spares after all
2387 */
2388 nospares = 1;
2389 if (force_change)
2390 nospares = 0;
2391 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002392 /* If the array is degraded, then skipping spares is both
2393 * dangerous and fairly pointless.
2394 * Dangerous because a device that was removed from the array
2395 * might have a event_count that still looks up-to-date,
2396 * so it can be re-added without a resync.
2397 * Pointless because if there are any spares to skip,
2398 * then a recovery will happen and soon that array won't
2399 * be degraded any more and the spare can go back to sleep then.
2400 */
NeilBrown850b2b422006-10-03 01:15:46 -07002401 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002402
NeilBrown06d91a52005-06-21 17:17:12 -07002403 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002404
2405 /* If this is just a dirty<->clean transition, and the array is clean
2406 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002407 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002408 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002409 && mddev->can_decrease_events
2410 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002411 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002412 mddev->can_decrease_events = 0;
2413 } else {
NeilBrown42543762006-06-26 00:27:57 -07002414 /* otherwise we have to go forward and ... */
2415 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002416 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 if (!mddev->events) {
2420 /*
2421 * oops, this 64-bit counter should never wrap.
2422 * Either we are in around ~1 trillion A.C., assuming
2423 * 1 reboot per second, or we have a bug:
2424 */
2425 MD_BUG();
2426 mddev->events --;
2427 }
NeilBrown2699b672011-07-28 11:31:47 +10002428
NeilBrownde393cd2011-07-28 11:31:48 +10002429 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002430 if (rdev->badblocks.changed)
2431 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002432 if (test_bit(Faulty, &rdev->flags))
2433 set_bit(FaultRecorded, &rdev->flags);
2434 }
NeilBrown2699b672011-07-28 11:31:47 +10002435
NeilBrowne6910632008-02-06 01:39:51 -08002436 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002437 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
NeilBrown36a4e1f2011-10-07 14:23:17 +11002439 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2440 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
NeilBrown4ad13662007-07-17 04:06:13 -07002442 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002443 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002445
NeilBrown42543762006-06-26 00:27:57 -07002446 if (rdev->sb_loaded != 1)
2447 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002449 if (!test_bit(Faulty, &rdev->flags) &&
2450 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002451 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002452 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002453 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002454 pr_debug("md: (write) %s's sb offset: %llu\n",
2455 bdevname(rdev->bdev, b),
2456 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002457 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002458 if (rdev->badblocks.size) {
2459 md_super_write(mddev, rdev,
2460 rdev->badblocks.sector,
2461 rdev->badblocks.size << 9,
2462 rdev->bb_page);
2463 rdev->badblocks.size = 0;
2464 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002465
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002466 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002467 pr_debug("md: %s (skipping faulty)\n",
2468 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002469 else
2470 pr_debug("(skipping incremental s/r ");
2471
NeilBrown7bfa19f2005-06-21 17:17:28 -07002472 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* only need to write one superblock... */
2474 break;
2475 }
NeilBrowna9701a32005-11-08 21:39:34 -08002476 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002477 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002478
NeilBrowna9701a32005-11-08 21:39:34 -08002479 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002480 if (mddev->in_sync != sync_req ||
2481 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002482 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002483 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002484 goto repeat;
2485 }
NeilBrown850b2b422006-10-03 01:15:46 -07002486 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002487 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002488 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002489 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2490 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002491
NeilBrownde393cd2011-07-28 11:31:48 +10002492 list_for_each_entry(rdev, &mddev->disks, same_set) {
2493 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2494 clear_bit(Blocked, &rdev->flags);
2495
2496 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002497 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002498 clear_bit(BlockedBadBlocks, &rdev->flags);
2499 wake_up(&rdev->blocked_wait);
2500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Andre Noll7f6ce762008-03-23 18:34:54 +01002503/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002504 * We want to accept with case. For this we use cmd_match.
2505 */
2506static int cmd_match(const char *cmd, const char *str)
2507{
2508 /* See if cmd, written into a sysfs file, matches
2509 * str. They must either be the same, or cmd can
2510 * have a trailing newline
2511 */
2512 while (*cmd && *str && *cmd == *str) {
2513 cmd++;
2514 str++;
2515 }
2516 if (*cmd == '\n')
2517 cmd++;
2518 if (*str || *cmd)
2519 return 0;
2520 return 1;
2521}
2522
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523struct rdev_sysfs_entry {
2524 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002525 ssize_t (*show)(struct md_rdev *, char *);
2526 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002527};
2528
2529static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002530state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002531{
2532 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002533 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002534
NeilBrownde393cd2011-07-28 11:31:48 +10002535 if (test_bit(Faulty, &rdev->flags) ||
2536 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002537 len+= sprintf(page+len, "%sfaulty",sep);
2538 sep = ",";
2539 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002540 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002541 len += sprintf(page+len, "%sin_sync",sep);
2542 sep = ",";
2543 }
NeilBrownf6556752006-06-26 00:28:01 -07002544 if (test_bit(WriteMostly, &rdev->flags)) {
2545 len += sprintf(page+len, "%swrite_mostly",sep);
2546 sep = ",";
2547 }
NeilBrownde393cd2011-07-28 11:31:48 +10002548 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002549 (rdev->badblocks.unacked_exist
2550 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002551 len += sprintf(page+len, "%sblocked", sep);
2552 sep = ",";
2553 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002554 if (!test_bit(Faulty, &rdev->flags) &&
2555 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002556 len += sprintf(page+len, "%sspare", sep);
2557 sep = ",";
2558 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002559 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2560 len += sprintf(page+len, "%swrite_error", sep);
2561 sep = ",";
2562 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002563 return len+sprintf(page+len, "\n");
2564}
2565
NeilBrown45dc2de2006-06-26 00:27:58 -07002566static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002567state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002568{
2569 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002570 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002571 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002572 * writemostly - sets write_mostly
2573 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002574 * blocked - sets the Blocked flags
2575 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002576 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002577 * write_error - sets WriteErrorSeen
2578 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002579 */
2580 int err = -EINVAL;
2581 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2582 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002583 if (test_bit(Faulty, &rdev->flags))
2584 err = 0;
2585 else
2586 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002587 } else if (cmd_match(buf, "remove")) {
2588 if (rdev->raid_disk >= 0)
2589 err = -EBUSY;
2590 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002591 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002592 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002593 if (mddev->pers)
2594 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002595 md_new_event(mddev);
2596 err = 0;
2597 }
NeilBrownf6556752006-06-26 00:28:01 -07002598 } else if (cmd_match(buf, "writemostly")) {
2599 set_bit(WriteMostly, &rdev->flags);
2600 err = 0;
2601 } else if (cmd_match(buf, "-writemostly")) {
2602 clear_bit(WriteMostly, &rdev->flags);
2603 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002604 } else if (cmd_match(buf, "blocked")) {
2605 set_bit(Blocked, &rdev->flags);
2606 err = 0;
2607 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002608 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002609 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002610 /* metadata handler doesn't understand badblocks,
2611 * so we need to fail the device
2612 */
2613 md_error(rdev->mddev, rdev);
2614 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002615 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002616 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002617 wake_up(&rdev->blocked_wait);
2618 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2619 md_wakeup_thread(rdev->mddev->thread);
2620
2621 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002622 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2623 set_bit(In_sync, &rdev->flags);
2624 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002625 } else if (cmd_match(buf, "write_error")) {
2626 set_bit(WriteErrorSeen, &rdev->flags);
2627 err = 0;
2628 } else if (cmd_match(buf, "-write_error")) {
2629 clear_bit(WriteErrorSeen, &rdev->flags);
2630 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002631 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002632 if (!err)
2633 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002634 return err ? err : len;
2635}
NeilBrown80ca3a42006-07-10 04:44:18 -07002636static struct rdev_sysfs_entry rdev_state =
2637__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002638
2639static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002640errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002641{
2642 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2643}
2644
2645static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002646errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002647{
2648 char *e;
2649 unsigned long n = simple_strtoul(buf, &e, 10);
2650 if (*buf && (*e == 0 || *e == '\n')) {
2651 atomic_set(&rdev->corrected_errors, n);
2652 return len;
2653 }
2654 return -EINVAL;
2655}
2656static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002657__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002658
NeilBrown014236d2006-01-06 00:20:55 -08002659static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002660slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002661{
2662 if (rdev->raid_disk < 0)
2663 return sprintf(page, "none\n");
2664 else
2665 return sprintf(page, "%d\n", rdev->raid_disk);
2666}
2667
2668static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002669slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002670{
2671 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002672 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002673 int slot = simple_strtoul(buf, &e, 10);
2674 if (strncmp(buf, "none", 4)==0)
2675 slot = -1;
2676 else if (e==buf || (*e && *e!= '\n'))
2677 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002678 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002679 /* Setting 'slot' on an active array requires also
2680 * updating the 'rd%d' link, and communicating
2681 * with the personality with ->hot_*_disk.
2682 * For now we only support removing
2683 * failed/spare devices. This normally happens automatically,
2684 * but not when the metadata is externally managed.
2685 */
NeilBrownc303da62008-02-06 01:39:51 -08002686 if (rdev->raid_disk == -1)
2687 return -EEXIST;
2688 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002689 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002690 return -EINVAL;
2691 err = rdev->mddev->pers->
2692 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2693 if (err)
2694 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002695 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002696 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002697 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2698 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002699 } else if (rdev->mddev->pers) {
NeilBrown3cb03002011-10-11 16:45:26 +11002700 struct md_rdev *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002701 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002702 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002703 */
2704
2705 if (rdev->raid_disk != -1)
2706 return -EBUSY;
2707
NeilBrownc6751b22011-02-02 11:57:13 +11002708 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2709 return -EBUSY;
2710
Neil Brown6c2fce22008-06-28 08:31:31 +10002711 if (rdev->mddev->pers->hot_add_disk == NULL)
2712 return -EINVAL;
2713
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002714 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002715 if (rdev2->raid_disk == slot)
2716 return -EEXIST;
2717
NeilBrownba1b41b2011-01-14 09:14:34 +11002718 if (slot >= rdev->mddev->raid_disks &&
2719 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2720 return -ENOSPC;
2721
Neil Brown6c2fce22008-06-28 08:31:31 +10002722 rdev->raid_disk = slot;
2723 if (test_bit(In_sync, &rdev->flags))
2724 rdev->saved_raid_disk = slot;
2725 else
2726 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002727 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002728 err = rdev->mddev->pers->
2729 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002730 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002731 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002732 return err;
Neil Brown52664732008-06-28 08:31:44 +10002733 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002734 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002735 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002736 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002737 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002738 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002739 if (slot >= rdev->mddev->raid_disks &&
2740 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002741 return -ENOSPC;
2742 rdev->raid_disk = slot;
2743 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002744 clear_bit(Faulty, &rdev->flags);
2745 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002746 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002747 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002748 }
NeilBrown014236d2006-01-06 00:20:55 -08002749 return len;
2750}
2751
2752
2753static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002754__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002755
NeilBrown93c8cad2006-01-06 00:20:56 -08002756static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002757offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002758{
Andrew Morton6961ece2006-01-06 00:20:59 -08002759 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002760}
2761
2762static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002763offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002764{
2765 char *e;
2766 unsigned long long offset = simple_strtoull(buf, &e, 10);
2767 if (e==buf || (*e && *e != '\n'))
2768 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002769 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002770 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002771 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002772 /* Must set offset before size, so overlap checks
2773 * can be sane */
2774 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002775 rdev->data_offset = offset;
2776 return len;
2777}
2778
2779static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002780__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002781
NeilBrown83303b62006-01-06 00:21:06 -08002782static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002783rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002784{
Andre Nolldd8ac332009-03-31 14:33:13 +11002785 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002786}
2787
NeilBrownc5d79ad2008-02-06 01:39:54 -08002788static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2789{
2790 /* check if two start/length pairs overlap */
2791 if (s1+l1 <= s2)
2792 return 0;
2793 if (s2+l2 <= s1)
2794 return 0;
2795 return 1;
2796}
2797
Dan Williamsb522adc2009-03-31 15:00:31 +11002798static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2799{
2800 unsigned long long blocks;
2801 sector_t new;
2802
2803 if (strict_strtoull(buf, 10, &blocks) < 0)
2804 return -EINVAL;
2805
2806 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2807 return -EINVAL; /* sector conversion overflow */
2808
2809 new = blocks * 2;
2810 if (new != blocks * 2)
2811 return -EINVAL; /* unsigned long long to sector_t overflow */
2812
2813 *sectors = new;
2814 return 0;
2815}
2816
NeilBrown83303b62006-01-06 00:21:06 -08002817static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002818rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002819{
NeilBrownfd01b882011-10-11 16:47:53 +11002820 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002821 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002822 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002823
Dan Williamsb522adc2009-03-31 15:00:31 +11002824 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002825 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002826 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002827 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002828 sectors = super_types[my_mddev->major_version].
2829 rdev_size_change(rdev, sectors);
2830 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002831 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002832 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002833 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002834 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002835 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002836 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002837 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002838
Andre Nolldd8ac332009-03-31 14:33:13 +11002839 rdev->sectors = sectors;
2840 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002841 /* need to check that all other rdevs with the same ->bdev
2842 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002843 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002844 * we have to change it back, we will have the lock again.
2845 */
NeilBrownfd01b882011-10-11 16:47:53 +11002846 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002848 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002849
NeilBrown27c529b2008-03-04 14:29:33 -08002850 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002851 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002852 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002853
2854 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002855 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002856 if (rdev->bdev == rdev2->bdev &&
2857 rdev != rdev2 &&
2858 overlaps(rdev->data_offset, rdev->sectors,
2859 rdev2->data_offset,
2860 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002861 overlap = 1;
2862 break;
2863 }
2864 mddev_unlock(mddev);
2865 if (overlap) {
2866 mddev_put(mddev);
2867 break;
2868 }
2869 }
NeilBrown27c529b2008-03-04 14:29:33 -08002870 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002871 if (overlap) {
2872 /* Someone else could have slipped in a size
2873 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002874 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002875 * safe, and trust userspace not to race with
2876 * itself
2877 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002878 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002879 return -EBUSY;
2880 }
2881 }
NeilBrown83303b62006-01-06 00:21:06 -08002882 return len;
2883}
2884
2885static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002886__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002887
Dan Williams06e3c812009-12-12 21:17:12 -07002888
NeilBrown3cb03002011-10-11 16:45:26 +11002889static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002890{
2891 unsigned long long recovery_start = rdev->recovery_offset;
2892
2893 if (test_bit(In_sync, &rdev->flags) ||
2894 recovery_start == MaxSector)
2895 return sprintf(page, "none\n");
2896
2897 return sprintf(page, "%llu\n", recovery_start);
2898}
2899
NeilBrown3cb03002011-10-11 16:45:26 +11002900static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002901{
2902 unsigned long long recovery_start;
2903
2904 if (cmd_match(buf, "none"))
2905 recovery_start = MaxSector;
2906 else if (strict_strtoull(buf, 10, &recovery_start))
2907 return -EINVAL;
2908
2909 if (rdev->mddev->pers &&
2910 rdev->raid_disk >= 0)
2911 return -EBUSY;
2912
2913 rdev->recovery_offset = recovery_start;
2914 if (recovery_start == MaxSector)
2915 set_bit(In_sync, &rdev->flags);
2916 else
2917 clear_bit(In_sync, &rdev->flags);
2918 return len;
2919}
2920
2921static struct rdev_sysfs_entry rdev_recovery_start =
2922__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2923
NeilBrown16c791a2011-07-28 11:31:47 +10002924
2925static ssize_t
2926badblocks_show(struct badblocks *bb, char *page, int unack);
2927static ssize_t
2928badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2929
NeilBrown3cb03002011-10-11 16:45:26 +11002930static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002931{
2932 return badblocks_show(&rdev->badblocks, page, 0);
2933}
NeilBrown3cb03002011-10-11 16:45:26 +11002934static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002935{
NeilBrownde393cd2011-07-28 11:31:48 +10002936 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2937 /* Maybe that ack was all we needed */
2938 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2939 wake_up(&rdev->blocked_wait);
2940 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002941}
2942static struct rdev_sysfs_entry rdev_bad_blocks =
2943__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2944
2945
NeilBrown3cb03002011-10-11 16:45:26 +11002946static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002947{
2948 return badblocks_show(&rdev->badblocks, page, 1);
2949}
NeilBrown3cb03002011-10-11 16:45:26 +11002950static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002951{
2952 return badblocks_store(&rdev->badblocks, page, len, 1);
2953}
2954static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2955__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2956
NeilBrown86e6ffd2005-11-08 21:39:24 -08002957static struct attribute *rdev_default_attrs[] = {
2958 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002959 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002960 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002961 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002962 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002963 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002964 &rdev_bad_blocks.attr,
2965 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002966 NULL,
2967};
2968static ssize_t
2969rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2970{
2971 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002972 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002973 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002974 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002975
2976 if (!entry->show)
2977 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002978
2979 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2980 if (!rv) {
2981 if (rdev->mddev == NULL)
2982 rv = -EBUSY;
2983 else
2984 rv = entry->show(rdev, page);
2985 mddev_unlock(mddev);
2986 }
2987 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002988}
2989
2990static ssize_t
2991rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2992 const char *page, size_t length)
2993{
2994 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002995 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002996 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002997 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002998
2999 if (!entry->store)
3000 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003001 if (!capable(CAP_SYS_ADMIN))
3002 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003003 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003004 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003005 if (rdev->mddev == NULL)
3006 rv = -EBUSY;
3007 else
3008 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003009 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003010 }
3011 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003012}
3013
3014static void rdev_free(struct kobject *ko)
3015{
NeilBrown3cb03002011-10-11 16:45:26 +11003016 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003017 kfree(rdev);
3018}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003019static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003020 .show = rdev_attr_show,
3021 .store = rdev_attr_store,
3022};
3023static struct kobj_type rdev_ktype = {
3024 .release = rdev_free,
3025 .sysfs_ops = &rdev_sysfs_ops,
3026 .default_attrs = rdev_default_attrs,
3027};
3028
NeilBrown3cb03002011-10-11 16:45:26 +11003029int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003030{
3031 rdev->desc_nr = -1;
3032 rdev->saved_raid_disk = -1;
3033 rdev->raid_disk = -1;
3034 rdev->flags = 0;
3035 rdev->data_offset = 0;
3036 rdev->sb_events = 0;
3037 rdev->last_read_error.tv_sec = 0;
3038 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003039 rdev->sb_loaded = 0;
3040 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003041 atomic_set(&rdev->nr_pending, 0);
3042 atomic_set(&rdev->read_errors, 0);
3043 atomic_set(&rdev->corrected_errors, 0);
3044
3045 INIT_LIST_HEAD(&rdev->same_set);
3046 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003047
3048 /* Add space to store bad block list.
3049 * This reserves the space even on arrays where it cannot
3050 * be used - I wonder if that matters
3051 */
3052 rdev->badblocks.count = 0;
3053 rdev->badblocks.shift = 0;
3054 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3055 seqlock_init(&rdev->badblocks.lock);
3056 if (rdev->badblocks.page == NULL)
3057 return -ENOMEM;
3058
3059 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003060}
3061EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062/*
3063 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3064 *
3065 * mark the device faulty if:
3066 *
3067 * - the device is nonexistent (zero size)
3068 * - the device has no valid superblock
3069 *
3070 * a faulty rdev _never_ has rdev->sb set.
3071 */
NeilBrown3cb03002011-10-11 16:45:26 +11003072static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 char b[BDEVNAME_SIZE];
3075 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003076 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 sector_t size;
3078
NeilBrown9ffae0c2006-01-06 00:20:32 -08003079 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!rdev) {
3081 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3082 return ERR_PTR(-ENOMEM);
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
NeilBrown2230dfe2011-07-28 11:31:46 +10003085 err = md_rdev_init(rdev);
3086 if (err)
3087 goto abort_free;
3088 err = alloc_disk_sb(rdev);
3089 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 goto abort_free;
3091
NeilBrownc5d79ad2008-02-06 01:39:54 -08003092 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (err)
3094 goto abort_free;
3095
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003096 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003097
Mike Snitzer77304d22010-11-08 14:39:12 +01003098 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (!size) {
3100 printk(KERN_WARNING
3101 "md: %s has zero or unknown size, marking faulty!\n",
3102 bdevname(rdev->bdev,b));
3103 err = -EINVAL;
3104 goto abort_free;
3105 }
3106
3107 if (super_format >= 0) {
3108 err = super_types[super_format].
3109 load_super(rdev, NULL, super_minor);
3110 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003111 printk(KERN_WARNING
3112 "md: %s does not have a valid v%d.%d "
3113 "superblock, not importing!\n",
3114 bdevname(rdev->bdev,b),
3115 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 goto abort_free;
3117 }
3118 if (err < 0) {
3119 printk(KERN_WARNING
3120 "md: could not read %s's sb, not importing!\n",
3121 bdevname(rdev->bdev,b));
3122 goto abort_free;
3123 }
3124 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003125 if (super_format == -1)
3126 /* hot-add for 0.90, or non-persistent: so no badblocks */
3127 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return rdev;
3130
3131abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003132 if (rdev->bdev)
3133 unlock_rdev(rdev);
3134 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003135 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 kfree(rdev);
3137 return ERR_PTR(err);
3138}
3139
3140/*
3141 * Check a full RAID array for plausibility
3142 */
3143
3144
NeilBrownfd01b882011-10-11 16:47:53 +11003145static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003148 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 char b[BDEVNAME_SIZE];
3150
3151 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003152 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 switch (super_types[mddev->major_version].
3154 load_super(rdev, freshest, mddev->minor_version)) {
3155 case 1:
3156 freshest = rdev;
3157 break;
3158 case 0:
3159 break;
3160 default:
3161 printk( KERN_ERR \
3162 "md: fatal superblock inconsistency in %s"
3163 " -- removing from array\n",
3164 bdevname(rdev->bdev,b));
3165 kick_rdev_from_array(rdev);
3166 }
3167
3168
3169 super_types[mddev->major_version].
3170 validate_super(mddev, freshest);
3171
3172 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003173 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003174 if (mddev->max_disks &&
3175 (rdev->desc_nr >= mddev->max_disks ||
3176 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003177 printk(KERN_WARNING
3178 "md: %s: %s: only %d devices permitted\n",
3179 mdname(mddev), bdevname(rdev->bdev, b),
3180 mddev->max_disks);
3181 kick_rdev_from_array(rdev);
3182 continue;
3183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (rdev != freshest)
3185 if (super_types[mddev->major_version].
3186 validate_super(mddev, rdev)) {
3187 printk(KERN_WARNING "md: kicking non-fresh %s"
3188 " from array!\n",
3189 bdevname(rdev->bdev,b));
3190 kick_rdev_from_array(rdev);
3191 continue;
3192 }
3193 if (mddev->level == LEVEL_MULTIPATH) {
3194 rdev->desc_nr = i++;
3195 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003196 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003197 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003198 rdev->raid_disk = -1;
3199 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
NeilBrown72e02072009-12-14 12:49:55 +11003204/* Read a fixed-point number.
3205 * Numbers in sysfs attributes should be in "standard" units where
3206 * possible, so time should be in seconds.
3207 * However we internally use a a much smaller unit such as
3208 * milliseconds or jiffies.
3209 * This function takes a decimal number with a possible fractional
3210 * component, and produces an integer which is the result of
3211 * multiplying that number by 10^'scale'.
3212 * all without any floating-point arithmetic.
3213 */
3214int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3215{
3216 unsigned long result = 0;
3217 long decimals = -1;
3218 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3219 if (*cp == '.')
3220 decimals = 0;
3221 else if (decimals < scale) {
3222 unsigned int value;
3223 value = *cp - '0';
3224 result = result * 10 + value;
3225 if (decimals >= 0)
3226 decimals++;
3227 }
3228 cp++;
3229 }
3230 if (*cp == '\n')
3231 cp++;
3232 if (*cp)
3233 return -EINVAL;
3234 if (decimals < 0)
3235 decimals = 0;
3236 while (decimals < scale) {
3237 result *= 10;
3238 decimals ++;
3239 }
3240 *res = result;
3241 return 0;
3242}
3243
3244
NeilBrown19052c02008-08-05 15:54:13 +10003245static void md_safemode_timeout(unsigned long data);
3246
NeilBrowneae17012005-11-08 21:39:23 -08003247static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003248safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003249{
3250 int msec = (mddev->safemode_delay*1000)/HZ;
3251 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3252}
3253static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003254safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003255{
NeilBrown16f17b32006-06-26 00:27:37 -07003256 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003257
NeilBrown72e02072009-12-14 12:49:55 +11003258 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003259 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003260 if (msec == 0)
3261 mddev->safemode_delay = 0;
3262 else {
NeilBrown19052c02008-08-05 15:54:13 +10003263 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003264 mddev->safemode_delay = (msec*HZ)/1000;
3265 if (mddev->safemode_delay == 0)
3266 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003267 if (mddev->safemode_delay < old_delay)
3268 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003269 }
3270 return len;
3271}
3272static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003273__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003274
3275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003276level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003277{
NeilBrown84fc4b52011-10-11 16:49:58 +11003278 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003279 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003280 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003281 else if (mddev->clevel[0])
3282 return sprintf(page, "%s\n", mddev->clevel);
3283 else if (mddev->level != LEVEL_NONE)
3284 return sprintf(page, "%d\n", mddev->level);
3285 else
3286 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003287}
3288
NeilBrownd9d166c2006-01-06 00:20:51 -08003289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003290level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003291{
Dan Williamsf2859af2010-05-02 10:04:16 -07003292 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003293 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003294 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003295 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003296 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003297 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003298
3299 if (mddev->pers == NULL) {
3300 if (len == 0)
3301 return 0;
3302 if (len >= sizeof(mddev->clevel))
3303 return -ENOSPC;
3304 strncpy(mddev->clevel, buf, len);
3305 if (mddev->clevel[len-1] == '\n')
3306 len--;
3307 mddev->clevel[len] = 0;
3308 mddev->level = LEVEL_NONE;
3309 return rv;
3310 }
3311
3312 /* request to change the personality. Need to ensure:
3313 * - array is not engaged in resync/recovery/reshape
3314 * - old personality can be suspended
3315 * - new personality will access other array.
3316 */
3317
NeilBrownbb4f1e92010-08-08 21:18:03 +10003318 if (mddev->sync_thread ||
3319 mddev->reshape_position != MaxSector ||
3320 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003321 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003322
3323 if (!mddev->pers->quiesce) {
3324 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3325 mdname(mddev), mddev->pers->name);
3326 return -EINVAL;
3327 }
3328
3329 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003330 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003331 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003332 strncpy(clevel, buf, len);
3333 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003334 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003335 clevel[len] = 0;
3336 if (strict_strtol(clevel, 10, &level))
3337 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003338
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 if (request_module("md-%s", clevel) != 0)
3340 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003341 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003342 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003343 if (!pers || !try_module_get(pers->owner)) {
3344 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003345 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003346 return -EINVAL;
3347 }
3348 spin_unlock(&pers_lock);
3349
3350 if (pers == mddev->pers) {
3351 /* Nothing to do! */
3352 module_put(pers->owner);
3353 return rv;
3354 }
3355 if (!pers->takeover) {
3356 module_put(pers->owner);
3357 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003358 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003359 return -EINVAL;
3360 }
3361
NeilBrowne93f68a2010-06-15 09:36:03 +01003362 list_for_each_entry(rdev, &mddev->disks, same_set)
3363 rdev->new_raid_disk = rdev->raid_disk;
3364
NeilBrown245f46c2009-03-31 14:39:39 +11003365 /* ->takeover must set new_* and/or delta_disks
3366 * if it succeeds, and may set them when it fails.
3367 */
3368 priv = pers->takeover(mddev);
3369 if (IS_ERR(priv)) {
3370 mddev->new_level = mddev->level;
3371 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003372 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003373 mddev->raid_disks -= mddev->delta_disks;
3374 mddev->delta_disks = 0;
3375 module_put(pers->owner);
3376 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003377 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003378 return PTR_ERR(priv);
3379 }
3380
3381 /* Looks like we have a winner */
3382 mddev_suspend(mddev);
3383 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003384
3385 if (mddev->pers->sync_request == NULL &&
3386 pers->sync_request != NULL) {
3387 /* need to add the md_redundancy_group */
3388 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3389 printk(KERN_WARNING
3390 "md: cannot register extra attributes for %s\n",
3391 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003392 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003393 }
3394 if (mddev->pers->sync_request != NULL &&
3395 pers->sync_request == NULL) {
3396 /* need to remove the md_redundancy_group */
3397 if (mddev->to_remove == NULL)
3398 mddev->to_remove = &md_redundancy_group;
3399 }
3400
Trela Maciej54071b32010-03-08 16:02:42 +11003401 if (mddev->pers->sync_request == NULL &&
3402 mddev->external) {
3403 /* We are converting from a no-redundancy array
3404 * to a redundancy array and metadata is managed
3405 * externally so we need to be sure that writes
3406 * won't block due to a need to transition
3407 * clean->dirty
3408 * until external management is started.
3409 */
3410 mddev->in_sync = 0;
3411 mddev->safemode_delay = 0;
3412 mddev->safemode = 0;
3413 }
3414
NeilBrowne93f68a2010-06-15 09:36:03 +01003415 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003416 if (rdev->raid_disk < 0)
3417 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003418 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003419 rdev->new_raid_disk = -1;
3420 if (rdev->new_raid_disk == rdev->raid_disk)
3421 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003422 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003423 }
3424 list_for_each_entry(rdev, &mddev->disks, same_set) {
3425 if (rdev->raid_disk < 0)
3426 continue;
3427 if (rdev->new_raid_disk == rdev->raid_disk)
3428 continue;
3429 rdev->raid_disk = rdev->new_raid_disk;
3430 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003431 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003432 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003433 if (sysfs_link_rdev(mddev, rdev))
3434 printk(KERN_WARNING "md: cannot register rd%d"
3435 " for %s after level change\n",
3436 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003437 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003438 }
3439
3440 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003441 mddev->pers = pers;
3442 mddev->private = priv;
3443 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3444 mddev->level = mddev->new_level;
3445 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003446 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003447 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003448 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003449 if (mddev->pers->sync_request == NULL) {
3450 /* this is now an array without redundancy, so
3451 * it must always be in_sync
3452 */
3453 mddev->in_sync = 1;
3454 del_timer_sync(&mddev->safemode_timer);
3455 }
NeilBrown245f46c2009-03-31 14:39:39 +11003456 pers->run(mddev);
3457 mddev_resume(mddev);
3458 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3459 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3460 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003461 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003462 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003463 return rv;
3464}
3465
3466static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003467__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003468
NeilBrownd4dbd022006-06-26 00:27:59 -07003469
3470static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003471layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003472{
3473 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003474 if (mddev->reshape_position != MaxSector &&
3475 mddev->layout != mddev->new_layout)
3476 return sprintf(page, "%d (%d)\n",
3477 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003478 return sprintf(page, "%d\n", mddev->layout);
3479}
3480
3481static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003482layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003483{
3484 char *e;
3485 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003486
3487 if (!*buf || (*e && *e != '\n'))
3488 return -EINVAL;
3489
NeilBrownb3546032009-03-31 14:56:41 +11003490 if (mddev->pers) {
3491 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003492 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003493 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003494 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003495 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003496 if (err) {
3497 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003498 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003499 }
NeilBrownb3546032009-03-31 14:56:41 +11003500 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003501 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003502 if (mddev->reshape_position == MaxSector)
3503 mddev->layout = n;
3504 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003505 return len;
3506}
3507static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003508__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003509
3510
NeilBrowneae17012005-11-08 21:39:23 -08003511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003512raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003513{
NeilBrownbb636542005-11-08 21:39:45 -08003514 if (mddev->raid_disks == 0)
3515 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003516 if (mddev->reshape_position != MaxSector &&
3517 mddev->delta_disks != 0)
3518 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3519 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003520 return sprintf(page, "%d\n", mddev->raid_disks);
3521}
3522
NeilBrownfd01b882011-10-11 16:47:53 +11003523static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003524
3525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003526raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003527{
NeilBrownda943b992006-01-06 00:20:54 -08003528 char *e;
3529 int rv = 0;
3530 unsigned long n = simple_strtoul(buf, &e, 10);
3531
3532 if (!*buf || (*e && *e != '\n'))
3533 return -EINVAL;
3534
3535 if (mddev->pers)
3536 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003537 else if (mddev->reshape_position != MaxSector) {
3538 int olddisks = mddev->raid_disks - mddev->delta_disks;
3539 mddev->delta_disks = n - olddisks;
3540 mddev->raid_disks = n;
3541 } else
NeilBrownda943b992006-01-06 00:20:54 -08003542 mddev->raid_disks = n;
3543 return rv ? rv : len;
3544}
3545static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003546__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003547
NeilBrown24dd4692005-11-08 21:39:26 -08003548static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003549chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003550{
NeilBrown08a02ec2007-05-09 02:35:38 -07003551 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003552 mddev->chunk_sectors != mddev->new_chunk_sectors)
3553 return sprintf(page, "%d (%d)\n",
3554 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003555 mddev->chunk_sectors << 9);
3556 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003557}
3558
3559static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003560chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003561{
NeilBrown3b343802006-01-06 00:20:47 -08003562 char *e;
3563 unsigned long n = simple_strtoul(buf, &e, 10);
3564
NeilBrown3b343802006-01-06 00:20:47 -08003565 if (!*buf || (*e && *e != '\n'))
3566 return -EINVAL;
3567
NeilBrownb3546032009-03-31 14:56:41 +11003568 if (mddev->pers) {
3569 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003570 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003571 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003572 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003573 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003574 if (err) {
3575 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003576 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003577 }
NeilBrownb3546032009-03-31 14:56:41 +11003578 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003579 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003580 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003581 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003582 }
NeilBrown3b343802006-01-06 00:20:47 -08003583 return len;
3584}
3585static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003586__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003587
NeilBrowna94213b2006-06-26 00:28:00 -07003588static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003589resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003590{
NeilBrownd1a7c502009-03-31 15:24:32 +11003591 if (mddev->recovery_cp == MaxSector)
3592 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003593 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3594}
3595
3596static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003597resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003598{
NeilBrowna94213b2006-06-26 00:28:00 -07003599 char *e;
3600 unsigned long long n = simple_strtoull(buf, &e, 10);
3601
NeilBrownb0986362011-05-11 15:52:21 +10003602 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003603 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003604 if (cmd_match(buf, "none"))
3605 n = MaxSector;
3606 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003607 return -EINVAL;
3608
3609 mddev->recovery_cp = n;
3610 return len;
3611}
3612static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003613__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003614
NeilBrown9e653b62006-06-26 00:27:58 -07003615/*
3616 * The array state can be:
3617 *
3618 * clear
3619 * No devices, no size, no level
3620 * Equivalent to STOP_ARRAY ioctl
3621 * inactive
3622 * May have some settings, but array is not active
3623 * all IO results in error
3624 * When written, doesn't tear down array, but just stops it
3625 * suspended (not supported yet)
3626 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003627 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003628 * readonly
3629 * no resync can happen. no superblocks get written.
3630 * write requests fail
3631 * read-auto
3632 * like readonly, but behaves like 'clean' on a write request.
3633 *
3634 * clean - no pending writes, but otherwise active.
3635 * When written to inactive array, starts without resync
3636 * If a write request arrives then
3637 * if metadata is known, mark 'dirty' and switch to 'active'.
3638 * if not known, block and switch to write-pending
3639 * If written to an active array that has pending writes, then fails.
3640 * active
3641 * fully active: IO and resync can be happening.
3642 * When written to inactive array, starts with resync
3643 *
3644 * write-pending
3645 * clean, but writes are blocked waiting for 'active' to be written.
3646 *
3647 * active-idle
3648 * like active, but no writes have been seen for a while (100msec).
3649 *
3650 */
3651enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3652 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003653static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003654 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3655 "write-pending", "active-idle", NULL };
3656
3657static int match_word(const char *word, char **list)
3658{
3659 int n;
3660 for (n=0; list[n]; n++)
3661 if (cmd_match(word, list[n]))
3662 break;
3663 return n;
3664}
3665
3666static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003667array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003668{
3669 enum array_state st = inactive;
3670
3671 if (mddev->pers)
3672 switch(mddev->ro) {
3673 case 1:
3674 st = readonly;
3675 break;
3676 case 2:
3677 st = read_auto;
3678 break;
3679 case 0:
3680 if (mddev->in_sync)
3681 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003682 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003683 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003684 else if (mddev->safemode)
3685 st = active_idle;
3686 else
3687 st = active;
3688 }
3689 else {
3690 if (list_empty(&mddev->disks) &&
3691 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003692 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003693 st = clear;
3694 else
3695 st = inactive;
3696 }
3697 return sprintf(page, "%s\n", array_states[st]);
3698}
3699
NeilBrownfd01b882011-10-11 16:47:53 +11003700static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3701static int md_set_readonly(struct mddev * mddev, int is_open);
3702static int do_md_run(struct mddev * mddev);
3703static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003704
3705static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003706array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003707{
3708 int err = -EINVAL;
3709 enum array_state st = match_word(buf, array_states);
3710 switch(st) {
3711 case bad_word:
3712 break;
3713 case clear:
3714 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003715 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003716 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003717 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003718 break;
3719 case inactive:
3720 /* stopping an active array */
3721 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003722 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003723 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003724 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003725 } else
3726 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003727 break;
3728 case suspended:
3729 break; /* not supported yet */
3730 case readonly:
3731 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003732 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003733 else {
3734 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003735 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003736 err = do_md_run(mddev);
3737 }
3738 break;
3739 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003740 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003741 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003742 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003743 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003744 err = restart_array(mddev);
3745 if (err == 0) {
3746 mddev->ro = 2;
3747 set_disk_ro(mddev->gendisk, 0);
3748 }
NeilBrown9e653b62006-06-26 00:27:58 -07003749 } else {
3750 mddev->ro = 2;
3751 err = do_md_run(mddev);
3752 }
3753 break;
3754 case clean:
3755 if (mddev->pers) {
3756 restart_array(mddev);
3757 spin_lock_irq(&mddev->write_lock);
3758 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003759 if (mddev->in_sync == 0) {
3760 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003761 if (mddev->safemode == 1)
3762 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003763 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003764 }
3765 err = 0;
3766 } else
3767 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003768 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003769 } else
3770 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003771 break;
3772 case active:
3773 if (mddev->pers) {
3774 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003775 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003776 wake_up(&mddev->sb_wait);
3777 err = 0;
3778 } else {
3779 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003780 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003781 err = do_md_run(mddev);
3782 }
3783 break;
3784 case write_pending:
3785 case active_idle:
3786 /* these cannot be set */
3787 break;
3788 }
3789 if (err)
3790 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003791 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003792 if (mddev->hold_active == UNTIL_IOCTL)
3793 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003794 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003795 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003796 }
NeilBrown9e653b62006-06-26 00:27:58 -07003797}
NeilBrown80ca3a42006-07-10 04:44:18 -07003798static struct md_sysfs_entry md_array_state =
3799__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003800
NeilBrown6d7ff7382006-01-06 00:21:16 -08003801static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003802max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003803 return sprintf(page, "%d\n",
3804 atomic_read(&mddev->max_corr_read_errors));
3805}
3806
3807static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003808max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003809{
3810 char *e;
3811 unsigned long n = simple_strtoul(buf, &e, 10);
3812
3813 if (*buf && (*e == 0 || *e == '\n')) {
3814 atomic_set(&mddev->max_corr_read_errors, n);
3815 return len;
3816 }
3817 return -EINVAL;
3818}
3819
3820static struct md_sysfs_entry max_corr_read_errors =
3821__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3822 max_corrected_read_errors_store);
3823
3824static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003825null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003826{
3827 return -EINVAL;
3828}
3829
3830static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003831new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003832{
3833 /* buf must be %d:%d\n? giving major and minor numbers */
3834 /* The new device is added to the array.
3835 * If the array has a persistent superblock, we read the
3836 * superblock to initialise info and check validity.
3837 * Otherwise, only checking done is that in bind_rdev_to_array,
3838 * which mainly checks size.
3839 */
3840 char *e;
3841 int major = simple_strtoul(buf, &e, 10);
3842 int minor;
3843 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003844 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003845 int err;
3846
3847 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3848 return -EINVAL;
3849 minor = simple_strtoul(e+1, &e, 10);
3850 if (*e && *e != '\n')
3851 return -EINVAL;
3852 dev = MKDEV(major, minor);
3853 if (major != MAJOR(dev) ||
3854 minor != MINOR(dev))
3855 return -EOVERFLOW;
3856
3857
3858 if (mddev->persistent) {
3859 rdev = md_import_device(dev, mddev->major_version,
3860 mddev->minor_version);
3861 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003862 struct md_rdev *rdev0
3863 = list_entry(mddev->disks.next,
3864 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003865 err = super_types[mddev->major_version]
3866 .load_super(rdev, rdev0, mddev->minor_version);
3867 if (err < 0)
3868 goto out;
3869 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003870 } else if (mddev->external)
3871 rdev = md_import_device(dev, -2, -1);
3872 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003873 rdev = md_import_device(dev, -1, -1);
3874
3875 if (IS_ERR(rdev))
3876 return PTR_ERR(rdev);
3877 err = bind_rdev_to_array(rdev, mddev);
3878 out:
3879 if (err)
3880 export_rdev(rdev);
3881 return err ? err : len;
3882}
3883
3884static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003885__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003886
3887static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003888bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003889{
3890 char *end;
3891 unsigned long chunk, end_chunk;
3892
3893 if (!mddev->bitmap)
3894 goto out;
3895 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3896 while (*buf) {
3897 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3898 if (buf == end) break;
3899 if (*end == '-') { /* range */
3900 buf = end + 1;
3901 end_chunk = simple_strtoul(buf, &end, 0);
3902 if (buf == end) break;
3903 }
3904 if (*end && !isspace(*end)) break;
3905 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003906 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003907 }
3908 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3909out:
3910 return len;
3911}
3912
3913static struct md_sysfs_entry md_bitmap =
3914__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3915
3916static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003917size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003918{
Andre Noll58c0fed2009-03-31 14:33:13 +11003919 return sprintf(page, "%llu\n",
3920 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003921}
3922
NeilBrownfd01b882011-10-11 16:47:53 +11003923static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003924
3925static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003926size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003927{
3928 /* If array is inactive, we can reduce the component size, but
3929 * not increase it (except from 0).
3930 * If array is active, we can try an on-line resize
3931 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003932 sector_t sectors;
3933 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003934
Andre Noll58c0fed2009-03-31 14:33:13 +11003935 if (err < 0)
3936 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003937 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003938 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003939 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003940 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003941 if (mddev->dev_sectors == 0 ||
3942 mddev->dev_sectors > sectors)
3943 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003944 else
3945 err = -ENOSPC;
3946 }
3947 return err ? err : len;
3948}
3949
3950static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003951__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003952
NeilBrown8bb93aa2006-01-06 00:20:50 -08003953
3954/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003955 * This is one of
3956 * 'none' for arrays with no metadata (good luck...)
3957 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003958 * or N.M for internally known formats
3959 */
3960static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003961metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003962{
3963 if (mddev->persistent)
3964 return sprintf(page, "%d.%d\n",
3965 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003966 else if (mddev->external)
3967 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003968 else
3969 return sprintf(page, "none\n");
3970}
3971
3972static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003973metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003974{
3975 int major, minor;
3976 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003977 /* Changing the details of 'external' metadata is
3978 * always permitted. Otherwise there must be
3979 * no devices attached to the array.
3980 */
3981 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3982 ;
3983 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003984 return -EBUSY;
3985
3986 if (cmd_match(buf, "none")) {
3987 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003988 mddev->external = 0;
3989 mddev->major_version = 0;
3990 mddev->minor_version = 90;
3991 return len;
3992 }
3993 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003994 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003995 if (namelen >= sizeof(mddev->metadata_type))
3996 namelen = sizeof(mddev->metadata_type)-1;
3997 strncpy(mddev->metadata_type, buf+9, namelen);
3998 mddev->metadata_type[namelen] = 0;
3999 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4000 mddev->metadata_type[--namelen] = 0;
4001 mddev->persistent = 0;
4002 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004003 mddev->major_version = 0;
4004 mddev->minor_version = 90;
4005 return len;
4006 }
4007 major = simple_strtoul(buf, &e, 10);
4008 if (e==buf || *e != '.')
4009 return -EINVAL;
4010 buf = e+1;
4011 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004012 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004013 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004014 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004015 return -ENOENT;
4016 mddev->major_version = major;
4017 mddev->minor_version = minor;
4018 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004019 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004020 return len;
4021}
4022
4023static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004024__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004025
NeilBrowna35b0d62006-01-06 00:20:49 -08004026static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004027action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004028{
NeilBrown7eec3142005-11-08 21:39:44 -08004029 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004030 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4031 type = "frozen";
4032 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004033 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004034 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4035 type = "reshape";
4036 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004037 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4038 type = "resync";
4039 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4040 type = "check";
4041 else
4042 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004043 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004044 type = "recover";
4045 }
4046 return sprintf(page, "%s\n", type);
4047}
4048
NeilBrownfd01b882011-10-11 16:47:53 +11004049static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004050
NeilBrown24dd4692005-11-08 21:39:26 -08004051static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004052action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004053{
NeilBrown7eec3142005-11-08 21:39:44 -08004054 if (!mddev->pers || !mddev->pers->sync_request)
4055 return -EINVAL;
4056
NeilBrownb6a9ce62009-05-26 09:41:17 +10004057 if (cmd_match(page, "frozen"))
4058 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4059 else
4060 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4061
4062 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004063 if (mddev->sync_thread) {
4064 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004065 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004066 }
NeilBrown03c902e2006-01-06 00:20:46 -08004067 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4068 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004069 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004070 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004071 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004072 else if (cmd_match(page, "recover")) {
4073 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4074 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4075 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004076 int err;
4077 if (mddev->pers->start_reshape == NULL)
4078 return -EINVAL;
4079 err = mddev->pers->start_reshape(mddev);
4080 if (err)
4081 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004082 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004083 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004084 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004085 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004086 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004087 return -EINVAL;
4088 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4089 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004090 }
NeilBrown03c902e2006-01-06 00:20:46 -08004091 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004092 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004093 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004094 return len;
4095}
4096
NeilBrown9d888832005-11-08 21:39:26 -08004097static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004098mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004099{
4100 return sprintf(page, "%llu\n",
4101 (unsigned long long) mddev->resync_mismatches);
4102}
4103
NeilBrown80ca3a42006-07-10 04:44:18 -07004104static struct md_sysfs_entry md_scan_mode =
4105__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004106
NeilBrown96de1e62005-11-08 21:39:39 -08004107
NeilBrown80ca3a42006-07-10 04:44:18 -07004108static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004109
NeilBrown88202a02006-01-06 00:21:36 -08004110static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004111sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004112{
4113 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4114 mddev->sync_speed_min ? "local": "system");
4115}
4116
4117static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004118sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004119{
4120 int min;
4121 char *e;
4122 if (strncmp(buf, "system", 6)==0) {
4123 mddev->sync_speed_min = 0;
4124 return len;
4125 }
4126 min = simple_strtoul(buf, &e, 10);
4127 if (buf == e || (*e && *e != '\n') || min <= 0)
4128 return -EINVAL;
4129 mddev->sync_speed_min = min;
4130 return len;
4131}
4132
4133static struct md_sysfs_entry md_sync_min =
4134__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4135
4136static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004137sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004138{
4139 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4140 mddev->sync_speed_max ? "local": "system");
4141}
4142
4143static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004144sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004145{
4146 int max;
4147 char *e;
4148 if (strncmp(buf, "system", 6)==0) {
4149 mddev->sync_speed_max = 0;
4150 return len;
4151 }
4152 max = simple_strtoul(buf, &e, 10);
4153 if (buf == e || (*e && *e != '\n') || max <= 0)
4154 return -EINVAL;
4155 mddev->sync_speed_max = max;
4156 return len;
4157}
4158
4159static struct md_sysfs_entry md_sync_max =
4160__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4161
Iustin Popd7f3d292007-10-16 23:30:54 -07004162static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004163degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004164{
4165 return sprintf(page, "%d\n", mddev->degraded);
4166}
4167static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004168
4169static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004170sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004171{
4172 return sprintf(page, "%d\n", mddev->parallel_resync);
4173}
4174
4175static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004176sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004177{
4178 long n;
4179
4180 if (strict_strtol(buf, 10, &n))
4181 return -EINVAL;
4182
4183 if (n != 0 && n != 1)
4184 return -EINVAL;
4185
4186 mddev->parallel_resync = n;
4187
4188 if (mddev->sync_thread)
4189 wake_up(&resync_wait);
4190
4191 return len;
4192}
4193
4194/* force parallel resync, even with shared block devices */
4195static struct md_sysfs_entry md_sync_force_parallel =
4196__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4197 sync_force_parallel_show, sync_force_parallel_store);
4198
4199static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004200sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004201{
4202 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004203 if (mddev->curr_resync == 0)
4204 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004205 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4206 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004207 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004208 db = resync - mddev->resync_mark_cnt;
4209 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004210}
4211
NeilBrown80ca3a42006-07-10 04:44:18 -07004212static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004213
4214static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004215sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004216{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004217 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004218
NeilBrownacb180b2009-04-14 16:28:34 +10004219 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4220 return sprintf(page, "none\n");
4221
NeilBrown88202a02006-01-06 00:21:36 -08004222 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004223 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004224 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004225 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004226
NeilBrownacb180b2009-04-14 16:28:34 +10004227 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004228 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004229}
4230
NeilBrown80ca3a42006-07-10 04:44:18 -07004231static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004232
NeilBrowne464eaf2006-03-27 01:18:14 -08004233static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004234min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004235{
4236 return sprintf(page, "%llu\n",
4237 (unsigned long long)mddev->resync_min);
4238}
4239static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004240min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004241{
4242 unsigned long long min;
4243 if (strict_strtoull(buf, 10, &min))
4244 return -EINVAL;
4245 if (min > mddev->resync_max)
4246 return -EINVAL;
4247 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4248 return -EBUSY;
4249
4250 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004251 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004252 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004253 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004254 return -EINVAL;
4255 }
4256 mddev->resync_min = min;
4257
4258 return len;
4259}
4260
4261static struct md_sysfs_entry md_min_sync =
4262__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4263
4264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004265max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004266{
4267 if (mddev->resync_max == MaxSector)
4268 return sprintf(page, "max\n");
4269 else
4270 return sprintf(page, "%llu\n",
4271 (unsigned long long)mddev->resync_max);
4272}
4273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004274max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004275{
4276 if (strncmp(buf, "max", 3) == 0)
4277 mddev->resync_max = MaxSector;
4278 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004279 unsigned long long max;
4280 if (strict_strtoull(buf, 10, &max))
4281 return -EINVAL;
4282 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004283 return -EINVAL;
4284 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004285 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004286 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4287 return -EBUSY;
4288
4289 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004290 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004291 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004292 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004293 return -EINVAL;
4294 }
4295 mddev->resync_max = max;
4296 }
4297 wake_up(&mddev->recovery_wait);
4298 return len;
4299}
4300
4301static struct md_sysfs_entry md_max_sync =
4302__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4303
4304static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004305suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004306{
4307 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4308}
4309
4310static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004311suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004312{
4313 char *e;
4314 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004315 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004316
NeilBrownb8d966e2009-07-01 11:14:04 +10004317 if (mddev->pers == NULL ||
4318 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004319 return -EINVAL;
4320 if (buf == e || (*e && *e != '\n'))
4321 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004322
4323 mddev->suspend_lo = new;
4324 if (new >= old)
4325 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004326 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004327 else {
4328 /* Expanding suspended region - need to wait */
4329 mddev->pers->quiesce(mddev, 1);
4330 mddev->pers->quiesce(mddev, 0);
4331 }
4332 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004333}
4334static struct md_sysfs_entry md_suspend_lo =
4335__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4336
4337
4338static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004339suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004340{
4341 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4342}
4343
4344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004345suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004346{
4347 char *e;
4348 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004349 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004350
NeilBrownb8d966e2009-07-01 11:14:04 +10004351 if (mddev->pers == NULL ||
4352 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004353 return -EINVAL;
4354 if (buf == e || (*e && *e != '\n'))
4355 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004356
4357 mddev->suspend_hi = new;
4358 if (new <= old)
4359 /* Shrinking suspended region */
4360 mddev->pers->quiesce(mddev, 2);
4361 else {
4362 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004363 mddev->pers->quiesce(mddev, 1);
4364 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004365 }
4366 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004367}
4368static struct md_sysfs_entry md_suspend_hi =
4369__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4370
NeilBrown08a02ec2007-05-09 02:35:38 -07004371static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004372reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004373{
4374 if (mddev->reshape_position != MaxSector)
4375 return sprintf(page, "%llu\n",
4376 (unsigned long long)mddev->reshape_position);
4377 strcpy(page, "none\n");
4378 return 5;
4379}
4380
4381static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004382reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004383{
4384 char *e;
4385 unsigned long long new = simple_strtoull(buf, &e, 10);
4386 if (mddev->pers)
4387 return -EBUSY;
4388 if (buf == e || (*e && *e != '\n'))
4389 return -EINVAL;
4390 mddev->reshape_position = new;
4391 mddev->delta_disks = 0;
4392 mddev->new_level = mddev->level;
4393 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004394 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004395 return len;
4396}
4397
4398static struct md_sysfs_entry md_reshape_position =
4399__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4400 reshape_position_store);
4401
Dan Williamsb522adc2009-03-31 15:00:31 +11004402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004404{
4405 if (mddev->external_size)
4406 return sprintf(page, "%llu\n",
4407 (unsigned long long)mddev->array_sectors/2);
4408 else
4409 return sprintf(page, "default\n");
4410}
4411
4412static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004413array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004414{
4415 sector_t sectors;
4416
4417 if (strncmp(buf, "default", 7) == 0) {
4418 if (mddev->pers)
4419 sectors = mddev->pers->size(mddev, 0, 0);
4420 else
4421 sectors = mddev->array_sectors;
4422
4423 mddev->external_size = 0;
4424 } else {
4425 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4426 return -EINVAL;
4427 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004428 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004429
4430 mddev->external_size = 1;
4431 }
4432
4433 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004434 if (mddev->pers) {
4435 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004436 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004437 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004438 return len;
4439}
4440
4441static struct md_sysfs_entry md_array_size =
4442__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4443 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004444
NeilBrowneae17012005-11-08 21:39:23 -08004445static struct attribute *md_default_attrs[] = {
4446 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004447 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004448 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004449 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004450 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004451 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004452 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004453 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004454 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004455 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004456 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004457 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004458 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004459 NULL,
4460};
4461
4462static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004463 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004464 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004465 &md_sync_min.attr,
4466 &md_sync_max.attr,
4467 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004468 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004469 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004470 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004471 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004472 &md_suspend_lo.attr,
4473 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004474 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004475 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004476 NULL,
4477};
NeilBrown411036f2005-11-08 21:39:40 -08004478static struct attribute_group md_redundancy_group = {
4479 .name = NULL,
4480 .attrs = md_redundancy_attrs,
4481};
4482
NeilBrowneae17012005-11-08 21:39:23 -08004483
4484static ssize_t
4485md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4486{
4487 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004488 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004489 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004490
4491 if (!entry->show)
4492 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004493 spin_lock(&all_mddevs_lock);
4494 if (list_empty(&mddev->all_mddevs)) {
4495 spin_unlock(&all_mddevs_lock);
4496 return -EBUSY;
4497 }
4498 mddev_get(mddev);
4499 spin_unlock(&all_mddevs_lock);
4500
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004501 rv = mddev_lock(mddev);
4502 if (!rv) {
4503 rv = entry->show(mddev, page);
4504 mddev_unlock(mddev);
4505 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004506 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004507 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004508}
4509
4510static ssize_t
4511md_attr_store(struct kobject *kobj, struct attribute *attr,
4512 const char *page, size_t length)
4513{
4514 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004515 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004516 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004517
4518 if (!entry->store)
4519 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004520 if (!capable(CAP_SYS_ADMIN))
4521 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004522 spin_lock(&all_mddevs_lock);
4523 if (list_empty(&mddev->all_mddevs)) {
4524 spin_unlock(&all_mddevs_lock);
4525 return -EBUSY;
4526 }
4527 mddev_get(mddev);
4528 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004529 rv = mddev_lock(mddev);
4530 if (!rv) {
4531 rv = entry->store(mddev, page, length);
4532 mddev_unlock(mddev);
4533 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004534 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004535 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004536}
4537
4538static void md_free(struct kobject *ko)
4539{
NeilBrownfd01b882011-10-11 16:47:53 +11004540 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004541
4542 if (mddev->sysfs_state)
4543 sysfs_put(mddev->sysfs_state);
4544
4545 if (mddev->gendisk) {
4546 del_gendisk(mddev->gendisk);
4547 put_disk(mddev->gendisk);
4548 }
4549 if (mddev->queue)
4550 blk_cleanup_queue(mddev->queue);
4551
NeilBrowneae17012005-11-08 21:39:23 -08004552 kfree(mddev);
4553}
4554
Emese Revfy52cf25d2010-01-19 02:58:23 +01004555static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004556 .show = md_attr_show,
4557 .store = md_attr_store,
4558};
4559static struct kobj_type md_ktype = {
4560 .release = md_free,
4561 .sysfs_ops = &md_sysfs_ops,
4562 .default_attrs = md_default_attrs,
4563};
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565int mdp_major = 0;
4566
Dan Williams5fd3a172009-03-04 00:57:25 -07004567static void mddev_delayed_delete(struct work_struct *ws)
4568{
NeilBrownfd01b882011-10-11 16:47:53 +11004569 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004570
NeilBrown43a70502009-12-14 12:49:55 +11004571 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004572 kobject_del(&mddev->kobj);
4573 kobject_put(&mddev->kobj);
4574}
4575
NeilBrownefeb53c2009-01-09 08:31:10 +11004576static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004578 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004579 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004581 int partitioned;
4582 int shift;
4583 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004584 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004587 return -ENODEV;
4588
4589 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4590 shift = partitioned ? MdpMinorShift : 0;
4591 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Tejun Heoe804ac72010-10-15 15:36:08 +02004593 /* wait for any previous instance of this device to be
4594 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004595 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004596 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004597
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004598 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004599 error = -EEXIST;
4600 if (mddev->gendisk)
4601 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004602
4603 if (name) {
4604 /* Need to ensure that 'name' is not a duplicate.
4605 */
NeilBrownfd01b882011-10-11 16:47:53 +11004606 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004607 spin_lock(&all_mddevs_lock);
4608
4609 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4610 if (mddev2->gendisk &&
4611 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4612 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004613 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004614 }
4615 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
NeilBrown8b765392009-01-09 08:31:08 +11004617
NeilBrown0909dc42009-07-01 12:27:21 +10004618 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004619 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004620 if (!mddev->queue)
4621 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004622 mddev->queue->queuedata = mddev;
4623
NeilBrown409c57f2009-03-31 14:39:39 +11004624 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004625 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 disk = alloc_disk(1 << shift);
4628 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004629 blk_cleanup_queue(mddev->queue);
4630 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004631 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004633 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004635 if (name)
4636 strcpy(disk->disk_name, name);
4637 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004639 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 disk->fops = &md_fops;
4642 disk->private_data = mddev;
4643 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004644 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004645 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004646 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004647 * remove it now.
4648 */
4649 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004651 /* As soon as we call add_disk(), another thread could get
4652 * through to md_open, so make sure it doesn't get too far
4653 */
4654 mutex_lock(&mddev->open_mutex);
4655 add_disk(disk);
4656
Tejun Heoed9e1982008-08-25 19:56:05 +09004657 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4658 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004659 if (error) {
4660 /* This isn't possible, but as kobject_init_and_add is marked
4661 * __must_check, we must do something with the result
4662 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004663 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4664 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004665 error = 0;
4666 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004667 if (mddev->kobj.sd &&
4668 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004669 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004670 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004671 abort:
4672 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004673 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004674 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004675 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004676 }
NeilBrownd3374822009-01-09 08:31:10 +11004677 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004678 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004679}
4680
4681static struct kobject *md_probe(dev_t dev, int *part, void *data)
4682{
4683 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return NULL;
4685}
4686
NeilBrownefeb53c2009-01-09 08:31:10 +11004687static int add_named_array(const char *val, struct kernel_param *kp)
4688{
4689 /* val must be "md_*" where * is not all digits.
4690 * We allocate an array with a large free minor number, and
4691 * set the name to val. val must not already be an active name.
4692 */
4693 int len = strlen(val);
4694 char buf[DISK_NAME_LEN];
4695
4696 while (len && val[len-1] == '\n')
4697 len--;
4698 if (len >= DISK_NAME_LEN)
4699 return -E2BIG;
4700 strlcpy(buf, val, len+1);
4701 if (strncmp(buf, "md_", 3) != 0)
4702 return -EINVAL;
4703 return md_alloc(0, buf);
4704}
4705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706static void md_safemode_timeout(unsigned long data)
4707{
NeilBrownfd01b882011-10-11 16:47:53 +11004708 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Neil Brown0fd62b82008-06-28 08:31:36 +10004710 if (!atomic_read(&mddev->writes_pending)) {
4711 mddev->safemode = 1;
4712 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004713 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 md_wakeup_thread(mddev->thread);
4716}
4717
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004718static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
NeilBrownfd01b882011-10-11 16:47:53 +11004720int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
NeilBrown2604b702006-01-06 00:20:36 -08004722 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004723 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004724 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
NeilBrowna757e642005-04-16 15:26:42 -07004726 if (list_empty(&mddev->disks))
4727 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 if (mddev->pers)
4731 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004732 /* Cannot run until previous stop completes properly */
4733 if (mddev->sysfs_active)
4734 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004735
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /*
4737 * Analyze all RAID superblock(s)
4738 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004739 if (!mddev->raid_disks) {
4740 if (!mddev->persistent)
4741 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004742 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
NeilBrownd9d166c2006-01-06 00:20:51 -08004745 if (mddev->level != LEVEL_NONE)
4746 request_module("md-level-%d", mddev->level);
4747 else if (mddev->clevel[0])
4748 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 /*
4751 * Drop all container device buffers, from now on
4752 * the only valid external interface is through the md
4753 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004755 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004756 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 continue;
4758 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004759 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004760
4761 /* perform some consistency tests on the device.
4762 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004763 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004764 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004765 if (rdev->meta_bdev) {
4766 /* Nothing to check */;
4767 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004768 if (mddev->dev_sectors &&
4769 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004770 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004771 printk("md: %s: data overlaps metadata\n",
4772 mdname(mddev));
4773 return -EINVAL;
4774 }
4775 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004776 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004777 > rdev->data_offset) {
4778 printk("md: %s: metadata overlaps data\n",
4779 mdname(mddev));
4780 return -EINVAL;
4781 }
4782 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004783 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
4785
NeilBrowna167f662010-10-26 18:31:13 +11004786 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004787 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004788 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004791 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004792 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004794 if (mddev->level != LEVEL_NONE)
4795 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4796 mddev->level);
4797 else
4798 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4799 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return -EINVAL;
4801 }
NeilBrown2604b702006-01-06 00:20:36 -08004802 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004804 if (mddev->level != pers->level) {
4805 mddev->level = pers->level;
4806 mddev->new_level = pers->level;
4807 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004808 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
NeilBrownf6705572006-03-27 01:18:11 -08004810 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004811 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004812 /* This personality cannot handle reshaping... */
4813 mddev->pers = NULL;
4814 module_put(pers->owner);
4815 return -EINVAL;
4816 }
4817
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004818 if (pers->sync_request) {
4819 /* Warn if this is a potentially silly
4820 * configuration.
4821 */
4822 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004823 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004824 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004825
4826 list_for_each_entry(rdev, &mddev->disks, same_set)
4827 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004828 if (rdev < rdev2 &&
4829 rdev->bdev->bd_contains ==
4830 rdev2->bdev->bd_contains) {
4831 printk(KERN_WARNING
4832 "%s: WARNING: %s appears to be"
4833 " on the same physical disk as"
4834 " %s.\n",
4835 mdname(mddev),
4836 bdevname(rdev->bdev,b),
4837 bdevname(rdev2->bdev,b2));
4838 warned = 1;
4839 }
4840 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004841
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004842 if (warned)
4843 printk(KERN_WARNING
4844 "True protection against single-disk"
4845 " failure might be compromised.\n");
4846 }
4847
NeilBrown657390d2005-08-26 18:34:16 -07004848 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004849 /* may be over-ridden by personality */
4850 mddev->resync_max_sectors = mddev->dev_sectors;
4851
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004852 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
NeilBrown0f9552b52009-12-30 12:08:50 +11004854 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004855 mddev->ro = 2; /* read-only, but switch on first write */
4856
NeilBrownb15c2e52006-01-06 00:20:16 -08004857 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004858 if (err)
4859 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004860 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4861 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4862 " but 'external_size' not in effect?\n", __func__);
4863 printk(KERN_ERR
4864 "md: invalid array_size %llu > default size %llu\n",
4865 (unsigned long long)mddev->array_sectors / 2,
4866 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4867 err = -EINVAL;
4868 mddev->pers->stop(mddev);
4869 }
4870 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004871 err = bitmap_create(mddev);
4872 if (err) {
4873 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4874 mdname(mddev), err);
4875 mddev->pers->stop(mddev);
4876 }
4877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 module_put(mddev->pers->owner);
4880 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004881 bitmap_destroy(mddev);
4882 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004884 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004885 if (mddev->kobj.sd &&
4886 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004887 printk(KERN_WARNING
4888 "md: cannot register extra attributes for %s\n",
4889 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004890 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004891 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004892 mddev->ro = 0;
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004895 atomic_set(&mddev->max_corr_read_errors,
4896 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 mddev->safemode = 0;
4898 mddev->safemode_timer.function = md_safemode_timeout;
4899 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004900 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004902 smp_wmb();
4903 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004904 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004905 if (rdev->raid_disk >= 0)
4906 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004907 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4910
NeilBrown850b2b422006-10-03 01:15:46 -07004911 if (mddev->flags)
4912 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
NeilBrownd7603b72006-01-06 00:20:30 -08004914 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004915 sysfs_notify_dirent_safe(mddev->sysfs_state);
4916 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004917 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return 0;
4919}
NeilBrown390ee602010-06-01 19:37:27 +10004920EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
NeilBrownfd01b882011-10-11 16:47:53 +11004922static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004923{
4924 int err;
4925
4926 err = md_run(mddev);
4927 if (err)
4928 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004929 err = bitmap_load(mddev);
4930 if (err) {
4931 bitmap_destroy(mddev);
4932 goto out;
4933 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004934
4935 md_wakeup_thread(mddev->thread);
4936 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4937
NeilBrownfe60b012010-03-29 11:10:42 +11004938 set_capacity(mddev->gendisk, mddev->array_sectors);
4939 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004940 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004941 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4942out:
4943 return err;
4944}
4945
NeilBrownfd01b882011-10-11 16:47:53 +11004946static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
4948 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Andre Noll80fab1d2008-07-11 22:02:21 +10004950 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004952 return -ENXIO;
4953 if (!mddev->pers)
4954 return -EINVAL;
4955 if (!mddev->ro)
4956 return -EBUSY;
4957 mddev->safemode = 0;
4958 mddev->ro = 0;
4959 set_disk_ro(disk, 0);
4960 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4961 mdname(mddev));
4962 /* Kick recovery or resync if necessary */
4963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4964 md_wakeup_thread(mddev->thread);
4965 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004966 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
NeilBrownacc55e22006-06-26 00:27:47 -07004970/* similar to deny_write_access, but accounts for our holding a reference
4971 * to the file ourselves */
4972static int deny_bitmap_write_access(struct file * file)
4973{
4974 struct inode *inode = file->f_mapping->host;
4975
4976 spin_lock(&inode->i_lock);
4977 if (atomic_read(&inode->i_writecount) > 1) {
4978 spin_unlock(&inode->i_lock);
4979 return -ETXTBSY;
4980 }
4981 atomic_set(&inode->i_writecount, -1);
4982 spin_unlock(&inode->i_lock);
4983
4984 return 0;
4985}
4986
NeilBrown43a70502009-12-14 12:49:55 +11004987void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004988{
4989 struct inode *inode = file->f_mapping->host;
4990
4991 spin_lock(&inode->i_lock);
4992 atomic_set(&inode->i_writecount, 1);
4993 spin_unlock(&inode->i_lock);
4994}
4995
NeilBrownfd01b882011-10-11 16:47:53 +11004996static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004997{
4998 mddev->array_sectors = 0;
4999 mddev->external_size = 0;
5000 mddev->dev_sectors = 0;
5001 mddev->raid_disks = 0;
5002 mddev->recovery_cp = 0;
5003 mddev->resync_min = 0;
5004 mddev->resync_max = MaxSector;
5005 mddev->reshape_position = MaxSector;
5006 mddev->external = 0;
5007 mddev->persistent = 0;
5008 mddev->level = LEVEL_NONE;
5009 mddev->clevel[0] = 0;
5010 mddev->flags = 0;
5011 mddev->ro = 0;
5012 mddev->metadata_type[0] = 0;
5013 mddev->chunk_sectors = 0;
5014 mddev->ctime = mddev->utime = 0;
5015 mddev->layout = 0;
5016 mddev->max_disks = 0;
5017 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005018 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005019 mddev->delta_disks = 0;
5020 mddev->new_level = LEVEL_NONE;
5021 mddev->new_layout = 0;
5022 mddev->new_chunk_sectors = 0;
5023 mddev->curr_resync = 0;
5024 mddev->resync_mismatches = 0;
5025 mddev->suspend_lo = mddev->suspend_hi = 0;
5026 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5027 mddev->recovery = 0;
5028 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005029 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005030 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005031 mddev->safemode = 0;
5032 mddev->bitmap_info.offset = 0;
5033 mddev->bitmap_info.default_offset = 0;
5034 mddev->bitmap_info.chunksize = 0;
5035 mddev->bitmap_info.daemon_sleep = 0;
5036 mddev->bitmap_info.max_write_behind = 0;
5037}
5038
NeilBrownfd01b882011-10-11 16:47:53 +11005039static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005040{
5041 if (mddev->sync_thread) {
5042 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5043 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005044 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005045 }
5046
5047 del_timer_sync(&mddev->safemode_timer);
5048
5049 bitmap_flush(mddev);
5050 md_super_wait(mddev);
5051
5052 if (!mddev->in_sync || mddev->flags) {
5053 /* mark array as shutdown cleanly */
5054 mddev->in_sync = 1;
5055 md_update_sb(mddev, 1);
5056 }
5057}
NeilBrowndefad612011-01-14 09:14:33 +11005058
NeilBrownfd01b882011-10-11 16:47:53 +11005059void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005060{
5061 mddev_lock(mddev);
5062 __md_stop_writes(mddev);
5063 mddev_unlock(mddev);
5064}
NeilBrown390ee602010-06-01 19:37:27 +10005065EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005066
NeilBrownfd01b882011-10-11 16:47:53 +11005067void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005068{
NeilBrown0ca69882011-01-14 09:14:33 +11005069 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005070 mddev->pers->stop(mddev);
5071 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5072 mddev->to_remove = &md_redundancy_group;
5073 module_put(mddev->pers->owner);
5074 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005075 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005076}
NeilBrown390ee602010-06-01 19:37:27 +10005077EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005078
NeilBrownfd01b882011-10-11 16:47:53 +11005079static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005080{
5081 int err = 0;
5082 mutex_lock(&mddev->open_mutex);
5083 if (atomic_read(&mddev->openers) > is_open) {
5084 printk("md: %s still in use.\n",mdname(mddev));
5085 err = -EBUSY;
5086 goto out;
5087 }
5088 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005089 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005090
5091 err = -ENXIO;
5092 if (mddev->ro==1)
5093 goto out;
5094 mddev->ro = 1;
5095 set_disk_ro(mddev->gendisk, 1);
5096 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005097 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005098 err = 0;
5099 }
5100out:
5101 mutex_unlock(&mddev->open_mutex);
5102 return err;
5103}
5104
NeilBrown9e653b62006-06-26 00:27:58 -07005105/* mode:
5106 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005107 * 2 - stop but do not disassemble array
5108 */
NeilBrownfd01b882011-10-11 16:47:53 +11005109static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005112 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
NeilBrownc8c00a62009-08-10 12:50:52 +10005114 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005115 if (atomic_read(&mddev->openers) > is_open ||
5116 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005117 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005118 mutex_unlock(&mddev->open_mutex);
5119 return -EBUSY;
5120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
NeilBrown6e17b022010-08-07 21:41:19 +10005122 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005123 if (mddev->ro)
5124 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005125
NeilBrowndefad612011-01-14 09:14:33 +11005126 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005127 md_stop(mddev);
5128 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005129 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005130
NeilBrowna4bd82d2010-03-29 13:23:10 +11005131 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005132 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005133
NeilBrowna4bd82d2010-03-29 13:23:10 +11005134 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005135 if (rdev->raid_disk >= 0)
5136 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005137
NeilBrowna4bd82d2010-03-29 13:23:10 +11005138 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005139 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005140 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005141 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005142
NeilBrowna4bd82d2010-03-29 13:23:10 +11005143 if (mddev->ro)
5144 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005145 } else
5146 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 /*
5148 * Free resources if final stop
5149 */
NeilBrown9e653b62006-06-26 00:27:58 -07005150 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5152
NeilBrown978f9462006-02-02 14:28:05 -08005153 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005154 if (mddev->bitmap_info.file) {
5155 restore_bitmap_write_access(mddev->bitmap_info.file);
5156 fput(mddev->bitmap_info.file);
5157 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005158 }
NeilBrownc3d97142009-12-14 12:49:52 +11005159 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 export_array(mddev);
5162
NeilBrown6177b472010-03-29 11:37:13 +11005163 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005164 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005165 if (mddev->hold_active == UNTIL_STOP)
5166 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005167 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005168 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005169 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005170 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172}
5173
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005174#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005175static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
NeilBrown3cb03002011-10-11 16:45:26 +11005177 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 int err;
5179
NeilBrowna757e642005-04-16 15:26:42 -07005180 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
5183 printk(KERN_INFO "md: running: ");
5184
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005185 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 char b[BDEVNAME_SIZE];
5187 printk("<%s>", bdevname(rdev->bdev,b));
5188 }
5189 printk("\n");
5190
NeilBrownd710e132008-10-13 11:55:12 +11005191 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 if (err) {
5193 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005194 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 }
5196}
5197
5198/*
5199 * lets try to run arrays based on all disks that have arrived
5200 * until now. (those are in pending_raid_disks)
5201 *
5202 * the method: pick the first pending disk, collect all disks with
5203 * the same UUID, remove all from the pending list and put them into
5204 * the 'same_array' list. Then order this list based on superblock
5205 * update time (freshest comes first), kick out 'old' disks and
5206 * compare superblocks. If everything's fine then run it.
5207 *
5208 * If "unit" is allocated, then bump its reference count
5209 */
5210static void autorun_devices(int part)
5211{
NeilBrown3cb03002011-10-11 16:45:26 +11005212 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005213 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 char b[BDEVNAME_SIZE];
5215
5216 printk(KERN_INFO "md: autorun ...\n");
5217 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005218 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005220 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005222 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
5224 printk(KERN_INFO "md: considering %s ...\n",
5225 bdevname(rdev0->bdev,b));
5226 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005227 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 if (super_90_load(rdev, rdev0, 0) >= 0) {
5229 printk(KERN_INFO "md: adding %s ...\n",
5230 bdevname(rdev->bdev,b));
5231 list_move(&rdev->same_set, &candidates);
5232 }
5233 /*
5234 * now we have a set of devices, with all of them having
5235 * mostly sane superblocks. It's time to allocate the
5236 * mddev.
5237 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005238 if (part) {
5239 dev = MKDEV(mdp_major,
5240 rdev0->preferred_minor << MdpMinorShift);
5241 unit = MINOR(dev) >> MdpMinorShift;
5242 } else {
5243 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5244 unit = MINOR(dev);
5245 }
5246 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5248 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5249 break;
5250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 md_probe(dev, NULL, NULL);
5253 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005254 if (!mddev || !mddev->gendisk) {
5255 if (mddev)
5256 mddev_put(mddev);
5257 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 "md: cannot allocate memory for md drive.\n");
5259 break;
5260 }
5261 if (mddev_lock(mddev))
5262 printk(KERN_WARNING "md: %s locked, cannot run\n",
5263 mdname(mddev));
5264 else if (mddev->raid_disks || mddev->major_version
5265 || !list_empty(&mddev->disks)) {
5266 printk(KERN_WARNING
5267 "md: %s already running, cannot run %s\n",
5268 mdname(mddev), bdevname(rdev0->bdev,b));
5269 mddev_unlock(mddev);
5270 } else {
5271 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005272 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005273 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 list_del_init(&rdev->same_set);
5275 if (bind_rdev_to_array(rdev, mddev))
5276 export_rdev(rdev);
5277 }
5278 autorun_array(mddev);
5279 mddev_unlock(mddev);
5280 }
5281 /* on success, candidates will be empty, on error
5282 * it won't...
5283 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005284 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005285 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 mddev_put(mddev);
5289 }
5290 printk(KERN_INFO "md: ... autorun DONE.\n");
5291}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005292#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294static int get_version(void __user * arg)
5295{
5296 mdu_version_t ver;
5297
5298 ver.major = MD_MAJOR_VERSION;
5299 ver.minor = MD_MINOR_VERSION;
5300 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5301
5302 if (copy_to_user(arg, &ver, sizeof(ver)))
5303 return -EFAULT;
5304
5305 return 0;
5306}
5307
NeilBrownfd01b882011-10-11 16:47:53 +11005308static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
5310 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005311 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005312 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
NeilBrowna9f326e2009-09-23 18:06:41 +10005314 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005315 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005317 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 failed++;
5319 else {
5320 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005321 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005322 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 else
5324 spare++;
5325 }
5326 }
5327
5328 info.major_version = mddev->major_version;
5329 info.minor_version = mddev->minor_version;
5330 info.patch_version = MD_PATCHLEVEL_VERSION;
5331 info.ctime = mddev->ctime;
5332 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005333 info.size = mddev->dev_sectors / 2;
5334 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005335 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 info.nr_disks = nr;
5337 info.raid_disks = mddev->raid_disks;
5338 info.md_minor = mddev->md_minor;
5339 info.not_persistent= !mddev->persistent;
5340
5341 info.utime = mddev->utime;
5342 info.state = 0;
5343 if (mddev->in_sync)
5344 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005345 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005346 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005347 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 info.working_disks = working;
5349 info.failed_disks = failed;
5350 info.spare_disks = spare;
5351
5352 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005353 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 if (copy_to_user(arg, &info, sizeof(info)))
5356 return -EFAULT;
5357
5358 return 0;
5359}
5360
NeilBrownfd01b882011-10-11 16:47:53 +11005361static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005362{
5363 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5364 char *ptr, *buf = NULL;
5365 int err = -ENOMEM;
5366
Dan Williamsb5470dc2008-06-27 21:44:04 -07005367 if (md_allow_write(mddev))
5368 file = kmalloc(sizeof(*file), GFP_NOIO);
5369 else
5370 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005371
NeilBrown32a76272005-06-21 17:17:14 -07005372 if (!file)
5373 goto out;
5374
5375 /* bitmap disabled, zero the first byte and copy out */
5376 if (!mddev->bitmap || !mddev->bitmap->file) {
5377 file->pathname[0] = '\0';
5378 goto copy_out;
5379 }
5380
5381 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5382 if (!buf)
5383 goto out;
5384
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005385 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5386 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005387 goto out;
5388
5389 strcpy(file->pathname, ptr);
5390
5391copy_out:
5392 err = 0;
5393 if (copy_to_user(arg, file, sizeof(*file)))
5394 err = -EFAULT;
5395out:
5396 kfree(buf);
5397 kfree(file);
5398 return err;
5399}
5400
NeilBrownfd01b882011-10-11 16:47:53 +11005401static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402{
5403 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005404 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
5406 if (copy_from_user(&info, arg, sizeof(info)))
5407 return -EFAULT;
5408
Andre Noll26ef3792008-07-11 22:02:21 +10005409 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 if (rdev) {
5411 info.major = MAJOR(rdev->bdev->bd_dev);
5412 info.minor = MINOR(rdev->bdev->bd_dev);
5413 info.raid_disk = rdev->raid_disk;
5414 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005415 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005417 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 info.state |= (1<<MD_DISK_ACTIVE);
5419 info.state |= (1<<MD_DISK_SYNC);
5420 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005421 if (test_bit(WriteMostly, &rdev->flags))
5422 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 } else {
5424 info.major = info.minor = 0;
5425 info.raid_disk = -1;
5426 info.state = (1<<MD_DISK_REMOVED);
5427 }
5428
5429 if (copy_to_user(arg, &info, sizeof(info)))
5430 return -EFAULT;
5431
5432 return 0;
5433}
5434
NeilBrownfd01b882011-10-11 16:47:53 +11005435static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005438 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 dev_t dev = MKDEV(info->major,info->minor);
5440
5441 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5442 return -EOVERFLOW;
5443
5444 if (!mddev->raid_disks) {
5445 int err;
5446 /* expecting a device which has a superblock */
5447 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5448 if (IS_ERR(rdev)) {
5449 printk(KERN_WARNING
5450 "md: md_import_device returned %ld\n",
5451 PTR_ERR(rdev));
5452 return PTR_ERR(rdev);
5453 }
5454 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005455 struct md_rdev *rdev0
5456 = list_entry(mddev->disks.next,
5457 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005458 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 .load_super(rdev, rdev0, mddev->minor_version);
5460 if (err < 0) {
5461 printk(KERN_WARNING
5462 "md: %s has different UUID to %s\n",
5463 bdevname(rdev->bdev,b),
5464 bdevname(rdev0->bdev,b2));
5465 export_rdev(rdev);
5466 return -EINVAL;
5467 }
5468 }
5469 err = bind_rdev_to_array(rdev, mddev);
5470 if (err)
5471 export_rdev(rdev);
5472 return err;
5473 }
5474
5475 /*
5476 * add_new_disk can be used once the array is assembled
5477 * to add "hot spares". They must already have a superblock
5478 * written
5479 */
5480 if (mddev->pers) {
5481 int err;
5482 if (!mddev->pers->hot_add_disk) {
5483 printk(KERN_WARNING
5484 "%s: personality does not support diskops!\n",
5485 mdname(mddev));
5486 return -EINVAL;
5487 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005488 if (mddev->persistent)
5489 rdev = md_import_device(dev, mddev->major_version,
5490 mddev->minor_version);
5491 else
5492 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 if (IS_ERR(rdev)) {
5494 printk(KERN_WARNING
5495 "md: md_import_device returned %ld\n",
5496 PTR_ERR(rdev));
5497 return PTR_ERR(rdev);
5498 }
NeilBrown1a855a02010-12-09 16:36:28 +11005499 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005500 if (!mddev->persistent) {
5501 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005502 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005503 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005504 set_bit(In_sync, &rdev->flags);
5505 } else
NeilBrown41158c72005-06-21 17:17:25 -07005506 rdev->raid_disk = -1;
5507 } else
5508 super_types[mddev->major_version].
5509 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005510 if ((info->state & (1<<MD_DISK_SYNC)) &&
5511 (!test_bit(In_sync, &rdev->flags) ||
5512 rdev->raid_disk != info->raid_disk)) {
5513 /* This was a hot-add request, but events doesn't
5514 * match, so reject it.
5515 */
5516 export_rdev(rdev);
5517 return -EINVAL;
5518 }
5519
NeilBrown1a855a02010-12-09 16:36:28 +11005520 if (test_bit(In_sync, &rdev->flags))
5521 rdev->saved_raid_disk = rdev->raid_disk;
5522 else
5523 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005524
NeilBrownb2d444d2005-11-08 21:39:31 -08005525 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005526 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5527 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005528 else
5529 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 rdev->raid_disk = -1;
5532 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005533 if (!err && !mddev->pers->hot_remove_disk) {
5534 /* If there is hot_add_disk but no hot_remove_disk
5535 * then added disks for geometry changes,
5536 * and should be added immediately.
5537 */
5538 super_types[mddev->major_version].
5539 validate_super(mddev, rdev);
5540 err = mddev->pers->hot_add_disk(mddev, rdev);
5541 if (err)
5542 unbind_rdev_from_array(rdev);
5543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 if (err)
5545 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005546 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005547 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005548
NeilBrown17571282006-12-10 02:20:52 -08005549 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005550 if (mddev->degraded)
5551 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005552 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005553 if (!err)
5554 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005555 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 return err;
5557 }
5558
5559 /* otherwise, add_new_disk is only allowed
5560 * for major_version==0 superblocks
5561 */
5562 if (mddev->major_version != 0) {
5563 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5564 mdname(mddev));
5565 return -EINVAL;
5566 }
5567
5568 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5569 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005570 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 if (IS_ERR(rdev)) {
5572 printk(KERN_WARNING
5573 "md: error, md_import_device() returned %ld\n",
5574 PTR_ERR(rdev));
5575 return PTR_ERR(rdev);
5576 }
5577 rdev->desc_nr = info->number;
5578 if (info->raid_disk < mddev->raid_disks)
5579 rdev->raid_disk = info->raid_disk;
5580 else
5581 rdev->raid_disk = -1;
5582
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005584 if (info->state & (1<<MD_DISK_SYNC))
5585 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005587 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5588 set_bit(WriteMostly, &rdev->flags);
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 if (!mddev->persistent) {
5591 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005592 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5593 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005594 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005595 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
NeilBrown2bf071b2006-01-06 00:20:55 -08005597 err = bind_rdev_to_array(rdev, mddev);
5598 if (err) {
5599 export_rdev(rdev);
5600 return err;
5601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603
5604 return 0;
5605}
5606
NeilBrownfd01b882011-10-11 16:47:53 +11005607static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
5609 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005610 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 rdev = find_rdev(mddev, dev);
5613 if (!rdev)
5614 return -ENXIO;
5615
5616 if (rdev->raid_disk >= 0)
5617 goto busy;
5618
5619 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005620 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005621 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 return 0;
5624busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005625 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 bdevname(rdev->bdev,b), mdname(mddev));
5627 return -EBUSY;
5628}
5629
NeilBrownfd01b882011-10-11 16:47:53 +11005630static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
5632 char b[BDEVNAME_SIZE];
5633 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005634 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
5636 if (!mddev->pers)
5637 return -ENODEV;
5638
5639 if (mddev->major_version != 0) {
5640 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5641 " version-0 superblocks.\n",
5642 mdname(mddev));
5643 return -EINVAL;
5644 }
5645 if (!mddev->pers->hot_add_disk) {
5646 printk(KERN_WARNING
5647 "%s: personality does not support diskops!\n",
5648 mdname(mddev));
5649 return -EINVAL;
5650 }
5651
NeilBrownd710e132008-10-13 11:55:12 +11005652 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (IS_ERR(rdev)) {
5654 printk(KERN_WARNING
5655 "md: error, md_import_device() returned %ld\n",
5656 PTR_ERR(rdev));
5657 return -EINVAL;
5658 }
5659
5660 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005661 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005663 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
NeilBrown8190e752009-06-18 08:48:58 +10005665 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
NeilBrownb2d444d2005-11-08 21:39:31 -08005667 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 printk(KERN_WARNING
5669 "md: can not hot-add faulty %s disk to %s!\n",
5670 bdevname(rdev->bdev,b), mdname(mddev));
5671 err = -EINVAL;
5672 goto abort_export;
5673 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005674 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005676 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005677 err = bind_rdev_to_array(rdev, mddev);
5678 if (err)
5679 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
5681 /*
5682 * The rest should better be atomic, we can have disk failures
5683 * noticed in interrupt contexts ...
5684 */
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 rdev->raid_disk = -1;
5687
NeilBrown850b2b422006-10-03 01:15:46 -07005688 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 /*
5691 * Kick recovery, maybe this spare has to be added to the
5692 * array immediately.
5693 */
5694 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5695 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005696 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 return 0;
5698
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699abort_export:
5700 export_rdev(rdev);
5701 return err;
5702}
5703
NeilBrownfd01b882011-10-11 16:47:53 +11005704static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005705{
5706 int err;
5707
NeilBrown36fa3062005-09-09 16:23:45 -07005708 if (mddev->pers) {
5709 if (!mddev->pers->quiesce)
5710 return -EBUSY;
5711 if (mddev->recovery || mddev->sync_thread)
5712 return -EBUSY;
5713 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005714 }
5715
NeilBrown36fa3062005-09-09 16:23:45 -07005716
5717 if (fd >= 0) {
5718 if (mddev->bitmap)
5719 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005720 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005721
NeilBrownc3d97142009-12-14 12:49:52 +11005722 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005723 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5724 mdname(mddev));
5725 return -EBADF;
5726 }
5727
NeilBrownc3d97142009-12-14 12:49:52 +11005728 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005729 if (err) {
5730 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5731 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005732 fput(mddev->bitmap_info.file);
5733 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005734 return err;
5735 }
NeilBrownc3d97142009-12-14 12:49:52 +11005736 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005737 } else if (mddev->bitmap == NULL)
5738 return -ENOENT; /* cannot remove what isn't there */
5739 err = 0;
5740 if (mddev->pers) {
5741 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005742 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005743 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005744 if (!err)
5745 err = bitmap_load(mddev);
5746 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005747 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005748 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005749 fd = -1; /* make sure to put the file */
5750 }
NeilBrown36fa3062005-09-09 16:23:45 -07005751 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005752 }
5753 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005754 if (mddev->bitmap_info.file) {
5755 restore_bitmap_write_access(mddev->bitmap_info.file);
5756 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005757 }
NeilBrownc3d97142009-12-14 12:49:52 +11005758 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005759 }
5760
NeilBrown32a76272005-06-21 17:17:14 -07005761 return err;
5762}
5763
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764/*
5765 * set_array_info is used two different ways
5766 * The original usage is when creating a new array.
5767 * In this usage, raid_disks is > 0 and it together with
5768 * level, size, not_persistent,layout,chunksize determine the
5769 * shape of the array.
5770 * This will always create an array with a type-0.90.0 superblock.
5771 * The newer usage is when assembling an array.
5772 * In this case raid_disks will be 0, and the major_version field is
5773 * use to determine which style super-blocks are to be found on the devices.
5774 * The minor and patch _version numbers are also kept incase the
5775 * super_block handler wishes to interpret them.
5776 */
NeilBrownfd01b882011-10-11 16:47:53 +11005777static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
5779
5780 if (info->raid_disks == 0) {
5781 /* just setting version number for superblock loading */
5782 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005783 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 super_types[info->major_version].name == NULL) {
5785 /* maybe try to auto-load a module? */
5786 printk(KERN_INFO
5787 "md: superblock version %d not known\n",
5788 info->major_version);
5789 return -EINVAL;
5790 }
5791 mddev->major_version = info->major_version;
5792 mddev->minor_version = info->minor_version;
5793 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005794 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005795 /* ensure mddev_put doesn't delete this now that there
5796 * is some minimal configuration.
5797 */
5798 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 return 0;
5800 }
5801 mddev->major_version = MD_MAJOR_VERSION;
5802 mddev->minor_version = MD_MINOR_VERSION;
5803 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5804 mddev->ctime = get_seconds();
5805
5806 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005807 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005808 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 mddev->raid_disks = info->raid_disks;
5810 /* don't set md_minor, it is determined by which /dev/md* was
5811 * openned
5812 */
5813 if (info->state & (1<<MD_SB_CLEAN))
5814 mddev->recovery_cp = MaxSector;
5815 else
5816 mddev->recovery_cp = 0;
5817 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005818 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005821 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 mddev->max_disks = MD_SB_DISKS;
5824
NeilBrowne6910632008-02-06 01:39:51 -08005825 if (mddev->persistent)
5826 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005827 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
NeilBrownc3d97142009-12-14 12:49:52 +11005829 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5830 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005831
NeilBrownf6705572006-03-27 01:18:11 -08005832 mddev->reshape_position = MaxSector;
5833
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 /*
5835 * Generate a 128 bit UUID
5836 */
5837 get_random_bytes(mddev->uuid, 16);
5838
NeilBrownf6705572006-03-27 01:18:11 -08005839 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005840 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005841 mddev->new_layout = mddev->layout;
5842 mddev->delta_disks = 0;
5843
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 return 0;
5845}
5846
NeilBrownfd01b882011-10-11 16:47:53 +11005847void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005848{
Dan Williamsb522adc2009-03-31 15:00:31 +11005849 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5850
5851 if (mddev->external_size)
5852 return;
5853
Dan Williams1f403622009-03-31 14:59:03 +11005854 mddev->array_sectors = array_sectors;
5855}
5856EXPORT_SYMBOL(md_set_array_sectors);
5857
NeilBrownfd01b882011-10-11 16:47:53 +11005858static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005859{
NeilBrown3cb03002011-10-11 16:45:26 +11005860 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005861 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005862 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005863
5864 if (mddev->pers->resize == NULL)
5865 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005866 /* The "num_sectors" is the number of sectors of each device that
5867 * is used. This can only make sense for arrays with redundancy.
5868 * linear and raid0 always use whatever space is available. We can only
5869 * consider changing this number if no resync or reconstruction is
5870 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005871 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005872 * of each device. If num_sectors is zero, we find the largest size
5873 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005874 */
5875 if (mddev->sync_thread)
5876 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005877 if (mddev->bitmap)
5878 /* Sorry, cannot grow a bitmap yet, just remove it,
5879 * grow, and re-add.
5880 */
5881 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005882 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005883 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005884
Andre Nolld71f9f82008-07-11 22:02:22 +10005885 if (fit && (num_sectors == 0 || num_sectors > avail))
5886 num_sectors = avail;
5887 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005888 return -ENOSPC;
5889 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005890 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005891 if (!rv)
5892 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005893 return rv;
5894}
5895
NeilBrownfd01b882011-10-11 16:47:53 +11005896static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08005897{
5898 int rv;
5899 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005900 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005901 return -EINVAL;
5902 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005903 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005904 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005905 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005906 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005907 mddev->delta_disks = raid_disks - mddev->raid_disks;
5908
5909 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005910 if (rv < 0)
5911 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005912 return rv;
5913}
5914
5915
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916/*
5917 * update_array_info is used to change the configuration of an
5918 * on-line array.
5919 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5920 * fields in the info are checked against the array.
5921 * Any differences that cannot be handled will cause an error.
5922 * Normally, only one change can be managed at a time.
5923 */
NeilBrownfd01b882011-10-11 16:47:53 +11005924static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925{
5926 int rv = 0;
5927 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005928 int state = 0;
5929
5930 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005931 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005932 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 if (mddev->major_version != info->major_version ||
5935 mddev->minor_version != info->minor_version ||
5936/* mddev->patch_version != info->patch_version || */
5937 mddev->ctime != info->ctime ||
5938 mddev->level != info->level ||
5939/* mddev->layout != info->layout || */
5940 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005941 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005942 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5943 ((state^info->state) & 0xfffffe00)
5944 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return -EINVAL;
5946 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005947 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5948 cnt++;
5949 if (mddev->raid_disks != info->raid_disks)
5950 cnt++;
5951 if (mddev->layout != info->layout)
5952 cnt++;
5953 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5954 cnt++;
5955 if (cnt == 0)
5956 return 0;
5957 if (cnt > 1)
5958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
5960 if (mddev->layout != info->layout) {
5961 /* Change layout
5962 * we don't need to do anything at the md level, the
5963 * personality will take care of it all.
5964 */
NeilBrown50ac1682009-06-18 08:47:55 +10005965 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005967 else {
5968 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005969 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005970 if (rv)
5971 mddev->new_layout = mddev->layout;
5972 return rv;
5973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005975 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005976 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
NeilBrownda943b992006-01-06 00:20:54 -08005978 if (mddev->raid_disks != info->raid_disks)
5979 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
NeilBrown36fa3062005-09-09 16:23:45 -07005981 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5982 if (mddev->pers->quiesce == NULL)
5983 return -EINVAL;
5984 if (mddev->recovery || mddev->sync_thread)
5985 return -EBUSY;
5986 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5987 /* add the bitmap */
5988 if (mddev->bitmap)
5989 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005990 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005991 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005992 mddev->bitmap_info.offset =
5993 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005994 mddev->pers->quiesce(mddev, 1);
5995 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005996 if (!rv)
5997 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005998 if (rv)
5999 bitmap_destroy(mddev);
6000 mddev->pers->quiesce(mddev, 0);
6001 } else {
6002 /* remove the bitmap */
6003 if (!mddev->bitmap)
6004 return -ENOENT;
6005 if (mddev->bitmap->file)
6006 return -EINVAL;
6007 mddev->pers->quiesce(mddev, 1);
6008 bitmap_destroy(mddev);
6009 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006010 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006011 }
6012 }
NeilBrown850b2b422006-10-03 01:15:46 -07006013 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return rv;
6015}
6016
NeilBrownfd01b882011-10-11 16:47:53 +11006017static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
NeilBrown3cb03002011-10-11 16:45:26 +11006019 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 if (mddev->pers == NULL)
6022 return -ENODEV;
6023
6024 rdev = find_rdev(mddev, dev);
6025 if (!rdev)
6026 return -ENODEV;
6027
6028 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006029 if (!test_bit(Faulty, &rdev->flags))
6030 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 return 0;
6032}
6033
Andre Noll2f9618c2008-04-25 18:57:58 +02006034/*
6035 * We have a problem here : there is no easy way to give a CHS
6036 * virtual geometry. We currently pretend that we have a 2 heads
6037 * 4 sectors (with a BIG number of cylinders...). This drives
6038 * dosfs just mad... ;-)
6039 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006040static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6041{
NeilBrownfd01b882011-10-11 16:47:53 +11006042 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006043
6044 geo->heads = 2;
6045 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006046 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006047 return 0;
6048}
6049
Al Viroa39907f2008-03-02 10:31:15 -05006050static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 unsigned int cmd, unsigned long arg)
6052{
6053 int err = 0;
6054 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006055 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006056 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
6058 if (!capable(CAP_SYS_ADMIN))
6059 return -EACCES;
6060
6061 /*
6062 * Commands dealing with the RAID driver but not any
6063 * particular array:
6064 */
6065 switch (cmd)
6066 {
6067 case RAID_VERSION:
6068 err = get_version(argp);
6069 goto done;
6070
6071 case PRINT_RAID_DEBUG:
6072 err = 0;
6073 md_print_devices();
6074 goto done;
6075
6076#ifndef MODULE
6077 case RAID_AUTORUN:
6078 err = 0;
6079 autostart_arrays(arg);
6080 goto done;
6081#endif
6082 default:;
6083 }
6084
6085 /*
6086 * Commands creating/starting a new array:
6087 */
6088
Al Viroa39907f2008-03-02 10:31:15 -05006089 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
6091 if (!mddev) {
6092 BUG();
6093 goto abort;
6094 }
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 err = mddev_lock(mddev);
6097 if (err) {
6098 printk(KERN_INFO
6099 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6100 err, cmd);
6101 goto abort;
6102 }
6103
6104 switch (cmd)
6105 {
6106 case SET_ARRAY_INFO:
6107 {
6108 mdu_array_info_t info;
6109 if (!arg)
6110 memset(&info, 0, sizeof(info));
6111 else if (copy_from_user(&info, argp, sizeof(info))) {
6112 err = -EFAULT;
6113 goto abort_unlock;
6114 }
6115 if (mddev->pers) {
6116 err = update_array_info(mddev, &info);
6117 if (err) {
6118 printk(KERN_WARNING "md: couldn't update"
6119 " array info. %d\n", err);
6120 goto abort_unlock;
6121 }
6122 goto done_unlock;
6123 }
6124 if (!list_empty(&mddev->disks)) {
6125 printk(KERN_WARNING
6126 "md: array %s already has disks!\n",
6127 mdname(mddev));
6128 err = -EBUSY;
6129 goto abort_unlock;
6130 }
6131 if (mddev->raid_disks) {
6132 printk(KERN_WARNING
6133 "md: array %s already initialised!\n",
6134 mdname(mddev));
6135 err = -EBUSY;
6136 goto abort_unlock;
6137 }
6138 err = set_array_info(mddev, &info);
6139 if (err) {
6140 printk(KERN_WARNING "md: couldn't set"
6141 " array info. %d\n", err);
6142 goto abort_unlock;
6143 }
6144 }
6145 goto done_unlock;
6146
6147 default:;
6148 }
6149
6150 /*
6151 * Commands querying/configuring an existing array:
6152 */
NeilBrown32a76272005-06-21 17:17:14 -07006153 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006154 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006155 if ((!mddev->raid_disks && !mddev->external)
6156 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6157 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6158 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 err = -ENODEV;
6160 goto abort_unlock;
6161 }
6162
6163 /*
6164 * Commands even a read-only array can execute:
6165 */
6166 switch (cmd)
6167 {
6168 case GET_ARRAY_INFO:
6169 err = get_array_info(mddev, argp);
6170 goto done_unlock;
6171
NeilBrown32a76272005-06-21 17:17:14 -07006172 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006173 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006174 goto done_unlock;
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 case GET_DISK_INFO:
6177 err = get_disk_info(mddev, argp);
6178 goto done_unlock;
6179
6180 case RESTART_ARRAY_RW:
6181 err = restart_array(mddev);
6182 goto done_unlock;
6183
6184 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006185 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 goto done_unlock;
6187
6188 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006189 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 goto done_unlock;
6191
Dan Williamse2218352010-05-12 08:25:37 +10006192 case BLKROSET:
6193 if (get_user(ro, (int __user *)(arg))) {
6194 err = -EFAULT;
6195 goto done_unlock;
6196 }
6197 err = -EINVAL;
6198
6199 /* if the bdev is going readonly the value of mddev->ro
6200 * does not matter, no writes are coming
6201 */
6202 if (ro)
6203 goto done_unlock;
6204
6205 /* are we are already prepared for writes? */
6206 if (mddev->ro != 1)
6207 goto done_unlock;
6208
6209 /* transitioning to readauto need only happen for
6210 * arrays that call md_write_start
6211 */
6212 if (mddev->pers) {
6213 err = restart_array(mddev);
6214 if (err == 0) {
6215 mddev->ro = 2;
6216 set_disk_ro(mddev->gendisk, 0);
6217 }
6218 }
6219 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 }
6221
6222 /*
6223 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006224 * superblock, so we do not allow them on read-only arrays.
6225 * However non-MD ioctls (e.g. get-size) will still come through
6226 * here and hit the 'default' below, so only disallow
6227 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006229 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006230 if (mddev->ro == 2) {
6231 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006232 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006233 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6234 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006235 } else {
6236 err = -EROFS;
6237 goto abort_unlock;
6238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 }
6240
6241 switch (cmd)
6242 {
6243 case ADD_NEW_DISK:
6244 {
6245 mdu_disk_info_t info;
6246 if (copy_from_user(&info, argp, sizeof(info)))
6247 err = -EFAULT;
6248 else
6249 err = add_new_disk(mddev, &info);
6250 goto done_unlock;
6251 }
6252
6253 case HOT_REMOVE_DISK:
6254 err = hot_remove_disk(mddev, new_decode_dev(arg));
6255 goto done_unlock;
6256
6257 case HOT_ADD_DISK:
6258 err = hot_add_disk(mddev, new_decode_dev(arg));
6259 goto done_unlock;
6260
6261 case SET_DISK_FAULTY:
6262 err = set_disk_faulty(mddev, new_decode_dev(arg));
6263 goto done_unlock;
6264
6265 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006266 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 goto done_unlock;
6268
NeilBrown32a76272005-06-21 17:17:14 -07006269 case SET_BITMAP_FILE:
6270 err = set_bitmap_file(mddev, (int)arg);
6271 goto done_unlock;
6272
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 err = -EINVAL;
6275 goto abort_unlock;
6276 }
6277
6278done_unlock:
6279abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006280 if (mddev->hold_active == UNTIL_IOCTL &&
6281 err != -EINVAL)
6282 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 mddev_unlock(mddev);
6284
6285 return err;
6286done:
6287 if (err)
6288 MD_BUG();
6289abort:
6290 return err;
6291}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006292#ifdef CONFIG_COMPAT
6293static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6294 unsigned int cmd, unsigned long arg)
6295{
6296 switch (cmd) {
6297 case HOT_REMOVE_DISK:
6298 case HOT_ADD_DISK:
6299 case SET_DISK_FAULTY:
6300 case SET_BITMAP_FILE:
6301 /* These take in integer arg, do not convert */
6302 break;
6303 default:
6304 arg = (unsigned long)compat_ptr(arg);
6305 break;
6306 }
6307
6308 return md_ioctl(bdev, mode, cmd, arg);
6309}
6310#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
Al Viroa39907f2008-03-02 10:31:15 -05006312static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313{
6314 /*
6315 * Succeed if we can lock the mddev, which confirms that
6316 * it isn't being stopped right now.
6317 */
NeilBrownfd01b882011-10-11 16:47:53 +11006318 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 int err;
6320
NeilBrownd3374822009-01-09 08:31:10 +11006321 if (mddev->gendisk != bdev->bd_disk) {
6322 /* we are racing with mddev_put which is discarding this
6323 * bd_disk.
6324 */
6325 mddev_put(mddev);
6326 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006327 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006328 /* Then retry the open from the top */
6329 return -ERESTARTSYS;
6330 }
6331 BUG_ON(mddev != bdev->bd_disk->private_data);
6332
NeilBrownc8c00a62009-08-10 12:50:52 +10006333 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 goto out;
6335
6336 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006337 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006338 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006340 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 out:
6342 return err;
6343}
6344
Al Viroa39907f2008-03-02 10:31:15 -05006345static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346{
NeilBrownfd01b882011-10-11 16:47:53 +11006347 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006349 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006350 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 mddev_put(mddev);
6352
6353 return 0;
6354}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006355
6356static int md_media_changed(struct gendisk *disk)
6357{
NeilBrownfd01b882011-10-11 16:47:53 +11006358 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006359
6360 return mddev->changed;
6361}
6362
6363static int md_revalidate(struct gendisk *disk)
6364{
NeilBrownfd01b882011-10-11 16:47:53 +11006365 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006366
6367 mddev->changed = 0;
6368 return 0;
6369}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006370static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371{
6372 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006373 .open = md_open,
6374 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006375 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006376#ifdef CONFIG_COMPAT
6377 .compat_ioctl = md_compat_ioctl,
6378#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006379 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006380 .media_changed = md_media_changed,
6381 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382};
6383
Adrian Bunk75c96f82005-05-05 16:16:09 -07006384static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385{
NeilBrown2b8bf342011-10-11 16:48:23 +11006386 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 /*
6389 * md_thread is a 'system-thread', it's priority should be very
6390 * high. We avoid resource deadlocks individually in each
6391 * raid personality. (RAID5 does preallocation) We also use RR and
6392 * the very same RT priority as kswapd, thus we will never get
6393 * into a priority inversion deadlock.
6394 *
6395 * we definitely have to have equal or higher priority than
6396 * bdflush, otherwise bdflush will deadlock if there are too
6397 * many dirty RAID5 blocks.
6398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
NeilBrown6985c432005-10-19 21:23:47 -07006400 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006401 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
NeilBrown93588e22005-11-15 00:09:12 -08006403 /* We need to wait INTERRUPTIBLE so that
6404 * we don't add to the load-average.
6405 * That means we need to be sure no signals are
6406 * pending
6407 */
6408 if (signal_pending(current))
6409 flush_signals(current);
6410
6411 wait_event_interruptible_timeout
6412 (thread->wqueue,
6413 test_bit(THREAD_WAKEUP, &thread->flags)
6414 || kthread_should_stop(),
6415 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
NeilBrown6c987912011-01-14 09:13:53 +11006417 clear_bit(THREAD_WAKEUP, &thread->flags);
6418 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006419 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006421
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 return 0;
6423}
6424
NeilBrown2b8bf342011-10-11 16:48:23 +11006425void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426{
6427 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006428 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 set_bit(THREAD_WAKEUP, &thread->flags);
6430 wake_up(&thread->wqueue);
6431 }
6432}
6433
NeilBrown2b8bf342011-10-11 16:48:23 +11006434struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 const char *name)
6436{
NeilBrown2b8bf342011-10-11 16:48:23 +11006437 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
NeilBrown2b8bf342011-10-11 16:48:23 +11006439 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 if (!thread)
6441 return NULL;
6442
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 init_waitqueue_head(&thread->wqueue);
6444
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 thread->run = run;
6446 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006447 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006448 thread->tsk = kthread_run(md_thread, thread,
6449 "%s_%s",
6450 mdname(thread->mddev),
6451 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006452 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 kfree(thread);
6454 return NULL;
6455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 return thread;
6457}
6458
NeilBrown2b8bf342011-10-11 16:48:23 +11006459void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460{
NeilBrown2b8bf342011-10-11 16:48:23 +11006461 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006462 if (!thread)
6463 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006464 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006465 /* Locking ensures that mddev_unlock does not wake_up a
6466 * non-existent thread
6467 */
6468 spin_lock(&pers_lock);
6469 *threadp = NULL;
6470 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006471
6472 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 kfree(thread);
6474}
6475
NeilBrownfd01b882011-10-11 16:47:53 +11006476void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477{
6478 if (!mddev) {
6479 MD_BUG();
6480 return;
6481 }
6482
NeilBrownb2d444d2005-11-08 21:39:31 -08006483 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006485
NeilBrownde393cd2011-07-28 11:31:48 +10006486 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 return;
6488 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006489 if (mddev->degraded)
6490 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006491 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6493 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6494 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006495 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006496 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006497 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498}
6499
6500/* seq_file implementation /proc/mdstat */
6501
6502static void status_unused(struct seq_file *seq)
6503{
6504 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006505 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
6507 seq_printf(seq, "unused devices: ");
6508
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006509 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 char b[BDEVNAME_SIZE];
6511 i++;
6512 seq_printf(seq, "%s ",
6513 bdevname(rdev->bdev,b));
6514 }
6515 if (!i)
6516 seq_printf(seq, "<none>");
6517
6518 seq_printf(seq, "\n");
6519}
6520
6521
NeilBrownfd01b882011-10-11 16:47:53 +11006522static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523{
NeilBrowndd71cf62009-05-07 12:49:35 +10006524 sector_t max_sectors, resync, res;
6525 unsigned long dt, db;
6526 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006527 int scale;
6528 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
NeilBrowndd71cf62009-05-07 12:49:35 +10006530 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
6532 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006533 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006535 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
6537 /*
6538 * Should not happen.
6539 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006540 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 MD_BUG();
6542 return;
6543 }
NeilBrown4588b422006-03-27 01:18:04 -08006544 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006545 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006546 * u32, as those are the requirements for sector_div.
6547 * Thus 'scale' must be at least 10
6548 */
6549 scale = 10;
6550 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006551 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006552 scale++;
6553 }
6554 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006555 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006556
6557 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 {
NeilBrown4588b422006-03-27 01:18:04 -08006559 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 seq_printf(seq, "[");
6561 for (i = 0; i < x; i++)
6562 seq_printf(seq, "=");
6563 seq_printf(seq, ">");
6564 for (i = 0; i < y; i++)
6565 seq_printf(seq, ".");
6566 seq_printf(seq, "] ");
6567 }
NeilBrown4588b422006-03-27 01:18:04 -08006568 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006569 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6570 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006571 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6572 "check" :
6573 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6574 "resync" : "recovery"))),
6575 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006576 (unsigned long long) resync/2,
6577 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578
6579 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 * dt: time from mark until now
6581 * db: blocks written from mark until now
6582 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006583 *
6584 * rt is a sector_t, so could be 32bit or 64bit.
6585 * So we divide before multiply in case it is 32bit and close
6586 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006587 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006588 * near the end of resync when the number of remaining sectors
6589 * is close to 'db'.
6590 * We then divide rt by 32 after multiplying by db to compensate.
6591 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 */
6593 dt = ((jiffies - mddev->resync_mark) / HZ);
6594 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006595 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6596 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
NeilBrowndd71cf62009-05-07 12:49:35 +10006598 rt = max_sectors - resync; /* number of remaining sectors */
6599 sector_div(rt, db/32+1);
6600 rt *= dt;
6601 rt >>= 5;
6602
6603 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6604 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
NeilBrownff4e8d92006-07-10 04:44:16 -07006606 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607}
6608
6609static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6610{
6611 struct list_head *tmp;
6612 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006613 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
6615 if (l >= 0x10000)
6616 return NULL;
6617 if (!l--)
6618 /* header */
6619 return (void*)1;
6620
6621 spin_lock(&all_mddevs_lock);
6622 list_for_each(tmp,&all_mddevs)
6623 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006624 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 mddev_get(mddev);
6626 spin_unlock(&all_mddevs_lock);
6627 return mddev;
6628 }
6629 spin_unlock(&all_mddevs_lock);
6630 if (!l--)
6631 return (void*)2;/* tail */
6632 return NULL;
6633}
6634
6635static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6636{
6637 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006638 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
6640 ++*pos;
6641 if (v == (void*)2)
6642 return NULL;
6643
6644 spin_lock(&all_mddevs_lock);
6645 if (v == (void*)1)
6646 tmp = all_mddevs.next;
6647 else
6648 tmp = mddev->all_mddevs.next;
6649 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006650 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 else {
6652 next_mddev = (void*)2;
6653 *pos = 0x10000;
6654 }
6655 spin_unlock(&all_mddevs_lock);
6656
6657 if (v != (void*)1)
6658 mddev_put(mddev);
6659 return next_mddev;
6660
6661}
6662
6663static void md_seq_stop(struct seq_file *seq, void *v)
6664{
NeilBrownfd01b882011-10-11 16:47:53 +11006665 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
6667 if (mddev && v != (void*)1 && v != (void*)2)
6668 mddev_put(mddev);
6669}
6670
6671static int md_seq_show(struct seq_file *seq, void *v)
6672{
NeilBrownfd01b882011-10-11 16:47:53 +11006673 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006674 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006675 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006676 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
6678 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006679 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 seq_printf(seq, "Personalities : ");
6681 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006682 list_for_each_entry(pers, &pers_list, list)
6683 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
6685 spin_unlock(&pers_lock);
6686 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006687 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 return 0;
6689 }
6690 if (v == (void*)2) {
6691 status_unused(seq);
6692 return 0;
6693 }
6694
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006695 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006697
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6699 seq_printf(seq, "%s : %sactive", mdname(mddev),
6700 mddev->pers ? "" : "in");
6701 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006702 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006704 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006705 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 seq_printf(seq, " %s", mddev->pers->name);
6707 }
6708
Andre Nolldd8ac332009-03-31 14:33:13 +11006709 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006710 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 char b[BDEVNAME_SIZE];
6712 seq_printf(seq, " %s[%d]",
6713 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006714 if (test_bit(WriteMostly, &rdev->flags))
6715 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006716 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 seq_printf(seq, "(F)");
6718 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006719 } else if (rdev->raid_disk < 0)
6720 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006721 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 }
6723
6724 if (!list_empty(&mddev->disks)) {
6725 if (mddev->pers)
6726 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006727 (unsigned long long)
6728 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 else
6730 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006731 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006733 if (mddev->persistent) {
6734 if (mddev->major_version != 0 ||
6735 mddev->minor_version != 90) {
6736 seq_printf(seq," super %d.%d",
6737 mddev->major_version,
6738 mddev->minor_version);
6739 }
NeilBrowne6910632008-02-06 01:39:51 -08006740 } else if (mddev->external)
6741 seq_printf(seq, " super external:%s",
6742 mddev->metadata_type);
6743 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006744 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
6746 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006747 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006749 if (mddev->pers->sync_request) {
6750 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006751 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006752 seq_printf(seq, "\n ");
6753 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6754 seq_printf(seq, "\tresync=DELAYED\n ");
6755 else if (mddev->recovery_cp < MaxSector)
6756 seq_printf(seq, "\tresync=PENDING\n ");
6757 }
NeilBrown32a76272005-06-21 17:17:14 -07006758 } else
6759 seq_printf(seq, "\n ");
6760
6761 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006762 unsigned long chunk_kb;
6763 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006764 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006765 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006766 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6767 "%lu%s chunk",
6768 bitmap->pages - bitmap->missing_pages,
6769 bitmap->pages,
6770 (bitmap->pages - bitmap->missing_pages)
6771 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006772 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006773 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006774 if (bitmap->file) {
6775 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006776 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006777 }
NeilBrown78d742d2005-06-21 17:17:15 -07006778
NeilBrown32a76272005-06-21 17:17:14 -07006779 seq_printf(seq, "\n");
6780 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 }
6782
6783 seq_printf(seq, "\n");
6784 }
6785 mddev_unlock(mddev);
6786
6787 return 0;
6788}
6789
Jan Engelhardt110518b2009-05-07 12:49:37 +10006790static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 .start = md_seq_start,
6792 .next = md_seq_next,
6793 .stop = md_seq_stop,
6794 .show = md_seq_show,
6795};
6796
6797static int md_seq_open(struct inode *inode, struct file *file)
6798{
Kay Sieversf1514632011-07-12 20:48:39 +02006799 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 int error;
6801
6802 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006803 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006804 return error;
6805
6806 seq = file->private_data;
6807 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 return error;
6809}
6810
NeilBrownd7603b72006-01-06 00:20:30 -08006811static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6812{
Kay Sieversf1514632011-07-12 20:48:39 +02006813 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006814 int mask;
6815
6816 poll_wait(filp, &md_event_waiters, wait);
6817
6818 /* always allow read */
6819 mask = POLLIN | POLLRDNORM;
6820
Kay Sieversf1514632011-07-12 20:48:39 +02006821 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006822 mask |= POLLERR | POLLPRI;
6823 return mask;
6824}
6825
Arjan van de Venfa027c22007-02-12 00:55:33 -08006826static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006827 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 .open = md_seq_open,
6829 .read = seq_read,
6830 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006831 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006832 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833};
6834
NeilBrown84fc4b52011-10-11 16:49:58 +11006835int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006838 list_add_tail(&p->list, &pers_list);
6839 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 spin_unlock(&pers_lock);
6841 return 0;
6842}
6843
NeilBrown84fc4b52011-10-11 16:49:58 +11006844int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845{
NeilBrown2604b702006-01-06 00:20:36 -08006846 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006848 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 spin_unlock(&pers_lock);
6850 return 0;
6851}
6852
NeilBrownfd01b882011-10-11 16:47:53 +11006853static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854{
NeilBrown3cb03002011-10-11 16:45:26 +11006855 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006857 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
6859 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006860 rcu_read_lock();
6861 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006863 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6864 (int)part_stat_read(&disk->part0, sectors[1]) -
6865 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006866 /* sync IO will cause sync_io to increase before the disk_stats
6867 * as sync_io is counted when a request starts, and
6868 * disk_stats is counted when it completes.
6869 * So resync activity will cause curr_events to be smaller than
6870 * when there was no such activity.
6871 * non-sync IO will cause disk_stat to increase without
6872 * increasing sync_io so curr_events will (eventually)
6873 * be larger than it was before. Once it becomes
6874 * substantially larger, the test below will cause
6875 * the array to appear non-idle, and resync will slow
6876 * down.
6877 * If there is a lot of outstanding resync activity when
6878 * we set last_event to curr_events, then all that activity
6879 * completing might cause the array to appear non-idle
6880 * and resync will be slowed down even though there might
6881 * not have been non-resync activity. This will only
6882 * happen once though. 'last_events' will soon reflect
6883 * the state where there is little or no outstanding
6884 * resync requests, and further resync activity will
6885 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006886 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006888 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 rdev->last_events = curr_events;
6890 idle = 0;
6891 }
6892 }
NeilBrown4b809912008-07-21 17:05:25 +10006893 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 return idle;
6895}
6896
NeilBrownfd01b882011-10-11 16:47:53 +11006897void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898{
6899 /* another "blocks" (512byte) blocks have been synced */
6900 atomic_sub(blocks, &mddev->recovery_active);
6901 wake_up(&mddev->recovery_wait);
6902 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006903 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 md_wakeup_thread(mddev->thread);
6905 // stop recovery, signal do_sync ....
6906 }
6907}
6908
6909
NeilBrown06d91a52005-06-21 17:17:12 -07006910/* md_write_start(mddev, bi)
6911 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006912 * in superblock) before writing, schedule a superblock update
6913 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006914 */
NeilBrownfd01b882011-10-11 16:47:53 +11006915void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916{
Neil Brown0fd62b82008-06-28 08:31:36 +10006917 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006918 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006919 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006920
NeilBrownf91de922005-11-08 21:39:36 -08006921 BUG_ON(mddev->ro == 1);
6922 if (mddev->ro == 2) {
6923 /* need to switch to read/write */
6924 mddev->ro = 0;
6925 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6926 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006927 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006928 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006929 }
NeilBrown06d91a52005-06-21 17:17:12 -07006930 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006931 if (mddev->safemode == 1)
6932 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006933 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006934 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006935 if (mddev->in_sync) {
6936 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006937 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006938 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006939 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006940 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006941 }
NeilBrowna9701a32005-11-08 21:39:34 -08006942 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006943 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006944 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006945 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006946 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006947 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948}
6949
NeilBrownfd01b882011-10-11 16:47:53 +11006950void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951{
6952 if (atomic_dec_and_test(&mddev->writes_pending)) {
6953 if (mddev->safemode == 2)
6954 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006955 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6957 }
6958}
6959
NeilBrown2a2275d2007-01-26 00:57:11 -08006960/* md_allow_write(mddev)
6961 * Calling this ensures that the array is marked 'active' so that writes
6962 * may proceed without blocking. It is important to call this before
6963 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6964 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006965 *
6966 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6967 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006968 */
NeilBrownfd01b882011-10-11 16:47:53 +11006969int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006970{
6971 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006972 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006973 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006974 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006975 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006976 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006977
6978 spin_lock_irq(&mddev->write_lock);
6979 if (mddev->in_sync) {
6980 mddev->in_sync = 0;
6981 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006982 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006983 if (mddev->safemode_delay &&
6984 mddev->safemode == 0)
6985 mddev->safemode = 1;
6986 spin_unlock_irq(&mddev->write_lock);
6987 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006988 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006989 } else
6990 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006991
NeilBrown070dc6d2010-08-30 17:33:34 +10006992 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006993 return -EAGAIN;
6994 else
6995 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006996}
6997EXPORT_SYMBOL_GPL(md_allow_write);
6998
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999#define SYNC_MARKS 10
7000#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007001void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002{
NeilBrownfd01b882011-10-11 16:47:53 +11007003 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 unsigned int currspeed = 0,
7005 window;
NeilBrown57afd892005-06-21 17:17:13 -07007006 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 unsigned long mark[SYNC_MARKS];
7008 sector_t mark_cnt[SYNC_MARKS];
7009 int last_mark,m;
7010 struct list_head *tmp;
7011 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007012 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007013 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007014 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
7016 /* just incase thread restarts... */
7017 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7018 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007019 if (mddev->ro) /* never try to sync a read-only array */
7020 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
NeilBrown61df9d92006-10-03 01:15:57 -07007022 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7023 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7024 desc = "data-check";
7025 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7026 desc = "requested-resync";
7027 else
7028 desc = "resync";
7029 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7030 desc = "reshape";
7031 else
7032 desc = "recovery";
7033
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 /* we overload curr_resync somewhat here.
7035 * 0 == not engaged in resync at all
7036 * 2 == checking that there is no conflict with another sync
7037 * 1 == like 2, but have yielded to allow conflicting resync to
7038 * commense
7039 * other == active in resync - this many blocks
7040 *
7041 * Before starting a resync we must have set curr_resync to
7042 * 2, and then checked that every "conflicting" array has curr_resync
7043 * less than ours. When we find one that is the same or higher
7044 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7045 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7046 * This will mean we have to start checking from the beginning again.
7047 *
7048 */
7049
7050 do {
7051 mddev->curr_resync = 2;
7052
7053 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007054 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007055 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007056
7057 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007059 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 if (mddev2 == mddev)
7061 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007062 if (!mddev->parallel_resync
7063 && mddev2->curr_resync
7064 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 DEFINE_WAIT(wq);
7066 if (mddev < mddev2 && mddev->curr_resync == 2) {
7067 /* arbitrarily yield */
7068 mddev->curr_resync = 1;
7069 wake_up(&resync_wait);
7070 }
7071 if (mddev > mddev2 && mddev->curr_resync == 1)
7072 /* no need to wait here, we can wait the next
7073 * time 'round when curr_resync == 2
7074 */
7075 continue;
NeilBrown97441972008-09-19 11:49:54 +10007076 /* We need to wait 'interruptible' so as not to
7077 * contribute to the load average, and not to
7078 * be caught by 'softlockup'
7079 */
7080 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007081 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007082 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007083 printk(KERN_INFO "md: delaying %s of %s"
7084 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007086 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007088 if (signal_pending(current))
7089 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 schedule();
7091 finish_wait(&resync_wait, &wq);
7092 goto try_again;
7093 }
7094 finish_wait(&resync_wait, &wq);
7095 }
7096 }
7097 } while (mddev->curr_resync < 2);
7098
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007099 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007100 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007102 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 */
7104 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007105 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007106 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007107 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7108 j = mddev->resync_min;
7109 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007110 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007111
NeilBrownccfcc3c2006-03-27 01:18:09 -08007112 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007113 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007114 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007116 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007117 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007118 rcu_read_lock();
7119 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007120 if (rdev->raid_disk >= 0 &&
7121 !test_bit(Faulty, &rdev->flags) &&
7122 !test_bit(In_sync, &rdev->flags) &&
7123 rdev->recovery_offset < j)
7124 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007125 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
NeilBrown61df9d92006-10-03 01:15:57 -07007128 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7129 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7130 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007131 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007132 "(but not more than %d KB/sec) for %s.\n",
7133 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
NeilBrowneea1bf32009-03-31 14:27:02 +11007135 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007136
NeilBrown57afd892005-06-21 17:17:13 -07007137 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 for (m = 0; m < SYNC_MARKS; m++) {
7139 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007140 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 }
7142 last_mark = 0;
7143 mddev->resync_mark = mark[last_mark];
7144 mddev->resync_mark_cnt = mark_cnt[last_mark];
7145
7146 /*
7147 * Tune reconstruction:
7148 */
7149 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007150 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7151 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
7153 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 last_check = 0;
7155
7156 if (j>2) {
7157 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007158 "md: resuming %s of %s from checkpoint.\n",
7159 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 mddev->curr_resync = j;
7161 }
NeilBrown75d3da42011-01-14 09:14:34 +11007162 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
7164 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007165 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
NeilBrown57afd892005-06-21 17:17:13 -07007167 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007168
NeilBrown7a91ee12009-05-26 12:57:21 +10007169 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7170 ((mddev->curr_resync > mddev->curr_resync_completed &&
7171 (mddev->curr_resync - mddev->curr_resync_completed)
7172 > (max_sectors >> 4)) ||
7173 (j - mddev->curr_resync_completed)*2
7174 >= mddev->resync_max - mddev->curr_resync_completed
7175 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007176 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007177 wait_event(mddev->recovery_wait,
7178 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007179 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007180 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007181 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007182 }
NeilBrownacb180b2009-04-14 16:28:34 +10007183
NeilBrowne62e58a2009-07-01 13:15:35 +10007184 while (j >= mddev->resync_max && !kthread_should_stop()) {
7185 /* As this condition is controlled by user-space,
7186 * we can block indefinitely, so use '_interruptible'
7187 * to avoid triggering warnings.
7188 */
7189 flush_signals(current); /* just in case */
7190 wait_event_interruptible(mddev->recovery_wait,
7191 mddev->resync_max > j
7192 || kthread_should_stop());
7193 }
NeilBrownacb180b2009-04-14 16:28:34 +10007194
7195 if (kthread_should_stop())
7196 goto interrupted;
7197
NeilBrown57afd892005-06-21 17:17:13 -07007198 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007199 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007200 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007201 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 goto out;
7203 }
NeilBrown57afd892005-06-21 17:17:13 -07007204
7205 if (!skipped) { /* actual IO requested */
7206 io_sectors += sectors;
7207 atomic_add(sectors, &mddev->recovery_active);
7208 }
7209
NeilBrowne875ece2011-07-28 11:39:24 +10007210 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7211 break;
7212
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 j += sectors;
7214 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007215 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007216 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007217 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007218 * visible in /proc/mdstat
7219 */
7220 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007221
7222 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 continue;
7224
NeilBrown57afd892005-06-21 17:17:13 -07007225 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 repeat:
7227 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7228 /* step marks */
7229 int next = (last_mark+1) % SYNC_MARKS;
7230
7231 mddev->resync_mark = mark[next];
7232 mddev->resync_mark_cnt = mark_cnt[next];
7233 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007234 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 last_mark = next;
7236 }
7237
7238
NeilBrownc6207272008-02-06 01:39:52 -08007239 if (kthread_should_stop())
7240 goto interrupted;
7241
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
7243 /*
7244 * this loop exits only if either when we are slower than
7245 * the 'hard' speed limit, or the system was IO-idle for
7246 * a jiffy.
7247 * the system might be non-idle CPU-wise, but we only care
7248 * about not overloading the IO subsystem. (things like an
7249 * e2fsck being done on the RAID array should execute fast)
7250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 cond_resched();
7252
NeilBrown57afd892005-06-21 17:17:13 -07007253 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7254 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255
NeilBrown88202a02006-01-06 00:21:36 -08007256 if (currspeed > speed_min(mddev)) {
7257 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007258 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007259 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 goto repeat;
7261 }
7262 }
7263 }
NeilBrown61df9d92006-10-03 01:15:57 -07007264 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 /*
7266 * this also signals 'finished resyncing' to md_stop
7267 */
7268 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7270
7271 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007272 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
NeilBrowndfc70642008-05-23 13:04:39 -07007274 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007275 mddev->curr_resync > 2) {
7276 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7277 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7278 if (mddev->curr_resync >= mddev->recovery_cp) {
7279 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007280 "md: checkpointing %s of %s.\n",
7281 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007282 mddev->recovery_cp = mddev->curr_resync;
7283 }
7284 } else
7285 mddev->recovery_cp = MaxSector;
7286 } else {
7287 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7288 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007289 rcu_read_lock();
7290 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007291 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007292 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007293 !test_bit(Faulty, &rdev->flags) &&
7294 !test_bit(In_sync, &rdev->flags) &&
7295 rdev->recovery_offset < mddev->curr_resync)
7296 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007297 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 }
NeilBrown17571282006-12-10 02:20:52 -08007300 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007303 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7304 /* We completed so min/max setting can be forgotten if used. */
7305 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7306 mddev->resync_min = 0;
7307 mddev->resync_max = MaxSector;
7308 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7309 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 mddev->curr_resync = 0;
7311 wake_up(&resync_wait);
7312 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7313 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007314 return;
7315
7316 interrupted:
7317 /*
7318 * got a signal, exit.
7319 */
7320 printk(KERN_INFO
7321 "md: md_do_sync() got signal ... exiting\n");
7322 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7323 goto out;
7324
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325}
NeilBrown29269552006-03-27 01:18:10 -08007326EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
NeilBrownfd01b882011-10-11 16:47:53 +11007328static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007329{
NeilBrown3cb03002011-10-11 16:45:26 +11007330 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007331 int spares = 0;
7332
NeilBrown97e4f422009-03-31 14:33:13 +11007333 mddev->curr_resync_completed = 0;
7334
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007335 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007336 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007337 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007338 (test_bit(Faulty, &rdev->flags) ||
7339 ! test_bit(In_sync, &rdev->flags)) &&
7340 atomic_read(&rdev->nr_pending)==0) {
7341 if (mddev->pers->hot_remove_disk(
7342 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007343 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007344 rdev->raid_disk = -1;
7345 }
7346 }
7347
NeilBrown53890422011-07-27 11:00:36 +10007348 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007349 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007350 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007351 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007352 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007353 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007354 if (rdev->raid_disk < 0
7355 && !test_bit(Faulty, &rdev->flags)) {
7356 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007357 if (mddev->pers->
7358 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007359 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007360 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007361 spares++;
7362 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007363 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007364 } else
7365 break;
7366 }
NeilBrowndfc70642008-05-23 13:04:39 -07007367 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007368 }
7369 return spares;
7370}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007371
NeilBrownfd01b882011-10-11 16:47:53 +11007372static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007373{
NeilBrown3cb03002011-10-11 16:45:26 +11007374 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007375
7376 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007377 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007378 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7379 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7380 /* success...*/
7381 /* activate any spares */
7382 if (mddev->pers->spare_active(mddev))
7383 sysfs_notify(&mddev->kobj, NULL,
7384 "degraded");
7385 }
7386 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7387 mddev->pers->finish_reshape)
7388 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007389
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007390 /* If array is no-longer degraded, then any saved_raid_disk
7391 * information must be scrapped. Also if any device is now
7392 * In_sync we must scrape the saved_raid_disk for that device
7393 * do the superblock for an incrementally recovered device
7394 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007395 */
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007396 list_for_each_entry(rdev, &mddev->disks, same_set)
7397 if (!mddev->degraded ||
7398 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007399 rdev->saved_raid_disk = -1;
7400
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007401 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007402 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7403 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7404 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7405 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7406 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7407 /* flag recovery needed just to double check */
7408 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7409 sysfs_notify_dirent_safe(mddev->sysfs_action);
7410 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007411 if (mddev->event_work.func)
7412 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007413}
7414
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415/*
7416 * This routine is regularly called by all per-raid-array threads to
7417 * deal with generic issues like resync and super-block update.
7418 * Raid personalities that don't have a thread (linear/raid0) do not
7419 * need this as they never do any recovery or update the superblock.
7420 *
7421 * It does not do any resync itself, but rather "forks" off other threads
7422 * to do that as needed.
7423 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7424 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007425 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 * and wakeups up this thread which will reap the thread and finish up.
7427 * This thread also removes any faulty devices (with nr_pending == 0).
7428 *
7429 * The overall approach is:
7430 * 1/ if the superblock needs updating, update it.
7431 * 2/ If a recovery thread is running, don't do anything else.
7432 * 3/ If recovery has finished, clean up, possibly marking spares active.
7433 * 4/ If there are any faulty devices, remove them.
7434 * 5/ If array is degraded, try to add spares devices
7435 * 6/ If array has spares or is not in-sync, start a resync thread.
7436 */
NeilBrownfd01b882011-10-11 16:47:53 +11007437void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007439 if (mddev->suspended)
7440 return;
7441
NeilBrown5f404022005-06-21 17:17:16 -07007442 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007443 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
NeilBrownfca4d842005-06-21 17:17:11 -07007445 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007446 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007447 printk(KERN_INFO "md: %s in immediate safe mode\n",
7448 mdname(mddev));
7449 mddev->safemode = 2;
7450 }
7451 flush_signals(current);
7452 }
7453
NeilBrownc89a8ee2008-08-05 15:54:13 +10007454 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007457 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007459 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007460 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007461 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7462 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 ))
7464 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007465
NeilBrowndf5b89b2006-03-27 01:18:20 -08007466 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007467 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007468
NeilBrownc89a8ee2008-08-05 15:54:13 +10007469 if (mddev->ro) {
7470 /* Only thing we do on a ro array is remove
7471 * failed devices.
7472 */
NeilBrown3cb03002011-10-11 16:45:26 +11007473 struct md_rdev *rdev;
NeilBrowna8c42c72011-01-31 13:47:13 +11007474 list_for_each_entry(rdev, &mddev->disks, same_set)
7475 if (rdev->raid_disk >= 0 &&
7476 !test_bit(Blocked, &rdev->flags) &&
7477 test_bit(Faulty, &rdev->flags) &&
7478 atomic_read(&rdev->nr_pending)==0) {
7479 if (mddev->pers->hot_remove_disk(
7480 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007481 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007482 rdev->raid_disk = -1;
7483 }
7484 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007485 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7486 goto unlock;
7487 }
7488
NeilBrown31a59e32008-04-30 00:52:30 -07007489 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007490 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007491 spin_lock_irq(&mddev->write_lock);
7492 if (mddev->safemode &&
7493 !atomic_read(&mddev->writes_pending) &&
7494 !mddev->in_sync &&
7495 mddev->recovery_cp == MaxSector) {
7496 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007497 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007498 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007499 }
7500 if (mddev->safemode == 1)
7501 mddev->safemode = 0;
7502 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007503 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007504 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007505 }
NeilBrownfca4d842005-06-21 17:17:11 -07007506
NeilBrown850b2b422006-10-03 01:15:46 -07007507 if (mddev->flags)
7508 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007509
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7511 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7512 /* resync/recovery still happening */
7513 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7514 goto unlock;
7515 }
7516 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007517 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 goto unlock;
7519 }
Neil Brown72a23c22008-06-28 08:31:41 +10007520 /* Set RUNNING before clearing NEEDED to avoid
7521 * any transients in the value of "sync_action".
7522 */
7523 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7524 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007525 /* Clear some bits that don't mean anything, but
7526 * might be left set
7527 */
NeilBrown24dd4692005-11-08 21:39:26 -08007528 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7529 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007531 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7532 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 /* no recovery is running.
7534 * remove any failed drives, then
7535 * add spares if possible.
7536 * Spare are also removed and re-added, to allow
7537 * the personality to fail the re-add.
7538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007540 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007541 if (mddev->pers->check_reshape == NULL ||
7542 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007543 /* Cannot proceed */
7544 goto unlock;
7545 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007546 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007547 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007548 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7549 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007550 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007551 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007552 } else if (mddev->recovery_cp < MaxSector) {
7553 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007554 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007555 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7556 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007558
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007560 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7561 /* We are adding a device or devices to an array
7562 * which has the bitmap stored on all devices.
7563 * So make sure all bitmap pages get written
7564 */
7565 bitmap_write_all(mddev->bitmap);
7566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 mddev->sync_thread = md_register_thread(md_do_sync,
7568 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007569 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 if (!mddev->sync_thread) {
7571 printk(KERN_ERR "%s: could not start resync"
7572 " thread...\n",
7573 mdname(mddev));
7574 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007575 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7576 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7577 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7578 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7579 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007580 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007582 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007583 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 }
7585 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007586 if (!mddev->sync_thread) {
7587 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7588 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7589 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007590 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007591 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 mddev_unlock(mddev);
7594 }
7595}
7596
NeilBrownfd01b882011-10-11 16:47:53 +11007597void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007598{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007599 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007600 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007601 !test_bit(Blocked, &rdev->flags) &&
7602 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007603 msecs_to_jiffies(5000));
7604 rdev_dec_pending(rdev, mddev);
7605}
7606EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7607
NeilBrown2230dfe2011-07-28 11:31:46 +10007608
7609/* Bad block management.
7610 * We can record which blocks on each device are 'bad' and so just
7611 * fail those blocks, or that stripe, rather than the whole device.
7612 * Entries in the bad-block table are 64bits wide. This comprises:
7613 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7614 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7615 * A 'shift' can be set so that larger blocks are tracked and
7616 * consequently larger devices can be covered.
7617 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7618 *
7619 * Locking of the bad-block table uses a seqlock so md_is_badblock
7620 * might need to retry if it is very unlucky.
7621 * We will sometimes want to check for bad blocks in a bi_end_io function,
7622 * so we use the write_seqlock_irq variant.
7623 *
7624 * When looking for a bad block we specify a range and want to
7625 * know if any block in the range is bad. So we binary-search
7626 * to the last range that starts at-or-before the given endpoint,
7627 * (or "before the sector after the target range")
7628 * then see if it ends after the given start.
7629 * We return
7630 * 0 if there are no known bad blocks in the range
7631 * 1 if there are known bad block which are all acknowledged
7632 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7633 * plus the start/length of the first bad section we overlap.
7634 */
7635int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7636 sector_t *first_bad, int *bad_sectors)
7637{
7638 int hi;
7639 int lo = 0;
7640 u64 *p = bb->page;
7641 int rv = 0;
7642 sector_t target = s + sectors;
7643 unsigned seq;
7644
7645 if (bb->shift > 0) {
7646 /* round the start down, and the end up */
7647 s >>= bb->shift;
7648 target += (1<<bb->shift) - 1;
7649 target >>= bb->shift;
7650 sectors = target - s;
7651 }
7652 /* 'target' is now the first block after the bad range */
7653
7654retry:
7655 seq = read_seqbegin(&bb->lock);
7656
7657 hi = bb->count;
7658
7659 /* Binary search between lo and hi for 'target'
7660 * i.e. for the last range that starts before 'target'
7661 */
7662 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7663 * are known not to be the last range before target.
7664 * VARIANT: hi-lo is the number of possible
7665 * ranges, and decreases until it reaches 1
7666 */
7667 while (hi - lo > 1) {
7668 int mid = (lo + hi) / 2;
7669 sector_t a = BB_OFFSET(p[mid]);
7670 if (a < target)
7671 /* This could still be the one, earlier ranges
7672 * could not. */
7673 lo = mid;
7674 else
7675 /* This and later ranges are definitely out. */
7676 hi = mid;
7677 }
7678 /* 'lo' might be the last that started before target, but 'hi' isn't */
7679 if (hi > lo) {
7680 /* need to check all range that end after 's' to see if
7681 * any are unacknowledged.
7682 */
7683 while (lo >= 0 &&
7684 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7685 if (BB_OFFSET(p[lo]) < target) {
7686 /* starts before the end, and finishes after
7687 * the start, so they must overlap
7688 */
7689 if (rv != -1 && BB_ACK(p[lo]))
7690 rv = 1;
7691 else
7692 rv = -1;
7693 *first_bad = BB_OFFSET(p[lo]);
7694 *bad_sectors = BB_LEN(p[lo]);
7695 }
7696 lo--;
7697 }
7698 }
7699
7700 if (read_seqretry(&bb->lock, seq))
7701 goto retry;
7702
7703 return rv;
7704}
7705EXPORT_SYMBOL_GPL(md_is_badblock);
7706
7707/*
7708 * Add a range of bad blocks to the table.
7709 * This might extend the table, or might contract it
7710 * if two adjacent ranges can be merged.
7711 * We binary-search to find the 'insertion' point, then
7712 * decide how best to handle it.
7713 */
7714static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7715 int acknowledged)
7716{
7717 u64 *p;
7718 int lo, hi;
7719 int rv = 1;
7720
7721 if (bb->shift < 0)
7722 /* badblocks are disabled */
7723 return 0;
7724
7725 if (bb->shift) {
7726 /* round the start down, and the end up */
7727 sector_t next = s + sectors;
7728 s >>= bb->shift;
7729 next += (1<<bb->shift) - 1;
7730 next >>= bb->shift;
7731 sectors = next - s;
7732 }
7733
7734 write_seqlock_irq(&bb->lock);
7735
7736 p = bb->page;
7737 lo = 0;
7738 hi = bb->count;
7739 /* Find the last range that starts at-or-before 's' */
7740 while (hi - lo > 1) {
7741 int mid = (lo + hi) / 2;
7742 sector_t a = BB_OFFSET(p[mid]);
7743 if (a <= s)
7744 lo = mid;
7745 else
7746 hi = mid;
7747 }
7748 if (hi > lo && BB_OFFSET(p[lo]) > s)
7749 hi = lo;
7750
7751 if (hi > lo) {
7752 /* we found a range that might merge with the start
7753 * of our new range
7754 */
7755 sector_t a = BB_OFFSET(p[lo]);
7756 sector_t e = a + BB_LEN(p[lo]);
7757 int ack = BB_ACK(p[lo]);
7758 if (e >= s) {
7759 /* Yes, we can merge with a previous range */
7760 if (s == a && s + sectors >= e)
7761 /* new range covers old */
7762 ack = acknowledged;
7763 else
7764 ack = ack && acknowledged;
7765
7766 if (e < s + sectors)
7767 e = s + sectors;
7768 if (e - a <= BB_MAX_LEN) {
7769 p[lo] = BB_MAKE(a, e-a, ack);
7770 s = e;
7771 } else {
7772 /* does not all fit in one range,
7773 * make p[lo] maximal
7774 */
7775 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7776 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7777 s = a + BB_MAX_LEN;
7778 }
7779 sectors = e - s;
7780 }
7781 }
7782 if (sectors && hi < bb->count) {
7783 /* 'hi' points to the first range that starts after 's'.
7784 * Maybe we can merge with the start of that range */
7785 sector_t a = BB_OFFSET(p[hi]);
7786 sector_t e = a + BB_LEN(p[hi]);
7787 int ack = BB_ACK(p[hi]);
7788 if (a <= s + sectors) {
7789 /* merging is possible */
7790 if (e <= s + sectors) {
7791 /* full overlap */
7792 e = s + sectors;
7793 ack = acknowledged;
7794 } else
7795 ack = ack && acknowledged;
7796
7797 a = s;
7798 if (e - a <= BB_MAX_LEN) {
7799 p[hi] = BB_MAKE(a, e-a, ack);
7800 s = e;
7801 } else {
7802 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7803 s = a + BB_MAX_LEN;
7804 }
7805 sectors = e - s;
7806 lo = hi;
7807 hi++;
7808 }
7809 }
7810 if (sectors == 0 && hi < bb->count) {
7811 /* we might be able to combine lo and hi */
7812 /* Note: 's' is at the end of 'lo' */
7813 sector_t a = BB_OFFSET(p[hi]);
7814 int lolen = BB_LEN(p[lo]);
7815 int hilen = BB_LEN(p[hi]);
7816 int newlen = lolen + hilen - (s - a);
7817 if (s >= a && newlen < BB_MAX_LEN) {
7818 /* yes, we can combine them */
7819 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7820 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7821 memmove(p + hi, p + hi + 1,
7822 (bb->count - hi - 1) * 8);
7823 bb->count--;
7824 }
7825 }
7826 while (sectors) {
7827 /* didn't merge (it all).
7828 * Need to add a range just before 'hi' */
7829 if (bb->count >= MD_MAX_BADBLOCKS) {
7830 /* No room for more */
7831 rv = 0;
7832 break;
7833 } else {
7834 int this_sectors = sectors;
7835 memmove(p + hi + 1, p + hi,
7836 (bb->count - hi) * 8);
7837 bb->count++;
7838
7839 if (this_sectors > BB_MAX_LEN)
7840 this_sectors = BB_MAX_LEN;
7841 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7842 sectors -= this_sectors;
7843 s += this_sectors;
7844 }
7845 }
7846
7847 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007848 if (!acknowledged)
7849 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007850 write_sequnlock_irq(&bb->lock);
7851
7852 return rv;
7853}
7854
NeilBrown3cb03002011-10-11 16:45:26 +11007855int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007856 int acknowledged)
7857{
7858 int rv = md_set_badblocks(&rdev->badblocks,
7859 s + rdev->data_offset, sectors, acknowledged);
7860 if (rv) {
7861 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007862 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007863 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7864 md_wakeup_thread(rdev->mddev->thread);
7865 }
7866 return rv;
7867}
7868EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7869
7870/*
7871 * Remove a range of bad blocks from the table.
7872 * This may involve extending the table if we spilt a region,
7873 * but it must not fail. So if the table becomes full, we just
7874 * drop the remove request.
7875 */
7876static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7877{
7878 u64 *p;
7879 int lo, hi;
7880 sector_t target = s + sectors;
7881 int rv = 0;
7882
7883 if (bb->shift > 0) {
7884 /* When clearing we round the start up and the end down.
7885 * This should not matter as the shift should align with
7886 * the block size and no rounding should ever be needed.
7887 * However it is better the think a block is bad when it
7888 * isn't than to think a block is not bad when it is.
7889 */
7890 s += (1<<bb->shift) - 1;
7891 s >>= bb->shift;
7892 target >>= bb->shift;
7893 sectors = target - s;
7894 }
7895
7896 write_seqlock_irq(&bb->lock);
7897
7898 p = bb->page;
7899 lo = 0;
7900 hi = bb->count;
7901 /* Find the last range that starts before 'target' */
7902 while (hi - lo > 1) {
7903 int mid = (lo + hi) / 2;
7904 sector_t a = BB_OFFSET(p[mid]);
7905 if (a < target)
7906 lo = mid;
7907 else
7908 hi = mid;
7909 }
7910 if (hi > lo) {
7911 /* p[lo] is the last range that could overlap the
7912 * current range. Earlier ranges could also overlap,
7913 * but only this one can overlap the end of the range.
7914 */
7915 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7916 /* Partial overlap, leave the tail of this range */
7917 int ack = BB_ACK(p[lo]);
7918 sector_t a = BB_OFFSET(p[lo]);
7919 sector_t end = a + BB_LEN(p[lo]);
7920
7921 if (a < s) {
7922 /* we need to split this range */
7923 if (bb->count >= MD_MAX_BADBLOCKS) {
7924 rv = 0;
7925 goto out;
7926 }
7927 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7928 bb->count++;
7929 p[lo] = BB_MAKE(a, s-a, ack);
7930 lo++;
7931 }
7932 p[lo] = BB_MAKE(target, end - target, ack);
7933 /* there is no longer an overlap */
7934 hi = lo;
7935 lo--;
7936 }
7937 while (lo >= 0 &&
7938 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7939 /* This range does overlap */
7940 if (BB_OFFSET(p[lo]) < s) {
7941 /* Keep the early parts of this range. */
7942 int ack = BB_ACK(p[lo]);
7943 sector_t start = BB_OFFSET(p[lo]);
7944 p[lo] = BB_MAKE(start, s - start, ack);
7945 /* now low doesn't overlap, so.. */
7946 break;
7947 }
7948 lo--;
7949 }
7950 /* 'lo' is strictly before, 'hi' is strictly after,
7951 * anything between needs to be discarded
7952 */
7953 if (hi - lo > 1) {
7954 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7955 bb->count -= (hi - lo - 1);
7956 }
7957 }
7958
7959 bb->changed = 1;
7960out:
7961 write_sequnlock_irq(&bb->lock);
7962 return rv;
7963}
7964
NeilBrown3cb03002011-10-11 16:45:26 +11007965int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10007966{
7967 return md_clear_badblocks(&rdev->badblocks,
7968 s + rdev->data_offset,
7969 sectors);
7970}
7971EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7972
7973/*
7974 * Acknowledge all bad blocks in a list.
7975 * This only succeeds if ->changed is clear. It is used by
7976 * in-kernel metadata updates
7977 */
7978void md_ack_all_badblocks(struct badblocks *bb)
7979{
7980 if (bb->page == NULL || bb->changed)
7981 /* no point even trying */
7982 return;
7983 write_seqlock_irq(&bb->lock);
7984
7985 if (bb->changed == 0) {
7986 u64 *p = bb->page;
7987 int i;
7988 for (i = 0; i < bb->count ; i++) {
7989 if (!BB_ACK(p[i])) {
7990 sector_t start = BB_OFFSET(p[i]);
7991 int len = BB_LEN(p[i]);
7992 p[i] = BB_MAKE(start, len, 1);
7993 }
7994 }
NeilBrownde393cd2011-07-28 11:31:48 +10007995 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007996 }
7997 write_sequnlock_irq(&bb->lock);
7998}
7999EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8000
NeilBrown16c791a2011-07-28 11:31:47 +10008001/* sysfs access to bad-blocks list.
8002 * We present two files.
8003 * 'bad-blocks' lists sector numbers and lengths of ranges that
8004 * are recorded as bad. The list is truncated to fit within
8005 * the one-page limit of sysfs.
8006 * Writing "sector length" to this file adds an acknowledged
8007 * bad block list.
8008 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8009 * been acknowledged. Writing to this file adds bad blocks
8010 * without acknowledging them. This is largely for testing.
8011 */
8012
8013static ssize_t
8014badblocks_show(struct badblocks *bb, char *page, int unack)
8015{
8016 size_t len;
8017 int i;
8018 u64 *p = bb->page;
8019 unsigned seq;
8020
8021 if (bb->shift < 0)
8022 return 0;
8023
8024retry:
8025 seq = read_seqbegin(&bb->lock);
8026
8027 len = 0;
8028 i = 0;
8029
8030 while (len < PAGE_SIZE && i < bb->count) {
8031 sector_t s = BB_OFFSET(p[i]);
8032 unsigned int length = BB_LEN(p[i]);
8033 int ack = BB_ACK(p[i]);
8034 i++;
8035
8036 if (unack && ack)
8037 continue;
8038
8039 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8040 (unsigned long long)s << bb->shift,
8041 length << bb->shift);
8042 }
NeilBrownde393cd2011-07-28 11:31:48 +10008043 if (unack && len == 0)
8044 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008045
8046 if (read_seqretry(&bb->lock, seq))
8047 goto retry;
8048
8049 return len;
8050}
8051
8052#define DO_DEBUG 1
8053
8054static ssize_t
8055badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8056{
8057 unsigned long long sector;
8058 int length;
8059 char newline;
8060#ifdef DO_DEBUG
8061 /* Allow clearing via sysfs *only* for testing/debugging.
8062 * Normally only a successful write may clear a badblock
8063 */
8064 int clear = 0;
8065 if (page[0] == '-') {
8066 clear = 1;
8067 page++;
8068 }
8069#endif /* DO_DEBUG */
8070
8071 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8072 case 3:
8073 if (newline != '\n')
8074 return -EINVAL;
8075 case 2:
8076 if (length <= 0)
8077 return -EINVAL;
8078 break;
8079 default:
8080 return -EINVAL;
8081 }
8082
8083#ifdef DO_DEBUG
8084 if (clear) {
8085 md_clear_badblocks(bb, sector, length);
8086 return len;
8087 }
8088#endif /* DO_DEBUG */
8089 if (md_set_badblocks(bb, sector, length, !unack))
8090 return len;
8091 else
8092 return -ENOSPC;
8093}
8094
Adrian Bunk75c96f82005-05-05 16:16:09 -07008095static int md_notify_reboot(struct notifier_block *this,
8096 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097{
8098 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008099 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008100 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101
8102 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8103
8104 printk(KERN_INFO "md: stopping all md devices.\n");
8105
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008106 for_each_mddev(mddev, tmp) {
Neil Brownc71d4882006-05-26 10:39:25 +10008107 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008108 /* Force a switch to readonly even array
8109 * appears to still be in use. Hence
8110 * the '100'.
8111 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008112 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008113 mddev_unlock(mddev);
8114 }
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008115 need_delay = 1;
8116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 /*
8118 * certain more exotic SCSI devices are known to be
8119 * volatile wrt too early system reboots. While the
8120 * right place to handle this issue is the given
8121 * driver, we do want to have a safe RAID driver ...
8122 */
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008123 if (need_delay)
8124 mdelay(1000*1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 }
8126 return NOTIFY_DONE;
8127}
8128
Adrian Bunk75c96f82005-05-05 16:16:09 -07008129static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008130 .notifier_call = md_notify_reboot,
8131 .next = NULL,
8132 .priority = INT_MAX, /* before any real devices */
8133};
8134
8135static void md_geninit(void)
8136{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008137 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008139 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140}
8141
Adrian Bunk75c96f82005-05-05 16:16:09 -07008142static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143{
Tejun Heoe804ac72010-10-15 15:36:08 +02008144 int ret = -ENOMEM;
8145
Tejun Heoada609e2011-01-25 14:35:54 +01008146 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008147 if (!md_wq)
8148 goto err_wq;
8149
8150 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8151 if (!md_misc_wq)
8152 goto err_misc_wq;
8153
8154 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8155 goto err_md;
8156
8157 if ((ret = register_blkdev(0, "mdp")) < 0)
8158 goto err_mdp;
8159 mdp_major = ret;
8160
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008161 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008162 md_probe, NULL, NULL);
8163 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 md_probe, NULL, NULL);
8165
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008167 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171
Tejun Heoe804ac72010-10-15 15:36:08 +02008172err_mdp:
8173 unregister_blkdev(MD_MAJOR, "md");
8174err_md:
8175 destroy_workqueue(md_misc_wq);
8176err_misc_wq:
8177 destroy_workqueue(md_wq);
8178err_wq:
8179 return ret;
8180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181
8182#ifndef MODULE
8183
8184/*
8185 * Searches all registered partitions for autorun RAID arrays
8186 * at boot time.
8187 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008188
8189static LIST_HEAD(all_detected_devices);
8190struct detected_devices_node {
8191 struct list_head list;
8192 dev_t dev;
8193};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
8195void md_autodetect_dev(dev_t dev)
8196{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008197 struct detected_devices_node *node_detected_dev;
8198
8199 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8200 if (node_detected_dev) {
8201 node_detected_dev->dev = dev;
8202 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8203 } else {
8204 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8205 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207}
8208
8209
8210static void autostart_arrays(int part)
8211{
NeilBrown3cb03002011-10-11 16:45:26 +11008212 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008213 struct detected_devices_node *node_detected_dev;
8214 dev_t dev;
8215 int i_scanned, i_passed;
8216
8217 i_scanned = 0;
8218 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
8220 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8221
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008222 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8223 i_scanned++;
8224 node_detected_dev = list_entry(all_detected_devices.next,
8225 struct detected_devices_node, list);
8226 list_del(&node_detected_dev->list);
8227 dev = node_detected_dev->dev;
8228 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008229 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008230 if (IS_ERR(rdev))
8231 continue;
8232
NeilBrownb2d444d2005-11-08 21:39:31 -08008233 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 MD_BUG();
8235 continue;
8236 }
NeilBrownd0fae182008-03-04 14:29:31 -08008237 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008239 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008241
8242 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8243 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
8245 autorun_devices(part);
8246}
8247
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008248#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250static __exit void md_exit(void)
8251{
NeilBrownfd01b882011-10-11 16:47:53 +11008252 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008254
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008255 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008256 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008258 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 unregister_blkdev(mdp_major, "mdp");
8260 unregister_reboot_notifier(&md_notifier);
8261 unregister_sysctl_table(raid_table_header);
8262 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008263 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008265 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008267 destroy_workqueue(md_misc_wq);
8268 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269}
8270
Dan Williams685784a2007-07-09 11:56:42 -07008271subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272module_exit(md_exit)
8273
NeilBrownf91de922005-11-08 21:39:36 -08008274static int get_ro(char *buffer, struct kernel_param *kp)
8275{
8276 return sprintf(buffer, "%d", start_readonly);
8277}
8278static int set_ro(const char *val, struct kernel_param *kp)
8279{
8280 char *e;
8281 int num = simple_strtoul(val, &e, 10);
8282 if (*val && (*e == '\0' || *e == '\n')) {
8283 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008284 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008285 }
8286 return -EINVAL;
8287}
8288
NeilBrown80ca3a42006-07-10 04:44:18 -07008289module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8290module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008291
NeilBrownefeb53c2009-01-09 08:31:10 +11008292module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008293
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294EXPORT_SYMBOL(register_md_personality);
8295EXPORT_SYMBOL(unregister_md_personality);
8296EXPORT_SYMBOL(md_error);
8297EXPORT_SYMBOL(md_done_sync);
8298EXPORT_SYMBOL(md_write_start);
8299EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300EXPORT_SYMBOL(md_register_thread);
8301EXPORT_SYMBOL(md_unregister_thread);
8302EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008303EXPORT_SYMBOL(md_check_recovery);
8304MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008305MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008306MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008307MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);