blob: 4566b61373d50d9841337e9649869c7b9232b6d2 [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();
NeilBrowndafb20f2012-03-19 12:46:39 +1100442 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100443 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
NeilBrowndafb20f2012-03-19 12:46:39 +1100752 rdev_for_each(rdev, mddev)
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
NeilBrowndafb20f2012-03-19 12:46:39 +1100763 rdev_for_each(rdev, mddev)
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);
NeilBrowndafb20f2012-03-19 12:46:39 +11001345 rdev_for_each(rdev2, mddev) {
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);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001716 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1717 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001718 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001719 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return 0;
1722}
1723
NeilBrownfd01b882011-10-11 16:47:53 +11001724static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001727 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 int max_dev, i;
1729 /* make rdev->sb match mddev and rdev data. */
1730
Namhyung Kim65a06f062011-07-27 11:00:36 +10001731 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 sb->feature_map = 0;
1734 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 memset(sb->pad3, 0, sizeof(sb->pad3));
1738
1739 sb->utime = cpu_to_le64((__u64)mddev->utime);
1740 sb->events = cpu_to_le64(mddev->events);
1741 if (mddev->in_sync)
1742 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1743 else
1744 sb->resync_offset = cpu_to_le64(0);
1745
NeilBrown1c05b4b2006-10-21 10:24:08 -07001746 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001747
NeilBrownf0ca3402006-02-02 14:28:04 -08001748 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001749 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001750 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001751 sb->level = cpu_to_le32(mddev->level);
1752 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001753
NeilBrownaeb9b2112011-08-25 14:43:08 +10001754 if (test_bit(WriteMostly, &rdev->flags))
1755 sb->devflags |= WriteMostly1;
1756 else
1757 sb->devflags &= ~WriteMostly1;
1758
NeilBrownc3d97142009-12-14 12:49:52 +11001759 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1760 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001761 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001762 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001763
1764 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001765 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001766 sb->feature_map |=
1767 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1768 sb->recovery_offset =
1769 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001770 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001771 if (test_bit(Replacement, &rdev->flags))
1772 sb->feature_map |=
1773 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001774
NeilBrownf6705572006-03-27 01:18:11 -08001775 if (mddev->reshape_position != MaxSector) {
1776 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1777 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1778 sb->new_layout = cpu_to_le32(mddev->new_layout);
1779 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1780 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001781 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001782 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001783
NeilBrown2699b672011-07-28 11:31:47 +10001784 if (rdev->badblocks.count == 0)
1785 /* Nothing to do for bad blocks*/ ;
1786 else if (sb->bblog_offset == 0)
1787 /* Cannot record bad blocks on this device */
1788 md_error(mddev, rdev);
1789 else {
1790 struct badblocks *bb = &rdev->badblocks;
1791 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1792 u64 *p = bb->page;
1793 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1794 if (bb->changed) {
1795 unsigned seq;
1796
1797retry:
1798 seq = read_seqbegin(&bb->lock);
1799
1800 memset(bbp, 0xff, PAGE_SIZE);
1801
1802 for (i = 0 ; i < bb->count ; i++) {
1803 u64 internal_bb = *p++;
1804 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1805 | BB_LEN(internal_bb));
1806 *bbp++ = cpu_to_le64(store_bb);
1807 }
1808 if (read_seqretry(&bb->lock, seq))
1809 goto retry;
1810
1811 bb->sector = (rdev->sb_start +
1812 (int)le32_to_cpu(sb->bblog_offset));
1813 bb->size = le16_to_cpu(sb->bblog_size);
1814 bb->changed = 0;
1815 }
1816 }
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001819 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (rdev2->desc_nr+1 > max_dev)
1821 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001822
NeilBrown70471da2009-08-03 10:59:57 +10001823 if (max_dev > le32_to_cpu(sb->max_dev)) {
1824 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001825 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001826 rdev->sb_size = max_dev * 2 + 256;
1827 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1828 if (rdev->sb_size & bmask)
1829 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001830 } else
1831 max_dev = le32_to_cpu(sb->max_dev);
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 for (i=0; i<max_dev;i++)
1834 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1835
NeilBrowndafb20f2012-03-19 12:46:39 +11001836 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001838 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001840 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001842 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001843 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 else
1845 sb->dev_roles[i] = cpu_to_le16(0xffff);
1846 }
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 sb->sb_csum = calc_sb_1_csum(sb);
1849}
1850
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001852super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853{
1854 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001855 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001856 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001857 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001858 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001859 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001860 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001861 max_sectors -= rdev->data_offset;
1862 if (!num_sectors || num_sectors > max_sectors)
1863 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001864 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001865 /* minor version 0 with bitmap we can't move */
1866 return 0;
1867 } else {
1868 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001869 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001870 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001871 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001872 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001873 if (!num_sectors || num_sectors > max_sectors)
1874 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001875 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001876 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001877 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001878 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001879 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001880 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001881 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001882 rdev->sb_page);
1883 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001884 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Adrian Bunk75c96f82005-05-05 16:16:09 -07001887static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 [0] = {
1889 .name = "0.90.0",
1890 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001891 .load_super = super_90_load,
1892 .validate_super = super_90_validate,
1893 .sync_super = super_90_sync,
1894 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 },
1896 [1] = {
1897 .name = "md-1",
1898 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 .load_super = super_1_load,
1900 .validate_super = super_1_validate,
1901 .sync_super = super_1_sync,
1902 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 },
1904};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
NeilBrownfd01b882011-10-11 16:47:53 +11001906static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001907{
1908 if (mddev->sync_super) {
1909 mddev->sync_super(mddev, rdev);
1910 return;
1911 }
1912
1913 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1914
1915 super_types[mddev->major_version].sync_super(mddev, rdev);
1916}
1917
NeilBrownfd01b882011-10-11 16:47:53 +11001918static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
NeilBrown3cb03002011-10-11 16:45:26 +11001920 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
NeilBrown4b809912008-07-21 17:05:25 +10001922 rcu_read_lock();
1923 rdev_for_each_rcu(rdev, mddev1)
1924 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001925 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001926 rdev2->bdev->bd_contains) {
1927 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001928 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001929 }
1930 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932}
1933
1934static LIST_HEAD(pending_raid_disks);
1935
Andre Nollac5e7112009-08-03 10:59:47 +10001936/*
1937 * Try to register data integrity profile for an mddev
1938 *
1939 * This is called when an array is started and after a disk has been kicked
1940 * from the array. It only succeeds if all working and active component devices
1941 * are integrity capable with matching profiles.
1942 */
NeilBrownfd01b882011-10-11 16:47:53 +11001943int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001944{
NeilBrown3cb03002011-10-11 16:45:26 +11001945 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001946
Andre Nollac5e7112009-08-03 10:59:47 +10001947 if (list_empty(&mddev->disks))
1948 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001949 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1950 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001951 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001952 /* skip spares and non-functional disks */
1953 if (test_bit(Faulty, &rdev->flags))
1954 continue;
1955 if (rdev->raid_disk < 0)
1956 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001957 if (!reference) {
1958 /* Use the first rdev as the reference */
1959 reference = rdev;
1960 continue;
1961 }
1962 /* does this rdev's profile match the reference profile? */
1963 if (blk_integrity_compare(reference->bdev->bd_disk,
1964 rdev->bdev->bd_disk) < 0)
1965 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001966 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001967 if (!reference || !bdev_get_integrity(reference->bdev))
1968 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001969 /*
1970 * All component devices are integrity capable and have matching
1971 * profiles, register the common profile for the md device.
1972 */
1973 if (blk_integrity_register(mddev->gendisk,
1974 bdev_get_integrity(reference->bdev)) != 0) {
1975 printk(KERN_ERR "md: failed to register integrity for %s\n",
1976 mdname(mddev));
1977 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001978 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001979 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1980 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1981 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1982 mdname(mddev));
1983 return -EINVAL;
1984 }
Andre Nollac5e7112009-08-03 10:59:47 +10001985 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001986}
Andre Nollac5e7112009-08-03 10:59:47 +10001987EXPORT_SYMBOL(md_integrity_register);
1988
1989/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001990void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001991{
1992 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1993 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1994
1995 if (!bi_mddev) /* nothing to do */
1996 return;
1997 if (rdev->raid_disk < 0) /* skip spares */
1998 return;
1999 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2000 rdev->bdev->bd_disk) >= 0)
2001 return;
2002 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2003 blk_integrity_unregister(mddev->gendisk);
2004}
2005EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002006
NeilBrownfd01b882011-10-11 16:47:53 +11002007static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002009 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002010 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002011 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 if (rdev->mddev) {
2015 MD_BUG();
2016 return -EINVAL;
2017 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002018
2019 /* prevent duplicates */
2020 if (find_rdev(mddev, rdev->bdev->bd_dev))
2021 return -EEXIST;
2022
Andre Nolldd8ac332009-03-31 14:33:13 +11002023 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2024 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2025 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002026 if (mddev->pers) {
2027 /* Cannot change size, so fail
2028 * If mddev->level <= 0, then we don't care
2029 * about aligning sizes (e.g. linear)
2030 */
2031 if (mddev->level > 0)
2032 return -ENOSPC;
2033 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002034 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /* Verify rdev->desc_nr is unique.
2038 * If it is -1, assign a free number, else
2039 * check number is not in use
2040 */
2041 if (rdev->desc_nr < 0) {
2042 int choice = 0;
2043 if (mddev->pers) choice = mddev->raid_disks;
2044 while (find_rdev_nr(mddev, choice))
2045 choice++;
2046 rdev->desc_nr = choice;
2047 } else {
2048 if (find_rdev_nr(mddev, rdev->desc_nr))
2049 return -EBUSY;
2050 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002051 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2052 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2053 mdname(mddev), mddev->max_disks);
2054 return -EBUSY;
2055 }
NeilBrown19133a42005-11-08 21:39:35 -08002056 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002057 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002058 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002061 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002062
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002063 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002064 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002065
Tejun Heo0762b8b2008-08-25 19:56:12 +09002066 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002067 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2068 /* failure here is OK */;
2069 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002070
NeilBrown4b809912008-07-21 17:05:25 +10002071 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002072 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002073
2074 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002075 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002078
2079 fail:
2080 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2081 b, mdname(mddev));
2082 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
NeilBrown177a99b2008-02-06 01:39:56 -08002085static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002086{
NeilBrown3cb03002011-10-11 16:45:26 +11002087 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002088 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002089 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002090}
2091
NeilBrown3cb03002011-10-11 16:45:26 +11002092static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 char b[BDEVNAME_SIZE];
2095 if (!rdev->mddev) {
2096 MD_BUG();
2097 return;
2098 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002099 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002100 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2102 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002103 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002104 sysfs_put(rdev->sysfs_state);
2105 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002106 kfree(rdev->badblocks.page);
2107 rdev->badblocks.count = 0;
2108 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002109 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002110 * writing to 'remove' to "dev/state". We also need
2111 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002112 */
NeilBrown4b809912008-07-21 17:05:25 +10002113 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002114 INIT_WORK(&rdev->del_work, md_delayed_delete);
2115 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002116 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
2119/*
2120 * prevent the device from being mounted, repartitioned or
2121 * otherwise reused by a RAID array (or any other kernel
2122 * subsystem), by bd_claiming the device.
2123 */
NeilBrown3cb03002011-10-11 16:45:26 +11002124static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 int err = 0;
2127 struct block_device *bdev;
2128 char b[BDEVNAME_SIZE];
2129
Tejun Heod4d77622010-11-13 11:55:18 +01002130 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002131 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (IS_ERR(bdev)) {
2133 printk(KERN_ERR "md: could not open %s.\n",
2134 __bdevname(dev, b));
2135 return PTR_ERR(bdev);
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rdev->bdev = bdev;
2138 return err;
2139}
2140
NeilBrown3cb03002011-10-11 16:45:26 +11002141static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 struct block_device *bdev = rdev->bdev;
2144 rdev->bdev = NULL;
2145 if (!bdev)
2146 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002147 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150void md_autodetect_dev(dev_t dev);
2151
NeilBrown3cb03002011-10-11 16:45:26 +11002152static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 char b[BDEVNAME_SIZE];
2155 printk(KERN_INFO "md: export_rdev(%s)\n",
2156 bdevname(rdev->bdev,b));
2157 if (rdev->mddev)
2158 MD_BUG();
2159 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002161 if (test_bit(AutoDetected, &rdev->flags))
2162 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163#endif
2164 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002165 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
NeilBrown3cb03002011-10-11 16:45:26 +11002168static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 unbind_rdev_from_array(rdev);
2171 export_rdev(rdev);
2172}
2173
NeilBrownfd01b882011-10-11 16:47:53 +11002174static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
NeilBrown3cb03002011-10-11 16:45:26 +11002176 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
NeilBrowndafb20f2012-03-19 12:46:39 +11002178 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (!rdev->mddev) {
2180 MD_BUG();
2181 continue;
2182 }
2183 kick_rdev_from_array(rdev);
2184 }
2185 if (!list_empty(&mddev->disks))
2186 MD_BUG();
2187 mddev->raid_disks = 0;
2188 mddev->major_version = 0;
2189}
2190
2191static void print_desc(mdp_disk_t *desc)
2192{
2193 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2194 desc->major,desc->minor,desc->raid_disk,desc->state);
2195}
2196
Cheng Renquancd2ac932009-01-09 08:31:08 +11002197static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 int i;
2200
2201 printk(KERN_INFO
2202 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2203 sb->major_version, sb->minor_version, sb->patch_version,
2204 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2205 sb->ctime);
2206 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2207 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2208 sb->md_minor, sb->layout, sb->chunk_size);
2209 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2210 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2211 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2212 sb->failed_disks, sb->spare_disks,
2213 sb->sb_csum, (unsigned long)sb->events_lo);
2214
2215 printk(KERN_INFO);
2216 for (i = 0; i < MD_SB_DISKS; i++) {
2217 mdp_disk_t *desc;
2218
2219 desc = sb->disks + i;
2220 if (desc->number || desc->major || desc->minor ||
2221 desc->raid_disk || (desc->state && (desc->state != 4))) {
2222 printk(" D %2d: ", i);
2223 print_desc(desc);
2224 }
2225 }
2226 printk(KERN_INFO "md: THIS: ");
2227 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Cheng Renquancd2ac932009-01-09 08:31:08 +11002230static void print_sb_1(struct mdp_superblock_1 *sb)
2231{
2232 __u8 *uuid;
2233
2234 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002235 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002236 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002237 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002238 le32_to_cpu(sb->major_version),
2239 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002240 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002241 sb->set_name,
2242 (unsigned long long)le64_to_cpu(sb->ctime)
2243 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2244
2245 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002246 printk(KERN_INFO
2247 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002248 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002249 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002250 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2251 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002252 le32_to_cpu(sb->level),
2253 (unsigned long long)le64_to_cpu(sb->size),
2254 le32_to_cpu(sb->raid_disks),
2255 le32_to_cpu(sb->layout),
2256 le32_to_cpu(sb->chunksize),
2257 (unsigned long long)le64_to_cpu(sb->data_offset),
2258 (unsigned long long)le64_to_cpu(sb->data_size),
2259 (unsigned long long)le64_to_cpu(sb->super_offset),
2260 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2261 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002262 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002263 sb->devflags,
2264 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2265 (unsigned long long)le64_to_cpu(sb->events),
2266 (unsigned long long)le64_to_cpu(sb->resync_offset),
2267 le32_to_cpu(sb->sb_csum),
2268 le32_to_cpu(sb->max_dev)
2269 );
2270}
2271
NeilBrown3cb03002011-10-11 16:45:26 +11002272static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002275 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2276 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002277 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2278 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002280 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2281 switch (major_version) {
2282 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002283 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002284 break;
2285 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002286 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002287 break;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 } else
2290 printk(KERN_INFO "md: no rdev superblock!\n");
2291}
2292
Adrian Bunk5e563412006-06-26 00:27:42 -07002293static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002295 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002296 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002297 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 char b[BDEVNAME_SIZE];
2299
2300 printk("\n");
2301 printk("md: **********************************\n");
2302 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2303 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002304 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
NeilBrown32a76272005-06-21 17:17:14 -07002306 if (mddev->bitmap)
2307 bitmap_print_sb(mddev->bitmap);
2308 else
2309 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002310 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 printk("<%s>", bdevname(rdev->bdev,b));
2312 printk("\n");
2313
NeilBrowndafb20f2012-03-19 12:46:39 +11002314 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002315 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 printk("md: **********************************\n");
2318 printk("\n");
2319}
2320
2321
NeilBrownfd01b882011-10-11 16:47:53 +11002322static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
NeilBrown42543762006-06-26 00:27:57 -07002324 /* Update each superblock (in-memory image), but
2325 * if we are allowed to, skip spares which already
2326 * have the right event counter, or have one earlier
2327 * (which would mean they aren't being marked as dirty
2328 * with the rest of the array)
2329 */
NeilBrown3cb03002011-10-11 16:45:26 +11002330 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002331 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002332 if (rdev->sb_events == mddev->events ||
2333 (nospares &&
2334 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002335 rdev->sb_events+1 == mddev->events)) {
2336 /* Don't update this superblock */
2337 rdev->sb_loaded = 2;
2338 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002339 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002340 rdev->sb_loaded = 1;
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343}
2344
NeilBrownfd01b882011-10-11 16:47:53 +11002345static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
NeilBrown3cb03002011-10-11 16:45:26 +11002347 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002348 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002349 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002350 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002353 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002354 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002355 if (rdev->raid_disk >= 0 &&
2356 mddev->delta_disks >= 0 &&
2357 !test_bit(In_sync, &rdev->flags) &&
2358 mddev->curr_resync_completed > rdev->recovery_offset)
2359 rdev->recovery_offset = mddev->curr_resync_completed;
2360
2361 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002362 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002363 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002364 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002365 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002366 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002367 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002368 if (rdev->badblocks.changed) {
2369 md_ack_all_badblocks(&rdev->badblocks);
2370 md_error(mddev, rdev);
2371 }
2372 clear_bit(Blocked, &rdev->flags);
2373 clear_bit(BlockedBadBlocks, &rdev->flags);
2374 wake_up(&rdev->blocked_wait);
2375 }
2376 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002377 wake_up(&mddev->sb_wait);
2378 return;
2379 }
2380
NeilBrowna9701a32005-11-08 21:39:34 -08002381 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002382
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002383 mddev->utime = get_seconds();
2384
NeilBrown850b2b422006-10-03 01:15:46 -07002385 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2386 force_change = 1;
2387 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2388 /* just a clean<-> dirty transition, possibly leave spares alone,
2389 * though if events isn't the right even/odd, we will have to do
2390 * spares after all
2391 */
2392 nospares = 1;
2393 if (force_change)
2394 nospares = 0;
2395 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002396 /* If the array is degraded, then skipping spares is both
2397 * dangerous and fairly pointless.
2398 * Dangerous because a device that was removed from the array
2399 * might have a event_count that still looks up-to-date,
2400 * so it can be re-added without a resync.
2401 * Pointless because if there are any spares to skip,
2402 * then a recovery will happen and soon that array won't
2403 * be degraded any more and the spare can go back to sleep then.
2404 */
NeilBrown850b2b422006-10-03 01:15:46 -07002405 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002406
NeilBrown06d91a52005-06-21 17:17:12 -07002407 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002408
2409 /* If this is just a dirty<->clean transition, and the array is clean
2410 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002411 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002412 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002413 && mddev->can_decrease_events
2414 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002415 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002416 mddev->can_decrease_events = 0;
2417 } else {
NeilBrown42543762006-06-26 00:27:57 -07002418 /* otherwise we have to go forward and ... */
2419 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002420 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 if (!mddev->events) {
2424 /*
2425 * oops, this 64-bit counter should never wrap.
2426 * Either we are in around ~1 trillion A.C., assuming
2427 * 1 reboot per second, or we have a bug:
2428 */
2429 MD_BUG();
2430 mddev->events --;
2431 }
NeilBrown2699b672011-07-28 11:31:47 +10002432
NeilBrowndafb20f2012-03-19 12:46:39 +11002433 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002434 if (rdev->badblocks.changed)
2435 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002436 if (test_bit(Faulty, &rdev->flags))
2437 set_bit(FaultRecorded, &rdev->flags);
2438 }
NeilBrown2699b672011-07-28 11:31:47 +10002439
NeilBrowne6910632008-02-06 01:39:51 -08002440 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002441 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
NeilBrown36a4e1f2011-10-07 14:23:17 +11002443 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2444 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
NeilBrown4ad13662007-07-17 04:06:13 -07002446 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002447 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002449
NeilBrown42543762006-06-26 00:27:57 -07002450 if (rdev->sb_loaded != 1)
2451 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002453 if (!test_bit(Faulty, &rdev->flags) &&
2454 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002455 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002456 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002457 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002458 pr_debug("md: (write) %s's sb offset: %llu\n",
2459 bdevname(rdev->bdev, b),
2460 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002461 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002462 if (rdev->badblocks.size) {
2463 md_super_write(mddev, rdev,
2464 rdev->badblocks.sector,
2465 rdev->badblocks.size << 9,
2466 rdev->bb_page);
2467 rdev->badblocks.size = 0;
2468 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002469
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002470 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002471 pr_debug("md: %s (skipping faulty)\n",
2472 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002473 else
2474 pr_debug("(skipping incremental s/r ");
2475
NeilBrown7bfa19f2005-06-21 17:17:28 -07002476 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /* only need to write one superblock... */
2478 break;
2479 }
NeilBrowna9701a32005-11-08 21:39:34 -08002480 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002481 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002482
NeilBrowna9701a32005-11-08 21:39:34 -08002483 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002484 if (mddev->in_sync != sync_req ||
2485 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002486 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002487 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002488 goto repeat;
2489 }
NeilBrown850b2b422006-10-03 01:15:46 -07002490 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002491 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002492 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002493 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2494 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002495
NeilBrowndafb20f2012-03-19 12:46:39 +11002496 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002497 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2498 clear_bit(Blocked, &rdev->flags);
2499
2500 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002501 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002502 clear_bit(BlockedBadBlocks, &rdev->flags);
2503 wake_up(&rdev->blocked_wait);
2504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Andre Noll7f6ce762008-03-23 18:34:54 +01002507/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002508 * We want to accept with case. For this we use cmd_match.
2509 */
2510static int cmd_match(const char *cmd, const char *str)
2511{
2512 /* See if cmd, written into a sysfs file, matches
2513 * str. They must either be the same, or cmd can
2514 * have a trailing newline
2515 */
2516 while (*cmd && *str && *cmd == *str) {
2517 cmd++;
2518 str++;
2519 }
2520 if (*cmd == '\n')
2521 cmd++;
2522 if (*str || *cmd)
2523 return 0;
2524 return 1;
2525}
2526
NeilBrown86e6ffd2005-11-08 21:39:24 -08002527struct rdev_sysfs_entry {
2528 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002529 ssize_t (*show)(struct md_rdev *, char *);
2530 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002531};
2532
2533static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002534state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002535{
2536 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002537 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002538
NeilBrownde393cd2011-07-28 11:31:48 +10002539 if (test_bit(Faulty, &rdev->flags) ||
2540 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002541 len+= sprintf(page+len, "%sfaulty",sep);
2542 sep = ",";
2543 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002544 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002545 len += sprintf(page+len, "%sin_sync",sep);
2546 sep = ",";
2547 }
NeilBrownf6556752006-06-26 00:28:01 -07002548 if (test_bit(WriteMostly, &rdev->flags)) {
2549 len += sprintf(page+len, "%swrite_mostly",sep);
2550 sep = ",";
2551 }
NeilBrownde393cd2011-07-28 11:31:48 +10002552 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002553 (rdev->badblocks.unacked_exist
2554 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002555 len += sprintf(page+len, "%sblocked", sep);
2556 sep = ",";
2557 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002558 if (!test_bit(Faulty, &rdev->flags) &&
2559 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002560 len += sprintf(page+len, "%sspare", sep);
2561 sep = ",";
2562 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002563 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2564 len += sprintf(page+len, "%swrite_error", sep);
2565 sep = ",";
2566 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002567 if (test_bit(WantReplacement, &rdev->flags)) {
2568 len += sprintf(page+len, "%swant_replacement", sep);
2569 sep = ",";
2570 }
2571 if (test_bit(Replacement, &rdev->flags)) {
2572 len += sprintf(page+len, "%sreplacement", sep);
2573 sep = ",";
2574 }
2575
NeilBrown86e6ffd2005-11-08 21:39:24 -08002576 return len+sprintf(page+len, "\n");
2577}
2578
NeilBrown45dc2de2006-06-26 00:27:58 -07002579static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002580state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002581{
2582 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002583 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002584 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002585 * writemostly - sets write_mostly
2586 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002587 * blocked - sets the Blocked flags
2588 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002589 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002590 * write_error - sets WriteErrorSeen
2591 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002592 */
2593 int err = -EINVAL;
2594 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2595 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002596 if (test_bit(Faulty, &rdev->flags))
2597 err = 0;
2598 else
2599 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002600 } else if (cmd_match(buf, "remove")) {
2601 if (rdev->raid_disk >= 0)
2602 err = -EBUSY;
2603 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002604 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002605 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002606 if (mddev->pers)
2607 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002608 md_new_event(mddev);
2609 err = 0;
2610 }
NeilBrownf6556752006-06-26 00:28:01 -07002611 } else if (cmd_match(buf, "writemostly")) {
2612 set_bit(WriteMostly, &rdev->flags);
2613 err = 0;
2614 } else if (cmd_match(buf, "-writemostly")) {
2615 clear_bit(WriteMostly, &rdev->flags);
2616 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002617 } else if (cmd_match(buf, "blocked")) {
2618 set_bit(Blocked, &rdev->flags);
2619 err = 0;
2620 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002621 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002622 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002623 /* metadata handler doesn't understand badblocks,
2624 * so we need to fail the device
2625 */
2626 md_error(rdev->mddev, rdev);
2627 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002628 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002629 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002630 wake_up(&rdev->blocked_wait);
2631 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2632 md_wakeup_thread(rdev->mddev->thread);
2633
2634 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002635 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2636 set_bit(In_sync, &rdev->flags);
2637 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002638 } else if (cmd_match(buf, "write_error")) {
2639 set_bit(WriteErrorSeen, &rdev->flags);
2640 err = 0;
2641 } else if (cmd_match(buf, "-write_error")) {
2642 clear_bit(WriteErrorSeen, &rdev->flags);
2643 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002644 } else if (cmd_match(buf, "want_replacement")) {
2645 /* Any non-spare device that is not a replacement can
2646 * become want_replacement at any time, but we then need to
2647 * check if recovery is needed.
2648 */
2649 if (rdev->raid_disk >= 0 &&
2650 !test_bit(Replacement, &rdev->flags))
2651 set_bit(WantReplacement, &rdev->flags);
2652 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2653 md_wakeup_thread(rdev->mddev->thread);
2654 err = 0;
2655 } else if (cmd_match(buf, "-want_replacement")) {
2656 /* Clearing 'want_replacement' is always allowed.
2657 * Once replacements starts it is too late though.
2658 */
2659 err = 0;
2660 clear_bit(WantReplacement, &rdev->flags);
2661 } else if (cmd_match(buf, "replacement")) {
2662 /* Can only set a device as a replacement when array has not
2663 * yet been started. Once running, replacement is automatic
2664 * from spares, or by assigning 'slot'.
2665 */
2666 if (rdev->mddev->pers)
2667 err = -EBUSY;
2668 else {
2669 set_bit(Replacement, &rdev->flags);
2670 err = 0;
2671 }
2672 } else if (cmd_match(buf, "-replacement")) {
2673 /* Similarly, can only clear Replacement before start */
2674 if (rdev->mddev->pers)
2675 err = -EBUSY;
2676 else {
2677 clear_bit(Replacement, &rdev->flags);
2678 err = 0;
2679 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002680 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002681 if (!err)
2682 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002683 return err ? err : len;
2684}
NeilBrown80ca3a42006-07-10 04:44:18 -07002685static struct rdev_sysfs_entry rdev_state =
2686__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002687
2688static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002689errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002690{
2691 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2692}
2693
2694static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002695errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002696{
2697 char *e;
2698 unsigned long n = simple_strtoul(buf, &e, 10);
2699 if (*buf && (*e == 0 || *e == '\n')) {
2700 atomic_set(&rdev->corrected_errors, n);
2701 return len;
2702 }
2703 return -EINVAL;
2704}
2705static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002706__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002707
NeilBrown014236d2006-01-06 00:20:55 -08002708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002710{
2711 if (rdev->raid_disk < 0)
2712 return sprintf(page, "none\n");
2713 else
2714 return sprintf(page, "%d\n", rdev->raid_disk);
2715}
2716
2717static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002718slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002719{
2720 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002721 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002722 int slot = simple_strtoul(buf, &e, 10);
2723 if (strncmp(buf, "none", 4)==0)
2724 slot = -1;
2725 else if (e==buf || (*e && *e!= '\n'))
2726 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002727 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002728 /* Setting 'slot' on an active array requires also
2729 * updating the 'rd%d' link, and communicating
2730 * with the personality with ->hot_*_disk.
2731 * For now we only support removing
2732 * failed/spare devices. This normally happens automatically,
2733 * but not when the metadata is externally managed.
2734 */
NeilBrownc303da62008-02-06 01:39:51 -08002735 if (rdev->raid_disk == -1)
2736 return -EEXIST;
2737 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002738 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002739 return -EINVAL;
2740 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002741 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002742 if (err)
2743 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002744 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002745 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002746 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2747 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002748 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002749 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002750 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002751 */
2752
2753 if (rdev->raid_disk != -1)
2754 return -EBUSY;
2755
NeilBrownc6751b22011-02-02 11:57:13 +11002756 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2757 return -EBUSY;
2758
Neil Brown6c2fce22008-06-28 08:31:31 +10002759 if (rdev->mddev->pers->hot_add_disk == NULL)
2760 return -EINVAL;
2761
NeilBrownba1b41b2011-01-14 09:14:34 +11002762 if (slot >= rdev->mddev->raid_disks &&
2763 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2764 return -ENOSPC;
2765
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 rdev->raid_disk = slot;
2767 if (test_bit(In_sync, &rdev->flags))
2768 rdev->saved_raid_disk = slot;
2769 else
2770 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002771 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002772 err = rdev->mddev->pers->
2773 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002774 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002775 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 return err;
Neil Brown52664732008-06-28 08:31:44 +10002777 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002778 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002779 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002780 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002781 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002782 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002783 if (slot >= rdev->mddev->raid_disks &&
2784 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002785 return -ENOSPC;
2786 rdev->raid_disk = slot;
2787 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002788 clear_bit(Faulty, &rdev->flags);
2789 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002790 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002791 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002792 }
NeilBrown014236d2006-01-06 00:20:55 -08002793 return len;
2794}
2795
2796
2797static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002798__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002799
NeilBrown93c8cad2006-01-06 00:20:56 -08002800static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002801offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002802{
Andrew Morton6961ece2006-01-06 00:20:59 -08002803 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002804}
2805
2806static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002807offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002808{
2809 char *e;
2810 unsigned long long offset = simple_strtoull(buf, &e, 10);
2811 if (e==buf || (*e && *e != '\n'))
2812 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002813 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002814 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002815 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002816 /* Must set offset before size, so overlap checks
2817 * can be sane */
2818 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002819 rdev->data_offset = offset;
2820 return len;
2821}
2822
2823static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002824__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002825
NeilBrown83303b62006-01-06 00:21:06 -08002826static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002827rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002828{
Andre Nolldd8ac332009-03-31 14:33:13 +11002829 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002830}
2831
NeilBrownc5d79ad2008-02-06 01:39:54 -08002832static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2833{
2834 /* check if two start/length pairs overlap */
2835 if (s1+l1 <= s2)
2836 return 0;
2837 if (s2+l2 <= s1)
2838 return 0;
2839 return 1;
2840}
2841
Dan Williamsb522adc2009-03-31 15:00:31 +11002842static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2843{
2844 unsigned long long blocks;
2845 sector_t new;
2846
2847 if (strict_strtoull(buf, 10, &blocks) < 0)
2848 return -EINVAL;
2849
2850 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2851 return -EINVAL; /* sector conversion overflow */
2852
2853 new = blocks * 2;
2854 if (new != blocks * 2)
2855 return -EINVAL; /* unsigned long long to sector_t overflow */
2856
2857 *sectors = new;
2858 return 0;
2859}
2860
NeilBrown83303b62006-01-06 00:21:06 -08002861static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002862rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002863{
NeilBrownfd01b882011-10-11 16:47:53 +11002864 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002865 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002866 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002867
Dan Williamsb522adc2009-03-31 15:00:31 +11002868 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002869 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002870 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002871 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002872 sectors = super_types[my_mddev->major_version].
2873 rdev_size_change(rdev, sectors);
2874 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002875 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002876 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002877 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002878 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002879 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002880 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002881 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002882
Andre Nolldd8ac332009-03-31 14:33:13 +11002883 rdev->sectors = sectors;
2884 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002885 /* need to check that all other rdevs with the same ->bdev
2886 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002887 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002888 * we have to change it back, we will have the lock again.
2889 */
NeilBrownfd01b882011-10-11 16:47:53 +11002890 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002891 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002892 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002893
NeilBrown27c529b2008-03-04 14:29:33 -08002894 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002895 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002896 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002897
2898 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002899 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002900 if (rdev->bdev == rdev2->bdev &&
2901 rdev != rdev2 &&
2902 overlaps(rdev->data_offset, rdev->sectors,
2903 rdev2->data_offset,
2904 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002905 overlap = 1;
2906 break;
2907 }
2908 mddev_unlock(mddev);
2909 if (overlap) {
2910 mddev_put(mddev);
2911 break;
2912 }
2913 }
NeilBrown27c529b2008-03-04 14:29:33 -08002914 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002915 if (overlap) {
2916 /* Someone else could have slipped in a size
2917 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002919 * safe, and trust userspace not to race with
2920 * itself
2921 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002922 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002923 return -EBUSY;
2924 }
2925 }
NeilBrown83303b62006-01-06 00:21:06 -08002926 return len;
2927}
2928
2929static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002930__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002931
Dan Williams06e3c812009-12-12 21:17:12 -07002932
NeilBrown3cb03002011-10-11 16:45:26 +11002933static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002934{
2935 unsigned long long recovery_start = rdev->recovery_offset;
2936
2937 if (test_bit(In_sync, &rdev->flags) ||
2938 recovery_start == MaxSector)
2939 return sprintf(page, "none\n");
2940
2941 return sprintf(page, "%llu\n", recovery_start);
2942}
2943
NeilBrown3cb03002011-10-11 16:45:26 +11002944static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002945{
2946 unsigned long long recovery_start;
2947
2948 if (cmd_match(buf, "none"))
2949 recovery_start = MaxSector;
2950 else if (strict_strtoull(buf, 10, &recovery_start))
2951 return -EINVAL;
2952
2953 if (rdev->mddev->pers &&
2954 rdev->raid_disk >= 0)
2955 return -EBUSY;
2956
2957 rdev->recovery_offset = recovery_start;
2958 if (recovery_start == MaxSector)
2959 set_bit(In_sync, &rdev->flags);
2960 else
2961 clear_bit(In_sync, &rdev->flags);
2962 return len;
2963}
2964
2965static struct rdev_sysfs_entry rdev_recovery_start =
2966__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2967
NeilBrown16c791a2011-07-28 11:31:47 +10002968
2969static ssize_t
2970badblocks_show(struct badblocks *bb, char *page, int unack);
2971static ssize_t
2972badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2973
NeilBrown3cb03002011-10-11 16:45:26 +11002974static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002975{
2976 return badblocks_show(&rdev->badblocks, page, 0);
2977}
NeilBrown3cb03002011-10-11 16:45:26 +11002978static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002979{
NeilBrownde393cd2011-07-28 11:31:48 +10002980 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2981 /* Maybe that ack was all we needed */
2982 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2983 wake_up(&rdev->blocked_wait);
2984 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002985}
2986static struct rdev_sysfs_entry rdev_bad_blocks =
2987__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2988
2989
NeilBrown3cb03002011-10-11 16:45:26 +11002990static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002991{
2992 return badblocks_show(&rdev->badblocks, page, 1);
2993}
NeilBrown3cb03002011-10-11 16:45:26 +11002994static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002995{
2996 return badblocks_store(&rdev->badblocks, page, len, 1);
2997}
2998static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2999__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3000
NeilBrown86e6ffd2005-11-08 21:39:24 -08003001static struct attribute *rdev_default_attrs[] = {
3002 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003003 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003004 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003005 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003006 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003007 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003008 &rdev_bad_blocks.attr,
3009 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003010 NULL,
3011};
3012static ssize_t
3013rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3014{
3015 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003016 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003017 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003018 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003019
3020 if (!entry->show)
3021 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003022
3023 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3024 if (!rv) {
3025 if (rdev->mddev == NULL)
3026 rv = -EBUSY;
3027 else
3028 rv = entry->show(rdev, page);
3029 mddev_unlock(mddev);
3030 }
3031 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003032}
3033
3034static ssize_t
3035rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3036 const char *page, size_t length)
3037{
3038 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003039 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003040 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003041 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003042
3043 if (!entry->store)
3044 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003045 if (!capable(CAP_SYS_ADMIN))
3046 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003047 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003048 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003049 if (rdev->mddev == NULL)
3050 rv = -EBUSY;
3051 else
3052 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003053 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003054 }
3055 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003056}
3057
3058static void rdev_free(struct kobject *ko)
3059{
NeilBrown3cb03002011-10-11 16:45:26 +11003060 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003061 kfree(rdev);
3062}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003063static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003064 .show = rdev_attr_show,
3065 .store = rdev_attr_store,
3066};
3067static struct kobj_type rdev_ktype = {
3068 .release = rdev_free,
3069 .sysfs_ops = &rdev_sysfs_ops,
3070 .default_attrs = rdev_default_attrs,
3071};
3072
NeilBrown3cb03002011-10-11 16:45:26 +11003073int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003074{
3075 rdev->desc_nr = -1;
3076 rdev->saved_raid_disk = -1;
3077 rdev->raid_disk = -1;
3078 rdev->flags = 0;
3079 rdev->data_offset = 0;
3080 rdev->sb_events = 0;
3081 rdev->last_read_error.tv_sec = 0;
3082 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003083 rdev->sb_loaded = 0;
3084 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003085 atomic_set(&rdev->nr_pending, 0);
3086 atomic_set(&rdev->read_errors, 0);
3087 atomic_set(&rdev->corrected_errors, 0);
3088
3089 INIT_LIST_HEAD(&rdev->same_set);
3090 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003091
3092 /* Add space to store bad block list.
3093 * This reserves the space even on arrays where it cannot
3094 * be used - I wonder if that matters
3095 */
3096 rdev->badblocks.count = 0;
3097 rdev->badblocks.shift = 0;
3098 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3099 seqlock_init(&rdev->badblocks.lock);
3100 if (rdev->badblocks.page == NULL)
3101 return -ENOMEM;
3102
3103 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003104}
3105EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106/*
3107 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3108 *
3109 * mark the device faulty if:
3110 *
3111 * - the device is nonexistent (zero size)
3112 * - the device has no valid superblock
3113 *
3114 * a faulty rdev _never_ has rdev->sb set.
3115 */
NeilBrown3cb03002011-10-11 16:45:26 +11003116static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 char b[BDEVNAME_SIZE];
3119 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003120 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 sector_t size;
3122
NeilBrown9ffae0c2006-01-06 00:20:32 -08003123 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (!rdev) {
3125 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3126 return ERR_PTR(-ENOMEM);
3127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
NeilBrown2230dfe2011-07-28 11:31:46 +10003129 err = md_rdev_init(rdev);
3130 if (err)
3131 goto abort_free;
3132 err = alloc_disk_sb(rdev);
3133 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto abort_free;
3135
NeilBrownc5d79ad2008-02-06 01:39:54 -08003136 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (err)
3138 goto abort_free;
3139
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003140 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003141
Mike Snitzer77304d22010-11-08 14:39:12 +01003142 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (!size) {
3144 printk(KERN_WARNING
3145 "md: %s has zero or unknown size, marking faulty!\n",
3146 bdevname(rdev->bdev,b));
3147 err = -EINVAL;
3148 goto abort_free;
3149 }
3150
3151 if (super_format >= 0) {
3152 err = super_types[super_format].
3153 load_super(rdev, NULL, super_minor);
3154 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003155 printk(KERN_WARNING
3156 "md: %s does not have a valid v%d.%d "
3157 "superblock, not importing!\n",
3158 bdevname(rdev->bdev,b),
3159 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 goto abort_free;
3161 }
3162 if (err < 0) {
3163 printk(KERN_WARNING
3164 "md: could not read %s's sb, not importing!\n",
3165 bdevname(rdev->bdev,b));
3166 goto abort_free;
3167 }
3168 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003169 if (super_format == -1)
3170 /* hot-add for 0.90, or non-persistent: so no badblocks */
3171 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return rdev;
3174
3175abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003176 if (rdev->bdev)
3177 unlock_rdev(rdev);
3178 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003179 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 kfree(rdev);
3181 return ERR_PTR(err);
3182}
3183
3184/*
3185 * Check a full RAID array for plausibility
3186 */
3187
3188
NeilBrownfd01b882011-10-11 16:47:53 +11003189static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003192 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 char b[BDEVNAME_SIZE];
3194
3195 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003196 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 switch (super_types[mddev->major_version].
3198 load_super(rdev, freshest, mddev->minor_version)) {
3199 case 1:
3200 freshest = rdev;
3201 break;
3202 case 0:
3203 break;
3204 default:
3205 printk( KERN_ERR \
3206 "md: fatal superblock inconsistency in %s"
3207 " -- removing from array\n",
3208 bdevname(rdev->bdev,b));
3209 kick_rdev_from_array(rdev);
3210 }
3211
3212
3213 super_types[mddev->major_version].
3214 validate_super(mddev, freshest);
3215
3216 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003217 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003218 if (mddev->max_disks &&
3219 (rdev->desc_nr >= mddev->max_disks ||
3220 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003221 printk(KERN_WARNING
3222 "md: %s: %s: only %d devices permitted\n",
3223 mdname(mddev), bdevname(rdev->bdev, b),
3224 mddev->max_disks);
3225 kick_rdev_from_array(rdev);
3226 continue;
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 if (rdev != freshest)
3229 if (super_types[mddev->major_version].
3230 validate_super(mddev, rdev)) {
3231 printk(KERN_WARNING "md: kicking non-fresh %s"
3232 " from array!\n",
3233 bdevname(rdev->bdev,b));
3234 kick_rdev_from_array(rdev);
3235 continue;
3236 }
3237 if (mddev->level == LEVEL_MULTIPATH) {
3238 rdev->desc_nr = i++;
3239 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003240 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003241 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003242 rdev->raid_disk = -1;
3243 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
NeilBrown72e02072009-12-14 12:49:55 +11003248/* Read a fixed-point number.
3249 * Numbers in sysfs attributes should be in "standard" units where
3250 * possible, so time should be in seconds.
3251 * However we internally use a a much smaller unit such as
3252 * milliseconds or jiffies.
3253 * This function takes a decimal number with a possible fractional
3254 * component, and produces an integer which is the result of
3255 * multiplying that number by 10^'scale'.
3256 * all without any floating-point arithmetic.
3257 */
3258int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3259{
3260 unsigned long result = 0;
3261 long decimals = -1;
3262 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3263 if (*cp == '.')
3264 decimals = 0;
3265 else if (decimals < scale) {
3266 unsigned int value;
3267 value = *cp - '0';
3268 result = result * 10 + value;
3269 if (decimals >= 0)
3270 decimals++;
3271 }
3272 cp++;
3273 }
3274 if (*cp == '\n')
3275 cp++;
3276 if (*cp)
3277 return -EINVAL;
3278 if (decimals < 0)
3279 decimals = 0;
3280 while (decimals < scale) {
3281 result *= 10;
3282 decimals ++;
3283 }
3284 *res = result;
3285 return 0;
3286}
3287
3288
NeilBrown19052c02008-08-05 15:54:13 +10003289static void md_safemode_timeout(unsigned long data);
3290
NeilBrowneae17012005-11-08 21:39:23 -08003291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003292safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003293{
3294 int msec = (mddev->safemode_delay*1000)/HZ;
3295 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3296}
3297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003298safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003299{
NeilBrown16f17b32006-06-26 00:27:37 -07003300 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003301
NeilBrown72e02072009-12-14 12:49:55 +11003302 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003303 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003304 if (msec == 0)
3305 mddev->safemode_delay = 0;
3306 else {
NeilBrown19052c02008-08-05 15:54:13 +10003307 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003308 mddev->safemode_delay = (msec*HZ)/1000;
3309 if (mddev->safemode_delay == 0)
3310 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003311 if (mddev->safemode_delay < old_delay)
3312 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003313 }
3314 return len;
3315}
3316static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003317__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003318
3319static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003320level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003321{
NeilBrown84fc4b52011-10-11 16:49:58 +11003322 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003323 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003324 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003325 else if (mddev->clevel[0])
3326 return sprintf(page, "%s\n", mddev->clevel);
3327 else if (mddev->level != LEVEL_NONE)
3328 return sprintf(page, "%d\n", mddev->level);
3329 else
3330 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003331}
3332
NeilBrownd9d166c2006-01-06 00:20:51 -08003333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003334level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003335{
Dan Williamsf2859af2010-05-02 10:04:16 -07003336 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003337 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003338 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003340 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003341 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003342
3343 if (mddev->pers == NULL) {
3344 if (len == 0)
3345 return 0;
3346 if (len >= sizeof(mddev->clevel))
3347 return -ENOSPC;
3348 strncpy(mddev->clevel, buf, len);
3349 if (mddev->clevel[len-1] == '\n')
3350 len--;
3351 mddev->clevel[len] = 0;
3352 mddev->level = LEVEL_NONE;
3353 return rv;
3354 }
3355
3356 /* request to change the personality. Need to ensure:
3357 * - array is not engaged in resync/recovery/reshape
3358 * - old personality can be suspended
3359 * - new personality will access other array.
3360 */
3361
NeilBrownbb4f1e92010-08-08 21:18:03 +10003362 if (mddev->sync_thread ||
3363 mddev->reshape_position != MaxSector ||
3364 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003365 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003366
3367 if (!mddev->pers->quiesce) {
3368 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3369 mdname(mddev), mddev->pers->name);
3370 return -EINVAL;
3371 }
3372
3373 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003374 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003375 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003376 strncpy(clevel, buf, len);
3377 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003378 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003379 clevel[len] = 0;
3380 if (strict_strtol(clevel, 10, &level))
3381 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003382
Dan Williamsf2859af2010-05-02 10:04:16 -07003383 if (request_module("md-%s", clevel) != 0)
3384 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003385 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003386 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003387 if (!pers || !try_module_get(pers->owner)) {
3388 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003389 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003390 return -EINVAL;
3391 }
3392 spin_unlock(&pers_lock);
3393
3394 if (pers == mddev->pers) {
3395 /* Nothing to do! */
3396 module_put(pers->owner);
3397 return rv;
3398 }
3399 if (!pers->takeover) {
3400 module_put(pers->owner);
3401 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003402 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003403 return -EINVAL;
3404 }
3405
NeilBrowndafb20f2012-03-19 12:46:39 +11003406 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003407 rdev->new_raid_disk = rdev->raid_disk;
3408
NeilBrown245f46c2009-03-31 14:39:39 +11003409 /* ->takeover must set new_* and/or delta_disks
3410 * if it succeeds, and may set them when it fails.
3411 */
3412 priv = pers->takeover(mddev);
3413 if (IS_ERR(priv)) {
3414 mddev->new_level = mddev->level;
3415 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003416 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003417 mddev->raid_disks -= mddev->delta_disks;
3418 mddev->delta_disks = 0;
3419 module_put(pers->owner);
3420 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003421 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003422 return PTR_ERR(priv);
3423 }
3424
3425 /* Looks like we have a winner */
3426 mddev_suspend(mddev);
3427 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003428
3429 if (mddev->pers->sync_request == NULL &&
3430 pers->sync_request != NULL) {
3431 /* need to add the md_redundancy_group */
3432 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3433 printk(KERN_WARNING
3434 "md: cannot register extra attributes for %s\n",
3435 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003436 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003437 }
3438 if (mddev->pers->sync_request != NULL &&
3439 pers->sync_request == NULL) {
3440 /* need to remove the md_redundancy_group */
3441 if (mddev->to_remove == NULL)
3442 mddev->to_remove = &md_redundancy_group;
3443 }
3444
Trela Maciej54071b32010-03-08 16:02:42 +11003445 if (mddev->pers->sync_request == NULL &&
3446 mddev->external) {
3447 /* We are converting from a no-redundancy array
3448 * to a redundancy array and metadata is managed
3449 * externally so we need to be sure that writes
3450 * won't block due to a need to transition
3451 * clean->dirty
3452 * until external management is started.
3453 */
3454 mddev->in_sync = 0;
3455 mddev->safemode_delay = 0;
3456 mddev->safemode = 0;
3457 }
3458
NeilBrowndafb20f2012-03-19 12:46:39 +11003459 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003460 if (rdev->raid_disk < 0)
3461 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003462 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003463 rdev->new_raid_disk = -1;
3464 if (rdev->new_raid_disk == rdev->raid_disk)
3465 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003466 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003467 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003468 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003469 if (rdev->raid_disk < 0)
3470 continue;
3471 if (rdev->new_raid_disk == rdev->raid_disk)
3472 continue;
3473 rdev->raid_disk = rdev->new_raid_disk;
3474 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003475 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003476 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003477 if (sysfs_link_rdev(mddev, rdev))
3478 printk(KERN_WARNING "md: cannot register rd%d"
3479 " for %s after level change\n",
3480 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003481 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003482 }
3483
3484 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003485 mddev->pers = pers;
3486 mddev->private = priv;
3487 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3488 mddev->level = mddev->new_level;
3489 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003490 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003491 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003492 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003493 if (mddev->pers->sync_request == NULL) {
3494 /* this is now an array without redundancy, so
3495 * it must always be in_sync
3496 */
3497 mddev->in_sync = 1;
3498 del_timer_sync(&mddev->safemode_timer);
3499 }
NeilBrown245f46c2009-03-31 14:39:39 +11003500 pers->run(mddev);
3501 mddev_resume(mddev);
3502 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3503 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3504 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003505 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003506 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003507 return rv;
3508}
3509
3510static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003511__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003512
NeilBrownd4dbd022006-06-26 00:27:59 -07003513
3514static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003515layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003516{
3517 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003518 if (mddev->reshape_position != MaxSector &&
3519 mddev->layout != mddev->new_layout)
3520 return sprintf(page, "%d (%d)\n",
3521 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003522 return sprintf(page, "%d\n", mddev->layout);
3523}
3524
3525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003526layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003527{
3528 char *e;
3529 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003530
3531 if (!*buf || (*e && *e != '\n'))
3532 return -EINVAL;
3533
NeilBrownb3546032009-03-31 14:56:41 +11003534 if (mddev->pers) {
3535 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003536 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003537 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003538 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003539 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003540 if (err) {
3541 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003542 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003543 }
NeilBrownb3546032009-03-31 14:56:41 +11003544 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003545 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003546 if (mddev->reshape_position == MaxSector)
3547 mddev->layout = n;
3548 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003549 return len;
3550}
3551static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003552__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003553
3554
NeilBrowneae17012005-11-08 21:39:23 -08003555static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003556raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003557{
NeilBrownbb636542005-11-08 21:39:45 -08003558 if (mddev->raid_disks == 0)
3559 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003560 if (mddev->reshape_position != MaxSector &&
3561 mddev->delta_disks != 0)
3562 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3563 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003564 return sprintf(page, "%d\n", mddev->raid_disks);
3565}
3566
NeilBrownfd01b882011-10-11 16:47:53 +11003567static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003568
3569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003570raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003571{
NeilBrownda943b992006-01-06 00:20:54 -08003572 char *e;
3573 int rv = 0;
3574 unsigned long n = simple_strtoul(buf, &e, 10);
3575
3576 if (!*buf || (*e && *e != '\n'))
3577 return -EINVAL;
3578
3579 if (mddev->pers)
3580 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003581 else if (mddev->reshape_position != MaxSector) {
3582 int olddisks = mddev->raid_disks - mddev->delta_disks;
3583 mddev->delta_disks = n - olddisks;
3584 mddev->raid_disks = n;
3585 } else
NeilBrownda943b992006-01-06 00:20:54 -08003586 mddev->raid_disks = n;
3587 return rv ? rv : len;
3588}
3589static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003590__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003591
NeilBrown24dd4692005-11-08 21:39:26 -08003592static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003593chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003594{
NeilBrown08a02ec2007-05-09 02:35:38 -07003595 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003596 mddev->chunk_sectors != mddev->new_chunk_sectors)
3597 return sprintf(page, "%d (%d)\n",
3598 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003599 mddev->chunk_sectors << 9);
3600 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003601}
3602
3603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003604chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003605{
NeilBrown3b343802006-01-06 00:20:47 -08003606 char *e;
3607 unsigned long n = simple_strtoul(buf, &e, 10);
3608
NeilBrown3b343802006-01-06 00:20:47 -08003609 if (!*buf || (*e && *e != '\n'))
3610 return -EINVAL;
3611
NeilBrownb3546032009-03-31 14:56:41 +11003612 if (mddev->pers) {
3613 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003614 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003615 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003616 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003617 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003618 if (err) {
3619 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003620 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003621 }
NeilBrownb3546032009-03-31 14:56:41 +11003622 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003623 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003624 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003625 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003626 }
NeilBrown3b343802006-01-06 00:20:47 -08003627 return len;
3628}
3629static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003630__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003631
NeilBrowna94213b2006-06-26 00:28:00 -07003632static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003633resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003634{
NeilBrownd1a7c502009-03-31 15:24:32 +11003635 if (mddev->recovery_cp == MaxSector)
3636 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003637 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3638}
3639
3640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003641resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003642{
NeilBrowna94213b2006-06-26 00:28:00 -07003643 char *e;
3644 unsigned long long n = simple_strtoull(buf, &e, 10);
3645
NeilBrownb0986362011-05-11 15:52:21 +10003646 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003647 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003648 if (cmd_match(buf, "none"))
3649 n = MaxSector;
3650 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003651 return -EINVAL;
3652
3653 mddev->recovery_cp = n;
3654 return len;
3655}
3656static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003657__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003658
NeilBrown9e653b62006-06-26 00:27:58 -07003659/*
3660 * The array state can be:
3661 *
3662 * clear
3663 * No devices, no size, no level
3664 * Equivalent to STOP_ARRAY ioctl
3665 * inactive
3666 * May have some settings, but array is not active
3667 * all IO results in error
3668 * When written, doesn't tear down array, but just stops it
3669 * suspended (not supported yet)
3670 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003671 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003672 * readonly
3673 * no resync can happen. no superblocks get written.
3674 * write requests fail
3675 * read-auto
3676 * like readonly, but behaves like 'clean' on a write request.
3677 *
3678 * clean - no pending writes, but otherwise active.
3679 * When written to inactive array, starts without resync
3680 * If a write request arrives then
3681 * if metadata is known, mark 'dirty' and switch to 'active'.
3682 * if not known, block and switch to write-pending
3683 * If written to an active array that has pending writes, then fails.
3684 * active
3685 * fully active: IO and resync can be happening.
3686 * When written to inactive array, starts with resync
3687 *
3688 * write-pending
3689 * clean, but writes are blocked waiting for 'active' to be written.
3690 *
3691 * active-idle
3692 * like active, but no writes have been seen for a while (100msec).
3693 *
3694 */
3695enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3696 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003697static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003698 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3699 "write-pending", "active-idle", NULL };
3700
3701static int match_word(const char *word, char **list)
3702{
3703 int n;
3704 for (n=0; list[n]; n++)
3705 if (cmd_match(word, list[n]))
3706 break;
3707 return n;
3708}
3709
3710static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003711array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003712{
3713 enum array_state st = inactive;
3714
3715 if (mddev->pers)
3716 switch(mddev->ro) {
3717 case 1:
3718 st = readonly;
3719 break;
3720 case 2:
3721 st = read_auto;
3722 break;
3723 case 0:
3724 if (mddev->in_sync)
3725 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003726 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003727 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003728 else if (mddev->safemode)
3729 st = active_idle;
3730 else
3731 st = active;
3732 }
3733 else {
3734 if (list_empty(&mddev->disks) &&
3735 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003736 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003737 st = clear;
3738 else
3739 st = inactive;
3740 }
3741 return sprintf(page, "%s\n", array_states[st]);
3742}
3743
NeilBrownfd01b882011-10-11 16:47:53 +11003744static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3745static int md_set_readonly(struct mddev * mddev, int is_open);
3746static int do_md_run(struct mddev * mddev);
3747static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003748
3749static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003750array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003751{
3752 int err = -EINVAL;
3753 enum array_state st = match_word(buf, array_states);
3754 switch(st) {
3755 case bad_word:
3756 break;
3757 case clear:
3758 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003759 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003760 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003761 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003762 break;
3763 case inactive:
3764 /* stopping an active array */
3765 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003766 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003767 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003768 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003769 } else
3770 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003771 break;
3772 case suspended:
3773 break; /* not supported yet */
3774 case readonly:
3775 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003776 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003777 else {
3778 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003779 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003780 err = do_md_run(mddev);
3781 }
3782 break;
3783 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003784 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003785 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003786 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003787 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003788 err = restart_array(mddev);
3789 if (err == 0) {
3790 mddev->ro = 2;
3791 set_disk_ro(mddev->gendisk, 0);
3792 }
NeilBrown9e653b62006-06-26 00:27:58 -07003793 } else {
3794 mddev->ro = 2;
3795 err = do_md_run(mddev);
3796 }
3797 break;
3798 case clean:
3799 if (mddev->pers) {
3800 restart_array(mddev);
3801 spin_lock_irq(&mddev->write_lock);
3802 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003803 if (mddev->in_sync == 0) {
3804 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003805 if (mddev->safemode == 1)
3806 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003807 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003808 }
3809 err = 0;
3810 } else
3811 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003812 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003813 } else
3814 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003815 break;
3816 case active:
3817 if (mddev->pers) {
3818 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003819 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003820 wake_up(&mddev->sb_wait);
3821 err = 0;
3822 } else {
3823 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003824 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003825 err = do_md_run(mddev);
3826 }
3827 break;
3828 case write_pending:
3829 case active_idle:
3830 /* these cannot be set */
3831 break;
3832 }
3833 if (err)
3834 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003835 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003836 if (mddev->hold_active == UNTIL_IOCTL)
3837 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003838 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003839 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003840 }
NeilBrown9e653b62006-06-26 00:27:58 -07003841}
NeilBrown80ca3a42006-07-10 04:44:18 -07003842static struct md_sysfs_entry md_array_state =
3843__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003844
NeilBrown6d7ff7382006-01-06 00:21:16 -08003845static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003846max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003847 return sprintf(page, "%d\n",
3848 atomic_read(&mddev->max_corr_read_errors));
3849}
3850
3851static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003852max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003853{
3854 char *e;
3855 unsigned long n = simple_strtoul(buf, &e, 10);
3856
3857 if (*buf && (*e == 0 || *e == '\n')) {
3858 atomic_set(&mddev->max_corr_read_errors, n);
3859 return len;
3860 }
3861 return -EINVAL;
3862}
3863
3864static struct md_sysfs_entry max_corr_read_errors =
3865__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3866 max_corrected_read_errors_store);
3867
3868static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003869null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003870{
3871 return -EINVAL;
3872}
3873
3874static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003875new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003876{
3877 /* buf must be %d:%d\n? giving major and minor numbers */
3878 /* The new device is added to the array.
3879 * If the array has a persistent superblock, we read the
3880 * superblock to initialise info and check validity.
3881 * Otherwise, only checking done is that in bind_rdev_to_array,
3882 * which mainly checks size.
3883 */
3884 char *e;
3885 int major = simple_strtoul(buf, &e, 10);
3886 int minor;
3887 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003888 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003889 int err;
3890
3891 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3892 return -EINVAL;
3893 minor = simple_strtoul(e+1, &e, 10);
3894 if (*e && *e != '\n')
3895 return -EINVAL;
3896 dev = MKDEV(major, minor);
3897 if (major != MAJOR(dev) ||
3898 minor != MINOR(dev))
3899 return -EOVERFLOW;
3900
3901
3902 if (mddev->persistent) {
3903 rdev = md_import_device(dev, mddev->major_version,
3904 mddev->minor_version);
3905 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003906 struct md_rdev *rdev0
3907 = list_entry(mddev->disks.next,
3908 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003909 err = super_types[mddev->major_version]
3910 .load_super(rdev, rdev0, mddev->minor_version);
3911 if (err < 0)
3912 goto out;
3913 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003914 } else if (mddev->external)
3915 rdev = md_import_device(dev, -2, -1);
3916 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003917 rdev = md_import_device(dev, -1, -1);
3918
3919 if (IS_ERR(rdev))
3920 return PTR_ERR(rdev);
3921 err = bind_rdev_to_array(rdev, mddev);
3922 out:
3923 if (err)
3924 export_rdev(rdev);
3925 return err ? err : len;
3926}
3927
3928static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003929__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003930
3931static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003932bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003933{
3934 char *end;
3935 unsigned long chunk, end_chunk;
3936
3937 if (!mddev->bitmap)
3938 goto out;
3939 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3940 while (*buf) {
3941 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3942 if (buf == end) break;
3943 if (*end == '-') { /* range */
3944 buf = end + 1;
3945 end_chunk = simple_strtoul(buf, &end, 0);
3946 if (buf == end) break;
3947 }
3948 if (*end && !isspace(*end)) break;
3949 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003950 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003951 }
3952 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3953out:
3954 return len;
3955}
3956
3957static struct md_sysfs_entry md_bitmap =
3958__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3959
3960static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003961size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003962{
Andre Noll58c0fed2009-03-31 14:33:13 +11003963 return sprintf(page, "%llu\n",
3964 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003965}
3966
NeilBrownfd01b882011-10-11 16:47:53 +11003967static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003968
3969static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003970size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003971{
3972 /* If array is inactive, we can reduce the component size, but
3973 * not increase it (except from 0).
3974 * If array is active, we can try an on-line resize
3975 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003976 sector_t sectors;
3977 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003978
Andre Noll58c0fed2009-03-31 14:33:13 +11003979 if (err < 0)
3980 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003981 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003982 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003983 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003984 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003985 if (mddev->dev_sectors == 0 ||
3986 mddev->dev_sectors > sectors)
3987 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003988 else
3989 err = -ENOSPC;
3990 }
3991 return err ? err : len;
3992}
3993
3994static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003995__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003996
NeilBrown8bb93aa2006-01-06 00:20:50 -08003997
3998/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003999 * This is one of
4000 * 'none' for arrays with no metadata (good luck...)
4001 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004002 * or N.M for internally known formats
4003 */
4004static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004005metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004006{
4007 if (mddev->persistent)
4008 return sprintf(page, "%d.%d\n",
4009 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004010 else if (mddev->external)
4011 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004012 else
4013 return sprintf(page, "none\n");
4014}
4015
4016static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004017metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004018{
4019 int major, minor;
4020 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004021 /* Changing the details of 'external' metadata is
4022 * always permitted. Otherwise there must be
4023 * no devices attached to the array.
4024 */
4025 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4026 ;
4027 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004028 return -EBUSY;
4029
4030 if (cmd_match(buf, "none")) {
4031 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004032 mddev->external = 0;
4033 mddev->major_version = 0;
4034 mddev->minor_version = 90;
4035 return len;
4036 }
4037 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004038 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004039 if (namelen >= sizeof(mddev->metadata_type))
4040 namelen = sizeof(mddev->metadata_type)-1;
4041 strncpy(mddev->metadata_type, buf+9, namelen);
4042 mddev->metadata_type[namelen] = 0;
4043 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4044 mddev->metadata_type[--namelen] = 0;
4045 mddev->persistent = 0;
4046 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004047 mddev->major_version = 0;
4048 mddev->minor_version = 90;
4049 return len;
4050 }
4051 major = simple_strtoul(buf, &e, 10);
4052 if (e==buf || *e != '.')
4053 return -EINVAL;
4054 buf = e+1;
4055 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004056 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004057 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004058 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004059 return -ENOENT;
4060 mddev->major_version = major;
4061 mddev->minor_version = minor;
4062 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004063 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004064 return len;
4065}
4066
4067static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004068__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004069
NeilBrowna35b0d62006-01-06 00:20:49 -08004070static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004071action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004072{
NeilBrown7eec3142005-11-08 21:39:44 -08004073 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004074 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4075 type = "frozen";
4076 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004077 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004078 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4079 type = "reshape";
4080 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004081 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4082 type = "resync";
4083 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4084 type = "check";
4085 else
4086 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004087 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004088 type = "recover";
4089 }
4090 return sprintf(page, "%s\n", type);
4091}
4092
NeilBrownfd01b882011-10-11 16:47:53 +11004093static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004094
NeilBrown24dd4692005-11-08 21:39:26 -08004095static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004096action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004097{
NeilBrown7eec3142005-11-08 21:39:44 -08004098 if (!mddev->pers || !mddev->pers->sync_request)
4099 return -EINVAL;
4100
NeilBrownb6a9ce62009-05-26 09:41:17 +10004101 if (cmd_match(page, "frozen"))
4102 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4103 else
4104 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4105
4106 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004107 if (mddev->sync_thread) {
4108 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004109 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004110 }
NeilBrown03c902e2006-01-06 00:20:46 -08004111 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4112 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004113 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004114 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004115 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004116 else if (cmd_match(page, "recover")) {
4117 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4119 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004120 int err;
4121 if (mddev->pers->start_reshape == NULL)
4122 return -EINVAL;
4123 err = mddev->pers->start_reshape(mddev);
4124 if (err)
4125 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004126 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004127 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004128 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004129 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004130 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004131 return -EINVAL;
4132 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4133 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004134 }
NeilBrown03c902e2006-01-06 00:20:46 -08004135 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004136 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004137 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004138 return len;
4139}
4140
NeilBrown9d888832005-11-08 21:39:26 -08004141static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004142mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004143{
4144 return sprintf(page, "%llu\n",
4145 (unsigned long long) mddev->resync_mismatches);
4146}
4147
NeilBrown80ca3a42006-07-10 04:44:18 -07004148static struct md_sysfs_entry md_scan_mode =
4149__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004150
NeilBrown96de1e62005-11-08 21:39:39 -08004151
NeilBrown80ca3a42006-07-10 04:44:18 -07004152static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004153
NeilBrown88202a02006-01-06 00:21:36 -08004154static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004155sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004156{
4157 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4158 mddev->sync_speed_min ? "local": "system");
4159}
4160
4161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004163{
4164 int min;
4165 char *e;
4166 if (strncmp(buf, "system", 6)==0) {
4167 mddev->sync_speed_min = 0;
4168 return len;
4169 }
4170 min = simple_strtoul(buf, &e, 10);
4171 if (buf == e || (*e && *e != '\n') || min <= 0)
4172 return -EINVAL;
4173 mddev->sync_speed_min = min;
4174 return len;
4175}
4176
4177static struct md_sysfs_entry md_sync_min =
4178__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4179
4180static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004181sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004182{
4183 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4184 mddev->sync_speed_max ? "local": "system");
4185}
4186
4187static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004188sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004189{
4190 int max;
4191 char *e;
4192 if (strncmp(buf, "system", 6)==0) {
4193 mddev->sync_speed_max = 0;
4194 return len;
4195 }
4196 max = simple_strtoul(buf, &e, 10);
4197 if (buf == e || (*e && *e != '\n') || max <= 0)
4198 return -EINVAL;
4199 mddev->sync_speed_max = max;
4200 return len;
4201}
4202
4203static struct md_sysfs_entry md_sync_max =
4204__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4205
Iustin Popd7f3d292007-10-16 23:30:54 -07004206static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004207degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004208{
4209 return sprintf(page, "%d\n", mddev->degraded);
4210}
4211static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004212
4213static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004214sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004215{
4216 return sprintf(page, "%d\n", mddev->parallel_resync);
4217}
4218
4219static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004220sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004221{
4222 long n;
4223
4224 if (strict_strtol(buf, 10, &n))
4225 return -EINVAL;
4226
4227 if (n != 0 && n != 1)
4228 return -EINVAL;
4229
4230 mddev->parallel_resync = n;
4231
4232 if (mddev->sync_thread)
4233 wake_up(&resync_wait);
4234
4235 return len;
4236}
4237
4238/* force parallel resync, even with shared block devices */
4239static struct md_sysfs_entry md_sync_force_parallel =
4240__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4241 sync_force_parallel_show, sync_force_parallel_store);
4242
4243static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004244sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004245{
4246 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004247 if (mddev->curr_resync == 0)
4248 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004249 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4250 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004251 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004252 db = resync - mddev->resync_mark_cnt;
4253 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004254}
4255
NeilBrown80ca3a42006-07-10 04:44:18 -07004256static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004257
4258static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004259sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004260{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004261 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004262
NeilBrownacb180b2009-04-14 16:28:34 +10004263 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4264 return sprintf(page, "none\n");
4265
NeilBrown88202a02006-01-06 00:21:36 -08004266 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004267 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004268 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004269 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004270
NeilBrownacb180b2009-04-14 16:28:34 +10004271 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004272 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004273}
4274
NeilBrown80ca3a42006-07-10 04:44:18 -07004275static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004276
NeilBrowne464eaf2006-03-27 01:18:14 -08004277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004279{
4280 return sprintf(page, "%llu\n",
4281 (unsigned long long)mddev->resync_min);
4282}
4283static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004284min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004285{
4286 unsigned long long min;
4287 if (strict_strtoull(buf, 10, &min))
4288 return -EINVAL;
4289 if (min > mddev->resync_max)
4290 return -EINVAL;
4291 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4292 return -EBUSY;
4293
4294 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004295 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004296 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004297 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004298 return -EINVAL;
4299 }
4300 mddev->resync_min = min;
4301
4302 return len;
4303}
4304
4305static struct md_sysfs_entry md_min_sync =
4306__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4307
4308static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004309max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004310{
4311 if (mddev->resync_max == MaxSector)
4312 return sprintf(page, "max\n");
4313 else
4314 return sprintf(page, "%llu\n",
4315 (unsigned long long)mddev->resync_max);
4316}
4317static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004318max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004319{
4320 if (strncmp(buf, "max", 3) == 0)
4321 mddev->resync_max = MaxSector;
4322 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004323 unsigned long long max;
4324 if (strict_strtoull(buf, 10, &max))
4325 return -EINVAL;
4326 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004327 return -EINVAL;
4328 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004329 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004330 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4331 return -EBUSY;
4332
4333 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004334 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004335 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004336 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004337 return -EINVAL;
4338 }
4339 mddev->resync_max = max;
4340 }
4341 wake_up(&mddev->recovery_wait);
4342 return len;
4343}
4344
4345static struct md_sysfs_entry md_max_sync =
4346__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4347
4348static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004349suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004350{
4351 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4352}
4353
4354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004356{
4357 char *e;
4358 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004359 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004360
NeilBrownb8d966e2009-07-01 11:14:04 +10004361 if (mddev->pers == NULL ||
4362 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004363 return -EINVAL;
4364 if (buf == e || (*e && *e != '\n'))
4365 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004366
4367 mddev->suspend_lo = new;
4368 if (new >= old)
4369 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004370 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004371 else {
4372 /* Expanding suspended region - need to wait */
4373 mddev->pers->quiesce(mddev, 1);
4374 mddev->pers->quiesce(mddev, 0);
4375 }
4376 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004377}
4378static struct md_sysfs_entry md_suspend_lo =
4379__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4380
4381
4382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004383suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004384{
4385 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4386}
4387
4388static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004389suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004390{
4391 char *e;
4392 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004393 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004394
NeilBrownb8d966e2009-07-01 11:14:04 +10004395 if (mddev->pers == NULL ||
4396 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004397 return -EINVAL;
4398 if (buf == e || (*e && *e != '\n'))
4399 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004400
4401 mddev->suspend_hi = new;
4402 if (new <= old)
4403 /* Shrinking suspended region */
4404 mddev->pers->quiesce(mddev, 2);
4405 else {
4406 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004407 mddev->pers->quiesce(mddev, 1);
4408 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004409 }
4410 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004411}
4412static struct md_sysfs_entry md_suspend_hi =
4413__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4414
NeilBrown08a02ec2007-05-09 02:35:38 -07004415static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004416reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004417{
4418 if (mddev->reshape_position != MaxSector)
4419 return sprintf(page, "%llu\n",
4420 (unsigned long long)mddev->reshape_position);
4421 strcpy(page, "none\n");
4422 return 5;
4423}
4424
4425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004426reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004427{
4428 char *e;
4429 unsigned long long new = simple_strtoull(buf, &e, 10);
4430 if (mddev->pers)
4431 return -EBUSY;
4432 if (buf == e || (*e && *e != '\n'))
4433 return -EINVAL;
4434 mddev->reshape_position = new;
4435 mddev->delta_disks = 0;
4436 mddev->new_level = mddev->level;
4437 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004438 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004439 return len;
4440}
4441
4442static struct md_sysfs_entry md_reshape_position =
4443__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4444 reshape_position_store);
4445
Dan Williamsb522adc2009-03-31 15:00:31 +11004446static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004447array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004448{
4449 if (mddev->external_size)
4450 return sprintf(page, "%llu\n",
4451 (unsigned long long)mddev->array_sectors/2);
4452 else
4453 return sprintf(page, "default\n");
4454}
4455
4456static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004457array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004458{
4459 sector_t sectors;
4460
4461 if (strncmp(buf, "default", 7) == 0) {
4462 if (mddev->pers)
4463 sectors = mddev->pers->size(mddev, 0, 0);
4464 else
4465 sectors = mddev->array_sectors;
4466
4467 mddev->external_size = 0;
4468 } else {
4469 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4470 return -EINVAL;
4471 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004472 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004473
4474 mddev->external_size = 1;
4475 }
4476
4477 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004478 if (mddev->pers) {
4479 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004480 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004481 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004482 return len;
4483}
4484
4485static struct md_sysfs_entry md_array_size =
4486__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4487 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004488
NeilBrowneae17012005-11-08 21:39:23 -08004489static struct attribute *md_default_attrs[] = {
4490 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004491 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004492 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004493 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004494 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004495 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004496 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004497 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004498 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004499 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004500 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004501 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004502 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004503 NULL,
4504};
4505
4506static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004507 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004508 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004509 &md_sync_min.attr,
4510 &md_sync_max.attr,
4511 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004512 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004513 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004514 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004515 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004516 &md_suspend_lo.attr,
4517 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004518 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004519 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004520 NULL,
4521};
NeilBrown411036f2005-11-08 21:39:40 -08004522static struct attribute_group md_redundancy_group = {
4523 .name = NULL,
4524 .attrs = md_redundancy_attrs,
4525};
4526
NeilBrowneae17012005-11-08 21:39:23 -08004527
4528static ssize_t
4529md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4530{
4531 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004532 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004533 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004534
4535 if (!entry->show)
4536 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004537 spin_lock(&all_mddevs_lock);
4538 if (list_empty(&mddev->all_mddevs)) {
4539 spin_unlock(&all_mddevs_lock);
4540 return -EBUSY;
4541 }
4542 mddev_get(mddev);
4543 spin_unlock(&all_mddevs_lock);
4544
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004545 rv = mddev_lock(mddev);
4546 if (!rv) {
4547 rv = entry->show(mddev, page);
4548 mddev_unlock(mddev);
4549 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004550 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004551 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004552}
4553
4554static ssize_t
4555md_attr_store(struct kobject *kobj, struct attribute *attr,
4556 const char *page, size_t length)
4557{
4558 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004559 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004560 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004561
4562 if (!entry->store)
4563 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004564 if (!capable(CAP_SYS_ADMIN))
4565 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004566 spin_lock(&all_mddevs_lock);
4567 if (list_empty(&mddev->all_mddevs)) {
4568 spin_unlock(&all_mddevs_lock);
4569 return -EBUSY;
4570 }
4571 mddev_get(mddev);
4572 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004573 rv = mddev_lock(mddev);
4574 if (!rv) {
4575 rv = entry->store(mddev, page, length);
4576 mddev_unlock(mddev);
4577 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004578 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004579 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004580}
4581
4582static void md_free(struct kobject *ko)
4583{
NeilBrownfd01b882011-10-11 16:47:53 +11004584 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004585
4586 if (mddev->sysfs_state)
4587 sysfs_put(mddev->sysfs_state);
4588
4589 if (mddev->gendisk) {
4590 del_gendisk(mddev->gendisk);
4591 put_disk(mddev->gendisk);
4592 }
4593 if (mddev->queue)
4594 blk_cleanup_queue(mddev->queue);
4595
NeilBrowneae17012005-11-08 21:39:23 -08004596 kfree(mddev);
4597}
4598
Emese Revfy52cf25d2010-01-19 02:58:23 +01004599static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004600 .show = md_attr_show,
4601 .store = md_attr_store,
4602};
4603static struct kobj_type md_ktype = {
4604 .release = md_free,
4605 .sysfs_ops = &md_sysfs_ops,
4606 .default_attrs = md_default_attrs,
4607};
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609int mdp_major = 0;
4610
Dan Williams5fd3a172009-03-04 00:57:25 -07004611static void mddev_delayed_delete(struct work_struct *ws)
4612{
NeilBrownfd01b882011-10-11 16:47:53 +11004613 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004614
NeilBrown43a70502009-12-14 12:49:55 +11004615 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004616 kobject_del(&mddev->kobj);
4617 kobject_put(&mddev->kobj);
4618}
4619
NeilBrownefeb53c2009-01-09 08:31:10 +11004620static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004622 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004623 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004625 int partitioned;
4626 int shift;
4627 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004628 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004631 return -ENODEV;
4632
4633 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4634 shift = partitioned ? MdpMinorShift : 0;
4635 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Tejun Heoe804ac72010-10-15 15:36:08 +02004637 /* wait for any previous instance of this device to be
4638 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004639 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004640 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004641
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004642 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004643 error = -EEXIST;
4644 if (mddev->gendisk)
4645 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004646
4647 if (name) {
4648 /* Need to ensure that 'name' is not a duplicate.
4649 */
NeilBrownfd01b882011-10-11 16:47:53 +11004650 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004651 spin_lock(&all_mddevs_lock);
4652
4653 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4654 if (mddev2->gendisk &&
4655 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4656 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004657 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004658 }
4659 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
NeilBrown8b765392009-01-09 08:31:08 +11004661
NeilBrown0909dc42009-07-01 12:27:21 +10004662 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004663 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004664 if (!mddev->queue)
4665 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004666 mddev->queue->queuedata = mddev;
4667
NeilBrown409c57f2009-03-31 14:39:39 +11004668 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004669 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 disk = alloc_disk(1 << shift);
4672 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004673 blk_cleanup_queue(mddev->queue);
4674 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004675 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004677 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004679 if (name)
4680 strcpy(disk->disk_name, name);
4681 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004683 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 disk->fops = &md_fops;
4686 disk->private_data = mddev;
4687 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004688 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004689 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004690 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004691 * remove it now.
4692 */
4693 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004695 /* As soon as we call add_disk(), another thread could get
4696 * through to md_open, so make sure it doesn't get too far
4697 */
4698 mutex_lock(&mddev->open_mutex);
4699 add_disk(disk);
4700
Tejun Heoed9e1982008-08-25 19:56:05 +09004701 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4702 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004703 if (error) {
4704 /* This isn't possible, but as kobject_init_and_add is marked
4705 * __must_check, we must do something with the result
4706 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004707 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4708 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004709 error = 0;
4710 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004711 if (mddev->kobj.sd &&
4712 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004713 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004714 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004715 abort:
4716 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004717 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004718 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004719 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004720 }
NeilBrownd3374822009-01-09 08:31:10 +11004721 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004722 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004723}
4724
4725static struct kobject *md_probe(dev_t dev, int *part, void *data)
4726{
4727 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 return NULL;
4729}
4730
NeilBrownefeb53c2009-01-09 08:31:10 +11004731static int add_named_array(const char *val, struct kernel_param *kp)
4732{
4733 /* val must be "md_*" where * is not all digits.
4734 * We allocate an array with a large free minor number, and
4735 * set the name to val. val must not already be an active name.
4736 */
4737 int len = strlen(val);
4738 char buf[DISK_NAME_LEN];
4739
4740 while (len && val[len-1] == '\n')
4741 len--;
4742 if (len >= DISK_NAME_LEN)
4743 return -E2BIG;
4744 strlcpy(buf, val, len+1);
4745 if (strncmp(buf, "md_", 3) != 0)
4746 return -EINVAL;
4747 return md_alloc(0, buf);
4748}
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750static void md_safemode_timeout(unsigned long data)
4751{
NeilBrownfd01b882011-10-11 16:47:53 +11004752 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Neil Brown0fd62b82008-06-28 08:31:36 +10004754 if (!atomic_read(&mddev->writes_pending)) {
4755 mddev->safemode = 1;
4756 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004757 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 md_wakeup_thread(mddev->thread);
4760}
4761
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004762static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
NeilBrownfd01b882011-10-11 16:47:53 +11004764int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
NeilBrown2604b702006-01-06 00:20:36 -08004766 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004767 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004768 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
NeilBrowna757e642005-04-16 15:26:42 -07004770 if (list_empty(&mddev->disks))
4771 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 if (mddev->pers)
4775 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004776 /* Cannot run until previous stop completes properly */
4777 if (mddev->sysfs_active)
4778 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 /*
4781 * Analyze all RAID superblock(s)
4782 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004783 if (!mddev->raid_disks) {
4784 if (!mddev->persistent)
4785 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004786 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
NeilBrownd9d166c2006-01-06 00:20:51 -08004789 if (mddev->level != LEVEL_NONE)
4790 request_module("md-level-%d", mddev->level);
4791 else if (mddev->clevel[0])
4792 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 /*
4795 * Drop all container device buffers, from now on
4796 * the only valid external interface is through the md
4797 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004799 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004800 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 continue;
4802 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004803 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004804
4805 /* perform some consistency tests on the device.
4806 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004807 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004808 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004809 if (rdev->meta_bdev) {
4810 /* Nothing to check */;
4811 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004812 if (mddev->dev_sectors &&
4813 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004814 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004815 printk("md: %s: data overlaps metadata\n",
4816 mdname(mddev));
4817 return -EINVAL;
4818 }
4819 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004820 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004821 > rdev->data_offset) {
4822 printk("md: %s: metadata overlaps data\n",
4823 mdname(mddev));
4824 return -EINVAL;
4825 }
4826 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004827 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 }
4829
NeilBrowna167f662010-10-26 18:31:13 +11004830 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004831 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004832 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004835 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004836 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004838 if (mddev->level != LEVEL_NONE)
4839 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4840 mddev->level);
4841 else
4842 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4843 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return -EINVAL;
4845 }
NeilBrown2604b702006-01-06 00:20:36 -08004846 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004848 if (mddev->level != pers->level) {
4849 mddev->level = pers->level;
4850 mddev->new_level = pers->level;
4851 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004852 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
NeilBrownf6705572006-03-27 01:18:11 -08004854 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004855 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004856 /* This personality cannot handle reshaping... */
4857 mddev->pers = NULL;
4858 module_put(pers->owner);
4859 return -EINVAL;
4860 }
4861
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004862 if (pers->sync_request) {
4863 /* Warn if this is a potentially silly
4864 * configuration.
4865 */
4866 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004867 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004868 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004869
NeilBrowndafb20f2012-03-19 12:46:39 +11004870 rdev_for_each(rdev, mddev)
4871 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004872 if (rdev < rdev2 &&
4873 rdev->bdev->bd_contains ==
4874 rdev2->bdev->bd_contains) {
4875 printk(KERN_WARNING
4876 "%s: WARNING: %s appears to be"
4877 " on the same physical disk as"
4878 " %s.\n",
4879 mdname(mddev),
4880 bdevname(rdev->bdev,b),
4881 bdevname(rdev2->bdev,b2));
4882 warned = 1;
4883 }
4884 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004885
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004886 if (warned)
4887 printk(KERN_WARNING
4888 "True protection against single-disk"
4889 " failure might be compromised.\n");
4890 }
4891
NeilBrown657390d2005-08-26 18:34:16 -07004892 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004893 /* may be over-ridden by personality */
4894 mddev->resync_max_sectors = mddev->dev_sectors;
4895
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004896 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
NeilBrown0f9552b52009-12-30 12:08:50 +11004898 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004899 mddev->ro = 2; /* read-only, but switch on first write */
4900
NeilBrownb15c2e52006-01-06 00:20:16 -08004901 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004902 if (err)
4903 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004904 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4905 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4906 " but 'external_size' not in effect?\n", __func__);
4907 printk(KERN_ERR
4908 "md: invalid array_size %llu > default size %llu\n",
4909 (unsigned long long)mddev->array_sectors / 2,
4910 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4911 err = -EINVAL;
4912 mddev->pers->stop(mddev);
4913 }
4914 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004915 err = bitmap_create(mddev);
4916 if (err) {
4917 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4918 mdname(mddev), err);
4919 mddev->pers->stop(mddev);
4920 }
4921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 module_put(mddev->pers->owner);
4924 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004925 bitmap_destroy(mddev);
4926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004928 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004929 if (mddev->kobj.sd &&
4930 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004931 printk(KERN_WARNING
4932 "md: cannot register extra attributes for %s\n",
4933 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004934 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004935 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004936 mddev->ro = 0;
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004939 atomic_set(&mddev->max_corr_read_errors,
4940 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 mddev->safemode = 0;
4942 mddev->safemode_timer.function = md_safemode_timeout;
4943 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004944 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004946 smp_wmb();
4947 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11004948 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10004949 if (rdev->raid_disk >= 0)
4950 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004951 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4954
NeilBrown850b2b422006-10-03 01:15:46 -07004955 if (mddev->flags)
4956 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
NeilBrownd7603b72006-01-06 00:20:30 -08004958 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004959 sysfs_notify_dirent_safe(mddev->sysfs_state);
4960 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004961 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return 0;
4963}
NeilBrown390ee602010-06-01 19:37:27 +10004964EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
NeilBrownfd01b882011-10-11 16:47:53 +11004966static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004967{
4968 int err;
4969
4970 err = md_run(mddev);
4971 if (err)
4972 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004973 err = bitmap_load(mddev);
4974 if (err) {
4975 bitmap_destroy(mddev);
4976 goto out;
4977 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004978
4979 md_wakeup_thread(mddev->thread);
4980 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4981
NeilBrownfe60b012010-03-29 11:10:42 +11004982 set_capacity(mddev->gendisk, mddev->array_sectors);
4983 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004984 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004985 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4986out:
4987 return err;
4988}
4989
NeilBrownfd01b882011-10-11 16:47:53 +11004990static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
4992 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Andre Noll80fab1d2008-07-11 22:02:21 +10004994 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004996 return -ENXIO;
4997 if (!mddev->pers)
4998 return -EINVAL;
4999 if (!mddev->ro)
5000 return -EBUSY;
5001 mddev->safemode = 0;
5002 mddev->ro = 0;
5003 set_disk_ro(disk, 0);
5004 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5005 mdname(mddev));
5006 /* Kick recovery or resync if necessary */
5007 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5008 md_wakeup_thread(mddev->thread);
5009 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005010 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005011 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
NeilBrownacc55e22006-06-26 00:27:47 -07005014/* similar to deny_write_access, but accounts for our holding a reference
5015 * to the file ourselves */
5016static int deny_bitmap_write_access(struct file * file)
5017{
5018 struct inode *inode = file->f_mapping->host;
5019
5020 spin_lock(&inode->i_lock);
5021 if (atomic_read(&inode->i_writecount) > 1) {
5022 spin_unlock(&inode->i_lock);
5023 return -ETXTBSY;
5024 }
5025 atomic_set(&inode->i_writecount, -1);
5026 spin_unlock(&inode->i_lock);
5027
5028 return 0;
5029}
5030
NeilBrown43a70502009-12-14 12:49:55 +11005031void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005032{
5033 struct inode *inode = file->f_mapping->host;
5034
5035 spin_lock(&inode->i_lock);
5036 atomic_set(&inode->i_writecount, 1);
5037 spin_unlock(&inode->i_lock);
5038}
5039
NeilBrownfd01b882011-10-11 16:47:53 +11005040static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005041{
5042 mddev->array_sectors = 0;
5043 mddev->external_size = 0;
5044 mddev->dev_sectors = 0;
5045 mddev->raid_disks = 0;
5046 mddev->recovery_cp = 0;
5047 mddev->resync_min = 0;
5048 mddev->resync_max = MaxSector;
5049 mddev->reshape_position = MaxSector;
5050 mddev->external = 0;
5051 mddev->persistent = 0;
5052 mddev->level = LEVEL_NONE;
5053 mddev->clevel[0] = 0;
5054 mddev->flags = 0;
5055 mddev->ro = 0;
5056 mddev->metadata_type[0] = 0;
5057 mddev->chunk_sectors = 0;
5058 mddev->ctime = mddev->utime = 0;
5059 mddev->layout = 0;
5060 mddev->max_disks = 0;
5061 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005062 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005063 mddev->delta_disks = 0;
5064 mddev->new_level = LEVEL_NONE;
5065 mddev->new_layout = 0;
5066 mddev->new_chunk_sectors = 0;
5067 mddev->curr_resync = 0;
5068 mddev->resync_mismatches = 0;
5069 mddev->suspend_lo = mddev->suspend_hi = 0;
5070 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5071 mddev->recovery = 0;
5072 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005073 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005074 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005075 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005076 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005077 mddev->bitmap_info.offset = 0;
5078 mddev->bitmap_info.default_offset = 0;
5079 mddev->bitmap_info.chunksize = 0;
5080 mddev->bitmap_info.daemon_sleep = 0;
5081 mddev->bitmap_info.max_write_behind = 0;
5082}
5083
NeilBrownfd01b882011-10-11 16:47:53 +11005084static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005085{
5086 if (mddev->sync_thread) {
5087 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5088 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005089 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005090 }
5091
5092 del_timer_sync(&mddev->safemode_timer);
5093
5094 bitmap_flush(mddev);
5095 md_super_wait(mddev);
5096
5097 if (!mddev->in_sync || mddev->flags) {
5098 /* mark array as shutdown cleanly */
5099 mddev->in_sync = 1;
5100 md_update_sb(mddev, 1);
5101 }
5102}
NeilBrowndefad612011-01-14 09:14:33 +11005103
NeilBrownfd01b882011-10-11 16:47:53 +11005104void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005105{
5106 mddev_lock(mddev);
5107 __md_stop_writes(mddev);
5108 mddev_unlock(mddev);
5109}
NeilBrown390ee602010-06-01 19:37:27 +10005110EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005111
NeilBrownfd01b882011-10-11 16:47:53 +11005112void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005113{
NeilBrown0ca69882011-01-14 09:14:33 +11005114 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005115 mddev->pers->stop(mddev);
5116 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5117 mddev->to_remove = &md_redundancy_group;
5118 module_put(mddev->pers->owner);
5119 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005120 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005121}
NeilBrown390ee602010-06-01 19:37:27 +10005122EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005123
NeilBrownfd01b882011-10-11 16:47:53 +11005124static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005125{
5126 int err = 0;
5127 mutex_lock(&mddev->open_mutex);
5128 if (atomic_read(&mddev->openers) > is_open) {
5129 printk("md: %s still in use.\n",mdname(mddev));
5130 err = -EBUSY;
5131 goto out;
5132 }
5133 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005134 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005135
5136 err = -ENXIO;
5137 if (mddev->ro==1)
5138 goto out;
5139 mddev->ro = 1;
5140 set_disk_ro(mddev->gendisk, 1);
5141 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005142 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005143 err = 0;
5144 }
5145out:
5146 mutex_unlock(&mddev->open_mutex);
5147 return err;
5148}
5149
NeilBrown9e653b62006-06-26 00:27:58 -07005150/* mode:
5151 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005152 * 2 - stop but do not disassemble array
5153 */
NeilBrownfd01b882011-10-11 16:47:53 +11005154static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005157 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
NeilBrownc8c00a62009-08-10 12:50:52 +10005159 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005160 if (atomic_read(&mddev->openers) > is_open ||
5161 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005162 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005163 mutex_unlock(&mddev->open_mutex);
5164 return -EBUSY;
5165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
NeilBrown6e17b022010-08-07 21:41:19 +10005167 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005168 if (mddev->ro)
5169 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005170
NeilBrowndefad612011-01-14 09:14:33 +11005171 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005172 md_stop(mddev);
5173 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005174 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005175
NeilBrowna4bd82d2010-03-29 13:23:10 +11005176 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005177 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005178
NeilBrowndafb20f2012-03-19 12:46:39 +11005179 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005180 if (rdev->raid_disk >= 0)
5181 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005182
NeilBrowna4bd82d2010-03-29 13:23:10 +11005183 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005184 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005185 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005186 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005187
NeilBrowna4bd82d2010-03-29 13:23:10 +11005188 if (mddev->ro)
5189 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005190 } else
5191 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 /*
5193 * Free resources if final stop
5194 */
NeilBrown9e653b62006-06-26 00:27:58 -07005195 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5197
NeilBrown978f9462006-02-02 14:28:05 -08005198 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005199 if (mddev->bitmap_info.file) {
5200 restore_bitmap_write_access(mddev->bitmap_info.file);
5201 fput(mddev->bitmap_info.file);
5202 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005203 }
NeilBrownc3d97142009-12-14 12:49:52 +11005204 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 export_array(mddev);
5207
NeilBrown6177b472010-03-29 11:37:13 +11005208 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005209 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005210 if (mddev->hold_active == UNTIL_STOP)
5211 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005212 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005213 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005214 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005215 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217}
5218
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005219#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005220static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
NeilBrown3cb03002011-10-11 16:45:26 +11005222 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 int err;
5224
NeilBrowna757e642005-04-16 15:26:42 -07005225 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
5228 printk(KERN_INFO "md: running: ");
5229
NeilBrowndafb20f2012-03-19 12:46:39 +11005230 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 char b[BDEVNAME_SIZE];
5232 printk("<%s>", bdevname(rdev->bdev,b));
5233 }
5234 printk("\n");
5235
NeilBrownd710e132008-10-13 11:55:12 +11005236 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 if (err) {
5238 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005239 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 }
5241}
5242
5243/*
5244 * lets try to run arrays based on all disks that have arrived
5245 * until now. (those are in pending_raid_disks)
5246 *
5247 * the method: pick the first pending disk, collect all disks with
5248 * the same UUID, remove all from the pending list and put them into
5249 * the 'same_array' list. Then order this list based on superblock
5250 * update time (freshest comes first), kick out 'old' disks and
5251 * compare superblocks. If everything's fine then run it.
5252 *
5253 * If "unit" is allocated, then bump its reference count
5254 */
5255static void autorun_devices(int part)
5256{
NeilBrown3cb03002011-10-11 16:45:26 +11005257 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005258 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 char b[BDEVNAME_SIZE];
5260
5261 printk(KERN_INFO "md: autorun ...\n");
5262 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005263 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005265 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005267 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
5269 printk(KERN_INFO "md: considering %s ...\n",
5270 bdevname(rdev0->bdev,b));
5271 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005272 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 if (super_90_load(rdev, rdev0, 0) >= 0) {
5274 printk(KERN_INFO "md: adding %s ...\n",
5275 bdevname(rdev->bdev,b));
5276 list_move(&rdev->same_set, &candidates);
5277 }
5278 /*
5279 * now we have a set of devices, with all of them having
5280 * mostly sane superblocks. It's time to allocate the
5281 * mddev.
5282 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005283 if (part) {
5284 dev = MKDEV(mdp_major,
5285 rdev0->preferred_minor << MdpMinorShift);
5286 unit = MINOR(dev) >> MdpMinorShift;
5287 } else {
5288 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5289 unit = MINOR(dev);
5290 }
5291 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5293 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5294 break;
5295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 md_probe(dev, NULL, NULL);
5298 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005299 if (!mddev || !mddev->gendisk) {
5300 if (mddev)
5301 mddev_put(mddev);
5302 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 "md: cannot allocate memory for md drive.\n");
5304 break;
5305 }
5306 if (mddev_lock(mddev))
5307 printk(KERN_WARNING "md: %s locked, cannot run\n",
5308 mdname(mddev));
5309 else if (mddev->raid_disks || mddev->major_version
5310 || !list_empty(&mddev->disks)) {
5311 printk(KERN_WARNING
5312 "md: %s already running, cannot run %s\n",
5313 mdname(mddev), bdevname(rdev0->bdev,b));
5314 mddev_unlock(mddev);
5315 } else {
5316 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005317 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005318 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 list_del_init(&rdev->same_set);
5320 if (bind_rdev_to_array(rdev, mddev))
5321 export_rdev(rdev);
5322 }
5323 autorun_array(mddev);
5324 mddev_unlock(mddev);
5325 }
5326 /* on success, candidates will be empty, on error
5327 * it won't...
5328 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005329 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005330 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 mddev_put(mddev);
5334 }
5335 printk(KERN_INFO "md: ... autorun DONE.\n");
5336}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005337#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339static int get_version(void __user * arg)
5340{
5341 mdu_version_t ver;
5342
5343 ver.major = MD_MAJOR_VERSION;
5344 ver.minor = MD_MINOR_VERSION;
5345 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5346
5347 if (copy_to_user(arg, &ver, sizeof(ver)))
5348 return -EFAULT;
5349
5350 return 0;
5351}
5352
NeilBrownfd01b882011-10-11 16:47:53 +11005353static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
5355 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005356 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005357 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
NeilBrowna9f326e2009-09-23 18:06:41 +10005359 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005360 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005362 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 failed++;
5364 else {
5365 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005366 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005367 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 else
5369 spare++;
5370 }
5371 }
5372
5373 info.major_version = mddev->major_version;
5374 info.minor_version = mddev->minor_version;
5375 info.patch_version = MD_PATCHLEVEL_VERSION;
5376 info.ctime = mddev->ctime;
5377 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005378 info.size = mddev->dev_sectors / 2;
5379 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005380 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 info.nr_disks = nr;
5382 info.raid_disks = mddev->raid_disks;
5383 info.md_minor = mddev->md_minor;
5384 info.not_persistent= !mddev->persistent;
5385
5386 info.utime = mddev->utime;
5387 info.state = 0;
5388 if (mddev->in_sync)
5389 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005390 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005391 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005392 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 info.working_disks = working;
5394 info.failed_disks = failed;
5395 info.spare_disks = spare;
5396
5397 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005398 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
5400 if (copy_to_user(arg, &info, sizeof(info)))
5401 return -EFAULT;
5402
5403 return 0;
5404}
5405
NeilBrownfd01b882011-10-11 16:47:53 +11005406static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005407{
5408 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5409 char *ptr, *buf = NULL;
5410 int err = -ENOMEM;
5411
Dan Williamsb5470dc2008-06-27 21:44:04 -07005412 if (md_allow_write(mddev))
5413 file = kmalloc(sizeof(*file), GFP_NOIO);
5414 else
5415 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005416
NeilBrown32a76272005-06-21 17:17:14 -07005417 if (!file)
5418 goto out;
5419
5420 /* bitmap disabled, zero the first byte and copy out */
5421 if (!mddev->bitmap || !mddev->bitmap->file) {
5422 file->pathname[0] = '\0';
5423 goto copy_out;
5424 }
5425
5426 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5427 if (!buf)
5428 goto out;
5429
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005430 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5431 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005432 goto out;
5433
5434 strcpy(file->pathname, ptr);
5435
5436copy_out:
5437 err = 0;
5438 if (copy_to_user(arg, file, sizeof(*file)))
5439 err = -EFAULT;
5440out:
5441 kfree(buf);
5442 kfree(file);
5443 return err;
5444}
5445
NeilBrownfd01b882011-10-11 16:47:53 +11005446static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
5448 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005449 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 if (copy_from_user(&info, arg, sizeof(info)))
5452 return -EFAULT;
5453
Andre Noll26ef3792008-07-11 22:02:21 +10005454 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 if (rdev) {
5456 info.major = MAJOR(rdev->bdev->bd_dev);
5457 info.minor = MINOR(rdev->bdev->bd_dev);
5458 info.raid_disk = rdev->raid_disk;
5459 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005460 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005462 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 info.state |= (1<<MD_DISK_ACTIVE);
5464 info.state |= (1<<MD_DISK_SYNC);
5465 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005466 if (test_bit(WriteMostly, &rdev->flags))
5467 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 } else {
5469 info.major = info.minor = 0;
5470 info.raid_disk = -1;
5471 info.state = (1<<MD_DISK_REMOVED);
5472 }
5473
5474 if (copy_to_user(arg, &info, sizeof(info)))
5475 return -EFAULT;
5476
5477 return 0;
5478}
5479
NeilBrownfd01b882011-10-11 16:47:53 +11005480static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481{
5482 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005483 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 dev_t dev = MKDEV(info->major,info->minor);
5485
5486 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5487 return -EOVERFLOW;
5488
5489 if (!mddev->raid_disks) {
5490 int err;
5491 /* expecting a device which has a superblock */
5492 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5493 if (IS_ERR(rdev)) {
5494 printk(KERN_WARNING
5495 "md: md_import_device returned %ld\n",
5496 PTR_ERR(rdev));
5497 return PTR_ERR(rdev);
5498 }
5499 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005500 struct md_rdev *rdev0
5501 = list_entry(mddev->disks.next,
5502 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005503 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 .load_super(rdev, rdev0, mddev->minor_version);
5505 if (err < 0) {
5506 printk(KERN_WARNING
5507 "md: %s has different UUID to %s\n",
5508 bdevname(rdev->bdev,b),
5509 bdevname(rdev0->bdev,b2));
5510 export_rdev(rdev);
5511 return -EINVAL;
5512 }
5513 }
5514 err = bind_rdev_to_array(rdev, mddev);
5515 if (err)
5516 export_rdev(rdev);
5517 return err;
5518 }
5519
5520 /*
5521 * add_new_disk can be used once the array is assembled
5522 * to add "hot spares". They must already have a superblock
5523 * written
5524 */
5525 if (mddev->pers) {
5526 int err;
5527 if (!mddev->pers->hot_add_disk) {
5528 printk(KERN_WARNING
5529 "%s: personality does not support diskops!\n",
5530 mdname(mddev));
5531 return -EINVAL;
5532 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005533 if (mddev->persistent)
5534 rdev = md_import_device(dev, mddev->major_version,
5535 mddev->minor_version);
5536 else
5537 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 if (IS_ERR(rdev)) {
5539 printk(KERN_WARNING
5540 "md: md_import_device returned %ld\n",
5541 PTR_ERR(rdev));
5542 return PTR_ERR(rdev);
5543 }
NeilBrown1a855a02010-12-09 16:36:28 +11005544 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005545 if (!mddev->persistent) {
5546 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005547 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005548 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005549 set_bit(In_sync, &rdev->flags);
5550 } else
NeilBrown41158c72005-06-21 17:17:25 -07005551 rdev->raid_disk = -1;
5552 } else
5553 super_types[mddev->major_version].
5554 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005555 if ((info->state & (1<<MD_DISK_SYNC)) &&
5556 (!test_bit(In_sync, &rdev->flags) ||
5557 rdev->raid_disk != info->raid_disk)) {
5558 /* This was a hot-add request, but events doesn't
5559 * match, so reject it.
5560 */
5561 export_rdev(rdev);
5562 return -EINVAL;
5563 }
5564
NeilBrown1a855a02010-12-09 16:36:28 +11005565 if (test_bit(In_sync, &rdev->flags))
5566 rdev->saved_raid_disk = rdev->raid_disk;
5567 else
5568 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005569
NeilBrownb2d444d2005-11-08 21:39:31 -08005570 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005571 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5572 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005573 else
5574 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 rdev->raid_disk = -1;
5577 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005578 if (!err && !mddev->pers->hot_remove_disk) {
5579 /* If there is hot_add_disk but no hot_remove_disk
5580 * then added disks for geometry changes,
5581 * and should be added immediately.
5582 */
5583 super_types[mddev->major_version].
5584 validate_super(mddev, rdev);
5585 err = mddev->pers->hot_add_disk(mddev, rdev);
5586 if (err)
5587 unbind_rdev_from_array(rdev);
5588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 if (err)
5590 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005591 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005592 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005593
NeilBrown17571282006-12-10 02:20:52 -08005594 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005595 if (mddev->degraded)
5596 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005597 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005598 if (!err)
5599 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005600 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 return err;
5602 }
5603
5604 /* otherwise, add_new_disk is only allowed
5605 * for major_version==0 superblocks
5606 */
5607 if (mddev->major_version != 0) {
5608 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5609 mdname(mddev));
5610 return -EINVAL;
5611 }
5612
5613 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5614 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005615 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 if (IS_ERR(rdev)) {
5617 printk(KERN_WARNING
5618 "md: error, md_import_device() returned %ld\n",
5619 PTR_ERR(rdev));
5620 return PTR_ERR(rdev);
5621 }
5622 rdev->desc_nr = info->number;
5623 if (info->raid_disk < mddev->raid_disks)
5624 rdev->raid_disk = info->raid_disk;
5625 else
5626 rdev->raid_disk = -1;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005629 if (info->state & (1<<MD_DISK_SYNC))
5630 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005632 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5633 set_bit(WriteMostly, &rdev->flags);
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 if (!mddev->persistent) {
5636 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005637 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5638 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005639 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005640 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
NeilBrown2bf071b2006-01-06 00:20:55 -08005642 err = bind_rdev_to_array(rdev, mddev);
5643 if (err) {
5644 export_rdev(rdev);
5645 return err;
5646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648
5649 return 0;
5650}
5651
NeilBrownfd01b882011-10-11 16:47:53 +11005652static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653{
5654 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005655 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 rdev = find_rdev(mddev, dev);
5658 if (!rdev)
5659 return -ENXIO;
5660
5661 if (rdev->raid_disk >= 0)
5662 goto busy;
5663
5664 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005665 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005666 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 return 0;
5669busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005670 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 bdevname(rdev->bdev,b), mdname(mddev));
5672 return -EBUSY;
5673}
5674
NeilBrownfd01b882011-10-11 16:47:53 +11005675static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
5677 char b[BDEVNAME_SIZE];
5678 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005679 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
5681 if (!mddev->pers)
5682 return -ENODEV;
5683
5684 if (mddev->major_version != 0) {
5685 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5686 " version-0 superblocks.\n",
5687 mdname(mddev));
5688 return -EINVAL;
5689 }
5690 if (!mddev->pers->hot_add_disk) {
5691 printk(KERN_WARNING
5692 "%s: personality does not support diskops!\n",
5693 mdname(mddev));
5694 return -EINVAL;
5695 }
5696
NeilBrownd710e132008-10-13 11:55:12 +11005697 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (IS_ERR(rdev)) {
5699 printk(KERN_WARNING
5700 "md: error, md_import_device() returned %ld\n",
5701 PTR_ERR(rdev));
5702 return -EINVAL;
5703 }
5704
5705 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005706 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005708 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
NeilBrown8190e752009-06-18 08:48:58 +10005710 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
NeilBrownb2d444d2005-11-08 21:39:31 -08005712 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 printk(KERN_WARNING
5714 "md: can not hot-add faulty %s disk to %s!\n",
5715 bdevname(rdev->bdev,b), mdname(mddev));
5716 err = -EINVAL;
5717 goto abort_export;
5718 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005719 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005721 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005722 err = bind_rdev_to_array(rdev, mddev);
5723 if (err)
5724 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 /*
5727 * The rest should better be atomic, we can have disk failures
5728 * noticed in interrupt contexts ...
5729 */
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 rdev->raid_disk = -1;
5732
NeilBrown850b2b422006-10-03 01:15:46 -07005733 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
5735 /*
5736 * Kick recovery, maybe this spare has to be added to the
5737 * array immediately.
5738 */
5739 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5740 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005741 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 return 0;
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744abort_export:
5745 export_rdev(rdev);
5746 return err;
5747}
5748
NeilBrownfd01b882011-10-11 16:47:53 +11005749static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005750{
5751 int err;
5752
NeilBrown36fa3062005-09-09 16:23:45 -07005753 if (mddev->pers) {
5754 if (!mddev->pers->quiesce)
5755 return -EBUSY;
5756 if (mddev->recovery || mddev->sync_thread)
5757 return -EBUSY;
5758 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005759 }
5760
NeilBrown36fa3062005-09-09 16:23:45 -07005761
5762 if (fd >= 0) {
5763 if (mddev->bitmap)
5764 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005765 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005766
NeilBrownc3d97142009-12-14 12:49:52 +11005767 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005768 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5769 mdname(mddev));
5770 return -EBADF;
5771 }
5772
NeilBrownc3d97142009-12-14 12:49:52 +11005773 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005774 if (err) {
5775 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5776 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005777 fput(mddev->bitmap_info.file);
5778 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005779 return err;
5780 }
NeilBrownc3d97142009-12-14 12:49:52 +11005781 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005782 } else if (mddev->bitmap == NULL)
5783 return -ENOENT; /* cannot remove what isn't there */
5784 err = 0;
5785 if (mddev->pers) {
5786 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005787 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005788 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005789 if (!err)
5790 err = bitmap_load(mddev);
5791 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005792 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005793 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005794 fd = -1; /* make sure to put the file */
5795 }
NeilBrown36fa3062005-09-09 16:23:45 -07005796 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005797 }
5798 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005799 if (mddev->bitmap_info.file) {
5800 restore_bitmap_write_access(mddev->bitmap_info.file);
5801 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005802 }
NeilBrownc3d97142009-12-14 12:49:52 +11005803 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005804 }
5805
NeilBrown32a76272005-06-21 17:17:14 -07005806 return err;
5807}
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809/*
5810 * set_array_info is used two different ways
5811 * The original usage is when creating a new array.
5812 * In this usage, raid_disks is > 0 and it together with
5813 * level, size, not_persistent,layout,chunksize determine the
5814 * shape of the array.
5815 * This will always create an array with a type-0.90.0 superblock.
5816 * The newer usage is when assembling an array.
5817 * In this case raid_disks will be 0, and the major_version field is
5818 * use to determine which style super-blocks are to be found on the devices.
5819 * The minor and patch _version numbers are also kept incase the
5820 * super_block handler wishes to interpret them.
5821 */
NeilBrownfd01b882011-10-11 16:47:53 +11005822static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823{
5824
5825 if (info->raid_disks == 0) {
5826 /* just setting version number for superblock loading */
5827 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005828 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 super_types[info->major_version].name == NULL) {
5830 /* maybe try to auto-load a module? */
5831 printk(KERN_INFO
5832 "md: superblock version %d not known\n",
5833 info->major_version);
5834 return -EINVAL;
5835 }
5836 mddev->major_version = info->major_version;
5837 mddev->minor_version = info->minor_version;
5838 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005839 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005840 /* ensure mddev_put doesn't delete this now that there
5841 * is some minimal configuration.
5842 */
5843 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 return 0;
5845 }
5846 mddev->major_version = MD_MAJOR_VERSION;
5847 mddev->minor_version = MD_MINOR_VERSION;
5848 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5849 mddev->ctime = get_seconds();
5850
5851 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005852 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005853 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 mddev->raid_disks = info->raid_disks;
5855 /* don't set md_minor, it is determined by which /dev/md* was
5856 * openned
5857 */
5858 if (info->state & (1<<MD_SB_CLEAN))
5859 mddev->recovery_cp = MaxSector;
5860 else
5861 mddev->recovery_cp = 0;
5862 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005863 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005866 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 mddev->max_disks = MD_SB_DISKS;
5869
NeilBrowne6910632008-02-06 01:39:51 -08005870 if (mddev->persistent)
5871 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005872 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
NeilBrownc3d97142009-12-14 12:49:52 +11005874 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5875 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005876
NeilBrownf6705572006-03-27 01:18:11 -08005877 mddev->reshape_position = MaxSector;
5878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 /*
5880 * Generate a 128 bit UUID
5881 */
5882 get_random_bytes(mddev->uuid, 16);
5883
NeilBrownf6705572006-03-27 01:18:11 -08005884 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005885 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005886 mddev->new_layout = mddev->layout;
5887 mddev->delta_disks = 0;
5888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 return 0;
5890}
5891
NeilBrownfd01b882011-10-11 16:47:53 +11005892void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005893{
Dan Williamsb522adc2009-03-31 15:00:31 +11005894 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5895
5896 if (mddev->external_size)
5897 return;
5898
Dan Williams1f403622009-03-31 14:59:03 +11005899 mddev->array_sectors = array_sectors;
5900}
5901EXPORT_SYMBOL(md_set_array_sectors);
5902
NeilBrownfd01b882011-10-11 16:47:53 +11005903static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005904{
NeilBrown3cb03002011-10-11 16:45:26 +11005905 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005906 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005907 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005908
5909 if (mddev->pers->resize == NULL)
5910 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005911 /* The "num_sectors" is the number of sectors of each device that
5912 * is used. This can only make sense for arrays with redundancy.
5913 * linear and raid0 always use whatever space is available. We can only
5914 * consider changing this number if no resync or reconstruction is
5915 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005916 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005917 * of each device. If num_sectors is zero, we find the largest size
5918 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005919 */
5920 if (mddev->sync_thread)
5921 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005922 if (mddev->bitmap)
5923 /* Sorry, cannot grow a bitmap yet, just remove it,
5924 * grow, and re-add.
5925 */
5926 return -EBUSY;
NeilBrowndafb20f2012-03-19 12:46:39 +11005927 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005928 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005929
Andre Nolld71f9f82008-07-11 22:02:22 +10005930 if (fit && (num_sectors == 0 || num_sectors > avail))
5931 num_sectors = avail;
5932 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005933 return -ENOSPC;
5934 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005935 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005936 if (!rv)
5937 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005938 return rv;
5939}
5940
NeilBrownfd01b882011-10-11 16:47:53 +11005941static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08005942{
5943 int rv;
5944 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005945 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005946 return -EINVAL;
5947 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005948 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005949 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005950 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005951 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005952 mddev->delta_disks = raid_disks - mddev->raid_disks;
5953
5954 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005955 if (rv < 0)
5956 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005957 return rv;
5958}
5959
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961/*
5962 * update_array_info is used to change the configuration of an
5963 * on-line array.
5964 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5965 * fields in the info are checked against the array.
5966 * Any differences that cannot be handled will cause an error.
5967 * Normally, only one change can be managed at a time.
5968 */
NeilBrownfd01b882011-10-11 16:47:53 +11005969static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970{
5971 int rv = 0;
5972 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005973 int state = 0;
5974
5975 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005976 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005977 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978
5979 if (mddev->major_version != info->major_version ||
5980 mddev->minor_version != info->minor_version ||
5981/* mddev->patch_version != info->patch_version || */
5982 mddev->ctime != info->ctime ||
5983 mddev->level != info->level ||
5984/* mddev->layout != info->layout || */
5985 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005986 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005987 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5988 ((state^info->state) & 0xfffffe00)
5989 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return -EINVAL;
5991 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005992 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5993 cnt++;
5994 if (mddev->raid_disks != info->raid_disks)
5995 cnt++;
5996 if (mddev->layout != info->layout)
5997 cnt++;
5998 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5999 cnt++;
6000 if (cnt == 0)
6001 return 0;
6002 if (cnt > 1)
6003 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
6005 if (mddev->layout != info->layout) {
6006 /* Change layout
6007 * we don't need to do anything at the md level, the
6008 * personality will take care of it all.
6009 */
NeilBrown50ac1682009-06-18 08:47:55 +10006010 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006012 else {
6013 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006014 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006015 if (rv)
6016 mddev->new_layout = mddev->layout;
6017 return rv;
6018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006020 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006021 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
NeilBrownda943b992006-01-06 00:20:54 -08006023 if (mddev->raid_disks != info->raid_disks)
6024 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
NeilBrown36fa3062005-09-09 16:23:45 -07006026 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6027 if (mddev->pers->quiesce == NULL)
6028 return -EINVAL;
6029 if (mddev->recovery || mddev->sync_thread)
6030 return -EBUSY;
6031 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6032 /* add the bitmap */
6033 if (mddev->bitmap)
6034 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006035 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006036 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006037 mddev->bitmap_info.offset =
6038 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07006039 mddev->pers->quiesce(mddev, 1);
6040 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006041 if (!rv)
6042 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006043 if (rv)
6044 bitmap_destroy(mddev);
6045 mddev->pers->quiesce(mddev, 0);
6046 } else {
6047 /* remove the bitmap */
6048 if (!mddev->bitmap)
6049 return -ENOENT;
6050 if (mddev->bitmap->file)
6051 return -EINVAL;
6052 mddev->pers->quiesce(mddev, 1);
6053 bitmap_destroy(mddev);
6054 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006055 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006056 }
6057 }
NeilBrown850b2b422006-10-03 01:15:46 -07006058 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return rv;
6060}
6061
NeilBrownfd01b882011-10-11 16:47:53 +11006062static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
NeilBrown3cb03002011-10-11 16:45:26 +11006064 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
6066 if (mddev->pers == NULL)
6067 return -ENODEV;
6068
6069 rdev = find_rdev(mddev, dev);
6070 if (!rdev)
6071 return -ENODEV;
6072
6073 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006074 if (!test_bit(Faulty, &rdev->flags))
6075 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 return 0;
6077}
6078
Andre Noll2f9618c2008-04-25 18:57:58 +02006079/*
6080 * We have a problem here : there is no easy way to give a CHS
6081 * virtual geometry. We currently pretend that we have a 2 heads
6082 * 4 sectors (with a BIG number of cylinders...). This drives
6083 * dosfs just mad... ;-)
6084 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006085static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6086{
NeilBrownfd01b882011-10-11 16:47:53 +11006087 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006088
6089 geo->heads = 2;
6090 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006091 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006092 return 0;
6093}
6094
Al Viroa39907f2008-03-02 10:31:15 -05006095static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 unsigned int cmd, unsigned long arg)
6097{
6098 int err = 0;
6099 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006100 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006101 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
NeilBrown506c9e42011-12-23 10:17:26 +11006103 switch (cmd) {
6104 case RAID_VERSION:
6105 case GET_ARRAY_INFO:
6106 case GET_DISK_INFO:
6107 break;
6108 default:
6109 if (!capable(CAP_SYS_ADMIN))
6110 return -EACCES;
6111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 /*
6114 * Commands dealing with the RAID driver but not any
6115 * particular array:
6116 */
6117 switch (cmd)
6118 {
6119 case RAID_VERSION:
6120 err = get_version(argp);
6121 goto done;
6122
6123 case PRINT_RAID_DEBUG:
6124 err = 0;
6125 md_print_devices();
6126 goto done;
6127
6128#ifndef MODULE
6129 case RAID_AUTORUN:
6130 err = 0;
6131 autostart_arrays(arg);
6132 goto done;
6133#endif
6134 default:;
6135 }
6136
6137 /*
6138 * Commands creating/starting a new array:
6139 */
6140
Al Viroa39907f2008-03-02 10:31:15 -05006141 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
6143 if (!mddev) {
6144 BUG();
6145 goto abort;
6146 }
6147
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 err = mddev_lock(mddev);
6149 if (err) {
6150 printk(KERN_INFO
6151 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6152 err, cmd);
6153 goto abort;
6154 }
6155
6156 switch (cmd)
6157 {
6158 case SET_ARRAY_INFO:
6159 {
6160 mdu_array_info_t info;
6161 if (!arg)
6162 memset(&info, 0, sizeof(info));
6163 else if (copy_from_user(&info, argp, sizeof(info))) {
6164 err = -EFAULT;
6165 goto abort_unlock;
6166 }
6167 if (mddev->pers) {
6168 err = update_array_info(mddev, &info);
6169 if (err) {
6170 printk(KERN_WARNING "md: couldn't update"
6171 " array info. %d\n", err);
6172 goto abort_unlock;
6173 }
6174 goto done_unlock;
6175 }
6176 if (!list_empty(&mddev->disks)) {
6177 printk(KERN_WARNING
6178 "md: array %s already has disks!\n",
6179 mdname(mddev));
6180 err = -EBUSY;
6181 goto abort_unlock;
6182 }
6183 if (mddev->raid_disks) {
6184 printk(KERN_WARNING
6185 "md: array %s already initialised!\n",
6186 mdname(mddev));
6187 err = -EBUSY;
6188 goto abort_unlock;
6189 }
6190 err = set_array_info(mddev, &info);
6191 if (err) {
6192 printk(KERN_WARNING "md: couldn't set"
6193 " array info. %d\n", err);
6194 goto abort_unlock;
6195 }
6196 }
6197 goto done_unlock;
6198
6199 default:;
6200 }
6201
6202 /*
6203 * Commands querying/configuring an existing array:
6204 */
NeilBrown32a76272005-06-21 17:17:14 -07006205 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006206 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006207 if ((!mddev->raid_disks && !mddev->external)
6208 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6209 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6210 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 err = -ENODEV;
6212 goto abort_unlock;
6213 }
6214
6215 /*
6216 * Commands even a read-only array can execute:
6217 */
6218 switch (cmd)
6219 {
6220 case GET_ARRAY_INFO:
6221 err = get_array_info(mddev, argp);
6222 goto done_unlock;
6223
NeilBrown32a76272005-06-21 17:17:14 -07006224 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006225 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006226 goto done_unlock;
6227
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 case GET_DISK_INFO:
6229 err = get_disk_info(mddev, argp);
6230 goto done_unlock;
6231
6232 case RESTART_ARRAY_RW:
6233 err = restart_array(mddev);
6234 goto done_unlock;
6235
6236 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006237 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 goto done_unlock;
6239
6240 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006241 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 goto done_unlock;
6243
Dan Williamse2218352010-05-12 08:25:37 +10006244 case BLKROSET:
6245 if (get_user(ro, (int __user *)(arg))) {
6246 err = -EFAULT;
6247 goto done_unlock;
6248 }
6249 err = -EINVAL;
6250
6251 /* if the bdev is going readonly the value of mddev->ro
6252 * does not matter, no writes are coming
6253 */
6254 if (ro)
6255 goto done_unlock;
6256
6257 /* are we are already prepared for writes? */
6258 if (mddev->ro != 1)
6259 goto done_unlock;
6260
6261 /* transitioning to readauto need only happen for
6262 * arrays that call md_write_start
6263 */
6264 if (mddev->pers) {
6265 err = restart_array(mddev);
6266 if (err == 0) {
6267 mddev->ro = 2;
6268 set_disk_ro(mddev->gendisk, 0);
6269 }
6270 }
6271 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 }
6273
6274 /*
6275 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006276 * superblock, so we do not allow them on read-only arrays.
6277 * However non-MD ioctls (e.g. get-size) will still come through
6278 * here and hit the 'default' below, so only disallow
6279 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006281 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006282 if (mddev->ro == 2) {
6283 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006284 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006285 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6286 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006287 } else {
6288 err = -EROFS;
6289 goto abort_unlock;
6290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 }
6292
6293 switch (cmd)
6294 {
6295 case ADD_NEW_DISK:
6296 {
6297 mdu_disk_info_t info;
6298 if (copy_from_user(&info, argp, sizeof(info)))
6299 err = -EFAULT;
6300 else
6301 err = add_new_disk(mddev, &info);
6302 goto done_unlock;
6303 }
6304
6305 case HOT_REMOVE_DISK:
6306 err = hot_remove_disk(mddev, new_decode_dev(arg));
6307 goto done_unlock;
6308
6309 case HOT_ADD_DISK:
6310 err = hot_add_disk(mddev, new_decode_dev(arg));
6311 goto done_unlock;
6312
6313 case SET_DISK_FAULTY:
6314 err = set_disk_faulty(mddev, new_decode_dev(arg));
6315 goto done_unlock;
6316
6317 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006318 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 goto done_unlock;
6320
NeilBrown32a76272005-06-21 17:17:14 -07006321 case SET_BITMAP_FILE:
6322 err = set_bitmap_file(mddev, (int)arg);
6323 goto done_unlock;
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 err = -EINVAL;
6327 goto abort_unlock;
6328 }
6329
6330done_unlock:
6331abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006332 if (mddev->hold_active == UNTIL_IOCTL &&
6333 err != -EINVAL)
6334 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 mddev_unlock(mddev);
6336
6337 return err;
6338done:
6339 if (err)
6340 MD_BUG();
6341abort:
6342 return err;
6343}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006344#ifdef CONFIG_COMPAT
6345static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6346 unsigned int cmd, unsigned long arg)
6347{
6348 switch (cmd) {
6349 case HOT_REMOVE_DISK:
6350 case HOT_ADD_DISK:
6351 case SET_DISK_FAULTY:
6352 case SET_BITMAP_FILE:
6353 /* These take in integer arg, do not convert */
6354 break;
6355 default:
6356 arg = (unsigned long)compat_ptr(arg);
6357 break;
6358 }
6359
6360 return md_ioctl(bdev, mode, cmd, arg);
6361}
6362#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
Al Viroa39907f2008-03-02 10:31:15 -05006364static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365{
6366 /*
6367 * Succeed if we can lock the mddev, which confirms that
6368 * it isn't being stopped right now.
6369 */
NeilBrownfd01b882011-10-11 16:47:53 +11006370 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 int err;
6372
NeilBrownd3374822009-01-09 08:31:10 +11006373 if (mddev->gendisk != bdev->bd_disk) {
6374 /* we are racing with mddev_put which is discarding this
6375 * bd_disk.
6376 */
6377 mddev_put(mddev);
6378 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006379 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006380 /* Then retry the open from the top */
6381 return -ERESTARTSYS;
6382 }
6383 BUG_ON(mddev != bdev->bd_disk->private_data);
6384
NeilBrownc8c00a62009-08-10 12:50:52 +10006385 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 goto out;
6387
6388 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006389 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006390 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006392 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 out:
6394 return err;
6395}
6396
Al Viroa39907f2008-03-02 10:31:15 -05006397static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398{
NeilBrownfd01b882011-10-11 16:47:53 +11006399 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006401 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006402 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 mddev_put(mddev);
6404
6405 return 0;
6406}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006407
6408static int md_media_changed(struct gendisk *disk)
6409{
NeilBrownfd01b882011-10-11 16:47:53 +11006410 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006411
6412 return mddev->changed;
6413}
6414
6415static int md_revalidate(struct gendisk *disk)
6416{
NeilBrownfd01b882011-10-11 16:47:53 +11006417 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006418
6419 mddev->changed = 0;
6420 return 0;
6421}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006422static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423{
6424 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006425 .open = md_open,
6426 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006427 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006428#ifdef CONFIG_COMPAT
6429 .compat_ioctl = md_compat_ioctl,
6430#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006431 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006432 .media_changed = md_media_changed,
6433 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434};
6435
Adrian Bunk75c96f82005-05-05 16:16:09 -07006436static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437{
NeilBrown2b8bf342011-10-11 16:48:23 +11006438 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /*
6441 * md_thread is a 'system-thread', it's priority should be very
6442 * high. We avoid resource deadlocks individually in each
6443 * raid personality. (RAID5 does preallocation) We also use RR and
6444 * the very same RT priority as kswapd, thus we will never get
6445 * into a priority inversion deadlock.
6446 *
6447 * we definitely have to have equal or higher priority than
6448 * bdflush, otherwise bdflush will deadlock if there are too
6449 * many dirty RAID5 blocks.
6450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
NeilBrown6985c432005-10-19 21:23:47 -07006452 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006453 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
NeilBrown93588e22005-11-15 00:09:12 -08006455 /* We need to wait INTERRUPTIBLE so that
6456 * we don't add to the load-average.
6457 * That means we need to be sure no signals are
6458 * pending
6459 */
6460 if (signal_pending(current))
6461 flush_signals(current);
6462
6463 wait_event_interruptible_timeout
6464 (thread->wqueue,
6465 test_bit(THREAD_WAKEUP, &thread->flags)
6466 || kthread_should_stop(),
6467 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
NeilBrown6c987912011-01-14 09:13:53 +11006469 clear_bit(THREAD_WAKEUP, &thread->flags);
6470 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006471 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 return 0;
6475}
6476
NeilBrown2b8bf342011-10-11 16:48:23 +11006477void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478{
6479 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006480 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 set_bit(THREAD_WAKEUP, &thread->flags);
6482 wake_up(&thread->wqueue);
6483 }
6484}
6485
NeilBrown2b8bf342011-10-11 16:48:23 +11006486struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 const char *name)
6488{
NeilBrown2b8bf342011-10-11 16:48:23 +11006489 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
NeilBrown2b8bf342011-10-11 16:48:23 +11006491 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 if (!thread)
6493 return NULL;
6494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 init_waitqueue_head(&thread->wqueue);
6496
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 thread->run = run;
6498 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006499 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006500 thread->tsk = kthread_run(md_thread, thread,
6501 "%s_%s",
6502 mdname(thread->mddev),
6503 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006504 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 kfree(thread);
6506 return NULL;
6507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 return thread;
6509}
6510
NeilBrown2b8bf342011-10-11 16:48:23 +11006511void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512{
NeilBrown2b8bf342011-10-11 16:48:23 +11006513 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006514 if (!thread)
6515 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006516 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006517 /* Locking ensures that mddev_unlock does not wake_up a
6518 * non-existent thread
6519 */
6520 spin_lock(&pers_lock);
6521 *threadp = NULL;
6522 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006523
6524 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 kfree(thread);
6526}
6527
NeilBrownfd01b882011-10-11 16:47:53 +11006528void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529{
6530 if (!mddev) {
6531 MD_BUG();
6532 return;
6533 }
6534
NeilBrownb2d444d2005-11-08 21:39:31 -08006535 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006537
NeilBrownde393cd2011-07-28 11:31:48 +10006538 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 return;
6540 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006541 if (mddev->degraded)
6542 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006543 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6545 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6546 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006547 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006548 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006549 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551
6552/* seq_file implementation /proc/mdstat */
6553
6554static void status_unused(struct seq_file *seq)
6555{
6556 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006557 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
6559 seq_printf(seq, "unused devices: ");
6560
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006561 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 char b[BDEVNAME_SIZE];
6563 i++;
6564 seq_printf(seq, "%s ",
6565 bdevname(rdev->bdev,b));
6566 }
6567 if (!i)
6568 seq_printf(seq, "<none>");
6569
6570 seq_printf(seq, "\n");
6571}
6572
6573
NeilBrownfd01b882011-10-11 16:47:53 +11006574static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575{
NeilBrowndd71cf62009-05-07 12:49:35 +10006576 sector_t max_sectors, resync, res;
6577 unsigned long dt, db;
6578 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006579 int scale;
6580 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
NeilBrowndd71cf62009-05-07 12:49:35 +10006582 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
6584 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006585 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006587 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 /*
6590 * Should not happen.
6591 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006592 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 MD_BUG();
6594 return;
6595 }
NeilBrown4588b422006-03-27 01:18:04 -08006596 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006597 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006598 * u32, as those are the requirements for sector_div.
6599 * Thus 'scale' must be at least 10
6600 */
6601 scale = 10;
6602 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006603 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006604 scale++;
6605 }
6606 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006607 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006608
6609 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 {
NeilBrown4588b422006-03-27 01:18:04 -08006611 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 seq_printf(seq, "[");
6613 for (i = 0; i < x; i++)
6614 seq_printf(seq, "=");
6615 seq_printf(seq, ">");
6616 for (i = 0; i < y; i++)
6617 seq_printf(seq, ".");
6618 seq_printf(seq, "] ");
6619 }
NeilBrown4588b422006-03-27 01:18:04 -08006620 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006621 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6622 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006623 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6624 "check" :
6625 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6626 "resync" : "recovery"))),
6627 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006628 (unsigned long long) resync/2,
6629 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
6631 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 * dt: time from mark until now
6633 * db: blocks written from mark until now
6634 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006635 *
6636 * rt is a sector_t, so could be 32bit or 64bit.
6637 * So we divide before multiply in case it is 32bit and close
6638 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006639 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006640 * near the end of resync when the number of remaining sectors
6641 * is close to 'db'.
6642 * We then divide rt by 32 after multiplying by db to compensate.
6643 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 */
6645 dt = ((jiffies - mddev->resync_mark) / HZ);
6646 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006647 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6648 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649
NeilBrowndd71cf62009-05-07 12:49:35 +10006650 rt = max_sectors - resync; /* number of remaining sectors */
6651 sector_div(rt, db/32+1);
6652 rt *= dt;
6653 rt >>= 5;
6654
6655 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6656 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
NeilBrownff4e8d92006-07-10 04:44:16 -07006658 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659}
6660
6661static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6662{
6663 struct list_head *tmp;
6664 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006665 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
6667 if (l >= 0x10000)
6668 return NULL;
6669 if (!l--)
6670 /* header */
6671 return (void*)1;
6672
6673 spin_lock(&all_mddevs_lock);
6674 list_for_each(tmp,&all_mddevs)
6675 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006676 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 mddev_get(mddev);
6678 spin_unlock(&all_mddevs_lock);
6679 return mddev;
6680 }
6681 spin_unlock(&all_mddevs_lock);
6682 if (!l--)
6683 return (void*)2;/* tail */
6684 return NULL;
6685}
6686
6687static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6688{
6689 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006690 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
6692 ++*pos;
6693 if (v == (void*)2)
6694 return NULL;
6695
6696 spin_lock(&all_mddevs_lock);
6697 if (v == (void*)1)
6698 tmp = all_mddevs.next;
6699 else
6700 tmp = mddev->all_mddevs.next;
6701 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006702 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 else {
6704 next_mddev = (void*)2;
6705 *pos = 0x10000;
6706 }
6707 spin_unlock(&all_mddevs_lock);
6708
6709 if (v != (void*)1)
6710 mddev_put(mddev);
6711 return next_mddev;
6712
6713}
6714
6715static void md_seq_stop(struct seq_file *seq, void *v)
6716{
NeilBrownfd01b882011-10-11 16:47:53 +11006717 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
6719 if (mddev && v != (void*)1 && v != (void*)2)
6720 mddev_put(mddev);
6721}
6722
6723static int md_seq_show(struct seq_file *seq, void *v)
6724{
NeilBrownfd01b882011-10-11 16:47:53 +11006725 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006726 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006727 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006728 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
6730 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006731 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 seq_printf(seq, "Personalities : ");
6733 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006734 list_for_each_entry(pers, &pers_list, list)
6735 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
6737 spin_unlock(&pers_lock);
6738 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006739 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 return 0;
6741 }
6742 if (v == (void*)2) {
6743 status_unused(seq);
6744 return 0;
6745 }
6746
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006747 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6751 seq_printf(seq, "%s : %sactive", mdname(mddev),
6752 mddev->pers ? "" : "in");
6753 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006754 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006756 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006757 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 seq_printf(seq, " %s", mddev->pers->name);
6759 }
6760
Andre Nolldd8ac332009-03-31 14:33:13 +11006761 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006762 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 char b[BDEVNAME_SIZE];
6764 seq_printf(seq, " %s[%d]",
6765 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006766 if (test_bit(WriteMostly, &rdev->flags))
6767 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006768 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 seq_printf(seq, "(F)");
6770 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006771 }
6772 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006773 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006774 if (test_bit(Replacement, &rdev->flags))
6775 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006776 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 }
6778
6779 if (!list_empty(&mddev->disks)) {
6780 if (mddev->pers)
6781 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006782 (unsigned long long)
6783 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 else
6785 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006786 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006788 if (mddev->persistent) {
6789 if (mddev->major_version != 0 ||
6790 mddev->minor_version != 90) {
6791 seq_printf(seq," super %d.%d",
6792 mddev->major_version,
6793 mddev->minor_version);
6794 }
NeilBrowne6910632008-02-06 01:39:51 -08006795 } else if (mddev->external)
6796 seq_printf(seq, " super external:%s",
6797 mddev->metadata_type);
6798 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006799 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006802 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006804 if (mddev->pers->sync_request) {
6805 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006806 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006807 seq_printf(seq, "\n ");
6808 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6809 seq_printf(seq, "\tresync=DELAYED\n ");
6810 else if (mddev->recovery_cp < MaxSector)
6811 seq_printf(seq, "\tresync=PENDING\n ");
6812 }
NeilBrown32a76272005-06-21 17:17:14 -07006813 } else
6814 seq_printf(seq, "\n ");
6815
6816 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006817 unsigned long chunk_kb;
6818 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006819 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006820 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006821 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6822 "%lu%s chunk",
6823 bitmap->pages - bitmap->missing_pages,
6824 bitmap->pages,
6825 (bitmap->pages - bitmap->missing_pages)
6826 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006827 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006828 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006829 if (bitmap->file) {
6830 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006831 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006832 }
NeilBrown78d742d2005-06-21 17:17:15 -07006833
NeilBrown32a76272005-06-21 17:17:14 -07006834 seq_printf(seq, "\n");
6835 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 }
6837
6838 seq_printf(seq, "\n");
6839 }
6840 mddev_unlock(mddev);
6841
6842 return 0;
6843}
6844
Jan Engelhardt110518b2009-05-07 12:49:37 +10006845static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 .start = md_seq_start,
6847 .next = md_seq_next,
6848 .stop = md_seq_stop,
6849 .show = md_seq_show,
6850};
6851
6852static int md_seq_open(struct inode *inode, struct file *file)
6853{
Kay Sieversf1514632011-07-12 20:48:39 +02006854 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 int error;
6856
6857 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006858 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006859 return error;
6860
6861 seq = file->private_data;
6862 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 return error;
6864}
6865
NeilBrownd7603b72006-01-06 00:20:30 -08006866static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6867{
Kay Sieversf1514632011-07-12 20:48:39 +02006868 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006869 int mask;
6870
6871 poll_wait(filp, &md_event_waiters, wait);
6872
6873 /* always allow read */
6874 mask = POLLIN | POLLRDNORM;
6875
Kay Sieversf1514632011-07-12 20:48:39 +02006876 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006877 mask |= POLLERR | POLLPRI;
6878 return mask;
6879}
6880
Arjan van de Venfa027c22007-02-12 00:55:33 -08006881static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006882 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 .open = md_seq_open,
6884 .read = seq_read,
6885 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006886 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006887 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888};
6889
NeilBrown84fc4b52011-10-11 16:49:58 +11006890int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006893 list_add_tail(&p->list, &pers_list);
6894 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 spin_unlock(&pers_lock);
6896 return 0;
6897}
6898
NeilBrown84fc4b52011-10-11 16:49:58 +11006899int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900{
NeilBrown2604b702006-01-06 00:20:36 -08006901 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006903 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 spin_unlock(&pers_lock);
6905 return 0;
6906}
6907
NeilBrownfd01b882011-10-11 16:47:53 +11006908static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909{
NeilBrown3cb03002011-10-11 16:45:26 +11006910 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006912 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
6914 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006915 rcu_read_lock();
6916 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006918 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6919 (int)part_stat_read(&disk->part0, sectors[1]) -
6920 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006921 /* sync IO will cause sync_io to increase before the disk_stats
6922 * as sync_io is counted when a request starts, and
6923 * disk_stats is counted when it completes.
6924 * So resync activity will cause curr_events to be smaller than
6925 * when there was no such activity.
6926 * non-sync IO will cause disk_stat to increase without
6927 * increasing sync_io so curr_events will (eventually)
6928 * be larger than it was before. Once it becomes
6929 * substantially larger, the test below will cause
6930 * the array to appear non-idle, and resync will slow
6931 * down.
6932 * If there is a lot of outstanding resync activity when
6933 * we set last_event to curr_events, then all that activity
6934 * completing might cause the array to appear non-idle
6935 * and resync will be slowed down even though there might
6936 * not have been non-resync activity. This will only
6937 * happen once though. 'last_events' will soon reflect
6938 * the state where there is little or no outstanding
6939 * resync requests, and further resync activity will
6940 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006941 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006943 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 rdev->last_events = curr_events;
6945 idle = 0;
6946 }
6947 }
NeilBrown4b809912008-07-21 17:05:25 +10006948 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 return idle;
6950}
6951
NeilBrownfd01b882011-10-11 16:47:53 +11006952void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953{
6954 /* another "blocks" (512byte) blocks have been synced */
6955 atomic_sub(blocks, &mddev->recovery_active);
6956 wake_up(&mddev->recovery_wait);
6957 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006958 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 md_wakeup_thread(mddev->thread);
6960 // stop recovery, signal do_sync ....
6961 }
6962}
6963
6964
NeilBrown06d91a52005-06-21 17:17:12 -07006965/* md_write_start(mddev, bi)
6966 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006967 * in superblock) before writing, schedule a superblock update
6968 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006969 */
NeilBrownfd01b882011-10-11 16:47:53 +11006970void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971{
Neil Brown0fd62b82008-06-28 08:31:36 +10006972 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006973 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006974 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006975
NeilBrownf91de922005-11-08 21:39:36 -08006976 BUG_ON(mddev->ro == 1);
6977 if (mddev->ro == 2) {
6978 /* need to switch to read/write */
6979 mddev->ro = 0;
6980 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6981 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006982 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006983 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006984 }
NeilBrown06d91a52005-06-21 17:17:12 -07006985 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006986 if (mddev->safemode == 1)
6987 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006988 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006989 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006990 if (mddev->in_sync) {
6991 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006992 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006993 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006994 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006995 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006996 }
NeilBrowna9701a32005-11-08 21:39:34 -08006997 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006998 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006999 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007000 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007001 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007002 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003}
7004
NeilBrownfd01b882011-10-11 16:47:53 +11007005void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006{
7007 if (atomic_dec_and_test(&mddev->writes_pending)) {
7008 if (mddev->safemode == 2)
7009 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007010 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7012 }
7013}
7014
NeilBrown2a2275d2007-01-26 00:57:11 -08007015/* md_allow_write(mddev)
7016 * Calling this ensures that the array is marked 'active' so that writes
7017 * may proceed without blocking. It is important to call this before
7018 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7019 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007020 *
7021 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7022 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007023 */
NeilBrownfd01b882011-10-11 16:47:53 +11007024int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007025{
7026 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007027 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007028 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007029 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007030 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007031 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007032
7033 spin_lock_irq(&mddev->write_lock);
7034 if (mddev->in_sync) {
7035 mddev->in_sync = 0;
7036 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007037 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007038 if (mddev->safemode_delay &&
7039 mddev->safemode == 0)
7040 mddev->safemode = 1;
7041 spin_unlock_irq(&mddev->write_lock);
7042 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007043 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007044 } else
7045 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007046
NeilBrown070dc6d2010-08-30 17:33:34 +10007047 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007048 return -EAGAIN;
7049 else
7050 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007051}
7052EXPORT_SYMBOL_GPL(md_allow_write);
7053
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054#define SYNC_MARKS 10
7055#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007056void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057{
NeilBrownfd01b882011-10-11 16:47:53 +11007058 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 unsigned int currspeed = 0,
7060 window;
NeilBrown57afd892005-06-21 17:17:13 -07007061 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 unsigned long mark[SYNC_MARKS];
7063 sector_t mark_cnt[SYNC_MARKS];
7064 int last_mark,m;
7065 struct list_head *tmp;
7066 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007067 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007068 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007069 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
7071 /* just incase thread restarts... */
7072 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7073 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007074 if (mddev->ro) /* never try to sync a read-only array */
7075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
NeilBrown61df9d92006-10-03 01:15:57 -07007077 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7078 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7079 desc = "data-check";
7080 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7081 desc = "requested-resync";
7082 else
7083 desc = "resync";
7084 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7085 desc = "reshape";
7086 else
7087 desc = "recovery";
7088
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 /* we overload curr_resync somewhat here.
7090 * 0 == not engaged in resync at all
7091 * 2 == checking that there is no conflict with another sync
7092 * 1 == like 2, but have yielded to allow conflicting resync to
7093 * commense
7094 * other == active in resync - this many blocks
7095 *
7096 * Before starting a resync we must have set curr_resync to
7097 * 2, and then checked that every "conflicting" array has curr_resync
7098 * less than ours. When we find one that is the same or higher
7099 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7100 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7101 * This will mean we have to start checking from the beginning again.
7102 *
7103 */
7104
7105 do {
7106 mddev->curr_resync = 2;
7107
7108 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007109 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007110 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007111
7112 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007114 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 if (mddev2 == mddev)
7116 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007117 if (!mddev->parallel_resync
7118 && mddev2->curr_resync
7119 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 DEFINE_WAIT(wq);
7121 if (mddev < mddev2 && mddev->curr_resync == 2) {
7122 /* arbitrarily yield */
7123 mddev->curr_resync = 1;
7124 wake_up(&resync_wait);
7125 }
7126 if (mddev > mddev2 && mddev->curr_resync == 1)
7127 /* no need to wait here, we can wait the next
7128 * time 'round when curr_resync == 2
7129 */
7130 continue;
NeilBrown97441972008-09-19 11:49:54 +10007131 /* We need to wait 'interruptible' so as not to
7132 * contribute to the load average, and not to
7133 * be caught by 'softlockup'
7134 */
7135 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007136 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007137 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007138 printk(KERN_INFO "md: delaying %s of %s"
7139 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007141 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007143 if (signal_pending(current))
7144 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 schedule();
7146 finish_wait(&resync_wait, &wq);
7147 goto try_again;
7148 }
7149 finish_wait(&resync_wait, &wq);
7150 }
7151 }
7152 } while (mddev->curr_resync < 2);
7153
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007154 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007155 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007157 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 */
7159 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007160 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007161 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007162 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7163 j = mddev->resync_min;
7164 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007165 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007166
NeilBrownccfcc3c2006-03-27 01:18:09 -08007167 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007168 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007169 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007171 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007172 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007173 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007174 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007175 if (rdev->raid_disk >= 0 &&
7176 !test_bit(Faulty, &rdev->flags) &&
7177 !test_bit(In_sync, &rdev->flags) &&
7178 rdev->recovery_offset < j)
7179 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007180 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
NeilBrown61df9d92006-10-03 01:15:57 -07007183 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7184 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7185 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007186 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007187 "(but not more than %d KB/sec) for %s.\n",
7188 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189
NeilBrowneea1bf32009-03-31 14:27:02 +11007190 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007191
NeilBrown57afd892005-06-21 17:17:13 -07007192 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 for (m = 0; m < SYNC_MARKS; m++) {
7194 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007195 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 }
7197 last_mark = 0;
7198 mddev->resync_mark = mark[last_mark];
7199 mddev->resync_mark_cnt = mark_cnt[last_mark];
7200
7201 /*
7202 * Tune reconstruction:
7203 */
7204 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007205 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7206 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207
7208 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 last_check = 0;
7210
7211 if (j>2) {
7212 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007213 "md: resuming %s of %s from checkpoint.\n",
7214 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 mddev->curr_resync = j;
7216 }
NeilBrown75d3da42011-01-14 09:14:34 +11007217 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
7219 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007220 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
NeilBrown57afd892005-06-21 17:17:13 -07007222 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007223
NeilBrown7a91ee12009-05-26 12:57:21 +10007224 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7225 ((mddev->curr_resync > mddev->curr_resync_completed &&
7226 (mddev->curr_resync - mddev->curr_resync_completed)
7227 > (max_sectors >> 4)) ||
7228 (j - mddev->curr_resync_completed)*2
7229 >= mddev->resync_max - mddev->curr_resync_completed
7230 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007231 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007232 wait_event(mddev->recovery_wait,
7233 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007234 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007235 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007236 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007237 }
NeilBrownacb180b2009-04-14 16:28:34 +10007238
NeilBrowne62e58a2009-07-01 13:15:35 +10007239 while (j >= mddev->resync_max && !kthread_should_stop()) {
7240 /* As this condition is controlled by user-space,
7241 * we can block indefinitely, so use '_interruptible'
7242 * to avoid triggering warnings.
7243 */
7244 flush_signals(current); /* just in case */
7245 wait_event_interruptible(mddev->recovery_wait,
7246 mddev->resync_max > j
7247 || kthread_should_stop());
7248 }
NeilBrownacb180b2009-04-14 16:28:34 +10007249
7250 if (kthread_should_stop())
7251 goto interrupted;
7252
NeilBrown57afd892005-06-21 17:17:13 -07007253 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007254 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007255 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007256 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 goto out;
7258 }
NeilBrown57afd892005-06-21 17:17:13 -07007259
7260 if (!skipped) { /* actual IO requested */
7261 io_sectors += sectors;
7262 atomic_add(sectors, &mddev->recovery_active);
7263 }
7264
NeilBrowne875ece2011-07-28 11:39:24 +10007265 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7266 break;
7267
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 j += sectors;
7269 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007270 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007271 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007272 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007273 * visible in /proc/mdstat
7274 */
7275 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007276
7277 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 continue;
7279
NeilBrown57afd892005-06-21 17:17:13 -07007280 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 repeat:
7282 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7283 /* step marks */
7284 int next = (last_mark+1) % SYNC_MARKS;
7285
7286 mddev->resync_mark = mark[next];
7287 mddev->resync_mark_cnt = mark_cnt[next];
7288 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007289 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 last_mark = next;
7291 }
7292
7293
NeilBrownc6207272008-02-06 01:39:52 -08007294 if (kthread_should_stop())
7295 goto interrupted;
7296
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297
7298 /*
7299 * this loop exits only if either when we are slower than
7300 * the 'hard' speed limit, or the system was IO-idle for
7301 * a jiffy.
7302 * the system might be non-idle CPU-wise, but we only care
7303 * about not overloading the IO subsystem. (things like an
7304 * e2fsck being done on the RAID array should execute fast)
7305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 cond_resched();
7307
NeilBrown57afd892005-06-21 17:17:13 -07007308 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7309 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
NeilBrown88202a02006-01-06 00:21:36 -08007311 if (currspeed > speed_min(mddev)) {
7312 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007313 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007314 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 goto repeat;
7316 }
7317 }
7318 }
NeilBrown61df9d92006-10-03 01:15:57 -07007319 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 /*
7321 * this also signals 'finished resyncing' to md_stop
7322 */
7323 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7325
7326 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007327 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
NeilBrowndfc70642008-05-23 13:04:39 -07007329 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007330 mddev->curr_resync > 2) {
7331 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7332 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7333 if (mddev->curr_resync >= mddev->recovery_cp) {
7334 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007335 "md: checkpointing %s of %s.\n",
7336 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007337 mddev->recovery_cp =
7338 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007339 }
7340 } else
7341 mddev->recovery_cp = MaxSector;
7342 } else {
7343 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7344 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007345 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007346 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007347 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007348 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007349 !test_bit(Faulty, &rdev->flags) &&
7350 !test_bit(In_sync, &rdev->flags) &&
7351 rdev->recovery_offset < mddev->curr_resync)
7352 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007353 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007356 skip:
NeilBrown17571282006-12-10 02:20:52 -08007357 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
NeilBrownc07b70a2009-12-14 12:49:48 +11007359 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7360 /* We completed so min/max setting can be forgotten if used. */
7361 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7362 mddev->resync_min = 0;
7363 mddev->resync_max = MaxSector;
7364 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7365 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 mddev->curr_resync = 0;
7367 wake_up(&resync_wait);
7368 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7369 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007370 return;
7371
7372 interrupted:
7373 /*
7374 * got a signal, exit.
7375 */
7376 printk(KERN_INFO
7377 "md: md_do_sync() got signal ... exiting\n");
7378 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7379 goto out;
7380
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381}
NeilBrown29269552006-03-27 01:18:10 -08007382EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
NeilBrownfd01b882011-10-11 16:47:53 +11007384static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007385{
NeilBrown3cb03002011-10-11 16:45:26 +11007386 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007387 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007388 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007389
NeilBrown97e4f422009-03-31 14:33:13 +11007390 mddev->curr_resync_completed = 0;
7391
NeilBrowndafb20f2012-03-19 12:46:39 +11007392 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007393 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007394 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007395 (test_bit(Faulty, &rdev->flags) ||
7396 ! test_bit(In_sync, &rdev->flags)) &&
7397 atomic_read(&rdev->nr_pending)==0) {
7398 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007399 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007400 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007401 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007402 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007403 }
7404 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007405 if (removed)
7406 sysfs_notify(&mddev->kobj, NULL,
7407 "degraded");
7408
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007409
NeilBrowndafb20f2012-03-19 12:46:39 +11007410 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007411 if (rdev->raid_disk >= 0 &&
7412 !test_bit(In_sync, &rdev->flags) &&
7413 !test_bit(Faulty, &rdev->flags))
7414 spares++;
7415 if (rdev->raid_disk < 0
7416 && !test_bit(Faulty, &rdev->flags)) {
7417 rdev->recovery_offset = 0;
7418 if (mddev->pers->
7419 hot_add_disk(mddev, rdev) == 0) {
7420 if (sysfs_link_rdev(mddev, rdev))
7421 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007422 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007423 md_new_event(mddev);
7424 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007425 }
NeilBrowndfc70642008-05-23 13:04:39 -07007426 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007427 }
7428 return spares;
7429}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007430
NeilBrownfd01b882011-10-11 16:47:53 +11007431static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007432{
NeilBrown3cb03002011-10-11 16:45:26 +11007433 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007434
7435 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007436 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007437 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7438 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7439 /* success...*/
7440 /* activate any spares */
7441 if (mddev->pers->spare_active(mddev))
7442 sysfs_notify(&mddev->kobj, NULL,
7443 "degraded");
7444 }
7445 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7446 mddev->pers->finish_reshape)
7447 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007448
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007449 /* If array is no-longer degraded, then any saved_raid_disk
7450 * information must be scrapped. Also if any device is now
7451 * In_sync we must scrape the saved_raid_disk for that device
7452 * do the superblock for an incrementally recovered device
7453 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007454 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007455 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007456 if (!mddev->degraded ||
7457 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007458 rdev->saved_raid_disk = -1;
7459
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007460 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007461 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7462 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7463 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7464 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7465 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7466 /* flag recovery needed just to double check */
7467 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7468 sysfs_notify_dirent_safe(mddev->sysfs_action);
7469 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007470 if (mddev->event_work.func)
7471 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007472}
7473
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474/*
7475 * This routine is regularly called by all per-raid-array threads to
7476 * deal with generic issues like resync and super-block update.
7477 * Raid personalities that don't have a thread (linear/raid0) do not
7478 * need this as they never do any recovery or update the superblock.
7479 *
7480 * It does not do any resync itself, but rather "forks" off other threads
7481 * to do that as needed.
7482 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7483 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007484 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 * and wakeups up this thread which will reap the thread and finish up.
7486 * This thread also removes any faulty devices (with nr_pending == 0).
7487 *
7488 * The overall approach is:
7489 * 1/ if the superblock needs updating, update it.
7490 * 2/ If a recovery thread is running, don't do anything else.
7491 * 3/ If recovery has finished, clean up, possibly marking spares active.
7492 * 4/ If there are any faulty devices, remove them.
7493 * 5/ If array is degraded, try to add spares devices
7494 * 6/ If array has spares or is not in-sync, start a resync thread.
7495 */
NeilBrownfd01b882011-10-11 16:47:53 +11007496void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007498 if (mddev->suspended)
7499 return;
7500
NeilBrown5f404022005-06-21 17:17:16 -07007501 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007502 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
NeilBrownfca4d842005-06-21 17:17:11 -07007504 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007505 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007506 printk(KERN_INFO "md: %s in immediate safe mode\n",
7507 mdname(mddev));
7508 mddev->safemode = 2;
7509 }
7510 flush_signals(current);
7511 }
7512
NeilBrownc89a8ee2008-08-05 15:54:13 +10007513 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7514 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007516 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007518 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007519 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007520 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7521 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 ))
7523 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007524
NeilBrowndf5b89b2006-03-27 01:18:20 -08007525 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007526 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007527
NeilBrownc89a8ee2008-08-05 15:54:13 +10007528 if (mddev->ro) {
7529 /* Only thing we do on a ro array is remove
7530 * failed devices.
7531 */
NeilBrown3cb03002011-10-11 16:45:26 +11007532 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007533 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007534 if (rdev->raid_disk >= 0 &&
7535 !test_bit(Blocked, &rdev->flags) &&
7536 test_bit(Faulty, &rdev->flags) &&
7537 atomic_read(&rdev->nr_pending)==0) {
7538 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007539 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007540 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007541 rdev->raid_disk = -1;
7542 }
7543 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007544 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7545 goto unlock;
7546 }
7547
NeilBrown31a59e32008-04-30 00:52:30 -07007548 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007549 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007550 spin_lock_irq(&mddev->write_lock);
7551 if (mddev->safemode &&
7552 !atomic_read(&mddev->writes_pending) &&
7553 !mddev->in_sync &&
7554 mddev->recovery_cp == MaxSector) {
7555 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007556 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007557 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007558 }
7559 if (mddev->safemode == 1)
7560 mddev->safemode = 0;
7561 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007562 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007563 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007564 }
NeilBrownfca4d842005-06-21 17:17:11 -07007565
NeilBrown850b2b422006-10-03 01:15:46 -07007566 if (mddev->flags)
7567 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007568
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7570 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7571 /* resync/recovery still happening */
7572 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7573 goto unlock;
7574 }
7575 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007576 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 goto unlock;
7578 }
Neil Brown72a23c22008-06-28 08:31:41 +10007579 /* Set RUNNING before clearing NEEDED to avoid
7580 * any transients in the value of "sync_action".
7581 */
7582 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7583 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007584 /* Clear some bits that don't mean anything, but
7585 * might be left set
7586 */
NeilBrown24dd4692005-11-08 21:39:26 -08007587 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7588 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007590 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7591 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 /* no recovery is running.
7593 * remove any failed drives, then
7594 * add spares if possible.
7595 * Spare are also removed and re-added, to allow
7596 * the personality to fail the re-add.
7597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007599 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007600 if (mddev->pers->check_reshape == NULL ||
7601 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007602 /* Cannot proceed */
7603 goto unlock;
7604 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007605 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007606 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007607 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7608 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007609 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007610 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007611 } else if (mddev->recovery_cp < MaxSector) {
7612 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007613 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007614 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7615 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007617
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007619 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7620 /* We are adding a device or devices to an array
7621 * which has the bitmap stored on all devices.
7622 * So make sure all bitmap pages get written
7623 */
7624 bitmap_write_all(mddev->bitmap);
7625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 mddev->sync_thread = md_register_thread(md_do_sync,
7627 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007628 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 if (!mddev->sync_thread) {
7630 printk(KERN_ERR "%s: could not start resync"
7631 " thread...\n",
7632 mdname(mddev));
7633 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007634 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7635 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7636 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7637 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7638 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007639 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007641 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007642 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 }
7644 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007645 if (!mddev->sync_thread) {
7646 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7647 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7648 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007649 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007650 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 mddev_unlock(mddev);
7653 }
7654}
7655
NeilBrownfd01b882011-10-11 16:47:53 +11007656void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007657{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007658 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007659 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007660 !test_bit(Blocked, &rdev->flags) &&
7661 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007662 msecs_to_jiffies(5000));
7663 rdev_dec_pending(rdev, mddev);
7664}
7665EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7666
NeilBrown2230dfe2011-07-28 11:31:46 +10007667
7668/* Bad block management.
7669 * We can record which blocks on each device are 'bad' and so just
7670 * fail those blocks, or that stripe, rather than the whole device.
7671 * Entries in the bad-block table are 64bits wide. This comprises:
7672 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7673 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7674 * A 'shift' can be set so that larger blocks are tracked and
7675 * consequently larger devices can be covered.
7676 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7677 *
7678 * Locking of the bad-block table uses a seqlock so md_is_badblock
7679 * might need to retry if it is very unlucky.
7680 * We will sometimes want to check for bad blocks in a bi_end_io function,
7681 * so we use the write_seqlock_irq variant.
7682 *
7683 * When looking for a bad block we specify a range and want to
7684 * know if any block in the range is bad. So we binary-search
7685 * to the last range that starts at-or-before the given endpoint,
7686 * (or "before the sector after the target range")
7687 * then see if it ends after the given start.
7688 * We return
7689 * 0 if there are no known bad blocks in the range
7690 * 1 if there are known bad block which are all acknowledged
7691 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7692 * plus the start/length of the first bad section we overlap.
7693 */
7694int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7695 sector_t *first_bad, int *bad_sectors)
7696{
7697 int hi;
7698 int lo = 0;
7699 u64 *p = bb->page;
7700 int rv = 0;
7701 sector_t target = s + sectors;
7702 unsigned seq;
7703
7704 if (bb->shift > 0) {
7705 /* round the start down, and the end up */
7706 s >>= bb->shift;
7707 target += (1<<bb->shift) - 1;
7708 target >>= bb->shift;
7709 sectors = target - s;
7710 }
7711 /* 'target' is now the first block after the bad range */
7712
7713retry:
7714 seq = read_seqbegin(&bb->lock);
7715
7716 hi = bb->count;
7717
7718 /* Binary search between lo and hi for 'target'
7719 * i.e. for the last range that starts before 'target'
7720 */
7721 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7722 * are known not to be the last range before target.
7723 * VARIANT: hi-lo is the number of possible
7724 * ranges, and decreases until it reaches 1
7725 */
7726 while (hi - lo > 1) {
7727 int mid = (lo + hi) / 2;
7728 sector_t a = BB_OFFSET(p[mid]);
7729 if (a < target)
7730 /* This could still be the one, earlier ranges
7731 * could not. */
7732 lo = mid;
7733 else
7734 /* This and later ranges are definitely out. */
7735 hi = mid;
7736 }
7737 /* 'lo' might be the last that started before target, but 'hi' isn't */
7738 if (hi > lo) {
7739 /* need to check all range that end after 's' to see if
7740 * any are unacknowledged.
7741 */
7742 while (lo >= 0 &&
7743 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7744 if (BB_OFFSET(p[lo]) < target) {
7745 /* starts before the end, and finishes after
7746 * the start, so they must overlap
7747 */
7748 if (rv != -1 && BB_ACK(p[lo]))
7749 rv = 1;
7750 else
7751 rv = -1;
7752 *first_bad = BB_OFFSET(p[lo]);
7753 *bad_sectors = BB_LEN(p[lo]);
7754 }
7755 lo--;
7756 }
7757 }
7758
7759 if (read_seqretry(&bb->lock, seq))
7760 goto retry;
7761
7762 return rv;
7763}
7764EXPORT_SYMBOL_GPL(md_is_badblock);
7765
7766/*
7767 * Add a range of bad blocks to the table.
7768 * This might extend the table, or might contract it
7769 * if two adjacent ranges can be merged.
7770 * We binary-search to find the 'insertion' point, then
7771 * decide how best to handle it.
7772 */
7773static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7774 int acknowledged)
7775{
7776 u64 *p;
7777 int lo, hi;
7778 int rv = 1;
7779
7780 if (bb->shift < 0)
7781 /* badblocks are disabled */
7782 return 0;
7783
7784 if (bb->shift) {
7785 /* round the start down, and the end up */
7786 sector_t next = s + sectors;
7787 s >>= bb->shift;
7788 next += (1<<bb->shift) - 1;
7789 next >>= bb->shift;
7790 sectors = next - s;
7791 }
7792
7793 write_seqlock_irq(&bb->lock);
7794
7795 p = bb->page;
7796 lo = 0;
7797 hi = bb->count;
7798 /* Find the last range that starts at-or-before 's' */
7799 while (hi - lo > 1) {
7800 int mid = (lo + hi) / 2;
7801 sector_t a = BB_OFFSET(p[mid]);
7802 if (a <= s)
7803 lo = mid;
7804 else
7805 hi = mid;
7806 }
7807 if (hi > lo && BB_OFFSET(p[lo]) > s)
7808 hi = lo;
7809
7810 if (hi > lo) {
7811 /* we found a range that might merge with the start
7812 * of our new range
7813 */
7814 sector_t a = BB_OFFSET(p[lo]);
7815 sector_t e = a + BB_LEN(p[lo]);
7816 int ack = BB_ACK(p[lo]);
7817 if (e >= s) {
7818 /* Yes, we can merge with a previous range */
7819 if (s == a && s + sectors >= e)
7820 /* new range covers old */
7821 ack = acknowledged;
7822 else
7823 ack = ack && acknowledged;
7824
7825 if (e < s + sectors)
7826 e = s + sectors;
7827 if (e - a <= BB_MAX_LEN) {
7828 p[lo] = BB_MAKE(a, e-a, ack);
7829 s = e;
7830 } else {
7831 /* does not all fit in one range,
7832 * make p[lo] maximal
7833 */
7834 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7835 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7836 s = a + BB_MAX_LEN;
7837 }
7838 sectors = e - s;
7839 }
7840 }
7841 if (sectors && hi < bb->count) {
7842 /* 'hi' points to the first range that starts after 's'.
7843 * Maybe we can merge with the start of that range */
7844 sector_t a = BB_OFFSET(p[hi]);
7845 sector_t e = a + BB_LEN(p[hi]);
7846 int ack = BB_ACK(p[hi]);
7847 if (a <= s + sectors) {
7848 /* merging is possible */
7849 if (e <= s + sectors) {
7850 /* full overlap */
7851 e = s + sectors;
7852 ack = acknowledged;
7853 } else
7854 ack = ack && acknowledged;
7855
7856 a = s;
7857 if (e - a <= BB_MAX_LEN) {
7858 p[hi] = BB_MAKE(a, e-a, ack);
7859 s = e;
7860 } else {
7861 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7862 s = a + BB_MAX_LEN;
7863 }
7864 sectors = e - s;
7865 lo = hi;
7866 hi++;
7867 }
7868 }
7869 if (sectors == 0 && hi < bb->count) {
7870 /* we might be able to combine lo and hi */
7871 /* Note: 's' is at the end of 'lo' */
7872 sector_t a = BB_OFFSET(p[hi]);
7873 int lolen = BB_LEN(p[lo]);
7874 int hilen = BB_LEN(p[hi]);
7875 int newlen = lolen + hilen - (s - a);
7876 if (s >= a && newlen < BB_MAX_LEN) {
7877 /* yes, we can combine them */
7878 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7879 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7880 memmove(p + hi, p + hi + 1,
7881 (bb->count - hi - 1) * 8);
7882 bb->count--;
7883 }
7884 }
7885 while (sectors) {
7886 /* didn't merge (it all).
7887 * Need to add a range just before 'hi' */
7888 if (bb->count >= MD_MAX_BADBLOCKS) {
7889 /* No room for more */
7890 rv = 0;
7891 break;
7892 } else {
7893 int this_sectors = sectors;
7894 memmove(p + hi + 1, p + hi,
7895 (bb->count - hi) * 8);
7896 bb->count++;
7897
7898 if (this_sectors > BB_MAX_LEN)
7899 this_sectors = BB_MAX_LEN;
7900 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7901 sectors -= this_sectors;
7902 s += this_sectors;
7903 }
7904 }
7905
7906 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007907 if (!acknowledged)
7908 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007909 write_sequnlock_irq(&bb->lock);
7910
7911 return rv;
7912}
7913
NeilBrown3cb03002011-10-11 16:45:26 +11007914int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007915 int acknowledged)
7916{
7917 int rv = md_set_badblocks(&rdev->badblocks,
7918 s + rdev->data_offset, sectors, acknowledged);
7919 if (rv) {
7920 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007921 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007922 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7923 md_wakeup_thread(rdev->mddev->thread);
7924 }
7925 return rv;
7926}
7927EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7928
7929/*
7930 * Remove a range of bad blocks from the table.
7931 * This may involve extending the table if we spilt a region,
7932 * but it must not fail. So if the table becomes full, we just
7933 * drop the remove request.
7934 */
7935static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7936{
7937 u64 *p;
7938 int lo, hi;
7939 sector_t target = s + sectors;
7940 int rv = 0;
7941
7942 if (bb->shift > 0) {
7943 /* When clearing we round the start up and the end down.
7944 * This should not matter as the shift should align with
7945 * the block size and no rounding should ever be needed.
7946 * However it is better the think a block is bad when it
7947 * isn't than to think a block is not bad when it is.
7948 */
7949 s += (1<<bb->shift) - 1;
7950 s >>= bb->shift;
7951 target >>= bb->shift;
7952 sectors = target - s;
7953 }
7954
7955 write_seqlock_irq(&bb->lock);
7956
7957 p = bb->page;
7958 lo = 0;
7959 hi = bb->count;
7960 /* Find the last range that starts before 'target' */
7961 while (hi - lo > 1) {
7962 int mid = (lo + hi) / 2;
7963 sector_t a = BB_OFFSET(p[mid]);
7964 if (a < target)
7965 lo = mid;
7966 else
7967 hi = mid;
7968 }
7969 if (hi > lo) {
7970 /* p[lo] is the last range that could overlap the
7971 * current range. Earlier ranges could also overlap,
7972 * but only this one can overlap the end of the range.
7973 */
7974 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7975 /* Partial overlap, leave the tail of this range */
7976 int ack = BB_ACK(p[lo]);
7977 sector_t a = BB_OFFSET(p[lo]);
7978 sector_t end = a + BB_LEN(p[lo]);
7979
7980 if (a < s) {
7981 /* we need to split this range */
7982 if (bb->count >= MD_MAX_BADBLOCKS) {
7983 rv = 0;
7984 goto out;
7985 }
7986 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7987 bb->count++;
7988 p[lo] = BB_MAKE(a, s-a, ack);
7989 lo++;
7990 }
7991 p[lo] = BB_MAKE(target, end - target, ack);
7992 /* there is no longer an overlap */
7993 hi = lo;
7994 lo--;
7995 }
7996 while (lo >= 0 &&
7997 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7998 /* This range does overlap */
7999 if (BB_OFFSET(p[lo]) < s) {
8000 /* Keep the early parts of this range. */
8001 int ack = BB_ACK(p[lo]);
8002 sector_t start = BB_OFFSET(p[lo]);
8003 p[lo] = BB_MAKE(start, s - start, ack);
8004 /* now low doesn't overlap, so.. */
8005 break;
8006 }
8007 lo--;
8008 }
8009 /* 'lo' is strictly before, 'hi' is strictly after,
8010 * anything between needs to be discarded
8011 */
8012 if (hi - lo > 1) {
8013 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8014 bb->count -= (hi - lo - 1);
8015 }
8016 }
8017
8018 bb->changed = 1;
8019out:
8020 write_sequnlock_irq(&bb->lock);
8021 return rv;
8022}
8023
NeilBrown3cb03002011-10-11 16:45:26 +11008024int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10008025{
8026 return md_clear_badblocks(&rdev->badblocks,
8027 s + rdev->data_offset,
8028 sectors);
8029}
8030EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8031
8032/*
8033 * Acknowledge all bad blocks in a list.
8034 * This only succeeds if ->changed is clear. It is used by
8035 * in-kernel metadata updates
8036 */
8037void md_ack_all_badblocks(struct badblocks *bb)
8038{
8039 if (bb->page == NULL || bb->changed)
8040 /* no point even trying */
8041 return;
8042 write_seqlock_irq(&bb->lock);
8043
8044 if (bb->changed == 0) {
8045 u64 *p = bb->page;
8046 int i;
8047 for (i = 0; i < bb->count ; i++) {
8048 if (!BB_ACK(p[i])) {
8049 sector_t start = BB_OFFSET(p[i]);
8050 int len = BB_LEN(p[i]);
8051 p[i] = BB_MAKE(start, len, 1);
8052 }
8053 }
NeilBrownde393cd2011-07-28 11:31:48 +10008054 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008055 }
8056 write_sequnlock_irq(&bb->lock);
8057}
8058EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8059
NeilBrown16c791a2011-07-28 11:31:47 +10008060/* sysfs access to bad-blocks list.
8061 * We present two files.
8062 * 'bad-blocks' lists sector numbers and lengths of ranges that
8063 * are recorded as bad. The list is truncated to fit within
8064 * the one-page limit of sysfs.
8065 * Writing "sector length" to this file adds an acknowledged
8066 * bad block list.
8067 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8068 * been acknowledged. Writing to this file adds bad blocks
8069 * without acknowledging them. This is largely for testing.
8070 */
8071
8072static ssize_t
8073badblocks_show(struct badblocks *bb, char *page, int unack)
8074{
8075 size_t len;
8076 int i;
8077 u64 *p = bb->page;
8078 unsigned seq;
8079
8080 if (bb->shift < 0)
8081 return 0;
8082
8083retry:
8084 seq = read_seqbegin(&bb->lock);
8085
8086 len = 0;
8087 i = 0;
8088
8089 while (len < PAGE_SIZE && i < bb->count) {
8090 sector_t s = BB_OFFSET(p[i]);
8091 unsigned int length = BB_LEN(p[i]);
8092 int ack = BB_ACK(p[i]);
8093 i++;
8094
8095 if (unack && ack)
8096 continue;
8097
8098 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8099 (unsigned long long)s << bb->shift,
8100 length << bb->shift);
8101 }
NeilBrownde393cd2011-07-28 11:31:48 +10008102 if (unack && len == 0)
8103 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008104
8105 if (read_seqretry(&bb->lock, seq))
8106 goto retry;
8107
8108 return len;
8109}
8110
8111#define DO_DEBUG 1
8112
8113static ssize_t
8114badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8115{
8116 unsigned long long sector;
8117 int length;
8118 char newline;
8119#ifdef DO_DEBUG
8120 /* Allow clearing via sysfs *only* for testing/debugging.
8121 * Normally only a successful write may clear a badblock
8122 */
8123 int clear = 0;
8124 if (page[0] == '-') {
8125 clear = 1;
8126 page++;
8127 }
8128#endif /* DO_DEBUG */
8129
8130 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8131 case 3:
8132 if (newline != '\n')
8133 return -EINVAL;
8134 case 2:
8135 if (length <= 0)
8136 return -EINVAL;
8137 break;
8138 default:
8139 return -EINVAL;
8140 }
8141
8142#ifdef DO_DEBUG
8143 if (clear) {
8144 md_clear_badblocks(bb, sector, length);
8145 return len;
8146 }
8147#endif /* DO_DEBUG */
8148 if (md_set_badblocks(bb, sector, length, !unack))
8149 return len;
8150 else
8151 return -ENOSPC;
8152}
8153
Adrian Bunk75c96f82005-05-05 16:16:09 -07008154static int md_notify_reboot(struct notifier_block *this,
8155 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156{
8157 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008158 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008159 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160
NeilBrownc744a652012-03-19 12:46:37 +11008161 for_each_mddev(mddev, tmp) {
8162 if (mddev_trylock(mddev)) {
8163 __md_stop_writes(mddev);
8164 mddev->safemode = 2;
8165 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008166 }
NeilBrownc744a652012-03-19 12:46:37 +11008167 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 }
NeilBrownc744a652012-03-19 12:46:37 +11008169 /*
8170 * certain more exotic SCSI devices are known to be
8171 * volatile wrt too early system reboots. While the
8172 * right place to handle this issue is the given
8173 * driver, we do want to have a safe RAID driver ...
8174 */
8175 if (need_delay)
8176 mdelay(1000*1);
8177
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 return NOTIFY_DONE;
8179}
8180
Adrian Bunk75c96f82005-05-05 16:16:09 -07008181static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 .notifier_call = md_notify_reboot,
8183 .next = NULL,
8184 .priority = INT_MAX, /* before any real devices */
8185};
8186
8187static void md_geninit(void)
8188{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008189 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008191 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192}
8193
Adrian Bunk75c96f82005-05-05 16:16:09 -07008194static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195{
Tejun Heoe804ac72010-10-15 15:36:08 +02008196 int ret = -ENOMEM;
8197
Tejun Heoada609e2011-01-25 14:35:54 +01008198 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008199 if (!md_wq)
8200 goto err_wq;
8201
8202 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8203 if (!md_misc_wq)
8204 goto err_misc_wq;
8205
8206 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8207 goto err_md;
8208
8209 if ((ret = register_blkdev(0, "mdp")) < 0)
8210 goto err_mdp;
8211 mdp_major = ret;
8212
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008213 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008214 md_probe, NULL, NULL);
8215 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 md_probe, NULL, NULL);
8217
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008219 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220
8221 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
Tejun Heoe804ac72010-10-15 15:36:08 +02008224err_mdp:
8225 unregister_blkdev(MD_MAJOR, "md");
8226err_md:
8227 destroy_workqueue(md_misc_wq);
8228err_misc_wq:
8229 destroy_workqueue(md_wq);
8230err_wq:
8231 return ret;
8232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
8234#ifndef MODULE
8235
8236/*
8237 * Searches all registered partitions for autorun RAID arrays
8238 * at boot time.
8239 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008240
8241static LIST_HEAD(all_detected_devices);
8242struct detected_devices_node {
8243 struct list_head list;
8244 dev_t dev;
8245};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246
8247void md_autodetect_dev(dev_t dev)
8248{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008249 struct detected_devices_node *node_detected_dev;
8250
8251 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8252 if (node_detected_dev) {
8253 node_detected_dev->dev = dev;
8254 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8255 } else {
8256 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8257 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259}
8260
8261
8262static void autostart_arrays(int part)
8263{
NeilBrown3cb03002011-10-11 16:45:26 +11008264 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008265 struct detected_devices_node *node_detected_dev;
8266 dev_t dev;
8267 int i_scanned, i_passed;
8268
8269 i_scanned = 0;
8270 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271
8272 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8273
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008274 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8275 i_scanned++;
8276 node_detected_dev = list_entry(all_detected_devices.next,
8277 struct detected_devices_node, list);
8278 list_del(&node_detected_dev->list);
8279 dev = node_detected_dev->dev;
8280 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008281 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 if (IS_ERR(rdev))
8283 continue;
8284
NeilBrownb2d444d2005-11-08 21:39:31 -08008285 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 MD_BUG();
8287 continue;
8288 }
NeilBrownd0fae182008-03-04 14:29:31 -08008289 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008290 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008291 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008293
8294 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8295 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296
8297 autorun_devices(part);
8298}
8299
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008300#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301
8302static __exit void md_exit(void)
8303{
NeilBrownfd01b882011-10-11 16:47:53 +11008304 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008306
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008307 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008308 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008309
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008310 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008311 unregister_blkdev(mdp_major, "mdp");
8312 unregister_reboot_notifier(&md_notifier);
8313 unregister_sysctl_table(raid_table_header);
8314 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008315 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008317 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008318 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008319 destroy_workqueue(md_misc_wq);
8320 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321}
8322
Dan Williams685784a2007-07-09 11:56:42 -07008323subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324module_exit(md_exit)
8325
NeilBrownf91de922005-11-08 21:39:36 -08008326static int get_ro(char *buffer, struct kernel_param *kp)
8327{
8328 return sprintf(buffer, "%d", start_readonly);
8329}
8330static int set_ro(const char *val, struct kernel_param *kp)
8331{
8332 char *e;
8333 int num = simple_strtoul(val, &e, 10);
8334 if (*val && (*e == '\0' || *e == '\n')) {
8335 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008336 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008337 }
8338 return -EINVAL;
8339}
8340
NeilBrown80ca3a42006-07-10 04:44:18 -07008341module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8342module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008343
NeilBrownefeb53c2009-01-09 08:31:10 +11008344module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008345
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346EXPORT_SYMBOL(register_md_personality);
8347EXPORT_SYMBOL(unregister_md_personality);
8348EXPORT_SYMBOL(md_error);
8349EXPORT_SYMBOL(md_done_sync);
8350EXPORT_SYMBOL(md_write_start);
8351EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352EXPORT_SYMBOL(md_register_thread);
8353EXPORT_SYMBOL(md_unregister_thread);
8354EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355EXPORT_SYMBOL(md_check_recovery);
8356MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008357MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008358MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008359MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);