blob: 3db3d1b271f7ef18650a227a8dd07dbc2b8fefbc [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
NeilBrowna167f662010-10-26 18:31:13 +1100158struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100159 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100160{
161 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100162
163 if (!mddev || !mddev->bio_set)
164 return bio_alloc(gfp_mask, nr_iovecs);
165
Kent Overstreet395c72a2012-09-06 15:34:55 -0700166 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100167 if (!b)
168 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100169 return b;
170}
171EXPORT_SYMBOL_GPL(bio_alloc_mddev);
172
173struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100174 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100175{
NeilBrowna167f662010-10-26 18:31:13 +1100176 if (!mddev || !mddev->bio_set)
177 return bio_clone(bio, gfp_mask);
178
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700179 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100180}
181EXPORT_SYMBOL_GPL(bio_clone_mddev);
182
NeilBrownd2eb35a2011-07-28 11:31:48 +1000183void md_trim_bio(struct bio *bio, int offset, int size)
184{
185 /* 'bio' is a cloned bio which we need to trim to match
186 * the given offset and size.
187 * This requires adjusting bi_sector, bi_size, and bi_io_vec
188 */
189 int i;
190 struct bio_vec *bvec;
191 int sofar = 0;
192
193 size <<= 9;
194 if (offset == 0 && size == bio->bi_size)
195 return;
196
197 bio->bi_sector += offset;
198 bio->bi_size = size;
199 offset <<= 9;
200 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
201
202 while (bio->bi_idx < bio->bi_vcnt &&
203 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
204 /* remove this whole bio_vec */
205 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
206 bio->bi_idx++;
207 }
208 if (bio->bi_idx < bio->bi_vcnt) {
209 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
210 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
211 }
212 /* avoid any complications with bi_idx being non-zero*/
213 if (bio->bi_idx) {
214 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
215 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
216 bio->bi_vcnt -= bio->bi_idx;
217 bio->bi_idx = 0;
218 }
219 /* Make sure vcnt and last bv are not too big */
220 bio_for_each_segment(bvec, bio, i) {
221 if (sofar + bvec->bv_len > size)
222 bvec->bv_len = size - sofar;
223 if (bvec->bv_len == 0) {
224 bio->bi_vcnt = i;
225 break;
226 }
227 sofar += bvec->bv_len;
228 }
229}
230EXPORT_SYMBOL_GPL(md_trim_bio);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
NeilBrownd7603b72006-01-06 00:20:30 -0800233 * We have a system wide 'event count' that is incremented
234 * on any 'interesting' event, and readers of /proc/mdstat
235 * can use 'poll' or 'select' to find out when the event
236 * count increases.
237 *
238 * Events are:
239 * start array, stop array, error, add device, remove device,
240 * start build, activate spare
241 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800242static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800243static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100244void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800245{
246 atomic_inc(&md_event_count);
247 wake_up(&md_event_waiters);
248}
NeilBrown29269552006-03-27 01:18:10 -0800249EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800250
NeilBrownc331eb02006-05-30 21:27:13 -0700251/* Alternate version that can be called from interrupts
252 * when calling sysfs_notify isn't needed.
253 */
NeilBrownfd01b882011-10-11 16:47:53 +1100254static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700255{
256 atomic_inc(&md_event_count);
257 wake_up(&md_event_waiters);
258}
259
NeilBrownd7603b72006-01-06 00:20:30 -0800260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * Enables to iterate over all existing md arrays
262 * all_mddevs_lock protects this list.
263 */
264static LIST_HEAD(all_mddevs);
265static DEFINE_SPINLOCK(all_mddevs_lock);
266
267
268/*
269 * iterates through all used mddevs in the system.
270 * We take care to grab the all_mddevs_lock whenever navigating
271 * the list, and to always hold a refcount when unlocked.
272 * Any code which breaks out of this loop while own
273 * a reference to the current mddev and must mddev_put it.
274 */
NeilBrownfd01b882011-10-11 16:47:53 +1100275#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 \
277 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100278 _tmp = all_mddevs.next; \
279 _mddev = NULL;}); \
280 ({ if (_tmp != &all_mddevs) \
281 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100283 if (_mddev) mddev_put(_mddev); \
284 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
285 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100287 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 )
289
290
NeilBrown409c57f2009-03-31 14:39:39 +1100291/* Rather than calling directly into the personality make_request function,
292 * IO requests come here first so that we can check if the device is
293 * being suspended pending a reconfiguration.
294 * We hold a refcount over the call to ->make_request. By the time that
295 * call has finished, the bio has been linked into some internal structure
296 * and so is visible to ->quiesce(), so we don't need the refcount any more.
297 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200298static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
NeilBrown49077322010-03-25 16:20:56 +1100300 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100301 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100302 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500303 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100304
NeilBrown0ca69882011-01-14 09:14:33 +1100305 if (mddev == NULL || mddev->pers == NULL
306 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100307 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200308 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100309 }
NeilBrown0ca69882011-01-14 09:14:33 +1100310 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100311 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200312 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100313 DEFINE_WAIT(__wait);
314 for (;;) {
315 prepare_to_wait(&mddev->sb_wait, &__wait,
316 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200317 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100318 break;
319 rcu_read_unlock();
320 schedule();
321 rcu_read_lock();
322 }
323 finish_wait(&mddev->sb_wait, &__wait);
324 }
325 atomic_inc(&mddev->active_io);
326 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100327
Chris Masone91ece52011-02-07 19:21:48 -0500328 /*
329 * save the sectors now since our bio can
330 * go away inside make_request
331 */
332 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200333 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100334
335 cpu = part_stat_lock();
336 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500337 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100338 part_stat_unlock();
339
NeilBrown409c57f2009-03-31 14:39:39 +1100340 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
341 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
NeilBrown9e35b992010-04-06 14:23:02 +1000344/* mddev_suspend makes sure no new requests are submitted
345 * to the device, and that any requests that have been submitted
346 * are completely handled.
347 * Once ->stop is called and completes, the module will be completely
348 * unused.
349 */
NeilBrownfd01b882011-10-11 16:47:53 +1100350void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100351{
352 BUG_ON(mddev->suspended);
353 mddev->suspended = 1;
354 synchronize_rcu();
355 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
356 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500357
358 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100359}
NeilBrown390ee602010-06-01 19:37:27 +1000360EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100361
NeilBrownfd01b882011-10-11 16:47:53 +1100362void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100363{
364 mddev->suspended = 0;
365 wake_up(&mddev->sb_wait);
366 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500367
Jonathan Brassow47525e52012-05-22 13:55:29 +1000368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500369 md_wakeup_thread(mddev->thread);
370 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100371}
NeilBrown390ee602010-06-01 19:37:27 +1000372EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100373
NeilBrownfd01b882011-10-11 16:47:53 +1100374int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000375{
376 return mddev->suspended;
377}
378EXPORT_SYMBOL(mddev_congested);
379
NeilBrowna2826aa2009-12-14 12:49:49 +1100380/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200381 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100382 */
383
Tejun Heoe9c74692010-09-03 11:56:18 +0200384static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100385{
NeilBrown3cb03002011-10-11 16:45:26 +1100386 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100387 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100388
389 rdev_dec_pending(rdev, mddev);
390
391 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200392 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200393 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100394 }
395 bio_put(bio);
396}
397
NeilBrowna7a07e62010-12-09 16:04:25 +1100398static void md_submit_flush_data(struct work_struct *ws);
399
NeilBrowna035fc32010-12-09 16:17:51 +1100400static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100401{
NeilBrownfd01b882011-10-11 16:47:53 +1100402 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100403 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100404
NeilBrowna7a07e62010-12-09 16:04:25 +1100405 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
406 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100408 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409 if (rdev->raid_disk >= 0 &&
410 !test_bit(Faulty, &rdev->flags)) {
411 /* Take two references, one is dropped
412 * when request finishes, one after
413 * we reclaim rcu_read_lock
414 */
415 struct bio *bi;
416 atomic_inc(&rdev->nr_pending);
417 atomic_inc(&rdev->nr_pending);
418 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000419 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 bi->bi_private = rdev;
422 bi->bi_bdev = rdev->bdev;
423 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 rcu_read_lock();
426 rdev_dec_pending(rdev, mddev);
427 }
428 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100429 if (atomic_dec_and_test(&mddev->flush_pending))
430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431}
432
Tejun Heoe9c74692010-09-03 11:56:18 +0200433static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100434{
NeilBrownfd01b882011-10-11 16:47:53 +1100435 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100437
Tejun Heoe9c74692010-09-03 11:56:18 +0200438 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 /* an empty barrier - all done */
440 bio_endio(bio, 0);
441 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200443 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 }
NeilBrown2b74e122010-12-09 15:59:01 +1100445
446 mddev->flush_bio = NULL;
447 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448}
449
NeilBrownfd01b882011-10-11 16:47:53 +1100450void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100451{
452 spin_lock_irq(&mddev->write_lock);
453 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100455 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200456 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100457 spin_unlock_irq(&mddev->write_lock);
458
NeilBrowna035fc32010-12-09 16:17:51 +1100459 INIT_WORK(&mddev->flush_work, submit_flushes);
460 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100461}
Tejun Heoe9c74692010-09-03 11:56:18 +0200462EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100463
NeilBrown74018dc2012-07-31 09:08:15 +0200464void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000465{
NeilBrown9cbb1752012-07-31 09:08:14 +0200466 struct mddev *mddev = cb->data;
467 md_wakeup_thread(mddev->thread);
468 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000469}
NeilBrown9cbb1752012-07-31 09:08:14 +0200470EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000471
NeilBrownfd01b882011-10-11 16:47:53 +1100472static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 atomic_inc(&mddev->active);
475 return mddev;
476}
477
Dan Williams5fd3a172009-03-04 00:57:25 -0700478static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100479
NeilBrownfd01b882011-10-11 16:47:53 +1100480static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
NeilBrowna167f662010-10-26 18:31:13 +1100482 struct bio_set *bs = NULL;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
485 return;
NeilBrownd3374822009-01-09 08:31:10 +1100486 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100487 mddev->ctime == 0 && !mddev->hold_active) {
488 /* Array is not configured at all, and not held active,
489 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100490 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100491 bs = mddev->bio_set;
492 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100493 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200494 /* We did a probe so need to clean up. Call
495 * queue_work inside the spinlock so that
496 * flush_workqueue() after mddev_find will
497 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100498 */
499 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200500 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100501 } else
502 kfree(mddev);
503 }
504 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100505 if (bs)
506 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
NeilBrownfd01b882011-10-11 16:47:53 +1100509void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100510{
511 mutex_init(&mddev->open_mutex);
512 mutex_init(&mddev->reconfig_mutex);
513 mutex_init(&mddev->bitmap_info.mutex);
514 INIT_LIST_HEAD(&mddev->disks);
515 INIT_LIST_HEAD(&mddev->all_mddevs);
516 init_timer(&mddev->safemode_timer);
517 atomic_set(&mddev->active, 1);
518 atomic_set(&mddev->openers, 0);
519 atomic_set(&mddev->active_io, 0);
520 spin_lock_init(&mddev->write_lock);
521 atomic_set(&mddev->flush_pending, 0);
522 init_waitqueue_head(&mddev->sb_wait);
523 init_waitqueue_head(&mddev->recovery_wait);
524 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000525 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100526 mddev->resync_min = 0;
527 mddev->resync_max = MaxSector;
528 mddev->level = LEVEL_NONE;
529}
NeilBrown390ee602010-06-01 19:37:27 +1000530EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100531
NeilBrownfd01b882011-10-11 16:47:53 +1100532static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
NeilBrownfd01b882011-10-11 16:47:53 +1100534 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
NeilBrown8f5f02c2011-02-16 13:58:51 +1100536 if (unit && MAJOR(unit) != MD_MAJOR)
537 unit &= ~((1<<MdpMinorShift)-1);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 retry:
540 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
NeilBrownefeb53c2009-01-09 08:31:10 +1100542 if (unit) {
543 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
544 if (mddev->unit == unit) {
545 mddev_get(mddev);
546 spin_unlock(&all_mddevs_lock);
547 kfree(new);
548 return mddev;
549 }
550
551 if (new) {
552 list_add(&new->all_mddevs, &all_mddevs);
553 spin_unlock(&all_mddevs_lock);
554 new->hold_active = UNTIL_IOCTL;
555 return new;
556 }
557 } else if (new) {
558 /* find an unused unit number */
559 static int next_minor = 512;
560 int start = next_minor;
561 int is_free = 0;
562 int dev = 0;
563 while (!is_free) {
564 dev = MKDEV(MD_MAJOR, next_minor);
565 next_minor++;
566 if (next_minor > MINORMASK)
567 next_minor = 0;
568 if (next_minor == start) {
569 /* Oh dear, all in use. */
570 spin_unlock(&all_mddevs_lock);
571 kfree(new);
572 return NULL;
573 }
574
575 is_free = 1;
576 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
577 if (mddev->unit == dev) {
578 is_free = 0;
579 break;
580 }
581 }
582 new->unit = dev;
583 new->md_minor = MINOR(dev);
584 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_add(&new->all_mddevs, &all_mddevs);
586 spin_unlock(&all_mddevs_lock);
587 return new;
588 }
589 spin_unlock(&all_mddevs_lock);
590
NeilBrown9ffae0c2006-01-06 00:20:32 -0800591 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!new)
593 return NULL;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 new->unit = unit;
596 if (MAJOR(unit) == MD_MAJOR)
597 new->md_minor = MINOR(unit);
598 else
599 new->md_minor = MINOR(unit) >> MdpMinorShift;
600
NeilBrownfafd7fb2010-04-01 15:55:30 +1100601 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto retry;
604}
605
NeilBrownfd01b882011-10-11 16:47:53 +1100606static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800608 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
NeilBrownfd01b882011-10-11 16:47:53 +1100611static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100612{
613 return mutex_is_locked(&mddev->reconfig_mutex);
614}
615
NeilBrownfd01b882011-10-11 16:47:53 +1100616static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800618 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
NeilBrownb6eb1272010-04-15 10:13:47 +1000621static struct attribute_group md_redundancy_group;
622
NeilBrownfd01b882011-10-11 16:47:53 +1100623static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
NeilBrowna64c8762010-04-14 17:15:37 +1000625 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000626 /* These cannot be removed under reconfig_mutex as
627 * an access to the files will try to take reconfig_mutex
628 * while holding the file unremovable, which leads to
629 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000630 * So hold set sysfs_active while the remove in happeing,
631 * and anything else which might set ->to_remove or my
632 * otherwise change the sysfs namespace will fail with
633 * -EBUSY if sysfs_active is still set.
634 * We set sysfs_active under reconfig_mutex and elsewhere
635 * test it under the same mutex to ensure its correct value
636 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000637 */
NeilBrowna64c8762010-04-14 17:15:37 +1000638 struct attribute_group *to_remove = mddev->to_remove;
639 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000640 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000641 mutex_unlock(&mddev->reconfig_mutex);
642
NeilBrown00bcb4a2010-06-01 19:37:23 +1000643 if (mddev->kobj.sd) {
644 if (to_remove != &md_redundancy_group)
645 sysfs_remove_group(&mddev->kobj, to_remove);
646 if (mddev->pers == NULL ||
647 mddev->pers->sync_request == NULL) {
648 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
649 if (mddev->sysfs_action)
650 sysfs_put(mddev->sysfs_action);
651 mddev->sysfs_action = NULL;
652 }
NeilBrowna64c8762010-04-14 17:15:37 +1000653 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000654 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000655 } else
656 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Chris Dunlop751e67c2011-10-19 16:48:26 +1100658 /* As we've dropped the mutex we need a spinlock to
659 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000660 */
661 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700662 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000663 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
NeilBrownfd01b882011-10-11 16:47:53 +1100666static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
NeilBrown3cb03002011-10-11 16:45:26 +1100668 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
NeilBrowndafb20f2012-03-19 12:46:39 +1100670 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (rdev->desc_nr == nr)
672 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return NULL;
675}
676
NeilBrown1ca69c42012-10-11 13:37:33 +1100677static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
678{
679 struct md_rdev *rdev;
680
681 rdev_for_each_rcu(rdev, mddev)
682 if (rdev->desc_nr == nr)
683 return rdev;
684
685 return NULL;
686}
687
688static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
NeilBrown3cb03002011-10-11 16:45:26 +1100690 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
NeilBrowndafb20f2012-03-19 12:46:39 +1100692 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (rdev->bdev->bd_dev == dev)
694 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return NULL;
697}
698
NeilBrown1ca69c42012-10-11 13:37:33 +1100699static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
700{
701 struct md_rdev *rdev;
702
703 rdev_for_each_rcu(rdev, mddev)
704 if (rdev->bdev->bd_dev == dev)
705 return rdev;
706
707 return NULL;
708}
709
NeilBrown84fc4b52011-10-11 16:49:58 +1100710static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800711{
NeilBrown84fc4b52011-10-11 16:49:58 +1100712 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800713 list_for_each_entry(pers, &pers_list, list) {
714 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800715 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800716 if (strcmp(pers->name, clevel)==0)
717 return pers;
718 }
NeilBrown2604b702006-01-06 00:20:36 -0800719 return NULL;
720}
721
Andre Nollb73df2d2008-07-11 22:02:23 +1000722/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100723static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100725 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000726 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
NeilBrown3cb03002011-10-11 16:45:26 +1100729static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 if (rdev->sb_page)
732 MD_BUG();
733
734 rdev->sb_page = alloc_page(GFP_KERNEL);
735 if (!rdev->sb_page) {
736 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000737 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 return 0;
741}
742
NeilBrown545c8792012-05-22 13:54:30 +1000743void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800746 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rdev->sb_loaded = 0;
748 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000749 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100750 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
NeilBrown2699b672011-07-28 11:31:47 +1000752 if (rdev->bb_page) {
753 put_page(rdev->bb_page);
754 rdev->bb_page = NULL;
755 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000756 kfree(rdev->badblocks.page);
757 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
NeilBrown545c8792012-05-22 13:54:30 +1000759EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
NeilBrown6712ecf2007-09-27 12:47:43 +0200761static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762{
NeilBrown3cb03002011-10-11 16:45:26 +1100763 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100764 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700765
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700766 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
767 printk("md: super_written gets error=%d, uptodate=%d\n",
768 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
769 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800770 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700771 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700772
NeilBrowna9701a32005-11-08 21:39:34 -0800773 if (atomic_dec_and_test(&mddev->pending_writes))
774 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700775 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700776}
777
NeilBrownfd01b882011-10-11 16:47:53 +1100778void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700779 sector_t sector, int size, struct page *page)
780{
781 /* write first size bytes of page to sector of rdev
782 * Increment mddev->pending_writes before returning
783 * and decrement it on completion, waking up sb_wait
784 * if zero is reached.
785 * If an error occurred, call md_error
786 */
NeilBrowna167f662010-10-26 18:31:13 +1100787 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700788
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100789 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790 bio->bi_sector = sector;
791 bio_add_page(bio, page, size, 0);
792 bio->bi_private = rdev;
793 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800794
NeilBrown7bfa19f2005-06-21 17:17:28 -0700795 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000796 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800797}
798
NeilBrownfd01b882011-10-11 16:47:53 +1100799void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800800{
Tejun Heoe9c74692010-09-03 11:56:18 +0200801 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800802 DEFINE_WAIT(wq);
803 for(;;) {
804 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
805 if (atomic_read(&mddev->pending_writes)==0)
806 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800807 schedule();
808 }
809 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700810}
811
NeilBrown6712ecf2007-09-27 12:47:43 +0200812static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
NeilBrown3cb03002011-10-11 16:45:26 +1100817int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100818 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
NeilBrowna167f662010-10-26 18:31:13 +1100820 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct completion event;
822 int ret;
823
Jens Axboe721a9602011-03-09 11:56:30 +0100824 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100826 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
827 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100828 if (metadata_op)
829 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000830 else if (rdev->mddev->reshape_position != MaxSector &&
831 (rdev->mddev->reshape_backwards ==
832 (sector >= rdev->mddev->reshape_position)))
833 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100834 else
835 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 bio_add_page(bio, page, size, 0);
837 init_completion(&event);
838 bio->bi_private = &event;
839 bio->bi_end_io = bi_complete;
840 submit_bio(rw, bio);
841 wait_for_completion(&event);
842
843 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
844 bio_put(bio);
845 return ret;
846}
NeilBrowna8745db2006-01-06 00:20:34 -0800847EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
NeilBrown3cb03002011-10-11 16:45:26 +1100849static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 char b[BDEVNAME_SIZE];
852 if (!rdev->sb_page) {
853 MD_BUG();
854 return -EINVAL;
855 }
856 if (rdev->sb_loaded)
857 return 0;
858
859
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100860 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto fail;
862 rdev->sb_loaded = 1;
863 return 0;
864
865fail:
866 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
867 bdevname(rdev->bdev,b));
868 return -EINVAL;
869}
870
871static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
872{
Andre Noll05710462008-07-11 22:02:20 +1000873 return sb1->set_uuid0 == sb2->set_uuid0 &&
874 sb1->set_uuid1 == sb2->set_uuid1 &&
875 sb1->set_uuid2 == sb2->set_uuid2 &&
876 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
880{
881 int ret;
882 mdp_super_t *tmp1, *tmp2;
883
884 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
885 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
886
887 if (!tmp1 || !tmp2) {
888 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100889 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 goto abort;
891 }
892
893 *tmp1 = *sb1;
894 *tmp2 = *sb2;
895
896 /*
897 * nr_disks is not constant
898 */
899 tmp1->nr_disks = 0;
900 tmp2->nr_disks = 0;
901
Andre Nollce0c8e02008-07-11 22:02:20 +1000902 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700904 kfree(tmp1);
905 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return ret;
907}
908
NeilBrown4d167f02007-05-09 02:35:37 -0700909
910static u32 md_csum_fold(u32 csum)
911{
912 csum = (csum & 0xffff) + (csum >> 16);
913 return (csum & 0xffff) + (csum >> 16);
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static unsigned int calc_sb_csum(mdp_super_t * sb)
917{
NeilBrown4d167f02007-05-09 02:35:37 -0700918 u64 newcsum = 0;
919 u32 *sb32 = (u32*)sb;
920 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned int disk_csum, csum;
922
923 disk_csum = sb->sb_csum;
924 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700925
926 for (i = 0; i < MD_SB_BYTES/4 ; i++)
927 newcsum += sb32[i];
928 csum = (newcsum & 0xffffffff) + (newcsum>>32);
929
930
931#ifdef CONFIG_ALPHA
932 /* This used to use csum_partial, which was wrong for several
933 * reasons including that different results are returned on
934 * different architectures. It isn't critical that we get exactly
935 * the same return value as before (we always csum_fold before
936 * testing, and that removes any differences). However as we
937 * know that csum_partial always returned a 16bit value on
938 * alphas, do a fold to maximise conformity to previous behaviour.
939 */
940 sb->sb_csum = md_csum_fold(disk_csum);
941#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return csum;
945}
946
947
948/*
949 * Handle superblock details.
950 * We want to be able to handle multiple superblock formats
951 * so we have a common interface to them all, and an array of
952 * different handlers.
953 * We rely on user-space to write the initial superblock, and support
954 * reading and updating of superblocks.
955 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100956 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * loads and validates a superblock on dev.
958 * if refdev != NULL, compare superblocks on both devices
959 * Return:
960 * 0 - dev has a superblock that is compatible with refdev
961 * 1 - dev has a superblock that is compatible and newer than refdev
962 * so dev should be used as the refdev in future
963 * -EINVAL superblock incompatible or invalid
964 * -othererror e.g. -EIO
965 *
NeilBrownfd01b882011-10-11 16:47:53 +1100966 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * Verify that dev is acceptable into mddev.
968 * The first time, mddev->raid_disks will be 0, and data from
969 * dev should be merged in. Subsequent calls check that dev
970 * is new enough. Return 0 or -EINVAL
971 *
NeilBrownfd01b882011-10-11 16:47:53 +1100972 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * Update the superblock for rdev with data in mddev
974 * This does not write to disc.
975 *
976 */
977
978struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000979 char *name;
980 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000981 int (*load_super)(struct md_rdev *rdev,
982 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000983 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000984 int (*validate_super)(struct mddev *mddev,
985 struct md_rdev *rdev);
986 void (*sync_super)(struct mddev *mddev,
987 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100988 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000989 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000990 int (*allow_new_offset)(struct md_rdev *rdev,
991 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992};
993
994/*
Andre Noll0894cc32009-06-18 08:49:23 +1000995 * Check that the given mddev has no bitmap.
996 *
997 * This function is called from the run method of all personalities that do not
998 * support bitmaps. It prints an error message and returns non-zero if mddev
999 * has a bitmap. Otherwise, it returns 0.
1000 *
1001 */
NeilBrownfd01b882011-10-11 16:47:53 +11001002int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001003{
NeilBrownc3d97142009-12-14 12:49:52 +11001004 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001005 return 0;
1006 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1007 mdname(mddev), mddev->pers->name);
1008 return 1;
1009}
1010EXPORT_SYMBOL(md_check_no_bitmap);
1011
1012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * load_super for 0.90.0
1014 */
NeilBrown3cb03002011-10-11 16:45:26 +11001015static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1018 mdp_super_t *sb;
1019 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /*
Andre Noll0f420352008-07-11 22:02:23 +10001022 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * it's at the end of the disk.
1024 *
1025 * It also happens to be a multiple of 4Kb.
1026 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001027 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
NeilBrown0002b272005-09-09 16:23:53 -07001029 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (ret) return ret;
1031
1032 ret = -EINVAL;
1033
1034 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001035 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (sb->md_magic != MD_SB_MAGIC) {
1038 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1039 b);
1040 goto abort;
1041 }
1042
1043 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001044 sb->minor_version < 90 ||
1045 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1047 sb->major_version, sb->minor_version,
1048 b);
1049 goto abort;
1050 }
1051
1052 if (sb->raid_disks <= 0)
1053 goto abort;
1054
NeilBrown4d167f02007-05-09 02:35:37 -07001055 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1057 b);
1058 goto abort;
1059 }
1060
1061 rdev->preferred_minor = sb->md_minor;
1062 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001063 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001064 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001065 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (sb->level == LEVEL_MULTIPATH)
1068 rdev->desc_nr = -1;
1069 else
1070 rdev->desc_nr = sb->this_disk.number;
1071
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001072 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001076 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!uuid_equal(refsb, sb)) {
1078 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1079 b, bdevname(refdev->bdev,b2));
1080 goto abort;
1081 }
1082 if (!sb_equal(refsb, sb)) {
1083 printk(KERN_WARNING "md: %s has same UUID"
1084 " but different superblock to %s\n",
1085 b, bdevname(refdev->bdev, b2));
1086 goto abort;
1087 }
1088 ev1 = md_event(sb);
1089 ev2 = md_event(refsb);
1090 if (ev1 > ev2)
1091 ret = 1;
1092 else
1093 ret = 0;
1094 }
NeilBrown8190e752009-06-18 08:48:58 +10001095 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001096 /* Limit to 4TB as metadata cannot record more than that.
1097 * (not needed for Linear and RAID0 as metadata doesn't
1098 * record this size)
1099 */
1100 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001101 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
NeilBrown27a7b262011-09-10 17:21:28 +10001103 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001104 /* "this cannot possibly happen" ... */
1105 ret = -EINVAL;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 abort:
1108 return ret;
1109}
1110
1111/*
1112 * validate_super for 0.90.0
1113 */
NeilBrownfd01b882011-10-11 16:47:53 +11001114static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001117 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001118 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
NeilBrown41158c72005-06-21 17:17:25 -07001120 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001121 clear_bit(Faulty, &rdev->flags);
1122 clear_bit(In_sync, &rdev->flags);
1123 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (mddev->raid_disks == 0) {
1126 mddev->major_version = 0;
1127 mddev->minor_version = sb->minor_version;
1128 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001129 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001130 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 mddev->ctime = sb->ctime;
1132 mddev->utime = sb->utime;
1133 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001134 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 mddev->layout = sb->layout;
1136 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001137 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001138 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001139 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001140 mddev->bitmap_info.space = 0;
1141 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001142 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001143 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001144 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
NeilBrownf6705572006-03-27 01:18:11 -08001146 if (mddev->minor_version >= 91) {
1147 mddev->reshape_position = sb->reshape_position;
1148 mddev->delta_disks = sb->delta_disks;
1149 mddev->new_level = sb->new_level;
1150 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001151 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001152 if (mddev->delta_disks < 0)
1153 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001154 } else {
1155 mddev->reshape_position = MaxSector;
1156 mddev->delta_disks = 0;
1157 mddev->new_level = mddev->level;
1158 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001159 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001160 }
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (sb->state & (1<<MD_SB_CLEAN))
1163 mddev->recovery_cp = MaxSector;
1164 else {
1165 if (sb->events_hi == sb->cp_events_hi &&
1166 sb->events_lo == sb->cp_events_lo) {
1167 mddev->recovery_cp = sb->recovery_cp;
1168 } else
1169 mddev->recovery_cp = 0;
1170 }
1171
1172 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1173 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1174 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1175 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1176
1177 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001178
1179 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001180 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001181 mddev->bitmap_info.offset =
1182 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001183 mddev->bitmap_info.space =
1184 mddev->bitmap_info.space;
1185 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001186
NeilBrown41158c72005-06-21 17:17:25 -07001187 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001188 /* Insist on good event counter while assembling, except
1189 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001191 if (sb->disks[rdev->desc_nr].state & (
1192 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1193 if (ev1 < mddev->events)
1194 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001195 } else if (mddev->bitmap) {
1196 /* if adding to array with a bitmap, then we can accept an
1197 * older device ... but not too old.
1198 */
NeilBrown41158c72005-06-21 17:17:25 -07001199 if (ev1 < mddev->bitmap->events_cleared)
1200 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001201 } else {
1202 if (ev1 < mddev->events)
1203 /* just a hot-add of a new device, leave raid_disk at -1 */
1204 return 0;
1205 }
NeilBrown41158c72005-06-21 17:17:25 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 desc = sb->disks + rdev->desc_nr;
1209
1210 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001211 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001212 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1213 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001214 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001216 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1217 /* active but not in sync implies recovery up to
1218 * reshape position. We don't know exactly where
1219 * that is, so set to zero for now */
1220 if (mddev->minor_version >= 91) {
1221 rdev->recovery_offset = 0;
1222 rdev->raid_disk = desc->raid_disk;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001225 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1226 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001227 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001228 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return 0;
1230}
1231
1232/*
1233 * sync_super for 0.90.0
1234 */
NeilBrownfd01b882011-10-11 16:47:53 +11001235static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001238 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* make rdev->sb match mddev data..
1243 *
1244 * 1/ zero out disks
1245 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1246 * 3/ any empty disks < next_spare become removed
1247 *
1248 * disks[0] gets initialised to REMOVED because
1249 * we cannot be sure from other fields if it has
1250 * been initialised or not.
1251 */
1252 int i;
1253 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1254
NeilBrown61181562005-09-09 16:24:02 -07001255 rdev->sb_size = MD_SB_BYTES;
1256
Namhyung Kim65a06f062011-07-27 11:00:36 +10001257 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 memset(sb, 0, sizeof(*sb));
1260
1261 sb->md_magic = MD_SB_MAGIC;
1262 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sb->patch_version = mddev->patch_version;
1264 sb->gvalid_words = 0; /* ignored */
1265 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1266 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1267 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1268 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1269
1270 sb->ctime = mddev->ctime;
1271 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001272 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 sb->raid_disks = mddev->raid_disks;
1274 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001275 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sb->utime = mddev->utime;
1277 sb->state = 0;
1278 sb->events_hi = (mddev->events>>32);
1279 sb->events_lo = (u32)mddev->events;
1280
NeilBrownf6705572006-03-27 01:18:11 -08001281 if (mddev->reshape_position == MaxSector)
1282 sb->minor_version = 90;
1283 else {
1284 sb->minor_version = 91;
1285 sb->reshape_position = mddev->reshape_position;
1286 sb->new_level = mddev->new_level;
1287 sb->delta_disks = mddev->delta_disks;
1288 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001289 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001290 }
1291 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (mddev->in_sync)
1293 {
1294 sb->recovery_cp = mddev->recovery_cp;
1295 sb->cp_events_hi = (mddev->events>>32);
1296 sb->cp_events_lo = (u32)mddev->events;
1297 if (mddev->recovery_cp == MaxSector)
1298 sb->state = (1<< MD_SB_CLEAN);
1299 } else
1300 sb->recovery_cp = 0;
1301
1302 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001303 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
NeilBrownc3d97142009-12-14 12:49:52 +11001305 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001306 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001309 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001311 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001312 int is_active = test_bit(In_sync, &rdev2->flags);
1313
1314 if (rdev2->raid_disk >= 0 &&
1315 sb->minor_version >= 91)
1316 /* we have nowhere to store the recovery_offset,
1317 * but if it is not below the reshape_position,
1318 * we can piggy-back on that.
1319 */
1320 is_active = 1;
1321 if (rdev2->raid_disk < 0 ||
1322 test_bit(Faulty, &rdev2->flags))
1323 is_active = 0;
1324 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001325 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001327 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001328 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 d = &sb->disks[rdev2->desc_nr];
1330 nr_disks++;
1331 d->number = rdev2->desc_nr;
1332 d->major = MAJOR(rdev2->bdev->bd_dev);
1333 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001334 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 d->raid_disk = rdev2->raid_disk;
1336 else
1337 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001338 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001340 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001342 if (test_bit(In_sync, &rdev2->flags))
1343 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 active++;
1345 working++;
1346 } else {
1347 d->state = 0;
1348 spare++;
1349 working++;
1350 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001351 if (test_bit(WriteMostly, &rdev2->flags))
1352 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* now set the "removed" and "faulty" bits on any missing devices */
1355 for (i=0 ; i < mddev->raid_disks ; i++) {
1356 mdp_disk_t *d = &sb->disks[i];
1357 if (d->state == 0 && d->number == 0) {
1358 d->number = i;
1359 d->raid_disk = i;
1360 d->state = (1<<MD_DISK_REMOVED);
1361 d->state |= (1<<MD_DISK_FAULTY);
1362 failed++;
1363 }
1364 }
1365 sb->nr_disks = nr_disks;
1366 sb->active_disks = active;
1367 sb->working_disks = working;
1368 sb->failed_disks = failed;
1369 sb->spare_disks = spare;
1370
1371 sb->this_disk = sb->disks[rdev->desc_nr];
1372 sb->sb_csum = calc_sb_csum(sb);
1373}
1374
1375/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001376 * rdev_size_change for 0.90.0
1377 */
1378static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001379super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001380{
Andre Noll58c0fed2009-03-31 14:33:13 +11001381 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001382 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001383 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001384 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001385 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001386 if (!num_sectors || num_sectors > rdev->sb_start)
1387 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001388 /* Limit to 4TB as metadata cannot record more than that.
1389 * 4TB == 2^32 KB, or 2*2^32 sectors.
1390 */
NeilBrown667a5312012-08-16 16:46:12 +10001391 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001392 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001393 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001394 rdev->sb_page);
1395 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001396 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001397}
1398
NeilBrownc6563a82012-05-21 09:27:00 +10001399static int
1400super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1401{
1402 /* non-zero offset changes not possible with v0.90 */
1403 return new_offset == 0;
1404}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001405
1406/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * version 1 superblock
1408 */
1409
NeilBrown1c05b4b2006-10-21 10:24:08 -07001410static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001412 __le32 disk_csum;
1413 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 unsigned long long newcsum;
1415 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001416 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 disk_csum = sb->sb_csum;
1419 sb->sb_csum = 0;
1420 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001421 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 newcsum += le32_to_cpu(*isuper++);
1423
1424 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001425 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1428 sb->sb_csum = disk_csum;
1429 return cpu_to_le32(csum);
1430}
1431
NeilBrown2699b672011-07-28 11:31:47 +10001432static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1433 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001434static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 struct mdp_superblock_1 *sb;
1437 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001438 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001439 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001441 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
Andre Noll0f420352008-07-11 22:02:23 +10001444 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * It is always aligned to a 4K boundary and
1446 * depeding on minor_version, it can be:
1447 * 0: At least 8K, but less than 12K, from end of device
1448 * 1: At start of device
1449 * 2: 4K from start of device.
1450 */
1451 switch(minor_version) {
1452 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001453 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001454 sb_start -= 8*2;
1455 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001458 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001461 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 break;
1463 default:
1464 return -EINVAL;
1465 }
Andre Noll0f420352008-07-11 22:02:23 +10001466 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
NeilBrown0002b272005-09-09 16:23:53 -07001468 /* superblock is rarely larger than 1K, but it can be larger,
1469 * and it is safe to read 4k, so we do that
1470 */
1471 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (ret) return ret;
1473
1474
Namhyung Kim65a06f062011-07-27 11:00:36 +10001475 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1478 sb->major_version != cpu_to_le32(1) ||
1479 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001480 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001481 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EINVAL;
1483
1484 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1485 printk("md: invalid superblock checksum on %s\n",
1486 bdevname(rdev->bdev,b));
1487 return -EINVAL;
1488 }
1489 if (le64_to_cpu(sb->data_size) < 10) {
1490 printk("md: data_size too small on %s\n",
1491 bdevname(rdev->bdev,b));
1492 return -EINVAL;
1493 }
NeilBrownc6563a82012-05-21 09:27:00 +10001494 if (sb->pad0 ||
1495 sb->pad3[0] ||
1496 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1497 /* Some padding is non-zero, might be a new feature */
1498 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 rdev->preferred_minor = 0xffff;
1501 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001502 rdev->new_data_offset = rdev->data_offset;
1503 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1504 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1505 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001506 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
NeilBrown0002b272005-09-09 16:23:53 -07001508 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001509 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001510 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001511 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1512
1513 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001514 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001515 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001516 if (minor_version
1517 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1518 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001519
NeilBrown31b65a02006-07-10 04:44:14 -07001520 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1521 rdev->desc_nr = -1;
1522 else
1523 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1524
NeilBrown2699b672011-07-28 11:31:47 +10001525 if (!rdev->bb_page) {
1526 rdev->bb_page = alloc_page(GFP_KERNEL);
1527 if (!rdev->bb_page)
1528 return -ENOMEM;
1529 }
1530 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1531 rdev->badblocks.count == 0) {
1532 /* need to load the bad block list.
1533 * Currently we limit it to one page.
1534 */
1535 s32 offset;
1536 sector_t bb_sector;
1537 u64 *bbp;
1538 int i;
1539 int sectors = le16_to_cpu(sb->bblog_size);
1540 if (sectors > (PAGE_SIZE / 512))
1541 return -EINVAL;
1542 offset = le32_to_cpu(sb->bblog_offset);
1543 if (offset == 0)
1544 return -EINVAL;
1545 bb_sector = (long long)offset;
1546 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1547 rdev->bb_page, READ, true))
1548 return -EIO;
1549 bbp = (u64 *)page_address(rdev->bb_page);
1550 rdev->badblocks.shift = sb->bblog_shift;
1551 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1552 u64 bb = le64_to_cpu(*bbp);
1553 int count = bb & (0x3ff);
1554 u64 sector = bb >> 10;
1555 sector <<= sb->bblog_shift;
1556 count <<= sb->bblog_shift;
1557 if (bb + 1 == 0)
1558 break;
1559 if (md_set_badblocks(&rdev->badblocks,
1560 sector, count, 1) == 0)
1561 return -EINVAL;
1562 }
1563 } else if (sb->bblog_offset == 0)
1564 rdev->badblocks.shift = -1;
1565
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001566 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001567 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001568 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001570 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1573 sb->level != refsb->level ||
1574 sb->layout != refsb->layout ||
1575 sb->chunksize != refsb->chunksize) {
1576 printk(KERN_WARNING "md: %s has strangely different"
1577 " superblock to %s\n",
1578 bdevname(rdev->bdev,b),
1579 bdevname(refdev->bdev,b2));
1580 return -EINVAL;
1581 }
1582 ev1 = le64_to_cpu(sb->events);
1583 ev2 = le64_to_cpu(refsb->events);
1584
1585 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001586 ret = 1;
1587 else
1588 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
NeilBrownc6563a82012-05-21 09:27:00 +10001590 if (minor_version) {
1591 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1592 sectors -= rdev->data_offset;
1593 } else
1594 sectors = rdev->sb_start;
1595 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001597 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
NeilBrownfd01b882011-10-11 16:47:53 +11001601static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001603 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001604 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
NeilBrown41158c72005-06-21 17:17:25 -07001606 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001607 clear_bit(Faulty, &rdev->flags);
1608 clear_bit(In_sync, &rdev->flags);
1609 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (mddev->raid_disks == 0) {
1612 mddev->major_version = 1;
1613 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001614 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001615 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1617 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1618 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001619 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 mddev->layout = le32_to_cpu(sb->layout);
1621 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001622 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001623 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001624 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001625 mddev->bitmap_info.space = 0;
1626 /* Default location for bitmap is 1K after superblock
1627 * using 3K - total of 4K
1628 */
NeilBrownc3d97142009-12-14 12:49:52 +11001629 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001630 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001631 mddev->reshape_backwards = 0;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1634 memcpy(mddev->uuid, sb->set_uuid, 16);
1635
1636 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001637
NeilBrown71c08052005-09-09 16:23:51 -07001638 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001639 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001640 mddev->bitmap_info.offset =
1641 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001642 /* Metadata doesn't record how much space is available.
1643 * For 1.0, we assume we can use up to the superblock
1644 * if before, else to 4K beyond superblock.
1645 * For others, assume no change is possible.
1646 */
1647 if (mddev->minor_version > 0)
1648 mddev->bitmap_info.space = 0;
1649 else if (mddev->bitmap_info.offset > 0)
1650 mddev->bitmap_info.space =
1651 8 - mddev->bitmap_info.offset;
1652 else
1653 mddev->bitmap_info.space =
1654 -mddev->bitmap_info.offset;
1655 }
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);
NeilBrown2c810cd2012-05-21 09:27:00 +10001663 if (mddev->delta_disks < 0 ||
1664 (mddev->delta_disks == 0 &&
1665 (le32_to_cpu(sb->feature_map)
1666 & MD_FEATURE_RESHAPE_BACKWARDS)))
1667 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001668 } else {
1669 mddev->reshape_position = MaxSector;
1670 mddev->delta_disks = 0;
1671 mddev->new_level = mddev->level;
1672 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001673 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001674 }
1675
NeilBrown41158c72005-06-21 17:17:25 -07001676 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001677 /* Insist of good event counter while assembling, except for
1678 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001680 if (rdev->desc_nr >= 0 &&
1681 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1682 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1683 if (ev1 < mddev->events)
1684 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001685 } else if (mddev->bitmap) {
1686 /* If adding to array with a bitmap, then we can accept an
1687 * older device, but not too old.
1688 */
NeilBrown41158c72005-06-21 17:17:25 -07001689 if (ev1 < mddev->bitmap->events_cleared)
1690 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001691 } else {
1692 if (ev1 < mddev->events)
1693 /* just a hot-add of a new device, leave raid_disk at -1 */
1694 return 0;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (mddev->level != LEVEL_MULTIPATH) {
1697 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001698 if (rdev->desc_nr < 0 ||
1699 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1700 role = 0xffff;
1701 rdev->desc_nr = -1;
1702 } else
1703 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 switch(role) {
1705 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 break;
1707 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001708 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001711 if ((le32_to_cpu(sb->feature_map) &
1712 MD_FEATURE_RECOVERY_OFFSET))
1713 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1714 else
1715 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rdev->raid_disk = role;
1717 break;
1718 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001719 if (sb->devflags & WriteMostly1)
1720 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001721 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1722 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001723 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001724 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727}
1728
NeilBrownfd01b882011-10-11 16:47:53 +11001729static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
1731 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001732 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 int max_dev, i;
1734 /* make rdev->sb match mddev and rdev data. */
1735
Namhyung Kim65a06f062011-07-27 11:00:36 +10001736 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 sb->feature_map = 0;
1739 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001740 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 memset(sb->pad3, 0, sizeof(sb->pad3));
1742
1743 sb->utime = cpu_to_le64((__u64)mddev->utime);
1744 sb->events = cpu_to_le64(mddev->events);
1745 if (mddev->in_sync)
1746 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1747 else
1748 sb->resync_offset = cpu_to_le64(0);
1749
NeilBrown1c05b4b2006-10-21 10:24:08 -07001750 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001751
NeilBrownf0ca3402006-02-02 14:28:04 -08001752 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001753 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001754 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001755 sb->level = cpu_to_le32(mddev->level);
1756 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001757
NeilBrownaeb9b212011-08-25 14:43:08 +10001758 if (test_bit(WriteMostly, &rdev->flags))
1759 sb->devflags |= WriteMostly1;
1760 else
1761 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001762 sb->data_offset = cpu_to_le64(rdev->data_offset);
1763 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001764
NeilBrownc3d97142009-12-14 12:49:52 +11001765 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1766 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001767 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001768 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001769
1770 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001771 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001772 sb->feature_map |=
1773 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1774 sb->recovery_offset =
1775 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001776 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001777 if (test_bit(Replacement, &rdev->flags))
1778 sb->feature_map |=
1779 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001780
NeilBrownf6705572006-03-27 01:18:11 -08001781 if (mddev->reshape_position != MaxSector) {
1782 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1783 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1784 sb->new_layout = cpu_to_le32(mddev->new_layout);
1785 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1786 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001787 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001788 if (mddev->delta_disks == 0 &&
1789 mddev->reshape_backwards)
1790 sb->feature_map
1791 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001792 if (rdev->new_data_offset != rdev->data_offset) {
1793 sb->feature_map
1794 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1795 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1796 - rdev->data_offset));
1797 }
NeilBrownf6705572006-03-27 01:18:11 -08001798 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001799
NeilBrown2699b672011-07-28 11:31:47 +10001800 if (rdev->badblocks.count == 0)
1801 /* Nothing to do for bad blocks*/ ;
1802 else if (sb->bblog_offset == 0)
1803 /* Cannot record bad blocks on this device */
1804 md_error(mddev, rdev);
1805 else {
1806 struct badblocks *bb = &rdev->badblocks;
1807 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1808 u64 *p = bb->page;
1809 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1810 if (bb->changed) {
1811 unsigned seq;
1812
1813retry:
1814 seq = read_seqbegin(&bb->lock);
1815
1816 memset(bbp, 0xff, PAGE_SIZE);
1817
1818 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001819 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001820 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1821 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001822 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001823 }
NeilBrownd0962932012-03-19 12:46:41 +11001824 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001825 if (read_seqretry(&bb->lock, seq))
1826 goto retry;
1827
1828 bb->sector = (rdev->sb_start +
1829 (int)le32_to_cpu(sb->bblog_offset));
1830 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001831 }
1832 }
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001835 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (rdev2->desc_nr+1 > max_dev)
1837 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001838
NeilBrown70471da2009-08-03 10:59:57 +10001839 if (max_dev > le32_to_cpu(sb->max_dev)) {
1840 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001841 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001842 rdev->sb_size = max_dev * 2 + 256;
1843 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1844 if (rdev->sb_size & bmask)
1845 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001846 } else
1847 max_dev = le32_to_cpu(sb->max_dev);
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 for (i=0; i<max_dev;i++)
1850 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1851
NeilBrowndafb20f2012-03-19 12:46:39 +11001852 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001854 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001856 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001858 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001859 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 else
1861 sb->dev_roles[i] = cpu_to_le16(0xffff);
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 sb->sb_csum = calc_sb_1_csum(sb);
1865}
1866
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001868super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001869{
1870 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001871 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001872 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001874 if (rdev->data_offset != rdev->new_data_offset)
1875 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001876 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001878 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001879 max_sectors -= rdev->data_offset;
1880 if (!num_sectors || num_sectors > max_sectors)
1881 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001882 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883 /* minor version 0 with bitmap we can't move */
1884 return 0;
1885 } else {
1886 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001887 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001888 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001889 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001890 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001891 if (!num_sectors || num_sectors > max_sectors)
1892 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001893 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001895 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001896 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001897 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001899 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 rdev->sb_page);
1901 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001902 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001903
1904}
1905
1906static int
1907super_1_allow_new_offset(struct md_rdev *rdev,
1908 unsigned long long new_offset)
1909{
1910 /* All necessary checks on new >= old have been done */
1911 struct bitmap *bitmap;
1912 if (new_offset >= rdev->data_offset)
1913 return 1;
1914
1915 /* with 1.0 metadata, there is no metadata to tread on
1916 * so we can always move back */
1917 if (rdev->mddev->minor_version == 0)
1918 return 1;
1919
1920 /* otherwise we must be sure not to step on
1921 * any metadata, so stay:
1922 * 36K beyond start of superblock
1923 * beyond end of badblocks
1924 * beyond write-intent bitmap
1925 */
1926 if (rdev->sb_start + (32+4)*2 > new_offset)
1927 return 0;
1928 bitmap = rdev->mddev->bitmap;
1929 if (bitmap && !rdev->mddev->bitmap_info.file &&
1930 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001931 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001932 return 0;
1933 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1934 return 0;
1935
1936 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Adrian Bunk75c96f82005-05-05 16:16:09 -07001939static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 [0] = {
1941 .name = "0.90.0",
1942 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001943 .load_super = super_90_load,
1944 .validate_super = super_90_validate,
1945 .sync_super = super_90_sync,
1946 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001947 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 },
1949 [1] = {
1950 .name = "md-1",
1951 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001952 .load_super = super_1_load,
1953 .validate_super = super_1_validate,
1954 .sync_super = super_1_sync,
1955 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001956 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 },
1958};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
NeilBrownfd01b882011-10-11 16:47:53 +11001960static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001961{
1962 if (mddev->sync_super) {
1963 mddev->sync_super(mddev, rdev);
1964 return;
1965 }
1966
1967 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1968
1969 super_types[mddev->major_version].sync_super(mddev, rdev);
1970}
1971
NeilBrownfd01b882011-10-11 16:47:53 +11001972static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
NeilBrown3cb03002011-10-11 16:45:26 +11001974 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
NeilBrown4b809912008-07-21 17:05:25 +10001976 rcu_read_lock();
1977 rdev_for_each_rcu(rdev, mddev1)
1978 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001979 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001980 rdev2->bdev->bd_contains) {
1981 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001982 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001983 }
1984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return 0;
1986}
1987
1988static LIST_HEAD(pending_raid_disks);
1989
Andre Nollac5e7112009-08-03 10:59:47 +10001990/*
1991 * Try to register data integrity profile for an mddev
1992 *
1993 * This is called when an array is started and after a disk has been kicked
1994 * from the array. It only succeeds if all working and active component devices
1995 * are integrity capable with matching profiles.
1996 */
NeilBrownfd01b882011-10-11 16:47:53 +11001997int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001998{
NeilBrown3cb03002011-10-11 16:45:26 +11001999 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002000
Andre Nollac5e7112009-08-03 10:59:47 +10002001 if (list_empty(&mddev->disks))
2002 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002003 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2004 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002005 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002006 /* skip spares and non-functional disks */
2007 if (test_bit(Faulty, &rdev->flags))
2008 continue;
2009 if (rdev->raid_disk < 0)
2010 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002011 if (!reference) {
2012 /* Use the first rdev as the reference */
2013 reference = rdev;
2014 continue;
2015 }
2016 /* does this rdev's profile match the reference profile? */
2017 if (blk_integrity_compare(reference->bdev->bd_disk,
2018 rdev->bdev->bd_disk) < 0)
2019 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002020 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002021 if (!reference || !bdev_get_integrity(reference->bdev))
2022 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002023 /*
2024 * All component devices are integrity capable and have matching
2025 * profiles, register the common profile for the md device.
2026 */
2027 if (blk_integrity_register(mddev->gendisk,
2028 bdev_get_integrity(reference->bdev)) != 0) {
2029 printk(KERN_ERR "md: failed to register integrity for %s\n",
2030 mdname(mddev));
2031 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002032 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002033 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2034 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2035 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2036 mdname(mddev));
2037 return -EINVAL;
2038 }
Andre Nollac5e7112009-08-03 10:59:47 +10002039 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002040}
Andre Nollac5e7112009-08-03 10:59:47 +10002041EXPORT_SYMBOL(md_integrity_register);
2042
2043/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002044void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002045{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002046 struct blk_integrity *bi_rdev;
2047 struct blk_integrity *bi_mddev;
2048
2049 if (!mddev->gendisk)
2050 return;
2051
2052 bi_rdev = bdev_get_integrity(rdev->bdev);
2053 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002054
2055 if (!bi_mddev) /* nothing to do */
2056 return;
2057 if (rdev->raid_disk < 0) /* skip spares */
2058 return;
2059 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2060 rdev->bdev->bd_disk) >= 0)
2061 return;
2062 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2063 blk_integrity_unregister(mddev->gendisk);
2064}
2065EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002066
NeilBrownfd01b882011-10-11 16:47:53 +11002067static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002069 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002070 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002071 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002072 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 if (rdev->mddev) {
2075 MD_BUG();
2076 return -EINVAL;
2077 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002078
2079 /* prevent duplicates */
2080 if (find_rdev(mddev, rdev->bdev->bd_dev))
2081 return -EEXIST;
2082
Andre Nolldd8ac332009-03-31 14:33:13 +11002083 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2084 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2085 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002086 if (mddev->pers) {
2087 /* Cannot change size, so fail
2088 * If mddev->level <= 0, then we don't care
2089 * about aligning sizes (e.g. linear)
2090 */
2091 if (mddev->level > 0)
2092 return -ENOSPC;
2093 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002094 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /* Verify rdev->desc_nr is unique.
2098 * If it is -1, assign a free number, else
2099 * check number is not in use
2100 */
2101 if (rdev->desc_nr < 0) {
2102 int choice = 0;
2103 if (mddev->pers) choice = mddev->raid_disks;
2104 while (find_rdev_nr(mddev, choice))
2105 choice++;
2106 rdev->desc_nr = choice;
2107 } else {
2108 if (find_rdev_nr(mddev, rdev->desc_nr))
2109 return -EBUSY;
2110 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002111 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2112 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2113 mdname(mddev), mddev->max_disks);
2114 return -EBUSY;
2115 }
NeilBrown19133a42005-11-08 21:39:35 -08002116 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002117 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002118 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002121 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002122
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002123 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002124 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002125
Tejun Heo0762b8b2008-08-25 19:56:12 +09002126 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002127 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2128 /* failure here is OK */;
2129 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002130
NeilBrown4b809912008-07-21 17:05:25 +10002131 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002132 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002133
2134 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002135 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002138
2139 fail:
2140 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2141 b, mdname(mddev));
2142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
NeilBrown177a99b2008-02-06 01:39:56 -08002145static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002146{
NeilBrown3cb03002011-10-11 16:45:26 +11002147 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002148 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002149 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002150}
2151
NeilBrown3cb03002011-10-11 16:45:26 +11002152static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 char b[BDEVNAME_SIZE];
2155 if (!rdev->mddev) {
2156 MD_BUG();
2157 return;
2158 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002159 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002160 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2162 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002163 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002164 sysfs_put(rdev->sysfs_state);
2165 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002166 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002167 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002168 * writing to 'remove' to "dev/state". We also need
2169 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002170 */
NeilBrown4b809912008-07-21 17:05:25 +10002171 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002172 INIT_WORK(&rdev->del_work, md_delayed_delete);
2173 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002174 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177/*
2178 * prevent the device from being mounted, repartitioned or
2179 * otherwise reused by a RAID array (or any other kernel
2180 * subsystem), by bd_claiming the device.
2181 */
NeilBrown3cb03002011-10-11 16:45:26 +11002182static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 int err = 0;
2185 struct block_device *bdev;
2186 char b[BDEVNAME_SIZE];
2187
Tejun Heod4d77622010-11-13 11:55:18 +01002188 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002189 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (IS_ERR(bdev)) {
2191 printk(KERN_ERR "md: could not open %s.\n",
2192 __bdevname(dev, b));
2193 return PTR_ERR(bdev);
2194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 rdev->bdev = bdev;
2196 return err;
2197}
2198
NeilBrown3cb03002011-10-11 16:45:26 +11002199static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct block_device *bdev = rdev->bdev;
2202 rdev->bdev = NULL;
2203 if (!bdev)
2204 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002205 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
2208void md_autodetect_dev(dev_t dev);
2209
NeilBrown3cb03002011-10-11 16:45:26 +11002210static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 char b[BDEVNAME_SIZE];
2213 printk(KERN_INFO "md: export_rdev(%s)\n",
2214 bdevname(rdev->bdev,b));
2215 if (rdev->mddev)
2216 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002217 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002219 if (test_bit(AutoDetected, &rdev->flags))
2220 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221#endif
2222 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
NeilBrown3cb03002011-10-11 16:45:26 +11002226static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
2228 unbind_rdev_from_array(rdev);
2229 export_rdev(rdev);
2230}
2231
NeilBrownfd01b882011-10-11 16:47:53 +11002232static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
NeilBrown3cb03002011-10-11 16:45:26 +11002234 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
NeilBrowndafb20f2012-03-19 12:46:39 +11002236 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (!rdev->mddev) {
2238 MD_BUG();
2239 continue;
2240 }
2241 kick_rdev_from_array(rdev);
2242 }
2243 if (!list_empty(&mddev->disks))
2244 MD_BUG();
2245 mddev->raid_disks = 0;
2246 mddev->major_version = 0;
2247}
2248
2249static void print_desc(mdp_disk_t *desc)
2250{
2251 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2252 desc->major,desc->minor,desc->raid_disk,desc->state);
2253}
2254
Cheng Renquancd2ac932009-01-09 08:31:08 +11002255static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 int i;
2258
2259 printk(KERN_INFO
2260 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2261 sb->major_version, sb->minor_version, sb->patch_version,
2262 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2263 sb->ctime);
2264 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2265 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2266 sb->md_minor, sb->layout, sb->chunk_size);
2267 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2268 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2269 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2270 sb->failed_disks, sb->spare_disks,
2271 sb->sb_csum, (unsigned long)sb->events_lo);
2272
2273 printk(KERN_INFO);
2274 for (i = 0; i < MD_SB_DISKS; i++) {
2275 mdp_disk_t *desc;
2276
2277 desc = sb->disks + i;
2278 if (desc->number || desc->major || desc->minor ||
2279 desc->raid_disk || (desc->state && (desc->state != 4))) {
2280 printk(" D %2d: ", i);
2281 print_desc(desc);
2282 }
2283 }
2284 printk(KERN_INFO "md: THIS: ");
2285 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Cheng Renquancd2ac932009-01-09 08:31:08 +11002288static void print_sb_1(struct mdp_superblock_1 *sb)
2289{
2290 __u8 *uuid;
2291
2292 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002293 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002294 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002295 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002296 le32_to_cpu(sb->major_version),
2297 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002298 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002299 sb->set_name,
2300 (unsigned long long)le64_to_cpu(sb->ctime)
2301 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2302
2303 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002304 printk(KERN_INFO
2305 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002306 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002307 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002308 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2309 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002310 le32_to_cpu(sb->level),
2311 (unsigned long long)le64_to_cpu(sb->size),
2312 le32_to_cpu(sb->raid_disks),
2313 le32_to_cpu(sb->layout),
2314 le32_to_cpu(sb->chunksize),
2315 (unsigned long long)le64_to_cpu(sb->data_offset),
2316 (unsigned long long)le64_to_cpu(sb->data_size),
2317 (unsigned long long)le64_to_cpu(sb->super_offset),
2318 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2319 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002320 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002321 sb->devflags,
2322 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2323 (unsigned long long)le64_to_cpu(sb->events),
2324 (unsigned long long)le64_to_cpu(sb->resync_offset),
2325 le32_to_cpu(sb->sb_csum),
2326 le32_to_cpu(sb->max_dev)
2327 );
2328}
2329
NeilBrown3cb03002011-10-11 16:45:26 +11002330static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
2332 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002333 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2334 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002335 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2336 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002338 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2339 switch (major_version) {
2340 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002341 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002342 break;
2343 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002344 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002345 break;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 } else
2348 printk(KERN_INFO "md: no rdev superblock!\n");
2349}
2350
Adrian Bunk5e563412006-06-26 00:27:42 -07002351static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002353 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002354 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002355 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 char b[BDEVNAME_SIZE];
2357
2358 printk("\n");
2359 printk("md: **********************************\n");
2360 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2361 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002362 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
NeilBrown32a76272005-06-21 17:17:14 -07002364 if (mddev->bitmap)
2365 bitmap_print_sb(mddev->bitmap);
2366 else
2367 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002368 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 printk("<%s>", bdevname(rdev->bdev,b));
2370 printk("\n");
2371
NeilBrowndafb20f2012-03-19 12:46:39 +11002372 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002373 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 printk("md: **********************************\n");
2376 printk("\n");
2377}
2378
2379
NeilBrownfd01b882011-10-11 16:47:53 +11002380static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
NeilBrown42543762006-06-26 00:27:57 -07002382 /* Update each superblock (in-memory image), but
2383 * if we are allowed to, skip spares which already
2384 * have the right event counter, or have one earlier
2385 * (which would mean they aren't being marked as dirty
2386 * with the rest of the array)
2387 */
NeilBrown3cb03002011-10-11 16:45:26 +11002388 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002389 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002390 if (rdev->sb_events == mddev->events ||
2391 (nospares &&
2392 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002393 rdev->sb_events+1 == mddev->events)) {
2394 /* Don't update this superblock */
2395 rdev->sb_loaded = 2;
2396 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002397 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002398 rdev->sb_loaded = 1;
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401}
2402
NeilBrownfd01b882011-10-11 16:47:53 +11002403static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
NeilBrown3cb03002011-10-11 16:45:26 +11002405 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002406 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002407 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002408 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002411 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002412 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002413 if (rdev->raid_disk >= 0 &&
2414 mddev->delta_disks >= 0 &&
2415 !test_bit(In_sync, &rdev->flags) &&
2416 mddev->curr_resync_completed > rdev->recovery_offset)
2417 rdev->recovery_offset = mddev->curr_resync_completed;
2418
2419 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002420 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002421 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002422 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002423 if (!mddev->external) {
NeilBrownd97a41dc2010-10-28 17:30:20 +11002424 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002425 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002426 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002427 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002428 md_ack_all_badblocks(&rdev->badblocks);
2429 md_error(mddev, rdev);
2430 }
2431 clear_bit(Blocked, &rdev->flags);
2432 clear_bit(BlockedBadBlocks, &rdev->flags);
2433 wake_up(&rdev->blocked_wait);
2434 }
2435 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002436 wake_up(&mddev->sb_wait);
2437 return;
2438 }
2439
NeilBrowna9701a32005-11-08 21:39:34 -08002440 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002441
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002442 mddev->utime = get_seconds();
2443
NeilBrown850b2b42006-10-03 01:15:46 -07002444 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2445 force_change = 1;
2446 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2447 /* just a clean<-> dirty transition, possibly leave spares alone,
2448 * though if events isn't the right even/odd, we will have to do
2449 * spares after all
2450 */
2451 nospares = 1;
2452 if (force_change)
2453 nospares = 0;
2454 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002455 /* If the array is degraded, then skipping spares is both
2456 * dangerous and fairly pointless.
2457 * Dangerous because a device that was removed from the array
2458 * might have a event_count that still looks up-to-date,
2459 * so it can be re-added without a resync.
2460 * Pointless because if there are any spares to skip,
2461 * then a recovery will happen and soon that array won't
2462 * be degraded any more and the spare can go back to sleep then.
2463 */
NeilBrown850b2b42006-10-03 01:15:46 -07002464 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002465
NeilBrown06d91a52005-06-21 17:17:12 -07002466 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002467
2468 /* If this is just a dirty<->clean transition, and the array is clean
2469 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002470 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002471 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002472 && mddev->can_decrease_events
2473 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002474 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002475 mddev->can_decrease_events = 0;
2476 } else {
NeilBrown42543762006-06-26 00:27:57 -07002477 /* otherwise we have to go forward and ... */
2478 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002479 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 if (!mddev->events) {
2483 /*
2484 * oops, this 64-bit counter should never wrap.
2485 * Either we are in around ~1 trillion A.C., assuming
2486 * 1 reboot per second, or we have a bug:
2487 */
2488 MD_BUG();
2489 mddev->events --;
2490 }
NeilBrown2699b672011-07-28 11:31:47 +10002491
NeilBrowndafb20f2012-03-19 12:46:39 +11002492 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002493 if (rdev->badblocks.changed)
2494 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002495 if (test_bit(Faulty, &rdev->flags))
2496 set_bit(FaultRecorded, &rdev->flags);
2497 }
NeilBrown2699b672011-07-28 11:31:47 +10002498
NeilBrowne6910632008-02-06 01:39:51 -08002499 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002500 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
NeilBrown36a4e1f2011-10-07 14:23:17 +11002502 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2503 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
NeilBrown4ad13662007-07-17 04:06:13 -07002505 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002506 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002508
NeilBrown42543762006-06-26 00:27:57 -07002509 if (rdev->sb_loaded != 1)
2510 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002512 if (!test_bit(Faulty, &rdev->flags) &&
2513 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002514 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002515 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002516 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002517 pr_debug("md: (write) %s's sb offset: %llu\n",
2518 bdevname(rdev->bdev, b),
2519 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002520 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002521 if (rdev->badblocks.size) {
2522 md_super_write(mddev, rdev,
2523 rdev->badblocks.sector,
2524 rdev->badblocks.size << 9,
2525 rdev->bb_page);
2526 rdev->badblocks.size = 0;
2527 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002528
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002529 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002530 pr_debug("md: %s (skipping faulty)\n",
2531 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002532 else
2533 pr_debug("(skipping incremental s/r ");
2534
NeilBrown7bfa19f2005-06-21 17:17:28 -07002535 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* only need to write one superblock... */
2537 break;
2538 }
NeilBrowna9701a32005-11-08 21:39:34 -08002539 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002540 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002541
NeilBrowna9701a32005-11-08 21:39:34 -08002542 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002543 if (mddev->in_sync != sync_req ||
2544 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002545 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002546 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002547 goto repeat;
2548 }
NeilBrown850b2b42006-10-03 01:15:46 -07002549 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002550 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002551 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002552 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2553 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002554
NeilBrowndafb20f2012-03-19 12:46:39 +11002555 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002556 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2557 clear_bit(Blocked, &rdev->flags);
2558
2559 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002560 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002561 clear_bit(BlockedBadBlocks, &rdev->flags);
2562 wake_up(&rdev->blocked_wait);
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Andre Noll7f6ce762008-03-23 18:34:54 +01002566/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002567 * We want to accept with case. For this we use cmd_match.
2568 */
2569static int cmd_match(const char *cmd, const char *str)
2570{
2571 /* See if cmd, written into a sysfs file, matches
2572 * str. They must either be the same, or cmd can
2573 * have a trailing newline
2574 */
2575 while (*cmd && *str && *cmd == *str) {
2576 cmd++;
2577 str++;
2578 }
2579 if (*cmd == '\n')
2580 cmd++;
2581 if (*str || *cmd)
2582 return 0;
2583 return 1;
2584}
2585
NeilBrown86e6ffd2005-11-08 21:39:24 -08002586struct rdev_sysfs_entry {
2587 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002588 ssize_t (*show)(struct md_rdev *, char *);
2589 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002590};
2591
2592static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002593state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002594{
2595 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002596 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002597
NeilBrownde393cd2011-07-28 11:31:48 +10002598 if (test_bit(Faulty, &rdev->flags) ||
2599 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600 len+= sprintf(page+len, "%sfaulty",sep);
2601 sep = ",";
2602 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002603 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604 len += sprintf(page+len, "%sin_sync",sep);
2605 sep = ",";
2606 }
NeilBrownf6556752006-06-26 00:28:01 -07002607 if (test_bit(WriteMostly, &rdev->flags)) {
2608 len += sprintf(page+len, "%swrite_mostly",sep);
2609 sep = ",";
2610 }
NeilBrownde393cd2011-07-28 11:31:48 +10002611 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002612 (rdev->badblocks.unacked_exist
2613 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002614 len += sprintf(page+len, "%sblocked", sep);
2615 sep = ",";
2616 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002617 if (!test_bit(Faulty, &rdev->flags) &&
2618 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002619 len += sprintf(page+len, "%sspare", sep);
2620 sep = ",";
2621 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002622 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2623 len += sprintf(page+len, "%swrite_error", sep);
2624 sep = ",";
2625 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002626 if (test_bit(WantReplacement, &rdev->flags)) {
2627 len += sprintf(page+len, "%swant_replacement", sep);
2628 sep = ",";
2629 }
2630 if (test_bit(Replacement, &rdev->flags)) {
2631 len += sprintf(page+len, "%sreplacement", sep);
2632 sep = ",";
2633 }
2634
NeilBrown86e6ffd2005-11-08 21:39:24 -08002635 return len+sprintf(page+len, "\n");
2636}
2637
NeilBrown45dc2de2006-06-26 00:27:58 -07002638static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002639state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002640{
2641 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002642 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002643 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002644 * writemostly - sets write_mostly
2645 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002646 * blocked - sets the Blocked flags
2647 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002648 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002649 * write_error - sets WriteErrorSeen
2650 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002651 */
2652 int err = -EINVAL;
2653 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2654 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002655 if (test_bit(Faulty, &rdev->flags))
2656 err = 0;
2657 else
2658 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002659 } else if (cmd_match(buf, "remove")) {
2660 if (rdev->raid_disk >= 0)
2661 err = -EBUSY;
2662 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002663 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002664 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002665 if (mddev->pers)
2666 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002667 md_new_event(mddev);
2668 err = 0;
2669 }
NeilBrownf6556752006-06-26 00:28:01 -07002670 } else if (cmd_match(buf, "writemostly")) {
2671 set_bit(WriteMostly, &rdev->flags);
2672 err = 0;
2673 } else if (cmd_match(buf, "-writemostly")) {
2674 clear_bit(WriteMostly, &rdev->flags);
2675 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002676 } else if (cmd_match(buf, "blocked")) {
2677 set_bit(Blocked, &rdev->flags);
2678 err = 0;
2679 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002680 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002681 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002682 /* metadata handler doesn't understand badblocks,
2683 * so we need to fail the device
2684 */
2685 md_error(rdev->mddev, rdev);
2686 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002687 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002688 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002689 wake_up(&rdev->blocked_wait);
2690 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2691 md_wakeup_thread(rdev->mddev->thread);
2692
2693 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002694 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2695 set_bit(In_sync, &rdev->flags);
2696 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002697 } else if (cmd_match(buf, "write_error")) {
2698 set_bit(WriteErrorSeen, &rdev->flags);
2699 err = 0;
2700 } else if (cmd_match(buf, "-write_error")) {
2701 clear_bit(WriteErrorSeen, &rdev->flags);
2702 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002703 } else if (cmd_match(buf, "want_replacement")) {
2704 /* Any non-spare device that is not a replacement can
2705 * become want_replacement at any time, but we then need to
2706 * check if recovery is needed.
2707 */
2708 if (rdev->raid_disk >= 0 &&
2709 !test_bit(Replacement, &rdev->flags))
2710 set_bit(WantReplacement, &rdev->flags);
2711 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2712 md_wakeup_thread(rdev->mddev->thread);
2713 err = 0;
2714 } else if (cmd_match(buf, "-want_replacement")) {
2715 /* Clearing 'want_replacement' is always allowed.
2716 * Once replacements starts it is too late though.
2717 */
2718 err = 0;
2719 clear_bit(WantReplacement, &rdev->flags);
2720 } else if (cmd_match(buf, "replacement")) {
2721 /* Can only set a device as a replacement when array has not
2722 * yet been started. Once running, replacement is automatic
2723 * from spares, or by assigning 'slot'.
2724 */
2725 if (rdev->mddev->pers)
2726 err = -EBUSY;
2727 else {
2728 set_bit(Replacement, &rdev->flags);
2729 err = 0;
2730 }
2731 } else if (cmd_match(buf, "-replacement")) {
2732 /* Similarly, can only clear Replacement before start */
2733 if (rdev->mddev->pers)
2734 err = -EBUSY;
2735 else {
2736 clear_bit(Replacement, &rdev->flags);
2737 err = 0;
2738 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002739 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002740 if (!err)
2741 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002742 return err ? err : len;
2743}
NeilBrown80ca3a42006-07-10 04:44:18 -07002744static struct rdev_sysfs_entry rdev_state =
2745__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002746
2747static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002748errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002749{
2750 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2751}
2752
2753static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002754errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002755{
2756 char *e;
2757 unsigned long n = simple_strtoul(buf, &e, 10);
2758 if (*buf && (*e == 0 || *e == '\n')) {
2759 atomic_set(&rdev->corrected_errors, n);
2760 return len;
2761 }
2762 return -EINVAL;
2763}
2764static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002765__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002766
NeilBrown014236d2006-01-06 00:20:55 -08002767static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002768slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002769{
2770 if (rdev->raid_disk < 0)
2771 return sprintf(page, "none\n");
2772 else
2773 return sprintf(page, "%d\n", rdev->raid_disk);
2774}
2775
2776static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002777slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002778{
2779 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002780 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002781 int slot = simple_strtoul(buf, &e, 10);
2782 if (strncmp(buf, "none", 4)==0)
2783 slot = -1;
2784 else if (e==buf || (*e && *e!= '\n'))
2785 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002786 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002787 /* Setting 'slot' on an active array requires also
2788 * updating the 'rd%d' link, and communicating
2789 * with the personality with ->hot_*_disk.
2790 * For now we only support removing
2791 * failed/spare devices. This normally happens automatically,
2792 * but not when the metadata is externally managed.
2793 */
NeilBrownc303da62008-02-06 01:39:51 -08002794 if (rdev->raid_disk == -1)
2795 return -EEXIST;
2796 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002797 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002798 return -EINVAL;
2799 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002800 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002801 if (err)
2802 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002803 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002804 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002805 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2806 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002807 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002809 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002810 */
2811
2812 if (rdev->raid_disk != -1)
2813 return -EBUSY;
2814
NeilBrownc6751b22011-02-02 11:57:13 +11002815 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2816 return -EBUSY;
2817
Neil Brown6c2fce22008-06-28 08:31:31 +10002818 if (rdev->mddev->pers->hot_add_disk == NULL)
2819 return -EINVAL;
2820
NeilBrownba1b41b2011-01-14 09:14:34 +11002821 if (slot >= rdev->mddev->raid_disks &&
2822 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2823 return -ENOSPC;
2824
Neil Brown6c2fce22008-06-28 08:31:31 +10002825 rdev->raid_disk = slot;
2826 if (test_bit(In_sync, &rdev->flags))
2827 rdev->saved_raid_disk = slot;
2828 else
2829 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002830 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002831 err = rdev->mddev->pers->
2832 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002833 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002834 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 return err;
Neil Brown52664732008-06-28 08:31:44 +10002836 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002837 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002838 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002839 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002840 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002841 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002842 if (slot >= rdev->mddev->raid_disks &&
2843 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002844 return -ENOSPC;
2845 rdev->raid_disk = slot;
2846 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847 clear_bit(Faulty, &rdev->flags);
2848 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002849 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002850 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002851 }
NeilBrown014236d2006-01-06 00:20:55 -08002852 return len;
2853}
2854
2855
2856static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002857__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002858
NeilBrown93c8cad2006-01-06 00:20:56 -08002859static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002860offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002861{
Andrew Morton6961ece2006-01-06 00:20:59 -08002862 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002863}
2864
2865static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002866offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002867{
NeilBrownc6563a82012-05-21 09:27:00 +10002868 unsigned long long offset;
2869 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002870 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002871 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002872 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002873 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002874 /* Must set offset before size, so overlap checks
2875 * can be sane */
2876 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002877 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002878 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002879 return len;
2880}
2881
2882static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002883__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002884
NeilBrownc6563a82012-05-21 09:27:00 +10002885static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2886{
2887 return sprintf(page, "%llu\n",
2888 (unsigned long long)rdev->new_data_offset);
2889}
2890
2891static ssize_t new_offset_store(struct md_rdev *rdev,
2892 const char *buf, size_t len)
2893{
2894 unsigned long long new_offset;
2895 struct mddev *mddev = rdev->mddev;
2896
2897 if (strict_strtoull(buf, 10, &new_offset) < 0)
2898 return -EINVAL;
2899
2900 if (mddev->sync_thread)
2901 return -EBUSY;
2902 if (new_offset == rdev->data_offset)
2903 /* reset is always permitted */
2904 ;
2905 else if (new_offset > rdev->data_offset) {
2906 /* must not push array size beyond rdev_sectors */
2907 if (new_offset - rdev->data_offset
2908 + mddev->dev_sectors > rdev->sectors)
2909 return -E2BIG;
2910 }
2911 /* Metadata worries about other space details. */
2912
2913 /* decreasing the offset is inconsistent with a backwards
2914 * reshape.
2915 */
2916 if (new_offset < rdev->data_offset &&
2917 mddev->reshape_backwards)
2918 return -EINVAL;
2919 /* Increasing offset is inconsistent with forwards
2920 * reshape. reshape_direction should be set to
2921 * 'backwards' first.
2922 */
2923 if (new_offset > rdev->data_offset &&
2924 !mddev->reshape_backwards)
2925 return -EINVAL;
2926
2927 if (mddev->pers && mddev->persistent &&
2928 !super_types[mddev->major_version]
2929 .allow_new_offset(rdev, new_offset))
2930 return -E2BIG;
2931 rdev->new_data_offset = new_offset;
2932 if (new_offset > rdev->data_offset)
2933 mddev->reshape_backwards = 1;
2934 else if (new_offset < rdev->data_offset)
2935 mddev->reshape_backwards = 0;
2936
2937 return len;
2938}
2939static struct rdev_sysfs_entry rdev_new_offset =
2940__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2941
NeilBrown83303b62006-01-06 00:21:06 -08002942static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002943rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002944{
Andre Nolldd8ac332009-03-31 14:33:13 +11002945 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002946}
2947
NeilBrownc5d79ad2008-02-06 01:39:54 -08002948static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2949{
2950 /* check if two start/length pairs overlap */
2951 if (s1+l1 <= s2)
2952 return 0;
2953 if (s2+l2 <= s1)
2954 return 0;
2955 return 1;
2956}
2957
Dan Williamsb522adc2009-03-31 15:00:31 +11002958static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2959{
2960 unsigned long long blocks;
2961 sector_t new;
2962
2963 if (strict_strtoull(buf, 10, &blocks) < 0)
2964 return -EINVAL;
2965
2966 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2967 return -EINVAL; /* sector conversion overflow */
2968
2969 new = blocks * 2;
2970 if (new != blocks * 2)
2971 return -EINVAL; /* unsigned long long to sector_t overflow */
2972
2973 *sectors = new;
2974 return 0;
2975}
2976
NeilBrown83303b62006-01-06 00:21:06 -08002977static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002978rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002979{
NeilBrownfd01b882011-10-11 16:47:53 +11002980 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002981 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002982 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002983
Dan Williamsb522adc2009-03-31 15:00:31 +11002984 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002985 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002986 if (rdev->data_offset != rdev->new_data_offset)
2987 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002988 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002989 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002990 sectors = super_types[my_mddev->major_version].
2991 rdev_size_change(rdev, sectors);
2992 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002993 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002994 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002995 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002996 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002997 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002998 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002999 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003000
Andre Nolldd8ac332009-03-31 14:33:13 +11003001 rdev->sectors = sectors;
3002 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 /* need to check that all other rdevs with the same ->bdev
3004 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003005 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003006 * we have to change it back, we will have the lock again.
3007 */
NeilBrownfd01b882011-10-11 16:47:53 +11003008 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003009 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003010 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003011
NeilBrown27c529b2008-03-04 14:29:33 -08003012 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003013 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003014 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003015
3016 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003017 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003018 if (rdev->bdev == rdev2->bdev &&
3019 rdev != rdev2 &&
3020 overlaps(rdev->data_offset, rdev->sectors,
3021 rdev2->data_offset,
3022 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003023 overlap = 1;
3024 break;
3025 }
3026 mddev_unlock(mddev);
3027 if (overlap) {
3028 mddev_put(mddev);
3029 break;
3030 }
3031 }
NeilBrown27c529b2008-03-04 14:29:33 -08003032 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003033 if (overlap) {
3034 /* Someone else could have slipped in a size
3035 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003036 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003037 * safe, and trust userspace not to race with
3038 * itself
3039 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003040 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003041 return -EBUSY;
3042 }
3043 }
NeilBrown83303b62006-01-06 00:21:06 -08003044 return len;
3045}
3046
3047static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003048__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003049
Dan Williams06e3c812009-12-12 21:17:12 -07003050
NeilBrown3cb03002011-10-11 16:45:26 +11003051static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003052{
3053 unsigned long long recovery_start = rdev->recovery_offset;
3054
3055 if (test_bit(In_sync, &rdev->flags) ||
3056 recovery_start == MaxSector)
3057 return sprintf(page, "none\n");
3058
3059 return sprintf(page, "%llu\n", recovery_start);
3060}
3061
NeilBrown3cb03002011-10-11 16:45:26 +11003062static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003063{
3064 unsigned long long recovery_start;
3065
3066 if (cmd_match(buf, "none"))
3067 recovery_start = MaxSector;
3068 else if (strict_strtoull(buf, 10, &recovery_start))
3069 return -EINVAL;
3070
3071 if (rdev->mddev->pers &&
3072 rdev->raid_disk >= 0)
3073 return -EBUSY;
3074
3075 rdev->recovery_offset = recovery_start;
3076 if (recovery_start == MaxSector)
3077 set_bit(In_sync, &rdev->flags);
3078 else
3079 clear_bit(In_sync, &rdev->flags);
3080 return len;
3081}
3082
3083static struct rdev_sysfs_entry rdev_recovery_start =
3084__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3085
NeilBrown16c791a2011-07-28 11:31:47 +10003086
3087static ssize_t
3088badblocks_show(struct badblocks *bb, char *page, int unack);
3089static ssize_t
3090badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3091
NeilBrown3cb03002011-10-11 16:45:26 +11003092static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003093{
3094 return badblocks_show(&rdev->badblocks, page, 0);
3095}
NeilBrown3cb03002011-10-11 16:45:26 +11003096static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003097{
NeilBrownde393cd2011-07-28 11:31:48 +10003098 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3099 /* Maybe that ack was all we needed */
3100 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3101 wake_up(&rdev->blocked_wait);
3102 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003103}
3104static struct rdev_sysfs_entry rdev_bad_blocks =
3105__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3106
3107
NeilBrown3cb03002011-10-11 16:45:26 +11003108static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003109{
3110 return badblocks_show(&rdev->badblocks, page, 1);
3111}
NeilBrown3cb03002011-10-11 16:45:26 +11003112static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003113{
3114 return badblocks_store(&rdev->badblocks, page, len, 1);
3115}
3116static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3117__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3118
NeilBrown86e6ffd2005-11-08 21:39:24 -08003119static struct attribute *rdev_default_attrs[] = {
3120 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003121 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003122 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003123 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003124 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003125 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003126 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003127 &rdev_bad_blocks.attr,
3128 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003129 NULL,
3130};
3131static ssize_t
3132rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3133{
3134 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003135 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003136 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003137 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003138
3139 if (!entry->show)
3140 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003141
3142 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3143 if (!rv) {
3144 if (rdev->mddev == NULL)
3145 rv = -EBUSY;
3146 else
3147 rv = entry->show(rdev, page);
3148 mddev_unlock(mddev);
3149 }
3150 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003151}
3152
3153static ssize_t
3154rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3155 const char *page, size_t length)
3156{
3157 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003158 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003159 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003160 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003161
3162 if (!entry->store)
3163 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003164 if (!capable(CAP_SYS_ADMIN))
3165 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003166 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003167 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003168 if (rdev->mddev == NULL)
3169 rv = -EBUSY;
3170 else
3171 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003172 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003173 }
3174 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003175}
3176
3177static void rdev_free(struct kobject *ko)
3178{
NeilBrown3cb03002011-10-11 16:45:26 +11003179 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003180 kfree(rdev);
3181}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003182static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003183 .show = rdev_attr_show,
3184 .store = rdev_attr_store,
3185};
3186static struct kobj_type rdev_ktype = {
3187 .release = rdev_free,
3188 .sysfs_ops = &rdev_sysfs_ops,
3189 .default_attrs = rdev_default_attrs,
3190};
3191
NeilBrown3cb03002011-10-11 16:45:26 +11003192int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003193{
3194 rdev->desc_nr = -1;
3195 rdev->saved_raid_disk = -1;
3196 rdev->raid_disk = -1;
3197 rdev->flags = 0;
3198 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003199 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003200 rdev->sb_events = 0;
3201 rdev->last_read_error.tv_sec = 0;
3202 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003203 rdev->sb_loaded = 0;
3204 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003205 atomic_set(&rdev->nr_pending, 0);
3206 atomic_set(&rdev->read_errors, 0);
3207 atomic_set(&rdev->corrected_errors, 0);
3208
3209 INIT_LIST_HEAD(&rdev->same_set);
3210 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003211
3212 /* Add space to store bad block list.
3213 * This reserves the space even on arrays where it cannot
3214 * be used - I wonder if that matters
3215 */
3216 rdev->badblocks.count = 0;
3217 rdev->badblocks.shift = 0;
3218 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3219 seqlock_init(&rdev->badblocks.lock);
3220 if (rdev->badblocks.page == NULL)
3221 return -ENOMEM;
3222
3223 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003224}
3225EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
3227 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3228 *
3229 * mark the device faulty if:
3230 *
3231 * - the device is nonexistent (zero size)
3232 * - the device has no valid superblock
3233 *
3234 * a faulty rdev _never_ has rdev->sb set.
3235 */
NeilBrown3cb03002011-10-11 16:45:26 +11003236static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
3238 char b[BDEVNAME_SIZE];
3239 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003240 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 sector_t size;
3242
NeilBrown9ffae0c2006-01-06 00:20:32 -08003243 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (!rdev) {
3245 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3246 return ERR_PTR(-ENOMEM);
3247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
NeilBrown2230dfe2011-07-28 11:31:46 +10003249 err = md_rdev_init(rdev);
3250 if (err)
3251 goto abort_free;
3252 err = alloc_disk_sb(rdev);
3253 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 goto abort_free;
3255
NeilBrownc5d79ad2008-02-06 01:39:54 -08003256 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (err)
3258 goto abort_free;
3259
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003260 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003261
Mike Snitzer77304d22010-11-08 14:39:12 +01003262 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (!size) {
3264 printk(KERN_WARNING
3265 "md: %s has zero or unknown size, marking faulty!\n",
3266 bdevname(rdev->bdev,b));
3267 err = -EINVAL;
3268 goto abort_free;
3269 }
3270
3271 if (super_format >= 0) {
3272 err = super_types[super_format].
3273 load_super(rdev, NULL, super_minor);
3274 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003275 printk(KERN_WARNING
3276 "md: %s does not have a valid v%d.%d "
3277 "superblock, not importing!\n",
3278 bdevname(rdev->bdev,b),
3279 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 goto abort_free;
3281 }
3282 if (err < 0) {
3283 printk(KERN_WARNING
3284 "md: could not read %s's sb, not importing!\n",
3285 bdevname(rdev->bdev,b));
3286 goto abort_free;
3287 }
3288 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003289 if (super_format == -1)
3290 /* hot-add for 0.90, or non-persistent: so no badblocks */
3291 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return rdev;
3294
3295abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003296 if (rdev->bdev)
3297 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003298 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 kfree(rdev);
3300 return ERR_PTR(err);
3301}
3302
3303/*
3304 * Check a full RAID array for plausibility
3305 */
3306
3307
NeilBrownfd01b882011-10-11 16:47:53 +11003308static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003311 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 char b[BDEVNAME_SIZE];
3313
3314 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003315 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 switch (super_types[mddev->major_version].
3317 load_super(rdev, freshest, mddev->minor_version)) {
3318 case 1:
3319 freshest = rdev;
3320 break;
3321 case 0:
3322 break;
3323 default:
3324 printk( KERN_ERR \
3325 "md: fatal superblock inconsistency in %s"
3326 " -- removing from array\n",
3327 bdevname(rdev->bdev,b));
3328 kick_rdev_from_array(rdev);
3329 }
3330
3331
3332 super_types[mddev->major_version].
3333 validate_super(mddev, freshest);
3334
3335 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003336 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003337 if (mddev->max_disks &&
3338 (rdev->desc_nr >= mddev->max_disks ||
3339 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003340 printk(KERN_WARNING
3341 "md: %s: %s: only %d devices permitted\n",
3342 mdname(mddev), bdevname(rdev->bdev, b),
3343 mddev->max_disks);
3344 kick_rdev_from_array(rdev);
3345 continue;
3346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 if (rdev != freshest)
3348 if (super_types[mddev->major_version].
3349 validate_super(mddev, rdev)) {
3350 printk(KERN_WARNING "md: kicking non-fresh %s"
3351 " from array!\n",
3352 bdevname(rdev->bdev,b));
3353 kick_rdev_from_array(rdev);
3354 continue;
3355 }
3356 if (mddev->level == LEVEL_MULTIPATH) {
3357 rdev->desc_nr = i++;
3358 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003359 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003360 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003361 rdev->raid_disk = -1;
3362 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
NeilBrown72e02072009-12-14 12:49:55 +11003367/* Read a fixed-point number.
3368 * Numbers in sysfs attributes should be in "standard" units where
3369 * possible, so time should be in seconds.
3370 * However we internally use a a much smaller unit such as
3371 * milliseconds or jiffies.
3372 * This function takes a decimal number with a possible fractional
3373 * component, and produces an integer which is the result of
3374 * multiplying that number by 10^'scale'.
3375 * all without any floating-point arithmetic.
3376 */
3377int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3378{
3379 unsigned long result = 0;
3380 long decimals = -1;
3381 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3382 if (*cp == '.')
3383 decimals = 0;
3384 else if (decimals < scale) {
3385 unsigned int value;
3386 value = *cp - '0';
3387 result = result * 10 + value;
3388 if (decimals >= 0)
3389 decimals++;
3390 }
3391 cp++;
3392 }
3393 if (*cp == '\n')
3394 cp++;
3395 if (*cp)
3396 return -EINVAL;
3397 if (decimals < 0)
3398 decimals = 0;
3399 while (decimals < scale) {
3400 result *= 10;
3401 decimals ++;
3402 }
3403 *res = result;
3404 return 0;
3405}
3406
3407
NeilBrown19052c02008-08-05 15:54:13 +10003408static void md_safemode_timeout(unsigned long data);
3409
NeilBrowneae17012005-11-08 21:39:23 -08003410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003411safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003412{
3413 int msec = (mddev->safemode_delay*1000)/HZ;
3414 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3415}
3416static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003417safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003418{
NeilBrown16f17b32006-06-26 00:27:37 -07003419 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003420
NeilBrown72e02072009-12-14 12:49:55 +11003421 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003422 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003423 if (msec == 0)
3424 mddev->safemode_delay = 0;
3425 else {
NeilBrown19052c02008-08-05 15:54:13 +10003426 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003427 mddev->safemode_delay = (msec*HZ)/1000;
3428 if (mddev->safemode_delay == 0)
3429 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003430 if (mddev->safemode_delay < old_delay)
3431 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003432 }
3433 return len;
3434}
3435static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003436__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003437
3438static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003439level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003440{
NeilBrown84fc4b52011-10-11 16:49:58 +11003441 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003442 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003443 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003444 else if (mddev->clevel[0])
3445 return sprintf(page, "%s\n", mddev->clevel);
3446 else if (mddev->level != LEVEL_NONE)
3447 return sprintf(page, "%d\n", mddev->level);
3448 else
3449 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003450}
3451
NeilBrownd9d166c2006-01-06 00:20:51 -08003452static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003453level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003454{
Dan Williamsf2859af2010-05-02 10:04:16 -07003455 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003456 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003457 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003458 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003459 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003460 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003461
3462 if (mddev->pers == NULL) {
3463 if (len == 0)
3464 return 0;
3465 if (len >= sizeof(mddev->clevel))
3466 return -ENOSPC;
3467 strncpy(mddev->clevel, buf, len);
3468 if (mddev->clevel[len-1] == '\n')
3469 len--;
3470 mddev->clevel[len] = 0;
3471 mddev->level = LEVEL_NONE;
3472 return rv;
3473 }
3474
3475 /* request to change the personality. Need to ensure:
3476 * - array is not engaged in resync/recovery/reshape
3477 * - old personality can be suspended
3478 * - new personality will access other array.
3479 */
3480
NeilBrownbb4f1e92010-08-08 21:18:03 +10003481 if (mddev->sync_thread ||
3482 mddev->reshape_position != MaxSector ||
3483 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003484 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003485
3486 if (!mddev->pers->quiesce) {
3487 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3488 mdname(mddev), mddev->pers->name);
3489 return -EINVAL;
3490 }
3491
3492 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003493 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003494 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003495 strncpy(clevel, buf, len);
3496 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003497 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003498 clevel[len] = 0;
3499 if (strict_strtol(clevel, 10, &level))
3500 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003501
Dan Williamsf2859af2010-05-02 10:04:16 -07003502 if (request_module("md-%s", clevel) != 0)
3503 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003504 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003506 if (!pers || !try_module_get(pers->owner)) {
3507 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003508 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003509 return -EINVAL;
3510 }
3511 spin_unlock(&pers_lock);
3512
3513 if (pers == mddev->pers) {
3514 /* Nothing to do! */
3515 module_put(pers->owner);
3516 return rv;
3517 }
3518 if (!pers->takeover) {
3519 module_put(pers->owner);
3520 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003521 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003522 return -EINVAL;
3523 }
3524
NeilBrowndafb20f2012-03-19 12:46:39 +11003525 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003526 rdev->new_raid_disk = rdev->raid_disk;
3527
NeilBrown245f46c2009-03-31 14:39:39 +11003528 /* ->takeover must set new_* and/or delta_disks
3529 * if it succeeds, and may set them when it fails.
3530 */
3531 priv = pers->takeover(mddev);
3532 if (IS_ERR(priv)) {
3533 mddev->new_level = mddev->level;
3534 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003535 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003536 mddev->raid_disks -= mddev->delta_disks;
3537 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003538 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003539 module_put(pers->owner);
3540 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003541 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003542 return PTR_ERR(priv);
3543 }
3544
3545 /* Looks like we have a winner */
3546 mddev_suspend(mddev);
3547 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003548
3549 if (mddev->pers->sync_request == NULL &&
3550 pers->sync_request != NULL) {
3551 /* need to add the md_redundancy_group */
3552 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3553 printk(KERN_WARNING
3554 "md: cannot register extra attributes for %s\n",
3555 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003556 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003557 }
3558 if (mddev->pers->sync_request != NULL &&
3559 pers->sync_request == NULL) {
3560 /* need to remove the md_redundancy_group */
3561 if (mddev->to_remove == NULL)
3562 mddev->to_remove = &md_redundancy_group;
3563 }
3564
Trela Maciej54071b32010-03-08 16:02:42 +11003565 if (mddev->pers->sync_request == NULL &&
3566 mddev->external) {
3567 /* We are converting from a no-redundancy array
3568 * to a redundancy array and metadata is managed
3569 * externally so we need to be sure that writes
3570 * won't block due to a need to transition
3571 * clean->dirty
3572 * until external management is started.
3573 */
3574 mddev->in_sync = 0;
3575 mddev->safemode_delay = 0;
3576 mddev->safemode = 0;
3577 }
3578
NeilBrowndafb20f2012-03-19 12:46:39 +11003579 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003580 if (rdev->raid_disk < 0)
3581 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003582 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003583 rdev->new_raid_disk = -1;
3584 if (rdev->new_raid_disk == rdev->raid_disk)
3585 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003586 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003587 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003588 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003589 if (rdev->raid_disk < 0)
3590 continue;
3591 if (rdev->new_raid_disk == rdev->raid_disk)
3592 continue;
3593 rdev->raid_disk = rdev->new_raid_disk;
3594 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003595 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003596 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003597 if (sysfs_link_rdev(mddev, rdev))
3598 printk(KERN_WARNING "md: cannot register rd%d"
3599 " for %s after level change\n",
3600 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003601 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003602 }
3603
3604 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003605 mddev->pers = pers;
3606 mddev->private = priv;
3607 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3608 mddev->level = mddev->new_level;
3609 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003610 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003611 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003612 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003613 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003614 if (mddev->pers->sync_request == NULL) {
3615 /* this is now an array without redundancy, so
3616 * it must always be in_sync
3617 */
3618 mddev->in_sync = 1;
3619 del_timer_sync(&mddev->safemode_timer);
3620 }
NeilBrown245f46c2009-03-31 14:39:39 +11003621 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003622 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003623 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003624 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003625 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003626 return rv;
3627}
3628
3629static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003630__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003631
NeilBrownd4dbd022006-06-26 00:27:59 -07003632
3633static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003634layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003635{
3636 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003637 if (mddev->reshape_position != MaxSector &&
3638 mddev->layout != mddev->new_layout)
3639 return sprintf(page, "%d (%d)\n",
3640 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003641 return sprintf(page, "%d\n", mddev->layout);
3642}
3643
3644static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003645layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003646{
3647 char *e;
3648 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003649
3650 if (!*buf || (*e && *e != '\n'))
3651 return -EINVAL;
3652
NeilBrownb3546032009-03-31 14:56:41 +11003653 if (mddev->pers) {
3654 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003655 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003656 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003657 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003658 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003659 if (err) {
3660 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003661 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003662 }
NeilBrownb3546032009-03-31 14:56:41 +11003663 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003664 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003665 if (mddev->reshape_position == MaxSector)
3666 mddev->layout = n;
3667 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003668 return len;
3669}
3670static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003671__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003672
3673
NeilBrowneae17012005-11-08 21:39:23 -08003674static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003675raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003676{
NeilBrownbb636542005-11-08 21:39:45 -08003677 if (mddev->raid_disks == 0)
3678 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003679 if (mddev->reshape_position != MaxSector &&
3680 mddev->delta_disks != 0)
3681 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3682 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003683 return sprintf(page, "%d\n", mddev->raid_disks);
3684}
3685
NeilBrownfd01b882011-10-11 16:47:53 +11003686static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003687
3688static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003689raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003690{
NeilBrownda943b992006-01-06 00:20:54 -08003691 char *e;
3692 int rv = 0;
3693 unsigned long n = simple_strtoul(buf, &e, 10);
3694
3695 if (!*buf || (*e && *e != '\n'))
3696 return -EINVAL;
3697
3698 if (mddev->pers)
3699 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003700 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003701 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003702 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003703
3704 rdev_for_each(rdev, mddev) {
3705 if (olddisks < n &&
3706 rdev->data_offset < rdev->new_data_offset)
3707 return -EINVAL;
3708 if (olddisks > n &&
3709 rdev->data_offset > rdev->new_data_offset)
3710 return -EINVAL;
3711 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003712 mddev->delta_disks = n - olddisks;
3713 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003714 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 } else
NeilBrownda943b992006-01-06 00:20:54 -08003716 mddev->raid_disks = n;
3717 return rv ? rv : len;
3718}
3719static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003720__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003721
NeilBrown24dd4692005-11-08 21:39:26 -08003722static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003723chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003724{
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003726 mddev->chunk_sectors != mddev->new_chunk_sectors)
3727 return sprintf(page, "%d (%d)\n",
3728 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003729 mddev->chunk_sectors << 9);
3730 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003731}
3732
3733static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003734chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003735{
NeilBrown3b343802006-01-06 00:20:47 -08003736 char *e;
3737 unsigned long n = simple_strtoul(buf, &e, 10);
3738
NeilBrown3b343802006-01-06 00:20:47 -08003739 if (!*buf || (*e && *e != '\n'))
3740 return -EINVAL;
3741
NeilBrownb3546032009-03-31 14:56:41 +11003742 if (mddev->pers) {
3743 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003744 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003745 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003746 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003747 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003748 if (err) {
3749 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003750 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003751 }
NeilBrownb3546032009-03-31 14:56:41 +11003752 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003753 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003754 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003755 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003756 }
NeilBrown3b343802006-01-06 00:20:47 -08003757 return len;
3758}
3759static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003760__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003761
NeilBrowna94213b2006-06-26 00:28:00 -07003762static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003763resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003764{
NeilBrownd1a7c502009-03-31 15:24:32 +11003765 if (mddev->recovery_cp == MaxSector)
3766 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003767 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3768}
3769
3770static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003771resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003772{
NeilBrowna94213b2006-06-26 00:28:00 -07003773 char *e;
3774 unsigned long long n = simple_strtoull(buf, &e, 10);
3775
NeilBrownb0986362011-05-11 15:52:21 +10003776 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003777 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003778 if (cmd_match(buf, "none"))
3779 n = MaxSector;
3780 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003781 return -EINVAL;
3782
3783 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003784 if (mddev->pers)
3785 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003786 return len;
3787}
3788static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003789__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003790
NeilBrown9e653b62006-06-26 00:27:58 -07003791/*
3792 * The array state can be:
3793 *
3794 * clear
3795 * No devices, no size, no level
3796 * Equivalent to STOP_ARRAY ioctl
3797 * inactive
3798 * May have some settings, but array is not active
3799 * all IO results in error
3800 * When written, doesn't tear down array, but just stops it
3801 * suspended (not supported yet)
3802 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003803 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003804 * readonly
3805 * no resync can happen. no superblocks get written.
3806 * write requests fail
3807 * read-auto
3808 * like readonly, but behaves like 'clean' on a write request.
3809 *
3810 * clean - no pending writes, but otherwise active.
3811 * When written to inactive array, starts without resync
3812 * If a write request arrives then
3813 * if metadata is known, mark 'dirty' and switch to 'active'.
3814 * if not known, block and switch to write-pending
3815 * If written to an active array that has pending writes, then fails.
3816 * active
3817 * fully active: IO and resync can be happening.
3818 * When written to inactive array, starts with resync
3819 *
3820 * write-pending
3821 * clean, but writes are blocked waiting for 'active' to be written.
3822 *
3823 * active-idle
3824 * like active, but no writes have been seen for a while (100msec).
3825 *
3826 */
3827enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3828 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003829static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003830 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3831 "write-pending", "active-idle", NULL };
3832
3833static int match_word(const char *word, char **list)
3834{
3835 int n;
3836 for (n=0; list[n]; n++)
3837 if (cmd_match(word, list[n]))
3838 break;
3839 return n;
3840}
3841
3842static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003843array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003844{
3845 enum array_state st = inactive;
3846
3847 if (mddev->pers)
3848 switch(mddev->ro) {
3849 case 1:
3850 st = readonly;
3851 break;
3852 case 2:
3853 st = read_auto;
3854 break;
3855 case 0:
3856 if (mddev->in_sync)
3857 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003858 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003859 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003860 else if (mddev->safemode)
3861 st = active_idle;
3862 else
3863 st = active;
3864 }
3865 else {
3866 if (list_empty(&mddev->disks) &&
3867 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003868 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003869 st = clear;
3870 else
3871 st = inactive;
3872 }
3873 return sprintf(page, "%s\n", array_states[st]);
3874}
3875
NeilBrowna05b7ea2012-07-19 15:59:18 +10003876static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3877static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003878static int do_md_run(struct mddev * mddev);
3879static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003880
3881static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003882array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003883{
3884 int err = -EINVAL;
3885 enum array_state st = match_word(buf, array_states);
3886 switch(st) {
3887 case bad_word:
3888 break;
3889 case clear:
3890 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003891 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003892 break;
3893 case inactive:
3894 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003895 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003896 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003897 else
NeilBrowne6910632008-02-06 01:39:51 -08003898 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003899 break;
3900 case suspended:
3901 break; /* not supported yet */
3902 case readonly:
3903 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003904 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003905 else {
3906 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003907 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003908 err = do_md_run(mddev);
3909 }
3910 break;
3911 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003912 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003913 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003914 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003915 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003916 err = restart_array(mddev);
3917 if (err == 0) {
3918 mddev->ro = 2;
3919 set_disk_ro(mddev->gendisk, 0);
3920 }
NeilBrown9e653b62006-06-26 00:27:58 -07003921 } else {
3922 mddev->ro = 2;
3923 err = do_md_run(mddev);
3924 }
3925 break;
3926 case clean:
3927 if (mddev->pers) {
3928 restart_array(mddev);
3929 spin_lock_irq(&mddev->write_lock);
3930 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003931 if (mddev->in_sync == 0) {
3932 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003933 if (mddev->safemode == 1)
3934 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003935 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003936 }
3937 err = 0;
3938 } else
3939 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003940 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003941 } else
3942 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003943 break;
3944 case active:
3945 if (mddev->pers) {
3946 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003947 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003948 wake_up(&mddev->sb_wait);
3949 err = 0;
3950 } else {
3951 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003952 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003953 err = do_md_run(mddev);
3954 }
3955 break;
3956 case write_pending:
3957 case active_idle:
3958 /* these cannot be set */
3959 break;
3960 }
3961 if (err)
3962 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003963 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003964 if (mddev->hold_active == UNTIL_IOCTL)
3965 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003966 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003967 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003968 }
NeilBrown9e653b62006-06-26 00:27:58 -07003969}
NeilBrown80ca3a42006-07-10 04:44:18 -07003970static struct md_sysfs_entry md_array_state =
3971__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003972
NeilBrown6d7ff732006-01-06 00:21:16 -08003973static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003974max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003975 return sprintf(page, "%d\n",
3976 atomic_read(&mddev->max_corr_read_errors));
3977}
3978
3979static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003980max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003981{
3982 char *e;
3983 unsigned long n = simple_strtoul(buf, &e, 10);
3984
3985 if (*buf && (*e == 0 || *e == '\n')) {
3986 atomic_set(&mddev->max_corr_read_errors, n);
3987 return len;
3988 }
3989 return -EINVAL;
3990}
3991
3992static struct md_sysfs_entry max_corr_read_errors =
3993__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3994 max_corrected_read_errors_store);
3995
3996static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003997null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003998{
3999 return -EINVAL;
4000}
4001
4002static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004003new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004004{
4005 /* buf must be %d:%d\n? giving major and minor numbers */
4006 /* The new device is added to the array.
4007 * If the array has a persistent superblock, we read the
4008 * superblock to initialise info and check validity.
4009 * Otherwise, only checking done is that in bind_rdev_to_array,
4010 * which mainly checks size.
4011 */
4012 char *e;
4013 int major = simple_strtoul(buf, &e, 10);
4014 int minor;
4015 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004016 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004017 int err;
4018
4019 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4020 return -EINVAL;
4021 minor = simple_strtoul(e+1, &e, 10);
4022 if (*e && *e != '\n')
4023 return -EINVAL;
4024 dev = MKDEV(major, minor);
4025 if (major != MAJOR(dev) ||
4026 minor != MINOR(dev))
4027 return -EOVERFLOW;
4028
4029
4030 if (mddev->persistent) {
4031 rdev = md_import_device(dev, mddev->major_version,
4032 mddev->minor_version);
4033 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004034 struct md_rdev *rdev0
4035 = list_entry(mddev->disks.next,
4036 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004037 err = super_types[mddev->major_version]
4038 .load_super(rdev, rdev0, mddev->minor_version);
4039 if (err < 0)
4040 goto out;
4041 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004042 } else if (mddev->external)
4043 rdev = md_import_device(dev, -2, -1);
4044 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004045 rdev = md_import_device(dev, -1, -1);
4046
4047 if (IS_ERR(rdev))
4048 return PTR_ERR(rdev);
4049 err = bind_rdev_to_array(rdev, mddev);
4050 out:
4051 if (err)
4052 export_rdev(rdev);
4053 return err ? err : len;
4054}
4055
4056static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004057__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004058
4059static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004060bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004061{
4062 char *end;
4063 unsigned long chunk, end_chunk;
4064
4065 if (!mddev->bitmap)
4066 goto out;
4067 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4068 while (*buf) {
4069 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4070 if (buf == end) break;
4071 if (*end == '-') { /* range */
4072 buf = end + 1;
4073 end_chunk = simple_strtoul(buf, &end, 0);
4074 if (buf == end) break;
4075 }
4076 if (*end && !isspace(*end)) break;
4077 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004078 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004079 }
4080 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4081out:
4082 return len;
4083}
4084
4085static struct md_sysfs_entry md_bitmap =
4086__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4087
4088static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004089size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004090{
Andre Noll58c0fed2009-03-31 14:33:13 +11004091 return sprintf(page, "%llu\n",
4092 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004093}
4094
NeilBrownfd01b882011-10-11 16:47:53 +11004095static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004096
4097static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004098size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004099{
4100 /* If array is inactive, we can reduce the component size, but
4101 * not increase it (except from 0).
4102 * If array is active, we can try an on-line resize
4103 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004104 sector_t sectors;
4105 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004106
Andre Noll58c0fed2009-03-31 14:33:13 +11004107 if (err < 0)
4108 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004109 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004110 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004111 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004112 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004113 if (mddev->dev_sectors == 0 ||
4114 mddev->dev_sectors > sectors)
4115 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004116 else
4117 err = -ENOSPC;
4118 }
4119 return err ? err : len;
4120}
4121
4122static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004123__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004124
NeilBrown8bb93aa2006-01-06 00:20:50 -08004125
Masanari Iida83f0d772012-10-30 00:18:08 +09004126/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004127 * This is one of
4128 * 'none' for arrays with no metadata (good luck...)
4129 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004130 * or N.M for internally known formats
4131 */
4132static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004133metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004134{
4135 if (mddev->persistent)
4136 return sprintf(page, "%d.%d\n",
4137 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004138 else if (mddev->external)
4139 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004140 else
4141 return sprintf(page, "none\n");
4142}
4143
4144static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004145metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146{
4147 int major, minor;
4148 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004149 /* Changing the details of 'external' metadata is
4150 * always permitted. Otherwise there must be
4151 * no devices attached to the array.
4152 */
4153 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4154 ;
4155 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156 return -EBUSY;
4157
4158 if (cmd_match(buf, "none")) {
4159 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004160 mddev->external = 0;
4161 mddev->major_version = 0;
4162 mddev->minor_version = 90;
4163 return len;
4164 }
4165 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004166 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004167 if (namelen >= sizeof(mddev->metadata_type))
4168 namelen = sizeof(mddev->metadata_type)-1;
4169 strncpy(mddev->metadata_type, buf+9, namelen);
4170 mddev->metadata_type[namelen] = 0;
4171 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4172 mddev->metadata_type[--namelen] = 0;
4173 mddev->persistent = 0;
4174 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004175 mddev->major_version = 0;
4176 mddev->minor_version = 90;
4177 return len;
4178 }
4179 major = simple_strtoul(buf, &e, 10);
4180 if (e==buf || *e != '.')
4181 return -EINVAL;
4182 buf = e+1;
4183 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004184 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004185 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004186 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004187 return -ENOENT;
4188 mddev->major_version = major;
4189 mddev->minor_version = minor;
4190 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004191 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004192 return len;
4193}
4194
4195static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004196__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004197
NeilBrowna35b0d62006-01-06 00:20:49 -08004198static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004199action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004200{
NeilBrown7eec3142005-11-08 21:39:44 -08004201 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004202 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4203 type = "frozen";
4204 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004205 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004206 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4207 type = "reshape";
4208 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004209 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4210 type = "resync";
4211 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4212 type = "check";
4213 else
4214 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004215 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004216 type = "recover";
4217 }
4218 return sprintf(page, "%s\n", type);
4219}
4220
NeilBrownfd01b882011-10-11 16:47:53 +11004221static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004222
NeilBrown24dd4692005-11-08 21:39:26 -08004223static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004224action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004225{
NeilBrown7eec3142005-11-08 21:39:44 -08004226 if (!mddev->pers || !mddev->pers->sync_request)
4227 return -EINVAL;
4228
NeilBrownb6a9ce62009-05-26 09:41:17 +10004229 if (cmd_match(page, "frozen"))
4230 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4231 else
4232 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4233
4234 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004235 if (mddev->sync_thread) {
4236 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004237 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004238 }
NeilBrown03c902e2006-01-06 00:20:46 -08004239 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4240 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004241 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004242 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004243 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004244 else if (cmd_match(page, "recover")) {
4245 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4247 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004248 int err;
4249 if (mddev->pers->start_reshape == NULL)
4250 return -EINVAL;
4251 err = mddev->pers->start_reshape(mddev);
4252 if (err)
4253 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004254 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004255 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004256 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004257 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004258 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004259 return -EINVAL;
4260 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4261 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004262 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004263 if (mddev->ro == 2) {
4264 /* A write to sync_action is enough to justify
4265 * canceling read-auto mode
4266 */
4267 mddev->ro = 0;
4268 md_wakeup_thread(mddev->sync_thread);
4269 }
NeilBrown03c902e2006-01-06 00:20:46 -08004270 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004271 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004272 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004273 return len;
4274}
4275
NeilBrown9d888832005-11-08 21:39:26 -08004276static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004277mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004278{
4279 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004280 (unsigned long long)
4281 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004282}
4283
NeilBrown80ca3a42006-07-10 04:44:18 -07004284static struct md_sysfs_entry md_scan_mode =
4285__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004286
NeilBrown96de1e62005-11-08 21:39:39 -08004287
NeilBrown80ca3a42006-07-10 04:44:18 -07004288static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004289
NeilBrown88202a02006-01-06 00:21:36 -08004290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004292{
4293 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4294 mddev->sync_speed_min ? "local": "system");
4295}
4296
4297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004298sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004299{
4300 int min;
4301 char *e;
4302 if (strncmp(buf, "system", 6)==0) {
4303 mddev->sync_speed_min = 0;
4304 return len;
4305 }
4306 min = simple_strtoul(buf, &e, 10);
4307 if (buf == e || (*e && *e != '\n') || min <= 0)
4308 return -EINVAL;
4309 mddev->sync_speed_min = min;
4310 return len;
4311}
4312
4313static struct md_sysfs_entry md_sync_min =
4314__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4315
4316static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004317sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004318{
4319 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4320 mddev->sync_speed_max ? "local": "system");
4321}
4322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004325{
4326 int max;
4327 char *e;
4328 if (strncmp(buf, "system", 6)==0) {
4329 mddev->sync_speed_max = 0;
4330 return len;
4331 }
4332 max = simple_strtoul(buf, &e, 10);
4333 if (buf == e || (*e && *e != '\n') || max <= 0)
4334 return -EINVAL;
4335 mddev->sync_speed_max = max;
4336 return len;
4337}
4338
4339static struct md_sysfs_entry md_sync_max =
4340__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4341
Iustin Popd7f3d292007-10-16 23:30:54 -07004342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004344{
4345 return sprintf(page, "%d\n", mddev->degraded);
4346}
4347static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004348
4349static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004350sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004351{
4352 return sprintf(page, "%d\n", mddev->parallel_resync);
4353}
4354
4355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004357{
4358 long n;
4359
4360 if (strict_strtol(buf, 10, &n))
4361 return -EINVAL;
4362
4363 if (n != 0 && n != 1)
4364 return -EINVAL;
4365
4366 mddev->parallel_resync = n;
4367
4368 if (mddev->sync_thread)
4369 wake_up(&resync_wait);
4370
4371 return len;
4372}
4373
4374/* force parallel resync, even with shared block devices */
4375static struct md_sysfs_entry md_sync_force_parallel =
4376__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4377 sync_force_parallel_show, sync_force_parallel_store);
4378
4379static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004380sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004381{
4382 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004383 if (mddev->curr_resync == 0)
4384 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004385 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4386 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004387 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004388 db = resync - mddev->resync_mark_cnt;
4389 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004390}
4391
NeilBrown80ca3a42006-07-10 04:44:18 -07004392static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004393
4394static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004395sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004396{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004397 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004398
NeilBrownacb180b2009-04-14 16:28:34 +10004399 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4400 return sprintf(page, "none\n");
4401
NeilBrown72f36d52012-10-11 14:25:57 +11004402 if (mddev->curr_resync == 1 ||
4403 mddev->curr_resync == 2)
4404 return sprintf(page, "delayed\n");
4405
NeilBrownc804cde2012-05-21 09:28:33 +10004406 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4407 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004408 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004409 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004410 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004411
NeilBrownacb180b2009-04-14 16:28:34 +10004412 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004413 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004414}
4415
NeilBrown80ca3a42006-07-10 04:44:18 -07004416static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004417
NeilBrowne464eaf2006-03-27 01:18:14 -08004418static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004419min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004420{
4421 return sprintf(page, "%llu\n",
4422 (unsigned long long)mddev->resync_min);
4423}
4424static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004425min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004426{
4427 unsigned long long min;
4428 if (strict_strtoull(buf, 10, &min))
4429 return -EINVAL;
4430 if (min > mddev->resync_max)
4431 return -EINVAL;
4432 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4433 return -EBUSY;
4434
4435 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004436 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004437 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004438 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004439 return -EINVAL;
4440 }
4441 mddev->resync_min = min;
4442
4443 return len;
4444}
4445
4446static struct md_sysfs_entry md_min_sync =
4447__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4448
4449static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004450max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004451{
4452 if (mddev->resync_max == MaxSector)
4453 return sprintf(page, "max\n");
4454 else
4455 return sprintf(page, "%llu\n",
4456 (unsigned long long)mddev->resync_max);
4457}
4458static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004459max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004460{
4461 if (strncmp(buf, "max", 3) == 0)
4462 mddev->resync_max = MaxSector;
4463 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004464 unsigned long long max;
4465 if (strict_strtoull(buf, 10, &max))
4466 return -EINVAL;
4467 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004468 return -EINVAL;
4469 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004470 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004471 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4472 return -EBUSY;
4473
4474 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004475 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004476 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004477 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004478 return -EINVAL;
4479 }
4480 mddev->resync_max = max;
4481 }
4482 wake_up(&mddev->recovery_wait);
4483 return len;
4484}
4485
4486static struct md_sysfs_entry md_max_sync =
4487__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4488
4489static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004490suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004491{
4492 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4493}
4494
4495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004496suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004497{
4498 char *e;
4499 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004500 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004501
NeilBrownb8d966e2009-07-01 11:14:04 +10004502 if (mddev->pers == NULL ||
4503 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004504 return -EINVAL;
4505 if (buf == e || (*e && *e != '\n'))
4506 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004507
4508 mddev->suspend_lo = new;
4509 if (new >= old)
4510 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004511 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004512 else {
4513 /* Expanding suspended region - need to wait */
4514 mddev->pers->quiesce(mddev, 1);
4515 mddev->pers->quiesce(mddev, 0);
4516 }
4517 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004518}
4519static struct md_sysfs_entry md_suspend_lo =
4520__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4521
4522
4523static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004524suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004525{
4526 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4527}
4528
4529static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004530suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004531{
4532 char *e;
4533 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004534 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004535
NeilBrownb8d966e2009-07-01 11:14:04 +10004536 if (mddev->pers == NULL ||
4537 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004538 return -EINVAL;
4539 if (buf == e || (*e && *e != '\n'))
4540 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004541
4542 mddev->suspend_hi = new;
4543 if (new <= old)
4544 /* Shrinking suspended region */
4545 mddev->pers->quiesce(mddev, 2);
4546 else {
4547 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004548 mddev->pers->quiesce(mddev, 1);
4549 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004550 }
4551 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004552}
4553static struct md_sysfs_entry md_suspend_hi =
4554__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4555
NeilBrown08a02ec2007-05-09 02:35:38 -07004556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004557reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004558{
4559 if (mddev->reshape_position != MaxSector)
4560 return sprintf(page, "%llu\n",
4561 (unsigned long long)mddev->reshape_position);
4562 strcpy(page, "none\n");
4563 return 5;
4564}
4565
4566static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004567reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004568{
NeilBrownc6563a82012-05-21 09:27:00 +10004569 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004570 char *e;
4571 unsigned long long new = simple_strtoull(buf, &e, 10);
4572 if (mddev->pers)
4573 return -EBUSY;
4574 if (buf == e || (*e && *e != '\n'))
4575 return -EINVAL;
4576 mddev->reshape_position = new;
4577 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004578 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004579 mddev->new_level = mddev->level;
4580 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004581 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004582 rdev_for_each(rdev, mddev)
4583 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004584 return len;
4585}
4586
4587static struct md_sysfs_entry md_reshape_position =
4588__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4589 reshape_position_store);
4590
Dan Williamsb522adc2009-03-31 15:00:31 +11004591static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004592reshape_direction_show(struct mddev *mddev, char *page)
4593{
4594 return sprintf(page, "%s\n",
4595 mddev->reshape_backwards ? "backwards" : "forwards");
4596}
4597
4598static ssize_t
4599reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4600{
4601 int backwards = 0;
4602 if (cmd_match(buf, "forwards"))
4603 backwards = 0;
4604 else if (cmd_match(buf, "backwards"))
4605 backwards = 1;
4606 else
4607 return -EINVAL;
4608 if (mddev->reshape_backwards == backwards)
4609 return len;
4610
4611 /* check if we are allowed to change */
4612 if (mddev->delta_disks)
4613 return -EBUSY;
4614
4615 if (mddev->persistent &&
4616 mddev->major_version == 0)
4617 return -EINVAL;
4618
4619 mddev->reshape_backwards = backwards;
4620 return len;
4621}
4622
4623static struct md_sysfs_entry md_reshape_direction =
4624__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4625 reshape_direction_store);
4626
4627static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004628array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004629{
4630 if (mddev->external_size)
4631 return sprintf(page, "%llu\n",
4632 (unsigned long long)mddev->array_sectors/2);
4633 else
4634 return sprintf(page, "default\n");
4635}
4636
4637static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004638array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004639{
4640 sector_t sectors;
4641
4642 if (strncmp(buf, "default", 7) == 0) {
4643 if (mddev->pers)
4644 sectors = mddev->pers->size(mddev, 0, 0);
4645 else
4646 sectors = mddev->array_sectors;
4647
4648 mddev->external_size = 0;
4649 } else {
4650 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4651 return -EINVAL;
4652 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004653 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004654
4655 mddev->external_size = 1;
4656 }
4657
4658 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004659 if (mddev->pers) {
4660 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004661 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004662 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004663 return len;
4664}
4665
4666static struct md_sysfs_entry md_array_size =
4667__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4668 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004669
NeilBrowneae17012005-11-08 21:39:23 -08004670static struct attribute *md_default_attrs[] = {
4671 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004672 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004673 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004674 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004675 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004676 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004677 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004678 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004679 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004680 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004681 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004682 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004683 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004684 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004685 NULL,
4686};
4687
4688static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004689 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004690 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004691 &md_sync_min.attr,
4692 &md_sync_max.attr,
4693 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004694 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004695 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004696 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004697 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004698 &md_suspend_lo.attr,
4699 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004700 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004701 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004702 NULL,
4703};
NeilBrown411036f2005-11-08 21:39:40 -08004704static struct attribute_group md_redundancy_group = {
4705 .name = NULL,
4706 .attrs = md_redundancy_attrs,
4707};
4708
NeilBrowneae17012005-11-08 21:39:23 -08004709
4710static ssize_t
4711md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4712{
4713 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004714 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004715 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004716
4717 if (!entry->show)
4718 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004719 spin_lock(&all_mddevs_lock);
4720 if (list_empty(&mddev->all_mddevs)) {
4721 spin_unlock(&all_mddevs_lock);
4722 return -EBUSY;
4723 }
4724 mddev_get(mddev);
4725 spin_unlock(&all_mddevs_lock);
4726
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004727 rv = mddev_lock(mddev);
4728 if (!rv) {
4729 rv = entry->show(mddev, page);
4730 mddev_unlock(mddev);
4731 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004732 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004733 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004734}
4735
4736static ssize_t
4737md_attr_store(struct kobject *kobj, struct attribute *attr,
4738 const char *page, size_t length)
4739{
4740 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004741 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004742 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004743
4744 if (!entry->store)
4745 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004746 if (!capable(CAP_SYS_ADMIN))
4747 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004748 spin_lock(&all_mddevs_lock);
4749 if (list_empty(&mddev->all_mddevs)) {
4750 spin_unlock(&all_mddevs_lock);
4751 return -EBUSY;
4752 }
4753 mddev_get(mddev);
4754 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004755 if (entry->store == new_dev_store)
4756 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004757 rv = mddev_lock(mddev);
4758 if (!rv) {
4759 rv = entry->store(mddev, page, length);
4760 mddev_unlock(mddev);
4761 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004762 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004763 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004764}
4765
4766static void md_free(struct kobject *ko)
4767{
NeilBrownfd01b882011-10-11 16:47:53 +11004768 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004769
4770 if (mddev->sysfs_state)
4771 sysfs_put(mddev->sysfs_state);
4772
4773 if (mddev->gendisk) {
4774 del_gendisk(mddev->gendisk);
4775 put_disk(mddev->gendisk);
4776 }
4777 if (mddev->queue)
4778 blk_cleanup_queue(mddev->queue);
4779
NeilBrowneae17012005-11-08 21:39:23 -08004780 kfree(mddev);
4781}
4782
Emese Revfy52cf25d2010-01-19 02:58:23 +01004783static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004784 .show = md_attr_show,
4785 .store = md_attr_store,
4786};
4787static struct kobj_type md_ktype = {
4788 .release = md_free,
4789 .sysfs_ops = &md_sysfs_ops,
4790 .default_attrs = md_default_attrs,
4791};
4792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793int mdp_major = 0;
4794
Dan Williams5fd3a172009-03-04 00:57:25 -07004795static void mddev_delayed_delete(struct work_struct *ws)
4796{
NeilBrownfd01b882011-10-11 16:47:53 +11004797 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004798
NeilBrown43a70502009-12-14 12:49:55 +11004799 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004800 kobject_del(&mddev->kobj);
4801 kobject_put(&mddev->kobj);
4802}
4803
NeilBrownefeb53c2009-01-09 08:31:10 +11004804static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004806 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004807 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004809 int partitioned;
4810 int shift;
4811 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004812 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004815 return -ENODEV;
4816
4817 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4818 shift = partitioned ? MdpMinorShift : 0;
4819 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Tejun Heoe804ac72010-10-15 15:36:08 +02004821 /* wait for any previous instance of this device to be
4822 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004823 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004824 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004825
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004826 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004827 error = -EEXIST;
4828 if (mddev->gendisk)
4829 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004830
4831 if (name) {
4832 /* Need to ensure that 'name' is not a duplicate.
4833 */
NeilBrownfd01b882011-10-11 16:47:53 +11004834 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004835 spin_lock(&all_mddevs_lock);
4836
4837 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4838 if (mddev2->gendisk &&
4839 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4840 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004841 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004842 }
4843 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
NeilBrown8b765392009-01-09 08:31:08 +11004845
NeilBrown0909dc42009-07-01 12:27:21 +10004846 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004847 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004848 if (!mddev->queue)
4849 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004850 mddev->queue->queuedata = mddev;
4851
NeilBrown409c57f2009-03-31 14:39:39 +11004852 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004853 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 disk = alloc_disk(1 << shift);
4856 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004857 blk_cleanup_queue(mddev->queue);
4858 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004859 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004861 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004863 if (name)
4864 strcpy(disk->disk_name, name);
4865 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004867 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 disk->fops = &md_fops;
4870 disk->private_data = mddev;
4871 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004872 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004873 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004874 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004875 * remove it now.
4876 */
4877 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004879 /* As soon as we call add_disk(), another thread could get
4880 * through to md_open, so make sure it doesn't get too far
4881 */
4882 mutex_lock(&mddev->open_mutex);
4883 add_disk(disk);
4884
Tejun Heoed9e1982008-08-25 19:56:05 +09004885 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4886 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004887 if (error) {
4888 /* This isn't possible, but as kobject_init_and_add is marked
4889 * __must_check, we must do something with the result
4890 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004891 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4892 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004893 error = 0;
4894 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004895 if (mddev->kobj.sd &&
4896 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004897 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004898 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004899 abort:
4900 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004901 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004902 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004903 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004904 }
NeilBrownd3374822009-01-09 08:31:10 +11004905 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004906 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004907}
4908
4909static struct kobject *md_probe(dev_t dev, int *part, void *data)
4910{
4911 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 return NULL;
4913}
4914
NeilBrownefeb53c2009-01-09 08:31:10 +11004915static int add_named_array(const char *val, struct kernel_param *kp)
4916{
4917 /* val must be "md_*" where * is not all digits.
4918 * We allocate an array with a large free minor number, and
4919 * set the name to val. val must not already be an active name.
4920 */
4921 int len = strlen(val);
4922 char buf[DISK_NAME_LEN];
4923
4924 while (len && val[len-1] == '\n')
4925 len--;
4926 if (len >= DISK_NAME_LEN)
4927 return -E2BIG;
4928 strlcpy(buf, val, len+1);
4929 if (strncmp(buf, "md_", 3) != 0)
4930 return -EINVAL;
4931 return md_alloc(0, buf);
4932}
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934static void md_safemode_timeout(unsigned long data)
4935{
NeilBrownfd01b882011-10-11 16:47:53 +11004936 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Neil Brown0fd62b82008-06-28 08:31:36 +10004938 if (!atomic_read(&mddev->writes_pending)) {
4939 mddev->safemode = 1;
4940 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004941 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 md_wakeup_thread(mddev->thread);
4944}
4945
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004946static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
NeilBrownfd01b882011-10-11 16:47:53 +11004948int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
NeilBrown2604b702006-01-06 00:20:36 -08004950 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004951 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004952 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
NeilBrowna757e642005-04-16 15:26:42 -07004954 if (list_empty(&mddev->disks))
4955 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 if (mddev->pers)
4959 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004960 /* Cannot run until previous stop completes properly */
4961 if (mddev->sysfs_active)
4962 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 /*
4965 * Analyze all RAID superblock(s)
4966 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004967 if (!mddev->raid_disks) {
4968 if (!mddev->persistent)
4969 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004970 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
NeilBrownd9d166c2006-01-06 00:20:51 -08004973 if (mddev->level != LEVEL_NONE)
4974 request_module("md-level-%d", mddev->level);
4975 else if (mddev->clevel[0])
4976 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 /*
4979 * Drop all container device buffers, from now on
4980 * the only valid external interface is through the md
4981 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004983 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004984 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 continue;
4986 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004987 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004988
4989 /* perform some consistency tests on the device.
4990 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004991 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004992 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004993 if (rdev->meta_bdev) {
4994 /* Nothing to check */;
4995 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004996 if (mddev->dev_sectors &&
4997 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004998 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004999 printk("md: %s: data overlaps metadata\n",
5000 mdname(mddev));
5001 return -EINVAL;
5002 }
5003 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005004 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005005 > rdev->data_offset) {
5006 printk("md: %s: metadata overlaps data\n",
5007 mdname(mddev));
5008 return -EINVAL;
5009 }
5010 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005011 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
5013
NeilBrowna167f662010-10-26 18:31:13 +11005014 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005015 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005018 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005019 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005021 if (mddev->level != LEVEL_NONE)
5022 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5023 mddev->level);
5024 else
5025 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5026 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 return -EINVAL;
5028 }
NeilBrown2604b702006-01-06 00:20:36 -08005029 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005031 if (mddev->level != pers->level) {
5032 mddev->level = pers->level;
5033 mddev->new_level = pers->level;
5034 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005035 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
NeilBrownf6705572006-03-27 01:18:11 -08005037 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005038 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005039 /* This personality cannot handle reshaping... */
5040 mddev->pers = NULL;
5041 module_put(pers->owner);
5042 return -EINVAL;
5043 }
5044
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005045 if (pers->sync_request) {
5046 /* Warn if this is a potentially silly
5047 * configuration.
5048 */
5049 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005050 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005051 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005052
NeilBrowndafb20f2012-03-19 12:46:39 +11005053 rdev_for_each(rdev, mddev)
5054 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005055 if (rdev < rdev2 &&
5056 rdev->bdev->bd_contains ==
5057 rdev2->bdev->bd_contains) {
5058 printk(KERN_WARNING
5059 "%s: WARNING: %s appears to be"
5060 " on the same physical disk as"
5061 " %s.\n",
5062 mdname(mddev),
5063 bdevname(rdev->bdev,b),
5064 bdevname(rdev2->bdev,b2));
5065 warned = 1;
5066 }
5067 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005068
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005069 if (warned)
5070 printk(KERN_WARNING
5071 "True protection against single-disk"
5072 " failure might be compromised.\n");
5073 }
5074
NeilBrown657390d2005-08-26 18:34:16 -07005075 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005076 /* may be over-ridden by personality */
5077 mddev->resync_max_sectors = mddev->dev_sectors;
5078
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005079 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
NeilBrown0f9552b2009-12-30 12:08:50 +11005081 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005082 mddev->ro = 2; /* read-only, but switch on first write */
5083
NeilBrownb15c2e52006-01-06 00:20:16 -08005084 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005085 if (err)
5086 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005087 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5088 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5089 " but 'external_size' not in effect?\n", __func__);
5090 printk(KERN_ERR
5091 "md: invalid array_size %llu > default size %llu\n",
5092 (unsigned long long)mddev->array_sectors / 2,
5093 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5094 err = -EINVAL;
5095 mddev->pers->stop(mddev);
5096 }
NeilBrownef99bf42012-05-22 13:55:08 +10005097 if (err == 0 && mddev->pers->sync_request &&
5098 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005099 err = bitmap_create(mddev);
5100 if (err) {
5101 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5102 mdname(mddev), err);
5103 mddev->pers->stop(mddev);
5104 }
5105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 module_put(mddev->pers->owner);
5108 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005109 bitmap_destroy(mddev);
5110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005112 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005113 if (mddev->kobj.sd &&
5114 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005115 printk(KERN_WARNING
5116 "md: cannot register extra attributes for %s\n",
5117 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005118 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005119 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005120 mddev->ro = 0;
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005123 atomic_set(&mddev->max_corr_read_errors,
5124 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 mddev->safemode = 0;
5126 mddev->safemode_timer.function = md_safemode_timeout;
5127 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005128 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005130 smp_wmb();
5131 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005132 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005133 if (rdev->raid_disk >= 0)
5134 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005135 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5138
NeilBrown850b2b42006-10-03 01:15:46 -07005139 if (mddev->flags)
5140 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
NeilBrownd7603b72006-01-06 00:20:30 -08005142 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005143 sysfs_notify_dirent_safe(mddev->sysfs_state);
5144 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005145 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 return 0;
5147}
NeilBrown390ee602010-06-01 19:37:27 +10005148EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
NeilBrownfd01b882011-10-11 16:47:53 +11005150static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005151{
5152 int err;
5153
5154 err = md_run(mddev);
5155 if (err)
5156 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005157 err = bitmap_load(mddev);
5158 if (err) {
5159 bitmap_destroy(mddev);
5160 goto out;
5161 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005162
5163 md_wakeup_thread(mddev->thread);
5164 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5165
NeilBrownfe60b012010-03-29 11:10:42 +11005166 set_capacity(mddev->gendisk, mddev->array_sectors);
5167 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005168 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005169 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5170out:
5171 return err;
5172}
5173
NeilBrownfd01b882011-10-11 16:47:53 +11005174static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175{
5176 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Andre Noll80fab1d2008-07-11 22:02:21 +10005178 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005180 return -ENXIO;
5181 if (!mddev->pers)
5182 return -EINVAL;
5183 if (!mddev->ro)
5184 return -EBUSY;
5185 mddev->safemode = 0;
5186 mddev->ro = 0;
5187 set_disk_ro(disk, 0);
5188 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5189 mdname(mddev));
5190 /* Kick recovery or resync if necessary */
5191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5192 md_wakeup_thread(mddev->thread);
5193 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005194 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196}
5197
NeilBrownacc55e22006-06-26 00:27:47 -07005198/* similar to deny_write_access, but accounts for our holding a reference
5199 * to the file ourselves */
5200static int deny_bitmap_write_access(struct file * file)
5201{
5202 struct inode *inode = file->f_mapping->host;
5203
5204 spin_lock(&inode->i_lock);
5205 if (atomic_read(&inode->i_writecount) > 1) {
5206 spin_unlock(&inode->i_lock);
5207 return -ETXTBSY;
5208 }
5209 atomic_set(&inode->i_writecount, -1);
5210 spin_unlock(&inode->i_lock);
5211
5212 return 0;
5213}
5214
NeilBrown43a70502009-12-14 12:49:55 +11005215void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005216{
5217 struct inode *inode = file->f_mapping->host;
5218
5219 spin_lock(&inode->i_lock);
5220 atomic_set(&inode->i_writecount, 1);
5221 spin_unlock(&inode->i_lock);
5222}
5223
NeilBrownfd01b882011-10-11 16:47:53 +11005224static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005225{
5226 mddev->array_sectors = 0;
5227 mddev->external_size = 0;
5228 mddev->dev_sectors = 0;
5229 mddev->raid_disks = 0;
5230 mddev->recovery_cp = 0;
5231 mddev->resync_min = 0;
5232 mddev->resync_max = MaxSector;
5233 mddev->reshape_position = MaxSector;
5234 mddev->external = 0;
5235 mddev->persistent = 0;
5236 mddev->level = LEVEL_NONE;
5237 mddev->clevel[0] = 0;
5238 mddev->flags = 0;
5239 mddev->ro = 0;
5240 mddev->metadata_type[0] = 0;
5241 mddev->chunk_sectors = 0;
5242 mddev->ctime = mddev->utime = 0;
5243 mddev->layout = 0;
5244 mddev->max_disks = 0;
5245 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005246 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005247 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005248 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005249 mddev->new_level = LEVEL_NONE;
5250 mddev->new_layout = 0;
5251 mddev->new_chunk_sectors = 0;
5252 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005253 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005254 mddev->suspend_lo = mddev->suspend_hi = 0;
5255 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5256 mddev->recovery = 0;
5257 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005258 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005259 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005260 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005261 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005262 mddev->bitmap_info.offset = 0;
5263 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005264 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005265 mddev->bitmap_info.chunksize = 0;
5266 mddev->bitmap_info.daemon_sleep = 0;
5267 mddev->bitmap_info.max_write_behind = 0;
5268}
5269
NeilBrownfd01b882011-10-11 16:47:53 +11005270static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005271{
5272 if (mddev->sync_thread) {
5273 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5274 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005275 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005276 }
5277
5278 del_timer_sync(&mddev->safemode_timer);
5279
5280 bitmap_flush(mddev);
5281 md_super_wait(mddev);
5282
5283 if (!mddev->in_sync || mddev->flags) {
5284 /* mark array as shutdown cleanly */
5285 mddev->in_sync = 1;
5286 md_update_sb(mddev, 1);
5287 }
5288}
NeilBrowndefad612011-01-14 09:14:33 +11005289
NeilBrownfd01b882011-10-11 16:47:53 +11005290void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005291{
5292 mddev_lock(mddev);
5293 __md_stop_writes(mddev);
5294 mddev_unlock(mddev);
5295}
NeilBrown390ee602010-06-01 19:37:27 +10005296EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005297
NeilBrown5eff3c42012-11-19 10:47:48 +11005298static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005299{
NeilBrown0ca69882011-01-14 09:14:33 +11005300 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005301 mddev->pers->stop(mddev);
5302 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5303 mddev->to_remove = &md_redundancy_group;
5304 module_put(mddev->pers->owner);
5305 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005306 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005307}
NeilBrown5eff3c42012-11-19 10:47:48 +11005308
5309void md_stop(struct mddev *mddev)
5310{
5311 /* stop the array and free an attached data structures.
5312 * This is called from dm-raid
5313 */
5314 __md_stop(mddev);
5315 bitmap_destroy(mddev);
5316 if (mddev->bio_set)
5317 bioset_free(mddev->bio_set);
5318}
5319
NeilBrown390ee602010-06-01 19:37:27 +10005320EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005321
NeilBrowna05b7ea2012-07-19 15:59:18 +10005322static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005323{
5324 int err = 0;
5325 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005326 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005327 printk("md: %s still in use.\n",mdname(mddev));
5328 err = -EBUSY;
5329 goto out;
5330 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005331 if (bdev)
5332 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005333 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005334 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005335
5336 err = -ENXIO;
5337 if (mddev->ro==1)
5338 goto out;
5339 mddev->ro = 1;
5340 set_disk_ro(mddev->gendisk, 1);
5341 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005342 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005343 err = 0;
5344 }
5345out:
5346 mutex_unlock(&mddev->open_mutex);
5347 return err;
5348}
5349
NeilBrown9e653b62006-06-26 00:27:58 -07005350/* mode:
5351 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005352 * 2 - stop but do not disassemble array
5353 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005354static int do_md_stop(struct mddev * mddev, int mode,
5355 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005358 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
NeilBrownc8c00a62009-08-10 12:50:52 +10005360 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005361 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005362 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005363 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005364 mutex_unlock(&mddev->open_mutex);
5365 return -EBUSY;
5366 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005367 if (bdev)
5368 /* It is possible IO was issued on some other
5369 * open file which was closed before we took ->open_mutex.
5370 * As that was not the last close __blkdev_put will not
5371 * have called sync_blockdev, so we must.
5372 */
5373 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
NeilBrown6e17b022010-08-07 21:41:19 +10005375 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005376 if (mddev->ro)
5377 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005378
NeilBrowndefad612011-01-14 09:14:33 +11005379 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005380 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005381 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005382 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005383
NeilBrowna4bd82d2010-03-29 13:23:10 +11005384 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005385 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005386
NeilBrowndafb20f2012-03-19 12:46:39 +11005387 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005388 if (rdev->raid_disk >= 0)
5389 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005390
NeilBrowna4bd82d2010-03-29 13:23:10 +11005391 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005392 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005393 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005394 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005395
NeilBrowna4bd82d2010-03-29 13:23:10 +11005396 if (mddev->ro)
5397 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005398 } else
5399 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 /*
5401 * Free resources if final stop
5402 */
NeilBrown9e653b62006-06-26 00:27:58 -07005403 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5405
NeilBrown978f9462006-02-02 14:28:05 -08005406 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005407 if (mddev->bitmap_info.file) {
5408 restore_bitmap_write_access(mddev->bitmap_info.file);
5409 fput(mddev->bitmap_info.file);
5410 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005411 }
NeilBrownc3d97142009-12-14 12:49:52 +11005412 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 export_array(mddev);
5415
NeilBrown6177b472010-03-29 11:37:13 +11005416 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005417 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005418 if (mddev->hold_active == UNTIL_STOP)
5419 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005420 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005421 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005422 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005423 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005427#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005428static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429{
NeilBrown3cb03002011-10-11 16:45:26 +11005430 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 int err;
5432
NeilBrowna757e642005-04-16 15:26:42 -07005433 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
5436 printk(KERN_INFO "md: running: ");
5437
NeilBrowndafb20f2012-03-19 12:46:39 +11005438 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 char b[BDEVNAME_SIZE];
5440 printk("<%s>", bdevname(rdev->bdev,b));
5441 }
5442 printk("\n");
5443
NeilBrownd710e132008-10-13 11:55:12 +11005444 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 if (err) {
5446 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005447 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449}
5450
5451/*
5452 * lets try to run arrays based on all disks that have arrived
5453 * until now. (those are in pending_raid_disks)
5454 *
5455 * the method: pick the first pending disk, collect all disks with
5456 * the same UUID, remove all from the pending list and put them into
5457 * the 'same_array' list. Then order this list based on superblock
5458 * update time (freshest comes first), kick out 'old' disks and
5459 * compare superblocks. If everything's fine then run it.
5460 *
5461 * If "unit" is allocated, then bump its reference count
5462 */
5463static void autorun_devices(int part)
5464{
NeilBrown3cb03002011-10-11 16:45:26 +11005465 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005466 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 char b[BDEVNAME_SIZE];
5468
5469 printk(KERN_INFO "md: autorun ...\n");
5470 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005471 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005473 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005475 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 printk(KERN_INFO "md: considering %s ...\n",
5478 bdevname(rdev0->bdev,b));
5479 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005480 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (super_90_load(rdev, rdev0, 0) >= 0) {
5482 printk(KERN_INFO "md: adding %s ...\n",
5483 bdevname(rdev->bdev,b));
5484 list_move(&rdev->same_set, &candidates);
5485 }
5486 /*
5487 * now we have a set of devices, with all of them having
5488 * mostly sane superblocks. It's time to allocate the
5489 * mddev.
5490 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005491 if (part) {
5492 dev = MKDEV(mdp_major,
5493 rdev0->preferred_minor << MdpMinorShift);
5494 unit = MINOR(dev) >> MdpMinorShift;
5495 } else {
5496 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5497 unit = MINOR(dev);
5498 }
5499 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5501 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5502 break;
5503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 md_probe(dev, NULL, NULL);
5506 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005507 if (!mddev || !mddev->gendisk) {
5508 if (mddev)
5509 mddev_put(mddev);
5510 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 "md: cannot allocate memory for md drive.\n");
5512 break;
5513 }
5514 if (mddev_lock(mddev))
5515 printk(KERN_WARNING "md: %s locked, cannot run\n",
5516 mdname(mddev));
5517 else if (mddev->raid_disks || mddev->major_version
5518 || !list_empty(&mddev->disks)) {
5519 printk(KERN_WARNING
5520 "md: %s already running, cannot run %s\n",
5521 mdname(mddev), bdevname(rdev0->bdev,b));
5522 mddev_unlock(mddev);
5523 } else {
5524 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005525 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005526 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 list_del_init(&rdev->same_set);
5528 if (bind_rdev_to_array(rdev, mddev))
5529 export_rdev(rdev);
5530 }
5531 autorun_array(mddev);
5532 mddev_unlock(mddev);
5533 }
5534 /* on success, candidates will be empty, on error
5535 * it won't...
5536 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005537 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005538 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 mddev_put(mddev);
5542 }
5543 printk(KERN_INFO "md: ... autorun DONE.\n");
5544}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005545#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547static int get_version(void __user * arg)
5548{
5549 mdu_version_t ver;
5550
5551 ver.major = MD_MAJOR_VERSION;
5552 ver.minor = MD_MINOR_VERSION;
5553 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5554
5555 if (copy_to_user(arg, &ver, sizeof(ver)))
5556 return -EFAULT;
5557
5558 return 0;
5559}
5560
NeilBrownfd01b882011-10-11 16:47:53 +11005561static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
5563 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005564 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005565 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
NeilBrown1ca69c42012-10-11 13:37:33 +11005567 nr = working = insync = failed = spare = 0;
5568 rcu_read_lock();
5569 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005571 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 failed++;
5573 else {
5574 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005575 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005576 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 else
5578 spare++;
5579 }
5580 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005581 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 info.major_version = mddev->major_version;
5584 info.minor_version = mddev->minor_version;
5585 info.patch_version = MD_PATCHLEVEL_VERSION;
5586 info.ctime = mddev->ctime;
5587 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005588 info.size = mddev->dev_sectors / 2;
5589 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005590 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 info.nr_disks = nr;
5592 info.raid_disks = mddev->raid_disks;
5593 info.md_minor = mddev->md_minor;
5594 info.not_persistent= !mddev->persistent;
5595
5596 info.utime = mddev->utime;
5597 info.state = 0;
5598 if (mddev->in_sync)
5599 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005600 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005601 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005602 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 info.working_disks = working;
5604 info.failed_disks = failed;
5605 info.spare_disks = spare;
5606
5607 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005608 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 if (copy_to_user(arg, &info, sizeof(info)))
5611 return -EFAULT;
5612
5613 return 0;
5614}
5615
NeilBrownfd01b882011-10-11 16:47:53 +11005616static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005617{
5618 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5619 char *ptr, *buf = NULL;
5620 int err = -ENOMEM;
5621
Dan Williamsb5470dc2008-06-27 21:44:04 -07005622 if (md_allow_write(mddev))
5623 file = kmalloc(sizeof(*file), GFP_NOIO);
5624 else
5625 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005626
NeilBrown32a76272005-06-21 17:17:14 -07005627 if (!file)
5628 goto out;
5629
5630 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005631 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005632 file->pathname[0] = '\0';
5633 goto copy_out;
5634 }
5635
5636 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5637 if (!buf)
5638 goto out;
5639
NeilBrown1ec885c2012-05-22 13:55:10 +10005640 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5641 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005642 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005643 goto out;
5644
5645 strcpy(file->pathname, ptr);
5646
5647copy_out:
5648 err = 0;
5649 if (copy_to_user(arg, file, sizeof(*file)))
5650 err = -EFAULT;
5651out:
5652 kfree(buf);
5653 kfree(file);
5654 return err;
5655}
5656
NeilBrownfd01b882011-10-11 16:47:53 +11005657static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
5659 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005660 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662 if (copy_from_user(&info, arg, sizeof(info)))
5663 return -EFAULT;
5664
NeilBrown1ca69c42012-10-11 13:37:33 +11005665 rcu_read_lock();
5666 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (rdev) {
5668 info.major = MAJOR(rdev->bdev->bd_dev);
5669 info.minor = MINOR(rdev->bdev->bd_dev);
5670 info.raid_disk = rdev->raid_disk;
5671 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005674 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 info.state |= (1<<MD_DISK_ACTIVE);
5676 info.state |= (1<<MD_DISK_SYNC);
5677 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005678 if (test_bit(WriteMostly, &rdev->flags))
5679 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 } else {
5681 info.major = info.minor = 0;
5682 info.raid_disk = -1;
5683 info.state = (1<<MD_DISK_REMOVED);
5684 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005685 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 if (copy_to_user(arg, &info, sizeof(info)))
5688 return -EFAULT;
5689
5690 return 0;
5691}
5692
NeilBrownfd01b882011-10-11 16:47:53 +11005693static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005696 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 dev_t dev = MKDEV(info->major,info->minor);
5698
5699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5700 return -EOVERFLOW;
5701
5702 if (!mddev->raid_disks) {
5703 int err;
5704 /* expecting a device which has a superblock */
5705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5706 if (IS_ERR(rdev)) {
5707 printk(KERN_WARNING
5708 "md: md_import_device returned %ld\n",
5709 PTR_ERR(rdev));
5710 return PTR_ERR(rdev);
5711 }
5712 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005713 struct md_rdev *rdev0
5714 = list_entry(mddev->disks.next,
5715 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005716 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 .load_super(rdev, rdev0, mddev->minor_version);
5718 if (err < 0) {
5719 printk(KERN_WARNING
5720 "md: %s has different UUID to %s\n",
5721 bdevname(rdev->bdev,b),
5722 bdevname(rdev0->bdev,b2));
5723 export_rdev(rdev);
5724 return -EINVAL;
5725 }
5726 }
5727 err = bind_rdev_to_array(rdev, mddev);
5728 if (err)
5729 export_rdev(rdev);
5730 return err;
5731 }
5732
5733 /*
5734 * add_new_disk can be used once the array is assembled
5735 * to add "hot spares". They must already have a superblock
5736 * written
5737 */
5738 if (mddev->pers) {
5739 int err;
5740 if (!mddev->pers->hot_add_disk) {
5741 printk(KERN_WARNING
5742 "%s: personality does not support diskops!\n",
5743 mdname(mddev));
5744 return -EINVAL;
5745 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005746 if (mddev->persistent)
5747 rdev = md_import_device(dev, mddev->major_version,
5748 mddev->minor_version);
5749 else
5750 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (IS_ERR(rdev)) {
5752 printk(KERN_WARNING
5753 "md: md_import_device returned %ld\n",
5754 PTR_ERR(rdev));
5755 return PTR_ERR(rdev);
5756 }
NeilBrown1a855a02010-12-09 16:36:28 +11005757 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005758 if (!mddev->persistent) {
5759 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005760 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005761 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005762 set_bit(In_sync, &rdev->flags);
5763 } else
NeilBrown41158c72005-06-21 17:17:25 -07005764 rdev->raid_disk = -1;
5765 } else
5766 super_types[mddev->major_version].
5767 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005768 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005769 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005770 /* This was a hot-add request, but events doesn't
5771 * match, so reject it.
5772 */
5773 export_rdev(rdev);
5774 return -EINVAL;
5775 }
5776
NeilBrown1a855a02010-12-09 16:36:28 +11005777 if (test_bit(In_sync, &rdev->flags))
5778 rdev->saved_raid_disk = rdev->raid_disk;
5779 else
5780 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005781
NeilBrownb2d444d2005-11-08 21:39:31 -08005782 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005783 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5784 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005785 else
5786 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 rdev->raid_disk = -1;
5789 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005790 if (!err && !mddev->pers->hot_remove_disk) {
5791 /* If there is hot_add_disk but no hot_remove_disk
5792 * then added disks for geometry changes,
5793 * and should be added immediately.
5794 */
5795 super_types[mddev->major_version].
5796 validate_super(mddev, rdev);
5797 err = mddev->pers->hot_add_disk(mddev, rdev);
5798 if (err)
5799 unbind_rdev_from_array(rdev);
5800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 if (err)
5802 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005803 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005804 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005805
NeilBrown17571282006-12-10 02:20:52 -08005806 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005807 if (mddev->degraded)
5808 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005809 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005810 if (!err)
5811 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005812 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 return err;
5814 }
5815
5816 /* otherwise, add_new_disk is only allowed
5817 * for major_version==0 superblocks
5818 */
5819 if (mddev->major_version != 0) {
5820 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5821 mdname(mddev));
5822 return -EINVAL;
5823 }
5824
5825 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5826 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005827 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (IS_ERR(rdev)) {
5829 printk(KERN_WARNING
5830 "md: error, md_import_device() returned %ld\n",
5831 PTR_ERR(rdev));
5832 return PTR_ERR(rdev);
5833 }
5834 rdev->desc_nr = info->number;
5835 if (info->raid_disk < mddev->raid_disks)
5836 rdev->raid_disk = info->raid_disk;
5837 else
5838 rdev->raid_disk = -1;
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005841 if (info->state & (1<<MD_DISK_SYNC))
5842 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005844 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5845 set_bit(WriteMostly, &rdev->flags);
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 if (!mddev->persistent) {
5848 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005849 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5850 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005851 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005852 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
NeilBrown2bf071b2006-01-06 00:20:55 -08005854 err = bind_rdev_to_array(rdev, mddev);
5855 if (err) {
5856 export_rdev(rdev);
5857 return err;
5858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
5860
5861 return 0;
5862}
5863
NeilBrownfd01b882011-10-11 16:47:53 +11005864static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865{
5866 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005867 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 rdev = find_rdev(mddev, dev);
5870 if (!rdev)
5871 return -ENXIO;
5872
5873 if (rdev->raid_disk >= 0)
5874 goto busy;
5875
5876 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005877 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005878 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
5880 return 0;
5881busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005882 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 bdevname(rdev->bdev,b), mdname(mddev));
5884 return -EBUSY;
5885}
5886
NeilBrownfd01b882011-10-11 16:47:53 +11005887static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888{
5889 char b[BDEVNAME_SIZE];
5890 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005891 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893 if (!mddev->pers)
5894 return -ENODEV;
5895
5896 if (mddev->major_version != 0) {
5897 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5898 " version-0 superblocks.\n",
5899 mdname(mddev));
5900 return -EINVAL;
5901 }
5902 if (!mddev->pers->hot_add_disk) {
5903 printk(KERN_WARNING
5904 "%s: personality does not support diskops!\n",
5905 mdname(mddev));
5906 return -EINVAL;
5907 }
5908
NeilBrownd710e132008-10-13 11:55:12 +11005909 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 if (IS_ERR(rdev)) {
5911 printk(KERN_WARNING
5912 "md: error, md_import_device() returned %ld\n",
5913 PTR_ERR(rdev));
5914 return -EINVAL;
5915 }
5916
5917 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005918 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005920 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
NeilBrown8190e752009-06-18 08:48:58 +10005922 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
NeilBrownb2d444d2005-11-08 21:39:31 -08005924 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 printk(KERN_WARNING
5926 "md: can not hot-add faulty %s disk to %s!\n",
5927 bdevname(rdev->bdev,b), mdname(mddev));
5928 err = -EINVAL;
5929 goto abort_export;
5930 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005931 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005933 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005934 err = bind_rdev_to_array(rdev, mddev);
5935 if (err)
5936 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
5938 /*
5939 * The rest should better be atomic, we can have disk failures
5940 * noticed in interrupt contexts ...
5941 */
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 rdev->raid_disk = -1;
5944
NeilBrown850b2b42006-10-03 01:15:46 -07005945 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 /*
5948 * Kick recovery, maybe this spare has to be added to the
5949 * array immediately.
5950 */
5951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5952 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005953 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 return 0;
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956abort_export:
5957 export_rdev(rdev);
5958 return err;
5959}
5960
NeilBrownfd01b882011-10-11 16:47:53 +11005961static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005962{
5963 int err;
5964
NeilBrown36fa3062005-09-09 16:23:45 -07005965 if (mddev->pers) {
5966 if (!mddev->pers->quiesce)
5967 return -EBUSY;
5968 if (mddev->recovery || mddev->sync_thread)
5969 return -EBUSY;
5970 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005971 }
5972
NeilBrown36fa3062005-09-09 16:23:45 -07005973
5974 if (fd >= 0) {
5975 if (mddev->bitmap)
5976 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005977 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005978
NeilBrownc3d97142009-12-14 12:49:52 +11005979 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005980 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5981 mdname(mddev));
5982 return -EBADF;
5983 }
5984
NeilBrownc3d97142009-12-14 12:49:52 +11005985 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005986 if (err) {
5987 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5988 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005989 fput(mddev->bitmap_info.file);
5990 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005991 return err;
5992 }
NeilBrownc3d97142009-12-14 12:49:52 +11005993 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005994 } else if (mddev->bitmap == NULL)
5995 return -ENOENT; /* cannot remove what isn't there */
5996 err = 0;
5997 if (mddev->pers) {
5998 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005999 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006000 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006001 if (!err)
6002 err = bitmap_load(mddev);
6003 }
NeilBrownd7375ab322006-06-26 00:27:43 -07006004 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006005 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07006006 fd = -1; /* make sure to put the file */
6007 }
NeilBrown36fa3062005-09-09 16:23:45 -07006008 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07006009 }
6010 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006011 if (mddev->bitmap_info.file) {
6012 restore_bitmap_write_access(mddev->bitmap_info.file);
6013 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006014 }
NeilBrownc3d97142009-12-14 12:49:52 +11006015 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006016 }
6017
NeilBrown32a76272005-06-21 17:17:14 -07006018 return err;
6019}
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021/*
6022 * set_array_info is used two different ways
6023 * The original usage is when creating a new array.
6024 * In this usage, raid_disks is > 0 and it together with
6025 * level, size, not_persistent,layout,chunksize determine the
6026 * shape of the array.
6027 * This will always create an array with a type-0.90.0 superblock.
6028 * The newer usage is when assembling an array.
6029 * In this case raid_disks will be 0, and the major_version field is
6030 * use to determine which style super-blocks are to be found on the devices.
6031 * The minor and patch _version numbers are also kept incase the
6032 * super_block handler wishes to interpret them.
6033 */
NeilBrownfd01b882011-10-11 16:47:53 +11006034static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035{
6036
6037 if (info->raid_disks == 0) {
6038 /* just setting version number for superblock loading */
6039 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006040 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 super_types[info->major_version].name == NULL) {
6042 /* maybe try to auto-load a module? */
6043 printk(KERN_INFO
6044 "md: superblock version %d not known\n",
6045 info->major_version);
6046 return -EINVAL;
6047 }
6048 mddev->major_version = info->major_version;
6049 mddev->minor_version = info->minor_version;
6050 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006051 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006052 /* ensure mddev_put doesn't delete this now that there
6053 * is some minimal configuration.
6054 */
6055 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 return 0;
6057 }
6058 mddev->major_version = MD_MAJOR_VERSION;
6059 mddev->minor_version = MD_MINOR_VERSION;
6060 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6061 mddev->ctime = get_seconds();
6062
6063 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006064 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006065 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 mddev->raid_disks = info->raid_disks;
6067 /* don't set md_minor, it is determined by which /dev/md* was
6068 * openned
6069 */
6070 if (info->state & (1<<MD_SB_CLEAN))
6071 mddev->recovery_cp = MaxSector;
6072 else
6073 mddev->recovery_cp = 0;
6074 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006075 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
6077 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006078 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 mddev->max_disks = MD_SB_DISKS;
6081
NeilBrowne6910632008-02-06 01:39:51 -08006082 if (mddev->persistent)
6083 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006084 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
NeilBrownc3d97142009-12-14 12:49:52 +11006086 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006087 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006088 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006089
NeilBrownf6705572006-03-27 01:18:11 -08006090 mddev->reshape_position = MaxSector;
6091
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 /*
6093 * Generate a 128 bit UUID
6094 */
6095 get_random_bytes(mddev->uuid, 16);
6096
NeilBrownf6705572006-03-27 01:18:11 -08006097 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006098 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006099 mddev->new_layout = mddev->layout;
6100 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006101 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006102
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 return 0;
6104}
6105
NeilBrownfd01b882011-10-11 16:47:53 +11006106void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006107{
Dan Williamsb522adc2009-03-31 15:00:31 +11006108 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6109
6110 if (mddev->external_size)
6111 return;
6112
Dan Williams1f403622009-03-31 14:59:03 +11006113 mddev->array_sectors = array_sectors;
6114}
6115EXPORT_SYMBOL(md_set_array_sectors);
6116
NeilBrownfd01b882011-10-11 16:47:53 +11006117static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006118{
NeilBrown3cb03002011-10-11 16:45:26 +11006119 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006120 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006121 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006122
6123 if (mddev->pers->resize == NULL)
6124 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006125 /* The "num_sectors" is the number of sectors of each device that
6126 * is used. This can only make sense for arrays with redundancy.
6127 * linear and raid0 always use whatever space is available. We can only
6128 * consider changing this number if no resync or reconstruction is
6129 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006130 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006131 * of each device. If num_sectors is zero, we find the largest size
6132 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006133 */
6134 if (mddev->sync_thread)
6135 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006136
NeilBrowndafb20f2012-03-19 12:46:39 +11006137 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006138 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006139
Andre Nolld71f9f82008-07-11 22:02:22 +10006140 if (fit && (num_sectors == 0 || num_sectors > avail))
6141 num_sectors = avail;
6142 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006143 return -ENOSPC;
6144 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006145 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006146 if (!rv)
6147 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006148 return rv;
6149}
6150
NeilBrownfd01b882011-10-11 16:47:53 +11006151static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006152{
6153 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006154 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006155 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006156 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006157 return -EINVAL;
6158 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006159 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006160 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006161 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006162 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006163
6164 rdev_for_each(rdev, mddev) {
6165 if (mddev->raid_disks < raid_disks &&
6166 rdev->data_offset < rdev->new_data_offset)
6167 return -EINVAL;
6168 if (mddev->raid_disks > raid_disks &&
6169 rdev->data_offset > rdev->new_data_offset)
6170 return -EINVAL;
6171 }
6172
NeilBrown63c70c42006-03-27 01:18:13 -08006173 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006174 if (mddev->delta_disks < 0)
6175 mddev->reshape_backwards = 1;
6176 else if (mddev->delta_disks > 0)
6177 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006178
6179 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006180 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006181 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006182 mddev->reshape_backwards = 0;
6183 }
NeilBrownda943b992006-01-06 00:20:54 -08006184 return rv;
6185}
6186
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188/*
6189 * update_array_info is used to change the configuration of an
6190 * on-line array.
6191 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6192 * fields in the info are checked against the array.
6193 * Any differences that cannot be handled will cause an error.
6194 * Normally, only one change can be managed at a time.
6195 */
NeilBrownfd01b882011-10-11 16:47:53 +11006196static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197{
6198 int rv = 0;
6199 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006200 int state = 0;
6201
6202 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006203 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006204 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 if (mddev->major_version != info->major_version ||
6207 mddev->minor_version != info->minor_version ||
6208/* mddev->patch_version != info->patch_version || */
6209 mddev->ctime != info->ctime ||
6210 mddev->level != info->level ||
6211/* mddev->layout != info->layout || */
6212 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006213 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006214 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6215 ((state^info->state) & 0xfffffe00)
6216 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 return -EINVAL;
6218 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006219 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6220 cnt++;
6221 if (mddev->raid_disks != info->raid_disks)
6222 cnt++;
6223 if (mddev->layout != info->layout)
6224 cnt++;
6225 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6226 cnt++;
6227 if (cnt == 0)
6228 return 0;
6229 if (cnt > 1)
6230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
6232 if (mddev->layout != info->layout) {
6233 /* Change layout
6234 * we don't need to do anything at the md level, the
6235 * personality will take care of it all.
6236 */
NeilBrown50ac1682009-06-18 08:47:55 +10006237 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006239 else {
6240 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006241 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006242 if (rv)
6243 mddev->new_layout = mddev->layout;
6244 return rv;
6245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006247 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006248 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
NeilBrownda943b992006-01-06 00:20:54 -08006250 if (mddev->raid_disks != info->raid_disks)
6251 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
NeilBrown36fa3062005-09-09 16:23:45 -07006253 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6254 if (mddev->pers->quiesce == NULL)
6255 return -EINVAL;
6256 if (mddev->recovery || mddev->sync_thread)
6257 return -EBUSY;
6258 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6259 /* add the bitmap */
6260 if (mddev->bitmap)
6261 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006262 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006263 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006264 mddev->bitmap_info.offset =
6265 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006266 mddev->bitmap_info.space =
6267 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006268 mddev->pers->quiesce(mddev, 1);
6269 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006270 if (!rv)
6271 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006272 if (rv)
6273 bitmap_destroy(mddev);
6274 mddev->pers->quiesce(mddev, 0);
6275 } else {
6276 /* remove the bitmap */
6277 if (!mddev->bitmap)
6278 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006279 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006280 return -EINVAL;
6281 mddev->pers->quiesce(mddev, 1);
6282 bitmap_destroy(mddev);
6283 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006284 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006285 }
6286 }
NeilBrown850b2b42006-10-03 01:15:46 -07006287 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 return rv;
6289}
6290
NeilBrownfd01b882011-10-11 16:47:53 +11006291static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
NeilBrown3cb03002011-10-11 16:45:26 +11006293 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006294 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
6296 if (mddev->pers == NULL)
6297 return -ENODEV;
6298
NeilBrown1ca69c42012-10-11 13:37:33 +11006299 rcu_read_lock();
6300 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006302 err = -ENODEV;
6303 else {
6304 md_error(mddev, rdev);
6305 if (!test_bit(Faulty, &rdev->flags))
6306 err = -EBUSY;
6307 }
6308 rcu_read_unlock();
6309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310}
6311
Andre Noll2f9618c2008-04-25 18:57:58 +02006312/*
6313 * We have a problem here : there is no easy way to give a CHS
6314 * virtual geometry. We currently pretend that we have a 2 heads
6315 * 4 sectors (with a BIG number of cylinders...). This drives
6316 * dosfs just mad... ;-)
6317 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006318static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6319{
NeilBrownfd01b882011-10-11 16:47:53 +11006320 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006321
6322 geo->heads = 2;
6323 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006324 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006325 return 0;
6326}
6327
Al Viroa39907f2008-03-02 10:31:15 -05006328static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 unsigned int cmd, unsigned long arg)
6330{
6331 int err = 0;
6332 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006333 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006334 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
NeilBrown506c9e42011-12-23 10:17:26 +11006336 switch (cmd) {
6337 case RAID_VERSION:
6338 case GET_ARRAY_INFO:
6339 case GET_DISK_INFO:
6340 break;
6341 default:
6342 if (!capable(CAP_SYS_ADMIN))
6343 return -EACCES;
6344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346 /*
6347 * Commands dealing with the RAID driver but not any
6348 * particular array:
6349 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006350 switch (cmd) {
6351 case RAID_VERSION:
6352 err = get_version(argp);
6353 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
NeilBrownc02c0ae2012-12-11 13:39:21 +11006355 case PRINT_RAID_DEBUG:
6356 err = 0;
6357 md_print_devices();
6358 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006361 case RAID_AUTORUN:
6362 err = 0;
6363 autostart_arrays(arg);
6364 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006366 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 }
6368
6369 /*
6370 * Commands creating/starting a new array:
6371 */
6372
Al Viroa39907f2008-03-02 10:31:15 -05006373 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 if (!mddev) {
6376 BUG();
6377 goto abort;
6378 }
6379
NeilBrown1ca69c42012-10-11 13:37:33 +11006380 /* Some actions do not requires the mutex */
6381 switch (cmd) {
6382 case GET_ARRAY_INFO:
6383 if (!mddev->raid_disks && !mddev->external)
6384 err = -ENODEV;
6385 else
6386 err = get_array_info(mddev, argp);
6387 goto abort;
6388
6389 case GET_DISK_INFO:
6390 if (!mddev->raid_disks && !mddev->external)
6391 err = -ENODEV;
6392 else
6393 err = get_disk_info(mddev, argp);
6394 goto abort;
6395
6396 case SET_DISK_FAULTY:
6397 err = set_disk_faulty(mddev, new_decode_dev(arg));
6398 goto abort;
6399 }
6400
NeilBrowna7a3f082012-12-11 13:35:54 +11006401 if (cmd == ADD_NEW_DISK)
6402 /* need to ensure md_delayed_delete() has completed */
6403 flush_workqueue(md_misc_wq);
6404
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 err = mddev_lock(mddev);
6406 if (err) {
6407 printk(KERN_INFO
6408 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6409 err, cmd);
6410 goto abort;
6411 }
6412
NeilBrownc02c0ae2012-12-11 13:39:21 +11006413 if (cmd == SET_ARRAY_INFO) {
6414 mdu_array_info_t info;
6415 if (!arg)
6416 memset(&info, 0, sizeof(info));
6417 else if (copy_from_user(&info, argp, sizeof(info))) {
6418 err = -EFAULT;
6419 goto abort_unlock;
6420 }
6421 if (mddev->pers) {
6422 err = update_array_info(mddev, &info);
6423 if (err) {
6424 printk(KERN_WARNING "md: couldn't update"
6425 " array info. %d\n", err);
6426 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 }
6428 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006429 }
6430 if (!list_empty(&mddev->disks)) {
6431 printk(KERN_WARNING
6432 "md: array %s already has disks!\n",
6433 mdname(mddev));
6434 err = -EBUSY;
6435 goto abort_unlock;
6436 }
6437 if (mddev->raid_disks) {
6438 printk(KERN_WARNING
6439 "md: array %s already initialised!\n",
6440 mdname(mddev));
6441 err = -EBUSY;
6442 goto abort_unlock;
6443 }
6444 err = set_array_info(mddev, &info);
6445 if (err) {
6446 printk(KERN_WARNING "md: couldn't set"
6447 " array info. %d\n", err);
6448 goto abort_unlock;
6449 }
6450 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 }
6452
6453 /*
6454 * Commands querying/configuring an existing array:
6455 */
NeilBrown32a76272005-06-21 17:17:14 -07006456 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006457 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006458 if ((!mddev->raid_disks && !mddev->external)
6459 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6460 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6461 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 err = -ENODEV;
6463 goto abort_unlock;
6464 }
6465
6466 /*
6467 * Commands even a read-only array can execute:
6468 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006469 switch (cmd) {
6470 case GET_BITMAP_FILE:
6471 err = get_bitmap_file(mddev, argp);
6472 goto done_unlock;
6473
6474 case RESTART_ARRAY_RW:
6475 err = restart_array(mddev);
6476 goto done_unlock;
6477
6478 case STOP_ARRAY:
6479 err = do_md_stop(mddev, 0, bdev);
6480 goto done_unlock;
6481
6482 case STOP_ARRAY_RO:
6483 err = md_set_readonly(mddev, bdev);
6484 goto done_unlock;
6485
6486 case BLKROSET:
6487 if (get_user(ro, (int __user *)(arg))) {
6488 err = -EFAULT;
6489 goto done_unlock;
6490 }
6491 err = -EINVAL;
6492
6493 /* if the bdev is going readonly the value of mddev->ro
6494 * does not matter, no writes are coming
6495 */
6496 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006497 goto done_unlock;
6498
NeilBrownc02c0ae2012-12-11 13:39:21 +11006499 /* are we are already prepared for writes? */
6500 if (mddev->ro != 1)
6501 goto done_unlock;
6502
6503 /* transitioning to readauto need only happen for
6504 * arrays that call md_write_start
6505 */
6506 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006508 if (err == 0) {
6509 mddev->ro = 2;
6510 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006511 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006512 }
6513 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
6515
6516 /*
6517 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006518 * superblock, so we do not allow them on read-only arrays.
6519 * However non-MD ioctls (e.g. get-size) will still come through
6520 * here and hit the 'default' below, so only disallow
6521 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006523 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006524 if (mddev->ro == 2) {
6525 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006526 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006527 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6528 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006529 } else {
6530 err = -EROFS;
6531 goto abort_unlock;
6532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 }
6534
NeilBrownc02c0ae2012-12-11 13:39:21 +11006535 switch (cmd) {
6536 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006538 mdu_disk_info_t info;
6539 if (copy_from_user(&info, argp, sizeof(info)))
6540 err = -EFAULT;
6541 else
6542 err = add_new_disk(mddev, &info);
6543 goto done_unlock;
6544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
NeilBrownc02c0ae2012-12-11 13:39:21 +11006546 case HOT_REMOVE_DISK:
6547 err = hot_remove_disk(mddev, new_decode_dev(arg));
6548 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
NeilBrownc02c0ae2012-12-11 13:39:21 +11006550 case HOT_ADD_DISK:
6551 err = hot_add_disk(mddev, new_decode_dev(arg));
6552 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
NeilBrownc02c0ae2012-12-11 13:39:21 +11006554 case RUN_ARRAY:
6555 err = do_md_run(mddev);
6556 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
NeilBrownc02c0ae2012-12-11 13:39:21 +11006558 case SET_BITMAP_FILE:
6559 err = set_bitmap_file(mddev, (int)arg);
6560 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006561
NeilBrownc02c0ae2012-12-11 13:39:21 +11006562 default:
6563 err = -EINVAL;
6564 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 }
6566
6567done_unlock:
6568abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006569 if (mddev->hold_active == UNTIL_IOCTL &&
6570 err != -EINVAL)
6571 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 mddev_unlock(mddev);
6573
6574 return err;
6575done:
6576 if (err)
6577 MD_BUG();
6578abort:
6579 return err;
6580}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006581#ifdef CONFIG_COMPAT
6582static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6583 unsigned int cmd, unsigned long arg)
6584{
6585 switch (cmd) {
6586 case HOT_REMOVE_DISK:
6587 case HOT_ADD_DISK:
6588 case SET_DISK_FAULTY:
6589 case SET_BITMAP_FILE:
6590 /* These take in integer arg, do not convert */
6591 break;
6592 default:
6593 arg = (unsigned long)compat_ptr(arg);
6594 break;
6595 }
6596
6597 return md_ioctl(bdev, mode, cmd, arg);
6598}
6599#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
Al Viroa39907f2008-03-02 10:31:15 -05006601static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602{
6603 /*
6604 * Succeed if we can lock the mddev, which confirms that
6605 * it isn't being stopped right now.
6606 */
NeilBrownfd01b882011-10-11 16:47:53 +11006607 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 int err;
6609
Yuanhan Liu0c098222012-05-22 13:55:32 +10006610 if (!mddev)
6611 return -ENODEV;
6612
NeilBrownd3374822009-01-09 08:31:10 +11006613 if (mddev->gendisk != bdev->bd_disk) {
6614 /* we are racing with mddev_put which is discarding this
6615 * bd_disk.
6616 */
6617 mddev_put(mddev);
6618 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006619 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006620 /* Then retry the open from the top */
6621 return -ERESTARTSYS;
6622 }
6623 BUG_ON(mddev != bdev->bd_disk->private_data);
6624
NeilBrownc8c00a62009-08-10 12:50:52 +10006625 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 goto out;
6627
6628 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006629 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006630 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006632 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 out:
6634 return err;
6635}
6636
Al Viroa39907f2008-03-02 10:31:15 -05006637static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638{
NeilBrownfd01b882011-10-11 16:47:53 +11006639 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006641 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006642 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 mddev_put(mddev);
6644
6645 return 0;
6646}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006647
6648static int md_media_changed(struct gendisk *disk)
6649{
NeilBrownfd01b882011-10-11 16:47:53 +11006650 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006651
6652 return mddev->changed;
6653}
6654
6655static int md_revalidate(struct gendisk *disk)
6656{
NeilBrownfd01b882011-10-11 16:47:53 +11006657 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006658
6659 mddev->changed = 0;
6660 return 0;
6661}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006662static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663{
6664 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006665 .open = md_open,
6666 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006667 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006668#ifdef CONFIG_COMPAT
6669 .compat_ioctl = md_compat_ioctl,
6670#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006671 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006672 .media_changed = md_media_changed,
6673 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674};
6675
Adrian Bunk75c96f82005-05-05 16:16:09 -07006676static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677{
NeilBrown2b8bf342011-10-11 16:48:23 +11006678 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 /*
6681 * md_thread is a 'system-thread', it's priority should be very
6682 * high. We avoid resource deadlocks individually in each
6683 * raid personality. (RAID5 does preallocation) We also use RR and
6684 * the very same RT priority as kswapd, thus we will never get
6685 * into a priority inversion deadlock.
6686 *
6687 * we definitely have to have equal or higher priority than
6688 * bdflush, otherwise bdflush will deadlock if there are too
6689 * many dirty RAID5 blocks.
6690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
NeilBrown6985c432005-10-19 21:23:47 -07006692 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006693 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694
NeilBrown93588e22005-11-15 00:09:12 -08006695 /* We need to wait INTERRUPTIBLE so that
6696 * we don't add to the load-average.
6697 * That means we need to be sure no signals are
6698 * pending
6699 */
6700 if (signal_pending(current))
6701 flush_signals(current);
6702
6703 wait_event_interruptible_timeout
6704 (thread->wqueue,
6705 test_bit(THREAD_WAKEUP, &thread->flags)
6706 || kthread_should_stop(),
6707 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
NeilBrown6c987912011-01-14 09:13:53 +11006709 clear_bit(THREAD_WAKEUP, &thread->flags);
6710 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006711 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006713
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 return 0;
6715}
6716
NeilBrown2b8bf342011-10-11 16:48:23 +11006717void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718{
6719 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006720 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 set_bit(THREAD_WAKEUP, &thread->flags);
6722 wake_up(&thread->wqueue);
6723 }
6724}
6725
Shaohua Li4ed87312012-10-11 13:34:00 +11006726struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6727 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728{
NeilBrown2b8bf342011-10-11 16:48:23 +11006729 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
NeilBrown2b8bf342011-10-11 16:48:23 +11006731 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 if (!thread)
6733 return NULL;
6734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 init_waitqueue_head(&thread->wqueue);
6736
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 thread->run = run;
6738 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006739 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006740 thread->tsk = kthread_run(md_thread, thread,
6741 "%s_%s",
6742 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006743 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006744 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 kfree(thread);
6746 return NULL;
6747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 return thread;
6749}
6750
NeilBrown2b8bf342011-10-11 16:48:23 +11006751void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752{
NeilBrown2b8bf342011-10-11 16:48:23 +11006753 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006754 if (!thread)
6755 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006756 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006757 /* Locking ensures that mddev_unlock does not wake_up a
6758 * non-existent thread
6759 */
6760 spin_lock(&pers_lock);
6761 *threadp = NULL;
6762 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006763
6764 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 kfree(thread);
6766}
6767
NeilBrownfd01b882011-10-11 16:47:53 +11006768void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769{
6770 if (!mddev) {
6771 MD_BUG();
6772 return;
6773 }
6774
NeilBrownb2d444d2005-11-08 21:39:31 -08006775 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006777
NeilBrownde393cd2011-07-28 11:31:48 +10006778 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 return;
6780 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006781 if (mddev->degraded)
6782 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006783 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6785 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6786 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006787 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006788 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006789 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790}
6791
6792/* seq_file implementation /proc/mdstat */
6793
6794static void status_unused(struct seq_file *seq)
6795{
6796 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006797 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799 seq_printf(seq, "unused devices: ");
6800
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006801 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 char b[BDEVNAME_SIZE];
6803 i++;
6804 seq_printf(seq, "%s ",
6805 bdevname(rdev->bdev,b));
6806 }
6807 if (!i)
6808 seq_printf(seq, "<none>");
6809
6810 seq_printf(seq, "\n");
6811}
6812
6813
NeilBrownfd01b882011-10-11 16:47:53 +11006814static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815{
NeilBrowndd71cf62009-05-07 12:49:35 +10006816 sector_t max_sectors, resync, res;
6817 unsigned long dt, db;
6818 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006819 int scale;
6820 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821
NeilBrown72f36d52012-10-11 14:25:57 +11006822 if (mddev->curr_resync <= 3)
6823 resync = 0;
6824 else
6825 resync = mddev->curr_resync
6826 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
NeilBrownc804cde2012-05-21 09:28:33 +10006828 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6829 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006830 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006832 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 /*
6835 * Should not happen.
6836 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006837 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 MD_BUG();
6839 return;
6840 }
NeilBrown4588b422006-03-27 01:18:04 -08006841 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006842 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006843 * u32, as those are the requirements for sector_div.
6844 * Thus 'scale' must be at least 10
6845 */
6846 scale = 10;
6847 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006848 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006849 scale++;
6850 }
6851 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006852 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006853
6854 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 {
NeilBrown4588b422006-03-27 01:18:04 -08006856 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 seq_printf(seq, "[");
6858 for (i = 0; i < x; i++)
6859 seq_printf(seq, "=");
6860 seq_printf(seq, ">");
6861 for (i = 0; i < y; i++)
6862 seq_printf(seq, ".");
6863 seq_printf(seq, "] ");
6864 }
NeilBrown4588b422006-03-27 01:18:04 -08006865 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006866 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6867 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006868 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6869 "check" :
6870 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6871 "resync" : "recovery"))),
6872 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006873 (unsigned long long) resync/2,
6874 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
6876 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 * dt: time from mark until now
6878 * db: blocks written from mark until now
6879 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006880 *
6881 * rt is a sector_t, so could be 32bit or 64bit.
6882 * So we divide before multiply in case it is 32bit and close
6883 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006884 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006885 * near the end of resync when the number of remaining sectors
6886 * is close to 'db'.
6887 * We then divide rt by 32 after multiplying by db to compensate.
6888 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 */
6890 dt = ((jiffies - mddev->resync_mark) / HZ);
6891 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006892 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6893 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
NeilBrowndd71cf62009-05-07 12:49:35 +10006895 rt = max_sectors - resync; /* number of remaining sectors */
6896 sector_div(rt, db/32+1);
6897 rt *= dt;
6898 rt >>= 5;
6899
6900 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6901 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
NeilBrownff4e8d92006-07-10 04:44:16 -07006903 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
6906static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6907{
6908 struct list_head *tmp;
6909 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006910 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
6912 if (l >= 0x10000)
6913 return NULL;
6914 if (!l--)
6915 /* header */
6916 return (void*)1;
6917
6918 spin_lock(&all_mddevs_lock);
6919 list_for_each(tmp,&all_mddevs)
6920 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006921 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 mddev_get(mddev);
6923 spin_unlock(&all_mddevs_lock);
6924 return mddev;
6925 }
6926 spin_unlock(&all_mddevs_lock);
6927 if (!l--)
6928 return (void*)2;/* tail */
6929 return NULL;
6930}
6931
6932static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6933{
6934 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006935 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936
6937 ++*pos;
6938 if (v == (void*)2)
6939 return NULL;
6940
6941 spin_lock(&all_mddevs_lock);
6942 if (v == (void*)1)
6943 tmp = all_mddevs.next;
6944 else
6945 tmp = mddev->all_mddevs.next;
6946 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006947 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 else {
6949 next_mddev = (void*)2;
6950 *pos = 0x10000;
6951 }
6952 spin_unlock(&all_mddevs_lock);
6953
6954 if (v != (void*)1)
6955 mddev_put(mddev);
6956 return next_mddev;
6957
6958}
6959
6960static void md_seq_stop(struct seq_file *seq, void *v)
6961{
NeilBrownfd01b882011-10-11 16:47:53 +11006962 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
6964 if (mddev && v != (void*)1 && v != (void*)2)
6965 mddev_put(mddev);
6966}
6967
6968static int md_seq_show(struct seq_file *seq, void *v)
6969{
NeilBrownfd01b882011-10-11 16:47:53 +11006970 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006971 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006972 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
6974 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006975 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 seq_printf(seq, "Personalities : ");
6977 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006978 list_for_each_entry(pers, &pers_list, list)
6979 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
6981 spin_unlock(&pers_lock);
6982 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006983 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 return 0;
6985 }
6986 if (v == (void*)2) {
6987 status_unused(seq);
6988 return 0;
6989 }
6990
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006991 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006993
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6995 seq_printf(seq, "%s : %sactive", mdname(mddev),
6996 mddev->pers ? "" : "in");
6997 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006998 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007000 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007001 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 seq_printf(seq, " %s", mddev->pers->name);
7003 }
7004
Andre Nolldd8ac332009-03-31 14:33:13 +11007005 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007006 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 char b[BDEVNAME_SIZE];
7008 seq_printf(seq, " %s[%d]",
7009 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007010 if (test_bit(WriteMostly, &rdev->flags))
7011 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007012 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 seq_printf(seq, "(F)");
7014 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007015 }
7016 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007017 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007018 if (test_bit(Replacement, &rdev->flags))
7019 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007020 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 }
7022
7023 if (!list_empty(&mddev->disks)) {
7024 if (mddev->pers)
7025 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007026 (unsigned long long)
7027 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 else
7029 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007030 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007032 if (mddev->persistent) {
7033 if (mddev->major_version != 0 ||
7034 mddev->minor_version != 90) {
7035 seq_printf(seq," super %d.%d",
7036 mddev->major_version,
7037 mddev->minor_version);
7038 }
NeilBrowne6910632008-02-06 01:39:51 -08007039 } else if (mddev->external)
7040 seq_printf(seq, " super external:%s",
7041 mddev->metadata_type);
7042 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007043 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007046 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007048 if (mddev->pers->sync_request) {
7049 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007050 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007051 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007052 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007053 seq_printf(seq, "\tresync=DELAYED\n ");
7054 else if (mddev->recovery_cp < MaxSector)
7055 seq_printf(seq, "\tresync=PENDING\n ");
7056 }
NeilBrown32a76272005-06-21 17:17:14 -07007057 } else
7058 seq_printf(seq, "\n ");
7059
NeilBrown57148962012-03-19 12:46:40 +11007060 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 seq_printf(seq, "\n");
7063 }
7064 mddev_unlock(mddev);
7065
7066 return 0;
7067}
7068
Jan Engelhardt110518b2009-05-07 12:49:37 +10007069static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 .start = md_seq_start,
7071 .next = md_seq_next,
7072 .stop = md_seq_stop,
7073 .show = md_seq_show,
7074};
7075
7076static int md_seq_open(struct inode *inode, struct file *file)
7077{
Kay Sieversf1514632011-07-12 20:48:39 +02007078 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 int error;
7080
7081 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007082 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007083 return error;
7084
7085 seq = file->private_data;
7086 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 return error;
7088}
7089
NeilBrownd7603b72006-01-06 00:20:30 -08007090static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7091{
Kay Sieversf1514632011-07-12 20:48:39 +02007092 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007093 int mask;
7094
7095 poll_wait(filp, &md_event_waiters, wait);
7096
7097 /* always allow read */
7098 mask = POLLIN | POLLRDNORM;
7099
Kay Sieversf1514632011-07-12 20:48:39 +02007100 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007101 mask |= POLLERR | POLLPRI;
7102 return mask;
7103}
7104
Arjan van de Venfa027c22007-02-12 00:55:33 -08007105static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007106 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 .open = md_seq_open,
7108 .read = seq_read,
7109 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007110 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007111 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112};
7113
NeilBrown84fc4b52011-10-11 16:49:58 +11007114int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007117 list_add_tail(&p->list, &pers_list);
7118 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 spin_unlock(&pers_lock);
7120 return 0;
7121}
7122
NeilBrown84fc4b52011-10-11 16:49:58 +11007123int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124{
NeilBrown2604b702006-01-06 00:20:36 -08007125 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007127 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 spin_unlock(&pers_lock);
7129 return 0;
7130}
7131
NeilBrownfd01b882011-10-11 16:47:53 +11007132static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133{
NeilBrown3cb03002011-10-11 16:45:26 +11007134 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007136 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137
7138 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007139 rcu_read_lock();
7140 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007142 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7143 (int)part_stat_read(&disk->part0, sectors[1]) -
7144 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007145 /* sync IO will cause sync_io to increase before the disk_stats
7146 * as sync_io is counted when a request starts, and
7147 * disk_stats is counted when it completes.
7148 * So resync activity will cause curr_events to be smaller than
7149 * when there was no such activity.
7150 * non-sync IO will cause disk_stat to increase without
7151 * increasing sync_io so curr_events will (eventually)
7152 * be larger than it was before. Once it becomes
7153 * substantially larger, the test below will cause
7154 * the array to appear non-idle, and resync will slow
7155 * down.
7156 * If there is a lot of outstanding resync activity when
7157 * we set last_event to curr_events, then all that activity
7158 * completing might cause the array to appear non-idle
7159 * and resync will be slowed down even though there might
7160 * not have been non-resync activity. This will only
7161 * happen once though. 'last_events' will soon reflect
7162 * the state where there is little or no outstanding
7163 * resync requests, and further resync activity will
7164 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007165 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007167 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 rdev->last_events = curr_events;
7169 idle = 0;
7170 }
7171 }
NeilBrown4b809912008-07-21 17:05:25 +10007172 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 return idle;
7174}
7175
NeilBrownfd01b882011-10-11 16:47:53 +11007176void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
7178 /* another "blocks" (512byte) blocks have been synced */
7179 atomic_sub(blocks, &mddev->recovery_active);
7180 wake_up(&mddev->recovery_wait);
7181 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007182 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007183 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 md_wakeup_thread(mddev->thread);
7185 // stop recovery, signal do_sync ....
7186 }
7187}
7188
7189
NeilBrown06d91a52005-06-21 17:17:12 -07007190/* md_write_start(mddev, bi)
7191 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007192 * in superblock) before writing, schedule a superblock update
7193 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007194 */
NeilBrownfd01b882011-10-11 16:47:53 +11007195void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196{
Neil Brown0fd62b82008-06-28 08:31:36 +10007197 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007198 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007199 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007200
NeilBrownf91de922005-11-08 21:39:36 -08007201 BUG_ON(mddev->ro == 1);
7202 if (mddev->ro == 2) {
7203 /* need to switch to read/write */
7204 mddev->ro = 0;
7205 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7206 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007207 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007208 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007209 }
NeilBrown06d91a52005-06-21 17:17:12 -07007210 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007211 if (mddev->safemode == 1)
7212 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007213 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007214 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007215 if (mddev->in_sync) {
7216 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007217 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007218 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007219 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007220 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007221 }
NeilBrowna9701a32005-11-08 21:39:34 -08007222 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007223 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007224 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007225 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007226 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007227 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228}
7229
NeilBrownfd01b882011-10-11 16:47:53 +11007230void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231{
7232 if (atomic_dec_and_test(&mddev->writes_pending)) {
7233 if (mddev->safemode == 2)
7234 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007235 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7237 }
7238}
7239
NeilBrown2a2275d2007-01-26 00:57:11 -08007240/* md_allow_write(mddev)
7241 * Calling this ensures that the array is marked 'active' so that writes
7242 * may proceed without blocking. It is important to call this before
7243 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7244 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007245 *
7246 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7247 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007248 */
NeilBrownfd01b882011-10-11 16:47:53 +11007249int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007250{
7251 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007252 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007253 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007254 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007255 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007256 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007257
7258 spin_lock_irq(&mddev->write_lock);
7259 if (mddev->in_sync) {
7260 mddev->in_sync = 0;
7261 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007262 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007263 if (mddev->safemode_delay &&
7264 mddev->safemode == 0)
7265 mddev->safemode = 1;
7266 spin_unlock_irq(&mddev->write_lock);
7267 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007268 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007269 } else
7270 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007271
NeilBrown070dc6d2010-08-30 17:33:34 +10007272 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007273 return -EAGAIN;
7274 else
7275 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007276}
7277EXPORT_SYMBOL_GPL(md_allow_write);
7278
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279#define SYNC_MARKS 10
7280#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007281#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007282void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283{
Shaohua Li4ed87312012-10-11 13:34:00 +11007284 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007285 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 unsigned int currspeed = 0,
7287 window;
NeilBrown57afd892005-06-21 17:17:13 -07007288 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007290 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 sector_t mark_cnt[SYNC_MARKS];
7292 int last_mark,m;
7293 struct list_head *tmp;
7294 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007295 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007296 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007297 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007298 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299
7300 /* just incase thread restarts... */
7301 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7302 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007303 if (mddev->ro) /* never try to sync a read-only array */
7304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305
NeilBrown61df9d92006-10-03 01:15:57 -07007306 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7307 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7308 desc = "data-check";
7309 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7310 desc = "requested-resync";
7311 else
7312 desc = "resync";
7313 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7314 desc = "reshape";
7315 else
7316 desc = "recovery";
7317
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 /* we overload curr_resync somewhat here.
7319 * 0 == not engaged in resync at all
7320 * 2 == checking that there is no conflict with another sync
7321 * 1 == like 2, but have yielded to allow conflicting resync to
7322 * commense
7323 * other == active in resync - this many blocks
7324 *
7325 * Before starting a resync we must have set curr_resync to
7326 * 2, and then checked that every "conflicting" array has curr_resync
7327 * less than ours. When we find one that is the same or higher
7328 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7329 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7330 * This will mean we have to start checking from the beginning again.
7331 *
7332 */
7333
7334 do {
7335 mddev->curr_resync = 2;
7336
7337 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007338 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007339 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007340
7341 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007343 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 if (mddev2 == mddev)
7345 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007346 if (!mddev->parallel_resync
7347 && mddev2->curr_resync
7348 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 DEFINE_WAIT(wq);
7350 if (mddev < mddev2 && mddev->curr_resync == 2) {
7351 /* arbitrarily yield */
7352 mddev->curr_resync = 1;
7353 wake_up(&resync_wait);
7354 }
7355 if (mddev > mddev2 && mddev->curr_resync == 1)
7356 /* no need to wait here, we can wait the next
7357 * time 'round when curr_resync == 2
7358 */
7359 continue;
NeilBrown97441972008-09-19 11:49:54 +10007360 /* We need to wait 'interruptible' so as not to
7361 * contribute to the load average, and not to
7362 * be caught by 'softlockup'
7363 */
7364 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007365 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007366 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007367 printk(KERN_INFO "md: delaying %s of %s"
7368 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007370 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007372 if (signal_pending(current))
7373 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 schedule();
7375 finish_wait(&resync_wait, &wq);
7376 goto try_again;
7377 }
7378 finish_wait(&resync_wait, &wq);
7379 }
7380 }
7381 } while (mddev->curr_resync < 2);
7382
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007383 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007384 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007386 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 */
7388 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007389 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007390 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007391 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7392 j = mddev->resync_min;
7393 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007394 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007395
NeilBrownccfcc3c2006-03-27 01:18:09 -08007396 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007397 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007398 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007400 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007401 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007402 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007403 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007404 if (rdev->raid_disk >= 0 &&
7405 !test_bit(Faulty, &rdev->flags) &&
7406 !test_bit(In_sync, &rdev->flags) &&
7407 rdev->recovery_offset < j)
7408 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007409 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411
NeilBrown61df9d92006-10-03 01:15:57 -07007412 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7413 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7414 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007415 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007416 "(but not more than %d KB/sec) for %s.\n",
7417 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418
NeilBrowneea1bf32009-03-31 14:27:02 +11007419 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007420
NeilBrown57afd892005-06-21 17:17:13 -07007421 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 for (m = 0; m < SYNC_MARKS; m++) {
7423 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007424 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 }
7426 last_mark = 0;
7427 mddev->resync_mark = mark[last_mark];
7428 mddev->resync_mark_cnt = mark_cnt[last_mark];
7429
7430 /*
7431 * Tune reconstruction:
7432 */
7433 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007434 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7435 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
7437 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 last_check = 0;
7439
7440 if (j>2) {
7441 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007442 "md: resuming %s of %s from checkpoint.\n",
7443 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007445 } else
7446 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007447 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007448 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7449 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007450 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451
majianpeng7c2c57c2012-07-03 12:12:26 +10007452 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007454 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
NeilBrown57afd892005-06-21 17:17:13 -07007456 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007457
NeilBrown7a91ee12009-05-26 12:57:21 +10007458 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7459 ((mddev->curr_resync > mddev->curr_resync_completed &&
7460 (mddev->curr_resync - mddev->curr_resync_completed)
7461 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007462 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007463 (j - mddev->curr_resync_completed)*2
7464 >= mddev->resync_max - mddev->curr_resync_completed
7465 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007466 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007467 wait_event(mddev->recovery_wait,
7468 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007469 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007470 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7471 j > mddev->recovery_cp)
7472 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007473 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007474 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007475 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007476 }
NeilBrownacb180b2009-04-14 16:28:34 +10007477
NeilBrowne62e58a2009-07-01 13:15:35 +10007478 while (j >= mddev->resync_max && !kthread_should_stop()) {
7479 /* As this condition is controlled by user-space,
7480 * we can block indefinitely, so use '_interruptible'
7481 * to avoid triggering warnings.
7482 */
7483 flush_signals(current); /* just in case */
7484 wait_event_interruptible(mddev->recovery_wait,
7485 mddev->resync_max > j
7486 || kthread_should_stop());
7487 }
NeilBrownacb180b2009-04-14 16:28:34 +10007488
7489 if (kthread_should_stop())
7490 goto interrupted;
7491
NeilBrown57afd892005-06-21 17:17:13 -07007492 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007493 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007494 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007495 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 goto out;
7497 }
NeilBrown57afd892005-06-21 17:17:13 -07007498
7499 if (!skipped) { /* actual IO requested */
7500 io_sectors += sectors;
7501 atomic_add(sectors, &mddev->recovery_active);
7502 }
7503
NeilBrowne875ece2011-07-28 11:39:24 +10007504 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7505 break;
7506
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007508 if (j > 2)
7509 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007510 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007511 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007512 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007513 * visible in /proc/mdstat
7514 */
7515 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007516
7517 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 continue;
7519
NeilBrown57afd892005-06-21 17:17:13 -07007520 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 repeat:
7522 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7523 /* step marks */
7524 int next = (last_mark+1) % SYNC_MARKS;
7525
7526 mddev->resync_mark = mark[next];
7527 mddev->resync_mark_cnt = mark_cnt[next];
7528 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007529 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 last_mark = next;
7531 }
7532
7533
NeilBrownc6207272008-02-06 01:39:52 -08007534 if (kthread_should_stop())
7535 goto interrupted;
7536
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
7538 /*
7539 * this loop exits only if either when we are slower than
7540 * the 'hard' speed limit, or the system was IO-idle for
7541 * a jiffy.
7542 * the system might be non-idle CPU-wise, but we only care
7543 * about not overloading the IO subsystem. (things like an
7544 * e2fsck being done on the RAID array should execute fast)
7545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 cond_resched();
7547
NeilBrown57afd892005-06-21 17:17:13 -07007548 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7549 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550
NeilBrown88202a02006-01-06 00:21:36 -08007551 if (currspeed > speed_min(mddev)) {
7552 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007553 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007554 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 goto repeat;
7556 }
7557 }
7558 }
NeilBrown61df9d92006-10-03 01:15:57 -07007559 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 /*
7561 * this also signals 'finished resyncing' to md_stop
7562 */
7563 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007564 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7566
7567 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007568 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
NeilBrowndfc70642008-05-23 13:04:39 -07007570 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007571 mddev->curr_resync > 2) {
7572 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7573 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7574 if (mddev->curr_resync >= mddev->recovery_cp) {
7575 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007576 "md: checkpointing %s of %s.\n",
7577 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007578 if (test_bit(MD_RECOVERY_ERROR,
7579 &mddev->recovery))
7580 mddev->recovery_cp =
7581 mddev->curr_resync_completed;
7582 else
7583 mddev->recovery_cp =
7584 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007585 }
7586 } else
7587 mddev->recovery_cp = MaxSector;
7588 } else {
7589 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7590 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007591 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007592 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007593 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007594 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007595 !test_bit(Faulty, &rdev->flags) &&
7596 !test_bit(In_sync, &rdev->flags) &&
7597 rdev->recovery_offset < mddev->curr_resync)
7598 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007599 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007602 skip:
NeilBrown17571282006-12-10 02:20:52 -08007603 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
NeilBrownc07b70a2009-12-14 12:49:48 +11007605 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7606 /* We completed so min/max setting can be forgotten if used. */
7607 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7608 mddev->resync_min = 0;
7609 mddev->resync_max = MaxSector;
7610 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7611 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 mddev->curr_resync = 0;
7613 wake_up(&resync_wait);
7614 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7615 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007616 return;
7617
7618 interrupted:
7619 /*
7620 * got a signal, exit.
7621 */
7622 printk(KERN_INFO
7623 "md: md_do_sync() got signal ... exiting\n");
7624 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7625 goto out;
7626
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627}
NeilBrown29269552006-03-27 01:18:10 -08007628EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
NeilBrownfd01b882011-10-11 16:47:53 +11007630static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007631{
NeilBrown3cb03002011-10-11 16:45:26 +11007632 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007633 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007634 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007635
NeilBrowndafb20f2012-03-19 12:46:39 +11007636 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007637 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007638 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007639 (test_bit(Faulty, &rdev->flags) ||
7640 ! test_bit(In_sync, &rdev->flags)) &&
7641 atomic_read(&rdev->nr_pending)==0) {
7642 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007643 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007644 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007645 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007646 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007647 }
7648 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007649 if (removed)
7650 sysfs_notify(&mddev->kobj, NULL,
7651 "degraded");
7652
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007653
NeilBrowndafb20f2012-03-19 12:46:39 +11007654 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007655 if (rdev->raid_disk >= 0 &&
7656 !test_bit(In_sync, &rdev->flags) &&
7657 !test_bit(Faulty, &rdev->flags))
7658 spares++;
7659 if (rdev->raid_disk < 0
7660 && !test_bit(Faulty, &rdev->flags)) {
7661 rdev->recovery_offset = 0;
7662 if (mddev->pers->
7663 hot_add_disk(mddev, rdev) == 0) {
7664 if (sysfs_link_rdev(mddev, rdev))
7665 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007666 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007667 md_new_event(mddev);
7668 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007669 }
NeilBrowndfc70642008-05-23 13:04:39 -07007670 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007671 }
NeilBrown6dafab62012-09-19 12:54:22 +10007672 if (removed)
7673 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007674 return spares;
7675}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007676
NeilBrownfd01b882011-10-11 16:47:53 +11007677static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007678{
NeilBrown3cb03002011-10-11 16:45:26 +11007679 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007680
7681 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007682 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007683 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7684 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7685 /* success...*/
7686 /* activate any spares */
NeilBrown6dafab62012-09-19 12:54:22 +10007687 if (mddev->pers->spare_active(mddev)) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007688 sysfs_notify(&mddev->kobj, NULL,
7689 "degraded");
NeilBrown6dafab62012-09-19 12:54:22 +10007690 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7691 }
NeilBrown7ebc0be2011-01-14 09:14:33 +11007692 }
7693 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7694 mddev->pers->finish_reshape)
7695 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007696
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007697 /* If array is no-longer degraded, then any saved_raid_disk
7698 * information must be scrapped. Also if any device is now
7699 * In_sync we must scrape the saved_raid_disk for that device
7700 * do the superblock for an incrementally recovered device
7701 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007702 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007703 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007704 if (!mddev->degraded ||
7705 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007706 rdev->saved_raid_disk = -1;
7707
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007708 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007709 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7710 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7711 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7712 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7713 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7714 /* flag recovery needed just to double check */
7715 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7716 sysfs_notify_dirent_safe(mddev->sysfs_action);
7717 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007718 if (mddev->event_work.func)
7719 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007720}
7721
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722/*
7723 * This routine is regularly called by all per-raid-array threads to
7724 * deal with generic issues like resync and super-block update.
7725 * Raid personalities that don't have a thread (linear/raid0) do not
7726 * need this as they never do any recovery or update the superblock.
7727 *
7728 * It does not do any resync itself, but rather "forks" off other threads
7729 * to do that as needed.
7730 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7731 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007732 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 * and wakeups up this thread which will reap the thread and finish up.
7734 * This thread also removes any faulty devices (with nr_pending == 0).
7735 *
7736 * The overall approach is:
7737 * 1/ if the superblock needs updating, update it.
7738 * 2/ If a recovery thread is running, don't do anything else.
7739 * 3/ If recovery has finished, clean up, possibly marking spares active.
7740 * 4/ If there are any faulty devices, remove them.
7741 * 5/ If array is degraded, try to add spares devices
7742 * 6/ If array has spares or is not in-sync, start a resync thread.
7743 */
NeilBrownfd01b882011-10-11 16:47:53 +11007744void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007746 if (mddev->suspended)
7747 return;
7748
NeilBrown5f404022005-06-21 17:17:16 -07007749 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007750 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751
NeilBrownfca4d842005-06-21 17:17:11 -07007752 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007753 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007754 printk(KERN_INFO "md: %s in immediate safe mode\n",
7755 mdname(mddev));
7756 mddev->safemode = 2;
7757 }
7758 flush_signals(current);
7759 }
7760
NeilBrownc89a8ee2008-08-05 15:54:13 +10007761 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7762 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007764 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007766 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007767 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007768 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7769 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 ))
7771 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007772
NeilBrowndf5b89b2006-03-27 01:18:20 -08007773 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007774 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007775
NeilBrownc89a8ee2008-08-05 15:54:13 +10007776 if (mddev->ro) {
7777 /* Only thing we do on a ro array is remove
7778 * failed devices.
7779 */
NeilBrown3cb03002011-10-11 16:45:26 +11007780 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007781 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007782 if (rdev->raid_disk >= 0 &&
7783 !test_bit(Blocked, &rdev->flags) &&
7784 test_bit(Faulty, &rdev->flags) &&
7785 atomic_read(&rdev->nr_pending)==0) {
7786 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007787 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007788 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007789 rdev->raid_disk = -1;
7790 }
7791 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007792 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7793 goto unlock;
7794 }
7795
NeilBrown31a59e32008-04-30 00:52:30 -07007796 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007797 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007798 spin_lock_irq(&mddev->write_lock);
7799 if (mddev->safemode &&
7800 !atomic_read(&mddev->writes_pending) &&
7801 !mddev->in_sync &&
7802 mddev->recovery_cp == MaxSector) {
7803 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007804 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007805 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007806 }
7807 if (mddev->safemode == 1)
7808 mddev->safemode = 0;
7809 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007810 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007811 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007812 }
NeilBrownfca4d842005-06-21 17:17:11 -07007813
NeilBrown850b2b42006-10-03 01:15:46 -07007814 if (mddev->flags)
7815 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7818 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7819 /* resync/recovery still happening */
7820 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7821 goto unlock;
7822 }
7823 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007824 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 goto unlock;
7826 }
Neil Brown72a23c22008-06-28 08:31:41 +10007827 /* Set RUNNING before clearing NEEDED to avoid
7828 * any transients in the value of "sync_action".
7829 */
NeilBrown72f36d52012-10-11 14:25:57 +11007830 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007831 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007832 /* Clear some bits that don't mean anything, but
7833 * might be left set
7834 */
NeilBrown24dd4692005-11-08 21:39:26 -08007835 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7836 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837
NeilBrowned209582012-04-24 10:23:14 +10007838 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7839 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007840 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 /* no recovery is running.
7842 * remove any failed drives, then
7843 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007844 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 * the personality to fail the re-add.
7846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007848 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007849 if (mddev->pers->check_reshape == NULL ||
7850 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007851 /* Cannot proceed */
7852 goto unlock;
7853 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007854 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007855 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007856 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7857 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007858 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007859 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007860 } else if (mddev->recovery_cp < MaxSector) {
7861 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007862 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007863 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7864 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007866
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007868 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007869 /* We are adding a device or devices to an array
7870 * which has the bitmap stored on all devices.
7871 * So make sure all bitmap pages get written
7872 */
7873 bitmap_write_all(mddev->bitmap);
7874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 mddev->sync_thread = md_register_thread(md_do_sync,
7876 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007877 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 if (!mddev->sync_thread) {
7879 printk(KERN_ERR "%s: could not start resync"
7880 " thread...\n",
7881 mdname(mddev));
7882 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007883 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7884 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7886 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7887 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007890 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007891 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 }
7893 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007894 if (!mddev->sync_thread) {
7895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7897 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007898 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007899 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 mddev_unlock(mddev);
7902 }
7903}
7904
NeilBrownfd01b882011-10-11 16:47:53 +11007905void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007906{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007907 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007908 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007909 !test_bit(Blocked, &rdev->flags) &&
7910 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007911 msecs_to_jiffies(5000));
7912 rdev_dec_pending(rdev, mddev);
7913}
7914EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7915
NeilBrownc6563a82012-05-21 09:27:00 +10007916void md_finish_reshape(struct mddev *mddev)
7917{
7918 /* called be personality module when reshape completes. */
7919 struct md_rdev *rdev;
7920
7921 rdev_for_each(rdev, mddev) {
7922 if (rdev->data_offset > rdev->new_data_offset)
7923 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7924 else
7925 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7926 rdev->data_offset = rdev->new_data_offset;
7927 }
7928}
7929EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007930
7931/* Bad block management.
7932 * We can record which blocks on each device are 'bad' and so just
7933 * fail those blocks, or that stripe, rather than the whole device.
7934 * Entries in the bad-block table are 64bits wide. This comprises:
7935 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7936 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7937 * A 'shift' can be set so that larger blocks are tracked and
7938 * consequently larger devices can be covered.
7939 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7940 *
7941 * Locking of the bad-block table uses a seqlock so md_is_badblock
7942 * might need to retry if it is very unlucky.
7943 * We will sometimes want to check for bad blocks in a bi_end_io function,
7944 * so we use the write_seqlock_irq variant.
7945 *
7946 * When looking for a bad block we specify a range and want to
7947 * know if any block in the range is bad. So we binary-search
7948 * to the last range that starts at-or-before the given endpoint,
7949 * (or "before the sector after the target range")
7950 * then see if it ends after the given start.
7951 * We return
7952 * 0 if there are no known bad blocks in the range
7953 * 1 if there are known bad block which are all acknowledged
7954 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7955 * plus the start/length of the first bad section we overlap.
7956 */
7957int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7958 sector_t *first_bad, int *bad_sectors)
7959{
7960 int hi;
majianpengab056132012-11-06 17:13:44 +08007961 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007962 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08007963 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10007964 sector_t target = s + sectors;
7965 unsigned seq;
7966
7967 if (bb->shift > 0) {
7968 /* round the start down, and the end up */
7969 s >>= bb->shift;
7970 target += (1<<bb->shift) - 1;
7971 target >>= bb->shift;
7972 sectors = target - s;
7973 }
7974 /* 'target' is now the first block after the bad range */
7975
7976retry:
7977 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08007978 lo = 0;
7979 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007980 hi = bb->count;
7981
7982 /* Binary search between lo and hi for 'target'
7983 * i.e. for the last range that starts before 'target'
7984 */
7985 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7986 * are known not to be the last range before target.
7987 * VARIANT: hi-lo is the number of possible
7988 * ranges, and decreases until it reaches 1
7989 */
7990 while (hi - lo > 1) {
7991 int mid = (lo + hi) / 2;
7992 sector_t a = BB_OFFSET(p[mid]);
7993 if (a < target)
7994 /* This could still be the one, earlier ranges
7995 * could not. */
7996 lo = mid;
7997 else
7998 /* This and later ranges are definitely out. */
7999 hi = mid;
8000 }
8001 /* 'lo' might be the last that started before target, but 'hi' isn't */
8002 if (hi > lo) {
8003 /* need to check all range that end after 's' to see if
8004 * any are unacknowledged.
8005 */
8006 while (lo >= 0 &&
8007 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8008 if (BB_OFFSET(p[lo]) < target) {
8009 /* starts before the end, and finishes after
8010 * the start, so they must overlap
8011 */
8012 if (rv != -1 && BB_ACK(p[lo]))
8013 rv = 1;
8014 else
8015 rv = -1;
8016 *first_bad = BB_OFFSET(p[lo]);
8017 *bad_sectors = BB_LEN(p[lo]);
8018 }
8019 lo--;
8020 }
8021 }
8022
8023 if (read_seqretry(&bb->lock, seq))
8024 goto retry;
8025
8026 return rv;
8027}
8028EXPORT_SYMBOL_GPL(md_is_badblock);
8029
8030/*
8031 * Add a range of bad blocks to the table.
8032 * This might extend the table, or might contract it
8033 * if two adjacent ranges can be merged.
8034 * We binary-search to find the 'insertion' point, then
8035 * decide how best to handle it.
8036 */
8037static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8038 int acknowledged)
8039{
8040 u64 *p;
8041 int lo, hi;
8042 int rv = 1;
8043
8044 if (bb->shift < 0)
8045 /* badblocks are disabled */
8046 return 0;
8047
8048 if (bb->shift) {
8049 /* round the start down, and the end up */
8050 sector_t next = s + sectors;
8051 s >>= bb->shift;
8052 next += (1<<bb->shift) - 1;
8053 next >>= bb->shift;
8054 sectors = next - s;
8055 }
8056
8057 write_seqlock_irq(&bb->lock);
8058
8059 p = bb->page;
8060 lo = 0;
8061 hi = bb->count;
8062 /* Find the last range that starts at-or-before 's' */
8063 while (hi - lo > 1) {
8064 int mid = (lo + hi) / 2;
8065 sector_t a = BB_OFFSET(p[mid]);
8066 if (a <= s)
8067 lo = mid;
8068 else
8069 hi = mid;
8070 }
8071 if (hi > lo && BB_OFFSET(p[lo]) > s)
8072 hi = lo;
8073
8074 if (hi > lo) {
8075 /* we found a range that might merge with the start
8076 * of our new range
8077 */
8078 sector_t a = BB_OFFSET(p[lo]);
8079 sector_t e = a + BB_LEN(p[lo]);
8080 int ack = BB_ACK(p[lo]);
8081 if (e >= s) {
8082 /* Yes, we can merge with a previous range */
8083 if (s == a && s + sectors >= e)
8084 /* new range covers old */
8085 ack = acknowledged;
8086 else
8087 ack = ack && acknowledged;
8088
8089 if (e < s + sectors)
8090 e = s + sectors;
8091 if (e - a <= BB_MAX_LEN) {
8092 p[lo] = BB_MAKE(a, e-a, ack);
8093 s = e;
8094 } else {
8095 /* does not all fit in one range,
8096 * make p[lo] maximal
8097 */
8098 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8099 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8100 s = a + BB_MAX_LEN;
8101 }
8102 sectors = e - s;
8103 }
8104 }
8105 if (sectors && hi < bb->count) {
8106 /* 'hi' points to the first range that starts after 's'.
8107 * Maybe we can merge with the start of that range */
8108 sector_t a = BB_OFFSET(p[hi]);
8109 sector_t e = a + BB_LEN(p[hi]);
8110 int ack = BB_ACK(p[hi]);
8111 if (a <= s + sectors) {
8112 /* merging is possible */
8113 if (e <= s + sectors) {
8114 /* full overlap */
8115 e = s + sectors;
8116 ack = acknowledged;
8117 } else
8118 ack = ack && acknowledged;
8119
8120 a = s;
8121 if (e - a <= BB_MAX_LEN) {
8122 p[hi] = BB_MAKE(a, e-a, ack);
8123 s = e;
8124 } else {
8125 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8126 s = a + BB_MAX_LEN;
8127 }
8128 sectors = e - s;
8129 lo = hi;
8130 hi++;
8131 }
8132 }
8133 if (sectors == 0 && hi < bb->count) {
8134 /* we might be able to combine lo and hi */
8135 /* Note: 's' is at the end of 'lo' */
8136 sector_t a = BB_OFFSET(p[hi]);
8137 int lolen = BB_LEN(p[lo]);
8138 int hilen = BB_LEN(p[hi]);
8139 int newlen = lolen + hilen - (s - a);
8140 if (s >= a && newlen < BB_MAX_LEN) {
8141 /* yes, we can combine them */
8142 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8143 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8144 memmove(p + hi, p + hi + 1,
8145 (bb->count - hi - 1) * 8);
8146 bb->count--;
8147 }
8148 }
8149 while (sectors) {
8150 /* didn't merge (it all).
8151 * Need to add a range just before 'hi' */
8152 if (bb->count >= MD_MAX_BADBLOCKS) {
8153 /* No room for more */
8154 rv = 0;
8155 break;
8156 } else {
8157 int this_sectors = sectors;
8158 memmove(p + hi + 1, p + hi,
8159 (bb->count - hi) * 8);
8160 bb->count++;
8161
8162 if (this_sectors > BB_MAX_LEN)
8163 this_sectors = BB_MAX_LEN;
8164 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8165 sectors -= this_sectors;
8166 s += this_sectors;
8167 }
8168 }
8169
8170 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008171 if (!acknowledged)
8172 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008173 write_sequnlock_irq(&bb->lock);
8174
8175 return rv;
8176}
8177
NeilBrown3cb03002011-10-11 16:45:26 +11008178int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008179 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008180{
NeilBrownc6563a82012-05-21 09:27:00 +10008181 int rv;
8182 if (is_new)
8183 s += rdev->new_data_offset;
8184 else
8185 s += rdev->data_offset;
8186 rv = md_set_badblocks(&rdev->badblocks,
8187 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008188 if (rv) {
8189 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008190 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008191 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8192 md_wakeup_thread(rdev->mddev->thread);
8193 }
8194 return rv;
8195}
8196EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8197
8198/*
8199 * Remove a range of bad blocks from the table.
8200 * This may involve extending the table if we spilt a region,
8201 * but it must not fail. So if the table becomes full, we just
8202 * drop the remove request.
8203 */
8204static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8205{
8206 u64 *p;
8207 int lo, hi;
8208 sector_t target = s + sectors;
8209 int rv = 0;
8210
8211 if (bb->shift > 0) {
8212 /* When clearing we round the start up and the end down.
8213 * This should not matter as the shift should align with
8214 * the block size and no rounding should ever be needed.
8215 * However it is better the think a block is bad when it
8216 * isn't than to think a block is not bad when it is.
8217 */
8218 s += (1<<bb->shift) - 1;
8219 s >>= bb->shift;
8220 target >>= bb->shift;
8221 sectors = target - s;
8222 }
8223
8224 write_seqlock_irq(&bb->lock);
8225
8226 p = bb->page;
8227 lo = 0;
8228 hi = bb->count;
8229 /* Find the last range that starts before 'target' */
8230 while (hi - lo > 1) {
8231 int mid = (lo + hi) / 2;
8232 sector_t a = BB_OFFSET(p[mid]);
8233 if (a < target)
8234 lo = mid;
8235 else
8236 hi = mid;
8237 }
8238 if (hi > lo) {
8239 /* p[lo] is the last range that could overlap the
8240 * current range. Earlier ranges could also overlap,
8241 * but only this one can overlap the end of the range.
8242 */
8243 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8244 /* Partial overlap, leave the tail of this range */
8245 int ack = BB_ACK(p[lo]);
8246 sector_t a = BB_OFFSET(p[lo]);
8247 sector_t end = a + BB_LEN(p[lo]);
8248
8249 if (a < s) {
8250 /* we need to split this range */
8251 if (bb->count >= MD_MAX_BADBLOCKS) {
8252 rv = 0;
8253 goto out;
8254 }
8255 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8256 bb->count++;
8257 p[lo] = BB_MAKE(a, s-a, ack);
8258 lo++;
8259 }
8260 p[lo] = BB_MAKE(target, end - target, ack);
8261 /* there is no longer an overlap */
8262 hi = lo;
8263 lo--;
8264 }
8265 while (lo >= 0 &&
8266 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8267 /* This range does overlap */
8268 if (BB_OFFSET(p[lo]) < s) {
8269 /* Keep the early parts of this range. */
8270 int ack = BB_ACK(p[lo]);
8271 sector_t start = BB_OFFSET(p[lo]);
8272 p[lo] = BB_MAKE(start, s - start, ack);
8273 /* now low doesn't overlap, so.. */
8274 break;
8275 }
8276 lo--;
8277 }
8278 /* 'lo' is strictly before, 'hi' is strictly after,
8279 * anything between needs to be discarded
8280 */
8281 if (hi - lo > 1) {
8282 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8283 bb->count -= (hi - lo - 1);
8284 }
8285 }
8286
8287 bb->changed = 1;
8288out:
8289 write_sequnlock_irq(&bb->lock);
8290 return rv;
8291}
8292
NeilBrownc6563a82012-05-21 09:27:00 +10008293int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8294 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008295{
NeilBrownc6563a82012-05-21 09:27:00 +10008296 if (is_new)
8297 s += rdev->new_data_offset;
8298 else
8299 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008300 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008301 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008302}
8303EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8304
8305/*
8306 * Acknowledge all bad blocks in a list.
8307 * This only succeeds if ->changed is clear. It is used by
8308 * in-kernel metadata updates
8309 */
8310void md_ack_all_badblocks(struct badblocks *bb)
8311{
8312 if (bb->page == NULL || bb->changed)
8313 /* no point even trying */
8314 return;
8315 write_seqlock_irq(&bb->lock);
8316
majianpengecb178b2012-03-19 12:46:42 +11008317 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008318 u64 *p = bb->page;
8319 int i;
8320 for (i = 0; i < bb->count ; i++) {
8321 if (!BB_ACK(p[i])) {
8322 sector_t start = BB_OFFSET(p[i]);
8323 int len = BB_LEN(p[i]);
8324 p[i] = BB_MAKE(start, len, 1);
8325 }
8326 }
NeilBrownde393cd2011-07-28 11:31:48 +10008327 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008328 }
8329 write_sequnlock_irq(&bb->lock);
8330}
8331EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8332
NeilBrown16c791a2011-07-28 11:31:47 +10008333/* sysfs access to bad-blocks list.
8334 * We present two files.
8335 * 'bad-blocks' lists sector numbers and lengths of ranges that
8336 * are recorded as bad. The list is truncated to fit within
8337 * the one-page limit of sysfs.
8338 * Writing "sector length" to this file adds an acknowledged
8339 * bad block list.
8340 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8341 * been acknowledged. Writing to this file adds bad blocks
8342 * without acknowledging them. This is largely for testing.
8343 */
8344
8345static ssize_t
8346badblocks_show(struct badblocks *bb, char *page, int unack)
8347{
8348 size_t len;
8349 int i;
8350 u64 *p = bb->page;
8351 unsigned seq;
8352
8353 if (bb->shift < 0)
8354 return 0;
8355
8356retry:
8357 seq = read_seqbegin(&bb->lock);
8358
8359 len = 0;
8360 i = 0;
8361
8362 while (len < PAGE_SIZE && i < bb->count) {
8363 sector_t s = BB_OFFSET(p[i]);
8364 unsigned int length = BB_LEN(p[i]);
8365 int ack = BB_ACK(p[i]);
8366 i++;
8367
8368 if (unack && ack)
8369 continue;
8370
8371 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8372 (unsigned long long)s << bb->shift,
8373 length << bb->shift);
8374 }
NeilBrownde393cd2011-07-28 11:31:48 +10008375 if (unack && len == 0)
8376 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008377
8378 if (read_seqretry(&bb->lock, seq))
8379 goto retry;
8380
8381 return len;
8382}
8383
8384#define DO_DEBUG 1
8385
8386static ssize_t
8387badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8388{
8389 unsigned long long sector;
8390 int length;
8391 char newline;
8392#ifdef DO_DEBUG
8393 /* Allow clearing via sysfs *only* for testing/debugging.
8394 * Normally only a successful write may clear a badblock
8395 */
8396 int clear = 0;
8397 if (page[0] == '-') {
8398 clear = 1;
8399 page++;
8400 }
8401#endif /* DO_DEBUG */
8402
8403 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8404 case 3:
8405 if (newline != '\n')
8406 return -EINVAL;
8407 case 2:
8408 if (length <= 0)
8409 return -EINVAL;
8410 break;
8411 default:
8412 return -EINVAL;
8413 }
8414
8415#ifdef DO_DEBUG
8416 if (clear) {
8417 md_clear_badblocks(bb, sector, length);
8418 return len;
8419 }
8420#endif /* DO_DEBUG */
8421 if (md_set_badblocks(bb, sector, length, !unack))
8422 return len;
8423 else
8424 return -ENOSPC;
8425}
8426
Adrian Bunk75c96f82005-05-05 16:16:09 -07008427static int md_notify_reboot(struct notifier_block *this,
8428 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429{
8430 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008431 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008432 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
NeilBrownc744a652012-03-19 12:46:37 +11008434 for_each_mddev(mddev, tmp) {
8435 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008436 if (mddev->pers)
8437 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008438 mddev->safemode = 2;
8439 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008440 }
NeilBrownc744a652012-03-19 12:46:37 +11008441 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 }
NeilBrownc744a652012-03-19 12:46:37 +11008443 /*
8444 * certain more exotic SCSI devices are known to be
8445 * volatile wrt too early system reboots. While the
8446 * right place to handle this issue is the given
8447 * driver, we do want to have a safe RAID driver ...
8448 */
8449 if (need_delay)
8450 mdelay(1000*1);
8451
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 return NOTIFY_DONE;
8453}
8454
Adrian Bunk75c96f82005-05-05 16:16:09 -07008455static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 .notifier_call = md_notify_reboot,
8457 .next = NULL,
8458 .priority = INT_MAX, /* before any real devices */
8459};
8460
8461static void md_geninit(void)
8462{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008463 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008465 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466}
8467
Adrian Bunk75c96f82005-05-05 16:16:09 -07008468static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469{
Tejun Heoe804ac72010-10-15 15:36:08 +02008470 int ret = -ENOMEM;
8471
Tejun Heoada609e2011-01-25 14:35:54 +01008472 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008473 if (!md_wq)
8474 goto err_wq;
8475
8476 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8477 if (!md_misc_wq)
8478 goto err_misc_wq;
8479
8480 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8481 goto err_md;
8482
8483 if ((ret = register_blkdev(0, "mdp")) < 0)
8484 goto err_mdp;
8485 mdp_major = ret;
8486
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008487 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008488 md_probe, NULL, NULL);
8489 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 md_probe, NULL, NULL);
8491
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008493 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494
8495 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497
Tejun Heoe804ac72010-10-15 15:36:08 +02008498err_mdp:
8499 unregister_blkdev(MD_MAJOR, "md");
8500err_md:
8501 destroy_workqueue(md_misc_wq);
8502err_misc_wq:
8503 destroy_workqueue(md_wq);
8504err_wq:
8505 return ret;
8506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507
8508#ifndef MODULE
8509
8510/*
8511 * Searches all registered partitions for autorun RAID arrays
8512 * at boot time.
8513 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008514
8515static LIST_HEAD(all_detected_devices);
8516struct detected_devices_node {
8517 struct list_head list;
8518 dev_t dev;
8519};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520
8521void md_autodetect_dev(dev_t dev)
8522{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008523 struct detected_devices_node *node_detected_dev;
8524
8525 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8526 if (node_detected_dev) {
8527 node_detected_dev->dev = dev;
8528 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8529 } else {
8530 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8531 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533}
8534
8535
8536static void autostart_arrays(int part)
8537{
NeilBrown3cb03002011-10-11 16:45:26 +11008538 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008539 struct detected_devices_node *node_detected_dev;
8540 dev_t dev;
8541 int i_scanned, i_passed;
8542
8543 i_scanned = 0;
8544 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
8546 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8547
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008548 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8549 i_scanned++;
8550 node_detected_dev = list_entry(all_detected_devices.next,
8551 struct detected_devices_node, list);
8552 list_del(&node_detected_dev->list);
8553 dev = node_detected_dev->dev;
8554 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008555 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 if (IS_ERR(rdev))
8557 continue;
8558
NeilBrownb2d444d2005-11-08 21:39:31 -08008559 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 MD_BUG();
8561 continue;
8562 }
NeilBrownd0fae182008-03-04 14:29:31 -08008563 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008565 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008567
8568 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8569 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570
8571 autorun_devices(part);
8572}
8573
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008574#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575
8576static __exit void md_exit(void)
8577{
NeilBrownfd01b882011-10-11 16:47:53 +11008578 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008580
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008581 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008582 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008584 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585 unregister_blkdev(mdp_major, "mdp");
8586 unregister_reboot_notifier(&md_notifier);
8587 unregister_sysctl_table(raid_table_header);
8588 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008589 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008591 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008593 destroy_workqueue(md_misc_wq);
8594 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595}
8596
Dan Williams685784a2007-07-09 11:56:42 -07008597subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598module_exit(md_exit)
8599
NeilBrownf91de922005-11-08 21:39:36 -08008600static int get_ro(char *buffer, struct kernel_param *kp)
8601{
8602 return sprintf(buffer, "%d", start_readonly);
8603}
8604static int set_ro(const char *val, struct kernel_param *kp)
8605{
8606 char *e;
8607 int num = simple_strtoul(val, &e, 10);
8608 if (*val && (*e == '\0' || *e == '\n')) {
8609 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008610 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008611 }
8612 return -EINVAL;
8613}
8614
NeilBrown80ca3a42006-07-10 04:44:18 -07008615module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8616module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008617
NeilBrownefeb53c2009-01-09 08:31:10 +11008618module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008619
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620EXPORT_SYMBOL(register_md_personality);
8621EXPORT_SYMBOL(unregister_md_personality);
8622EXPORT_SYMBOL(md_error);
8623EXPORT_SYMBOL(md_done_sync);
8624EXPORT_SYMBOL(md_write_start);
8625EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626EXPORT_SYMBOL(md_register_thread);
8627EXPORT_SYMBOL(md_unregister_thread);
8628EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629EXPORT_SYMBOL(md_check_recovery);
8630MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008631MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008632MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008633MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);