blob: 5d1b6762f108263f5e9ea7861215543b34b42685 [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>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
158static void mddev_bio_destructor(struct bio *bio)
159{
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 mddevp = (void*)bio;
163 mddev = mddevp[-1];
164
165 bio_free(bio, mddev->bio_set);
166}
167
168struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100169 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100170{
171 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100172 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100173
174 if (!mddev || !mddev->bio_set)
175 return bio_alloc(gfp_mask, nr_iovecs);
176
177 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
178 mddev->bio_set);
179 if (!b)
180 return NULL;
181 mddevp = (void*)b;
182 mddevp[-1] = mddev;
183 b->bi_destructor = mddev_bio_destructor;
184 return b;
185}
186EXPORT_SYMBOL_GPL(bio_alloc_mddev);
187
188struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100189 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100190{
191 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100193
194 if (!mddev || !mddev->bio_set)
195 return bio_clone(bio, gfp_mask);
196
197 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
198 mddev->bio_set);
199 if (!b)
200 return NULL;
201 mddevp = (void*)b;
202 mddevp[-1] = mddev;
203 b->bi_destructor = mddev_bio_destructor;
204 __bio_clone(b, bio);
205 if (bio_integrity(bio)) {
206 int ret;
207
208 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
209
210 if (ret < 0) {
211 bio_put(b);
212 return NULL;
213 }
214 }
215
216 return b;
217}
218EXPORT_SYMBOL_GPL(bio_clone_mddev);
219
NeilBrownd2eb35a2011-07-28 11:31:48 +1000220void md_trim_bio(struct bio *bio, int offset, int size)
221{
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 */
226 int i;
227 struct bio_vec *bvec;
228 int sofar = 0;
229
230 size <<= 9;
231 if (offset == 0 && size == bio->bi_size)
232 return;
233
234 bio->bi_sector += offset;
235 bio->bi_size = size;
236 offset <<= 9;
237 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
238
239 while (bio->bi_idx < bio->bi_vcnt &&
240 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
241 /* remove this whole bio_vec */
242 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
243 bio->bi_idx++;
244 }
245 if (bio->bi_idx < bio->bi_vcnt) {
246 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
247 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
248 }
249 /* avoid any complications with bi_idx being non-zero*/
250 if (bio->bi_idx) {
251 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
252 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
253 bio->bi_vcnt -= bio->bi_idx;
254 bio->bi_idx = 0;
255 }
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec, bio, i) {
258 if (sofar + bvec->bv_len > size)
259 bvec->bv_len = size - sofar;
260 if (bvec->bv_len == 0) {
261 bio->bi_vcnt = i;
262 break;
263 }
264 sofar += bvec->bv_len;
265 }
266}
267EXPORT_SYMBOL_GPL(md_trim_bio);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
NeilBrownd7603b72006-01-06 00:20:30 -0800270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
273 * count increases.
274 *
275 * Events are:
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
278 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800279static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800280static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100281void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800282{
283 atomic_inc(&md_event_count);
284 wake_up(&md_event_waiters);
285}
NeilBrown29269552006-03-27 01:18:10 -0800286EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800287
NeilBrownc331eb02006-05-30 21:27:13 -0700288/* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
290 */
NeilBrownfd01b882011-10-11 16:47:53 +1100291static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700292{
293 atomic_inc(&md_event_count);
294 wake_up(&md_event_waiters);
295}
296
NeilBrownd7603b72006-01-06 00:20:30 -0800297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
300 */
301static LIST_HEAD(all_mddevs);
302static DEFINE_SPINLOCK(all_mddevs_lock);
303
304
305/*
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 \
314 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100315 _tmp = all_mddevs.next; \
316 _mddev = NULL;}); \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100324 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 )
326
327
NeilBrown409c57f2009-03-31 14:39:39 +1100328/* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200335static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
NeilBrown49077322010-03-25 16:20:56 +1100337 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100338 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100339 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500340 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100341
NeilBrown0ca69882011-01-14 09:14:33 +1100342 if (mddev == NULL || mddev->pers == NULL
343 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100344 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200345 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100346 }
NeilBrown0ca69882011-01-14 09:14:33 +1100347 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100348 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200349 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100350 DEFINE_WAIT(__wait);
351 for (;;) {
352 prepare_to_wait(&mddev->sb_wait, &__wait,
353 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200354 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100355 break;
356 rcu_read_unlock();
357 schedule();
358 rcu_read_lock();
359 }
360 finish_wait(&mddev->sb_wait, &__wait);
361 }
362 atomic_inc(&mddev->active_io);
363 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100364
Chris Masone91ece52011-02-07 19:21:48 -0500365 /*
366 * save the sectors now since our bio can
367 * go away inside make_request
368 */
369 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200370 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100371
372 cpu = part_stat_lock();
373 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500374 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100375 part_stat_unlock();
376
NeilBrown409c57f2009-03-31 14:39:39 +1100377 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
378 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
NeilBrown9e35b992010-04-06 14:23:02 +1000381/* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
385 * unused.
386 */
NeilBrownfd01b882011-10-11 16:47:53 +1100387void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100388{
389 BUG_ON(mddev->suspended);
390 mddev->suspended = 1;
391 synchronize_rcu();
392 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
393 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100394}
NeilBrown390ee602010-06-01 19:37:27 +1000395EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100396
NeilBrownfd01b882011-10-11 16:47:53 +1100397void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100398{
399 mddev->suspended = 0;
400 wake_up(&mddev->sb_wait);
401 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500402
403 md_wakeup_thread(mddev->thread);
404 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100405}
NeilBrown390ee602010-06-01 19:37:27 +1000406EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100407
NeilBrownfd01b882011-10-11 16:47:53 +1100408int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000409{
410 return mddev->suspended;
411}
412EXPORT_SYMBOL(mddev_congested);
413
NeilBrowna2826aa2009-12-14 12:49:49 +1100414/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200415 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 */
417
Tejun Heoe9c74692010-09-03 11:56:18 +0200418static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100419{
NeilBrown3cb03002011-10-11 16:45:26 +1100420 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100421 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100422
423 rdev_dec_pending(rdev, mddev);
424
425 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200426 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200427 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428 }
429 bio_put(bio);
430}
431
NeilBrowna7a07e62010-12-09 16:04:25 +1100432static void md_submit_flush_data(struct work_struct *ws);
433
NeilBrowna035fc32010-12-09 16:17:51 +1100434static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100435{
NeilBrownfd01b882011-10-11 16:47:53 +1100436 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100437 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100438
NeilBrowna7a07e62010-12-09 16:04:25 +1100439 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
440 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 rcu_read_lock();
442 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
443 if (rdev->raid_disk >= 0 &&
444 !test_bit(Faulty, &rdev->flags)) {
445 /* Take two references, one is dropped
446 * when request finishes, one after
447 * we reclaim rcu_read_lock
448 */
449 struct bio *bi;
450 atomic_inc(&rdev->nr_pending);
451 atomic_inc(&rdev->nr_pending);
452 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100453 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100455 bi->bi_private = rdev;
456 bi->bi_bdev = rdev->bdev;
457 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200458 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100459 rcu_read_lock();
460 rdev_dec_pending(rdev, mddev);
461 }
462 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100463 if (atomic_dec_and_test(&mddev->flush_pending))
464 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100465}
466
Tejun Heoe9c74692010-09-03 11:56:18 +0200467static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100468{
NeilBrownfd01b882011-10-11 16:47:53 +1100469 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200470 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100471
Tejun Heoe9c74692010-09-03 11:56:18 +0200472 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100473 /* an empty barrier - all done */
474 bio_endio(bio, 0);
475 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200476 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200477 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100478 }
NeilBrown2b74e122010-12-09 15:59:01 +1100479
480 mddev->flush_bio = NULL;
481 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100482}
483
NeilBrownfd01b882011-10-11 16:47:53 +1100484void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100485{
486 spin_lock_irq(&mddev->write_lock);
487 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200488 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100489 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200490 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100491 spin_unlock_irq(&mddev->write_lock);
492
NeilBrowna035fc32010-12-09 16:17:51 +1100493 INIT_WORK(&mddev->flush_work, submit_flushes);
494 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100495}
Tejun Heoe9c74692010-09-03 11:56:18 +0200496EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100497
NeilBrown97658cd2011-04-18 18:25:42 +1000498/* Support for plugging.
499 * This mirrors the plugging support in request_queue, but does not
500 * require having a whole queue or request structures.
501 * We allocate an md_plug_cb for each md device and each thread it gets
502 * plugged on. This links tot the private plug_handle structure in the
503 * personality data where we keep a count of the number of outstanding
504 * plugs so other code can see if a plug is active.
505 */
506struct md_plug_cb {
507 struct blk_plug_cb cb;
NeilBrownfd01b882011-10-11 16:47:53 +1100508 struct mddev *mddev;
NeilBrown97658cd2011-04-18 18:25:42 +1000509};
510
511static void plugger_unplug(struct blk_plug_cb *cb)
512{
513 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
514 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
515 md_wakeup_thread(mdcb->mddev->thread);
516 kfree(mdcb);
517}
518
519/* Check that an unplug wakeup will come shortly.
520 * If not, wakeup the md thread immediately
521 */
NeilBrownfd01b882011-10-11 16:47:53 +1100522int mddev_check_plugged(struct mddev *mddev)
NeilBrown97658cd2011-04-18 18:25:42 +1000523{
524 struct blk_plug *plug = current->plug;
525 struct md_plug_cb *mdcb;
526
527 if (!plug)
528 return 0;
529
530 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
531 if (mdcb->cb.callback == plugger_unplug &&
532 mdcb->mddev == mddev) {
533 /* Already on the list, move to top */
534 if (mdcb != list_first_entry(&plug->cb_list,
535 struct md_plug_cb,
536 cb.list))
537 list_move(&mdcb->cb.list, &plug->cb_list);
538 return 1;
539 }
540 }
541 /* Not currently on the callback list */
542 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
543 if (!mdcb)
544 return 0;
545
546 mdcb->mddev = mddev;
547 mdcb->cb.callback = plugger_unplug;
548 atomic_inc(&mddev->plug_cnt);
549 list_add(&mdcb->cb.list, &plug->cb_list);
550 return 1;
551}
552EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000553
NeilBrownfd01b882011-10-11 16:47:53 +1100554static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 atomic_inc(&mddev->active);
557 return mddev;
558}
559
Dan Williams5fd3a172009-03-04 00:57:25 -0700560static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100561
NeilBrownfd01b882011-10-11 16:47:53 +1100562static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
NeilBrowna167f662010-10-26 18:31:13 +1100564 struct bio_set *bs = NULL;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
567 return;
NeilBrownd3374822009-01-09 08:31:10 +1100568 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100569 mddev->ctime == 0 && !mddev->hold_active) {
570 /* Array is not configured at all, and not held active,
571 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100572 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100573 bs = mddev->bio_set;
574 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100575 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200576 /* We did a probe so need to clean up. Call
577 * queue_work inside the spinlock so that
578 * flush_workqueue() after mddev_find will
579 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100580 */
581 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200582 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100583 } else
584 kfree(mddev);
585 }
586 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100587 if (bs)
588 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
NeilBrownfd01b882011-10-11 16:47:53 +1100591void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100592{
593 mutex_init(&mddev->open_mutex);
594 mutex_init(&mddev->reconfig_mutex);
595 mutex_init(&mddev->bitmap_info.mutex);
596 INIT_LIST_HEAD(&mddev->disks);
597 INIT_LIST_HEAD(&mddev->all_mddevs);
598 init_timer(&mddev->safemode_timer);
599 atomic_set(&mddev->active, 1);
600 atomic_set(&mddev->openers, 0);
601 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000602 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100603 spin_lock_init(&mddev->write_lock);
604 atomic_set(&mddev->flush_pending, 0);
605 init_waitqueue_head(&mddev->sb_wait);
606 init_waitqueue_head(&mddev->recovery_wait);
607 mddev->reshape_position = MaxSector;
608 mddev->resync_min = 0;
609 mddev->resync_max = MaxSector;
610 mddev->level = LEVEL_NONE;
611}
NeilBrown390ee602010-06-01 19:37:27 +1000612EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100613
NeilBrownfd01b882011-10-11 16:47:53 +1100614static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
NeilBrownfd01b882011-10-11 16:47:53 +1100616 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
NeilBrown8f5f02c2011-02-16 13:58:51 +1100618 if (unit && MAJOR(unit) != MD_MAJOR)
619 unit &= ~((1<<MdpMinorShift)-1);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 retry:
622 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
NeilBrownefeb53c2009-01-09 08:31:10 +1100624 if (unit) {
625 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
626 if (mddev->unit == unit) {
627 mddev_get(mddev);
628 spin_unlock(&all_mddevs_lock);
629 kfree(new);
630 return mddev;
631 }
632
633 if (new) {
634 list_add(&new->all_mddevs, &all_mddevs);
635 spin_unlock(&all_mddevs_lock);
636 new->hold_active = UNTIL_IOCTL;
637 return new;
638 }
639 } else if (new) {
640 /* find an unused unit number */
641 static int next_minor = 512;
642 int start = next_minor;
643 int is_free = 0;
644 int dev = 0;
645 while (!is_free) {
646 dev = MKDEV(MD_MAJOR, next_minor);
647 next_minor++;
648 if (next_minor > MINORMASK)
649 next_minor = 0;
650 if (next_minor == start) {
651 /* Oh dear, all in use. */
652 spin_unlock(&all_mddevs_lock);
653 kfree(new);
654 return NULL;
655 }
656
657 is_free = 1;
658 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
659 if (mddev->unit == dev) {
660 is_free = 0;
661 break;
662 }
663 }
664 new->unit = dev;
665 new->md_minor = MINOR(dev);
666 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 list_add(&new->all_mddevs, &all_mddevs);
668 spin_unlock(&all_mddevs_lock);
669 return new;
670 }
671 spin_unlock(&all_mddevs_lock);
672
NeilBrown9ffae0c2006-01-06 00:20:32 -0800673 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!new)
675 return NULL;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 new->unit = unit;
678 if (MAJOR(unit) == MD_MAJOR)
679 new->md_minor = MINOR(unit);
680 else
681 new->md_minor = MINOR(unit) >> MdpMinorShift;
682
NeilBrownfafd7fb2010-04-01 15:55:30 +1100683 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto retry;
686}
687
NeilBrownfd01b882011-10-11 16:47:53 +1100688static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800690 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
NeilBrownfd01b882011-10-11 16:47:53 +1100693static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100694{
695 return mutex_is_locked(&mddev->reconfig_mutex);
696}
697
NeilBrownfd01b882011-10-11 16:47:53 +1100698static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800700 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
NeilBrownb6eb1272010-04-15 10:13:47 +1000703static struct attribute_group md_redundancy_group;
704
NeilBrownfd01b882011-10-11 16:47:53 +1100705static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
NeilBrowna64c8762010-04-14 17:15:37 +1000707 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000708 /* These cannot be removed under reconfig_mutex as
709 * an access to the files will try to take reconfig_mutex
710 * while holding the file unremovable, which leads to
711 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000712 * So hold set sysfs_active while the remove in happeing,
713 * and anything else which might set ->to_remove or my
714 * otherwise change the sysfs namespace will fail with
715 * -EBUSY if sysfs_active is still set.
716 * We set sysfs_active under reconfig_mutex and elsewhere
717 * test it under the same mutex to ensure its correct value
718 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000719 */
NeilBrowna64c8762010-04-14 17:15:37 +1000720 struct attribute_group *to_remove = mddev->to_remove;
721 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000722 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000723 mutex_unlock(&mddev->reconfig_mutex);
724
NeilBrown00bcb4a2010-06-01 19:37:23 +1000725 if (mddev->kobj.sd) {
726 if (to_remove != &md_redundancy_group)
727 sysfs_remove_group(&mddev->kobj, to_remove);
728 if (mddev->pers == NULL ||
729 mddev->pers->sync_request == NULL) {
730 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
731 if (mddev->sysfs_action)
732 sysfs_put(mddev->sysfs_action);
733 mddev->sysfs_action = NULL;
734 }
NeilBrowna64c8762010-04-14 17:15:37 +1000735 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000736 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000737 } else
738 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Chris Dunlop751e67c2011-10-19 16:48:26 +1100740 /* As we've dropped the mutex we need a spinlock to
741 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000742 */
743 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700744 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000745 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
NeilBrownfd01b882011-10-11 16:47:53 +1100748static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
NeilBrown3cb03002011-10-11 16:45:26 +1100750 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100752 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (rdev->desc_nr == nr)
754 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return NULL;
757}
758
NeilBrownfd01b882011-10-11 16:47:53 +1100759static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
NeilBrown3cb03002011-10-11 16:45:26 +1100761 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100763 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (rdev->bdev->bd_dev == dev)
765 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return NULL;
768}
769
NeilBrown84fc4b52011-10-11 16:49:58 +1100770static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800771{
NeilBrown84fc4b52011-10-11 16:49:58 +1100772 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800773 list_for_each_entry(pers, &pers_list, list) {
774 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800775 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800776 if (strcmp(pers->name, clevel)==0)
777 return pers;
778 }
NeilBrown2604b702006-01-06 00:20:36 -0800779 return NULL;
780}
781
Andre Nollb73df2d2008-07-11 22:02:23 +1000782/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100783static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100785 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000786 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
NeilBrown3cb03002011-10-11 16:45:26 +1100789static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 if (rdev->sb_page)
792 MD_BUG();
793
794 rdev->sb_page = alloc_page(GFP_KERNEL);
795 if (!rdev->sb_page) {
796 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000797 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 return 0;
801}
802
NeilBrown3cb03002011-10-11 16:45:26 +1100803static void free_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800806 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 rdev->sb_loaded = 0;
808 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000809 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100810 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
NeilBrown2699b672011-07-28 11:31:47 +1000812 if (rdev->bb_page) {
813 put_page(rdev->bb_page);
814 rdev->bb_page = NULL;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818
NeilBrown6712ecf2007-09-27 12:47:43 +0200819static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700820{
NeilBrown3cb03002011-10-11 16:45:26 +1100821 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100822 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700823
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700824 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
825 printk("md: super_written gets error=%d, uptodate=%d\n",
826 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
827 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800828 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700829 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700830
NeilBrowna9701a32005-11-08 21:39:34 -0800831 if (atomic_dec_and_test(&mddev->pending_writes))
832 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700833 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700834}
835
NeilBrownfd01b882011-10-11 16:47:53 +1100836void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700837 sector_t sector, int size, struct page *page)
838{
839 /* write first size bytes of page to sector of rdev
840 * Increment mddev->pending_writes before returning
841 * and decrement it on completion, waking up sb_wait
842 * if zero is reached.
843 * If an error occurred, call md_error
844 */
NeilBrowna167f662010-10-26 18:31:13 +1100845 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700846
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100847 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700848 bio->bi_sector = sector;
849 bio_add_page(bio, page, size, 0);
850 bio->bi_private = rdev;
851 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800852
NeilBrown7bfa19f2005-06-21 17:17:28 -0700853 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000854 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800855}
856
NeilBrownfd01b882011-10-11 16:47:53 +1100857void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800858{
Tejun Heoe9c74692010-09-03 11:56:18 +0200859 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800860 DEFINE_WAIT(wq);
861 for(;;) {
862 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
863 if (atomic_read(&mddev->pending_writes)==0)
864 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800865 schedule();
866 }
867 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700868}
869
NeilBrown6712ecf2007-09-27 12:47:43 +0200870static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
NeilBrown3cb03002011-10-11 16:45:26 +1100875int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100876 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
NeilBrowna167f662010-10-26 18:31:13 +1100878 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct completion event;
880 int ret;
881
Jens Axboe721a9602011-03-09 11:56:30 +0100882 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100884 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
885 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100886 if (metadata_op)
887 bio->bi_sector = sector + rdev->sb_start;
888 else
889 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 bio_add_page(bio, page, size, 0);
891 init_completion(&event);
892 bio->bi_private = &event;
893 bio->bi_end_io = bi_complete;
894 submit_bio(rw, bio);
895 wait_for_completion(&event);
896
897 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
898 bio_put(bio);
899 return ret;
900}
NeilBrowna8745db2006-01-06 00:20:34 -0800901EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
NeilBrown3cb03002011-10-11 16:45:26 +1100903static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 char b[BDEVNAME_SIZE];
906 if (!rdev->sb_page) {
907 MD_BUG();
908 return -EINVAL;
909 }
910 if (rdev->sb_loaded)
911 return 0;
912
913
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100914 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto fail;
916 rdev->sb_loaded = 1;
917 return 0;
918
919fail:
920 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
921 bdevname(rdev->bdev,b));
922 return -EINVAL;
923}
924
925static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
926{
Andre Noll05710462008-07-11 22:02:20 +1000927 return sb1->set_uuid0 == sb2->set_uuid0 &&
928 sb1->set_uuid1 == sb2->set_uuid1 &&
929 sb1->set_uuid2 == sb2->set_uuid2 &&
930 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
934{
935 int ret;
936 mdp_super_t *tmp1, *tmp2;
937
938 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
939 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
940
941 if (!tmp1 || !tmp2) {
942 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100943 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto abort;
945 }
946
947 *tmp1 = *sb1;
948 *tmp2 = *sb2;
949
950 /*
951 * nr_disks is not constant
952 */
953 tmp1->nr_disks = 0;
954 tmp2->nr_disks = 0;
955
Andre Nollce0c8e02008-07-11 22:02:20 +1000956 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700958 kfree(tmp1);
959 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return ret;
961}
962
NeilBrown4d167f02007-05-09 02:35:37 -0700963
964static u32 md_csum_fold(u32 csum)
965{
966 csum = (csum & 0xffff) + (csum >> 16);
967 return (csum & 0xffff) + (csum >> 16);
968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static unsigned int calc_sb_csum(mdp_super_t * sb)
971{
NeilBrown4d167f02007-05-09 02:35:37 -0700972 u64 newcsum = 0;
973 u32 *sb32 = (u32*)sb;
974 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned int disk_csum, csum;
976
977 disk_csum = sb->sb_csum;
978 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700979
980 for (i = 0; i < MD_SB_BYTES/4 ; i++)
981 newcsum += sb32[i];
982 csum = (newcsum & 0xffffffff) + (newcsum>>32);
983
984
985#ifdef CONFIG_ALPHA
986 /* This used to use csum_partial, which was wrong for several
987 * reasons including that different results are returned on
988 * different architectures. It isn't critical that we get exactly
989 * the same return value as before (we always csum_fold before
990 * testing, and that removes any differences). However as we
991 * know that csum_partial always returned a 16bit value on
992 * alphas, do a fold to maximise conformity to previous behaviour.
993 */
994 sb->sb_csum = md_csum_fold(disk_csum);
995#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return csum;
999}
1000
1001
1002/*
1003 * Handle superblock details.
1004 * We want to be able to handle multiple superblock formats
1005 * so we have a common interface to them all, and an array of
1006 * different handlers.
1007 * We rely on user-space to write the initial superblock, and support
1008 * reading and updating of superblocks.
1009 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001010 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * loads and validates a superblock on dev.
1012 * if refdev != NULL, compare superblocks on both devices
1013 * Return:
1014 * 0 - dev has a superblock that is compatible with refdev
1015 * 1 - dev has a superblock that is compatible and newer than refdev
1016 * so dev should be used as the refdev in future
1017 * -EINVAL superblock incompatible or invalid
1018 * -othererror e.g. -EIO
1019 *
NeilBrownfd01b882011-10-11 16:47:53 +11001020 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * Verify that dev is acceptable into mddev.
1022 * The first time, mddev->raid_disks will be 0, and data from
1023 * dev should be merged in. Subsequent calls check that dev
1024 * is new enough. Return 0 or -EINVAL
1025 *
NeilBrownfd01b882011-10-11 16:47:53 +11001026 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * Update the superblock for rdev with data in mddev
1028 * This does not write to disc.
1029 *
1030 */
1031
1032struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001033 char *name;
1034 struct module *owner;
NeilBrown3cb03002011-10-11 16:45:26 +11001035 int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001036 int minor_version);
NeilBrownfd01b882011-10-11 16:47:53 +11001037 int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev);
1038 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001039 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001040 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041};
1042
1043/*
Andre Noll0894cc32009-06-18 08:49:23 +10001044 * Check that the given mddev has no bitmap.
1045 *
1046 * This function is called from the run method of all personalities that do not
1047 * support bitmaps. It prints an error message and returns non-zero if mddev
1048 * has a bitmap. Otherwise, it returns 0.
1049 *
1050 */
NeilBrownfd01b882011-10-11 16:47:53 +11001051int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001052{
NeilBrownc3d97142009-12-14 12:49:52 +11001053 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001054 return 0;
1055 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1056 mdname(mddev), mddev->pers->name);
1057 return 1;
1058}
1059EXPORT_SYMBOL(md_check_no_bitmap);
1060
1061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * load_super for 0.90.0
1063 */
NeilBrown3cb03002011-10-11 16:45:26 +11001064static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1067 mdp_super_t *sb;
1068 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /*
Andre Noll0f420352008-07-11 22:02:23 +10001071 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * it's at the end of the disk.
1073 *
1074 * It also happens to be a multiple of 4Kb.
1075 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001076 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
NeilBrown0002b272005-09-09 16:23:53 -07001078 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (ret) return ret;
1080
1081 ret = -EINVAL;
1082
1083 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001084 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 if (sb->md_magic != MD_SB_MAGIC) {
1087 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1088 b);
1089 goto abort;
1090 }
1091
1092 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001093 sb->minor_version < 90 ||
1094 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1096 sb->major_version, sb->minor_version,
1097 b);
1098 goto abort;
1099 }
1100
1101 if (sb->raid_disks <= 0)
1102 goto abort;
1103
NeilBrown4d167f02007-05-09 02:35:37 -07001104 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1106 b);
1107 goto abort;
1108 }
1109
1110 rdev->preferred_minor = sb->md_minor;
1111 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001112 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001113 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (sb->level == LEVEL_MULTIPATH)
1116 rdev->desc_nr = -1;
1117 else
1118 rdev->desc_nr = sb->this_disk.number;
1119
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001120 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001124 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (!uuid_equal(refsb, sb)) {
1126 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1127 b, bdevname(refdev->bdev,b2));
1128 goto abort;
1129 }
1130 if (!sb_equal(refsb, sb)) {
1131 printk(KERN_WARNING "md: %s has same UUID"
1132 " but different superblock to %s\n",
1133 b, bdevname(refdev->bdev, b2));
1134 goto abort;
1135 }
1136 ev1 = md_event(sb);
1137 ev2 = md_event(refsb);
1138 if (ev1 > ev2)
1139 ret = 1;
1140 else
1141 ret = 0;
1142 }
NeilBrown8190e752009-06-18 08:48:58 +10001143 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001144 /* Limit to 4TB as metadata cannot record more than that */
1145 if (rdev->sectors >= (2ULL << 32))
1146 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
NeilBrown27a7b262011-09-10 17:21:28 +10001148 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001149 /* "this cannot possibly happen" ... */
1150 ret = -EINVAL;
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 abort:
1153 return ret;
1154}
1155
1156/*
1157 * validate_super for 0.90.0
1158 */
NeilBrownfd01b882011-10-11 16:47:53 +11001159static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001162 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001163 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
NeilBrown41158c72005-06-21 17:17:25 -07001165 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001166 clear_bit(Faulty, &rdev->flags);
1167 clear_bit(In_sync, &rdev->flags);
1168 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (mddev->raid_disks == 0) {
1171 mddev->major_version = 0;
1172 mddev->minor_version = sb->minor_version;
1173 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001174 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001175 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 mddev->ctime = sb->ctime;
1177 mddev->utime = sb->utime;
1178 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001179 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 mddev->layout = sb->layout;
1181 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001182 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001183 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001184 mddev->bitmap_info.offset = 0;
1185 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
NeilBrownf6705572006-03-27 01:18:11 -08001187 if (mddev->minor_version >= 91) {
1188 mddev->reshape_position = sb->reshape_position;
1189 mddev->delta_disks = sb->delta_disks;
1190 mddev->new_level = sb->new_level;
1191 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001192 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001193 } else {
1194 mddev->reshape_position = MaxSector;
1195 mddev->delta_disks = 0;
1196 mddev->new_level = mddev->level;
1197 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001198 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (sb->state & (1<<MD_SB_CLEAN))
1202 mddev->recovery_cp = MaxSector;
1203 else {
1204 if (sb->events_hi == sb->cp_events_hi &&
1205 sb->events_lo == sb->cp_events_lo) {
1206 mddev->recovery_cp = sb->recovery_cp;
1207 } else
1208 mddev->recovery_cp = 0;
1209 }
1210
1211 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1212 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1213 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1214 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1215
1216 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001217
1218 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001219 mddev->bitmap_info.file == NULL)
1220 mddev->bitmap_info.offset =
1221 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001222
NeilBrown41158c72005-06-21 17:17:25 -07001223 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001224 /* Insist on good event counter while assembling, except
1225 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001227 if (sb->disks[rdev->desc_nr].state & (
1228 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1229 if (ev1 < mddev->events)
1230 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001231 } else if (mddev->bitmap) {
1232 /* if adding to array with a bitmap, then we can accept an
1233 * older device ... but not too old.
1234 */
NeilBrown41158c72005-06-21 17:17:25 -07001235 if (ev1 < mddev->bitmap->events_cleared)
1236 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001237 } else {
1238 if (ev1 < mddev->events)
1239 /* just a hot-add of a new device, leave raid_disk at -1 */
1240 return 0;
1241 }
NeilBrown41158c72005-06-21 17:17:25 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 desc = sb->disks + rdev->desc_nr;
1245
1246 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001247 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001248 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1249 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001250 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001252 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1253 /* active but not in sync implies recovery up to
1254 * reshape position. We don't know exactly where
1255 * that is, so set to zero for now */
1256 if (mddev->minor_version >= 91) {
1257 rdev->recovery_offset = 0;
1258 rdev->raid_disk = desc->raid_disk;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001261 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1262 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001263 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001264 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 0;
1266}
1267
1268/*
1269 * sync_super for 0.90.0
1270 */
NeilBrownfd01b882011-10-11 16:47:53 +11001271static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001274 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /* make rdev->sb match mddev data..
1279 *
1280 * 1/ zero out disks
1281 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1282 * 3/ any empty disks < next_spare become removed
1283 *
1284 * disks[0] gets initialised to REMOVED because
1285 * we cannot be sure from other fields if it has
1286 * been initialised or not.
1287 */
1288 int i;
1289 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1290
NeilBrown61181562005-09-09 16:24:02 -07001291 rdev->sb_size = MD_SB_BYTES;
1292
Namhyung Kim65a06f062011-07-27 11:00:36 +10001293 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 memset(sb, 0, sizeof(*sb));
1296
1297 sb->md_magic = MD_SB_MAGIC;
1298 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 sb->patch_version = mddev->patch_version;
1300 sb->gvalid_words = 0; /* ignored */
1301 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1302 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1303 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1304 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1305
1306 sb->ctime = mddev->ctime;
1307 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001308 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 sb->raid_disks = mddev->raid_disks;
1310 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001311 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 sb->utime = mddev->utime;
1313 sb->state = 0;
1314 sb->events_hi = (mddev->events>>32);
1315 sb->events_lo = (u32)mddev->events;
1316
NeilBrownf6705572006-03-27 01:18:11 -08001317 if (mddev->reshape_position == MaxSector)
1318 sb->minor_version = 90;
1319 else {
1320 sb->minor_version = 91;
1321 sb->reshape_position = mddev->reshape_position;
1322 sb->new_level = mddev->new_level;
1323 sb->delta_disks = mddev->delta_disks;
1324 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001325 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001326 }
1327 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (mddev->in_sync)
1329 {
1330 sb->recovery_cp = mddev->recovery_cp;
1331 sb->cp_events_hi = (mddev->events>>32);
1332 sb->cp_events_lo = (u32)mddev->events;
1333 if (mddev->recovery_cp == MaxSector)
1334 sb->state = (1<< MD_SB_CLEAN);
1335 } else
1336 sb->recovery_cp = 0;
1337
1338 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001339 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
NeilBrownc3d97142009-12-14 12:49:52 +11001341 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001342 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001345 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001347 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001348 int is_active = test_bit(In_sync, &rdev2->flags);
1349
1350 if (rdev2->raid_disk >= 0 &&
1351 sb->minor_version >= 91)
1352 /* we have nowhere to store the recovery_offset,
1353 * but if it is not below the reshape_position,
1354 * we can piggy-back on that.
1355 */
1356 is_active = 1;
1357 if (rdev2->raid_disk < 0 ||
1358 test_bit(Faulty, &rdev2->flags))
1359 is_active = 0;
1360 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001361 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001363 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001364 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 d = &sb->disks[rdev2->desc_nr];
1366 nr_disks++;
1367 d->number = rdev2->desc_nr;
1368 d->major = MAJOR(rdev2->bdev->bd_dev);
1369 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001370 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 d->raid_disk = rdev2->raid_disk;
1372 else
1373 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001374 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001376 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001378 if (test_bit(In_sync, &rdev2->flags))
1379 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 active++;
1381 working++;
1382 } else {
1383 d->state = 0;
1384 spare++;
1385 working++;
1386 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001387 if (test_bit(WriteMostly, &rdev2->flags))
1388 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* now set the "removed" and "faulty" bits on any missing devices */
1391 for (i=0 ; i < mddev->raid_disks ; i++) {
1392 mdp_disk_t *d = &sb->disks[i];
1393 if (d->state == 0 && d->number == 0) {
1394 d->number = i;
1395 d->raid_disk = i;
1396 d->state = (1<<MD_DISK_REMOVED);
1397 d->state |= (1<<MD_DISK_FAULTY);
1398 failed++;
1399 }
1400 }
1401 sb->nr_disks = nr_disks;
1402 sb->active_disks = active;
1403 sb->working_disks = working;
1404 sb->failed_disks = failed;
1405 sb->spare_disks = spare;
1406
1407 sb->this_disk = sb->disks[rdev->desc_nr];
1408 sb->sb_csum = calc_sb_csum(sb);
1409}
1410
1411/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412 * rdev_size_change for 0.90.0
1413 */
1414static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001415super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001416{
Andre Noll58c0fed2009-03-31 14:33:13 +11001417 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001418 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001419 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001421 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001422 if (!num_sectors || num_sectors > rdev->sb_start)
1423 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001424 /* Limit to 4TB as metadata cannot record more than that.
1425 * 4TB == 2^32 KB, or 2*2^32 sectors.
1426 */
1427 if (num_sectors >= (2ULL << 32))
1428 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001429 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001430 rdev->sb_page);
1431 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001432 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001433}
1434
1435
1436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * version 1 superblock
1438 */
1439
NeilBrown1c05b4b2006-10-21 10:24:08 -07001440static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001442 __le32 disk_csum;
1443 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 unsigned long long newcsum;
1445 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001446 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int i;
1448
1449 disk_csum = sb->sb_csum;
1450 sb->sb_csum = 0;
1451 newcsum = 0;
1452 for (i=0; size>=4; size -= 4 )
1453 newcsum += le32_to_cpu(*isuper++);
1454
1455 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001456 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1459 sb->sb_csum = disk_csum;
1460 return cpu_to_le32(csum);
1461}
1462
NeilBrown2699b672011-07-28 11:31:47 +10001463static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1464 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001465static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct mdp_superblock_1 *sb;
1468 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001469 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001471 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /*
Andre Noll0f420352008-07-11 22:02:23 +10001474 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * It is always aligned to a 4K boundary and
1476 * depeding on minor_version, it can be:
1477 * 0: At least 8K, but less than 12K, from end of device
1478 * 1: At start of device
1479 * 2: 4K from start of device.
1480 */
1481 switch(minor_version) {
1482 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001483 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001484 sb_start -= 8*2;
1485 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
1487 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001488 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001491 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
1493 default:
1494 return -EINVAL;
1495 }
Andre Noll0f420352008-07-11 22:02:23 +10001496 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
NeilBrown0002b272005-09-09 16:23:53 -07001498 /* superblock is rarely larger than 1K, but it can be larger,
1499 * and it is safe to read 4k, so we do that
1500 */
1501 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (ret) return ret;
1503
1504
Namhyung Kim65a06f062011-07-27 11:00:36 +10001505 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1508 sb->major_version != cpu_to_le32(1) ||
1509 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001510 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001511 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EINVAL;
1513
1514 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1515 printk("md: invalid superblock checksum on %s\n",
1516 bdevname(rdev->bdev,b));
1517 return -EINVAL;
1518 }
1519 if (le64_to_cpu(sb->data_size) < 10) {
1520 printk("md: data_size too small on %s\n",
1521 bdevname(rdev->bdev,b));
1522 return -EINVAL;
1523 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 rdev->preferred_minor = 0xffff;
1526 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001527 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
NeilBrown0002b272005-09-09 16:23:53 -07001529 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001530 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001531 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001532 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1533
1534 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001535 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001536 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001537
NeilBrown31b65a02006-07-10 04:44:14 -07001538 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1539 rdev->desc_nr = -1;
1540 else
1541 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1542
NeilBrown2699b672011-07-28 11:31:47 +10001543 if (!rdev->bb_page) {
1544 rdev->bb_page = alloc_page(GFP_KERNEL);
1545 if (!rdev->bb_page)
1546 return -ENOMEM;
1547 }
1548 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1549 rdev->badblocks.count == 0) {
1550 /* need to load the bad block list.
1551 * Currently we limit it to one page.
1552 */
1553 s32 offset;
1554 sector_t bb_sector;
1555 u64 *bbp;
1556 int i;
1557 int sectors = le16_to_cpu(sb->bblog_size);
1558 if (sectors > (PAGE_SIZE / 512))
1559 return -EINVAL;
1560 offset = le32_to_cpu(sb->bblog_offset);
1561 if (offset == 0)
1562 return -EINVAL;
1563 bb_sector = (long long)offset;
1564 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1565 rdev->bb_page, READ, true))
1566 return -EIO;
1567 bbp = (u64 *)page_address(rdev->bb_page);
1568 rdev->badblocks.shift = sb->bblog_shift;
1569 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1570 u64 bb = le64_to_cpu(*bbp);
1571 int count = bb & (0x3ff);
1572 u64 sector = bb >> 10;
1573 sector <<= sb->bblog_shift;
1574 count <<= sb->bblog_shift;
1575 if (bb + 1 == 0)
1576 break;
1577 if (md_set_badblocks(&rdev->badblocks,
1578 sector, count, 1) == 0)
1579 return -EINVAL;
1580 }
1581 } else if (sb->bblog_offset == 0)
1582 rdev->badblocks.shift = -1;
1583
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001584 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001585 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001586 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001588 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1591 sb->level != refsb->level ||
1592 sb->layout != refsb->layout ||
1593 sb->chunksize != refsb->chunksize) {
1594 printk(KERN_WARNING "md: %s has strangely different"
1595 " superblock to %s\n",
1596 bdevname(rdev->bdev,b),
1597 bdevname(refdev->bdev,b2));
1598 return -EINVAL;
1599 }
1600 ev1 = le64_to_cpu(sb->events);
1601 ev2 = le64_to_cpu(refsb->events);
1602
1603 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001604 ret = 1;
1605 else
1606 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
NeilBrowna1801f82008-03-04 14:29:31 -08001608 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001609 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001610 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001612 rdev->sectors = rdev->sb_start;
1613 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001615 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001616 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001617 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
NeilBrownfd01b882011-10-11 16:47:53 +11001621static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001623 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001624 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
NeilBrown41158c72005-06-21 17:17:25 -07001626 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001627 clear_bit(Faulty, &rdev->flags);
1628 clear_bit(In_sync, &rdev->flags);
1629 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (mddev->raid_disks == 0) {
1632 mddev->major_version = 1;
1633 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001634 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001635 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1637 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1638 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001639 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 mddev->layout = le32_to_cpu(sb->layout);
1641 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001642 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001643 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001644 mddev->bitmap_info.offset = 0;
1645 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1648 memcpy(mddev->uuid, sb->set_uuid, 16);
1649
1650 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001651
NeilBrown71c08052005-09-09 16:23:51 -07001652 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001653 mddev->bitmap_info.file == NULL )
1654 mddev->bitmap_info.offset =
1655 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001656
NeilBrownf6705572006-03-27 01:18:11 -08001657 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1658 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1659 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1660 mddev->new_level = le32_to_cpu(sb->new_level);
1661 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001662 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001663 } else {
1664 mddev->reshape_position = MaxSector;
1665 mddev->delta_disks = 0;
1666 mddev->new_level = mddev->level;
1667 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001668 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001669 }
1670
NeilBrown41158c72005-06-21 17:17:25 -07001671 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001672 /* Insist of good event counter while assembling, except for
1673 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001675 if (rdev->desc_nr >= 0 &&
1676 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1677 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1678 if (ev1 < mddev->events)
1679 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001680 } else if (mddev->bitmap) {
1681 /* If adding to array with a bitmap, then we can accept an
1682 * older device, but not too old.
1683 */
NeilBrown41158c72005-06-21 17:17:25 -07001684 if (ev1 < mddev->bitmap->events_cleared)
1685 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001686 } else {
1687 if (ev1 < mddev->events)
1688 /* just a hot-add of a new device, leave raid_disk at -1 */
1689 return 0;
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (mddev->level != LEVEL_MULTIPATH) {
1692 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001693 if (rdev->desc_nr < 0 ||
1694 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1695 role = 0xffff;
1696 rdev->desc_nr = -1;
1697 } else
1698 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 switch(role) {
1700 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001703 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001706 if ((le32_to_cpu(sb->feature_map) &
1707 MD_FEATURE_RECOVERY_OFFSET))
1708 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1709 else
1710 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 rdev->raid_disk = role;
1712 break;
1713 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001714 if (sb->devflags & WriteMostly1)
1715 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001716 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001717 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return 0;
1720}
1721
NeilBrownfd01b882011-10-11 16:47:53 +11001722static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001725 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int max_dev, i;
1727 /* make rdev->sb match mddev and rdev data. */
1728
Namhyung Kim65a06f062011-07-27 11:00:36 +10001729 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 sb->feature_map = 0;
1732 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001733 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 memset(sb->pad3, 0, sizeof(sb->pad3));
1736
1737 sb->utime = cpu_to_le64((__u64)mddev->utime);
1738 sb->events = cpu_to_le64(mddev->events);
1739 if (mddev->in_sync)
1740 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1741 else
1742 sb->resync_offset = cpu_to_le64(0);
1743
NeilBrown1c05b4b2006-10-21 10:24:08 -07001744 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001745
NeilBrownf0ca3402006-02-02 14:28:04 -08001746 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001747 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001748 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001749 sb->level = cpu_to_le32(mddev->level);
1750 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001751
NeilBrownaeb9b212011-08-25 14:43:08 +10001752 if (test_bit(WriteMostly, &rdev->flags))
1753 sb->devflags |= WriteMostly1;
1754 else
1755 sb->devflags &= ~WriteMostly1;
1756
NeilBrownc3d97142009-12-14 12:49:52 +11001757 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1758 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001759 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001760 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001761
1762 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001763 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001764 sb->feature_map |=
1765 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1766 sb->recovery_offset =
1767 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001768 }
1769
NeilBrownf6705572006-03-27 01:18:11 -08001770 if (mddev->reshape_position != MaxSector) {
1771 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1772 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1773 sb->new_layout = cpu_to_le32(mddev->new_layout);
1774 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1775 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001776 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001777 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001778
NeilBrown2699b672011-07-28 11:31:47 +10001779 if (rdev->badblocks.count == 0)
1780 /* Nothing to do for bad blocks*/ ;
1781 else if (sb->bblog_offset == 0)
1782 /* Cannot record bad blocks on this device */
1783 md_error(mddev, rdev);
1784 else {
1785 struct badblocks *bb = &rdev->badblocks;
1786 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1787 u64 *p = bb->page;
1788 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1789 if (bb->changed) {
1790 unsigned seq;
1791
1792retry:
1793 seq = read_seqbegin(&bb->lock);
1794
1795 memset(bbp, 0xff, PAGE_SIZE);
1796
1797 for (i = 0 ; i < bb->count ; i++) {
1798 u64 internal_bb = *p++;
1799 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1800 | BB_LEN(internal_bb));
1801 *bbp++ = cpu_to_le64(store_bb);
1802 }
1803 if (read_seqretry(&bb->lock, seq))
1804 goto retry;
1805
1806 bb->sector = (rdev->sb_start +
1807 (int)le32_to_cpu(sb->bblog_offset));
1808 bb->size = le16_to_cpu(sb->bblog_size);
1809 bb->changed = 0;
1810 }
1811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001814 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (rdev2->desc_nr+1 > max_dev)
1816 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001817
NeilBrown70471da2009-08-03 10:59:57 +10001818 if (max_dev > le32_to_cpu(sb->max_dev)) {
1819 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001820 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001821 rdev->sb_size = max_dev * 2 + 256;
1822 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1823 if (rdev->sb_size & bmask)
1824 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001825 } else
1826 max_dev = le32_to_cpu(sb->max_dev);
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 for (i=0; i<max_dev;i++)
1829 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1830
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001831 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001833 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001837 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001838 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 else
1840 sb->dev_roles[i] = cpu_to_le16(0xffff);
1841 }
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 sb->sb_csum = calc_sb_1_csum(sb);
1844}
1845
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001847super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001848{
1849 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001850 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001851 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001853 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001855 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001856 max_sectors -= rdev->data_offset;
1857 if (!num_sectors || num_sectors > max_sectors)
1858 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001859 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001860 /* minor version 0 with bitmap we can't move */
1861 return 0;
1862 } else {
1863 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001864 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001865 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001866 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001867 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001868 if (!num_sectors || num_sectors > max_sectors)
1869 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001870 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001871 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001872 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001873 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001874 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001876 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 rdev->sb_page);
1878 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001879 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001880}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Adrian Bunk75c96f82005-05-05 16:16:09 -07001882static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 [0] = {
1884 .name = "0.90.0",
1885 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001886 .load_super = super_90_load,
1887 .validate_super = super_90_validate,
1888 .sync_super = super_90_sync,
1889 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 },
1891 [1] = {
1892 .name = "md-1",
1893 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894 .load_super = super_1_load,
1895 .validate_super = super_1_validate,
1896 .sync_super = super_1_sync,
1897 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 },
1899};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
NeilBrownfd01b882011-10-11 16:47:53 +11001901static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001902{
1903 if (mddev->sync_super) {
1904 mddev->sync_super(mddev, rdev);
1905 return;
1906 }
1907
1908 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1909
1910 super_types[mddev->major_version].sync_super(mddev, rdev);
1911}
1912
NeilBrownfd01b882011-10-11 16:47:53 +11001913static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
NeilBrown3cb03002011-10-11 16:45:26 +11001915 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
NeilBrown4b809912008-07-21 17:05:25 +10001917 rcu_read_lock();
1918 rdev_for_each_rcu(rdev, mddev1)
1919 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001920 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001921 rdev2->bdev->bd_contains) {
1922 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001923 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001924 }
1925 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
1927}
1928
1929static LIST_HEAD(pending_raid_disks);
1930
Andre Nollac5e7112009-08-03 10:59:47 +10001931/*
1932 * Try to register data integrity profile for an mddev
1933 *
1934 * This is called when an array is started and after a disk has been kicked
1935 * from the array. It only succeeds if all working and active component devices
1936 * are integrity capable with matching profiles.
1937 */
NeilBrownfd01b882011-10-11 16:47:53 +11001938int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001939{
NeilBrown3cb03002011-10-11 16:45:26 +11001940 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001941
Andre Nollac5e7112009-08-03 10:59:47 +10001942 if (list_empty(&mddev->disks))
1943 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001944 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1945 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001946 list_for_each_entry(rdev, &mddev->disks, same_set) {
1947 /* skip spares and non-functional disks */
1948 if (test_bit(Faulty, &rdev->flags))
1949 continue;
1950 if (rdev->raid_disk < 0)
1951 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001952 if (!reference) {
1953 /* Use the first rdev as the reference */
1954 reference = rdev;
1955 continue;
1956 }
1957 /* does this rdev's profile match the reference profile? */
1958 if (blk_integrity_compare(reference->bdev->bd_disk,
1959 rdev->bdev->bd_disk) < 0)
1960 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001961 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001962 if (!reference || !bdev_get_integrity(reference->bdev))
1963 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001964 /*
1965 * All component devices are integrity capable and have matching
1966 * profiles, register the common profile for the md device.
1967 */
1968 if (blk_integrity_register(mddev->gendisk,
1969 bdev_get_integrity(reference->bdev)) != 0) {
1970 printk(KERN_ERR "md: failed to register integrity for %s\n",
1971 mdname(mddev));
1972 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001973 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001974 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1975 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1976 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1977 mdname(mddev));
1978 return -EINVAL;
1979 }
Andre Nollac5e7112009-08-03 10:59:47 +10001980 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001981}
Andre Nollac5e7112009-08-03 10:59:47 +10001982EXPORT_SYMBOL(md_integrity_register);
1983
1984/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001985void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001986{
1987 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1988 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1989
1990 if (!bi_mddev) /* nothing to do */
1991 return;
1992 if (rdev->raid_disk < 0) /* skip spares */
1993 return;
1994 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1995 rdev->bdev->bd_disk) >= 0)
1996 return;
1997 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1998 blk_integrity_unregister(mddev->gendisk);
1999}
2000EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002001
NeilBrownfd01b882011-10-11 16:47:53 +11002002static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002004 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002005 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002006 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002007 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 if (rdev->mddev) {
2010 MD_BUG();
2011 return -EINVAL;
2012 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002013
2014 /* prevent duplicates */
2015 if (find_rdev(mddev, rdev->bdev->bd_dev))
2016 return -EEXIST;
2017
Andre Nolldd8ac332009-03-31 14:33:13 +11002018 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2019 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2020 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002021 if (mddev->pers) {
2022 /* Cannot change size, so fail
2023 * If mddev->level <= 0, then we don't care
2024 * about aligning sizes (e.g. linear)
2025 */
2026 if (mddev->level > 0)
2027 return -ENOSPC;
2028 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002029 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* Verify rdev->desc_nr is unique.
2033 * If it is -1, assign a free number, else
2034 * check number is not in use
2035 */
2036 if (rdev->desc_nr < 0) {
2037 int choice = 0;
2038 if (mddev->pers) choice = mddev->raid_disks;
2039 while (find_rdev_nr(mddev, choice))
2040 choice++;
2041 rdev->desc_nr = choice;
2042 } else {
2043 if (find_rdev_nr(mddev, rdev->desc_nr))
2044 return -EBUSY;
2045 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002046 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2047 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2048 mdname(mddev), mddev->max_disks);
2049 return -EBUSY;
2050 }
NeilBrown19133a42005-11-08 21:39:35 -08002051 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002052 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002053 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002056 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002057
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002058 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002059 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002060
Tejun Heo0762b8b2008-08-25 19:56:12 +09002061 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002062 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2063 /* failure here is OK */;
2064 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002065
NeilBrown4b809912008-07-21 17:05:25 +10002066 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002067 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002068
2069 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002070 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002073
2074 fail:
2075 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2076 b, mdname(mddev));
2077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
NeilBrown177a99b2008-02-06 01:39:56 -08002080static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002081{
NeilBrown3cb03002011-10-11 16:45:26 +11002082 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002083 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002084 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002085}
2086
NeilBrown3cb03002011-10-11 16:45:26 +11002087static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 char b[BDEVNAME_SIZE];
2090 if (!rdev->mddev) {
2091 MD_BUG();
2092 return;
2093 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002094 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002095 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2097 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002098 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002099 sysfs_put(rdev->sysfs_state);
2100 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002101 kfree(rdev->badblocks.page);
2102 rdev->badblocks.count = 0;
2103 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002104 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002105 * writing to 'remove' to "dev/state". We also need
2106 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002107 */
NeilBrown4b809912008-07-21 17:05:25 +10002108 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002109 INIT_WORK(&rdev->del_work, md_delayed_delete);
2110 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002111 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114/*
2115 * prevent the device from being mounted, repartitioned or
2116 * otherwise reused by a RAID array (or any other kernel
2117 * subsystem), by bd_claiming the device.
2118 */
NeilBrown3cb03002011-10-11 16:45:26 +11002119static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 int err = 0;
2122 struct block_device *bdev;
2123 char b[BDEVNAME_SIZE];
2124
Tejun Heod4d77622010-11-13 11:55:18 +01002125 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002126 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (IS_ERR(bdev)) {
2128 printk(KERN_ERR "md: could not open %s.\n",
2129 __bdevname(dev, b));
2130 return PTR_ERR(bdev);
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 rdev->bdev = bdev;
2133 return err;
2134}
2135
NeilBrown3cb03002011-10-11 16:45:26 +11002136static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 struct block_device *bdev = rdev->bdev;
2139 rdev->bdev = NULL;
2140 if (!bdev)
2141 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002142 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145void md_autodetect_dev(dev_t dev);
2146
NeilBrown3cb03002011-10-11 16:45:26 +11002147static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 char b[BDEVNAME_SIZE];
2150 printk(KERN_INFO "md: export_rdev(%s)\n",
2151 bdevname(rdev->bdev,b));
2152 if (rdev->mddev)
2153 MD_BUG();
2154 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002156 if (test_bit(AutoDetected, &rdev->flags))
2157 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158#endif
2159 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002160 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
NeilBrown3cb03002011-10-11 16:45:26 +11002163static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 unbind_rdev_from_array(rdev);
2166 export_rdev(rdev);
2167}
2168
NeilBrownfd01b882011-10-11 16:47:53 +11002169static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
NeilBrown3cb03002011-10-11 16:45:26 +11002171 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
NeilBrownd089c6a2008-02-06 01:39:59 -08002173 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!rdev->mddev) {
2175 MD_BUG();
2176 continue;
2177 }
2178 kick_rdev_from_array(rdev);
2179 }
2180 if (!list_empty(&mddev->disks))
2181 MD_BUG();
2182 mddev->raid_disks = 0;
2183 mddev->major_version = 0;
2184}
2185
2186static void print_desc(mdp_disk_t *desc)
2187{
2188 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2189 desc->major,desc->minor,desc->raid_disk,desc->state);
2190}
2191
Cheng Renquancd2ac932009-01-09 08:31:08 +11002192static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int i;
2195
2196 printk(KERN_INFO
2197 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2198 sb->major_version, sb->minor_version, sb->patch_version,
2199 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2200 sb->ctime);
2201 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2202 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2203 sb->md_minor, sb->layout, sb->chunk_size);
2204 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2205 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2206 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2207 sb->failed_disks, sb->spare_disks,
2208 sb->sb_csum, (unsigned long)sb->events_lo);
2209
2210 printk(KERN_INFO);
2211 for (i = 0; i < MD_SB_DISKS; i++) {
2212 mdp_disk_t *desc;
2213
2214 desc = sb->disks + i;
2215 if (desc->number || desc->major || desc->minor ||
2216 desc->raid_disk || (desc->state && (desc->state != 4))) {
2217 printk(" D %2d: ", i);
2218 print_desc(desc);
2219 }
2220 }
2221 printk(KERN_INFO "md: THIS: ");
2222 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Cheng Renquancd2ac932009-01-09 08:31:08 +11002225static void print_sb_1(struct mdp_superblock_1 *sb)
2226{
2227 __u8 *uuid;
2228
2229 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002230 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002231 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002232 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002233 le32_to_cpu(sb->major_version),
2234 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002235 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002236 sb->set_name,
2237 (unsigned long long)le64_to_cpu(sb->ctime)
2238 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2239
2240 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002241 printk(KERN_INFO
2242 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002243 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002244 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002245 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2246 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002247 le32_to_cpu(sb->level),
2248 (unsigned long long)le64_to_cpu(sb->size),
2249 le32_to_cpu(sb->raid_disks),
2250 le32_to_cpu(sb->layout),
2251 le32_to_cpu(sb->chunksize),
2252 (unsigned long long)le64_to_cpu(sb->data_offset),
2253 (unsigned long long)le64_to_cpu(sb->data_size),
2254 (unsigned long long)le64_to_cpu(sb->super_offset),
2255 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2256 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002257 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002258 sb->devflags,
2259 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2260 (unsigned long long)le64_to_cpu(sb->events),
2261 (unsigned long long)le64_to_cpu(sb->resync_offset),
2262 le32_to_cpu(sb->sb_csum),
2263 le32_to_cpu(sb->max_dev)
2264 );
2265}
2266
NeilBrown3cb03002011-10-11 16:45:26 +11002267static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002270 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2271 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002272 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2273 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002275 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2276 switch (major_version) {
2277 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002278 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002279 break;
2280 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002281 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002282 break;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 } else
2285 printk(KERN_INFO "md: no rdev superblock!\n");
2286}
2287
Adrian Bunk5e563412006-06-26 00:27:42 -07002288static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002290 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002291 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002292 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 char b[BDEVNAME_SIZE];
2294
2295 printk("\n");
2296 printk("md: **********************************\n");
2297 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2298 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002299 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
NeilBrown32a76272005-06-21 17:17:14 -07002301 if (mddev->bitmap)
2302 bitmap_print_sb(mddev->bitmap);
2303 else
2304 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002305 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 printk("<%s>", bdevname(rdev->bdev,b));
2307 printk("\n");
2308
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002309 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002310 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 printk("md: **********************************\n");
2313 printk("\n");
2314}
2315
2316
NeilBrownfd01b882011-10-11 16:47:53 +11002317static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
NeilBrown42543762006-06-26 00:27:57 -07002319 /* Update each superblock (in-memory image), but
2320 * if we are allowed to, skip spares which already
2321 * have the right event counter, or have one earlier
2322 * (which would mean they aren't being marked as dirty
2323 * with the rest of the array)
2324 */
NeilBrown3cb03002011-10-11 16:45:26 +11002325 struct md_rdev *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002326 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002327 if (rdev->sb_events == mddev->events ||
2328 (nospares &&
2329 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002330 rdev->sb_events+1 == mddev->events)) {
2331 /* Don't update this superblock */
2332 rdev->sb_loaded = 2;
2333 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002334 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002335 rdev->sb_loaded = 1;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338}
2339
NeilBrownfd01b882011-10-11 16:47:53 +11002340static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
NeilBrown3cb03002011-10-11 16:45:26 +11002342 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002343 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002344 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002345 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002348 /* First make sure individual recovery_offsets are correct */
2349 list_for_each_entry(rdev, &mddev->disks, same_set) {
2350 if (rdev->raid_disk >= 0 &&
2351 mddev->delta_disks >= 0 &&
2352 !test_bit(In_sync, &rdev->flags) &&
2353 mddev->curr_resync_completed > rdev->recovery_offset)
2354 rdev->recovery_offset = mddev->curr_resync_completed;
2355
2356 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002357 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002358 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002359 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002360 if (!mddev->external) {
NeilBrownd97a41dc2010-10-28 17:30:20 +11002361 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002362 list_for_each_entry(rdev, &mddev->disks, same_set) {
2363 if (rdev->badblocks.changed) {
2364 md_ack_all_badblocks(&rdev->badblocks);
2365 md_error(mddev, rdev);
2366 }
2367 clear_bit(Blocked, &rdev->flags);
2368 clear_bit(BlockedBadBlocks, &rdev->flags);
2369 wake_up(&rdev->blocked_wait);
2370 }
2371 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002372 wake_up(&mddev->sb_wait);
2373 return;
2374 }
2375
NeilBrowna9701a32005-11-08 21:39:34 -08002376 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002377
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002378 mddev->utime = get_seconds();
2379
NeilBrown850b2b42006-10-03 01:15:46 -07002380 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2381 force_change = 1;
2382 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2383 /* just a clean<-> dirty transition, possibly leave spares alone,
2384 * though if events isn't the right even/odd, we will have to do
2385 * spares after all
2386 */
2387 nospares = 1;
2388 if (force_change)
2389 nospares = 0;
2390 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002391 /* If the array is degraded, then skipping spares is both
2392 * dangerous and fairly pointless.
2393 * Dangerous because a device that was removed from the array
2394 * might have a event_count that still looks up-to-date,
2395 * so it can be re-added without a resync.
2396 * Pointless because if there are any spares to skip,
2397 * then a recovery will happen and soon that array won't
2398 * be degraded any more and the spare can go back to sleep then.
2399 */
NeilBrown850b2b42006-10-03 01:15:46 -07002400 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002401
NeilBrown06d91a52005-06-21 17:17:12 -07002402 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002403
2404 /* If this is just a dirty<->clean transition, and the array is clean
2405 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002406 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002407 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002408 && mddev->can_decrease_events
2409 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002410 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002411 mddev->can_decrease_events = 0;
2412 } else {
NeilBrown42543762006-06-26 00:27:57 -07002413 /* otherwise we have to go forward and ... */
2414 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002415 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 if (!mddev->events) {
2419 /*
2420 * oops, this 64-bit counter should never wrap.
2421 * Either we are in around ~1 trillion A.C., assuming
2422 * 1 reboot per second, or we have a bug:
2423 */
2424 MD_BUG();
2425 mddev->events --;
2426 }
NeilBrown2699b672011-07-28 11:31:47 +10002427
NeilBrownde393cd2011-07-28 11:31:48 +10002428 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002429 if (rdev->badblocks.changed)
2430 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002431 if (test_bit(Faulty, &rdev->flags))
2432 set_bit(FaultRecorded, &rdev->flags);
2433 }
NeilBrown2699b672011-07-28 11:31:47 +10002434
NeilBrowne6910632008-02-06 01:39:51 -08002435 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002436 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
NeilBrown36a4e1f2011-10-07 14:23:17 +11002438 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2439 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
NeilBrown4ad13662007-07-17 04:06:13 -07002441 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002442 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002444
NeilBrown42543762006-06-26 00:27:57 -07002445 if (rdev->sb_loaded != 1)
2446 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002448 if (!test_bit(Faulty, &rdev->flags) &&
2449 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002450 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002451 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002452 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002453 pr_debug("md: (write) %s's sb offset: %llu\n",
2454 bdevname(rdev->bdev, b),
2455 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002456 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002457 if (rdev->badblocks.size) {
2458 md_super_write(mddev, rdev,
2459 rdev->badblocks.sector,
2460 rdev->badblocks.size << 9,
2461 rdev->bb_page);
2462 rdev->badblocks.size = 0;
2463 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002464
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002465 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002466 pr_debug("md: %s (skipping faulty)\n",
2467 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002468 else
2469 pr_debug("(skipping incremental s/r ");
2470
NeilBrown7bfa19f2005-06-21 17:17:28 -07002471 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* only need to write one superblock... */
2473 break;
2474 }
NeilBrowna9701a32005-11-08 21:39:34 -08002475 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002476 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002477
NeilBrowna9701a32005-11-08 21:39:34 -08002478 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002479 if (mddev->in_sync != sync_req ||
2480 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002481 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002482 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002483 goto repeat;
2484 }
NeilBrown850b2b42006-10-03 01:15:46 -07002485 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002486 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002487 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002488 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2489 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002490
NeilBrownde393cd2011-07-28 11:31:48 +10002491 list_for_each_entry(rdev, &mddev->disks, same_set) {
2492 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2493 clear_bit(Blocked, &rdev->flags);
2494
2495 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002496 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002497 clear_bit(BlockedBadBlocks, &rdev->flags);
2498 wake_up(&rdev->blocked_wait);
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Andre Noll7f6ce762008-03-23 18:34:54 +01002502/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002503 * We want to accept with case. For this we use cmd_match.
2504 */
2505static int cmd_match(const char *cmd, const char *str)
2506{
2507 /* See if cmd, written into a sysfs file, matches
2508 * str. They must either be the same, or cmd can
2509 * have a trailing newline
2510 */
2511 while (*cmd && *str && *cmd == *str) {
2512 cmd++;
2513 str++;
2514 }
2515 if (*cmd == '\n')
2516 cmd++;
2517 if (*str || *cmd)
2518 return 0;
2519 return 1;
2520}
2521
NeilBrown86e6ffd2005-11-08 21:39:24 -08002522struct rdev_sysfs_entry {
2523 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002524 ssize_t (*show)(struct md_rdev *, char *);
2525 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002526};
2527
2528static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002529state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002530{
2531 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002532 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002533
NeilBrownde393cd2011-07-28 11:31:48 +10002534 if (test_bit(Faulty, &rdev->flags) ||
2535 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002536 len+= sprintf(page+len, "%sfaulty",sep);
2537 sep = ",";
2538 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002539 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002540 len += sprintf(page+len, "%sin_sync",sep);
2541 sep = ",";
2542 }
NeilBrownf6556752006-06-26 00:28:01 -07002543 if (test_bit(WriteMostly, &rdev->flags)) {
2544 len += sprintf(page+len, "%swrite_mostly",sep);
2545 sep = ",";
2546 }
NeilBrownde393cd2011-07-28 11:31:48 +10002547 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002548 (rdev->badblocks.unacked_exist
2549 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002550 len += sprintf(page+len, "%sblocked", sep);
2551 sep = ",";
2552 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002553 if (!test_bit(Faulty, &rdev->flags) &&
2554 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555 len += sprintf(page+len, "%sspare", sep);
2556 sep = ",";
2557 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002558 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2559 len += sprintf(page+len, "%swrite_error", sep);
2560 sep = ",";
2561 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002562 return len+sprintf(page+len, "\n");
2563}
2564
NeilBrown45dc2de2006-06-26 00:27:58 -07002565static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002566state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002567{
2568 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002569 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002570 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002571 * writemostly - sets write_mostly
2572 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002573 * blocked - sets the Blocked flags
2574 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002575 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002576 * write_error - sets WriteErrorSeen
2577 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002578 */
2579 int err = -EINVAL;
2580 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2581 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002582 if (test_bit(Faulty, &rdev->flags))
2583 err = 0;
2584 else
2585 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002586 } else if (cmd_match(buf, "remove")) {
2587 if (rdev->raid_disk >= 0)
2588 err = -EBUSY;
2589 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002590 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002591 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002592 if (mddev->pers)
2593 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002594 md_new_event(mddev);
2595 err = 0;
2596 }
NeilBrownf6556752006-06-26 00:28:01 -07002597 } else if (cmd_match(buf, "writemostly")) {
2598 set_bit(WriteMostly, &rdev->flags);
2599 err = 0;
2600 } else if (cmd_match(buf, "-writemostly")) {
2601 clear_bit(WriteMostly, &rdev->flags);
2602 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002603 } else if (cmd_match(buf, "blocked")) {
2604 set_bit(Blocked, &rdev->flags);
2605 err = 0;
2606 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002607 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002608 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002609 /* metadata handler doesn't understand badblocks,
2610 * so we need to fail the device
2611 */
2612 md_error(rdev->mddev, rdev);
2613 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002614 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002615 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002616 wake_up(&rdev->blocked_wait);
2617 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2618 md_wakeup_thread(rdev->mddev->thread);
2619
2620 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002621 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2622 set_bit(In_sync, &rdev->flags);
2623 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002624 } else if (cmd_match(buf, "write_error")) {
2625 set_bit(WriteErrorSeen, &rdev->flags);
2626 err = 0;
2627 } else if (cmd_match(buf, "-write_error")) {
2628 clear_bit(WriteErrorSeen, &rdev->flags);
2629 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002630 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002631 if (!err)
2632 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002633 return err ? err : len;
2634}
NeilBrown80ca3a42006-07-10 04:44:18 -07002635static struct rdev_sysfs_entry rdev_state =
2636__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002637
2638static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002639errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002640{
2641 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2642}
2643
2644static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002645errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002646{
2647 char *e;
2648 unsigned long n = simple_strtoul(buf, &e, 10);
2649 if (*buf && (*e == 0 || *e == '\n')) {
2650 atomic_set(&rdev->corrected_errors, n);
2651 return len;
2652 }
2653 return -EINVAL;
2654}
2655static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002656__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002657
NeilBrown014236d2006-01-06 00:20:55 -08002658static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002659slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002660{
2661 if (rdev->raid_disk < 0)
2662 return sprintf(page, "none\n");
2663 else
2664 return sprintf(page, "%d\n", rdev->raid_disk);
2665}
2666
2667static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002668slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002669{
2670 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002671 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002672 int slot = simple_strtoul(buf, &e, 10);
2673 if (strncmp(buf, "none", 4)==0)
2674 slot = -1;
2675 else if (e==buf || (*e && *e!= '\n'))
2676 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002677 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002678 /* Setting 'slot' on an active array requires also
2679 * updating the 'rd%d' link, and communicating
2680 * with the personality with ->hot_*_disk.
2681 * For now we only support removing
2682 * failed/spare devices. This normally happens automatically,
2683 * but not when the metadata is externally managed.
2684 */
NeilBrownc303da62008-02-06 01:39:51 -08002685 if (rdev->raid_disk == -1)
2686 return -EEXIST;
2687 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002688 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002689 return -EINVAL;
2690 err = rdev->mddev->pers->
2691 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2692 if (err)
2693 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002694 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002695 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002696 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2697 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002698 } else if (rdev->mddev->pers) {
NeilBrown3cb03002011-10-11 16:45:26 +11002699 struct md_rdev *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002700 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002701 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002702 */
2703
2704 if (rdev->raid_disk != -1)
2705 return -EBUSY;
2706
NeilBrownc6751b22011-02-02 11:57:13 +11002707 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2708 return -EBUSY;
2709
Neil Brown6c2fce22008-06-28 08:31:31 +10002710 if (rdev->mddev->pers->hot_add_disk == NULL)
2711 return -EINVAL;
2712
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002713 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002714 if (rdev2->raid_disk == slot)
2715 return -EEXIST;
2716
NeilBrownba1b41b2011-01-14 09:14:34 +11002717 if (slot >= rdev->mddev->raid_disks &&
2718 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2719 return -ENOSPC;
2720
Neil Brown6c2fce22008-06-28 08:31:31 +10002721 rdev->raid_disk = slot;
2722 if (test_bit(In_sync, &rdev->flags))
2723 rdev->saved_raid_disk = slot;
2724 else
2725 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002726 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002727 err = rdev->mddev->pers->
2728 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002729 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002730 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002731 return err;
Neil Brown52664732008-06-28 08:31:44 +10002732 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002733 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002734 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002735 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002736 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002737 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002738 if (slot >= rdev->mddev->raid_disks &&
2739 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002740 return -ENOSPC;
2741 rdev->raid_disk = slot;
2742 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002743 clear_bit(Faulty, &rdev->flags);
2744 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002745 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002746 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002747 }
NeilBrown014236d2006-01-06 00:20:55 -08002748 return len;
2749}
2750
2751
2752static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002753__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002754
NeilBrown93c8cad2006-01-06 00:20:56 -08002755static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002756offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002757{
Andrew Morton6961ece2006-01-06 00:20:59 -08002758 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002759}
2760
2761static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002762offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002763{
2764 char *e;
2765 unsigned long long offset = simple_strtoull(buf, &e, 10);
2766 if (e==buf || (*e && *e != '\n'))
2767 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002768 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002769 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002770 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002771 /* Must set offset before size, so overlap checks
2772 * can be sane */
2773 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002774 rdev->data_offset = offset;
2775 return len;
2776}
2777
2778static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002779__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002780
NeilBrown83303b62006-01-06 00:21:06 -08002781static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002782rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002783{
Andre Nolldd8ac332009-03-31 14:33:13 +11002784 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002785}
2786
NeilBrownc5d79ad2008-02-06 01:39:54 -08002787static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2788{
2789 /* check if two start/length pairs overlap */
2790 if (s1+l1 <= s2)
2791 return 0;
2792 if (s2+l2 <= s1)
2793 return 0;
2794 return 1;
2795}
2796
Dan Williamsb522adc2009-03-31 15:00:31 +11002797static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2798{
2799 unsigned long long blocks;
2800 sector_t new;
2801
2802 if (strict_strtoull(buf, 10, &blocks) < 0)
2803 return -EINVAL;
2804
2805 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2806 return -EINVAL; /* sector conversion overflow */
2807
2808 new = blocks * 2;
2809 if (new != blocks * 2)
2810 return -EINVAL; /* unsigned long long to sector_t overflow */
2811
2812 *sectors = new;
2813 return 0;
2814}
2815
NeilBrown83303b62006-01-06 00:21:06 -08002816static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002817rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002818{
NeilBrownfd01b882011-10-11 16:47:53 +11002819 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002820 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002821 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002822
Dan Williamsb522adc2009-03-31 15:00:31 +11002823 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002824 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002825 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002826 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002827 sectors = super_types[my_mddev->major_version].
2828 rdev_size_change(rdev, sectors);
2829 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002830 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002831 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002832 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002834 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002835 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002836 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002837
Andre Nolldd8ac332009-03-31 14:33:13 +11002838 rdev->sectors = sectors;
2839 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002840 /* need to check that all other rdevs with the same ->bdev
2841 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002842 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002843 * we have to change it back, we will have the lock again.
2844 */
NeilBrownfd01b882011-10-11 16:47:53 +11002845 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002847 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848
NeilBrown27c529b2008-03-04 14:29:33 -08002849 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002850 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002851 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002852
2853 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002854 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002855 if (rdev->bdev == rdev2->bdev &&
2856 rdev != rdev2 &&
2857 overlaps(rdev->data_offset, rdev->sectors,
2858 rdev2->data_offset,
2859 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002860 overlap = 1;
2861 break;
2862 }
2863 mddev_unlock(mddev);
2864 if (overlap) {
2865 mddev_put(mddev);
2866 break;
2867 }
2868 }
NeilBrown27c529b2008-03-04 14:29:33 -08002869 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002870 if (overlap) {
2871 /* Someone else could have slipped in a size
2872 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002873 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 * safe, and trust userspace not to race with
2875 * itself
2876 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002877 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002878 return -EBUSY;
2879 }
2880 }
NeilBrown83303b62006-01-06 00:21:06 -08002881 return len;
2882}
2883
2884static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002885__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002886
Dan Williams06e3c812009-12-12 21:17:12 -07002887
NeilBrown3cb03002011-10-11 16:45:26 +11002888static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002889{
2890 unsigned long long recovery_start = rdev->recovery_offset;
2891
2892 if (test_bit(In_sync, &rdev->flags) ||
2893 recovery_start == MaxSector)
2894 return sprintf(page, "none\n");
2895
2896 return sprintf(page, "%llu\n", recovery_start);
2897}
2898
NeilBrown3cb03002011-10-11 16:45:26 +11002899static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002900{
2901 unsigned long long recovery_start;
2902
2903 if (cmd_match(buf, "none"))
2904 recovery_start = MaxSector;
2905 else if (strict_strtoull(buf, 10, &recovery_start))
2906 return -EINVAL;
2907
2908 if (rdev->mddev->pers &&
2909 rdev->raid_disk >= 0)
2910 return -EBUSY;
2911
2912 rdev->recovery_offset = recovery_start;
2913 if (recovery_start == MaxSector)
2914 set_bit(In_sync, &rdev->flags);
2915 else
2916 clear_bit(In_sync, &rdev->flags);
2917 return len;
2918}
2919
2920static struct rdev_sysfs_entry rdev_recovery_start =
2921__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2922
NeilBrown16c791a2011-07-28 11:31:47 +10002923
2924static ssize_t
2925badblocks_show(struct badblocks *bb, char *page, int unack);
2926static ssize_t
2927badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2928
NeilBrown3cb03002011-10-11 16:45:26 +11002929static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002930{
2931 return badblocks_show(&rdev->badblocks, page, 0);
2932}
NeilBrown3cb03002011-10-11 16:45:26 +11002933static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002934{
NeilBrownde393cd2011-07-28 11:31:48 +10002935 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2936 /* Maybe that ack was all we needed */
2937 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2938 wake_up(&rdev->blocked_wait);
2939 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002940}
2941static struct rdev_sysfs_entry rdev_bad_blocks =
2942__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2943
2944
NeilBrown3cb03002011-10-11 16:45:26 +11002945static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002946{
2947 return badblocks_show(&rdev->badblocks, page, 1);
2948}
NeilBrown3cb03002011-10-11 16:45:26 +11002949static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002950{
2951 return badblocks_store(&rdev->badblocks, page, len, 1);
2952}
2953static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2954__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2955
NeilBrown86e6ffd2005-11-08 21:39:24 -08002956static struct attribute *rdev_default_attrs[] = {
2957 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002958 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002959 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002960 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002961 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002962 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002963 &rdev_bad_blocks.attr,
2964 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002965 NULL,
2966};
2967static ssize_t
2968rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2969{
2970 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002971 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002972 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002973 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002974
2975 if (!entry->show)
2976 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002977
2978 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2979 if (!rv) {
2980 if (rdev->mddev == NULL)
2981 rv = -EBUSY;
2982 else
2983 rv = entry->show(rdev, page);
2984 mddev_unlock(mddev);
2985 }
2986 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002987}
2988
2989static ssize_t
2990rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2991 const char *page, size_t length)
2992{
2993 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002994 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002995 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002996 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002997
2998 if (!entry->store)
2999 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003000 if (!capable(CAP_SYS_ADMIN))
3001 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003002 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003003 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003004 if (rdev->mddev == NULL)
3005 rv = -EBUSY;
3006 else
3007 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003008 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003009 }
3010 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003011}
3012
3013static void rdev_free(struct kobject *ko)
3014{
NeilBrown3cb03002011-10-11 16:45:26 +11003015 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003016 kfree(rdev);
3017}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003018static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003019 .show = rdev_attr_show,
3020 .store = rdev_attr_store,
3021};
3022static struct kobj_type rdev_ktype = {
3023 .release = rdev_free,
3024 .sysfs_ops = &rdev_sysfs_ops,
3025 .default_attrs = rdev_default_attrs,
3026};
3027
NeilBrown3cb03002011-10-11 16:45:26 +11003028int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003029{
3030 rdev->desc_nr = -1;
3031 rdev->saved_raid_disk = -1;
3032 rdev->raid_disk = -1;
3033 rdev->flags = 0;
3034 rdev->data_offset = 0;
3035 rdev->sb_events = 0;
3036 rdev->last_read_error.tv_sec = 0;
3037 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003038 rdev->sb_loaded = 0;
3039 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003040 atomic_set(&rdev->nr_pending, 0);
3041 atomic_set(&rdev->read_errors, 0);
3042 atomic_set(&rdev->corrected_errors, 0);
3043
3044 INIT_LIST_HEAD(&rdev->same_set);
3045 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003046
3047 /* Add space to store bad block list.
3048 * This reserves the space even on arrays where it cannot
3049 * be used - I wonder if that matters
3050 */
3051 rdev->badblocks.count = 0;
3052 rdev->badblocks.shift = 0;
3053 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3054 seqlock_init(&rdev->badblocks.lock);
3055 if (rdev->badblocks.page == NULL)
3056 return -ENOMEM;
3057
3058 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003059}
3060EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061/*
3062 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3063 *
3064 * mark the device faulty if:
3065 *
3066 * - the device is nonexistent (zero size)
3067 * - the device has no valid superblock
3068 *
3069 * a faulty rdev _never_ has rdev->sb set.
3070 */
NeilBrown3cb03002011-10-11 16:45:26 +11003071static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 char b[BDEVNAME_SIZE];
3074 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003075 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 sector_t size;
3077
NeilBrown9ffae0c2006-01-06 00:20:32 -08003078 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (!rdev) {
3080 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3081 return ERR_PTR(-ENOMEM);
3082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
NeilBrown2230dfe2011-07-28 11:31:46 +10003084 err = md_rdev_init(rdev);
3085 if (err)
3086 goto abort_free;
3087 err = alloc_disk_sb(rdev);
3088 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 goto abort_free;
3090
NeilBrownc5d79ad2008-02-06 01:39:54 -08003091 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (err)
3093 goto abort_free;
3094
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003095 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003096
Mike Snitzer77304d22010-11-08 14:39:12 +01003097 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (!size) {
3099 printk(KERN_WARNING
3100 "md: %s has zero or unknown size, marking faulty!\n",
3101 bdevname(rdev->bdev,b));
3102 err = -EINVAL;
3103 goto abort_free;
3104 }
3105
3106 if (super_format >= 0) {
3107 err = super_types[super_format].
3108 load_super(rdev, NULL, super_minor);
3109 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003110 printk(KERN_WARNING
3111 "md: %s does not have a valid v%d.%d "
3112 "superblock, not importing!\n",
3113 bdevname(rdev->bdev,b),
3114 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 goto abort_free;
3116 }
3117 if (err < 0) {
3118 printk(KERN_WARNING
3119 "md: could not read %s's sb, not importing!\n",
3120 bdevname(rdev->bdev,b));
3121 goto abort_free;
3122 }
3123 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003124 if (super_format == -1)
3125 /* hot-add for 0.90, or non-persistent: so no badblocks */
3126 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return rdev;
3129
3130abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003131 if (rdev->bdev)
3132 unlock_rdev(rdev);
3133 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003134 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 kfree(rdev);
3136 return ERR_PTR(err);
3137}
3138
3139/*
3140 * Check a full RAID array for plausibility
3141 */
3142
3143
NeilBrownfd01b882011-10-11 16:47:53 +11003144static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
3146 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003147 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 char b[BDEVNAME_SIZE];
3149
3150 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003151 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 switch (super_types[mddev->major_version].
3153 load_super(rdev, freshest, mddev->minor_version)) {
3154 case 1:
3155 freshest = rdev;
3156 break;
3157 case 0:
3158 break;
3159 default:
3160 printk( KERN_ERR \
3161 "md: fatal superblock inconsistency in %s"
3162 " -- removing from array\n",
3163 bdevname(rdev->bdev,b));
3164 kick_rdev_from_array(rdev);
3165 }
3166
3167
3168 super_types[mddev->major_version].
3169 validate_super(mddev, freshest);
3170
3171 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003172 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003173 if (mddev->max_disks &&
3174 (rdev->desc_nr >= mddev->max_disks ||
3175 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003176 printk(KERN_WARNING
3177 "md: %s: %s: only %d devices permitted\n",
3178 mdname(mddev), bdevname(rdev->bdev, b),
3179 mddev->max_disks);
3180 kick_rdev_from_array(rdev);
3181 continue;
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (rdev != freshest)
3184 if (super_types[mddev->major_version].
3185 validate_super(mddev, rdev)) {
3186 printk(KERN_WARNING "md: kicking non-fresh %s"
3187 " from array!\n",
3188 bdevname(rdev->bdev,b));
3189 kick_rdev_from_array(rdev);
3190 continue;
3191 }
3192 if (mddev->level == LEVEL_MULTIPATH) {
3193 rdev->desc_nr = i++;
3194 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003195 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003196 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003197 rdev->raid_disk = -1;
3198 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
NeilBrown72e02072009-12-14 12:49:55 +11003203/* Read a fixed-point number.
3204 * Numbers in sysfs attributes should be in "standard" units where
3205 * possible, so time should be in seconds.
3206 * However we internally use a a much smaller unit such as
3207 * milliseconds or jiffies.
3208 * This function takes a decimal number with a possible fractional
3209 * component, and produces an integer which is the result of
3210 * multiplying that number by 10^'scale'.
3211 * all without any floating-point arithmetic.
3212 */
3213int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3214{
3215 unsigned long result = 0;
3216 long decimals = -1;
3217 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3218 if (*cp == '.')
3219 decimals = 0;
3220 else if (decimals < scale) {
3221 unsigned int value;
3222 value = *cp - '0';
3223 result = result * 10 + value;
3224 if (decimals >= 0)
3225 decimals++;
3226 }
3227 cp++;
3228 }
3229 if (*cp == '\n')
3230 cp++;
3231 if (*cp)
3232 return -EINVAL;
3233 if (decimals < 0)
3234 decimals = 0;
3235 while (decimals < scale) {
3236 result *= 10;
3237 decimals ++;
3238 }
3239 *res = result;
3240 return 0;
3241}
3242
3243
NeilBrown19052c02008-08-05 15:54:13 +10003244static void md_safemode_timeout(unsigned long data);
3245
NeilBrowneae17012005-11-08 21:39:23 -08003246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003247safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003248{
3249 int msec = (mddev->safemode_delay*1000)/HZ;
3250 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3251}
3252static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003253safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003254{
NeilBrown16f17b32006-06-26 00:27:37 -07003255 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003256
NeilBrown72e02072009-12-14 12:49:55 +11003257 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003258 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003259 if (msec == 0)
3260 mddev->safemode_delay = 0;
3261 else {
NeilBrown19052c02008-08-05 15:54:13 +10003262 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003263 mddev->safemode_delay = (msec*HZ)/1000;
3264 if (mddev->safemode_delay == 0)
3265 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003266 if (mddev->safemode_delay < old_delay)
3267 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003268 }
3269 return len;
3270}
3271static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003272__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003273
3274static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003275level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003276{
NeilBrown84fc4b52011-10-11 16:49:58 +11003277 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003278 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003279 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003280 else if (mddev->clevel[0])
3281 return sprintf(page, "%s\n", mddev->clevel);
3282 else if (mddev->level != LEVEL_NONE)
3283 return sprintf(page, "%d\n", mddev->level);
3284 else
3285 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003286}
3287
NeilBrownd9d166c2006-01-06 00:20:51 -08003288static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003289level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003290{
Dan Williamsf2859af2010-05-02 10:04:16 -07003291 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003292 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003293 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003294 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003295 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003296 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003297
3298 if (mddev->pers == NULL) {
3299 if (len == 0)
3300 return 0;
3301 if (len >= sizeof(mddev->clevel))
3302 return -ENOSPC;
3303 strncpy(mddev->clevel, buf, len);
3304 if (mddev->clevel[len-1] == '\n')
3305 len--;
3306 mddev->clevel[len] = 0;
3307 mddev->level = LEVEL_NONE;
3308 return rv;
3309 }
3310
3311 /* request to change the personality. Need to ensure:
3312 * - array is not engaged in resync/recovery/reshape
3313 * - old personality can be suspended
3314 * - new personality will access other array.
3315 */
3316
NeilBrownbb4f1e92010-08-08 21:18:03 +10003317 if (mddev->sync_thread ||
3318 mddev->reshape_position != MaxSector ||
3319 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003320 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003321
3322 if (!mddev->pers->quiesce) {
3323 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3324 mdname(mddev), mddev->pers->name);
3325 return -EINVAL;
3326 }
3327
3328 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003329 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003330 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003331 strncpy(clevel, buf, len);
3332 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003333 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003334 clevel[len] = 0;
3335 if (strict_strtol(clevel, 10, &level))
3336 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003337
Dan Williamsf2859af2010-05-02 10:04:16 -07003338 if (request_module("md-%s", clevel) != 0)
3339 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003340 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003341 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003342 if (!pers || !try_module_get(pers->owner)) {
3343 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003344 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003345 return -EINVAL;
3346 }
3347 spin_unlock(&pers_lock);
3348
3349 if (pers == mddev->pers) {
3350 /* Nothing to do! */
3351 module_put(pers->owner);
3352 return rv;
3353 }
3354 if (!pers->takeover) {
3355 module_put(pers->owner);
3356 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003357 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003358 return -EINVAL;
3359 }
3360
NeilBrowne93f68a2010-06-15 09:36:03 +01003361 list_for_each_entry(rdev, &mddev->disks, same_set)
3362 rdev->new_raid_disk = rdev->raid_disk;
3363
NeilBrown245f46c2009-03-31 14:39:39 +11003364 /* ->takeover must set new_* and/or delta_disks
3365 * if it succeeds, and may set them when it fails.
3366 */
3367 priv = pers->takeover(mddev);
3368 if (IS_ERR(priv)) {
3369 mddev->new_level = mddev->level;
3370 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003371 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003372 mddev->raid_disks -= mddev->delta_disks;
3373 mddev->delta_disks = 0;
3374 module_put(pers->owner);
3375 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003376 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003377 return PTR_ERR(priv);
3378 }
3379
3380 /* Looks like we have a winner */
3381 mddev_suspend(mddev);
3382 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003383
3384 if (mddev->pers->sync_request == NULL &&
3385 pers->sync_request != NULL) {
3386 /* need to add the md_redundancy_group */
3387 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3388 printk(KERN_WARNING
3389 "md: cannot register extra attributes for %s\n",
3390 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003391 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003392 }
3393 if (mddev->pers->sync_request != NULL &&
3394 pers->sync_request == NULL) {
3395 /* need to remove the md_redundancy_group */
3396 if (mddev->to_remove == NULL)
3397 mddev->to_remove = &md_redundancy_group;
3398 }
3399
Trela Maciej54071b32010-03-08 16:02:42 +11003400 if (mddev->pers->sync_request == NULL &&
3401 mddev->external) {
3402 /* We are converting from a no-redundancy array
3403 * to a redundancy array and metadata is managed
3404 * externally so we need to be sure that writes
3405 * won't block due to a need to transition
3406 * clean->dirty
3407 * until external management is started.
3408 */
3409 mddev->in_sync = 0;
3410 mddev->safemode_delay = 0;
3411 mddev->safemode = 0;
3412 }
3413
NeilBrowne93f68a2010-06-15 09:36:03 +01003414 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003415 if (rdev->raid_disk < 0)
3416 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003417 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003418 rdev->new_raid_disk = -1;
3419 if (rdev->new_raid_disk == rdev->raid_disk)
3420 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003421 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003422 }
3423 list_for_each_entry(rdev, &mddev->disks, same_set) {
3424 if (rdev->raid_disk < 0)
3425 continue;
3426 if (rdev->new_raid_disk == rdev->raid_disk)
3427 continue;
3428 rdev->raid_disk = rdev->new_raid_disk;
3429 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003430 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003431 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003432 if (sysfs_link_rdev(mddev, rdev))
3433 printk(KERN_WARNING "md: cannot register rd%d"
3434 " for %s after level change\n",
3435 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003436 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003437 }
3438
3439 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003440 mddev->pers = pers;
3441 mddev->private = priv;
3442 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3443 mddev->level = mddev->new_level;
3444 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003445 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003446 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003447 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003448 if (mddev->pers->sync_request == NULL) {
3449 /* this is now an array without redundancy, so
3450 * it must always be in_sync
3451 */
3452 mddev->in_sync = 1;
3453 del_timer_sync(&mddev->safemode_timer);
3454 }
NeilBrown245f46c2009-03-31 14:39:39 +11003455 pers->run(mddev);
3456 mddev_resume(mddev);
3457 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3458 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3459 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003460 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003461 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003462 return rv;
3463}
3464
3465static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003466__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003467
NeilBrownd4dbd022006-06-26 00:27:59 -07003468
3469static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003470layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003471{
3472 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003473 if (mddev->reshape_position != MaxSector &&
3474 mddev->layout != mddev->new_layout)
3475 return sprintf(page, "%d (%d)\n",
3476 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003477 return sprintf(page, "%d\n", mddev->layout);
3478}
3479
3480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003481layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003482{
3483 char *e;
3484 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003485
3486 if (!*buf || (*e && *e != '\n'))
3487 return -EINVAL;
3488
NeilBrownb3546032009-03-31 14:56:41 +11003489 if (mddev->pers) {
3490 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003491 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003492 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003493 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003494 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003495 if (err) {
3496 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003497 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003498 }
NeilBrownb3546032009-03-31 14:56:41 +11003499 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003500 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003501 if (mddev->reshape_position == MaxSector)
3502 mddev->layout = n;
3503 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003504 return len;
3505}
3506static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003507__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003508
3509
NeilBrowneae17012005-11-08 21:39:23 -08003510static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003511raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003512{
NeilBrownbb636542005-11-08 21:39:45 -08003513 if (mddev->raid_disks == 0)
3514 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003515 if (mddev->reshape_position != MaxSector &&
3516 mddev->delta_disks != 0)
3517 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3518 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003519 return sprintf(page, "%d\n", mddev->raid_disks);
3520}
3521
NeilBrownfd01b882011-10-11 16:47:53 +11003522static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003523
3524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003525raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003526{
NeilBrownda943b992006-01-06 00:20:54 -08003527 char *e;
3528 int rv = 0;
3529 unsigned long n = simple_strtoul(buf, &e, 10);
3530
3531 if (!*buf || (*e && *e != '\n'))
3532 return -EINVAL;
3533
3534 if (mddev->pers)
3535 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003536 else if (mddev->reshape_position != MaxSector) {
3537 int olddisks = mddev->raid_disks - mddev->delta_disks;
3538 mddev->delta_disks = n - olddisks;
3539 mddev->raid_disks = n;
3540 } else
NeilBrownda943b992006-01-06 00:20:54 -08003541 mddev->raid_disks = n;
3542 return rv ? rv : len;
3543}
3544static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003545__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003546
NeilBrown24dd4692005-11-08 21:39:26 -08003547static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003548chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003549{
NeilBrown08a02ec2007-05-09 02:35:38 -07003550 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003551 mddev->chunk_sectors != mddev->new_chunk_sectors)
3552 return sprintf(page, "%d (%d)\n",
3553 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003554 mddev->chunk_sectors << 9);
3555 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003556}
3557
3558static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003559chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003560{
NeilBrown3b343802006-01-06 00:20:47 -08003561 char *e;
3562 unsigned long n = simple_strtoul(buf, &e, 10);
3563
NeilBrown3b343802006-01-06 00:20:47 -08003564 if (!*buf || (*e && *e != '\n'))
3565 return -EINVAL;
3566
NeilBrownb3546032009-03-31 14:56:41 +11003567 if (mddev->pers) {
3568 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003569 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003570 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003571 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003572 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003573 if (err) {
3574 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003575 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003576 }
NeilBrownb3546032009-03-31 14:56:41 +11003577 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003578 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003579 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003580 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003581 }
NeilBrown3b343802006-01-06 00:20:47 -08003582 return len;
3583}
3584static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003585__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003586
NeilBrowna94213b2006-06-26 00:28:00 -07003587static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003588resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003589{
NeilBrownd1a7c502009-03-31 15:24:32 +11003590 if (mddev->recovery_cp == MaxSector)
3591 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003592 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3593}
3594
3595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003596resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003597{
NeilBrowna94213b2006-06-26 00:28:00 -07003598 char *e;
3599 unsigned long long n = simple_strtoull(buf, &e, 10);
3600
NeilBrownb0986362011-05-11 15:52:21 +10003601 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003602 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003603 if (cmd_match(buf, "none"))
3604 n = MaxSector;
3605 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003606 return -EINVAL;
3607
3608 mddev->recovery_cp = n;
3609 return len;
3610}
3611static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003612__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003613
NeilBrown9e653b62006-06-26 00:27:58 -07003614/*
3615 * The array state can be:
3616 *
3617 * clear
3618 * No devices, no size, no level
3619 * Equivalent to STOP_ARRAY ioctl
3620 * inactive
3621 * May have some settings, but array is not active
3622 * all IO results in error
3623 * When written, doesn't tear down array, but just stops it
3624 * suspended (not supported yet)
3625 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003626 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003627 * readonly
3628 * no resync can happen. no superblocks get written.
3629 * write requests fail
3630 * read-auto
3631 * like readonly, but behaves like 'clean' on a write request.
3632 *
3633 * clean - no pending writes, but otherwise active.
3634 * When written to inactive array, starts without resync
3635 * If a write request arrives then
3636 * if metadata is known, mark 'dirty' and switch to 'active'.
3637 * if not known, block and switch to write-pending
3638 * If written to an active array that has pending writes, then fails.
3639 * active
3640 * fully active: IO and resync can be happening.
3641 * When written to inactive array, starts with resync
3642 *
3643 * write-pending
3644 * clean, but writes are blocked waiting for 'active' to be written.
3645 *
3646 * active-idle
3647 * like active, but no writes have been seen for a while (100msec).
3648 *
3649 */
3650enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3651 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003652static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003653 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3654 "write-pending", "active-idle", NULL };
3655
3656static int match_word(const char *word, char **list)
3657{
3658 int n;
3659 for (n=0; list[n]; n++)
3660 if (cmd_match(word, list[n]))
3661 break;
3662 return n;
3663}
3664
3665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003666array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003667{
3668 enum array_state st = inactive;
3669
3670 if (mddev->pers)
3671 switch(mddev->ro) {
3672 case 1:
3673 st = readonly;
3674 break;
3675 case 2:
3676 st = read_auto;
3677 break;
3678 case 0:
3679 if (mddev->in_sync)
3680 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003681 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003682 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003683 else if (mddev->safemode)
3684 st = active_idle;
3685 else
3686 st = active;
3687 }
3688 else {
3689 if (list_empty(&mddev->disks) &&
3690 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003691 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003692 st = clear;
3693 else
3694 st = inactive;
3695 }
3696 return sprintf(page, "%s\n", array_states[st]);
3697}
3698
NeilBrownfd01b882011-10-11 16:47:53 +11003699static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3700static int md_set_readonly(struct mddev * mddev, int is_open);
3701static int do_md_run(struct mddev * mddev);
3702static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003703
3704static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003705array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003706{
3707 int err = -EINVAL;
3708 enum array_state st = match_word(buf, array_states);
3709 switch(st) {
3710 case bad_word:
3711 break;
3712 case clear:
3713 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003714 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003715 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003716 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003717 break;
3718 case inactive:
3719 /* stopping an active array */
3720 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003721 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003722 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003723 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003724 } else
3725 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003726 break;
3727 case suspended:
3728 break; /* not supported yet */
3729 case readonly:
3730 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003731 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003732 else {
3733 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003734 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003735 err = do_md_run(mddev);
3736 }
3737 break;
3738 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003739 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003740 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003741 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003742 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003743 err = restart_array(mddev);
3744 if (err == 0) {
3745 mddev->ro = 2;
3746 set_disk_ro(mddev->gendisk, 0);
3747 }
NeilBrown9e653b62006-06-26 00:27:58 -07003748 } else {
3749 mddev->ro = 2;
3750 err = do_md_run(mddev);
3751 }
3752 break;
3753 case clean:
3754 if (mddev->pers) {
3755 restart_array(mddev);
3756 spin_lock_irq(&mddev->write_lock);
3757 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003758 if (mddev->in_sync == 0) {
3759 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003760 if (mddev->safemode == 1)
3761 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003762 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003763 }
3764 err = 0;
3765 } else
3766 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003767 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003768 } else
3769 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003770 break;
3771 case active:
3772 if (mddev->pers) {
3773 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003774 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003775 wake_up(&mddev->sb_wait);
3776 err = 0;
3777 } else {
3778 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003779 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003780 err = do_md_run(mddev);
3781 }
3782 break;
3783 case write_pending:
3784 case active_idle:
3785 /* these cannot be set */
3786 break;
3787 }
3788 if (err)
3789 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003790 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003791 if (mddev->hold_active == UNTIL_IOCTL)
3792 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003793 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003794 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003795 }
NeilBrown9e653b62006-06-26 00:27:58 -07003796}
NeilBrown80ca3a42006-07-10 04:44:18 -07003797static struct md_sysfs_entry md_array_state =
3798__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003799
NeilBrown6d7ff732006-01-06 00:21:16 -08003800static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003801max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003802 return sprintf(page, "%d\n",
3803 atomic_read(&mddev->max_corr_read_errors));
3804}
3805
3806static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003807max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003808{
3809 char *e;
3810 unsigned long n = simple_strtoul(buf, &e, 10);
3811
3812 if (*buf && (*e == 0 || *e == '\n')) {
3813 atomic_set(&mddev->max_corr_read_errors, n);
3814 return len;
3815 }
3816 return -EINVAL;
3817}
3818
3819static struct md_sysfs_entry max_corr_read_errors =
3820__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3821 max_corrected_read_errors_store);
3822
3823static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003824null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003825{
3826 return -EINVAL;
3827}
3828
3829static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003830new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003831{
3832 /* buf must be %d:%d\n? giving major and minor numbers */
3833 /* The new device is added to the array.
3834 * If the array has a persistent superblock, we read the
3835 * superblock to initialise info and check validity.
3836 * Otherwise, only checking done is that in bind_rdev_to_array,
3837 * which mainly checks size.
3838 */
3839 char *e;
3840 int major = simple_strtoul(buf, &e, 10);
3841 int minor;
3842 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003843 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003844 int err;
3845
3846 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3847 return -EINVAL;
3848 minor = simple_strtoul(e+1, &e, 10);
3849 if (*e && *e != '\n')
3850 return -EINVAL;
3851 dev = MKDEV(major, minor);
3852 if (major != MAJOR(dev) ||
3853 minor != MINOR(dev))
3854 return -EOVERFLOW;
3855
3856
3857 if (mddev->persistent) {
3858 rdev = md_import_device(dev, mddev->major_version,
3859 mddev->minor_version);
3860 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003861 struct md_rdev *rdev0
3862 = list_entry(mddev->disks.next,
3863 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003864 err = super_types[mddev->major_version]
3865 .load_super(rdev, rdev0, mddev->minor_version);
3866 if (err < 0)
3867 goto out;
3868 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003869 } else if (mddev->external)
3870 rdev = md_import_device(dev, -2, -1);
3871 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003872 rdev = md_import_device(dev, -1, -1);
3873
3874 if (IS_ERR(rdev))
3875 return PTR_ERR(rdev);
3876 err = bind_rdev_to_array(rdev, mddev);
3877 out:
3878 if (err)
3879 export_rdev(rdev);
3880 return err ? err : len;
3881}
3882
3883static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003884__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003885
3886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003887bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003888{
3889 char *end;
3890 unsigned long chunk, end_chunk;
3891
3892 if (!mddev->bitmap)
3893 goto out;
3894 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3895 while (*buf) {
3896 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3897 if (buf == end) break;
3898 if (*end == '-') { /* range */
3899 buf = end + 1;
3900 end_chunk = simple_strtoul(buf, &end, 0);
3901 if (buf == end) break;
3902 }
3903 if (*end && !isspace(*end)) break;
3904 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003905 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003906 }
3907 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3908out:
3909 return len;
3910}
3911
3912static struct md_sysfs_entry md_bitmap =
3913__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3914
3915static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003916size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003917{
Andre Noll58c0fed2009-03-31 14:33:13 +11003918 return sprintf(page, "%llu\n",
3919 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003920}
3921
NeilBrownfd01b882011-10-11 16:47:53 +11003922static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003923
3924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003925size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003926{
3927 /* If array is inactive, we can reduce the component size, but
3928 * not increase it (except from 0).
3929 * If array is active, we can try an on-line resize
3930 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003931 sector_t sectors;
3932 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003933
Andre Noll58c0fed2009-03-31 14:33:13 +11003934 if (err < 0)
3935 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003936 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003937 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003938 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003939 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003940 if (mddev->dev_sectors == 0 ||
3941 mddev->dev_sectors > sectors)
3942 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003943 else
3944 err = -ENOSPC;
3945 }
3946 return err ? err : len;
3947}
3948
3949static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003950__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003951
NeilBrown8bb93aa2006-01-06 00:20:50 -08003952
3953/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003954 * This is one of
3955 * 'none' for arrays with no metadata (good luck...)
3956 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003957 * or N.M for internally known formats
3958 */
3959static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003960metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003961{
3962 if (mddev->persistent)
3963 return sprintf(page, "%d.%d\n",
3964 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003965 else if (mddev->external)
3966 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003967 else
3968 return sprintf(page, "none\n");
3969}
3970
3971static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003972metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003973{
3974 int major, minor;
3975 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003976 /* Changing the details of 'external' metadata is
3977 * always permitted. Otherwise there must be
3978 * no devices attached to the array.
3979 */
3980 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3981 ;
3982 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003983 return -EBUSY;
3984
3985 if (cmd_match(buf, "none")) {
3986 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003987 mddev->external = 0;
3988 mddev->major_version = 0;
3989 mddev->minor_version = 90;
3990 return len;
3991 }
3992 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003993 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003994 if (namelen >= sizeof(mddev->metadata_type))
3995 namelen = sizeof(mddev->metadata_type)-1;
3996 strncpy(mddev->metadata_type, buf+9, namelen);
3997 mddev->metadata_type[namelen] = 0;
3998 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3999 mddev->metadata_type[--namelen] = 0;
4000 mddev->persistent = 0;
4001 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004002 mddev->major_version = 0;
4003 mddev->minor_version = 90;
4004 return len;
4005 }
4006 major = simple_strtoul(buf, &e, 10);
4007 if (e==buf || *e != '.')
4008 return -EINVAL;
4009 buf = e+1;
4010 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004011 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004012 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004013 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004014 return -ENOENT;
4015 mddev->major_version = major;
4016 mddev->minor_version = minor;
4017 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004018 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004019 return len;
4020}
4021
4022static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004023__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004024
NeilBrowna35b0d62006-01-06 00:20:49 -08004025static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004026action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004027{
NeilBrown7eec3142005-11-08 21:39:44 -08004028 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004029 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4030 type = "frozen";
4031 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004032 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004033 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4034 type = "reshape";
4035 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004036 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4037 type = "resync";
4038 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4039 type = "check";
4040 else
4041 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004042 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004043 type = "recover";
4044 }
4045 return sprintf(page, "%s\n", type);
4046}
4047
NeilBrownfd01b882011-10-11 16:47:53 +11004048static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004049
NeilBrown24dd4692005-11-08 21:39:26 -08004050static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004051action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004052{
NeilBrown7eec3142005-11-08 21:39:44 -08004053 if (!mddev->pers || !mddev->pers->sync_request)
4054 return -EINVAL;
4055
NeilBrownb6a9ce62009-05-26 09:41:17 +10004056 if (cmd_match(page, "frozen"))
4057 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4058 else
4059 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4060
4061 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004062 if (mddev->sync_thread) {
4063 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004064 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004065 }
NeilBrown03c902e2006-01-06 00:20:46 -08004066 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4067 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004068 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004069 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004070 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004071 else if (cmd_match(page, "recover")) {
4072 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4073 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4074 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004075 int err;
4076 if (mddev->pers->start_reshape == NULL)
4077 return -EINVAL;
4078 err = mddev->pers->start_reshape(mddev);
4079 if (err)
4080 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004081 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004082 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004083 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004084 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004085 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004086 return -EINVAL;
4087 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4088 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004089 }
NeilBrown03c902e2006-01-06 00:20:46 -08004090 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004091 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004092 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004093 return len;
4094}
4095
NeilBrown9d888832005-11-08 21:39:26 -08004096static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004097mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004098{
4099 return sprintf(page, "%llu\n",
4100 (unsigned long long) mddev->resync_mismatches);
4101}
4102
NeilBrown80ca3a42006-07-10 04:44:18 -07004103static struct md_sysfs_entry md_scan_mode =
4104__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004105
NeilBrown96de1e62005-11-08 21:39:39 -08004106
NeilBrown80ca3a42006-07-10 04:44:18 -07004107static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004108
NeilBrown88202a02006-01-06 00:21:36 -08004109static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004110sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004111{
4112 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4113 mddev->sync_speed_min ? "local": "system");
4114}
4115
4116static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004117sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004118{
4119 int min;
4120 char *e;
4121 if (strncmp(buf, "system", 6)==0) {
4122 mddev->sync_speed_min = 0;
4123 return len;
4124 }
4125 min = simple_strtoul(buf, &e, 10);
4126 if (buf == e || (*e && *e != '\n') || min <= 0)
4127 return -EINVAL;
4128 mddev->sync_speed_min = min;
4129 return len;
4130}
4131
4132static struct md_sysfs_entry md_sync_min =
4133__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4134
4135static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004136sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004137{
4138 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4139 mddev->sync_speed_max ? "local": "system");
4140}
4141
4142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004143sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004144{
4145 int max;
4146 char *e;
4147 if (strncmp(buf, "system", 6)==0) {
4148 mddev->sync_speed_max = 0;
4149 return len;
4150 }
4151 max = simple_strtoul(buf, &e, 10);
4152 if (buf == e || (*e && *e != '\n') || max <= 0)
4153 return -EINVAL;
4154 mddev->sync_speed_max = max;
4155 return len;
4156}
4157
4158static struct md_sysfs_entry md_sync_max =
4159__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4160
Iustin Popd7f3d292007-10-16 23:30:54 -07004161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004163{
4164 return sprintf(page, "%d\n", mddev->degraded);
4165}
4166static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004167
4168static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004169sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004170{
4171 return sprintf(page, "%d\n", mddev->parallel_resync);
4172}
4173
4174static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004175sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004176{
4177 long n;
4178
4179 if (strict_strtol(buf, 10, &n))
4180 return -EINVAL;
4181
4182 if (n != 0 && n != 1)
4183 return -EINVAL;
4184
4185 mddev->parallel_resync = n;
4186
4187 if (mddev->sync_thread)
4188 wake_up(&resync_wait);
4189
4190 return len;
4191}
4192
4193/* force parallel resync, even with shared block devices */
4194static struct md_sysfs_entry md_sync_force_parallel =
4195__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4196 sync_force_parallel_show, sync_force_parallel_store);
4197
4198static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004199sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004200{
4201 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004202 if (mddev->curr_resync == 0)
4203 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004204 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4205 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004206 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004207 db = resync - mddev->resync_mark_cnt;
4208 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004209}
4210
NeilBrown80ca3a42006-07-10 04:44:18 -07004211static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004212
4213static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004214sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004215{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004216 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004217
NeilBrownacb180b2009-04-14 16:28:34 +10004218 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4219 return sprintf(page, "none\n");
4220
NeilBrown88202a02006-01-06 00:21:36 -08004221 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004222 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004223 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004224 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004225
NeilBrownacb180b2009-04-14 16:28:34 +10004226 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004227 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004228}
4229
NeilBrown80ca3a42006-07-10 04:44:18 -07004230static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004231
NeilBrowne464eaf2006-03-27 01:18:14 -08004232static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004233min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004234{
4235 return sprintf(page, "%llu\n",
4236 (unsigned long long)mddev->resync_min);
4237}
4238static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004239min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004240{
4241 unsigned long long min;
4242 if (strict_strtoull(buf, 10, &min))
4243 return -EINVAL;
4244 if (min > mddev->resync_max)
4245 return -EINVAL;
4246 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4247 return -EBUSY;
4248
4249 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004250 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004251 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004252 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004253 return -EINVAL;
4254 }
4255 mddev->resync_min = min;
4256
4257 return len;
4258}
4259
4260static struct md_sysfs_entry md_min_sync =
4261__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4262
4263static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004264max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004265{
4266 if (mddev->resync_max == MaxSector)
4267 return sprintf(page, "max\n");
4268 else
4269 return sprintf(page, "%llu\n",
4270 (unsigned long long)mddev->resync_max);
4271}
4272static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004273max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004274{
4275 if (strncmp(buf, "max", 3) == 0)
4276 mddev->resync_max = MaxSector;
4277 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004278 unsigned long long max;
4279 if (strict_strtoull(buf, 10, &max))
4280 return -EINVAL;
4281 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004282 return -EINVAL;
4283 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004284 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004285 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4286 return -EBUSY;
4287
4288 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004289 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004290 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004291 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004292 return -EINVAL;
4293 }
4294 mddev->resync_max = max;
4295 }
4296 wake_up(&mddev->recovery_wait);
4297 return len;
4298}
4299
4300static struct md_sysfs_entry md_max_sync =
4301__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4302
4303static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004304suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004305{
4306 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4307}
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004311{
4312 char *e;
4313 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004314 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004315
NeilBrownb8d966e2009-07-01 11:14:04 +10004316 if (mddev->pers == NULL ||
4317 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004318 return -EINVAL;
4319 if (buf == e || (*e && *e != '\n'))
4320 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004321
4322 mddev->suspend_lo = new;
4323 if (new >= old)
4324 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004325 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004326 else {
4327 /* Expanding suspended region - need to wait */
4328 mddev->pers->quiesce(mddev, 1);
4329 mddev->pers->quiesce(mddev, 0);
4330 }
4331 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004332}
4333static struct md_sysfs_entry md_suspend_lo =
4334__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4335
4336
4337static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004338suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004339{
4340 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4341}
4342
4343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004345{
4346 char *e;
4347 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004348 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004349
NeilBrownb8d966e2009-07-01 11:14:04 +10004350 if (mddev->pers == NULL ||
4351 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004352 return -EINVAL;
4353 if (buf == e || (*e && *e != '\n'))
4354 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004355
4356 mddev->suspend_hi = new;
4357 if (new <= old)
4358 /* Shrinking suspended region */
4359 mddev->pers->quiesce(mddev, 2);
4360 else {
4361 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004362 mddev->pers->quiesce(mddev, 1);
4363 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004364 }
4365 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004366}
4367static struct md_sysfs_entry md_suspend_hi =
4368__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4369
NeilBrown08a02ec2007-05-09 02:35:38 -07004370static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004371reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004372{
4373 if (mddev->reshape_position != MaxSector)
4374 return sprintf(page, "%llu\n",
4375 (unsigned long long)mddev->reshape_position);
4376 strcpy(page, "none\n");
4377 return 5;
4378}
4379
4380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004381reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004382{
4383 char *e;
4384 unsigned long long new = simple_strtoull(buf, &e, 10);
4385 if (mddev->pers)
4386 return -EBUSY;
4387 if (buf == e || (*e && *e != '\n'))
4388 return -EINVAL;
4389 mddev->reshape_position = new;
4390 mddev->delta_disks = 0;
4391 mddev->new_level = mddev->level;
4392 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004393 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004394 return len;
4395}
4396
4397static struct md_sysfs_entry md_reshape_position =
4398__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4399 reshape_position_store);
4400
Dan Williamsb522adc2009-03-31 15:00:31 +11004401static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004402array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004403{
4404 if (mddev->external_size)
4405 return sprintf(page, "%llu\n",
4406 (unsigned long long)mddev->array_sectors/2);
4407 else
4408 return sprintf(page, "default\n");
4409}
4410
4411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004412array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004413{
4414 sector_t sectors;
4415
4416 if (strncmp(buf, "default", 7) == 0) {
4417 if (mddev->pers)
4418 sectors = mddev->pers->size(mddev, 0, 0);
4419 else
4420 sectors = mddev->array_sectors;
4421
4422 mddev->external_size = 0;
4423 } else {
4424 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4425 return -EINVAL;
4426 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004427 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004428
4429 mddev->external_size = 1;
4430 }
4431
4432 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004433 if (mddev->pers) {
4434 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004435 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004436 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004437 return len;
4438}
4439
4440static struct md_sysfs_entry md_array_size =
4441__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4442 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004443
NeilBrowneae17012005-11-08 21:39:23 -08004444static struct attribute *md_default_attrs[] = {
4445 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004446 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004447 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004448 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004449 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004450 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004451 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004452 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004453 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004454 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004455 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004456 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004457 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004458 NULL,
4459};
4460
4461static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004462 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004463 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004464 &md_sync_min.attr,
4465 &md_sync_max.attr,
4466 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004467 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004468 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004469 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004470 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004471 &md_suspend_lo.attr,
4472 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004473 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004474 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004475 NULL,
4476};
NeilBrown411036f2005-11-08 21:39:40 -08004477static struct attribute_group md_redundancy_group = {
4478 .name = NULL,
4479 .attrs = md_redundancy_attrs,
4480};
4481
NeilBrowneae17012005-11-08 21:39:23 -08004482
4483static ssize_t
4484md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4485{
4486 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004487 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004488 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004489
4490 if (!entry->show)
4491 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004492 spin_lock(&all_mddevs_lock);
4493 if (list_empty(&mddev->all_mddevs)) {
4494 spin_unlock(&all_mddevs_lock);
4495 return -EBUSY;
4496 }
4497 mddev_get(mddev);
4498 spin_unlock(&all_mddevs_lock);
4499
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004500 rv = mddev_lock(mddev);
4501 if (!rv) {
4502 rv = entry->show(mddev, page);
4503 mddev_unlock(mddev);
4504 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004505 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004506 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004507}
4508
4509static ssize_t
4510md_attr_store(struct kobject *kobj, struct attribute *attr,
4511 const char *page, size_t length)
4512{
4513 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004514 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004515 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004516
4517 if (!entry->store)
4518 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004519 if (!capable(CAP_SYS_ADMIN))
4520 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004521 spin_lock(&all_mddevs_lock);
4522 if (list_empty(&mddev->all_mddevs)) {
4523 spin_unlock(&all_mddevs_lock);
4524 return -EBUSY;
4525 }
4526 mddev_get(mddev);
4527 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004528 rv = mddev_lock(mddev);
4529 if (!rv) {
4530 rv = entry->store(mddev, page, length);
4531 mddev_unlock(mddev);
4532 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004533 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004534 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004535}
4536
4537static void md_free(struct kobject *ko)
4538{
NeilBrownfd01b882011-10-11 16:47:53 +11004539 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004540
4541 if (mddev->sysfs_state)
4542 sysfs_put(mddev->sysfs_state);
4543
4544 if (mddev->gendisk) {
4545 del_gendisk(mddev->gendisk);
4546 put_disk(mddev->gendisk);
4547 }
4548 if (mddev->queue)
4549 blk_cleanup_queue(mddev->queue);
4550
NeilBrowneae17012005-11-08 21:39:23 -08004551 kfree(mddev);
4552}
4553
Emese Revfy52cf25d2010-01-19 02:58:23 +01004554static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004555 .show = md_attr_show,
4556 .store = md_attr_store,
4557};
4558static struct kobj_type md_ktype = {
4559 .release = md_free,
4560 .sysfs_ops = &md_sysfs_ops,
4561 .default_attrs = md_default_attrs,
4562};
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564int mdp_major = 0;
4565
Dan Williams5fd3a172009-03-04 00:57:25 -07004566static void mddev_delayed_delete(struct work_struct *ws)
4567{
NeilBrownfd01b882011-10-11 16:47:53 +11004568 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004569
NeilBrown43a70502009-12-14 12:49:55 +11004570 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004571 kobject_del(&mddev->kobj);
4572 kobject_put(&mddev->kobj);
4573}
4574
NeilBrownefeb53c2009-01-09 08:31:10 +11004575static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004577 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004578 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004580 int partitioned;
4581 int shift;
4582 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004583 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004586 return -ENODEV;
4587
4588 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4589 shift = partitioned ? MdpMinorShift : 0;
4590 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Tejun Heoe804ac72010-10-15 15:36:08 +02004592 /* wait for any previous instance of this device to be
4593 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004594 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004595 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004596
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004597 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004598 error = -EEXIST;
4599 if (mddev->gendisk)
4600 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004601
4602 if (name) {
4603 /* Need to ensure that 'name' is not a duplicate.
4604 */
NeilBrownfd01b882011-10-11 16:47:53 +11004605 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004606 spin_lock(&all_mddevs_lock);
4607
4608 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4609 if (mddev2->gendisk &&
4610 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4611 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004612 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004613 }
4614 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
NeilBrown8b765392009-01-09 08:31:08 +11004616
NeilBrown0909dc42009-07-01 12:27:21 +10004617 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004618 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004619 if (!mddev->queue)
4620 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004621 mddev->queue->queuedata = mddev;
4622
NeilBrown409c57f2009-03-31 14:39:39 +11004623 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 disk = alloc_disk(1 << shift);
4626 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004627 blk_cleanup_queue(mddev->queue);
4628 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004629 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004631 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004633 if (name)
4634 strcpy(disk->disk_name, name);
4635 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004637 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 disk->fops = &md_fops;
4640 disk->private_data = mddev;
4641 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004642 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004643 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004644 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004645 * remove it now.
4646 */
4647 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004649 /* As soon as we call add_disk(), another thread could get
4650 * through to md_open, so make sure it doesn't get too far
4651 */
4652 mutex_lock(&mddev->open_mutex);
4653 add_disk(disk);
4654
Tejun Heoed9e1982008-08-25 19:56:05 +09004655 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4656 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004657 if (error) {
4658 /* This isn't possible, but as kobject_init_and_add is marked
4659 * __must_check, we must do something with the result
4660 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004661 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4662 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004663 error = 0;
4664 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004665 if (mddev->kobj.sd &&
4666 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004667 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004668 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004669 abort:
4670 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004671 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004672 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004673 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004674 }
NeilBrownd3374822009-01-09 08:31:10 +11004675 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004676 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004677}
4678
4679static struct kobject *md_probe(dev_t dev, int *part, void *data)
4680{
4681 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return NULL;
4683}
4684
NeilBrownefeb53c2009-01-09 08:31:10 +11004685static int add_named_array(const char *val, struct kernel_param *kp)
4686{
4687 /* val must be "md_*" where * is not all digits.
4688 * We allocate an array with a large free minor number, and
4689 * set the name to val. val must not already be an active name.
4690 */
4691 int len = strlen(val);
4692 char buf[DISK_NAME_LEN];
4693
4694 while (len && val[len-1] == '\n')
4695 len--;
4696 if (len >= DISK_NAME_LEN)
4697 return -E2BIG;
4698 strlcpy(buf, val, len+1);
4699 if (strncmp(buf, "md_", 3) != 0)
4700 return -EINVAL;
4701 return md_alloc(0, buf);
4702}
4703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704static void md_safemode_timeout(unsigned long data)
4705{
NeilBrownfd01b882011-10-11 16:47:53 +11004706 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Neil Brown0fd62b82008-06-28 08:31:36 +10004708 if (!atomic_read(&mddev->writes_pending)) {
4709 mddev->safemode = 1;
4710 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004711 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 md_wakeup_thread(mddev->thread);
4714}
4715
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004716static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
NeilBrownfd01b882011-10-11 16:47:53 +11004718int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
NeilBrown2604b702006-01-06 00:20:36 -08004720 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004721 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004722 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
NeilBrowna757e642005-04-16 15:26:42 -07004724 if (list_empty(&mddev->disks))
4725 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 if (mddev->pers)
4729 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004730 /* Cannot run until previous stop completes properly */
4731 if (mddev->sysfs_active)
4732 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 /*
4735 * Analyze all RAID superblock(s)
4736 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004737 if (!mddev->raid_disks) {
4738 if (!mddev->persistent)
4739 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004740 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
NeilBrownd9d166c2006-01-06 00:20:51 -08004743 if (mddev->level != LEVEL_NONE)
4744 request_module("md-level-%d", mddev->level);
4745 else if (mddev->clevel[0])
4746 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 /*
4749 * Drop all container device buffers, from now on
4750 * the only valid external interface is through the md
4751 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004753 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004754 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 continue;
4756 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004757 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004758
4759 /* perform some consistency tests on the device.
4760 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004761 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004762 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004763 if (rdev->meta_bdev) {
4764 /* Nothing to check */;
4765 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004766 if (mddev->dev_sectors &&
4767 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004768 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004769 printk("md: %s: data overlaps metadata\n",
4770 mdname(mddev));
4771 return -EINVAL;
4772 }
4773 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004774 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004775 > rdev->data_offset) {
4776 printk("md: %s: metadata overlaps data\n",
4777 mdname(mddev));
4778 return -EINVAL;
4779 }
4780 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004781 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 }
4783
NeilBrowna167f662010-10-26 18:31:13 +11004784 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004785 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004786 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004789 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004790 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004792 if (mddev->level != LEVEL_NONE)
4793 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4794 mddev->level);
4795 else
4796 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4797 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 return -EINVAL;
4799 }
NeilBrown2604b702006-01-06 00:20:36 -08004800 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004802 if (mddev->level != pers->level) {
4803 mddev->level = pers->level;
4804 mddev->new_level = pers->level;
4805 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004806 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
NeilBrownf6705572006-03-27 01:18:11 -08004808 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004809 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004810 /* This personality cannot handle reshaping... */
4811 mddev->pers = NULL;
4812 module_put(pers->owner);
4813 return -EINVAL;
4814 }
4815
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004816 if (pers->sync_request) {
4817 /* Warn if this is a potentially silly
4818 * configuration.
4819 */
4820 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004821 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004822 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004823
4824 list_for_each_entry(rdev, &mddev->disks, same_set)
4825 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004826 if (rdev < rdev2 &&
4827 rdev->bdev->bd_contains ==
4828 rdev2->bdev->bd_contains) {
4829 printk(KERN_WARNING
4830 "%s: WARNING: %s appears to be"
4831 " on the same physical disk as"
4832 " %s.\n",
4833 mdname(mddev),
4834 bdevname(rdev->bdev,b),
4835 bdevname(rdev2->bdev,b2));
4836 warned = 1;
4837 }
4838 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004839
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004840 if (warned)
4841 printk(KERN_WARNING
4842 "True protection against single-disk"
4843 " failure might be compromised.\n");
4844 }
4845
NeilBrown657390d2005-08-26 18:34:16 -07004846 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004847 /* may be over-ridden by personality */
4848 mddev->resync_max_sectors = mddev->dev_sectors;
4849
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004850 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
NeilBrown0f9552b2009-12-30 12:08:50 +11004852 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004853 mddev->ro = 2; /* read-only, but switch on first write */
4854
NeilBrownb15c2e52006-01-06 00:20:16 -08004855 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004856 if (err)
4857 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004858 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4859 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4860 " but 'external_size' not in effect?\n", __func__);
4861 printk(KERN_ERR
4862 "md: invalid array_size %llu > default size %llu\n",
4863 (unsigned long long)mddev->array_sectors / 2,
4864 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4865 err = -EINVAL;
4866 mddev->pers->stop(mddev);
4867 }
4868 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004869 err = bitmap_create(mddev);
4870 if (err) {
4871 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4872 mdname(mddev), err);
4873 mddev->pers->stop(mddev);
4874 }
4875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 module_put(mddev->pers->owner);
4878 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004879 bitmap_destroy(mddev);
4880 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004882 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004883 if (mddev->kobj.sd &&
4884 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004885 printk(KERN_WARNING
4886 "md: cannot register extra attributes for %s\n",
4887 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004888 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004889 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004890 mddev->ro = 0;
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004893 atomic_set(&mddev->max_corr_read_errors,
4894 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 mddev->safemode = 0;
4896 mddev->safemode_timer.function = md_safemode_timeout;
4897 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004898 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004900 smp_wmb();
4901 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004902 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004903 if (rdev->raid_disk >= 0)
4904 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004905 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4908
NeilBrown850b2b42006-10-03 01:15:46 -07004909 if (mddev->flags)
4910 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
NeilBrownd7603b72006-01-06 00:20:30 -08004912 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004913 sysfs_notify_dirent_safe(mddev->sysfs_state);
4914 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004915 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 return 0;
4917}
NeilBrown390ee602010-06-01 19:37:27 +10004918EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
NeilBrownfd01b882011-10-11 16:47:53 +11004920static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004921{
4922 int err;
4923
4924 err = md_run(mddev);
4925 if (err)
4926 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004927 err = bitmap_load(mddev);
4928 if (err) {
4929 bitmap_destroy(mddev);
4930 goto out;
4931 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004932
4933 md_wakeup_thread(mddev->thread);
4934 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4935
NeilBrownfe60b012010-03-29 11:10:42 +11004936 set_capacity(mddev->gendisk, mddev->array_sectors);
4937 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004938 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004939 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4940out:
4941 return err;
4942}
4943
NeilBrownfd01b882011-10-11 16:47:53 +11004944static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
4946 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Andre Noll80fab1d2008-07-11 22:02:21 +10004948 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004950 return -ENXIO;
4951 if (!mddev->pers)
4952 return -EINVAL;
4953 if (!mddev->ro)
4954 return -EBUSY;
4955 mddev->safemode = 0;
4956 mddev->ro = 0;
4957 set_disk_ro(disk, 0);
4958 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4959 mdname(mddev));
4960 /* Kick recovery or resync if necessary */
4961 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4962 md_wakeup_thread(mddev->thread);
4963 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004964 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
NeilBrownacc55e22006-06-26 00:27:47 -07004968/* similar to deny_write_access, but accounts for our holding a reference
4969 * to the file ourselves */
4970static int deny_bitmap_write_access(struct file * file)
4971{
4972 struct inode *inode = file->f_mapping->host;
4973
4974 spin_lock(&inode->i_lock);
4975 if (atomic_read(&inode->i_writecount) > 1) {
4976 spin_unlock(&inode->i_lock);
4977 return -ETXTBSY;
4978 }
4979 atomic_set(&inode->i_writecount, -1);
4980 spin_unlock(&inode->i_lock);
4981
4982 return 0;
4983}
4984
NeilBrown43a70502009-12-14 12:49:55 +11004985void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004986{
4987 struct inode *inode = file->f_mapping->host;
4988
4989 spin_lock(&inode->i_lock);
4990 atomic_set(&inode->i_writecount, 1);
4991 spin_unlock(&inode->i_lock);
4992}
4993
NeilBrownfd01b882011-10-11 16:47:53 +11004994static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004995{
4996 mddev->array_sectors = 0;
4997 mddev->external_size = 0;
4998 mddev->dev_sectors = 0;
4999 mddev->raid_disks = 0;
5000 mddev->recovery_cp = 0;
5001 mddev->resync_min = 0;
5002 mddev->resync_max = MaxSector;
5003 mddev->reshape_position = MaxSector;
5004 mddev->external = 0;
5005 mddev->persistent = 0;
5006 mddev->level = LEVEL_NONE;
5007 mddev->clevel[0] = 0;
5008 mddev->flags = 0;
5009 mddev->ro = 0;
5010 mddev->metadata_type[0] = 0;
5011 mddev->chunk_sectors = 0;
5012 mddev->ctime = mddev->utime = 0;
5013 mddev->layout = 0;
5014 mddev->max_disks = 0;
5015 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005016 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005017 mddev->delta_disks = 0;
5018 mddev->new_level = LEVEL_NONE;
5019 mddev->new_layout = 0;
5020 mddev->new_chunk_sectors = 0;
5021 mddev->curr_resync = 0;
5022 mddev->resync_mismatches = 0;
5023 mddev->suspend_lo = mddev->suspend_hi = 0;
5024 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5025 mddev->recovery = 0;
5026 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005027 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005028 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005029 mddev->safemode = 0;
5030 mddev->bitmap_info.offset = 0;
5031 mddev->bitmap_info.default_offset = 0;
5032 mddev->bitmap_info.chunksize = 0;
5033 mddev->bitmap_info.daemon_sleep = 0;
5034 mddev->bitmap_info.max_write_behind = 0;
5035}
5036
NeilBrownfd01b882011-10-11 16:47:53 +11005037static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005038{
5039 if (mddev->sync_thread) {
5040 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5041 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005042 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005043 }
5044
5045 del_timer_sync(&mddev->safemode_timer);
5046
5047 bitmap_flush(mddev);
5048 md_super_wait(mddev);
5049
5050 if (!mddev->in_sync || mddev->flags) {
5051 /* mark array as shutdown cleanly */
5052 mddev->in_sync = 1;
5053 md_update_sb(mddev, 1);
5054 }
5055}
NeilBrowndefad612011-01-14 09:14:33 +11005056
NeilBrownfd01b882011-10-11 16:47:53 +11005057void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005058{
5059 mddev_lock(mddev);
5060 __md_stop_writes(mddev);
5061 mddev_unlock(mddev);
5062}
NeilBrown390ee602010-06-01 19:37:27 +10005063EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005064
NeilBrownfd01b882011-10-11 16:47:53 +11005065void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005066{
NeilBrown0ca69882011-01-14 09:14:33 +11005067 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005068 mddev->pers->stop(mddev);
5069 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5070 mddev->to_remove = &md_redundancy_group;
5071 module_put(mddev->pers->owner);
5072 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005073 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005074}
NeilBrown390ee602010-06-01 19:37:27 +10005075EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005076
NeilBrownfd01b882011-10-11 16:47:53 +11005077static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005078{
5079 int err = 0;
5080 mutex_lock(&mddev->open_mutex);
5081 if (atomic_read(&mddev->openers) > is_open) {
5082 printk("md: %s still in use.\n",mdname(mddev));
5083 err = -EBUSY;
5084 goto out;
5085 }
5086 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005087 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005088
5089 err = -ENXIO;
5090 if (mddev->ro==1)
5091 goto out;
5092 mddev->ro = 1;
5093 set_disk_ro(mddev->gendisk, 1);
5094 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005095 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005096 err = 0;
5097 }
5098out:
5099 mutex_unlock(&mddev->open_mutex);
5100 return err;
5101}
5102
NeilBrown9e653b62006-06-26 00:27:58 -07005103/* mode:
5104 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005105 * 2 - stop but do not disassemble array
5106 */
NeilBrownfd01b882011-10-11 16:47:53 +11005107static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005110 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
NeilBrownc8c00a62009-08-10 12:50:52 +10005112 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005113 if (atomic_read(&mddev->openers) > is_open ||
5114 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005115 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005116 mutex_unlock(&mddev->open_mutex);
5117 return -EBUSY;
5118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
NeilBrown6e17b022010-08-07 21:41:19 +10005120 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005121 if (mddev->ro)
5122 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005123
NeilBrowndefad612011-01-14 09:14:33 +11005124 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005125 md_stop(mddev);
5126 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005127 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005128
NeilBrowna4bd82d2010-03-29 13:23:10 +11005129 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005130 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005131
NeilBrowna4bd82d2010-03-29 13:23:10 +11005132 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005133 if (rdev->raid_disk >= 0)
5134 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005135
NeilBrowna4bd82d2010-03-29 13:23:10 +11005136 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005137 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005138 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005139 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005140
NeilBrowna4bd82d2010-03-29 13:23:10 +11005141 if (mddev->ro)
5142 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005143 } else
5144 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 /*
5146 * Free resources if final stop
5147 */
NeilBrown9e653b62006-06-26 00:27:58 -07005148 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5150
NeilBrown978f9462006-02-02 14:28:05 -08005151 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005152 if (mddev->bitmap_info.file) {
5153 restore_bitmap_write_access(mddev->bitmap_info.file);
5154 fput(mddev->bitmap_info.file);
5155 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005156 }
NeilBrownc3d97142009-12-14 12:49:52 +11005157 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 export_array(mddev);
5160
NeilBrown6177b472010-03-29 11:37:13 +11005161 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005162 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005163 if (mddev->hold_active == UNTIL_STOP)
5164 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005165 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005166 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005167 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005168 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170}
5171
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005172#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005173static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
NeilBrown3cb03002011-10-11 16:45:26 +11005175 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 int err;
5177
NeilBrowna757e642005-04-16 15:26:42 -07005178 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 printk(KERN_INFO "md: running: ");
5182
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005183 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 char b[BDEVNAME_SIZE];
5185 printk("<%s>", bdevname(rdev->bdev,b));
5186 }
5187 printk("\n");
5188
NeilBrownd710e132008-10-13 11:55:12 +11005189 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 if (err) {
5191 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005192 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
5194}
5195
5196/*
5197 * lets try to run arrays based on all disks that have arrived
5198 * until now. (those are in pending_raid_disks)
5199 *
5200 * the method: pick the first pending disk, collect all disks with
5201 * the same UUID, remove all from the pending list and put them into
5202 * the 'same_array' list. Then order this list based on superblock
5203 * update time (freshest comes first), kick out 'old' disks and
5204 * compare superblocks. If everything's fine then run it.
5205 *
5206 * If "unit" is allocated, then bump its reference count
5207 */
5208static void autorun_devices(int part)
5209{
NeilBrown3cb03002011-10-11 16:45:26 +11005210 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005211 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 char b[BDEVNAME_SIZE];
5213
5214 printk(KERN_INFO "md: autorun ...\n");
5215 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005216 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005218 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005220 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
5222 printk(KERN_INFO "md: considering %s ...\n",
5223 bdevname(rdev0->bdev,b));
5224 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005225 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (super_90_load(rdev, rdev0, 0) >= 0) {
5227 printk(KERN_INFO "md: adding %s ...\n",
5228 bdevname(rdev->bdev,b));
5229 list_move(&rdev->same_set, &candidates);
5230 }
5231 /*
5232 * now we have a set of devices, with all of them having
5233 * mostly sane superblocks. It's time to allocate the
5234 * mddev.
5235 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005236 if (part) {
5237 dev = MKDEV(mdp_major,
5238 rdev0->preferred_minor << MdpMinorShift);
5239 unit = MINOR(dev) >> MdpMinorShift;
5240 } else {
5241 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5242 unit = MINOR(dev);
5243 }
5244 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5246 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5247 break;
5248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 md_probe(dev, NULL, NULL);
5251 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005252 if (!mddev || !mddev->gendisk) {
5253 if (mddev)
5254 mddev_put(mddev);
5255 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 "md: cannot allocate memory for md drive.\n");
5257 break;
5258 }
5259 if (mddev_lock(mddev))
5260 printk(KERN_WARNING "md: %s locked, cannot run\n",
5261 mdname(mddev));
5262 else if (mddev->raid_disks || mddev->major_version
5263 || !list_empty(&mddev->disks)) {
5264 printk(KERN_WARNING
5265 "md: %s already running, cannot run %s\n",
5266 mdname(mddev), bdevname(rdev0->bdev,b));
5267 mddev_unlock(mddev);
5268 } else {
5269 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005270 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005271 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 list_del_init(&rdev->same_set);
5273 if (bind_rdev_to_array(rdev, mddev))
5274 export_rdev(rdev);
5275 }
5276 autorun_array(mddev);
5277 mddev_unlock(mddev);
5278 }
5279 /* on success, candidates will be empty, on error
5280 * it won't...
5281 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005282 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005283 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 mddev_put(mddev);
5287 }
5288 printk(KERN_INFO "md: ... autorun DONE.\n");
5289}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005290#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292static int get_version(void __user * arg)
5293{
5294 mdu_version_t ver;
5295
5296 ver.major = MD_MAJOR_VERSION;
5297 ver.minor = MD_MINOR_VERSION;
5298 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5299
5300 if (copy_to_user(arg, &ver, sizeof(ver)))
5301 return -EFAULT;
5302
5303 return 0;
5304}
5305
NeilBrownfd01b882011-10-11 16:47:53 +11005306static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307{
5308 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005309 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005310 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
NeilBrowna9f326e2009-09-23 18:06:41 +10005312 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005313 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005315 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 failed++;
5317 else {
5318 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005319 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005320 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 else
5322 spare++;
5323 }
5324 }
5325
5326 info.major_version = mddev->major_version;
5327 info.minor_version = mddev->minor_version;
5328 info.patch_version = MD_PATCHLEVEL_VERSION;
5329 info.ctime = mddev->ctime;
5330 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005331 info.size = mddev->dev_sectors / 2;
5332 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005333 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 info.nr_disks = nr;
5335 info.raid_disks = mddev->raid_disks;
5336 info.md_minor = mddev->md_minor;
5337 info.not_persistent= !mddev->persistent;
5338
5339 info.utime = mddev->utime;
5340 info.state = 0;
5341 if (mddev->in_sync)
5342 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005343 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005344 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005345 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 info.working_disks = working;
5347 info.failed_disks = failed;
5348 info.spare_disks = spare;
5349
5350 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005351 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 if (copy_to_user(arg, &info, sizeof(info)))
5354 return -EFAULT;
5355
5356 return 0;
5357}
5358
NeilBrownfd01b882011-10-11 16:47:53 +11005359static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005360{
5361 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5362 char *ptr, *buf = NULL;
5363 int err = -ENOMEM;
5364
Dan Williamsb5470dc2008-06-27 21:44:04 -07005365 if (md_allow_write(mddev))
5366 file = kmalloc(sizeof(*file), GFP_NOIO);
5367 else
5368 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005369
NeilBrown32a76272005-06-21 17:17:14 -07005370 if (!file)
5371 goto out;
5372
5373 /* bitmap disabled, zero the first byte and copy out */
5374 if (!mddev->bitmap || !mddev->bitmap->file) {
5375 file->pathname[0] = '\0';
5376 goto copy_out;
5377 }
5378
5379 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5380 if (!buf)
5381 goto out;
5382
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005383 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5384 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005385 goto out;
5386
5387 strcpy(file->pathname, ptr);
5388
5389copy_out:
5390 err = 0;
5391 if (copy_to_user(arg, file, sizeof(*file)))
5392 err = -EFAULT;
5393out:
5394 kfree(buf);
5395 kfree(file);
5396 return err;
5397}
5398
NeilBrownfd01b882011-10-11 16:47:53 +11005399static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
5401 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005402 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
5404 if (copy_from_user(&info, arg, sizeof(info)))
5405 return -EFAULT;
5406
Andre Noll26ef3792008-07-11 22:02:21 +10005407 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 if (rdev) {
5409 info.major = MAJOR(rdev->bdev->bd_dev);
5410 info.minor = MINOR(rdev->bdev->bd_dev);
5411 info.raid_disk = rdev->raid_disk;
5412 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005413 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005415 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 info.state |= (1<<MD_DISK_ACTIVE);
5417 info.state |= (1<<MD_DISK_SYNC);
5418 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005419 if (test_bit(WriteMostly, &rdev->flags))
5420 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 } else {
5422 info.major = info.minor = 0;
5423 info.raid_disk = -1;
5424 info.state = (1<<MD_DISK_REMOVED);
5425 }
5426
5427 if (copy_to_user(arg, &info, sizeof(info)))
5428 return -EFAULT;
5429
5430 return 0;
5431}
5432
NeilBrownfd01b882011-10-11 16:47:53 +11005433static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005436 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 dev_t dev = MKDEV(info->major,info->minor);
5438
5439 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5440 return -EOVERFLOW;
5441
5442 if (!mddev->raid_disks) {
5443 int err;
5444 /* expecting a device which has a superblock */
5445 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5446 if (IS_ERR(rdev)) {
5447 printk(KERN_WARNING
5448 "md: md_import_device returned %ld\n",
5449 PTR_ERR(rdev));
5450 return PTR_ERR(rdev);
5451 }
5452 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005453 struct md_rdev *rdev0
5454 = list_entry(mddev->disks.next,
5455 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005456 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 .load_super(rdev, rdev0, mddev->minor_version);
5458 if (err < 0) {
5459 printk(KERN_WARNING
5460 "md: %s has different UUID to %s\n",
5461 bdevname(rdev->bdev,b),
5462 bdevname(rdev0->bdev,b2));
5463 export_rdev(rdev);
5464 return -EINVAL;
5465 }
5466 }
5467 err = bind_rdev_to_array(rdev, mddev);
5468 if (err)
5469 export_rdev(rdev);
5470 return err;
5471 }
5472
5473 /*
5474 * add_new_disk can be used once the array is assembled
5475 * to add "hot spares". They must already have a superblock
5476 * written
5477 */
5478 if (mddev->pers) {
5479 int err;
5480 if (!mddev->pers->hot_add_disk) {
5481 printk(KERN_WARNING
5482 "%s: personality does not support diskops!\n",
5483 mdname(mddev));
5484 return -EINVAL;
5485 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005486 if (mddev->persistent)
5487 rdev = md_import_device(dev, mddev->major_version,
5488 mddev->minor_version);
5489 else
5490 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 if (IS_ERR(rdev)) {
5492 printk(KERN_WARNING
5493 "md: md_import_device returned %ld\n",
5494 PTR_ERR(rdev));
5495 return PTR_ERR(rdev);
5496 }
NeilBrown1a855a02010-12-09 16:36:28 +11005497 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005498 if (!mddev->persistent) {
5499 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005500 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005501 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005502 set_bit(In_sync, &rdev->flags);
5503 } else
NeilBrown41158c72005-06-21 17:17:25 -07005504 rdev->raid_disk = -1;
5505 } else
5506 super_types[mddev->major_version].
5507 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005508 if ((info->state & (1<<MD_DISK_SYNC)) &&
5509 (!test_bit(In_sync, &rdev->flags) ||
5510 rdev->raid_disk != info->raid_disk)) {
5511 /* This was a hot-add request, but events doesn't
5512 * match, so reject it.
5513 */
5514 export_rdev(rdev);
5515 return -EINVAL;
5516 }
5517
NeilBrown1a855a02010-12-09 16:36:28 +11005518 if (test_bit(In_sync, &rdev->flags))
5519 rdev->saved_raid_disk = rdev->raid_disk;
5520 else
5521 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005522
NeilBrownb2d444d2005-11-08 21:39:31 -08005523 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005524 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5525 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005526 else
5527 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 rdev->raid_disk = -1;
5530 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005531 if (!err && !mddev->pers->hot_remove_disk) {
5532 /* If there is hot_add_disk but no hot_remove_disk
5533 * then added disks for geometry changes,
5534 * and should be added immediately.
5535 */
5536 super_types[mddev->major_version].
5537 validate_super(mddev, rdev);
5538 err = mddev->pers->hot_add_disk(mddev, rdev);
5539 if (err)
5540 unbind_rdev_from_array(rdev);
5541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (err)
5543 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005544 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005545 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005546
NeilBrown17571282006-12-10 02:20:52 -08005547 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005548 if (mddev->degraded)
5549 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005550 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005551 if (!err)
5552 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005553 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return err;
5555 }
5556
5557 /* otherwise, add_new_disk is only allowed
5558 * for major_version==0 superblocks
5559 */
5560 if (mddev->major_version != 0) {
5561 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5562 mdname(mddev));
5563 return -EINVAL;
5564 }
5565
5566 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5567 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005568 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 if (IS_ERR(rdev)) {
5570 printk(KERN_WARNING
5571 "md: error, md_import_device() returned %ld\n",
5572 PTR_ERR(rdev));
5573 return PTR_ERR(rdev);
5574 }
5575 rdev->desc_nr = info->number;
5576 if (info->raid_disk < mddev->raid_disks)
5577 rdev->raid_disk = info->raid_disk;
5578 else
5579 rdev->raid_disk = -1;
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005582 if (info->state & (1<<MD_DISK_SYNC))
5583 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005585 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5586 set_bit(WriteMostly, &rdev->flags);
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (!mddev->persistent) {
5589 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005590 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5591 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005592 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005593 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
NeilBrown2bf071b2006-01-06 00:20:55 -08005595 err = bind_rdev_to_array(rdev, mddev);
5596 if (err) {
5597 export_rdev(rdev);
5598 return err;
5599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 }
5601
5602 return 0;
5603}
5604
NeilBrownfd01b882011-10-11 16:47:53 +11005605static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
5607 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005608 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 rdev = find_rdev(mddev, dev);
5611 if (!rdev)
5612 return -ENXIO;
5613
5614 if (rdev->raid_disk >= 0)
5615 goto busy;
5616
5617 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005618 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005619 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 return 0;
5622busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005623 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 bdevname(rdev->bdev,b), mdname(mddev));
5625 return -EBUSY;
5626}
5627
NeilBrownfd01b882011-10-11 16:47:53 +11005628static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629{
5630 char b[BDEVNAME_SIZE];
5631 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005632 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 if (!mddev->pers)
5635 return -ENODEV;
5636
5637 if (mddev->major_version != 0) {
5638 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5639 " version-0 superblocks.\n",
5640 mdname(mddev));
5641 return -EINVAL;
5642 }
5643 if (!mddev->pers->hot_add_disk) {
5644 printk(KERN_WARNING
5645 "%s: personality does not support diskops!\n",
5646 mdname(mddev));
5647 return -EINVAL;
5648 }
5649
NeilBrownd710e132008-10-13 11:55:12 +11005650 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 if (IS_ERR(rdev)) {
5652 printk(KERN_WARNING
5653 "md: error, md_import_device() returned %ld\n",
5654 PTR_ERR(rdev));
5655 return -EINVAL;
5656 }
5657
5658 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005659 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005661 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
NeilBrown8190e752009-06-18 08:48:58 +10005663 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
NeilBrownb2d444d2005-11-08 21:39:31 -08005665 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 printk(KERN_WARNING
5667 "md: can not hot-add faulty %s disk to %s!\n",
5668 bdevname(rdev->bdev,b), mdname(mddev));
5669 err = -EINVAL;
5670 goto abort_export;
5671 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005674 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005675 err = bind_rdev_to_array(rdev, mddev);
5676 if (err)
5677 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678
5679 /*
5680 * The rest should better be atomic, we can have disk failures
5681 * noticed in interrupt contexts ...
5682 */
5683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 rdev->raid_disk = -1;
5685
NeilBrown850b2b42006-10-03 01:15:46 -07005686 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 /*
5689 * Kick recovery, maybe this spare has to be added to the
5690 * array immediately.
5691 */
5692 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5693 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005694 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 return 0;
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697abort_export:
5698 export_rdev(rdev);
5699 return err;
5700}
5701
NeilBrownfd01b882011-10-11 16:47:53 +11005702static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005703{
5704 int err;
5705
NeilBrown36fa3062005-09-09 16:23:45 -07005706 if (mddev->pers) {
5707 if (!mddev->pers->quiesce)
5708 return -EBUSY;
5709 if (mddev->recovery || mddev->sync_thread)
5710 return -EBUSY;
5711 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005712 }
5713
NeilBrown36fa3062005-09-09 16:23:45 -07005714
5715 if (fd >= 0) {
5716 if (mddev->bitmap)
5717 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005718 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005719
NeilBrownc3d97142009-12-14 12:49:52 +11005720 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005721 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5722 mdname(mddev));
5723 return -EBADF;
5724 }
5725
NeilBrownc3d97142009-12-14 12:49:52 +11005726 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005727 if (err) {
5728 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5729 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005730 fput(mddev->bitmap_info.file);
5731 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005732 return err;
5733 }
NeilBrownc3d97142009-12-14 12:49:52 +11005734 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005735 } else if (mddev->bitmap == NULL)
5736 return -ENOENT; /* cannot remove what isn't there */
5737 err = 0;
5738 if (mddev->pers) {
5739 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005740 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005741 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005742 if (!err)
5743 err = bitmap_load(mddev);
5744 }
NeilBrownd7375ab322006-06-26 00:27:43 -07005745 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005746 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005747 fd = -1; /* make sure to put the file */
5748 }
NeilBrown36fa3062005-09-09 16:23:45 -07005749 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005750 }
5751 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005752 if (mddev->bitmap_info.file) {
5753 restore_bitmap_write_access(mddev->bitmap_info.file);
5754 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005755 }
NeilBrownc3d97142009-12-14 12:49:52 +11005756 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005757 }
5758
NeilBrown32a76272005-06-21 17:17:14 -07005759 return err;
5760}
5761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762/*
5763 * set_array_info is used two different ways
5764 * The original usage is when creating a new array.
5765 * In this usage, raid_disks is > 0 and it together with
5766 * level, size, not_persistent,layout,chunksize determine the
5767 * shape of the array.
5768 * This will always create an array with a type-0.90.0 superblock.
5769 * The newer usage is when assembling an array.
5770 * In this case raid_disks will be 0, and the major_version field is
5771 * use to determine which style super-blocks are to be found on the devices.
5772 * The minor and patch _version numbers are also kept incase the
5773 * super_block handler wishes to interpret them.
5774 */
NeilBrownfd01b882011-10-11 16:47:53 +11005775static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776{
5777
5778 if (info->raid_disks == 0) {
5779 /* just setting version number for superblock loading */
5780 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005781 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 super_types[info->major_version].name == NULL) {
5783 /* maybe try to auto-load a module? */
5784 printk(KERN_INFO
5785 "md: superblock version %d not known\n",
5786 info->major_version);
5787 return -EINVAL;
5788 }
5789 mddev->major_version = info->major_version;
5790 mddev->minor_version = info->minor_version;
5791 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005792 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005793 /* ensure mddev_put doesn't delete this now that there
5794 * is some minimal configuration.
5795 */
5796 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 return 0;
5798 }
5799 mddev->major_version = MD_MAJOR_VERSION;
5800 mddev->minor_version = MD_MINOR_VERSION;
5801 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5802 mddev->ctime = get_seconds();
5803
5804 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005805 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005806 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 mddev->raid_disks = info->raid_disks;
5808 /* don't set md_minor, it is determined by which /dev/md* was
5809 * openned
5810 */
5811 if (info->state & (1<<MD_SB_CLEAN))
5812 mddev->recovery_cp = MaxSector;
5813 else
5814 mddev->recovery_cp = 0;
5815 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005816 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005819 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 mddev->max_disks = MD_SB_DISKS;
5822
NeilBrowne6910632008-02-06 01:39:51 -08005823 if (mddev->persistent)
5824 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005825 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
NeilBrownc3d97142009-12-14 12:49:52 +11005827 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5828 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005829
NeilBrownf6705572006-03-27 01:18:11 -08005830 mddev->reshape_position = MaxSector;
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 /*
5833 * Generate a 128 bit UUID
5834 */
5835 get_random_bytes(mddev->uuid, 16);
5836
NeilBrownf6705572006-03-27 01:18:11 -08005837 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005838 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005839 mddev->new_layout = mddev->layout;
5840 mddev->delta_disks = 0;
5841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 return 0;
5843}
5844
NeilBrownfd01b882011-10-11 16:47:53 +11005845void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005846{
Dan Williamsb522adc2009-03-31 15:00:31 +11005847 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5848
5849 if (mddev->external_size)
5850 return;
5851
Dan Williams1f403622009-03-31 14:59:03 +11005852 mddev->array_sectors = array_sectors;
5853}
5854EXPORT_SYMBOL(md_set_array_sectors);
5855
NeilBrownfd01b882011-10-11 16:47:53 +11005856static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005857{
NeilBrown3cb03002011-10-11 16:45:26 +11005858 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005859 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005860 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005861
5862 if (mddev->pers->resize == NULL)
5863 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005864 /* The "num_sectors" is the number of sectors of each device that
5865 * is used. This can only make sense for arrays with redundancy.
5866 * linear and raid0 always use whatever space is available. We can only
5867 * consider changing this number if no resync or reconstruction is
5868 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005869 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005870 * of each device. If num_sectors is zero, we find the largest size
5871 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005872 */
5873 if (mddev->sync_thread)
5874 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005875 if (mddev->bitmap)
5876 /* Sorry, cannot grow a bitmap yet, just remove it,
5877 * grow, and re-add.
5878 */
5879 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005880 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005881 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005882
Andre Nolld71f9f82008-07-11 22:02:22 +10005883 if (fit && (num_sectors == 0 || num_sectors > avail))
5884 num_sectors = avail;
5885 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005886 return -ENOSPC;
5887 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005888 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005889 if (!rv)
5890 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005891 return rv;
5892}
5893
NeilBrownfd01b882011-10-11 16:47:53 +11005894static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08005895{
5896 int rv;
5897 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005898 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005899 return -EINVAL;
5900 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005901 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005902 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005903 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005904 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005905 mddev->delta_disks = raid_disks - mddev->raid_disks;
5906
5907 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005908 if (rv < 0)
5909 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005910 return rv;
5911}
5912
5913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914/*
5915 * update_array_info is used to change the configuration of an
5916 * on-line array.
5917 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5918 * fields in the info are checked against the array.
5919 * Any differences that cannot be handled will cause an error.
5920 * Normally, only one change can be managed at a time.
5921 */
NeilBrownfd01b882011-10-11 16:47:53 +11005922static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923{
5924 int rv = 0;
5925 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005926 int state = 0;
5927
5928 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005929 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005930 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (mddev->major_version != info->major_version ||
5933 mddev->minor_version != info->minor_version ||
5934/* mddev->patch_version != info->patch_version || */
5935 mddev->ctime != info->ctime ||
5936 mddev->level != info->level ||
5937/* mddev->layout != info->layout || */
5938 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005939 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005940 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5941 ((state^info->state) & 0xfffffe00)
5942 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return -EINVAL;
5944 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005945 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5946 cnt++;
5947 if (mddev->raid_disks != info->raid_disks)
5948 cnt++;
5949 if (mddev->layout != info->layout)
5950 cnt++;
5951 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5952 cnt++;
5953 if (cnt == 0)
5954 return 0;
5955 if (cnt > 1)
5956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
5958 if (mddev->layout != info->layout) {
5959 /* Change layout
5960 * we don't need to do anything at the md level, the
5961 * personality will take care of it all.
5962 */
NeilBrown50ac1682009-06-18 08:47:55 +10005963 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005965 else {
5966 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005967 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005968 if (rv)
5969 mddev->new_layout = mddev->layout;
5970 return rv;
5971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005973 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005974 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
NeilBrownda943b992006-01-06 00:20:54 -08005976 if (mddev->raid_disks != info->raid_disks)
5977 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
NeilBrown36fa3062005-09-09 16:23:45 -07005979 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5980 if (mddev->pers->quiesce == NULL)
5981 return -EINVAL;
5982 if (mddev->recovery || mddev->sync_thread)
5983 return -EBUSY;
5984 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5985 /* add the bitmap */
5986 if (mddev->bitmap)
5987 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005988 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005989 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005990 mddev->bitmap_info.offset =
5991 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005992 mddev->pers->quiesce(mddev, 1);
5993 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005994 if (!rv)
5995 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005996 if (rv)
5997 bitmap_destroy(mddev);
5998 mddev->pers->quiesce(mddev, 0);
5999 } else {
6000 /* remove the bitmap */
6001 if (!mddev->bitmap)
6002 return -ENOENT;
6003 if (mddev->bitmap->file)
6004 return -EINVAL;
6005 mddev->pers->quiesce(mddev, 1);
6006 bitmap_destroy(mddev);
6007 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006008 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006009 }
6010 }
NeilBrown850b2b42006-10-03 01:15:46 -07006011 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return rv;
6013}
6014
NeilBrownfd01b882011-10-11 16:47:53 +11006015static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
NeilBrown3cb03002011-10-11 16:45:26 +11006017 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 if (mddev->pers == NULL)
6020 return -ENODEV;
6021
6022 rdev = find_rdev(mddev, dev);
6023 if (!rdev)
6024 return -ENODEV;
6025
6026 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006027 if (!test_bit(Faulty, &rdev->flags))
6028 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 return 0;
6030}
6031
Andre Noll2f9618c2008-04-25 18:57:58 +02006032/*
6033 * We have a problem here : there is no easy way to give a CHS
6034 * virtual geometry. We currently pretend that we have a 2 heads
6035 * 4 sectors (with a BIG number of cylinders...). This drives
6036 * dosfs just mad... ;-)
6037 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006038static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6039{
NeilBrownfd01b882011-10-11 16:47:53 +11006040 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006041
6042 geo->heads = 2;
6043 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006044 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006045 return 0;
6046}
6047
Al Viroa39907f2008-03-02 10:31:15 -05006048static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 unsigned int cmd, unsigned long arg)
6050{
6051 int err = 0;
6052 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006053 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006054 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
6056 if (!capable(CAP_SYS_ADMIN))
6057 return -EACCES;
6058
6059 /*
6060 * Commands dealing with the RAID driver but not any
6061 * particular array:
6062 */
6063 switch (cmd)
6064 {
6065 case RAID_VERSION:
6066 err = get_version(argp);
6067 goto done;
6068
6069 case PRINT_RAID_DEBUG:
6070 err = 0;
6071 md_print_devices();
6072 goto done;
6073
6074#ifndef MODULE
6075 case RAID_AUTORUN:
6076 err = 0;
6077 autostart_arrays(arg);
6078 goto done;
6079#endif
6080 default:;
6081 }
6082
6083 /*
6084 * Commands creating/starting a new array:
6085 */
6086
Al Viroa39907f2008-03-02 10:31:15 -05006087 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
6089 if (!mddev) {
6090 BUG();
6091 goto abort;
6092 }
6093
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 err = mddev_lock(mddev);
6095 if (err) {
6096 printk(KERN_INFO
6097 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6098 err, cmd);
6099 goto abort;
6100 }
6101
6102 switch (cmd)
6103 {
6104 case SET_ARRAY_INFO:
6105 {
6106 mdu_array_info_t info;
6107 if (!arg)
6108 memset(&info, 0, sizeof(info));
6109 else if (copy_from_user(&info, argp, sizeof(info))) {
6110 err = -EFAULT;
6111 goto abort_unlock;
6112 }
6113 if (mddev->pers) {
6114 err = update_array_info(mddev, &info);
6115 if (err) {
6116 printk(KERN_WARNING "md: couldn't update"
6117 " array info. %d\n", err);
6118 goto abort_unlock;
6119 }
6120 goto done_unlock;
6121 }
6122 if (!list_empty(&mddev->disks)) {
6123 printk(KERN_WARNING
6124 "md: array %s already has disks!\n",
6125 mdname(mddev));
6126 err = -EBUSY;
6127 goto abort_unlock;
6128 }
6129 if (mddev->raid_disks) {
6130 printk(KERN_WARNING
6131 "md: array %s already initialised!\n",
6132 mdname(mddev));
6133 err = -EBUSY;
6134 goto abort_unlock;
6135 }
6136 err = set_array_info(mddev, &info);
6137 if (err) {
6138 printk(KERN_WARNING "md: couldn't set"
6139 " array info. %d\n", err);
6140 goto abort_unlock;
6141 }
6142 }
6143 goto done_unlock;
6144
6145 default:;
6146 }
6147
6148 /*
6149 * Commands querying/configuring an existing array:
6150 */
NeilBrown32a76272005-06-21 17:17:14 -07006151 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006152 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006153 if ((!mddev->raid_disks && !mddev->external)
6154 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6155 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6156 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 err = -ENODEV;
6158 goto abort_unlock;
6159 }
6160
6161 /*
6162 * Commands even a read-only array can execute:
6163 */
6164 switch (cmd)
6165 {
6166 case GET_ARRAY_INFO:
6167 err = get_array_info(mddev, argp);
6168 goto done_unlock;
6169
NeilBrown32a76272005-06-21 17:17:14 -07006170 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006171 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006172 goto done_unlock;
6173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 case GET_DISK_INFO:
6175 err = get_disk_info(mddev, argp);
6176 goto done_unlock;
6177
6178 case RESTART_ARRAY_RW:
6179 err = restart_array(mddev);
6180 goto done_unlock;
6181
6182 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006183 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 goto done_unlock;
6185
6186 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006187 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 goto done_unlock;
6189
Dan Williamse2218352010-05-12 08:25:37 +10006190 case BLKROSET:
6191 if (get_user(ro, (int __user *)(arg))) {
6192 err = -EFAULT;
6193 goto done_unlock;
6194 }
6195 err = -EINVAL;
6196
6197 /* if the bdev is going readonly the value of mddev->ro
6198 * does not matter, no writes are coming
6199 */
6200 if (ro)
6201 goto done_unlock;
6202
6203 /* are we are already prepared for writes? */
6204 if (mddev->ro != 1)
6205 goto done_unlock;
6206
6207 /* transitioning to readauto need only happen for
6208 * arrays that call md_write_start
6209 */
6210 if (mddev->pers) {
6211 err = restart_array(mddev);
6212 if (err == 0) {
6213 mddev->ro = 2;
6214 set_disk_ro(mddev->gendisk, 0);
6215 }
6216 }
6217 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 }
6219
6220 /*
6221 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006222 * superblock, so we do not allow them on read-only arrays.
6223 * However non-MD ioctls (e.g. get-size) will still come through
6224 * here and hit the 'default' below, so only disallow
6225 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006227 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006228 if (mddev->ro == 2) {
6229 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006230 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6232 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006233 } else {
6234 err = -EROFS;
6235 goto abort_unlock;
6236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 }
6238
6239 switch (cmd)
6240 {
6241 case ADD_NEW_DISK:
6242 {
6243 mdu_disk_info_t info;
6244 if (copy_from_user(&info, argp, sizeof(info)))
6245 err = -EFAULT;
6246 else
6247 err = add_new_disk(mddev, &info);
6248 goto done_unlock;
6249 }
6250
6251 case HOT_REMOVE_DISK:
6252 err = hot_remove_disk(mddev, new_decode_dev(arg));
6253 goto done_unlock;
6254
6255 case HOT_ADD_DISK:
6256 err = hot_add_disk(mddev, new_decode_dev(arg));
6257 goto done_unlock;
6258
6259 case SET_DISK_FAULTY:
6260 err = set_disk_faulty(mddev, new_decode_dev(arg));
6261 goto done_unlock;
6262
6263 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006264 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 goto done_unlock;
6266
NeilBrown32a76272005-06-21 17:17:14 -07006267 case SET_BITMAP_FILE:
6268 err = set_bitmap_file(mddev, (int)arg);
6269 goto done_unlock;
6270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 err = -EINVAL;
6273 goto abort_unlock;
6274 }
6275
6276done_unlock:
6277abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006278 if (mddev->hold_active == UNTIL_IOCTL &&
6279 err != -EINVAL)
6280 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 mddev_unlock(mddev);
6282
6283 return err;
6284done:
6285 if (err)
6286 MD_BUG();
6287abort:
6288 return err;
6289}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006290#ifdef CONFIG_COMPAT
6291static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6292 unsigned int cmd, unsigned long arg)
6293{
6294 switch (cmd) {
6295 case HOT_REMOVE_DISK:
6296 case HOT_ADD_DISK:
6297 case SET_DISK_FAULTY:
6298 case SET_BITMAP_FILE:
6299 /* These take in integer arg, do not convert */
6300 break;
6301 default:
6302 arg = (unsigned long)compat_ptr(arg);
6303 break;
6304 }
6305
6306 return md_ioctl(bdev, mode, cmd, arg);
6307}
6308#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
Al Viroa39907f2008-03-02 10:31:15 -05006310static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
6312 /*
6313 * Succeed if we can lock the mddev, which confirms that
6314 * it isn't being stopped right now.
6315 */
NeilBrownfd01b882011-10-11 16:47:53 +11006316 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 int err;
6318
NeilBrownd3374822009-01-09 08:31:10 +11006319 if (mddev->gendisk != bdev->bd_disk) {
6320 /* we are racing with mddev_put which is discarding this
6321 * bd_disk.
6322 */
6323 mddev_put(mddev);
6324 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006325 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006326 /* Then retry the open from the top */
6327 return -ERESTARTSYS;
6328 }
6329 BUG_ON(mddev != bdev->bd_disk->private_data);
6330
NeilBrownc8c00a62009-08-10 12:50:52 +10006331 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 goto out;
6333
6334 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006335 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006336 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006338 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 out:
6340 return err;
6341}
6342
Al Viroa39907f2008-03-02 10:31:15 -05006343static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
NeilBrownfd01b882011-10-11 16:47:53 +11006345 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006347 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006348 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 mddev_put(mddev);
6350
6351 return 0;
6352}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006353
6354static int md_media_changed(struct gendisk *disk)
6355{
NeilBrownfd01b882011-10-11 16:47:53 +11006356 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006357
6358 return mddev->changed;
6359}
6360
6361static int md_revalidate(struct gendisk *disk)
6362{
NeilBrownfd01b882011-10-11 16:47:53 +11006363 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006364
6365 mddev->changed = 0;
6366 return 0;
6367}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006368static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369{
6370 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006371 .open = md_open,
6372 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006373 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006374#ifdef CONFIG_COMPAT
6375 .compat_ioctl = md_compat_ioctl,
6376#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006377 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006378 .media_changed = md_media_changed,
6379 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380};
6381
Adrian Bunk75c96f82005-05-05 16:16:09 -07006382static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
NeilBrown2b8bf342011-10-11 16:48:23 +11006384 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 /*
6387 * md_thread is a 'system-thread', it's priority should be very
6388 * high. We avoid resource deadlocks individually in each
6389 * raid personality. (RAID5 does preallocation) We also use RR and
6390 * the very same RT priority as kswapd, thus we will never get
6391 * into a priority inversion deadlock.
6392 *
6393 * we definitely have to have equal or higher priority than
6394 * bdflush, otherwise bdflush will deadlock if there are too
6395 * many dirty RAID5 blocks.
6396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
NeilBrown6985c432005-10-19 21:23:47 -07006398 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006399 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
NeilBrown93588e22005-11-15 00:09:12 -08006401 /* We need to wait INTERRUPTIBLE so that
6402 * we don't add to the load-average.
6403 * That means we need to be sure no signals are
6404 * pending
6405 */
6406 if (signal_pending(current))
6407 flush_signals(current);
6408
6409 wait_event_interruptible_timeout
6410 (thread->wqueue,
6411 test_bit(THREAD_WAKEUP, &thread->flags)
6412 || kthread_should_stop(),
6413 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414
NeilBrown6c987912011-01-14 09:13:53 +11006415 clear_bit(THREAD_WAKEUP, &thread->flags);
6416 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006417 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006419
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 return 0;
6421}
6422
NeilBrown2b8bf342011-10-11 16:48:23 +11006423void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
6425 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006426 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 set_bit(THREAD_WAKEUP, &thread->flags);
6428 wake_up(&thread->wqueue);
6429 }
6430}
6431
NeilBrown2b8bf342011-10-11 16:48:23 +11006432struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 const char *name)
6434{
NeilBrown2b8bf342011-10-11 16:48:23 +11006435 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436
NeilBrown2b8bf342011-10-11 16:48:23 +11006437 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 if (!thread)
6439 return NULL;
6440
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 init_waitqueue_head(&thread->wqueue);
6442
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 thread->run = run;
6444 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006445 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006446 thread->tsk = kthread_run(md_thread, thread,
6447 "%s_%s",
6448 mdname(thread->mddev),
6449 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006450 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 kfree(thread);
6452 return NULL;
6453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 return thread;
6455}
6456
NeilBrown2b8bf342011-10-11 16:48:23 +11006457void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458{
NeilBrown2b8bf342011-10-11 16:48:23 +11006459 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006460 if (!thread)
6461 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006462 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006463 /* Locking ensures that mddev_unlock does not wake_up a
6464 * non-existent thread
6465 */
6466 spin_lock(&pers_lock);
6467 *threadp = NULL;
6468 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006469
6470 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 kfree(thread);
6472}
6473
NeilBrownfd01b882011-10-11 16:47:53 +11006474void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
6476 if (!mddev) {
6477 MD_BUG();
6478 return;
6479 }
6480
NeilBrownb2d444d2005-11-08 21:39:31 -08006481 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006483
NeilBrownde393cd2011-07-28 11:31:48 +10006484 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 return;
6486 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006487 if (mddev->degraded)
6488 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006489 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6491 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6492 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006493 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006494 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006495 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496}
6497
6498/* seq_file implementation /proc/mdstat */
6499
6500static void status_unused(struct seq_file *seq)
6501{
6502 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006503 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 seq_printf(seq, "unused devices: ");
6506
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006507 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 char b[BDEVNAME_SIZE];
6509 i++;
6510 seq_printf(seq, "%s ",
6511 bdevname(rdev->bdev,b));
6512 }
6513 if (!i)
6514 seq_printf(seq, "<none>");
6515
6516 seq_printf(seq, "\n");
6517}
6518
6519
NeilBrownfd01b882011-10-11 16:47:53 +11006520static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
NeilBrowndd71cf62009-05-07 12:49:35 +10006522 sector_t max_sectors, resync, res;
6523 unsigned long dt, db;
6524 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006525 int scale;
6526 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
NeilBrowndd71cf62009-05-07 12:49:35 +10006528 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
6530 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006531 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006533 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
6535 /*
6536 * Should not happen.
6537 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006538 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 MD_BUG();
6540 return;
6541 }
NeilBrown4588b422006-03-27 01:18:04 -08006542 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006543 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006544 * u32, as those are the requirements for sector_div.
6545 * Thus 'scale' must be at least 10
6546 */
6547 scale = 10;
6548 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006549 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006550 scale++;
6551 }
6552 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006553 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006554
6555 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 {
NeilBrown4588b422006-03-27 01:18:04 -08006557 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 seq_printf(seq, "[");
6559 for (i = 0; i < x; i++)
6560 seq_printf(seq, "=");
6561 seq_printf(seq, ">");
6562 for (i = 0; i < y; i++)
6563 seq_printf(seq, ".");
6564 seq_printf(seq, "] ");
6565 }
NeilBrown4588b422006-03-27 01:18:04 -08006566 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006567 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6568 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006569 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6570 "check" :
6571 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6572 "resync" : "recovery"))),
6573 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006574 (unsigned long long) resync/2,
6575 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
6577 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 * dt: time from mark until now
6579 * db: blocks written from mark until now
6580 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006581 *
6582 * rt is a sector_t, so could be 32bit or 64bit.
6583 * So we divide before multiply in case it is 32bit and close
6584 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006585 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006586 * near the end of resync when the number of remaining sectors
6587 * is close to 'db'.
6588 * We then divide rt by 32 after multiplying by db to compensate.
6589 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 */
6591 dt = ((jiffies - mddev->resync_mark) / HZ);
6592 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006593 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6594 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
NeilBrowndd71cf62009-05-07 12:49:35 +10006596 rt = max_sectors - resync; /* number of remaining sectors */
6597 sector_div(rt, db/32+1);
6598 rt *= dt;
6599 rt >>= 5;
6600
6601 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6602 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
NeilBrownff4e8d92006-07-10 04:44:16 -07006604 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605}
6606
6607static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6608{
6609 struct list_head *tmp;
6610 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006611 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
6613 if (l >= 0x10000)
6614 return NULL;
6615 if (!l--)
6616 /* header */
6617 return (void*)1;
6618
6619 spin_lock(&all_mddevs_lock);
6620 list_for_each(tmp,&all_mddevs)
6621 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006622 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 mddev_get(mddev);
6624 spin_unlock(&all_mddevs_lock);
6625 return mddev;
6626 }
6627 spin_unlock(&all_mddevs_lock);
6628 if (!l--)
6629 return (void*)2;/* tail */
6630 return NULL;
6631}
6632
6633static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6634{
6635 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006636 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
6638 ++*pos;
6639 if (v == (void*)2)
6640 return NULL;
6641
6642 spin_lock(&all_mddevs_lock);
6643 if (v == (void*)1)
6644 tmp = all_mddevs.next;
6645 else
6646 tmp = mddev->all_mddevs.next;
6647 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006648 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 else {
6650 next_mddev = (void*)2;
6651 *pos = 0x10000;
6652 }
6653 spin_unlock(&all_mddevs_lock);
6654
6655 if (v != (void*)1)
6656 mddev_put(mddev);
6657 return next_mddev;
6658
6659}
6660
6661static void md_seq_stop(struct seq_file *seq, void *v)
6662{
NeilBrownfd01b882011-10-11 16:47:53 +11006663 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 if (mddev && v != (void*)1 && v != (void*)2)
6666 mddev_put(mddev);
6667}
6668
6669static int md_seq_show(struct seq_file *seq, void *v)
6670{
NeilBrownfd01b882011-10-11 16:47:53 +11006671 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006672 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006673 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006674 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
6676 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006677 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 seq_printf(seq, "Personalities : ");
6679 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006680 list_for_each_entry(pers, &pers_list, list)
6681 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682
6683 spin_unlock(&pers_lock);
6684 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006685 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 return 0;
6687 }
6688 if (v == (void*)2) {
6689 status_unused(seq);
6690 return 0;
6691 }
6692
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006693 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006695
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6697 seq_printf(seq, "%s : %sactive", mdname(mddev),
6698 mddev->pers ? "" : "in");
6699 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006700 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006702 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006703 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 seq_printf(seq, " %s", mddev->pers->name);
6705 }
6706
Andre Nolldd8ac332009-03-31 14:33:13 +11006707 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006708 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 char b[BDEVNAME_SIZE];
6710 seq_printf(seq, " %s[%d]",
6711 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006712 if (test_bit(WriteMostly, &rdev->flags))
6713 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006714 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 seq_printf(seq, "(F)");
6716 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006717 } else if (rdev->raid_disk < 0)
6718 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006719 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 }
6721
6722 if (!list_empty(&mddev->disks)) {
6723 if (mddev->pers)
6724 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006725 (unsigned long long)
6726 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 else
6728 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006729 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006731 if (mddev->persistent) {
6732 if (mddev->major_version != 0 ||
6733 mddev->minor_version != 90) {
6734 seq_printf(seq," super %d.%d",
6735 mddev->major_version,
6736 mddev->minor_version);
6737 }
NeilBrowne6910632008-02-06 01:39:51 -08006738 } else if (mddev->external)
6739 seq_printf(seq, " super external:%s",
6740 mddev->metadata_type);
6741 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006742 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
6744 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006745 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006747 if (mddev->pers->sync_request) {
6748 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006749 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006750 seq_printf(seq, "\n ");
6751 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6752 seq_printf(seq, "\tresync=DELAYED\n ");
6753 else if (mddev->recovery_cp < MaxSector)
6754 seq_printf(seq, "\tresync=PENDING\n ");
6755 }
NeilBrown32a76272005-06-21 17:17:14 -07006756 } else
6757 seq_printf(seq, "\n ");
6758
6759 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006760 unsigned long chunk_kb;
6761 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006762 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006763 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006764 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6765 "%lu%s chunk",
6766 bitmap->pages - bitmap->missing_pages,
6767 bitmap->pages,
6768 (bitmap->pages - bitmap->missing_pages)
6769 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006770 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006771 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006772 if (bitmap->file) {
6773 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006774 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006775 }
NeilBrown78d742d2005-06-21 17:17:15 -07006776
NeilBrown32a76272005-06-21 17:17:14 -07006777 seq_printf(seq, "\n");
6778 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 }
6780
6781 seq_printf(seq, "\n");
6782 }
6783 mddev_unlock(mddev);
6784
6785 return 0;
6786}
6787
Jan Engelhardt110518b2009-05-07 12:49:37 +10006788static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 .start = md_seq_start,
6790 .next = md_seq_next,
6791 .stop = md_seq_stop,
6792 .show = md_seq_show,
6793};
6794
6795static int md_seq_open(struct inode *inode, struct file *file)
6796{
Kay Sieversf1514632011-07-12 20:48:39 +02006797 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 int error;
6799
6800 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006801 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006802 return error;
6803
6804 seq = file->private_data;
6805 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 return error;
6807}
6808
NeilBrownd7603b72006-01-06 00:20:30 -08006809static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6810{
Kay Sieversf1514632011-07-12 20:48:39 +02006811 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006812 int mask;
6813
6814 poll_wait(filp, &md_event_waiters, wait);
6815
6816 /* always allow read */
6817 mask = POLLIN | POLLRDNORM;
6818
Kay Sieversf1514632011-07-12 20:48:39 +02006819 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006820 mask |= POLLERR | POLLPRI;
6821 return mask;
6822}
6823
Arjan van de Venfa027c22007-02-12 00:55:33 -08006824static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006825 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 .open = md_seq_open,
6827 .read = seq_read,
6828 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006829 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006830 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831};
6832
NeilBrown84fc4b52011-10-11 16:49:58 +11006833int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006836 list_add_tail(&p->list, &pers_list);
6837 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 spin_unlock(&pers_lock);
6839 return 0;
6840}
6841
NeilBrown84fc4b52011-10-11 16:49:58 +11006842int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843{
NeilBrown2604b702006-01-06 00:20:36 -08006844 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006846 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 spin_unlock(&pers_lock);
6848 return 0;
6849}
6850
NeilBrownfd01b882011-10-11 16:47:53 +11006851static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852{
NeilBrown3cb03002011-10-11 16:45:26 +11006853 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006855 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
6857 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006858 rcu_read_lock();
6859 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006861 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6862 (int)part_stat_read(&disk->part0, sectors[1]) -
6863 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006864 /* sync IO will cause sync_io to increase before the disk_stats
6865 * as sync_io is counted when a request starts, and
6866 * disk_stats is counted when it completes.
6867 * So resync activity will cause curr_events to be smaller than
6868 * when there was no such activity.
6869 * non-sync IO will cause disk_stat to increase without
6870 * increasing sync_io so curr_events will (eventually)
6871 * be larger than it was before. Once it becomes
6872 * substantially larger, the test below will cause
6873 * the array to appear non-idle, and resync will slow
6874 * down.
6875 * If there is a lot of outstanding resync activity when
6876 * we set last_event to curr_events, then all that activity
6877 * completing might cause the array to appear non-idle
6878 * and resync will be slowed down even though there might
6879 * not have been non-resync activity. This will only
6880 * happen once though. 'last_events' will soon reflect
6881 * the state where there is little or no outstanding
6882 * resync requests, and further resync activity will
6883 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006884 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006886 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 rdev->last_events = curr_events;
6888 idle = 0;
6889 }
6890 }
NeilBrown4b809912008-07-21 17:05:25 +10006891 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 return idle;
6893}
6894
NeilBrownfd01b882011-10-11 16:47:53 +11006895void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
6897 /* another "blocks" (512byte) blocks have been synced */
6898 atomic_sub(blocks, &mddev->recovery_active);
6899 wake_up(&mddev->recovery_wait);
6900 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006901 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 md_wakeup_thread(mddev->thread);
6903 // stop recovery, signal do_sync ....
6904 }
6905}
6906
6907
NeilBrown06d91a52005-06-21 17:17:12 -07006908/* md_write_start(mddev, bi)
6909 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006910 * in superblock) before writing, schedule a superblock update
6911 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006912 */
NeilBrownfd01b882011-10-11 16:47:53 +11006913void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914{
Neil Brown0fd62b82008-06-28 08:31:36 +10006915 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006916 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006917 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006918
NeilBrownf91de922005-11-08 21:39:36 -08006919 BUG_ON(mddev->ro == 1);
6920 if (mddev->ro == 2) {
6921 /* need to switch to read/write */
6922 mddev->ro = 0;
6923 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6924 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006925 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006926 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006927 }
NeilBrown06d91a52005-06-21 17:17:12 -07006928 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006929 if (mddev->safemode == 1)
6930 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006931 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006932 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006933 if (mddev->in_sync) {
6934 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006935 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006936 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006937 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006938 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006939 }
NeilBrowna9701a32005-11-08 21:39:34 -08006940 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006941 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006942 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006943 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006944 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006945 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
NeilBrownfd01b882011-10-11 16:47:53 +11006948void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949{
6950 if (atomic_dec_and_test(&mddev->writes_pending)) {
6951 if (mddev->safemode == 2)
6952 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006953 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6955 }
6956}
6957
NeilBrown2a2275d2007-01-26 00:57:11 -08006958/* md_allow_write(mddev)
6959 * Calling this ensures that the array is marked 'active' so that writes
6960 * may proceed without blocking. It is important to call this before
6961 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6962 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006963 *
6964 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6965 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006966 */
NeilBrownfd01b882011-10-11 16:47:53 +11006967int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006968{
6969 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006970 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006971 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006972 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006973 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006974 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006975
6976 spin_lock_irq(&mddev->write_lock);
6977 if (mddev->in_sync) {
6978 mddev->in_sync = 0;
6979 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006980 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006981 if (mddev->safemode_delay &&
6982 mddev->safemode == 0)
6983 mddev->safemode = 1;
6984 spin_unlock_irq(&mddev->write_lock);
6985 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006986 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006987 } else
6988 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006989
NeilBrown070dc6d2010-08-30 17:33:34 +10006990 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006991 return -EAGAIN;
6992 else
6993 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006994}
6995EXPORT_SYMBOL_GPL(md_allow_write);
6996
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997#define SYNC_MARKS 10
6998#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11006999void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000{
NeilBrownfd01b882011-10-11 16:47:53 +11007001 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 unsigned int currspeed = 0,
7003 window;
NeilBrown57afd892005-06-21 17:17:13 -07007004 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 unsigned long mark[SYNC_MARKS];
7006 sector_t mark_cnt[SYNC_MARKS];
7007 int last_mark,m;
7008 struct list_head *tmp;
7009 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007010 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007011 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007012 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 /* just incase thread restarts... */
7015 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7016 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007017 if (mddev->ro) /* never try to sync a read-only array */
7018 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
NeilBrown61df9d92006-10-03 01:15:57 -07007020 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7021 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7022 desc = "data-check";
7023 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7024 desc = "requested-resync";
7025 else
7026 desc = "resync";
7027 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7028 desc = "reshape";
7029 else
7030 desc = "recovery";
7031
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 /* we overload curr_resync somewhat here.
7033 * 0 == not engaged in resync at all
7034 * 2 == checking that there is no conflict with another sync
7035 * 1 == like 2, but have yielded to allow conflicting resync to
7036 * commense
7037 * other == active in resync - this many blocks
7038 *
7039 * Before starting a resync we must have set curr_resync to
7040 * 2, and then checked that every "conflicting" array has curr_resync
7041 * less than ours. When we find one that is the same or higher
7042 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7043 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7044 * This will mean we have to start checking from the beginning again.
7045 *
7046 */
7047
7048 do {
7049 mddev->curr_resync = 2;
7050
7051 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007052 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007053 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007054
7055 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007057 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 if (mddev2 == mddev)
7059 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007060 if (!mddev->parallel_resync
7061 && mddev2->curr_resync
7062 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 DEFINE_WAIT(wq);
7064 if (mddev < mddev2 && mddev->curr_resync == 2) {
7065 /* arbitrarily yield */
7066 mddev->curr_resync = 1;
7067 wake_up(&resync_wait);
7068 }
7069 if (mddev > mddev2 && mddev->curr_resync == 1)
7070 /* no need to wait here, we can wait the next
7071 * time 'round when curr_resync == 2
7072 */
7073 continue;
NeilBrown97441972008-09-19 11:49:54 +10007074 /* We need to wait 'interruptible' so as not to
7075 * contribute to the load average, and not to
7076 * be caught by 'softlockup'
7077 */
7078 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007079 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007080 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007081 printk(KERN_INFO "md: delaying %s of %s"
7082 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007084 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007086 if (signal_pending(current))
7087 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 schedule();
7089 finish_wait(&resync_wait, &wq);
7090 goto try_again;
7091 }
7092 finish_wait(&resync_wait, &wq);
7093 }
7094 }
7095 } while (mddev->curr_resync < 2);
7096
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007097 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007098 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007100 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 */
7102 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007103 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007104 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007105 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7106 j = mddev->resync_min;
7107 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007108 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007109
NeilBrownccfcc3c2006-03-27 01:18:09 -08007110 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007111 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007112 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007114 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007115 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007116 rcu_read_lock();
7117 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007118 if (rdev->raid_disk >= 0 &&
7119 !test_bit(Faulty, &rdev->flags) &&
7120 !test_bit(In_sync, &rdev->flags) &&
7121 rdev->recovery_offset < j)
7122 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007123 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
NeilBrown61df9d92006-10-03 01:15:57 -07007126 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7127 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7128 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007129 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007130 "(but not more than %d KB/sec) for %s.\n",
7131 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
NeilBrowneea1bf32009-03-31 14:27:02 +11007133 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007134
NeilBrown57afd892005-06-21 17:17:13 -07007135 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 for (m = 0; m < SYNC_MARKS; m++) {
7137 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007138 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 }
7140 last_mark = 0;
7141 mddev->resync_mark = mark[last_mark];
7142 mddev->resync_mark_cnt = mark_cnt[last_mark];
7143
7144 /*
7145 * Tune reconstruction:
7146 */
7147 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007148 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7149 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
7151 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 last_check = 0;
7153
7154 if (j>2) {
7155 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007156 "md: resuming %s of %s from checkpoint.\n",
7157 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 mddev->curr_resync = j;
7159 }
NeilBrown75d3da42011-01-14 09:14:34 +11007160 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
7162 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007163 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
NeilBrown57afd892005-06-21 17:17:13 -07007165 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007166
NeilBrown7a91ee12009-05-26 12:57:21 +10007167 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7168 ((mddev->curr_resync > mddev->curr_resync_completed &&
7169 (mddev->curr_resync - mddev->curr_resync_completed)
7170 > (max_sectors >> 4)) ||
7171 (j - mddev->curr_resync_completed)*2
7172 >= mddev->resync_max - mddev->curr_resync_completed
7173 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007174 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007175 wait_event(mddev->recovery_wait,
7176 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007177 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007178 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007179 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007180 }
NeilBrownacb180b2009-04-14 16:28:34 +10007181
NeilBrowne62e58a2009-07-01 13:15:35 +10007182 while (j >= mddev->resync_max && !kthread_should_stop()) {
7183 /* As this condition is controlled by user-space,
7184 * we can block indefinitely, so use '_interruptible'
7185 * to avoid triggering warnings.
7186 */
7187 flush_signals(current); /* just in case */
7188 wait_event_interruptible(mddev->recovery_wait,
7189 mddev->resync_max > j
7190 || kthread_should_stop());
7191 }
NeilBrownacb180b2009-04-14 16:28:34 +10007192
7193 if (kthread_should_stop())
7194 goto interrupted;
7195
NeilBrown57afd892005-06-21 17:17:13 -07007196 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007197 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007198 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007199 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 goto out;
7201 }
NeilBrown57afd892005-06-21 17:17:13 -07007202
7203 if (!skipped) { /* actual IO requested */
7204 io_sectors += sectors;
7205 atomic_add(sectors, &mddev->recovery_active);
7206 }
7207
NeilBrowne875ece2011-07-28 11:39:24 +10007208 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7209 break;
7210
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 j += sectors;
7212 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007213 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007214 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007215 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007216 * visible in /proc/mdstat
7217 */
7218 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007219
7220 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 continue;
7222
NeilBrown57afd892005-06-21 17:17:13 -07007223 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 repeat:
7225 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7226 /* step marks */
7227 int next = (last_mark+1) % SYNC_MARKS;
7228
7229 mddev->resync_mark = mark[next];
7230 mddev->resync_mark_cnt = mark_cnt[next];
7231 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007232 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 last_mark = next;
7234 }
7235
7236
NeilBrownc6207272008-02-06 01:39:52 -08007237 if (kthread_should_stop())
7238 goto interrupted;
7239
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
7241 /*
7242 * this loop exits only if either when we are slower than
7243 * the 'hard' speed limit, or the system was IO-idle for
7244 * a jiffy.
7245 * the system might be non-idle CPU-wise, but we only care
7246 * about not overloading the IO subsystem. (things like an
7247 * e2fsck being done on the RAID array should execute fast)
7248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 cond_resched();
7250
NeilBrown57afd892005-06-21 17:17:13 -07007251 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7252 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
NeilBrown88202a02006-01-06 00:21:36 -08007254 if (currspeed > speed_min(mddev)) {
7255 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007256 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007257 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 goto repeat;
7259 }
7260 }
7261 }
NeilBrown61df9d92006-10-03 01:15:57 -07007262 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 /*
7264 * this also signals 'finished resyncing' to md_stop
7265 */
7266 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7268
7269 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007270 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271
NeilBrowndfc70642008-05-23 13:04:39 -07007272 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007273 mddev->curr_resync > 2) {
7274 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7275 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7276 if (mddev->curr_resync >= mddev->recovery_cp) {
7277 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007278 "md: checkpointing %s of %s.\n",
7279 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007280 mddev->recovery_cp = mddev->curr_resync;
7281 }
7282 } else
7283 mddev->recovery_cp = MaxSector;
7284 } else {
7285 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7286 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007287 rcu_read_lock();
7288 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007289 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007290 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007291 !test_bit(Faulty, &rdev->flags) &&
7292 !test_bit(In_sync, &rdev->flags) &&
7293 rdev->recovery_offset < mddev->curr_resync)
7294 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007295 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 }
NeilBrown17571282006-12-10 02:20:52 -08007298 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007301 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7302 /* We completed so min/max setting can be forgotten if used. */
7303 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7304 mddev->resync_min = 0;
7305 mddev->resync_max = MaxSector;
7306 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7307 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 mddev->curr_resync = 0;
7309 wake_up(&resync_wait);
7310 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7311 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007312 return;
7313
7314 interrupted:
7315 /*
7316 * got a signal, exit.
7317 */
7318 printk(KERN_INFO
7319 "md: md_do_sync() got signal ... exiting\n");
7320 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7321 goto out;
7322
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
NeilBrown29269552006-03-27 01:18:10 -08007324EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
NeilBrownfd01b882011-10-11 16:47:53 +11007326static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007327{
NeilBrown3cb03002011-10-11 16:45:26 +11007328 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007329 int spares = 0;
7330
NeilBrown97e4f422009-03-31 14:33:13 +11007331 mddev->curr_resync_completed = 0;
7332
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007333 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007334 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007335 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007336 (test_bit(Faulty, &rdev->flags) ||
7337 ! test_bit(In_sync, &rdev->flags)) &&
7338 atomic_read(&rdev->nr_pending)==0) {
7339 if (mddev->pers->hot_remove_disk(
7340 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007341 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007342 rdev->raid_disk = -1;
7343 }
7344 }
7345
NeilBrown53890422011-07-27 11:00:36 +10007346 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007347 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007348 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007349 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007350 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007351 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007352 if (rdev->raid_disk < 0
7353 && !test_bit(Faulty, &rdev->flags)) {
7354 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007355 if (mddev->pers->
7356 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007357 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007358 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007359 spares++;
7360 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007361 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown60fc1372011-12-23 09:57:19 +11007362 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007363 }
NeilBrowndfc70642008-05-23 13:04:39 -07007364 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007365 }
7366 return spares;
7367}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007368
NeilBrownfd01b882011-10-11 16:47:53 +11007369static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007370{
NeilBrown3cb03002011-10-11 16:45:26 +11007371 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007372
7373 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007374 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007375 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7376 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7377 /* success...*/
7378 /* activate any spares */
7379 if (mddev->pers->spare_active(mddev))
7380 sysfs_notify(&mddev->kobj, NULL,
7381 "degraded");
7382 }
7383 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7384 mddev->pers->finish_reshape)
7385 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007386
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007387 /* If array is no-longer degraded, then any saved_raid_disk
7388 * information must be scrapped. Also if any device is now
7389 * In_sync we must scrape the saved_raid_disk for that device
7390 * do the superblock for an incrementally recovered device
7391 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007392 */
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007393 list_for_each_entry(rdev, &mddev->disks, same_set)
7394 if (!mddev->degraded ||
7395 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007396 rdev->saved_raid_disk = -1;
7397
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007398 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007399 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7400 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7401 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7402 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7403 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7404 /* flag recovery needed just to double check */
7405 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7406 sysfs_notify_dirent_safe(mddev->sysfs_action);
7407 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007408 if (mddev->event_work.func)
7409 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007410}
7411
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412/*
7413 * This routine is regularly called by all per-raid-array threads to
7414 * deal with generic issues like resync and super-block update.
7415 * Raid personalities that don't have a thread (linear/raid0) do not
7416 * need this as they never do any recovery or update the superblock.
7417 *
7418 * It does not do any resync itself, but rather "forks" off other threads
7419 * to do that as needed.
7420 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7421 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007422 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 * and wakeups up this thread which will reap the thread and finish up.
7424 * This thread also removes any faulty devices (with nr_pending == 0).
7425 *
7426 * The overall approach is:
7427 * 1/ if the superblock needs updating, update it.
7428 * 2/ If a recovery thread is running, don't do anything else.
7429 * 3/ If recovery has finished, clean up, possibly marking spares active.
7430 * 4/ If there are any faulty devices, remove them.
7431 * 5/ If array is degraded, try to add spares devices
7432 * 6/ If array has spares or is not in-sync, start a resync thread.
7433 */
NeilBrownfd01b882011-10-11 16:47:53 +11007434void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007436 if (mddev->suspended)
7437 return;
7438
NeilBrown5f404022005-06-21 17:17:16 -07007439 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007440 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
NeilBrownfca4d842005-06-21 17:17:11 -07007442 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007443 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007444 printk(KERN_INFO "md: %s in immediate safe mode\n",
7445 mdname(mddev));
7446 mddev->safemode = 2;
7447 }
7448 flush_signals(current);
7449 }
7450
NeilBrownc89a8ee2008-08-05 15:54:13 +10007451 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007454 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007456 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007457 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007458 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7459 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 ))
7461 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007462
NeilBrowndf5b89b2006-03-27 01:18:20 -08007463 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007464 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007465
NeilBrownc89a8ee2008-08-05 15:54:13 +10007466 if (mddev->ro) {
7467 /* Only thing we do on a ro array is remove
7468 * failed devices.
7469 */
NeilBrown3cb03002011-10-11 16:45:26 +11007470 struct md_rdev *rdev;
NeilBrowna8c42c72011-01-31 13:47:13 +11007471 list_for_each_entry(rdev, &mddev->disks, same_set)
7472 if (rdev->raid_disk >= 0 &&
7473 !test_bit(Blocked, &rdev->flags) &&
7474 test_bit(Faulty, &rdev->flags) &&
7475 atomic_read(&rdev->nr_pending)==0) {
7476 if (mddev->pers->hot_remove_disk(
7477 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007478 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007479 rdev->raid_disk = -1;
7480 }
7481 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007482 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7483 goto unlock;
7484 }
7485
NeilBrown31a59e32008-04-30 00:52:30 -07007486 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007487 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007488 spin_lock_irq(&mddev->write_lock);
7489 if (mddev->safemode &&
7490 !atomic_read(&mddev->writes_pending) &&
7491 !mddev->in_sync &&
7492 mddev->recovery_cp == MaxSector) {
7493 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007494 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007495 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007496 }
7497 if (mddev->safemode == 1)
7498 mddev->safemode = 0;
7499 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007500 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007501 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007502 }
NeilBrownfca4d842005-06-21 17:17:11 -07007503
NeilBrown850b2b42006-10-03 01:15:46 -07007504 if (mddev->flags)
7505 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007506
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7508 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7509 /* resync/recovery still happening */
7510 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7511 goto unlock;
7512 }
7513 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007514 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 goto unlock;
7516 }
Neil Brown72a23c22008-06-28 08:31:41 +10007517 /* Set RUNNING before clearing NEEDED to avoid
7518 * any transients in the value of "sync_action".
7519 */
7520 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7521 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007522 /* Clear some bits that don't mean anything, but
7523 * might be left set
7524 */
NeilBrown24dd4692005-11-08 21:39:26 -08007525 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7526 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007528 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7529 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 /* no recovery is running.
7531 * remove any failed drives, then
7532 * add spares if possible.
7533 * Spare are also removed and re-added, to allow
7534 * the personality to fail the re-add.
7535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007537 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007538 if (mddev->pers->check_reshape == NULL ||
7539 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007540 /* Cannot proceed */
7541 goto unlock;
7542 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007543 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007544 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007545 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7546 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007547 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007548 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007549 } else if (mddev->recovery_cp < MaxSector) {
7550 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007551 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007552 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7553 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007555
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007557 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7558 /* We are adding a device or devices to an array
7559 * which has the bitmap stored on all devices.
7560 * So make sure all bitmap pages get written
7561 */
7562 bitmap_write_all(mddev->bitmap);
7563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 mddev->sync_thread = md_register_thread(md_do_sync,
7565 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007566 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 if (!mddev->sync_thread) {
7568 printk(KERN_ERR "%s: could not start resync"
7569 " thread...\n",
7570 mdname(mddev));
7571 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007572 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7573 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7574 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7575 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7576 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007577 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007579 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007580 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 }
7582 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007583 if (!mddev->sync_thread) {
7584 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7585 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7586 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007587 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007588 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 mddev_unlock(mddev);
7591 }
7592}
7593
NeilBrownfd01b882011-10-11 16:47:53 +11007594void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007595{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007596 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007597 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007598 !test_bit(Blocked, &rdev->flags) &&
7599 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007600 msecs_to_jiffies(5000));
7601 rdev_dec_pending(rdev, mddev);
7602}
7603EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7604
NeilBrown2230dfe2011-07-28 11:31:46 +10007605
7606/* Bad block management.
7607 * We can record which blocks on each device are 'bad' and so just
7608 * fail those blocks, or that stripe, rather than the whole device.
7609 * Entries in the bad-block table are 64bits wide. This comprises:
7610 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7611 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7612 * A 'shift' can be set so that larger blocks are tracked and
7613 * consequently larger devices can be covered.
7614 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7615 *
7616 * Locking of the bad-block table uses a seqlock so md_is_badblock
7617 * might need to retry if it is very unlucky.
7618 * We will sometimes want to check for bad blocks in a bi_end_io function,
7619 * so we use the write_seqlock_irq variant.
7620 *
7621 * When looking for a bad block we specify a range and want to
7622 * know if any block in the range is bad. So we binary-search
7623 * to the last range that starts at-or-before the given endpoint,
7624 * (or "before the sector after the target range")
7625 * then see if it ends after the given start.
7626 * We return
7627 * 0 if there are no known bad blocks in the range
7628 * 1 if there are known bad block which are all acknowledged
7629 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7630 * plus the start/length of the first bad section we overlap.
7631 */
7632int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7633 sector_t *first_bad, int *bad_sectors)
7634{
7635 int hi;
7636 int lo = 0;
7637 u64 *p = bb->page;
7638 int rv = 0;
7639 sector_t target = s + sectors;
7640 unsigned seq;
7641
7642 if (bb->shift > 0) {
7643 /* round the start down, and the end up */
7644 s >>= bb->shift;
7645 target += (1<<bb->shift) - 1;
7646 target >>= bb->shift;
7647 sectors = target - s;
7648 }
7649 /* 'target' is now the first block after the bad range */
7650
7651retry:
7652 seq = read_seqbegin(&bb->lock);
7653
7654 hi = bb->count;
7655
7656 /* Binary search between lo and hi for 'target'
7657 * i.e. for the last range that starts before 'target'
7658 */
7659 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7660 * are known not to be the last range before target.
7661 * VARIANT: hi-lo is the number of possible
7662 * ranges, and decreases until it reaches 1
7663 */
7664 while (hi - lo > 1) {
7665 int mid = (lo + hi) / 2;
7666 sector_t a = BB_OFFSET(p[mid]);
7667 if (a < target)
7668 /* This could still be the one, earlier ranges
7669 * could not. */
7670 lo = mid;
7671 else
7672 /* This and later ranges are definitely out. */
7673 hi = mid;
7674 }
7675 /* 'lo' might be the last that started before target, but 'hi' isn't */
7676 if (hi > lo) {
7677 /* need to check all range that end after 's' to see if
7678 * any are unacknowledged.
7679 */
7680 while (lo >= 0 &&
7681 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7682 if (BB_OFFSET(p[lo]) < target) {
7683 /* starts before the end, and finishes after
7684 * the start, so they must overlap
7685 */
7686 if (rv != -1 && BB_ACK(p[lo]))
7687 rv = 1;
7688 else
7689 rv = -1;
7690 *first_bad = BB_OFFSET(p[lo]);
7691 *bad_sectors = BB_LEN(p[lo]);
7692 }
7693 lo--;
7694 }
7695 }
7696
7697 if (read_seqretry(&bb->lock, seq))
7698 goto retry;
7699
7700 return rv;
7701}
7702EXPORT_SYMBOL_GPL(md_is_badblock);
7703
7704/*
7705 * Add a range of bad blocks to the table.
7706 * This might extend the table, or might contract it
7707 * if two adjacent ranges can be merged.
7708 * We binary-search to find the 'insertion' point, then
7709 * decide how best to handle it.
7710 */
7711static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7712 int acknowledged)
7713{
7714 u64 *p;
7715 int lo, hi;
7716 int rv = 1;
7717
7718 if (bb->shift < 0)
7719 /* badblocks are disabled */
7720 return 0;
7721
7722 if (bb->shift) {
7723 /* round the start down, and the end up */
7724 sector_t next = s + sectors;
7725 s >>= bb->shift;
7726 next += (1<<bb->shift) - 1;
7727 next >>= bb->shift;
7728 sectors = next - s;
7729 }
7730
7731 write_seqlock_irq(&bb->lock);
7732
7733 p = bb->page;
7734 lo = 0;
7735 hi = bb->count;
7736 /* Find the last range that starts at-or-before 's' */
7737 while (hi - lo > 1) {
7738 int mid = (lo + hi) / 2;
7739 sector_t a = BB_OFFSET(p[mid]);
7740 if (a <= s)
7741 lo = mid;
7742 else
7743 hi = mid;
7744 }
7745 if (hi > lo && BB_OFFSET(p[lo]) > s)
7746 hi = lo;
7747
7748 if (hi > lo) {
7749 /* we found a range that might merge with the start
7750 * of our new range
7751 */
7752 sector_t a = BB_OFFSET(p[lo]);
7753 sector_t e = a + BB_LEN(p[lo]);
7754 int ack = BB_ACK(p[lo]);
7755 if (e >= s) {
7756 /* Yes, we can merge with a previous range */
7757 if (s == a && s + sectors >= e)
7758 /* new range covers old */
7759 ack = acknowledged;
7760 else
7761 ack = ack && acknowledged;
7762
7763 if (e < s + sectors)
7764 e = s + sectors;
7765 if (e - a <= BB_MAX_LEN) {
7766 p[lo] = BB_MAKE(a, e-a, ack);
7767 s = e;
7768 } else {
7769 /* does not all fit in one range,
7770 * make p[lo] maximal
7771 */
7772 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7773 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7774 s = a + BB_MAX_LEN;
7775 }
7776 sectors = e - s;
7777 }
7778 }
7779 if (sectors && hi < bb->count) {
7780 /* 'hi' points to the first range that starts after 's'.
7781 * Maybe we can merge with the start of that range */
7782 sector_t a = BB_OFFSET(p[hi]);
7783 sector_t e = a + BB_LEN(p[hi]);
7784 int ack = BB_ACK(p[hi]);
7785 if (a <= s + sectors) {
7786 /* merging is possible */
7787 if (e <= s + sectors) {
7788 /* full overlap */
7789 e = s + sectors;
7790 ack = acknowledged;
7791 } else
7792 ack = ack && acknowledged;
7793
7794 a = s;
7795 if (e - a <= BB_MAX_LEN) {
7796 p[hi] = BB_MAKE(a, e-a, ack);
7797 s = e;
7798 } else {
7799 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7800 s = a + BB_MAX_LEN;
7801 }
7802 sectors = e - s;
7803 lo = hi;
7804 hi++;
7805 }
7806 }
7807 if (sectors == 0 && hi < bb->count) {
7808 /* we might be able to combine lo and hi */
7809 /* Note: 's' is at the end of 'lo' */
7810 sector_t a = BB_OFFSET(p[hi]);
7811 int lolen = BB_LEN(p[lo]);
7812 int hilen = BB_LEN(p[hi]);
7813 int newlen = lolen + hilen - (s - a);
7814 if (s >= a && newlen < BB_MAX_LEN) {
7815 /* yes, we can combine them */
7816 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7817 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7818 memmove(p + hi, p + hi + 1,
7819 (bb->count - hi - 1) * 8);
7820 bb->count--;
7821 }
7822 }
7823 while (sectors) {
7824 /* didn't merge (it all).
7825 * Need to add a range just before 'hi' */
7826 if (bb->count >= MD_MAX_BADBLOCKS) {
7827 /* No room for more */
7828 rv = 0;
7829 break;
7830 } else {
7831 int this_sectors = sectors;
7832 memmove(p + hi + 1, p + hi,
7833 (bb->count - hi) * 8);
7834 bb->count++;
7835
7836 if (this_sectors > BB_MAX_LEN)
7837 this_sectors = BB_MAX_LEN;
7838 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7839 sectors -= this_sectors;
7840 s += this_sectors;
7841 }
7842 }
7843
7844 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007845 if (!acknowledged)
7846 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007847 write_sequnlock_irq(&bb->lock);
7848
7849 return rv;
7850}
7851
NeilBrown3cb03002011-10-11 16:45:26 +11007852int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007853 int acknowledged)
7854{
7855 int rv = md_set_badblocks(&rdev->badblocks,
7856 s + rdev->data_offset, sectors, acknowledged);
7857 if (rv) {
7858 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007859 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007860 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7861 md_wakeup_thread(rdev->mddev->thread);
7862 }
7863 return rv;
7864}
7865EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7866
7867/*
7868 * Remove a range of bad blocks from the table.
7869 * This may involve extending the table if we spilt a region,
7870 * but it must not fail. So if the table becomes full, we just
7871 * drop the remove request.
7872 */
7873static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7874{
7875 u64 *p;
7876 int lo, hi;
7877 sector_t target = s + sectors;
7878 int rv = 0;
7879
7880 if (bb->shift > 0) {
7881 /* When clearing we round the start up and the end down.
7882 * This should not matter as the shift should align with
7883 * the block size and no rounding should ever be needed.
7884 * However it is better the think a block is bad when it
7885 * isn't than to think a block is not bad when it is.
7886 */
7887 s += (1<<bb->shift) - 1;
7888 s >>= bb->shift;
7889 target >>= bb->shift;
7890 sectors = target - s;
7891 }
7892
7893 write_seqlock_irq(&bb->lock);
7894
7895 p = bb->page;
7896 lo = 0;
7897 hi = bb->count;
7898 /* Find the last range that starts before 'target' */
7899 while (hi - lo > 1) {
7900 int mid = (lo + hi) / 2;
7901 sector_t a = BB_OFFSET(p[mid]);
7902 if (a < target)
7903 lo = mid;
7904 else
7905 hi = mid;
7906 }
7907 if (hi > lo) {
7908 /* p[lo] is the last range that could overlap the
7909 * current range. Earlier ranges could also overlap,
7910 * but only this one can overlap the end of the range.
7911 */
7912 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7913 /* Partial overlap, leave the tail of this range */
7914 int ack = BB_ACK(p[lo]);
7915 sector_t a = BB_OFFSET(p[lo]);
7916 sector_t end = a + BB_LEN(p[lo]);
7917
7918 if (a < s) {
7919 /* we need to split this range */
7920 if (bb->count >= MD_MAX_BADBLOCKS) {
7921 rv = 0;
7922 goto out;
7923 }
7924 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7925 bb->count++;
7926 p[lo] = BB_MAKE(a, s-a, ack);
7927 lo++;
7928 }
7929 p[lo] = BB_MAKE(target, end - target, ack);
7930 /* there is no longer an overlap */
7931 hi = lo;
7932 lo--;
7933 }
7934 while (lo >= 0 &&
7935 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7936 /* This range does overlap */
7937 if (BB_OFFSET(p[lo]) < s) {
7938 /* Keep the early parts of this range. */
7939 int ack = BB_ACK(p[lo]);
7940 sector_t start = BB_OFFSET(p[lo]);
7941 p[lo] = BB_MAKE(start, s - start, ack);
7942 /* now low doesn't overlap, so.. */
7943 break;
7944 }
7945 lo--;
7946 }
7947 /* 'lo' is strictly before, 'hi' is strictly after,
7948 * anything between needs to be discarded
7949 */
7950 if (hi - lo > 1) {
7951 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7952 bb->count -= (hi - lo - 1);
7953 }
7954 }
7955
7956 bb->changed = 1;
7957out:
7958 write_sequnlock_irq(&bb->lock);
7959 return rv;
7960}
7961
NeilBrown3cb03002011-10-11 16:45:26 +11007962int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10007963{
7964 return md_clear_badblocks(&rdev->badblocks,
7965 s + rdev->data_offset,
7966 sectors);
7967}
7968EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7969
7970/*
7971 * Acknowledge all bad blocks in a list.
7972 * This only succeeds if ->changed is clear. It is used by
7973 * in-kernel metadata updates
7974 */
7975void md_ack_all_badblocks(struct badblocks *bb)
7976{
7977 if (bb->page == NULL || bb->changed)
7978 /* no point even trying */
7979 return;
7980 write_seqlock_irq(&bb->lock);
7981
7982 if (bb->changed == 0) {
7983 u64 *p = bb->page;
7984 int i;
7985 for (i = 0; i < bb->count ; i++) {
7986 if (!BB_ACK(p[i])) {
7987 sector_t start = BB_OFFSET(p[i]);
7988 int len = BB_LEN(p[i]);
7989 p[i] = BB_MAKE(start, len, 1);
7990 }
7991 }
NeilBrownde393cd2011-07-28 11:31:48 +10007992 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007993 }
7994 write_sequnlock_irq(&bb->lock);
7995}
7996EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7997
NeilBrown16c791a2011-07-28 11:31:47 +10007998/* sysfs access to bad-blocks list.
7999 * We present two files.
8000 * 'bad-blocks' lists sector numbers and lengths of ranges that
8001 * are recorded as bad. The list is truncated to fit within
8002 * the one-page limit of sysfs.
8003 * Writing "sector length" to this file adds an acknowledged
8004 * bad block list.
8005 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8006 * been acknowledged. Writing to this file adds bad blocks
8007 * without acknowledging them. This is largely for testing.
8008 */
8009
8010static ssize_t
8011badblocks_show(struct badblocks *bb, char *page, int unack)
8012{
8013 size_t len;
8014 int i;
8015 u64 *p = bb->page;
8016 unsigned seq;
8017
8018 if (bb->shift < 0)
8019 return 0;
8020
8021retry:
8022 seq = read_seqbegin(&bb->lock);
8023
8024 len = 0;
8025 i = 0;
8026
8027 while (len < PAGE_SIZE && i < bb->count) {
8028 sector_t s = BB_OFFSET(p[i]);
8029 unsigned int length = BB_LEN(p[i]);
8030 int ack = BB_ACK(p[i]);
8031 i++;
8032
8033 if (unack && ack)
8034 continue;
8035
8036 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8037 (unsigned long long)s << bb->shift,
8038 length << bb->shift);
8039 }
NeilBrownde393cd2011-07-28 11:31:48 +10008040 if (unack && len == 0)
8041 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008042
8043 if (read_seqretry(&bb->lock, seq))
8044 goto retry;
8045
8046 return len;
8047}
8048
8049#define DO_DEBUG 1
8050
8051static ssize_t
8052badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8053{
8054 unsigned long long sector;
8055 int length;
8056 char newline;
8057#ifdef DO_DEBUG
8058 /* Allow clearing via sysfs *only* for testing/debugging.
8059 * Normally only a successful write may clear a badblock
8060 */
8061 int clear = 0;
8062 if (page[0] == '-') {
8063 clear = 1;
8064 page++;
8065 }
8066#endif /* DO_DEBUG */
8067
8068 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8069 case 3:
8070 if (newline != '\n')
8071 return -EINVAL;
8072 case 2:
8073 if (length <= 0)
8074 return -EINVAL;
8075 break;
8076 default:
8077 return -EINVAL;
8078 }
8079
8080#ifdef DO_DEBUG
8081 if (clear) {
8082 md_clear_badblocks(bb, sector, length);
8083 return len;
8084 }
8085#endif /* DO_DEBUG */
8086 if (md_set_badblocks(bb, sector, length, !unack))
8087 return len;
8088 else
8089 return -ENOSPC;
8090}
8091
Adrian Bunk75c96f82005-05-05 16:16:09 -07008092static int md_notify_reboot(struct notifier_block *this,
8093 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094{
8095 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008096 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008097 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098
8099 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8100
8101 printk(KERN_INFO "md: stopping all md devices.\n");
8102
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008103 for_each_mddev(mddev, tmp) {
Neil Brownc71d4882006-05-26 10:39:25 +10008104 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008105 /* Force a switch to readonly even array
8106 * appears to still be in use. Hence
8107 * the '100'.
8108 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008109 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008110 mddev_unlock(mddev);
8111 }
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008112 need_delay = 1;
8113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 /*
8115 * certain more exotic SCSI devices are known to be
8116 * volatile wrt too early system reboots. While the
8117 * right place to handle this issue is the given
8118 * driver, we do want to have a safe RAID driver ...
8119 */
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008120 if (need_delay)
8121 mdelay(1000*1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 }
8123 return NOTIFY_DONE;
8124}
8125
Adrian Bunk75c96f82005-05-05 16:16:09 -07008126static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 .notifier_call = md_notify_reboot,
8128 .next = NULL,
8129 .priority = INT_MAX, /* before any real devices */
8130};
8131
8132static void md_geninit(void)
8133{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008134 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008136 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137}
8138
Adrian Bunk75c96f82005-05-05 16:16:09 -07008139static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140{
Tejun Heoe804ac72010-10-15 15:36:08 +02008141 int ret = -ENOMEM;
8142
Tejun Heoada609e2011-01-25 14:35:54 +01008143 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008144 if (!md_wq)
8145 goto err_wq;
8146
8147 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8148 if (!md_misc_wq)
8149 goto err_misc_wq;
8150
8151 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8152 goto err_md;
8153
8154 if ((ret = register_blkdev(0, "mdp")) < 0)
8155 goto err_mdp;
8156 mdp_major = ret;
8157
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008158 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008159 md_probe, NULL, NULL);
8160 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 md_probe, NULL, NULL);
8162
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008164 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165
8166 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
Tejun Heoe804ac72010-10-15 15:36:08 +02008169err_mdp:
8170 unregister_blkdev(MD_MAJOR, "md");
8171err_md:
8172 destroy_workqueue(md_misc_wq);
8173err_misc_wq:
8174 destroy_workqueue(md_wq);
8175err_wq:
8176 return ret;
8177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178
8179#ifndef MODULE
8180
8181/*
8182 * Searches all registered partitions for autorun RAID arrays
8183 * at boot time.
8184 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008185
8186static LIST_HEAD(all_detected_devices);
8187struct detected_devices_node {
8188 struct list_head list;
8189 dev_t dev;
8190};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008191
8192void md_autodetect_dev(dev_t dev)
8193{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008194 struct detected_devices_node *node_detected_dev;
8195
8196 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8197 if (node_detected_dev) {
8198 node_detected_dev->dev = dev;
8199 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8200 } else {
8201 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8202 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204}
8205
8206
8207static void autostart_arrays(int part)
8208{
NeilBrown3cb03002011-10-11 16:45:26 +11008209 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008210 struct detected_devices_node *node_detected_dev;
8211 dev_t dev;
8212 int i_scanned, i_passed;
8213
8214 i_scanned = 0;
8215 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
8217 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8218
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008219 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8220 i_scanned++;
8221 node_detected_dev = list_entry(all_detected_devices.next,
8222 struct detected_devices_node, list);
8223 list_del(&node_detected_dev->list);
8224 dev = node_detected_dev->dev;
8225 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008226 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 if (IS_ERR(rdev))
8228 continue;
8229
NeilBrownb2d444d2005-11-08 21:39:31 -08008230 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231 MD_BUG();
8232 continue;
8233 }
NeilBrownd0fae182008-03-04 14:29:31 -08008234 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008236 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008238
8239 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8240 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241
8242 autorun_devices(part);
8243}
8244
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008245#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246
8247static __exit void md_exit(void)
8248{
NeilBrownfd01b882011-10-11 16:47:53 +11008249 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008251
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008252 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008253 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008255 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 unregister_blkdev(mdp_major, "mdp");
8257 unregister_reboot_notifier(&md_notifier);
8258 unregister_sysctl_table(raid_table_header);
8259 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008260 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008262 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008264 destroy_workqueue(md_misc_wq);
8265 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266}
8267
Dan Williams685784a2007-07-09 11:56:42 -07008268subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269module_exit(md_exit)
8270
NeilBrownf91de922005-11-08 21:39:36 -08008271static int get_ro(char *buffer, struct kernel_param *kp)
8272{
8273 return sprintf(buffer, "%d", start_readonly);
8274}
8275static int set_ro(const char *val, struct kernel_param *kp)
8276{
8277 char *e;
8278 int num = simple_strtoul(val, &e, 10);
8279 if (*val && (*e == '\0' || *e == '\n')) {
8280 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008281 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008282 }
8283 return -EINVAL;
8284}
8285
NeilBrown80ca3a42006-07-10 04:44:18 -07008286module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8287module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008288
NeilBrownefeb53c2009-01-09 08:31:10 +11008289module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008290
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291EXPORT_SYMBOL(register_md_personality);
8292EXPORT_SYMBOL(unregister_md_personality);
8293EXPORT_SYMBOL(md_error);
8294EXPORT_SYMBOL(md_done_sync);
8295EXPORT_SYMBOL(md_write_start);
8296EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297EXPORT_SYMBOL(md_register_thread);
8298EXPORT_SYMBOL(md_unregister_thread);
8299EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300EXPORT_SYMBOL(md_check_recovery);
8301MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008302MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008303MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008304MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);