blob: 9ab768acfb623f8bbb13870e5f65150a60ecad07 [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 Viroff01bb482011-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,
NeilBrowna2826aa2009-12-14 12:49:49 +1100455 mddev->write_lock, /*nothing*/);
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 int i;
1418
1419 disk_csum = sb->sb_csum;
1420 sb->sb_csum = 0;
1421 newcsum = 0;
1422 for (i=0; size>=4; size -= 4 )
1423 newcsum += le32_to_cpu(*isuper++);
1424
1425 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001426 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1429 sb->sb_csum = disk_csum;
1430 return cpu_to_le32(csum);
1431}
1432
NeilBrown2699b672011-07-28 11:31:47 +10001433static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1434 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001435static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct mdp_superblock_1 *sb;
1438 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001439 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001440 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001442 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /*
Andre Noll0f420352008-07-11 22:02:23 +10001445 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * It is always aligned to a 4K boundary and
1447 * depeding on minor_version, it can be:
1448 * 0: At least 8K, but less than 12K, from end of device
1449 * 1: At start of device
1450 * 2: 4K from start of device.
1451 */
1452 switch(minor_version) {
1453 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001454 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001455 sb_start -= 8*2;
1456 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
1458 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001459 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
1461 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001462 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 default:
1465 return -EINVAL;
1466 }
Andre Noll0f420352008-07-11 22:02:23 +10001467 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
NeilBrown0002b272005-09-09 16:23:53 -07001469 /* superblock is rarely larger than 1K, but it can be larger,
1470 * and it is safe to read 4k, so we do that
1471 */
1472 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (ret) return ret;
1474
1475
Namhyung Kim65a06f062011-07-27 11:00:36 +10001476 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1479 sb->major_version != cpu_to_le32(1) ||
1480 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001481 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001482 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return -EINVAL;
1484
1485 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1486 printk("md: invalid superblock checksum on %s\n",
1487 bdevname(rdev->bdev,b));
1488 return -EINVAL;
1489 }
1490 if (le64_to_cpu(sb->data_size) < 10) {
1491 printk("md: data_size too small on %s\n",
1492 bdevname(rdev->bdev,b));
1493 return -EINVAL;
1494 }
NeilBrownc6563a82012-05-21 09:27:00 +10001495 if (sb->pad0 ||
1496 sb->pad3[0] ||
1497 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1498 /* Some padding is non-zero, might be a new feature */
1499 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 rdev->preferred_minor = 0xffff;
1502 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001503 rdev->new_data_offset = rdev->data_offset;
1504 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1505 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1506 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001507 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
NeilBrown0002b272005-09-09 16:23:53 -07001509 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001510 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001511 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001512 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1513
1514 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001515 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001516 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001517 if (minor_version
1518 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1519 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001520
NeilBrown31b65a02006-07-10 04:44:14 -07001521 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1522 rdev->desc_nr = -1;
1523 else
1524 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1525
NeilBrown2699b672011-07-28 11:31:47 +10001526 if (!rdev->bb_page) {
1527 rdev->bb_page = alloc_page(GFP_KERNEL);
1528 if (!rdev->bb_page)
1529 return -ENOMEM;
1530 }
1531 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1532 rdev->badblocks.count == 0) {
1533 /* need to load the bad block list.
1534 * Currently we limit it to one page.
1535 */
1536 s32 offset;
1537 sector_t bb_sector;
1538 u64 *bbp;
1539 int i;
1540 int sectors = le16_to_cpu(sb->bblog_size);
1541 if (sectors > (PAGE_SIZE / 512))
1542 return -EINVAL;
1543 offset = le32_to_cpu(sb->bblog_offset);
1544 if (offset == 0)
1545 return -EINVAL;
1546 bb_sector = (long long)offset;
1547 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1548 rdev->bb_page, READ, true))
1549 return -EIO;
1550 bbp = (u64 *)page_address(rdev->bb_page);
1551 rdev->badblocks.shift = sb->bblog_shift;
1552 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1553 u64 bb = le64_to_cpu(*bbp);
1554 int count = bb & (0x3ff);
1555 u64 sector = bb >> 10;
1556 sector <<= sb->bblog_shift;
1557 count <<= sb->bblog_shift;
1558 if (bb + 1 == 0)
1559 break;
1560 if (md_set_badblocks(&rdev->badblocks,
1561 sector, count, 1) == 0)
1562 return -EINVAL;
1563 }
1564 } else if (sb->bblog_offset == 0)
1565 rdev->badblocks.shift = -1;
1566
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001567 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001568 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001569 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001571 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1574 sb->level != refsb->level ||
1575 sb->layout != refsb->layout ||
1576 sb->chunksize != refsb->chunksize) {
1577 printk(KERN_WARNING "md: %s has strangely different"
1578 " superblock to %s\n",
1579 bdevname(rdev->bdev,b),
1580 bdevname(refdev->bdev,b2));
1581 return -EINVAL;
1582 }
1583 ev1 = le64_to_cpu(sb->events);
1584 ev2 = le64_to_cpu(refsb->events);
1585
1586 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001587 ret = 1;
1588 else
1589 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
NeilBrownc6563a82012-05-21 09:27:00 +10001591 if (minor_version) {
1592 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1593 sectors -= rdev->data_offset;
1594 } else
1595 sectors = rdev->sb_start;
1596 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001598 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
NeilBrownfd01b882011-10-11 16:47:53 +11001602static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001604 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001605 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
NeilBrown41158c72005-06-21 17:17:25 -07001607 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001608 clear_bit(Faulty, &rdev->flags);
1609 clear_bit(In_sync, &rdev->flags);
1610 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (mddev->raid_disks == 0) {
1613 mddev->major_version = 1;
1614 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001615 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001616 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1618 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1619 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001620 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 mddev->layout = le32_to_cpu(sb->layout);
1622 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001623 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001624 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001625 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001626 mddev->bitmap_info.space = 0;
1627 /* Default location for bitmap is 1K after superblock
1628 * using 3K - total of 4K
1629 */
NeilBrownc3d97142009-12-14 12:49:52 +11001630 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001631 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001632 mddev->reshape_backwards = 0;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1635 memcpy(mddev->uuid, sb->set_uuid, 16);
1636
1637 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001638
NeilBrown71c08052005-09-09 16:23:51 -07001639 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001640 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001641 mddev->bitmap_info.offset =
1642 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001643 /* Metadata doesn't record how much space is available.
1644 * For 1.0, we assume we can use up to the superblock
1645 * if before, else to 4K beyond superblock.
1646 * For others, assume no change is possible.
1647 */
1648 if (mddev->minor_version > 0)
1649 mddev->bitmap_info.space = 0;
1650 else if (mddev->bitmap_info.offset > 0)
1651 mddev->bitmap_info.space =
1652 8 - mddev->bitmap_info.offset;
1653 else
1654 mddev->bitmap_info.space =
1655 -mddev->bitmap_info.offset;
1656 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001657
NeilBrownf6705572006-03-27 01:18:11 -08001658 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1659 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1660 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1661 mddev->new_level = le32_to_cpu(sb->new_level);
1662 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001663 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001664 if (mddev->delta_disks < 0 ||
1665 (mddev->delta_disks == 0 &&
1666 (le32_to_cpu(sb->feature_map)
1667 & MD_FEATURE_RESHAPE_BACKWARDS)))
1668 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001669 } else {
1670 mddev->reshape_position = MaxSector;
1671 mddev->delta_disks = 0;
1672 mddev->new_level = mddev->level;
1673 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001674 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001675 }
1676
NeilBrown41158c72005-06-21 17:17:25 -07001677 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001678 /* Insist of good event counter while assembling, except for
1679 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001681 if (rdev->desc_nr >= 0 &&
1682 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1683 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1684 if (ev1 < mddev->events)
1685 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001686 } else if (mddev->bitmap) {
1687 /* If adding to array with a bitmap, then we can accept an
1688 * older device, but not too old.
1689 */
NeilBrown41158c72005-06-21 17:17:25 -07001690 if (ev1 < mddev->bitmap->events_cleared)
1691 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001692 } else {
1693 if (ev1 < mddev->events)
1694 /* just a hot-add of a new device, leave raid_disk at -1 */
1695 return 0;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (mddev->level != LEVEL_MULTIPATH) {
1698 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001699 if (rdev->desc_nr < 0 ||
1700 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1701 role = 0xffff;
1702 rdev->desc_nr = -1;
1703 } else
1704 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 switch(role) {
1706 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001709 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
1711 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001712 if ((le32_to_cpu(sb->feature_map) &
1713 MD_FEATURE_RECOVERY_OFFSET))
1714 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1715 else
1716 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 rdev->raid_disk = role;
1718 break;
1719 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001720 if (sb->devflags & WriteMostly1)
1721 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001722 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1723 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001724 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001725 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return 0;
1728}
1729
NeilBrownfd01b882011-10-11 16:47:53 +11001730static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001733 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int max_dev, i;
1735 /* make rdev->sb match mddev and rdev data. */
1736
Namhyung Kim65a06f062011-07-27 11:00:36 +10001737 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 sb->feature_map = 0;
1740 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001741 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 memset(sb->pad3, 0, sizeof(sb->pad3));
1743
1744 sb->utime = cpu_to_le64((__u64)mddev->utime);
1745 sb->events = cpu_to_le64(mddev->events);
1746 if (mddev->in_sync)
1747 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1748 else
1749 sb->resync_offset = cpu_to_le64(0);
1750
NeilBrown1c05b4b2006-10-21 10:24:08 -07001751 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001752
NeilBrownf0ca3402006-02-02 14:28:04 -08001753 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001754 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001755 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001756 sb->level = cpu_to_le32(mddev->level);
1757 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001758
NeilBrownaeb9b212011-08-25 14:43:08 +10001759 if (test_bit(WriteMostly, &rdev->flags))
1760 sb->devflags |= WriteMostly1;
1761 else
1762 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001763 sb->data_offset = cpu_to_le64(rdev->data_offset);
1764 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001765
NeilBrownc3d97142009-12-14 12:49:52 +11001766 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1767 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001768 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001769 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001770
1771 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001772 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001773 sb->feature_map |=
1774 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1775 sb->recovery_offset =
1776 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001777 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001778 if (test_bit(Replacement, &rdev->flags))
1779 sb->feature_map |=
1780 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001781
NeilBrownf6705572006-03-27 01:18:11 -08001782 if (mddev->reshape_position != MaxSector) {
1783 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1784 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1785 sb->new_layout = cpu_to_le32(mddev->new_layout);
1786 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1787 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001788 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001789 if (mddev->delta_disks == 0 &&
1790 mddev->reshape_backwards)
1791 sb->feature_map
1792 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001793 if (rdev->new_data_offset != rdev->data_offset) {
1794 sb->feature_map
1795 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1796 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1797 - rdev->data_offset));
1798 }
NeilBrownf6705572006-03-27 01:18:11 -08001799 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001800
NeilBrown2699b672011-07-28 11:31:47 +10001801 if (rdev->badblocks.count == 0)
1802 /* Nothing to do for bad blocks*/ ;
1803 else if (sb->bblog_offset == 0)
1804 /* Cannot record bad blocks on this device */
1805 md_error(mddev, rdev);
1806 else {
1807 struct badblocks *bb = &rdev->badblocks;
1808 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1809 u64 *p = bb->page;
1810 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1811 if (bb->changed) {
1812 unsigned seq;
1813
1814retry:
1815 seq = read_seqbegin(&bb->lock);
1816
1817 memset(bbp, 0xff, PAGE_SIZE);
1818
1819 for (i = 0 ; i < bb->count ; i++) {
1820 u64 internal_bb = *p++;
1821 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1822 | BB_LEN(internal_bb));
1823 *bbp++ = cpu_to_le64(store_bb);
1824 }
NeilBrownd0962932012-03-19 12:46:41 +11001825 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001826 if (read_seqretry(&bb->lock, seq))
1827 goto retry;
1828
1829 bb->sector = (rdev->sb_start +
1830 (int)le32_to_cpu(sb->bblog_offset));
1831 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001832 }
1833 }
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001836 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (rdev2->desc_nr+1 > max_dev)
1838 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001839
NeilBrown70471da2009-08-03 10:59:57 +10001840 if (max_dev > le32_to_cpu(sb->max_dev)) {
1841 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001842 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001843 rdev->sb_size = max_dev * 2 + 256;
1844 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1845 if (rdev->sb_size & bmask)
1846 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001847 } else
1848 max_dev = le32_to_cpu(sb->max_dev);
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 for (i=0; i<max_dev;i++)
1851 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1852
NeilBrowndafb20f2012-03-19 12:46:39 +11001853 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001855 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001857 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001859 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001860 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 else
1862 sb->dev_roles[i] = cpu_to_le16(0xffff);
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 sb->sb_csum = calc_sb_1_csum(sb);
1866}
1867
Chris Webb0cd17fe2008-06-28 08:31:46 +10001868static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001869super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001870{
1871 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001872 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001873 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001874 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001875 if (rdev->data_offset != rdev->new_data_offset)
1876 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001877 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001879 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001880 max_sectors -= rdev->data_offset;
1881 if (!num_sectors || num_sectors > max_sectors)
1882 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001883 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884 /* minor version 0 with bitmap we can't move */
1885 return 0;
1886 } else {
1887 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001888 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001889 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001890 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001891 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001892 if (!num_sectors || num_sectors > max_sectors)
1893 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001894 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001895 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001896 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001897 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001898 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001900 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901 rdev->sb_page);
1902 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001903 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001904
1905}
1906
1907static int
1908super_1_allow_new_offset(struct md_rdev *rdev,
1909 unsigned long long new_offset)
1910{
1911 /* All necessary checks on new >= old have been done */
1912 struct bitmap *bitmap;
1913 if (new_offset >= rdev->data_offset)
1914 return 1;
1915
1916 /* with 1.0 metadata, there is no metadata to tread on
1917 * so we can always move back */
1918 if (rdev->mddev->minor_version == 0)
1919 return 1;
1920
1921 /* otherwise we must be sure not to step on
1922 * any metadata, so stay:
1923 * 36K beyond start of superblock
1924 * beyond end of badblocks
1925 * beyond write-intent bitmap
1926 */
1927 if (rdev->sb_start + (32+4)*2 > new_offset)
1928 return 0;
1929 bitmap = rdev->mddev->bitmap;
1930 if (bitmap && !rdev->mddev->bitmap_info.file &&
1931 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001932 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001933 return 0;
1934 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1935 return 0;
1936
1937 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Adrian Bunk75c96f82005-05-05 16:16:09 -07001940static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 [0] = {
1942 .name = "0.90.0",
1943 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001944 .load_super = super_90_load,
1945 .validate_super = super_90_validate,
1946 .sync_super = super_90_sync,
1947 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001948 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 },
1950 [1] = {
1951 .name = "md-1",
1952 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001953 .load_super = super_1_load,
1954 .validate_super = super_1_validate,
1955 .sync_super = super_1_sync,
1956 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001957 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 },
1959};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
NeilBrownfd01b882011-10-11 16:47:53 +11001961static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001962{
1963 if (mddev->sync_super) {
1964 mddev->sync_super(mddev, rdev);
1965 return;
1966 }
1967
1968 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1969
1970 super_types[mddev->major_version].sync_super(mddev, rdev);
1971}
1972
NeilBrownfd01b882011-10-11 16:47:53 +11001973static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
NeilBrown3cb03002011-10-11 16:45:26 +11001975 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
NeilBrown4b809912008-07-21 17:05:25 +10001977 rcu_read_lock();
1978 rdev_for_each_rcu(rdev, mddev1)
1979 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001980 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001981 rdev2->bdev->bd_contains) {
1982 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001983 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001984 }
1985 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987}
1988
1989static LIST_HEAD(pending_raid_disks);
1990
Andre Nollac5e7112009-08-03 10:59:47 +10001991/*
1992 * Try to register data integrity profile for an mddev
1993 *
1994 * This is called when an array is started and after a disk has been kicked
1995 * from the array. It only succeeds if all working and active component devices
1996 * are integrity capable with matching profiles.
1997 */
NeilBrownfd01b882011-10-11 16:47:53 +11001998int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001999{
NeilBrown3cb03002011-10-11 16:45:26 +11002000 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002001
Andre Nollac5e7112009-08-03 10:59:47 +10002002 if (list_empty(&mddev->disks))
2003 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002004 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2005 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002006 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002007 /* skip spares and non-functional disks */
2008 if (test_bit(Faulty, &rdev->flags))
2009 continue;
2010 if (rdev->raid_disk < 0)
2011 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002012 if (!reference) {
2013 /* Use the first rdev as the reference */
2014 reference = rdev;
2015 continue;
2016 }
2017 /* does this rdev's profile match the reference profile? */
2018 if (blk_integrity_compare(reference->bdev->bd_disk,
2019 rdev->bdev->bd_disk) < 0)
2020 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002021 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002022 if (!reference || !bdev_get_integrity(reference->bdev))
2023 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002024 /*
2025 * All component devices are integrity capable and have matching
2026 * profiles, register the common profile for the md device.
2027 */
2028 if (blk_integrity_register(mddev->gendisk,
2029 bdev_get_integrity(reference->bdev)) != 0) {
2030 printk(KERN_ERR "md: failed to register integrity for %s\n",
2031 mdname(mddev));
2032 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002033 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002034 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2035 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2036 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2037 mdname(mddev));
2038 return -EINVAL;
2039 }
Andre Nollac5e7112009-08-03 10:59:47 +10002040 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002041}
Andre Nollac5e7112009-08-03 10:59:47 +10002042EXPORT_SYMBOL(md_integrity_register);
2043
2044/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002045void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002046{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002047 struct blk_integrity *bi_rdev;
2048 struct blk_integrity *bi_mddev;
2049
2050 if (!mddev->gendisk)
2051 return;
2052
2053 bi_rdev = bdev_get_integrity(rdev->bdev);
2054 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002055
2056 if (!bi_mddev) /* nothing to do */
2057 return;
2058 if (rdev->raid_disk < 0) /* skip spares */
2059 return;
2060 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2061 rdev->bdev->bd_disk) >= 0)
2062 return;
2063 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2064 blk_integrity_unregister(mddev->gendisk);
2065}
2066EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002067
NeilBrownfd01b882011-10-11 16:47:53 +11002068static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002070 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002071 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002072 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002073 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (rdev->mddev) {
2076 MD_BUG();
2077 return -EINVAL;
2078 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002079
2080 /* prevent duplicates */
2081 if (find_rdev(mddev, rdev->bdev->bd_dev))
2082 return -EEXIST;
2083
Andre Nolldd8ac332009-03-31 14:33:13 +11002084 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2085 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2086 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002087 if (mddev->pers) {
2088 /* Cannot change size, so fail
2089 * If mddev->level <= 0, then we don't care
2090 * about aligning sizes (e.g. linear)
2091 */
2092 if (mddev->level > 0)
2093 return -ENOSPC;
2094 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002095 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* Verify rdev->desc_nr is unique.
2099 * If it is -1, assign a free number, else
2100 * check number is not in use
2101 */
2102 if (rdev->desc_nr < 0) {
2103 int choice = 0;
2104 if (mddev->pers) choice = mddev->raid_disks;
2105 while (find_rdev_nr(mddev, choice))
2106 choice++;
2107 rdev->desc_nr = choice;
2108 } else {
2109 if (find_rdev_nr(mddev, rdev->desc_nr))
2110 return -EBUSY;
2111 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002112 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2113 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2114 mdname(mddev), mddev->max_disks);
2115 return -EBUSY;
2116 }
NeilBrown19133a42005-11-08 21:39:35 -08002117 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002118 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002119 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002122 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002123
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002124 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002125 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002126
Tejun Heo0762b8b2008-08-25 19:56:12 +09002127 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002128 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2129 /* failure here is OK */;
2130 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002131
NeilBrown4b809912008-07-21 17:05:25 +10002132 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002133 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002134
2135 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002136 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002139
2140 fail:
2141 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2142 b, mdname(mddev));
2143 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
NeilBrown177a99b2008-02-06 01:39:56 -08002146static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002147{
NeilBrown3cb03002011-10-11 16:45:26 +11002148 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002149 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002150 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002151}
2152
NeilBrown3cb03002011-10-11 16:45:26 +11002153static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 char b[BDEVNAME_SIZE];
2156 if (!rdev->mddev) {
2157 MD_BUG();
2158 return;
2159 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002160 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002161 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2163 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002164 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002165 sysfs_put(rdev->sysfs_state);
2166 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002167 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002168 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002169 * writing to 'remove' to "dev/state". We also need
2170 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002171 */
NeilBrown4b809912008-07-21 17:05:25 +10002172 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002173 INIT_WORK(&rdev->del_work, md_delayed_delete);
2174 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002175 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178/*
2179 * prevent the device from being mounted, repartitioned or
2180 * otherwise reused by a RAID array (or any other kernel
2181 * subsystem), by bd_claiming the device.
2182 */
NeilBrown3cb03002011-10-11 16:45:26 +11002183static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 int err = 0;
2186 struct block_device *bdev;
2187 char b[BDEVNAME_SIZE];
2188
Tejun Heod4d77622010-11-13 11:55:18 +01002189 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002190 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (IS_ERR(bdev)) {
2192 printk(KERN_ERR "md: could not open %s.\n",
2193 __bdevname(dev, b));
2194 return PTR_ERR(bdev);
2195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 rdev->bdev = bdev;
2197 return err;
2198}
2199
NeilBrown3cb03002011-10-11 16:45:26 +11002200static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct block_device *bdev = rdev->bdev;
2203 rdev->bdev = NULL;
2204 if (!bdev)
2205 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002206 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
2209void md_autodetect_dev(dev_t dev);
2210
NeilBrown3cb03002011-10-11 16:45:26 +11002211static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 char b[BDEVNAME_SIZE];
2214 printk(KERN_INFO "md: export_rdev(%s)\n",
2215 bdevname(rdev->bdev,b));
2216 if (rdev->mddev)
2217 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002218 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002220 if (test_bit(AutoDetected, &rdev->flags))
2221 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222#endif
2223 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002224 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
NeilBrown3cb03002011-10-11 16:45:26 +11002227static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 unbind_rdev_from_array(rdev);
2230 export_rdev(rdev);
2231}
2232
NeilBrownfd01b882011-10-11 16:47:53 +11002233static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
NeilBrown3cb03002011-10-11 16:45:26 +11002235 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
NeilBrowndafb20f2012-03-19 12:46:39 +11002237 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (!rdev->mddev) {
2239 MD_BUG();
2240 continue;
2241 }
2242 kick_rdev_from_array(rdev);
2243 }
2244 if (!list_empty(&mddev->disks))
2245 MD_BUG();
2246 mddev->raid_disks = 0;
2247 mddev->major_version = 0;
2248}
2249
2250static void print_desc(mdp_disk_t *desc)
2251{
2252 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2253 desc->major,desc->minor,desc->raid_disk,desc->state);
2254}
2255
Cheng Renquancd2ac932009-01-09 08:31:08 +11002256static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 int i;
2259
2260 printk(KERN_INFO
2261 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2262 sb->major_version, sb->minor_version, sb->patch_version,
2263 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2264 sb->ctime);
2265 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2266 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2267 sb->md_minor, sb->layout, sb->chunk_size);
2268 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2269 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2270 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2271 sb->failed_disks, sb->spare_disks,
2272 sb->sb_csum, (unsigned long)sb->events_lo);
2273
2274 printk(KERN_INFO);
2275 for (i = 0; i < MD_SB_DISKS; i++) {
2276 mdp_disk_t *desc;
2277
2278 desc = sb->disks + i;
2279 if (desc->number || desc->major || desc->minor ||
2280 desc->raid_disk || (desc->state && (desc->state != 4))) {
2281 printk(" D %2d: ", i);
2282 print_desc(desc);
2283 }
2284 }
2285 printk(KERN_INFO "md: THIS: ");
2286 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Cheng Renquancd2ac932009-01-09 08:31:08 +11002289static void print_sb_1(struct mdp_superblock_1 *sb)
2290{
2291 __u8 *uuid;
2292
2293 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002294 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002295 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002296 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002297 le32_to_cpu(sb->major_version),
2298 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002299 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002300 sb->set_name,
2301 (unsigned long long)le64_to_cpu(sb->ctime)
2302 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2303
2304 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002305 printk(KERN_INFO
2306 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002307 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002308 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002309 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2310 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002311 le32_to_cpu(sb->level),
2312 (unsigned long long)le64_to_cpu(sb->size),
2313 le32_to_cpu(sb->raid_disks),
2314 le32_to_cpu(sb->layout),
2315 le32_to_cpu(sb->chunksize),
2316 (unsigned long long)le64_to_cpu(sb->data_offset),
2317 (unsigned long long)le64_to_cpu(sb->data_size),
2318 (unsigned long long)le64_to_cpu(sb->super_offset),
2319 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2320 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002321 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002322 sb->devflags,
2323 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2324 (unsigned long long)le64_to_cpu(sb->events),
2325 (unsigned long long)le64_to_cpu(sb->resync_offset),
2326 le32_to_cpu(sb->sb_csum),
2327 le32_to_cpu(sb->max_dev)
2328 );
2329}
2330
NeilBrown3cb03002011-10-11 16:45:26 +11002331static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002334 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2335 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002336 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2337 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002339 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2340 switch (major_version) {
2341 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002342 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002343 break;
2344 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002345 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002346 break;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 } else
2349 printk(KERN_INFO "md: no rdev superblock!\n");
2350}
2351
Adrian Bunk5e563412006-06-26 00:27:42 -07002352static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002354 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002355 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002356 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 char b[BDEVNAME_SIZE];
2358
2359 printk("\n");
2360 printk("md: **********************************\n");
2361 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2362 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002363 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
NeilBrown32a76272005-06-21 17:17:14 -07002365 if (mddev->bitmap)
2366 bitmap_print_sb(mddev->bitmap);
2367 else
2368 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002369 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 printk("<%s>", bdevname(rdev->bdev,b));
2371 printk("\n");
2372
NeilBrowndafb20f2012-03-19 12:46:39 +11002373 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002374 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 printk("md: **********************************\n");
2377 printk("\n");
2378}
2379
2380
NeilBrownfd01b882011-10-11 16:47:53 +11002381static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
NeilBrown42543762006-06-26 00:27:57 -07002383 /* Update each superblock (in-memory image), but
2384 * if we are allowed to, skip spares which already
2385 * have the right event counter, or have one earlier
2386 * (which would mean they aren't being marked as dirty
2387 * with the rest of the array)
2388 */
NeilBrown3cb03002011-10-11 16:45:26 +11002389 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002390 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002391 if (rdev->sb_events == mddev->events ||
2392 (nospares &&
2393 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002394 rdev->sb_events+1 == mddev->events)) {
2395 /* Don't update this superblock */
2396 rdev->sb_loaded = 2;
2397 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002398 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002399 rdev->sb_loaded = 1;
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402}
2403
NeilBrownfd01b882011-10-11 16:47:53 +11002404static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
NeilBrown3cb03002011-10-11 16:45:26 +11002406 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002407 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002408 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002409 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002412 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002413 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002414 if (rdev->raid_disk >= 0 &&
2415 mddev->delta_disks >= 0 &&
2416 !test_bit(In_sync, &rdev->flags) &&
2417 mddev->curr_resync_completed > rdev->recovery_offset)
2418 rdev->recovery_offset = mddev->curr_resync_completed;
2419
2420 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002421 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002422 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002423 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002424 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002425 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002426 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002427 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002428 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002429 md_ack_all_badblocks(&rdev->badblocks);
2430 md_error(mddev, rdev);
2431 }
2432 clear_bit(Blocked, &rdev->flags);
2433 clear_bit(BlockedBadBlocks, &rdev->flags);
2434 wake_up(&rdev->blocked_wait);
2435 }
2436 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002437 wake_up(&mddev->sb_wait);
2438 return;
2439 }
2440
NeilBrowna9701a32005-11-08 21:39:34 -08002441 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002442
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002443 mddev->utime = get_seconds();
2444
NeilBrown850b2b42006-10-03 01:15:46 -07002445 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2446 force_change = 1;
2447 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2448 /* just a clean<-> dirty transition, possibly leave spares alone,
2449 * though if events isn't the right even/odd, we will have to do
2450 * spares after all
2451 */
2452 nospares = 1;
2453 if (force_change)
2454 nospares = 0;
2455 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002456 /* If the array is degraded, then skipping spares is both
2457 * dangerous and fairly pointless.
2458 * Dangerous because a device that was removed from the array
2459 * might have a event_count that still looks up-to-date,
2460 * so it can be re-added without a resync.
2461 * Pointless because if there are any spares to skip,
2462 * then a recovery will happen and soon that array won't
2463 * be degraded any more and the spare can go back to sleep then.
2464 */
NeilBrown850b2b42006-10-03 01:15:46 -07002465 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002466
NeilBrown06d91a52005-06-21 17:17:12 -07002467 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002468
2469 /* If this is just a dirty<->clean transition, and the array is clean
2470 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002471 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002472 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002473 && mddev->can_decrease_events
2474 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002475 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002476 mddev->can_decrease_events = 0;
2477 } else {
NeilBrown42543762006-06-26 00:27:57 -07002478 /* otherwise we have to go forward and ... */
2479 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002480 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 if (!mddev->events) {
2484 /*
2485 * oops, this 64-bit counter should never wrap.
2486 * Either we are in around ~1 trillion A.C., assuming
2487 * 1 reboot per second, or we have a bug:
2488 */
2489 MD_BUG();
2490 mddev->events --;
2491 }
NeilBrown2699b672011-07-28 11:31:47 +10002492
NeilBrowndafb20f2012-03-19 12:46:39 +11002493 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002494 if (rdev->badblocks.changed)
2495 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002496 if (test_bit(Faulty, &rdev->flags))
2497 set_bit(FaultRecorded, &rdev->flags);
2498 }
NeilBrown2699b672011-07-28 11:31:47 +10002499
NeilBrowne6910632008-02-06 01:39:51 -08002500 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002501 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
NeilBrown36a4e1f2011-10-07 14:23:17 +11002503 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2504 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
NeilBrown4ad13662007-07-17 04:06:13 -07002506 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002507 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002509
NeilBrown42543762006-06-26 00:27:57 -07002510 if (rdev->sb_loaded != 1)
2511 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002513 if (!test_bit(Faulty, &rdev->flags) &&
2514 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002515 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002516 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002517 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002518 pr_debug("md: (write) %s's sb offset: %llu\n",
2519 bdevname(rdev->bdev, b),
2520 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002521 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002522 if (rdev->badblocks.size) {
2523 md_super_write(mddev, rdev,
2524 rdev->badblocks.sector,
2525 rdev->badblocks.size << 9,
2526 rdev->bb_page);
2527 rdev->badblocks.size = 0;
2528 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002529
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002530 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002531 pr_debug("md: %s (skipping faulty)\n",
2532 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002533 else
2534 pr_debug("(skipping incremental s/r ");
2535
NeilBrown7bfa19f2005-06-21 17:17:28 -07002536 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* only need to write one superblock... */
2538 break;
2539 }
NeilBrowna9701a32005-11-08 21:39:34 -08002540 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002541 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002542
NeilBrowna9701a32005-11-08 21:39:34 -08002543 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002544 if (mddev->in_sync != sync_req ||
2545 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002546 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002547 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002548 goto repeat;
2549 }
NeilBrown850b2b42006-10-03 01:15:46 -07002550 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002551 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002552 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002553 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2554 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002555
NeilBrowndafb20f2012-03-19 12:46:39 +11002556 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002557 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2558 clear_bit(Blocked, &rdev->flags);
2559
2560 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002561 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002562 clear_bit(BlockedBadBlocks, &rdev->flags);
2563 wake_up(&rdev->blocked_wait);
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Andre Noll7f6ce762008-03-23 18:34:54 +01002567/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002568 * We want to accept with case. For this we use cmd_match.
2569 */
2570static int cmd_match(const char *cmd, const char *str)
2571{
2572 /* See if cmd, written into a sysfs file, matches
2573 * str. They must either be the same, or cmd can
2574 * have a trailing newline
2575 */
2576 while (*cmd && *str && *cmd == *str) {
2577 cmd++;
2578 str++;
2579 }
2580 if (*cmd == '\n')
2581 cmd++;
2582 if (*str || *cmd)
2583 return 0;
2584 return 1;
2585}
2586
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587struct rdev_sysfs_entry {
2588 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002589 ssize_t (*show)(struct md_rdev *, char *);
2590 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002591};
2592
2593static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002594state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002595{
2596 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002597 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002598
NeilBrownde393cd2011-07-28 11:31:48 +10002599 if (test_bit(Faulty, &rdev->flags) ||
2600 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601 len+= sprintf(page+len, "%sfaulty",sep);
2602 sep = ",";
2603 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002604 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002605 len += sprintf(page+len, "%sin_sync",sep);
2606 sep = ",";
2607 }
NeilBrownf6556752006-06-26 00:28:01 -07002608 if (test_bit(WriteMostly, &rdev->flags)) {
2609 len += sprintf(page+len, "%swrite_mostly",sep);
2610 sep = ",";
2611 }
NeilBrownde393cd2011-07-28 11:31:48 +10002612 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002613 (rdev->badblocks.unacked_exist
2614 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002615 len += sprintf(page+len, "%sblocked", sep);
2616 sep = ",";
2617 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002618 if (!test_bit(Faulty, &rdev->flags) &&
2619 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002620 len += sprintf(page+len, "%sspare", sep);
2621 sep = ",";
2622 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002623 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2624 len += sprintf(page+len, "%swrite_error", sep);
2625 sep = ",";
2626 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002627 if (test_bit(WantReplacement, &rdev->flags)) {
2628 len += sprintf(page+len, "%swant_replacement", sep);
2629 sep = ",";
2630 }
2631 if (test_bit(Replacement, &rdev->flags)) {
2632 len += sprintf(page+len, "%sreplacement", sep);
2633 sep = ",";
2634 }
2635
NeilBrown86e6ffd2005-11-08 21:39:24 -08002636 return len+sprintf(page+len, "\n");
2637}
2638
NeilBrown45dc2de2006-06-26 00:27:58 -07002639static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002640state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002641{
2642 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002643 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002644 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002645 * writemostly - sets write_mostly
2646 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002647 * blocked - sets the Blocked flags
2648 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002649 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002650 * write_error - sets WriteErrorSeen
2651 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002652 */
2653 int err = -EINVAL;
2654 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2655 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002656 if (test_bit(Faulty, &rdev->flags))
2657 err = 0;
2658 else
2659 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002660 } else if (cmd_match(buf, "remove")) {
2661 if (rdev->raid_disk >= 0)
2662 err = -EBUSY;
2663 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002664 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002665 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002666 if (mddev->pers)
2667 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002668 md_new_event(mddev);
2669 err = 0;
2670 }
NeilBrownf6556752006-06-26 00:28:01 -07002671 } else if (cmd_match(buf, "writemostly")) {
2672 set_bit(WriteMostly, &rdev->flags);
2673 err = 0;
2674 } else if (cmd_match(buf, "-writemostly")) {
2675 clear_bit(WriteMostly, &rdev->flags);
2676 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002677 } else if (cmd_match(buf, "blocked")) {
2678 set_bit(Blocked, &rdev->flags);
2679 err = 0;
2680 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002681 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002682 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002683 /* metadata handler doesn't understand badblocks,
2684 * so we need to fail the device
2685 */
2686 md_error(rdev->mddev, rdev);
2687 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002688 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002689 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002690 wake_up(&rdev->blocked_wait);
2691 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2692 md_wakeup_thread(rdev->mddev->thread);
2693
2694 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002695 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2696 set_bit(In_sync, &rdev->flags);
2697 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002698 } else if (cmd_match(buf, "write_error")) {
2699 set_bit(WriteErrorSeen, &rdev->flags);
2700 err = 0;
2701 } else if (cmd_match(buf, "-write_error")) {
2702 clear_bit(WriteErrorSeen, &rdev->flags);
2703 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002704 } else if (cmd_match(buf, "want_replacement")) {
2705 /* Any non-spare device that is not a replacement can
2706 * become want_replacement at any time, but we then need to
2707 * check if recovery is needed.
2708 */
2709 if (rdev->raid_disk >= 0 &&
2710 !test_bit(Replacement, &rdev->flags))
2711 set_bit(WantReplacement, &rdev->flags);
2712 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2713 md_wakeup_thread(rdev->mddev->thread);
2714 err = 0;
2715 } else if (cmd_match(buf, "-want_replacement")) {
2716 /* Clearing 'want_replacement' is always allowed.
2717 * Once replacements starts it is too late though.
2718 */
2719 err = 0;
2720 clear_bit(WantReplacement, &rdev->flags);
2721 } else if (cmd_match(buf, "replacement")) {
2722 /* Can only set a device as a replacement when array has not
2723 * yet been started. Once running, replacement is automatic
2724 * from spares, or by assigning 'slot'.
2725 */
2726 if (rdev->mddev->pers)
2727 err = -EBUSY;
2728 else {
2729 set_bit(Replacement, &rdev->flags);
2730 err = 0;
2731 }
2732 } else if (cmd_match(buf, "-replacement")) {
2733 /* Similarly, can only clear Replacement before start */
2734 if (rdev->mddev->pers)
2735 err = -EBUSY;
2736 else {
2737 clear_bit(Replacement, &rdev->flags);
2738 err = 0;
2739 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002740 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002741 if (!err)
2742 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002743 return err ? err : len;
2744}
NeilBrown80ca3a42006-07-10 04:44:18 -07002745static struct rdev_sysfs_entry rdev_state =
2746__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002747
2748static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002749errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002750{
2751 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2752}
2753
2754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002756{
2757 char *e;
2758 unsigned long n = simple_strtoul(buf, &e, 10);
2759 if (*buf && (*e == 0 || *e == '\n')) {
2760 atomic_set(&rdev->corrected_errors, n);
2761 return len;
2762 }
2763 return -EINVAL;
2764}
2765static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002766__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002767
NeilBrown014236d2006-01-06 00:20:55 -08002768static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002769slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002770{
2771 if (rdev->raid_disk < 0)
2772 return sprintf(page, "none\n");
2773 else
2774 return sprintf(page, "%d\n", rdev->raid_disk);
2775}
2776
2777static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002778slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002779{
2780 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002781 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002782 int slot = simple_strtoul(buf, &e, 10);
2783 if (strncmp(buf, "none", 4)==0)
2784 slot = -1;
2785 else if (e==buf || (*e && *e!= '\n'))
2786 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002787 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002788 /* Setting 'slot' on an active array requires also
2789 * updating the 'rd%d' link, and communicating
2790 * with the personality with ->hot_*_disk.
2791 * For now we only support removing
2792 * failed/spare devices. This normally happens automatically,
2793 * but not when the metadata is externally managed.
2794 */
NeilBrownc303da62008-02-06 01:39:51 -08002795 if (rdev->raid_disk == -1)
2796 return -EEXIST;
2797 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002798 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002799 return -EINVAL;
2800 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002801 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002802 if (err)
2803 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002804 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002805 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002806 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2807 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002809 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002810 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002811 */
2812
2813 if (rdev->raid_disk != -1)
2814 return -EBUSY;
2815
NeilBrownc6751b22011-02-02 11:57:13 +11002816 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2817 return -EBUSY;
2818
Neil Brown6c2fce22008-06-28 08:31:31 +10002819 if (rdev->mddev->pers->hot_add_disk == NULL)
2820 return -EINVAL;
2821
NeilBrownba1b41b2011-01-14 09:14:34 +11002822 if (slot >= rdev->mddev->raid_disks &&
2823 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2824 return -ENOSPC;
2825
Neil Brown6c2fce22008-06-28 08:31:31 +10002826 rdev->raid_disk = slot;
2827 if (test_bit(In_sync, &rdev->flags))
2828 rdev->saved_raid_disk = slot;
2829 else
2830 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002831 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002832 err = rdev->mddev->pers->
2833 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002834 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002836 return err;
Neil Brown52664732008-06-28 08:31:44 +10002837 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002838 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002839 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002840 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002842 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002843 if (slot >= rdev->mddev->raid_disks &&
2844 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002845 return -ENOSPC;
2846 rdev->raid_disk = slot;
2847 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 clear_bit(Faulty, &rdev->flags);
2849 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002850 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002851 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002852 }
NeilBrown014236d2006-01-06 00:20:55 -08002853 return len;
2854}
2855
2856
2857static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002858__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002859
NeilBrown93c8cad2006-01-06 00:20:56 -08002860static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002861offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002862{
Andrew Morton6961ece2006-01-06 00:20:59 -08002863 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002864}
2865
2866static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002867offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002868{
NeilBrownc6563a82012-05-21 09:27:00 +10002869 unsigned long long offset;
2870 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002871 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002872 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002873 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002874 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002875 /* Must set offset before size, so overlap checks
2876 * can be sane */
2877 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002878 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002879 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002880 return len;
2881}
2882
2883static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002884__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002885
NeilBrownc6563a82012-05-21 09:27:00 +10002886static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2887{
2888 return sprintf(page, "%llu\n",
2889 (unsigned long long)rdev->new_data_offset);
2890}
2891
2892static ssize_t new_offset_store(struct md_rdev *rdev,
2893 const char *buf, size_t len)
2894{
2895 unsigned long long new_offset;
2896 struct mddev *mddev = rdev->mddev;
2897
2898 if (strict_strtoull(buf, 10, &new_offset) < 0)
2899 return -EINVAL;
2900
2901 if (mddev->sync_thread)
2902 return -EBUSY;
2903 if (new_offset == rdev->data_offset)
2904 /* reset is always permitted */
2905 ;
2906 else if (new_offset > rdev->data_offset) {
2907 /* must not push array size beyond rdev_sectors */
2908 if (new_offset - rdev->data_offset
2909 + mddev->dev_sectors > rdev->sectors)
2910 return -E2BIG;
2911 }
2912 /* Metadata worries about other space details. */
2913
2914 /* decreasing the offset is inconsistent with a backwards
2915 * reshape.
2916 */
2917 if (new_offset < rdev->data_offset &&
2918 mddev->reshape_backwards)
2919 return -EINVAL;
2920 /* Increasing offset is inconsistent with forwards
2921 * reshape. reshape_direction should be set to
2922 * 'backwards' first.
2923 */
2924 if (new_offset > rdev->data_offset &&
2925 !mddev->reshape_backwards)
2926 return -EINVAL;
2927
2928 if (mddev->pers && mddev->persistent &&
2929 !super_types[mddev->major_version]
2930 .allow_new_offset(rdev, new_offset))
2931 return -E2BIG;
2932 rdev->new_data_offset = new_offset;
2933 if (new_offset > rdev->data_offset)
2934 mddev->reshape_backwards = 1;
2935 else if (new_offset < rdev->data_offset)
2936 mddev->reshape_backwards = 0;
2937
2938 return len;
2939}
2940static struct rdev_sysfs_entry rdev_new_offset =
2941__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2942
NeilBrown83303b62006-01-06 00:21:06 -08002943static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002944rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002945{
Andre Nolldd8ac332009-03-31 14:33:13 +11002946 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002947}
2948
NeilBrownc5d79ad2008-02-06 01:39:54 -08002949static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2950{
2951 /* check if two start/length pairs overlap */
2952 if (s1+l1 <= s2)
2953 return 0;
2954 if (s2+l2 <= s1)
2955 return 0;
2956 return 1;
2957}
2958
Dan Williamsb522adc2009-03-31 15:00:31 +11002959static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2960{
2961 unsigned long long blocks;
2962 sector_t new;
2963
2964 if (strict_strtoull(buf, 10, &blocks) < 0)
2965 return -EINVAL;
2966
2967 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2968 return -EINVAL; /* sector conversion overflow */
2969
2970 new = blocks * 2;
2971 if (new != blocks * 2)
2972 return -EINVAL; /* unsigned long long to sector_t overflow */
2973
2974 *sectors = new;
2975 return 0;
2976}
2977
NeilBrown83303b62006-01-06 00:21:06 -08002978static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002979rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002980{
NeilBrownfd01b882011-10-11 16:47:53 +11002981 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002982 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002983 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002984
Dan Williamsb522adc2009-03-31 15:00:31 +11002985 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002986 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002987 if (rdev->data_offset != rdev->new_data_offset)
2988 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002989 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002990 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002991 sectors = super_types[my_mddev->major_version].
2992 rdev_size_change(rdev, sectors);
2993 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002994 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002995 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002996 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002998 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002999 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003000 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003001
Andre Nolldd8ac332009-03-31 14:33:13 +11003002 rdev->sectors = sectors;
3003 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 /* need to check that all other rdevs with the same ->bdev
3005 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003006 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003007 * we have to change it back, we will have the lock again.
3008 */
NeilBrownfd01b882011-10-11 16:47:53 +11003009 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003011 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003012
NeilBrown27c529b2008-03-04 14:29:33 -08003013 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003014 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003015 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016
3017 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003018 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003019 if (rdev->bdev == rdev2->bdev &&
3020 rdev != rdev2 &&
3021 overlaps(rdev->data_offset, rdev->sectors,
3022 rdev2->data_offset,
3023 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003024 overlap = 1;
3025 break;
3026 }
3027 mddev_unlock(mddev);
3028 if (overlap) {
3029 mddev_put(mddev);
3030 break;
3031 }
3032 }
NeilBrown27c529b2008-03-04 14:29:33 -08003033 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003034 if (overlap) {
3035 /* Someone else could have slipped in a size
3036 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003037 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003038 * safe, and trust userspace not to race with
3039 * itself
3040 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003041 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003042 return -EBUSY;
3043 }
3044 }
NeilBrown83303b62006-01-06 00:21:06 -08003045 return len;
3046}
3047
3048static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003049__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003050
Dan Williams06e3c812009-12-12 21:17:12 -07003051
NeilBrown3cb03002011-10-11 16:45:26 +11003052static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003053{
3054 unsigned long long recovery_start = rdev->recovery_offset;
3055
3056 if (test_bit(In_sync, &rdev->flags) ||
3057 recovery_start == MaxSector)
3058 return sprintf(page, "none\n");
3059
3060 return sprintf(page, "%llu\n", recovery_start);
3061}
3062
NeilBrown3cb03002011-10-11 16:45:26 +11003063static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003064{
3065 unsigned long long recovery_start;
3066
3067 if (cmd_match(buf, "none"))
3068 recovery_start = MaxSector;
3069 else if (strict_strtoull(buf, 10, &recovery_start))
3070 return -EINVAL;
3071
3072 if (rdev->mddev->pers &&
3073 rdev->raid_disk >= 0)
3074 return -EBUSY;
3075
3076 rdev->recovery_offset = recovery_start;
3077 if (recovery_start == MaxSector)
3078 set_bit(In_sync, &rdev->flags);
3079 else
3080 clear_bit(In_sync, &rdev->flags);
3081 return len;
3082}
3083
3084static struct rdev_sysfs_entry rdev_recovery_start =
3085__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3086
NeilBrown16c791a2011-07-28 11:31:47 +10003087
3088static ssize_t
3089badblocks_show(struct badblocks *bb, char *page, int unack);
3090static ssize_t
3091badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3092
NeilBrown3cb03002011-10-11 16:45:26 +11003093static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003094{
3095 return badblocks_show(&rdev->badblocks, page, 0);
3096}
NeilBrown3cb03002011-10-11 16:45:26 +11003097static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003098{
NeilBrownde393cd2011-07-28 11:31:48 +10003099 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3100 /* Maybe that ack was all we needed */
3101 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3102 wake_up(&rdev->blocked_wait);
3103 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003104}
3105static struct rdev_sysfs_entry rdev_bad_blocks =
3106__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3107
3108
NeilBrown3cb03002011-10-11 16:45:26 +11003109static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003110{
3111 return badblocks_show(&rdev->badblocks, page, 1);
3112}
NeilBrown3cb03002011-10-11 16:45:26 +11003113static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003114{
3115 return badblocks_store(&rdev->badblocks, page, len, 1);
3116}
3117static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3118__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3119
NeilBrown86e6ffd2005-11-08 21:39:24 -08003120static struct attribute *rdev_default_attrs[] = {
3121 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003122 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003123 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003124 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003125 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003126 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003127 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003128 &rdev_bad_blocks.attr,
3129 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003130 NULL,
3131};
3132static ssize_t
3133rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3134{
3135 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003136 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003137 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003138 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003139
3140 if (!entry->show)
3141 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003142
3143 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3144 if (!rv) {
3145 if (rdev->mddev == NULL)
3146 rv = -EBUSY;
3147 else
3148 rv = entry->show(rdev, page);
3149 mddev_unlock(mddev);
3150 }
3151 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003152}
3153
3154static ssize_t
3155rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3156 const char *page, size_t length)
3157{
3158 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003159 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003160 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003161 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003162
3163 if (!entry->store)
3164 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003165 if (!capable(CAP_SYS_ADMIN))
3166 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003167 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003168 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003169 if (rdev->mddev == NULL)
3170 rv = -EBUSY;
3171 else
3172 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003173 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003174 }
3175 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003176}
3177
3178static void rdev_free(struct kobject *ko)
3179{
NeilBrown3cb03002011-10-11 16:45:26 +11003180 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003181 kfree(rdev);
3182}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003183static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003184 .show = rdev_attr_show,
3185 .store = rdev_attr_store,
3186};
3187static struct kobj_type rdev_ktype = {
3188 .release = rdev_free,
3189 .sysfs_ops = &rdev_sysfs_ops,
3190 .default_attrs = rdev_default_attrs,
3191};
3192
NeilBrown3cb03002011-10-11 16:45:26 +11003193int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003194{
3195 rdev->desc_nr = -1;
3196 rdev->saved_raid_disk = -1;
3197 rdev->raid_disk = -1;
3198 rdev->flags = 0;
3199 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003200 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003201 rdev->sb_events = 0;
3202 rdev->last_read_error.tv_sec = 0;
3203 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003204 rdev->sb_loaded = 0;
3205 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003206 atomic_set(&rdev->nr_pending, 0);
3207 atomic_set(&rdev->read_errors, 0);
3208 atomic_set(&rdev->corrected_errors, 0);
3209
3210 INIT_LIST_HEAD(&rdev->same_set);
3211 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003212
3213 /* Add space to store bad block list.
3214 * This reserves the space even on arrays where it cannot
3215 * be used - I wonder if that matters
3216 */
3217 rdev->badblocks.count = 0;
3218 rdev->badblocks.shift = 0;
3219 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3220 seqlock_init(&rdev->badblocks.lock);
3221 if (rdev->badblocks.page == NULL)
3222 return -ENOMEM;
3223
3224 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003225}
3226EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227/*
3228 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3229 *
3230 * mark the device faulty if:
3231 *
3232 * - the device is nonexistent (zero size)
3233 * - the device has no valid superblock
3234 *
3235 * a faulty rdev _never_ has rdev->sb set.
3236 */
NeilBrown3cb03002011-10-11 16:45:26 +11003237static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 char b[BDEVNAME_SIZE];
3240 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003241 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 sector_t size;
3243
NeilBrown9ffae0c2006-01-06 00:20:32 -08003244 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (!rdev) {
3246 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3247 return ERR_PTR(-ENOMEM);
3248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
NeilBrown2230dfe2011-07-28 11:31:46 +10003250 err = md_rdev_init(rdev);
3251 if (err)
3252 goto abort_free;
3253 err = alloc_disk_sb(rdev);
3254 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto abort_free;
3256
NeilBrownc5d79ad2008-02-06 01:39:54 -08003257 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (err)
3259 goto abort_free;
3260
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003261 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003262
Mike Snitzer77304d22010-11-08 14:39:12 +01003263 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (!size) {
3265 printk(KERN_WARNING
3266 "md: %s has zero or unknown size, marking faulty!\n",
3267 bdevname(rdev->bdev,b));
3268 err = -EINVAL;
3269 goto abort_free;
3270 }
3271
3272 if (super_format >= 0) {
3273 err = super_types[super_format].
3274 load_super(rdev, NULL, super_minor);
3275 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003276 printk(KERN_WARNING
3277 "md: %s does not have a valid v%d.%d "
3278 "superblock, not importing!\n",
3279 bdevname(rdev->bdev,b),
3280 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 goto abort_free;
3282 }
3283 if (err < 0) {
3284 printk(KERN_WARNING
3285 "md: could not read %s's sb, not importing!\n",
3286 bdevname(rdev->bdev,b));
3287 goto abort_free;
3288 }
3289 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003290 if (super_format == -1)
3291 /* hot-add for 0.90, or non-persistent: so no badblocks */
3292 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return rdev;
3295
3296abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003297 if (rdev->bdev)
3298 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003299 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 kfree(rdev);
3301 return ERR_PTR(err);
3302}
3303
3304/*
3305 * Check a full RAID array for plausibility
3306 */
3307
3308
NeilBrownfd01b882011-10-11 16:47:53 +11003309static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003312 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 char b[BDEVNAME_SIZE];
3314
3315 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003316 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 switch (super_types[mddev->major_version].
3318 load_super(rdev, freshest, mddev->minor_version)) {
3319 case 1:
3320 freshest = rdev;
3321 break;
3322 case 0:
3323 break;
3324 default:
3325 printk( KERN_ERR \
3326 "md: fatal superblock inconsistency in %s"
3327 " -- removing from array\n",
3328 bdevname(rdev->bdev,b));
3329 kick_rdev_from_array(rdev);
3330 }
3331
3332
3333 super_types[mddev->major_version].
3334 validate_super(mddev, freshest);
3335
3336 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003337 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003338 if (mddev->max_disks &&
3339 (rdev->desc_nr >= mddev->max_disks ||
3340 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003341 printk(KERN_WARNING
3342 "md: %s: %s: only %d devices permitted\n",
3343 mdname(mddev), bdevname(rdev->bdev, b),
3344 mddev->max_disks);
3345 kick_rdev_from_array(rdev);
3346 continue;
3347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (rdev != freshest)
3349 if (super_types[mddev->major_version].
3350 validate_super(mddev, rdev)) {
3351 printk(KERN_WARNING "md: kicking non-fresh %s"
3352 " from array!\n",
3353 bdevname(rdev->bdev,b));
3354 kick_rdev_from_array(rdev);
3355 continue;
3356 }
3357 if (mddev->level == LEVEL_MULTIPATH) {
3358 rdev->desc_nr = i++;
3359 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003360 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003361 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003362 rdev->raid_disk = -1;
3363 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
3365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
NeilBrown72e02072009-12-14 12:49:55 +11003368/* Read a fixed-point number.
3369 * Numbers in sysfs attributes should be in "standard" units where
3370 * possible, so time should be in seconds.
3371 * However we internally use a a much smaller unit such as
3372 * milliseconds or jiffies.
3373 * This function takes a decimal number with a possible fractional
3374 * component, and produces an integer which is the result of
3375 * multiplying that number by 10^'scale'.
3376 * all without any floating-point arithmetic.
3377 */
3378int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3379{
3380 unsigned long result = 0;
3381 long decimals = -1;
3382 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3383 if (*cp == '.')
3384 decimals = 0;
3385 else if (decimals < scale) {
3386 unsigned int value;
3387 value = *cp - '0';
3388 result = result * 10 + value;
3389 if (decimals >= 0)
3390 decimals++;
3391 }
3392 cp++;
3393 }
3394 if (*cp == '\n')
3395 cp++;
3396 if (*cp)
3397 return -EINVAL;
3398 if (decimals < 0)
3399 decimals = 0;
3400 while (decimals < scale) {
3401 result *= 10;
3402 decimals ++;
3403 }
3404 *res = result;
3405 return 0;
3406}
3407
3408
NeilBrown19052c02008-08-05 15:54:13 +10003409static void md_safemode_timeout(unsigned long data);
3410
NeilBrowneae17012005-11-08 21:39:23 -08003411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003412safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003413{
3414 int msec = (mddev->safemode_delay*1000)/HZ;
3415 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3416}
3417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003418safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003419{
NeilBrown16f17b32006-06-26 00:27:37 -07003420 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003421
NeilBrown72e02072009-12-14 12:49:55 +11003422 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003423 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003424 if (msec == 0)
3425 mddev->safemode_delay = 0;
3426 else {
NeilBrown19052c02008-08-05 15:54:13 +10003427 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003428 mddev->safemode_delay = (msec*HZ)/1000;
3429 if (mddev->safemode_delay == 0)
3430 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003431 if (mddev->safemode_delay < old_delay)
3432 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003433 }
3434 return len;
3435}
3436static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003437__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003438
3439static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003440level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003441{
NeilBrown84fc4b52011-10-11 16:49:58 +11003442 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003443 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003444 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003445 else if (mddev->clevel[0])
3446 return sprintf(page, "%s\n", mddev->clevel);
3447 else if (mddev->level != LEVEL_NONE)
3448 return sprintf(page, "%d\n", mddev->level);
3449 else
3450 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003451}
3452
NeilBrownd9d166c2006-01-06 00:20:51 -08003453static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003454level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003455{
Dan Williamsf2859af2010-05-02 10:04:16 -07003456 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003457 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003458 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003459 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003460 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003461 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003462
3463 if (mddev->pers == NULL) {
3464 if (len == 0)
3465 return 0;
3466 if (len >= sizeof(mddev->clevel))
3467 return -ENOSPC;
3468 strncpy(mddev->clevel, buf, len);
3469 if (mddev->clevel[len-1] == '\n')
3470 len--;
3471 mddev->clevel[len] = 0;
3472 mddev->level = LEVEL_NONE;
3473 return rv;
3474 }
3475
3476 /* request to change the personality. Need to ensure:
3477 * - array is not engaged in resync/recovery/reshape
3478 * - old personality can be suspended
3479 * - new personality will access other array.
3480 */
3481
NeilBrownbb4f1e92010-08-08 21:18:03 +10003482 if (mddev->sync_thread ||
3483 mddev->reshape_position != MaxSector ||
3484 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003485 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003486
3487 if (!mddev->pers->quiesce) {
3488 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3489 mdname(mddev), mddev->pers->name);
3490 return -EINVAL;
3491 }
3492
3493 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003495 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003496 strncpy(clevel, buf, len);
3497 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003498 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003499 clevel[len] = 0;
3500 if (strict_strtol(clevel, 10, &level))
3501 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003502
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 if (request_module("md-%s", clevel) != 0)
3504 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003505 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003506 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003507 if (!pers || !try_module_get(pers->owner)) {
3508 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003509 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003510 return -EINVAL;
3511 }
3512 spin_unlock(&pers_lock);
3513
3514 if (pers == mddev->pers) {
3515 /* Nothing to do! */
3516 module_put(pers->owner);
3517 return rv;
3518 }
3519 if (!pers->takeover) {
3520 module_put(pers->owner);
3521 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003522 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003523 return -EINVAL;
3524 }
3525
NeilBrowndafb20f2012-03-19 12:46:39 +11003526 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003527 rdev->new_raid_disk = rdev->raid_disk;
3528
NeilBrown245f46c2009-03-31 14:39:39 +11003529 /* ->takeover must set new_* and/or delta_disks
3530 * if it succeeds, and may set them when it fails.
3531 */
3532 priv = pers->takeover(mddev);
3533 if (IS_ERR(priv)) {
3534 mddev->new_level = mddev->level;
3535 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003536 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003537 mddev->raid_disks -= mddev->delta_disks;
3538 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003539 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003540 module_put(pers->owner);
3541 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003542 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003543 return PTR_ERR(priv);
3544 }
3545
3546 /* Looks like we have a winner */
3547 mddev_suspend(mddev);
3548 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003549
3550 if (mddev->pers->sync_request == NULL &&
3551 pers->sync_request != NULL) {
3552 /* need to add the md_redundancy_group */
3553 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3554 printk(KERN_WARNING
3555 "md: cannot register extra attributes for %s\n",
3556 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003557 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003558 }
3559 if (mddev->pers->sync_request != NULL &&
3560 pers->sync_request == NULL) {
3561 /* need to remove the md_redundancy_group */
3562 if (mddev->to_remove == NULL)
3563 mddev->to_remove = &md_redundancy_group;
3564 }
3565
Trela Maciej54071b32010-03-08 16:02:42 +11003566 if (mddev->pers->sync_request == NULL &&
3567 mddev->external) {
3568 /* We are converting from a no-redundancy array
3569 * to a redundancy array and metadata is managed
3570 * externally so we need to be sure that writes
3571 * won't block due to a need to transition
3572 * clean->dirty
3573 * until external management is started.
3574 */
3575 mddev->in_sync = 0;
3576 mddev->safemode_delay = 0;
3577 mddev->safemode = 0;
3578 }
3579
NeilBrowndafb20f2012-03-19 12:46:39 +11003580 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003581 if (rdev->raid_disk < 0)
3582 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003583 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003584 rdev->new_raid_disk = -1;
3585 if (rdev->new_raid_disk == rdev->raid_disk)
3586 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003587 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003588 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003589 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 if (rdev->raid_disk < 0)
3591 continue;
3592 if (rdev->new_raid_disk == rdev->raid_disk)
3593 continue;
3594 rdev->raid_disk = rdev->new_raid_disk;
3595 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003596 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003597 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003598 if (sysfs_link_rdev(mddev, rdev))
3599 printk(KERN_WARNING "md: cannot register rd%d"
3600 " for %s after level change\n",
3601 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003602 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003603 }
3604
3605 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003606 mddev->pers = pers;
3607 mddev->private = priv;
3608 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3609 mddev->level = mddev->new_level;
3610 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003611 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003612 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003613 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003614 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003615 if (mddev->pers->sync_request == NULL) {
3616 /* this is now an array without redundancy, so
3617 * it must always be in_sync
3618 */
3619 mddev->in_sync = 1;
3620 del_timer_sync(&mddev->safemode_timer);
3621 }
NeilBrown245f46c2009-03-31 14:39:39 +11003622 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003623 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003624 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003625 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003626 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003627 return rv;
3628}
3629
3630static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003631__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003632
NeilBrownd4dbd022006-06-26 00:27:59 -07003633
3634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003635layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003636{
3637 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003638 if (mddev->reshape_position != MaxSector &&
3639 mddev->layout != mddev->new_layout)
3640 return sprintf(page, "%d (%d)\n",
3641 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003642 return sprintf(page, "%d\n", mddev->layout);
3643}
3644
3645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003646layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003647{
3648 char *e;
3649 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003650
3651 if (!*buf || (*e && *e != '\n'))
3652 return -EINVAL;
3653
NeilBrownb3546032009-03-31 14:56:41 +11003654 if (mddev->pers) {
3655 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003656 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003657 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003658 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003659 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003660 if (err) {
3661 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003662 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003663 }
NeilBrownb3546032009-03-31 14:56:41 +11003664 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003665 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003666 if (mddev->reshape_position == MaxSector)
3667 mddev->layout = n;
3668 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003669 return len;
3670}
3671static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003672__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003673
3674
NeilBrowneae17012005-11-08 21:39:23 -08003675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003676raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003677{
NeilBrownbb636542005-11-08 21:39:45 -08003678 if (mddev->raid_disks == 0)
3679 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003680 if (mddev->reshape_position != MaxSector &&
3681 mddev->delta_disks != 0)
3682 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3683 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003684 return sprintf(page, "%d\n", mddev->raid_disks);
3685}
3686
NeilBrownfd01b882011-10-11 16:47:53 +11003687static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003688
3689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003690raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003691{
NeilBrownda943b992006-01-06 00:20:54 -08003692 char *e;
3693 int rv = 0;
3694 unsigned long n = simple_strtoul(buf, &e, 10);
3695
3696 if (!*buf || (*e && *e != '\n'))
3697 return -EINVAL;
3698
3699 if (mddev->pers)
3700 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003701 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003702 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003703 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003704
3705 rdev_for_each(rdev, mddev) {
3706 if (olddisks < n &&
3707 rdev->data_offset < rdev->new_data_offset)
3708 return -EINVAL;
3709 if (olddisks > n &&
3710 rdev->data_offset > rdev->new_data_offset)
3711 return -EINVAL;
3712 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003713 mddev->delta_disks = n - olddisks;
3714 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003715 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003716 } else
NeilBrownda943b992006-01-06 00:20:54 -08003717 mddev->raid_disks = n;
3718 return rv ? rv : len;
3719}
3720static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003721__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003722
NeilBrown24dd4692005-11-08 21:39:26 -08003723static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003724chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003725{
NeilBrown08a02ec2007-05-09 02:35:38 -07003726 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003727 mddev->chunk_sectors != mddev->new_chunk_sectors)
3728 return sprintf(page, "%d (%d)\n",
3729 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003730 mddev->chunk_sectors << 9);
3731 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003732}
3733
3734static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003735chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003736{
NeilBrown3b343802006-01-06 00:20:47 -08003737 char *e;
3738 unsigned long n = simple_strtoul(buf, &e, 10);
3739
NeilBrown3b343802006-01-06 00:20:47 -08003740 if (!*buf || (*e && *e != '\n'))
3741 return -EINVAL;
3742
NeilBrownb3546032009-03-31 14:56:41 +11003743 if (mddev->pers) {
3744 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003745 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003746 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003747 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003748 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003749 if (err) {
3750 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003751 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003752 }
NeilBrownb3546032009-03-31 14:56:41 +11003753 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003754 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003755 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003756 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003757 }
NeilBrown3b343802006-01-06 00:20:47 -08003758 return len;
3759}
3760static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003761__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003762
NeilBrowna94213b2006-06-26 00:28:00 -07003763static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003764resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003765{
NeilBrownd1a7c502009-03-31 15:24:32 +11003766 if (mddev->recovery_cp == MaxSector)
3767 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003768 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3769}
3770
3771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003772resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003773{
NeilBrowna94213b2006-06-26 00:28:00 -07003774 char *e;
3775 unsigned long long n = simple_strtoull(buf, &e, 10);
3776
NeilBrownb0986362011-05-11 15:52:21 +10003777 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003778 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003779 if (cmd_match(buf, "none"))
3780 n = MaxSector;
3781 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003782 return -EINVAL;
3783
3784 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003785 if (mddev->pers)
3786 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003787 return len;
3788}
3789static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003790__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003791
NeilBrown9e653b62006-06-26 00:27:58 -07003792/*
3793 * The array state can be:
3794 *
3795 * clear
3796 * No devices, no size, no level
3797 * Equivalent to STOP_ARRAY ioctl
3798 * inactive
3799 * May have some settings, but array is not active
3800 * all IO results in error
3801 * When written, doesn't tear down array, but just stops it
3802 * suspended (not supported yet)
3803 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003804 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003805 * readonly
3806 * no resync can happen. no superblocks get written.
3807 * write requests fail
3808 * read-auto
3809 * like readonly, but behaves like 'clean' on a write request.
3810 *
3811 * clean - no pending writes, but otherwise active.
3812 * When written to inactive array, starts without resync
3813 * If a write request arrives then
3814 * if metadata is known, mark 'dirty' and switch to 'active'.
3815 * if not known, block and switch to write-pending
3816 * If written to an active array that has pending writes, then fails.
3817 * active
3818 * fully active: IO and resync can be happening.
3819 * When written to inactive array, starts with resync
3820 *
3821 * write-pending
3822 * clean, but writes are blocked waiting for 'active' to be written.
3823 *
3824 * active-idle
3825 * like active, but no writes have been seen for a while (100msec).
3826 *
3827 */
3828enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3829 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003830static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003831 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3832 "write-pending", "active-idle", NULL };
3833
3834static int match_word(const char *word, char **list)
3835{
3836 int n;
3837 for (n=0; list[n]; n++)
3838 if (cmd_match(word, list[n]))
3839 break;
3840 return n;
3841}
3842
3843static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003844array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003845{
3846 enum array_state st = inactive;
3847
3848 if (mddev->pers)
3849 switch(mddev->ro) {
3850 case 1:
3851 st = readonly;
3852 break;
3853 case 2:
3854 st = read_auto;
3855 break;
3856 case 0:
3857 if (mddev->in_sync)
3858 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003859 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003860 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003861 else if (mddev->safemode)
3862 st = active_idle;
3863 else
3864 st = active;
3865 }
3866 else {
3867 if (list_empty(&mddev->disks) &&
3868 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003869 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003870 st = clear;
3871 else
3872 st = inactive;
3873 }
3874 return sprintf(page, "%s\n", array_states[st]);
3875}
3876
NeilBrowna05b7ea02012-07-19 15:59:18 +10003877static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3878static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003879static int do_md_run(struct mddev * mddev);
3880static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003881
3882static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003883array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003884{
3885 int err = -EINVAL;
3886 enum array_state st = match_word(buf, array_states);
3887 switch(st) {
3888 case bad_word:
3889 break;
3890 case clear:
3891 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003892 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003893 break;
3894 case inactive:
3895 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003896 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003897 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003898 else
NeilBrowne6910632008-02-06 01:39:51 -08003899 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003900 break;
3901 case suspended:
3902 break; /* not supported yet */
3903 case readonly:
3904 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003905 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003906 else {
3907 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003908 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003909 err = do_md_run(mddev);
3910 }
3911 break;
3912 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003913 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003914 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003915 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003916 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003917 err = restart_array(mddev);
3918 if (err == 0) {
3919 mddev->ro = 2;
3920 set_disk_ro(mddev->gendisk, 0);
3921 }
NeilBrown9e653b62006-06-26 00:27:58 -07003922 } else {
3923 mddev->ro = 2;
3924 err = do_md_run(mddev);
3925 }
3926 break;
3927 case clean:
3928 if (mddev->pers) {
3929 restart_array(mddev);
3930 spin_lock_irq(&mddev->write_lock);
3931 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003932 if (mddev->in_sync == 0) {
3933 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003934 if (mddev->safemode == 1)
3935 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003936 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003937 }
3938 err = 0;
3939 } else
3940 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003941 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003942 } else
3943 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003944 break;
3945 case active:
3946 if (mddev->pers) {
3947 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003948 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003949 wake_up(&mddev->sb_wait);
3950 err = 0;
3951 } else {
3952 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003953 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003954 err = do_md_run(mddev);
3955 }
3956 break;
3957 case write_pending:
3958 case active_idle:
3959 /* these cannot be set */
3960 break;
3961 }
3962 if (err)
3963 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003964 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003965 if (mddev->hold_active == UNTIL_IOCTL)
3966 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003967 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003968 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003969 }
NeilBrown9e653b62006-06-26 00:27:58 -07003970}
NeilBrown80ca3a42006-07-10 04:44:18 -07003971static struct md_sysfs_entry md_array_state =
3972__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003973
NeilBrown6d7ff732006-01-06 00:21:16 -08003974static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003975max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003976 return sprintf(page, "%d\n",
3977 atomic_read(&mddev->max_corr_read_errors));
3978}
3979
3980static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003981max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003982{
3983 char *e;
3984 unsigned long n = simple_strtoul(buf, &e, 10);
3985
3986 if (*buf && (*e == 0 || *e == '\n')) {
3987 atomic_set(&mddev->max_corr_read_errors, n);
3988 return len;
3989 }
3990 return -EINVAL;
3991}
3992
3993static struct md_sysfs_entry max_corr_read_errors =
3994__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3995 max_corrected_read_errors_store);
3996
3997static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003998null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003999{
4000 return -EINVAL;
4001}
4002
4003static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004004new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004005{
4006 /* buf must be %d:%d\n? giving major and minor numbers */
4007 /* The new device is added to the array.
4008 * If the array has a persistent superblock, we read the
4009 * superblock to initialise info and check validity.
4010 * Otherwise, only checking done is that in bind_rdev_to_array,
4011 * which mainly checks size.
4012 */
4013 char *e;
4014 int major = simple_strtoul(buf, &e, 10);
4015 int minor;
4016 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004017 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004018 int err;
4019
4020 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4021 return -EINVAL;
4022 minor = simple_strtoul(e+1, &e, 10);
4023 if (*e && *e != '\n')
4024 return -EINVAL;
4025 dev = MKDEV(major, minor);
4026 if (major != MAJOR(dev) ||
4027 minor != MINOR(dev))
4028 return -EOVERFLOW;
4029
4030
4031 if (mddev->persistent) {
4032 rdev = md_import_device(dev, mddev->major_version,
4033 mddev->minor_version);
4034 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004035 struct md_rdev *rdev0
4036 = list_entry(mddev->disks.next,
4037 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004038 err = super_types[mddev->major_version]
4039 .load_super(rdev, rdev0, mddev->minor_version);
4040 if (err < 0)
4041 goto out;
4042 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004043 } else if (mddev->external)
4044 rdev = md_import_device(dev, -2, -1);
4045 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004046 rdev = md_import_device(dev, -1, -1);
4047
4048 if (IS_ERR(rdev))
4049 return PTR_ERR(rdev);
4050 err = bind_rdev_to_array(rdev, mddev);
4051 out:
4052 if (err)
4053 export_rdev(rdev);
4054 return err ? err : len;
4055}
4056
4057static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004058__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004059
4060static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004061bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004062{
4063 char *end;
4064 unsigned long chunk, end_chunk;
4065
4066 if (!mddev->bitmap)
4067 goto out;
4068 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4069 while (*buf) {
4070 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4071 if (buf == end) break;
4072 if (*end == '-') { /* range */
4073 buf = end + 1;
4074 end_chunk = simple_strtoul(buf, &end, 0);
4075 if (buf == end) break;
4076 }
4077 if (*end && !isspace(*end)) break;
4078 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004079 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004080 }
4081 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4082out:
4083 return len;
4084}
4085
4086static struct md_sysfs_entry md_bitmap =
4087__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4088
4089static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004090size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004091{
Andre Noll58c0fed2009-03-31 14:33:13 +11004092 return sprintf(page, "%llu\n",
4093 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004094}
4095
NeilBrownfd01b882011-10-11 16:47:53 +11004096static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004097
4098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004099size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004100{
4101 /* If array is inactive, we can reduce the component size, but
4102 * not increase it (except from 0).
4103 * If array is active, we can try an on-line resize
4104 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004105 sector_t sectors;
4106 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004107
Andre Noll58c0fed2009-03-31 14:33:13 +11004108 if (err < 0)
4109 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004110 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004111 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004112 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004113 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004114 if (mddev->dev_sectors == 0 ||
4115 mddev->dev_sectors > sectors)
4116 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004117 else
4118 err = -ENOSPC;
4119 }
4120 return err ? err : len;
4121}
4122
4123static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004124__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004125
NeilBrown8bb93aa2006-01-06 00:20:50 -08004126
4127/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004128 * This is one of
4129 * 'none' for arrays with no metadata (good luck...)
4130 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004131 * or N.M for internally known formats
4132 */
4133static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004134metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004135{
4136 if (mddev->persistent)
4137 return sprintf(page, "%d.%d\n",
4138 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004139 else if (mddev->external)
4140 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004141 else
4142 return sprintf(page, "none\n");
4143}
4144
4145static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004146metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004147{
4148 int major, minor;
4149 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004150 /* Changing the details of 'external' metadata is
4151 * always permitted. Otherwise there must be
4152 * no devices attached to the array.
4153 */
4154 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4155 ;
4156 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004157 return -EBUSY;
4158
4159 if (cmd_match(buf, "none")) {
4160 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004161 mddev->external = 0;
4162 mddev->major_version = 0;
4163 mddev->minor_version = 90;
4164 return len;
4165 }
4166 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004167 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004168 if (namelen >= sizeof(mddev->metadata_type))
4169 namelen = sizeof(mddev->metadata_type)-1;
4170 strncpy(mddev->metadata_type, buf+9, namelen);
4171 mddev->metadata_type[namelen] = 0;
4172 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4173 mddev->metadata_type[--namelen] = 0;
4174 mddev->persistent = 0;
4175 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004176 mddev->major_version = 0;
4177 mddev->minor_version = 90;
4178 return len;
4179 }
4180 major = simple_strtoul(buf, &e, 10);
4181 if (e==buf || *e != '.')
4182 return -EINVAL;
4183 buf = e+1;
4184 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004185 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004186 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004187 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004188 return -ENOENT;
4189 mddev->major_version = major;
4190 mddev->minor_version = minor;
4191 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004192 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004193 return len;
4194}
4195
4196static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004197__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004198
NeilBrowna35b0d62006-01-06 00:20:49 -08004199static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004200action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004201{
NeilBrown7eec3142005-11-08 21:39:44 -08004202 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004203 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4204 type = "frozen";
4205 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004206 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004207 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4208 type = "reshape";
4209 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004210 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4211 type = "resync";
4212 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4213 type = "check";
4214 else
4215 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004216 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004217 type = "recover";
4218 }
4219 return sprintf(page, "%s\n", type);
4220}
4221
NeilBrownfd01b882011-10-11 16:47:53 +11004222static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004223
NeilBrown24dd4692005-11-08 21:39:26 -08004224static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004225action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004226{
NeilBrown7eec3142005-11-08 21:39:44 -08004227 if (!mddev->pers || !mddev->pers->sync_request)
4228 return -EINVAL;
4229
NeilBrownb6a9ce62009-05-26 09:41:17 +10004230 if (cmd_match(page, "frozen"))
4231 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4232 else
4233 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4234
4235 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004236 if (mddev->sync_thread) {
4237 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004238 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004239 }
NeilBrown03c902e2006-01-06 00:20:46 -08004240 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4241 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004242 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004243 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004244 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004245 else if (cmd_match(page, "recover")) {
4246 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4247 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4248 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004249 int err;
4250 if (mddev->pers->start_reshape == NULL)
4251 return -EINVAL;
4252 err = mddev->pers->start_reshape(mddev);
4253 if (err)
4254 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004255 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004256 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004257 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004258 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004259 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004260 return -EINVAL;
4261 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4262 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004263 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004264 if (mddev->ro == 2) {
4265 /* A write to sync_action is enough to justify
4266 * canceling read-auto mode
4267 */
4268 mddev->ro = 0;
4269 md_wakeup_thread(mddev->sync_thread);
4270 }
NeilBrown03c902e2006-01-06 00:20:46 -08004271 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004272 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004273 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004274 return len;
4275}
4276
NeilBrown9d888832005-11-08 21:39:26 -08004277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004279{
4280 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004281 (unsigned long long)
4282 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004283}
4284
NeilBrown80ca3a42006-07-10 04:44:18 -07004285static struct md_sysfs_entry md_scan_mode =
4286__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004287
NeilBrown96de1e62005-11-08 21:39:39 -08004288
NeilBrown80ca3a42006-07-10 04:44:18 -07004289static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004290
NeilBrown88202a02006-01-06 00:21:36 -08004291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004292sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004293{
4294 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4295 mddev->sync_speed_min ? "local": "system");
4296}
4297
4298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004299sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004300{
4301 int min;
4302 char *e;
4303 if (strncmp(buf, "system", 6)==0) {
4304 mddev->sync_speed_min = 0;
4305 return len;
4306 }
4307 min = simple_strtoul(buf, &e, 10);
4308 if (buf == e || (*e && *e != '\n') || min <= 0)
4309 return -EINVAL;
4310 mddev->sync_speed_min = min;
4311 return len;
4312}
4313
4314static struct md_sysfs_entry md_sync_min =
4315__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4316
4317static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004318sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004319{
4320 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4321 mddev->sync_speed_max ? "local": "system");
4322}
4323
4324static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004325sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004326{
4327 int max;
4328 char *e;
4329 if (strncmp(buf, "system", 6)==0) {
4330 mddev->sync_speed_max = 0;
4331 return len;
4332 }
4333 max = simple_strtoul(buf, &e, 10);
4334 if (buf == e || (*e && *e != '\n') || max <= 0)
4335 return -EINVAL;
4336 mddev->sync_speed_max = max;
4337 return len;
4338}
4339
4340static struct md_sysfs_entry md_sync_max =
4341__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4342
Iustin Popd7f3d292007-10-16 23:30:54 -07004343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004345{
4346 return sprintf(page, "%d\n", mddev->degraded);
4347}
4348static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004349
4350static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004351sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004352{
4353 return sprintf(page, "%d\n", mddev->parallel_resync);
4354}
4355
4356static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004357sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004358{
4359 long n;
4360
4361 if (strict_strtol(buf, 10, &n))
4362 return -EINVAL;
4363
4364 if (n != 0 && n != 1)
4365 return -EINVAL;
4366
4367 mddev->parallel_resync = n;
4368
4369 if (mddev->sync_thread)
4370 wake_up(&resync_wait);
4371
4372 return len;
4373}
4374
4375/* force parallel resync, even with shared block devices */
4376static struct md_sysfs_entry md_sync_force_parallel =
4377__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4378 sync_force_parallel_show, sync_force_parallel_store);
4379
4380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004381sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004382{
4383 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004384 if (mddev->curr_resync == 0)
4385 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004386 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4387 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004388 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004389 db = resync - mddev->resync_mark_cnt;
4390 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004391}
4392
NeilBrown80ca3a42006-07-10 04:44:18 -07004393static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004394
4395static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004396sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004397{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004398 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004399
NeilBrownacb180b2009-04-14 16:28:34 +10004400 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4401 return sprintf(page, "none\n");
4402
NeilBrown72f36d52012-10-11 14:25:57 +11004403 if (mddev->curr_resync == 1 ||
4404 mddev->curr_resync == 2)
4405 return sprintf(page, "delayed\n");
4406
NeilBrownc804cde2012-05-21 09:28:33 +10004407 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4408 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004409 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004410 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004411 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004412
NeilBrownacb180b2009-04-14 16:28:34 +10004413 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004414 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004415}
4416
NeilBrown80ca3a42006-07-10 04:44:18 -07004417static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004418
NeilBrowne464eaf2006-03-27 01:18:14 -08004419static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004420min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004421{
4422 return sprintf(page, "%llu\n",
4423 (unsigned long long)mddev->resync_min);
4424}
4425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004426min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004427{
4428 unsigned long long min;
4429 if (strict_strtoull(buf, 10, &min))
4430 return -EINVAL;
4431 if (min > mddev->resync_max)
4432 return -EINVAL;
4433 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4434 return -EBUSY;
4435
4436 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004437 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004438 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004439 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004440 return -EINVAL;
4441 }
4442 mddev->resync_min = min;
4443
4444 return len;
4445}
4446
4447static struct md_sysfs_entry md_min_sync =
4448__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4449
4450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004451max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004452{
4453 if (mddev->resync_max == MaxSector)
4454 return sprintf(page, "max\n");
4455 else
4456 return sprintf(page, "%llu\n",
4457 (unsigned long long)mddev->resync_max);
4458}
4459static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004460max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004461{
4462 if (strncmp(buf, "max", 3) == 0)
4463 mddev->resync_max = MaxSector;
4464 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004465 unsigned long long max;
4466 if (strict_strtoull(buf, 10, &max))
4467 return -EINVAL;
4468 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004469 return -EINVAL;
4470 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004471 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004472 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4473 return -EBUSY;
4474
4475 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004476 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004477 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004478 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004479 return -EINVAL;
4480 }
4481 mddev->resync_max = max;
4482 }
4483 wake_up(&mddev->recovery_wait);
4484 return len;
4485}
4486
4487static struct md_sysfs_entry md_max_sync =
4488__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4489
4490static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004491suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004492{
4493 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4494}
4495
4496static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004497suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004498{
4499 char *e;
4500 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004501 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004502
NeilBrownb8d966e2009-07-01 11:14:04 +10004503 if (mddev->pers == NULL ||
4504 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004505 return -EINVAL;
4506 if (buf == e || (*e && *e != '\n'))
4507 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004508
4509 mddev->suspend_lo = new;
4510 if (new >= old)
4511 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004512 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004513 else {
4514 /* Expanding suspended region - need to wait */
4515 mddev->pers->quiesce(mddev, 1);
4516 mddev->pers->quiesce(mddev, 0);
4517 }
4518 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004519}
4520static struct md_sysfs_entry md_suspend_lo =
4521__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4522
4523
4524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004525suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004526{
4527 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4528}
4529
4530static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004531suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004532{
4533 char *e;
4534 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004535 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004536
NeilBrownb8d966e2009-07-01 11:14:04 +10004537 if (mddev->pers == NULL ||
4538 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004539 return -EINVAL;
4540 if (buf == e || (*e && *e != '\n'))
4541 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004542
4543 mddev->suspend_hi = new;
4544 if (new <= old)
4545 /* Shrinking suspended region */
4546 mddev->pers->quiesce(mddev, 2);
4547 else {
4548 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004549 mddev->pers->quiesce(mddev, 1);
4550 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004551 }
4552 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004553}
4554static struct md_sysfs_entry md_suspend_hi =
4555__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4556
NeilBrown08a02ec2007-05-09 02:35:38 -07004557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004559{
4560 if (mddev->reshape_position != MaxSector)
4561 return sprintf(page, "%llu\n",
4562 (unsigned long long)mddev->reshape_position);
4563 strcpy(page, "none\n");
4564 return 5;
4565}
4566
4567static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004568reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004569{
NeilBrownc6563a82012-05-21 09:27:00 +10004570 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004571 char *e;
4572 unsigned long long new = simple_strtoull(buf, &e, 10);
4573 if (mddev->pers)
4574 return -EBUSY;
4575 if (buf == e || (*e && *e != '\n'))
4576 return -EINVAL;
4577 mddev->reshape_position = new;
4578 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004579 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004580 mddev->new_level = mddev->level;
4581 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004582 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004583 rdev_for_each(rdev, mddev)
4584 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004585 return len;
4586}
4587
4588static struct md_sysfs_entry md_reshape_position =
4589__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4590 reshape_position_store);
4591
Dan Williamsb522adc2009-03-31 15:00:31 +11004592static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004593reshape_direction_show(struct mddev *mddev, char *page)
4594{
4595 return sprintf(page, "%s\n",
4596 mddev->reshape_backwards ? "backwards" : "forwards");
4597}
4598
4599static ssize_t
4600reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4601{
4602 int backwards = 0;
4603 if (cmd_match(buf, "forwards"))
4604 backwards = 0;
4605 else if (cmd_match(buf, "backwards"))
4606 backwards = 1;
4607 else
4608 return -EINVAL;
4609 if (mddev->reshape_backwards == backwards)
4610 return len;
4611
4612 /* check if we are allowed to change */
4613 if (mddev->delta_disks)
4614 return -EBUSY;
4615
4616 if (mddev->persistent &&
4617 mddev->major_version == 0)
4618 return -EINVAL;
4619
4620 mddev->reshape_backwards = backwards;
4621 return len;
4622}
4623
4624static struct md_sysfs_entry md_reshape_direction =
4625__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4626 reshape_direction_store);
4627
4628static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004629array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004630{
4631 if (mddev->external_size)
4632 return sprintf(page, "%llu\n",
4633 (unsigned long long)mddev->array_sectors/2);
4634 else
4635 return sprintf(page, "default\n");
4636}
4637
4638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004639array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004640{
4641 sector_t sectors;
4642
4643 if (strncmp(buf, "default", 7) == 0) {
4644 if (mddev->pers)
4645 sectors = mddev->pers->size(mddev, 0, 0);
4646 else
4647 sectors = mddev->array_sectors;
4648
4649 mddev->external_size = 0;
4650 } else {
4651 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4652 return -EINVAL;
4653 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004654 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004655
4656 mddev->external_size = 1;
4657 }
4658
4659 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004660 if (mddev->pers) {
4661 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004662 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004663 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004664 return len;
4665}
4666
4667static struct md_sysfs_entry md_array_size =
4668__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4669 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004670
NeilBrowneae17012005-11-08 21:39:23 -08004671static struct attribute *md_default_attrs[] = {
4672 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004673 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004674 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004675 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004676 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004677 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004678 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004679 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004680 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004681 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004682 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004683 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004684 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004685 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004686 NULL,
4687};
4688
4689static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004690 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004691 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004692 &md_sync_min.attr,
4693 &md_sync_max.attr,
4694 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004695 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004696 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004697 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004698 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004699 &md_suspend_lo.attr,
4700 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004701 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004702 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004703 NULL,
4704};
NeilBrown411036f2005-11-08 21:39:40 -08004705static struct attribute_group md_redundancy_group = {
4706 .name = NULL,
4707 .attrs = md_redundancy_attrs,
4708};
4709
NeilBrowneae17012005-11-08 21:39:23 -08004710
4711static ssize_t
4712md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4713{
4714 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004715 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004716 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004717
4718 if (!entry->show)
4719 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004720 spin_lock(&all_mddevs_lock);
4721 if (list_empty(&mddev->all_mddevs)) {
4722 spin_unlock(&all_mddevs_lock);
4723 return -EBUSY;
4724 }
4725 mddev_get(mddev);
4726 spin_unlock(&all_mddevs_lock);
4727
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004728 rv = mddev_lock(mddev);
4729 if (!rv) {
4730 rv = entry->show(mddev, page);
4731 mddev_unlock(mddev);
4732 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004733 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004734 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004735}
4736
4737static ssize_t
4738md_attr_store(struct kobject *kobj, struct attribute *attr,
4739 const char *page, size_t length)
4740{
4741 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004742 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004743 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004744
4745 if (!entry->store)
4746 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004747 if (!capable(CAP_SYS_ADMIN))
4748 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004749 spin_lock(&all_mddevs_lock);
4750 if (list_empty(&mddev->all_mddevs)) {
4751 spin_unlock(&all_mddevs_lock);
4752 return -EBUSY;
4753 }
4754 mddev_get(mddev);
4755 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004756 rv = mddev_lock(mddev);
4757 if (!rv) {
4758 rv = entry->store(mddev, page, length);
4759 mddev_unlock(mddev);
4760 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004761 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004762 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004763}
4764
4765static void md_free(struct kobject *ko)
4766{
NeilBrownfd01b882011-10-11 16:47:53 +11004767 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004768
4769 if (mddev->sysfs_state)
4770 sysfs_put(mddev->sysfs_state);
4771
4772 if (mddev->gendisk) {
4773 del_gendisk(mddev->gendisk);
4774 put_disk(mddev->gendisk);
4775 }
4776 if (mddev->queue)
4777 blk_cleanup_queue(mddev->queue);
4778
NeilBrowneae17012005-11-08 21:39:23 -08004779 kfree(mddev);
4780}
4781
Emese Revfy52cf25d2010-01-19 02:58:23 +01004782static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004783 .show = md_attr_show,
4784 .store = md_attr_store,
4785};
4786static struct kobj_type md_ktype = {
4787 .release = md_free,
4788 .sysfs_ops = &md_sysfs_ops,
4789 .default_attrs = md_default_attrs,
4790};
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792int mdp_major = 0;
4793
Dan Williams5fd3a172009-03-04 00:57:25 -07004794static void mddev_delayed_delete(struct work_struct *ws)
4795{
NeilBrownfd01b882011-10-11 16:47:53 +11004796 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004797
NeilBrown43a70502009-12-14 12:49:55 +11004798 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004799 kobject_del(&mddev->kobj);
4800 kobject_put(&mddev->kobj);
4801}
4802
NeilBrownefeb53c2009-01-09 08:31:10 +11004803static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004805 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004806 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004808 int partitioned;
4809 int shift;
4810 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004811 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004814 return -ENODEV;
4815
4816 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4817 shift = partitioned ? MdpMinorShift : 0;
4818 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Tejun Heoe804ac72010-10-15 15:36:08 +02004820 /* wait for any previous instance of this device to be
4821 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004822 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004823 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004824
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004825 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004826 error = -EEXIST;
4827 if (mddev->gendisk)
4828 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004829
4830 if (name) {
4831 /* Need to ensure that 'name' is not a duplicate.
4832 */
NeilBrownfd01b882011-10-11 16:47:53 +11004833 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004834 spin_lock(&all_mddevs_lock);
4835
4836 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4837 if (mddev2->gendisk &&
4838 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4839 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004840 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004841 }
4842 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
NeilBrown8b765392009-01-09 08:31:08 +11004844
NeilBrown0909dc42009-07-01 12:27:21 +10004845 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004846 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004847 if (!mddev->queue)
4848 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004849 mddev->queue->queuedata = mddev;
4850
NeilBrown409c57f2009-03-31 14:39:39 +11004851 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004852 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 disk = alloc_disk(1 << shift);
4855 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004856 blk_cleanup_queue(mddev->queue);
4857 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004858 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004860 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004862 if (name)
4863 strcpy(disk->disk_name, name);
4864 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 disk->fops = &md_fops;
4869 disk->private_data = mddev;
4870 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004871 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004872 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004873 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004874 * remove it now.
4875 */
4876 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004878 /* As soon as we call add_disk(), another thread could get
4879 * through to md_open, so make sure it doesn't get too far
4880 */
4881 mutex_lock(&mddev->open_mutex);
4882 add_disk(disk);
4883
Tejun Heoed9e1982008-08-25 19:56:05 +09004884 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4885 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004886 if (error) {
4887 /* This isn't possible, but as kobject_init_and_add is marked
4888 * __must_check, we must do something with the result
4889 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004890 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4891 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004892 error = 0;
4893 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004894 if (mddev->kobj.sd &&
4895 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004896 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004897 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004898 abort:
4899 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004900 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004901 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004902 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004903 }
NeilBrownd3374822009-01-09 08:31:10 +11004904 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004905 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004906}
4907
4908static struct kobject *md_probe(dev_t dev, int *part, void *data)
4909{
4910 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 return NULL;
4912}
4913
NeilBrownefeb53c2009-01-09 08:31:10 +11004914static int add_named_array(const char *val, struct kernel_param *kp)
4915{
4916 /* val must be "md_*" where * is not all digits.
4917 * We allocate an array with a large free minor number, and
4918 * set the name to val. val must not already be an active name.
4919 */
4920 int len = strlen(val);
4921 char buf[DISK_NAME_LEN];
4922
4923 while (len && val[len-1] == '\n')
4924 len--;
4925 if (len >= DISK_NAME_LEN)
4926 return -E2BIG;
4927 strlcpy(buf, val, len+1);
4928 if (strncmp(buf, "md_", 3) != 0)
4929 return -EINVAL;
4930 return md_alloc(0, buf);
4931}
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933static void md_safemode_timeout(unsigned long data)
4934{
NeilBrownfd01b882011-10-11 16:47:53 +11004935 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Neil Brown0fd62b82008-06-28 08:31:36 +10004937 if (!atomic_read(&mddev->writes_pending)) {
4938 mddev->safemode = 1;
4939 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004940 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 md_wakeup_thread(mddev->thread);
4943}
4944
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004945static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
NeilBrownfd01b882011-10-11 16:47:53 +11004947int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
NeilBrown2604b702006-01-06 00:20:36 -08004949 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004950 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004951 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
NeilBrowna757e642005-04-16 15:26:42 -07004953 if (list_empty(&mddev->disks))
4954 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 if (mddev->pers)
4958 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004959 /* Cannot run until previous stop completes properly */
4960 if (mddev->sysfs_active)
4961 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 /*
4964 * Analyze all RAID superblock(s)
4965 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004966 if (!mddev->raid_disks) {
4967 if (!mddev->persistent)
4968 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004969 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
NeilBrownd9d166c2006-01-06 00:20:51 -08004972 if (mddev->level != LEVEL_NONE)
4973 request_module("md-level-%d", mddev->level);
4974 else if (mddev->clevel[0])
4975 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 /*
4978 * Drop all container device buffers, from now on
4979 * the only valid external interface is through the md
4980 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004982 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004983 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 continue;
4985 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004986 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004987
4988 /* perform some consistency tests on the device.
4989 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004990 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004991 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004992 if (rdev->meta_bdev) {
4993 /* Nothing to check */;
4994 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004995 if (mddev->dev_sectors &&
4996 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004997 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004998 printk("md: %s: data overlaps metadata\n",
4999 mdname(mddev));
5000 return -EINVAL;
5001 }
5002 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005003 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005004 > rdev->data_offset) {
5005 printk("md: %s: metadata overlaps data\n",
5006 mdname(mddev));
5007 return -EINVAL;
5008 }
5009 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005010 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
5012
NeilBrowna167f662010-10-26 18:31:13 +11005013 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005014 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005017 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005018 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005020 if (mddev->level != LEVEL_NONE)
5021 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5022 mddev->level);
5023 else
5024 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5025 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 return -EINVAL;
5027 }
NeilBrown2604b702006-01-06 00:20:36 -08005028 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005030 if (mddev->level != pers->level) {
5031 mddev->level = pers->level;
5032 mddev->new_level = pers->level;
5033 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005034 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
NeilBrownf6705572006-03-27 01:18:11 -08005036 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005037 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005038 /* This personality cannot handle reshaping... */
5039 mddev->pers = NULL;
5040 module_put(pers->owner);
5041 return -EINVAL;
5042 }
5043
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005044 if (pers->sync_request) {
5045 /* Warn if this is a potentially silly
5046 * configuration.
5047 */
5048 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005049 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005050 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005051
NeilBrowndafb20f2012-03-19 12:46:39 +11005052 rdev_for_each(rdev, mddev)
5053 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005054 if (rdev < rdev2 &&
5055 rdev->bdev->bd_contains ==
5056 rdev2->bdev->bd_contains) {
5057 printk(KERN_WARNING
5058 "%s: WARNING: %s appears to be"
5059 " on the same physical disk as"
5060 " %s.\n",
5061 mdname(mddev),
5062 bdevname(rdev->bdev,b),
5063 bdevname(rdev2->bdev,b2));
5064 warned = 1;
5065 }
5066 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005067
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005068 if (warned)
5069 printk(KERN_WARNING
5070 "True protection against single-disk"
5071 " failure might be compromised.\n");
5072 }
5073
NeilBrown657390d2005-08-26 18:34:16 -07005074 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005075 /* may be over-ridden by personality */
5076 mddev->resync_max_sectors = mddev->dev_sectors;
5077
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005078 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
NeilBrown0f9552b2009-12-30 12:08:50 +11005080 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005081 mddev->ro = 2; /* read-only, but switch on first write */
5082
NeilBrownb15c2e52006-01-06 00:20:16 -08005083 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005084 if (err)
5085 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005086 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5087 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5088 " but 'external_size' not in effect?\n", __func__);
5089 printk(KERN_ERR
5090 "md: invalid array_size %llu > default size %llu\n",
5091 (unsigned long long)mddev->array_sectors / 2,
5092 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5093 err = -EINVAL;
5094 mddev->pers->stop(mddev);
5095 }
NeilBrownef99bf42012-05-22 13:55:08 +10005096 if (err == 0 && mddev->pers->sync_request &&
5097 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005098 err = bitmap_create(mddev);
5099 if (err) {
5100 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5101 mdname(mddev), err);
5102 mddev->pers->stop(mddev);
5103 }
5104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 module_put(mddev->pers->owner);
5107 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005108 bitmap_destroy(mddev);
5109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005111 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005112 if (mddev->kobj.sd &&
5113 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005114 printk(KERN_WARNING
5115 "md: cannot register extra attributes for %s\n",
5116 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005117 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005118 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005119 mddev->ro = 0;
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005122 atomic_set(&mddev->max_corr_read_errors,
5123 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mddev->safemode = 0;
5125 mddev->safemode_timer.function = md_safemode_timeout;
5126 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005127 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005129 smp_wmb();
5130 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005131 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005132 if (rdev->raid_disk >= 0)
5133 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005134 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5137
NeilBrown850b2b42006-10-03 01:15:46 -07005138 if (mddev->flags)
5139 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
NeilBrownd7603b72006-01-06 00:20:30 -08005141 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005142 sysfs_notify_dirent_safe(mddev->sysfs_state);
5143 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005144 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 return 0;
5146}
NeilBrown390ee602010-06-01 19:37:27 +10005147EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrownfd01b882011-10-11 16:47:53 +11005149static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005150{
5151 int err;
5152
5153 err = md_run(mddev);
5154 if (err)
5155 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005156 err = bitmap_load(mddev);
5157 if (err) {
5158 bitmap_destroy(mddev);
5159 goto out;
5160 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005161
5162 md_wakeup_thread(mddev->thread);
5163 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5164
NeilBrownfe60b012010-03-29 11:10:42 +11005165 set_capacity(mddev->gendisk, mddev->array_sectors);
5166 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005167 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005168 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5169out:
5170 return err;
5171}
5172
NeilBrownfd01b882011-10-11 16:47:53 +11005173static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
5175 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Andre Noll80fab1d2008-07-11 22:02:21 +10005177 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005179 return -ENXIO;
5180 if (!mddev->pers)
5181 return -EINVAL;
5182 if (!mddev->ro)
5183 return -EBUSY;
5184 mddev->safemode = 0;
5185 mddev->ro = 0;
5186 set_disk_ro(disk, 0);
5187 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5188 mdname(mddev));
5189 /* Kick recovery or resync if necessary */
5190 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5191 md_wakeup_thread(mddev->thread);
5192 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005193 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195}
5196
NeilBrownacc55e22006-06-26 00:27:47 -07005197/* similar to deny_write_access, but accounts for our holding a reference
5198 * to the file ourselves */
5199static int deny_bitmap_write_access(struct file * file)
5200{
5201 struct inode *inode = file->f_mapping->host;
5202
5203 spin_lock(&inode->i_lock);
5204 if (atomic_read(&inode->i_writecount) > 1) {
5205 spin_unlock(&inode->i_lock);
5206 return -ETXTBSY;
5207 }
5208 atomic_set(&inode->i_writecount, -1);
5209 spin_unlock(&inode->i_lock);
5210
5211 return 0;
5212}
5213
NeilBrown43a70502009-12-14 12:49:55 +11005214void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005215{
5216 struct inode *inode = file->f_mapping->host;
5217
5218 spin_lock(&inode->i_lock);
5219 atomic_set(&inode->i_writecount, 1);
5220 spin_unlock(&inode->i_lock);
5221}
5222
NeilBrownfd01b882011-10-11 16:47:53 +11005223static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005224{
5225 mddev->array_sectors = 0;
5226 mddev->external_size = 0;
5227 mddev->dev_sectors = 0;
5228 mddev->raid_disks = 0;
5229 mddev->recovery_cp = 0;
5230 mddev->resync_min = 0;
5231 mddev->resync_max = MaxSector;
5232 mddev->reshape_position = MaxSector;
5233 mddev->external = 0;
5234 mddev->persistent = 0;
5235 mddev->level = LEVEL_NONE;
5236 mddev->clevel[0] = 0;
5237 mddev->flags = 0;
5238 mddev->ro = 0;
5239 mddev->metadata_type[0] = 0;
5240 mddev->chunk_sectors = 0;
5241 mddev->ctime = mddev->utime = 0;
5242 mddev->layout = 0;
5243 mddev->max_disks = 0;
5244 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005245 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005246 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005247 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005248 mddev->new_level = LEVEL_NONE;
5249 mddev->new_layout = 0;
5250 mddev->new_chunk_sectors = 0;
5251 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005252 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005253 mddev->suspend_lo = mddev->suspend_hi = 0;
5254 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5255 mddev->recovery = 0;
5256 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005257 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005258 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005259 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005260 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->bitmap_info.offset = 0;
5262 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005263 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005264 mddev->bitmap_info.chunksize = 0;
5265 mddev->bitmap_info.daemon_sleep = 0;
5266 mddev->bitmap_info.max_write_behind = 0;
5267}
5268
NeilBrownfd01b882011-10-11 16:47:53 +11005269static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005270{
5271 if (mddev->sync_thread) {
5272 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5273 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005274 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005275 }
5276
5277 del_timer_sync(&mddev->safemode_timer);
5278
5279 bitmap_flush(mddev);
5280 md_super_wait(mddev);
5281
5282 if (!mddev->in_sync || mddev->flags) {
5283 /* mark array as shutdown cleanly */
5284 mddev->in_sync = 1;
5285 md_update_sb(mddev, 1);
5286 }
5287}
NeilBrowndefad612011-01-14 09:14:33 +11005288
NeilBrownfd01b882011-10-11 16:47:53 +11005289void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005290{
5291 mddev_lock(mddev);
5292 __md_stop_writes(mddev);
5293 mddev_unlock(mddev);
5294}
NeilBrown390ee602010-06-01 19:37:27 +10005295EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005296
NeilBrownfd01b882011-10-11 16:47:53 +11005297void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005298{
NeilBrown0ca69882011-01-14 09:14:33 +11005299 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005300 mddev->pers->stop(mddev);
5301 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5302 mddev->to_remove = &md_redundancy_group;
5303 module_put(mddev->pers->owner);
5304 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005305 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005306}
NeilBrown390ee602010-06-01 19:37:27 +10005307EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005308
NeilBrowna05b7ea02012-07-19 15:59:18 +10005309static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005310{
5311 int err = 0;
5312 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005313 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005314 printk("md: %s still in use.\n",mdname(mddev));
5315 err = -EBUSY;
5316 goto out;
5317 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005318 if (bdev)
5319 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005320 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005321 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005322
5323 err = -ENXIO;
5324 if (mddev->ro==1)
5325 goto out;
5326 mddev->ro = 1;
5327 set_disk_ro(mddev->gendisk, 1);
5328 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005329 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005330 err = 0;
5331 }
5332out:
5333 mutex_unlock(&mddev->open_mutex);
5334 return err;
5335}
5336
NeilBrown9e653b62006-06-26 00:27:58 -07005337/* mode:
5338 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005339 * 2 - stop but do not disassemble array
5340 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005341static int do_md_stop(struct mddev * mddev, int mode,
5342 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005345 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrownc8c00a62009-08-10 12:50:52 +10005347 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005348 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005349 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005350 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005351 mutex_unlock(&mddev->open_mutex);
5352 return -EBUSY;
5353 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005354 if (bdev)
5355 /* It is possible IO was issued on some other
5356 * open file which was closed before we took ->open_mutex.
5357 * As that was not the last close __blkdev_put will not
5358 * have called sync_blockdev, so we must.
5359 */
5360 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
NeilBrown6e17b022010-08-07 21:41:19 +10005362 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005363 if (mddev->ro)
5364 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005365
NeilBrowndefad612011-01-14 09:14:33 +11005366 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005367 md_stop(mddev);
5368 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005369 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005370
NeilBrowna4bd82d2010-03-29 13:23:10 +11005371 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005372 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005373
NeilBrowndafb20f2012-03-19 12:46:39 +11005374 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005375 if (rdev->raid_disk >= 0)
5376 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005377
NeilBrowna4bd82d2010-03-29 13:23:10 +11005378 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005379 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005380 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005381 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005382
NeilBrowna4bd82d2010-03-29 13:23:10 +11005383 if (mddev->ro)
5384 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005385 } else
5386 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 /*
5388 * Free resources if final stop
5389 */
NeilBrown9e653b62006-06-26 00:27:58 -07005390 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5392
NeilBrown978f9462006-02-02 14:28:05 -08005393 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005394 if (mddev->bitmap_info.file) {
5395 restore_bitmap_write_access(mddev->bitmap_info.file);
5396 fput(mddev->bitmap_info.file);
5397 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005398 }
NeilBrownc3d97142009-12-14 12:49:52 +11005399 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 export_array(mddev);
5402
NeilBrown6177b472010-03-29 11:37:13 +11005403 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005404 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005405 if (mddev->hold_active == UNTIL_STOP)
5406 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005407 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005408 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005409 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005410 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005414#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005415static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
NeilBrown3cb03002011-10-11 16:45:26 +11005417 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 int err;
5419
NeilBrowna757e642005-04-16 15:26:42 -07005420 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 printk(KERN_INFO "md: running: ");
5424
NeilBrowndafb20f2012-03-19 12:46:39 +11005425 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 char b[BDEVNAME_SIZE];
5427 printk("<%s>", bdevname(rdev->bdev,b));
5428 }
5429 printk("\n");
5430
NeilBrownd710e132008-10-13 11:55:12 +11005431 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 if (err) {
5433 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005434 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 }
5436}
5437
5438/*
5439 * lets try to run arrays based on all disks that have arrived
5440 * until now. (those are in pending_raid_disks)
5441 *
5442 * the method: pick the first pending disk, collect all disks with
5443 * the same UUID, remove all from the pending list and put them into
5444 * the 'same_array' list. Then order this list based on superblock
5445 * update time (freshest comes first), kick out 'old' disks and
5446 * compare superblocks. If everything's fine then run it.
5447 *
5448 * If "unit" is allocated, then bump its reference count
5449 */
5450static void autorun_devices(int part)
5451{
NeilBrown3cb03002011-10-11 16:45:26 +11005452 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005453 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 char b[BDEVNAME_SIZE];
5455
5456 printk(KERN_INFO "md: autorun ...\n");
5457 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005458 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005460 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005462 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
5464 printk(KERN_INFO "md: considering %s ...\n",
5465 bdevname(rdev0->bdev,b));
5466 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005467 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 if (super_90_load(rdev, rdev0, 0) >= 0) {
5469 printk(KERN_INFO "md: adding %s ...\n",
5470 bdevname(rdev->bdev,b));
5471 list_move(&rdev->same_set, &candidates);
5472 }
5473 /*
5474 * now we have a set of devices, with all of them having
5475 * mostly sane superblocks. It's time to allocate the
5476 * mddev.
5477 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005478 if (part) {
5479 dev = MKDEV(mdp_major,
5480 rdev0->preferred_minor << MdpMinorShift);
5481 unit = MINOR(dev) >> MdpMinorShift;
5482 } else {
5483 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5484 unit = MINOR(dev);
5485 }
5486 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5488 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5489 break;
5490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
5492 md_probe(dev, NULL, NULL);
5493 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005494 if (!mddev || !mddev->gendisk) {
5495 if (mddev)
5496 mddev_put(mddev);
5497 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 "md: cannot allocate memory for md drive.\n");
5499 break;
5500 }
5501 if (mddev_lock(mddev))
5502 printk(KERN_WARNING "md: %s locked, cannot run\n",
5503 mdname(mddev));
5504 else if (mddev->raid_disks || mddev->major_version
5505 || !list_empty(&mddev->disks)) {
5506 printk(KERN_WARNING
5507 "md: %s already running, cannot run %s\n",
5508 mdname(mddev), bdevname(rdev0->bdev,b));
5509 mddev_unlock(mddev);
5510 } else {
5511 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005512 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005513 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 list_del_init(&rdev->same_set);
5515 if (bind_rdev_to_array(rdev, mddev))
5516 export_rdev(rdev);
5517 }
5518 autorun_array(mddev);
5519 mddev_unlock(mddev);
5520 }
5521 /* on success, candidates will be empty, on error
5522 * it won't...
5523 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005524 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005525 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 mddev_put(mddev);
5529 }
5530 printk(KERN_INFO "md: ... autorun DONE.\n");
5531}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005532#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534static int get_version(void __user * arg)
5535{
5536 mdu_version_t ver;
5537
5538 ver.major = MD_MAJOR_VERSION;
5539 ver.minor = MD_MINOR_VERSION;
5540 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5541
5542 if (copy_to_user(arg, &ver, sizeof(ver)))
5543 return -EFAULT;
5544
5545 return 0;
5546}
5547
NeilBrownfd01b882011-10-11 16:47:53 +11005548static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
5550 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005551 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005552 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
NeilBrown1ca69c42012-10-11 13:37:33 +11005554 nr = working = insync = failed = spare = 0;
5555 rcu_read_lock();
5556 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005558 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 failed++;
5560 else {
5561 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005562 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005563 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 else
5565 spare++;
5566 }
5567 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005568 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
5570 info.major_version = mddev->major_version;
5571 info.minor_version = mddev->minor_version;
5572 info.patch_version = MD_PATCHLEVEL_VERSION;
5573 info.ctime = mddev->ctime;
5574 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005575 info.size = mddev->dev_sectors / 2;
5576 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005577 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 info.nr_disks = nr;
5579 info.raid_disks = mddev->raid_disks;
5580 info.md_minor = mddev->md_minor;
5581 info.not_persistent= !mddev->persistent;
5582
5583 info.utime = mddev->utime;
5584 info.state = 0;
5585 if (mddev->in_sync)
5586 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005587 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005588 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005589 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 info.working_disks = working;
5591 info.failed_disks = failed;
5592 info.spare_disks = spare;
5593
5594 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005595 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
5597 if (copy_to_user(arg, &info, sizeof(info)))
5598 return -EFAULT;
5599
5600 return 0;
5601}
5602
NeilBrownfd01b882011-10-11 16:47:53 +11005603static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005604{
5605 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5606 char *ptr, *buf = NULL;
5607 int err = -ENOMEM;
5608
Dan Williamsb5470dc2008-06-27 21:44:04 -07005609 if (md_allow_write(mddev))
5610 file = kmalloc(sizeof(*file), GFP_NOIO);
5611 else
5612 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005613
NeilBrown32a76272005-06-21 17:17:14 -07005614 if (!file)
5615 goto out;
5616
5617 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005618 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005619 file->pathname[0] = '\0';
5620 goto copy_out;
5621 }
5622
5623 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5624 if (!buf)
5625 goto out;
5626
NeilBrown1ec885c2012-05-22 13:55:10 +10005627 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5628 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005629 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005630 goto out;
5631
5632 strcpy(file->pathname, ptr);
5633
5634copy_out:
5635 err = 0;
5636 if (copy_to_user(arg, file, sizeof(*file)))
5637 err = -EFAULT;
5638out:
5639 kfree(buf);
5640 kfree(file);
5641 return err;
5642}
5643
NeilBrownfd01b882011-10-11 16:47:53 +11005644static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
5646 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005647 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
5649 if (copy_from_user(&info, arg, sizeof(info)))
5650 return -EFAULT;
5651
NeilBrown1ca69c42012-10-11 13:37:33 +11005652 rcu_read_lock();
5653 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (rdev) {
5655 info.major = MAJOR(rdev->bdev->bd_dev);
5656 info.minor = MINOR(rdev->bdev->bd_dev);
5657 info.raid_disk = rdev->raid_disk;
5658 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005659 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005661 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 info.state |= (1<<MD_DISK_ACTIVE);
5663 info.state |= (1<<MD_DISK_SYNC);
5664 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005665 if (test_bit(WriteMostly, &rdev->flags))
5666 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 } else {
5668 info.major = info.minor = 0;
5669 info.raid_disk = -1;
5670 info.state = (1<<MD_DISK_REMOVED);
5671 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005672 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 if (copy_to_user(arg, &info, sizeof(info)))
5675 return -EFAULT;
5676
5677 return 0;
5678}
5679
NeilBrownfd01b882011-10-11 16:47:53 +11005680static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
5682 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005683 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 dev_t dev = MKDEV(info->major,info->minor);
5685
5686 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5687 return -EOVERFLOW;
5688
5689 if (!mddev->raid_disks) {
5690 int err;
5691 /* expecting a device which has a superblock */
5692 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5693 if (IS_ERR(rdev)) {
5694 printk(KERN_WARNING
5695 "md: md_import_device returned %ld\n",
5696 PTR_ERR(rdev));
5697 return PTR_ERR(rdev);
5698 }
5699 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005700 struct md_rdev *rdev0
5701 = list_entry(mddev->disks.next,
5702 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005703 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 .load_super(rdev, rdev0, mddev->minor_version);
5705 if (err < 0) {
5706 printk(KERN_WARNING
5707 "md: %s has different UUID to %s\n",
5708 bdevname(rdev->bdev,b),
5709 bdevname(rdev0->bdev,b2));
5710 export_rdev(rdev);
5711 return -EINVAL;
5712 }
5713 }
5714 err = bind_rdev_to_array(rdev, mddev);
5715 if (err)
5716 export_rdev(rdev);
5717 return err;
5718 }
5719
5720 /*
5721 * add_new_disk can be used once the array is assembled
5722 * to add "hot spares". They must already have a superblock
5723 * written
5724 */
5725 if (mddev->pers) {
5726 int err;
5727 if (!mddev->pers->hot_add_disk) {
5728 printk(KERN_WARNING
5729 "%s: personality does not support diskops!\n",
5730 mdname(mddev));
5731 return -EINVAL;
5732 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005733 if (mddev->persistent)
5734 rdev = md_import_device(dev, mddev->major_version,
5735 mddev->minor_version);
5736 else
5737 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 if (IS_ERR(rdev)) {
5739 printk(KERN_WARNING
5740 "md: md_import_device returned %ld\n",
5741 PTR_ERR(rdev));
5742 return PTR_ERR(rdev);
5743 }
NeilBrown1a855a02010-12-09 16:36:28 +11005744 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005745 if (!mddev->persistent) {
5746 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005747 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005748 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005749 set_bit(In_sync, &rdev->flags);
5750 } else
NeilBrown41158c72005-06-21 17:17:25 -07005751 rdev->raid_disk = -1;
5752 } else
5753 super_types[mddev->major_version].
5754 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005755 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005756 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005757 /* This was a hot-add request, but events doesn't
5758 * match, so reject it.
5759 */
5760 export_rdev(rdev);
5761 return -EINVAL;
5762 }
5763
NeilBrown1a855a02010-12-09 16:36:28 +11005764 if (test_bit(In_sync, &rdev->flags))
5765 rdev->saved_raid_disk = rdev->raid_disk;
5766 else
5767 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005768
NeilBrownb2d444d2005-11-08 21:39:31 -08005769 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005770 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5771 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005772 else
5773 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 rdev->raid_disk = -1;
5776 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005777 if (!err && !mddev->pers->hot_remove_disk) {
5778 /* If there is hot_add_disk but no hot_remove_disk
5779 * then added disks for geometry changes,
5780 * and should be added immediately.
5781 */
5782 super_types[mddev->major_version].
5783 validate_super(mddev, rdev);
5784 err = mddev->pers->hot_add_disk(mddev, rdev);
5785 if (err)
5786 unbind_rdev_from_array(rdev);
5787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 if (err)
5789 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005790 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005791 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005792
NeilBrown17571282006-12-10 02:20:52 -08005793 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005794 if (mddev->degraded)
5795 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005796 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005797 if (!err)
5798 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005799 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 return err;
5801 }
5802
5803 /* otherwise, add_new_disk is only allowed
5804 * for major_version==0 superblocks
5805 */
5806 if (mddev->major_version != 0) {
5807 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5808 mdname(mddev));
5809 return -EINVAL;
5810 }
5811
5812 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5813 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005814 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 if (IS_ERR(rdev)) {
5816 printk(KERN_WARNING
5817 "md: error, md_import_device() returned %ld\n",
5818 PTR_ERR(rdev));
5819 return PTR_ERR(rdev);
5820 }
5821 rdev->desc_nr = info->number;
5822 if (info->raid_disk < mddev->raid_disks)
5823 rdev->raid_disk = info->raid_disk;
5824 else
5825 rdev->raid_disk = -1;
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005828 if (info->state & (1<<MD_DISK_SYNC))
5829 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005831 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5832 set_bit(WriteMostly, &rdev->flags);
5833
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 if (!mddev->persistent) {
5835 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005836 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5837 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005838 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005839 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
NeilBrown2bf071b2006-01-06 00:20:55 -08005841 err = bind_rdev_to_array(rdev, mddev);
5842 if (err) {
5843 export_rdev(rdev);
5844 return err;
5845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
5847
5848 return 0;
5849}
5850
NeilBrownfd01b882011-10-11 16:47:53 +11005851static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852{
5853 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005854 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 rdev = find_rdev(mddev, dev);
5857 if (!rdev)
5858 return -ENXIO;
5859
5860 if (rdev->raid_disk >= 0)
5861 goto busy;
5862
5863 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005864 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005865 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 return 0;
5868busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005869 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 bdevname(rdev->bdev,b), mdname(mddev));
5871 return -EBUSY;
5872}
5873
NeilBrownfd01b882011-10-11 16:47:53 +11005874static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875{
5876 char b[BDEVNAME_SIZE];
5877 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005878 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
5880 if (!mddev->pers)
5881 return -ENODEV;
5882
5883 if (mddev->major_version != 0) {
5884 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5885 " version-0 superblocks.\n",
5886 mdname(mddev));
5887 return -EINVAL;
5888 }
5889 if (!mddev->pers->hot_add_disk) {
5890 printk(KERN_WARNING
5891 "%s: personality does not support diskops!\n",
5892 mdname(mddev));
5893 return -EINVAL;
5894 }
5895
NeilBrownd710e132008-10-13 11:55:12 +11005896 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 if (IS_ERR(rdev)) {
5898 printk(KERN_WARNING
5899 "md: error, md_import_device() returned %ld\n",
5900 PTR_ERR(rdev));
5901 return -EINVAL;
5902 }
5903
5904 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005905 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005907 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
NeilBrown8190e752009-06-18 08:48:58 +10005909 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
NeilBrownb2d444d2005-11-08 21:39:31 -08005911 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 printk(KERN_WARNING
5913 "md: can not hot-add faulty %s disk to %s!\n",
5914 bdevname(rdev->bdev,b), mdname(mddev));
5915 err = -EINVAL;
5916 goto abort_export;
5917 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005918 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005920 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005921 err = bind_rdev_to_array(rdev, mddev);
5922 if (err)
5923 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
5925 /*
5926 * The rest should better be atomic, we can have disk failures
5927 * noticed in interrupt contexts ...
5928 */
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 rdev->raid_disk = -1;
5931
NeilBrown850b2b42006-10-03 01:15:46 -07005932 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 /*
5935 * Kick recovery, maybe this spare has to be added to the
5936 * array immediately.
5937 */
5938 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5939 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005940 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 return 0;
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943abort_export:
5944 export_rdev(rdev);
5945 return err;
5946}
5947
NeilBrownfd01b882011-10-11 16:47:53 +11005948static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005949{
5950 int err;
5951
NeilBrown36fa3062005-09-09 16:23:45 -07005952 if (mddev->pers) {
5953 if (!mddev->pers->quiesce)
5954 return -EBUSY;
5955 if (mddev->recovery || mddev->sync_thread)
5956 return -EBUSY;
5957 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005958 }
5959
NeilBrown36fa3062005-09-09 16:23:45 -07005960
5961 if (fd >= 0) {
5962 if (mddev->bitmap)
5963 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005964 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005965
NeilBrownc3d97142009-12-14 12:49:52 +11005966 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005967 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5968 mdname(mddev));
5969 return -EBADF;
5970 }
5971
NeilBrownc3d97142009-12-14 12:49:52 +11005972 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005973 if (err) {
5974 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5975 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005976 fput(mddev->bitmap_info.file);
5977 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005978 return err;
5979 }
NeilBrownc3d97142009-12-14 12:49:52 +11005980 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005981 } else if (mddev->bitmap == NULL)
5982 return -ENOENT; /* cannot remove what isn't there */
5983 err = 0;
5984 if (mddev->pers) {
5985 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005986 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005987 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005988 if (!err)
5989 err = bitmap_load(mddev);
5990 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005991 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005992 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005993 fd = -1; /* make sure to put the file */
5994 }
NeilBrown36fa3062005-09-09 16:23:45 -07005995 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005996 }
5997 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005998 if (mddev->bitmap_info.file) {
5999 restore_bitmap_write_access(mddev->bitmap_info.file);
6000 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006001 }
NeilBrownc3d97142009-12-14 12:49:52 +11006002 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006003 }
6004
NeilBrown32a76272005-06-21 17:17:14 -07006005 return err;
6006}
6007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008/*
6009 * set_array_info is used two different ways
6010 * The original usage is when creating a new array.
6011 * In this usage, raid_disks is > 0 and it together with
6012 * level, size, not_persistent,layout,chunksize determine the
6013 * shape of the array.
6014 * This will always create an array with a type-0.90.0 superblock.
6015 * The newer usage is when assembling an array.
6016 * In this case raid_disks will be 0, and the major_version field is
6017 * use to determine which style super-blocks are to be found on the devices.
6018 * The minor and patch _version numbers are also kept incase the
6019 * super_block handler wishes to interpret them.
6020 */
NeilBrownfd01b882011-10-11 16:47:53 +11006021static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
6023
6024 if (info->raid_disks == 0) {
6025 /* just setting version number for superblock loading */
6026 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006027 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 super_types[info->major_version].name == NULL) {
6029 /* maybe try to auto-load a module? */
6030 printk(KERN_INFO
6031 "md: superblock version %d not known\n",
6032 info->major_version);
6033 return -EINVAL;
6034 }
6035 mddev->major_version = info->major_version;
6036 mddev->minor_version = info->minor_version;
6037 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006038 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006039 /* ensure mddev_put doesn't delete this now that there
6040 * is some minimal configuration.
6041 */
6042 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 return 0;
6044 }
6045 mddev->major_version = MD_MAJOR_VERSION;
6046 mddev->minor_version = MD_MINOR_VERSION;
6047 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6048 mddev->ctime = get_seconds();
6049
6050 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006051 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006052 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 mddev->raid_disks = info->raid_disks;
6054 /* don't set md_minor, it is determined by which /dev/md* was
6055 * openned
6056 */
6057 if (info->state & (1<<MD_SB_CLEAN))
6058 mddev->recovery_cp = MaxSector;
6059 else
6060 mddev->recovery_cp = 0;
6061 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006062 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006065 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 mddev->max_disks = MD_SB_DISKS;
6068
NeilBrowne6910632008-02-06 01:39:51 -08006069 if (mddev->persistent)
6070 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006071 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
NeilBrownc3d97142009-12-14 12:49:52 +11006073 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006074 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006075 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006076
NeilBrownf6705572006-03-27 01:18:11 -08006077 mddev->reshape_position = MaxSector;
6078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 /*
6080 * Generate a 128 bit UUID
6081 */
6082 get_random_bytes(mddev->uuid, 16);
6083
NeilBrownf6705572006-03-27 01:18:11 -08006084 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006085 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006086 mddev->new_layout = mddev->layout;
6087 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006088 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return 0;
6091}
6092
NeilBrownfd01b882011-10-11 16:47:53 +11006093void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006094{
Dan Williamsb522adc2009-03-31 15:00:31 +11006095 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6096
6097 if (mddev->external_size)
6098 return;
6099
Dan Williams1f403622009-03-31 14:59:03 +11006100 mddev->array_sectors = array_sectors;
6101}
6102EXPORT_SYMBOL(md_set_array_sectors);
6103
NeilBrownfd01b882011-10-11 16:47:53 +11006104static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006105{
NeilBrown3cb03002011-10-11 16:45:26 +11006106 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006107 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006108 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006109
6110 if (mddev->pers->resize == NULL)
6111 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006112 /* The "num_sectors" is the number of sectors of each device that
6113 * is used. This can only make sense for arrays with redundancy.
6114 * linear and raid0 always use whatever space is available. We can only
6115 * consider changing this number if no resync or reconstruction is
6116 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006117 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006118 * of each device. If num_sectors is zero, we find the largest size
6119 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006120 */
6121 if (mddev->sync_thread)
6122 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006123
NeilBrowndafb20f2012-03-19 12:46:39 +11006124 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006125 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006126
Andre Nolld71f9f82008-07-11 22:02:22 +10006127 if (fit && (num_sectors == 0 || num_sectors > avail))
6128 num_sectors = avail;
6129 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006130 return -ENOSPC;
6131 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006132 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006133 if (!rv)
6134 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006135 return rv;
6136}
6137
NeilBrownfd01b882011-10-11 16:47:53 +11006138static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006139{
6140 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006141 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006142 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006143 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006144 return -EINVAL;
6145 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006146 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006147 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006148 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006149 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006150
6151 rdev_for_each(rdev, mddev) {
6152 if (mddev->raid_disks < raid_disks &&
6153 rdev->data_offset < rdev->new_data_offset)
6154 return -EINVAL;
6155 if (mddev->raid_disks > raid_disks &&
6156 rdev->data_offset > rdev->new_data_offset)
6157 return -EINVAL;
6158 }
6159
NeilBrown63c70c42006-03-27 01:18:13 -08006160 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006161 if (mddev->delta_disks < 0)
6162 mddev->reshape_backwards = 1;
6163 else if (mddev->delta_disks > 0)
6164 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006165
6166 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006167 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006168 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006169 mddev->reshape_backwards = 0;
6170 }
NeilBrownda943b992006-01-06 00:20:54 -08006171 return rv;
6172}
6173
6174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175/*
6176 * update_array_info is used to change the configuration of an
6177 * on-line array.
6178 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6179 * fields in the info are checked against the array.
6180 * Any differences that cannot be handled will cause an error.
6181 * Normally, only one change can be managed at a time.
6182 */
NeilBrownfd01b882011-10-11 16:47:53 +11006183static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184{
6185 int rv = 0;
6186 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006187 int state = 0;
6188
6189 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006190 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006191 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
6193 if (mddev->major_version != info->major_version ||
6194 mddev->minor_version != info->minor_version ||
6195/* mddev->patch_version != info->patch_version || */
6196 mddev->ctime != info->ctime ||
6197 mddev->level != info->level ||
6198/* mddev->layout != info->layout || */
6199 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006200 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006201 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6202 ((state^info->state) & 0xfffffe00)
6203 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 return -EINVAL;
6205 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006206 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6207 cnt++;
6208 if (mddev->raid_disks != info->raid_disks)
6209 cnt++;
6210 if (mddev->layout != info->layout)
6211 cnt++;
6212 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6213 cnt++;
6214 if (cnt == 0)
6215 return 0;
6216 if (cnt > 1)
6217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 if (mddev->layout != info->layout) {
6220 /* Change layout
6221 * we don't need to do anything at the md level, the
6222 * personality will take care of it all.
6223 */
NeilBrown50ac1682009-06-18 08:47:55 +10006224 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006226 else {
6227 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006228 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006229 if (rv)
6230 mddev->new_layout = mddev->layout;
6231 return rv;
6232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006234 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006235 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
NeilBrownda943b992006-01-06 00:20:54 -08006237 if (mddev->raid_disks != info->raid_disks)
6238 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
NeilBrown36fa3062005-09-09 16:23:45 -07006240 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6241 if (mddev->pers->quiesce == NULL)
6242 return -EINVAL;
6243 if (mddev->recovery || mddev->sync_thread)
6244 return -EBUSY;
6245 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6246 /* add the bitmap */
6247 if (mddev->bitmap)
6248 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006249 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006250 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006251 mddev->bitmap_info.offset =
6252 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006253 mddev->bitmap_info.space =
6254 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006255 mddev->pers->quiesce(mddev, 1);
6256 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006257 if (!rv)
6258 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006259 if (rv)
6260 bitmap_destroy(mddev);
6261 mddev->pers->quiesce(mddev, 0);
6262 } else {
6263 /* remove the bitmap */
6264 if (!mddev->bitmap)
6265 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006266 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006267 return -EINVAL;
6268 mddev->pers->quiesce(mddev, 1);
6269 bitmap_destroy(mddev);
6270 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006271 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006272 }
6273 }
NeilBrown850b2b42006-10-03 01:15:46 -07006274 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 return rv;
6276}
6277
NeilBrownfd01b882011-10-11 16:47:53 +11006278static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279{
NeilBrown3cb03002011-10-11 16:45:26 +11006280 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006281 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 if (mddev->pers == NULL)
6284 return -ENODEV;
6285
NeilBrown1ca69c42012-10-11 13:37:33 +11006286 rcu_read_lock();
6287 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006289 err = -ENODEV;
6290 else {
6291 md_error(mddev, rdev);
6292 if (!test_bit(Faulty, &rdev->flags))
6293 err = -EBUSY;
6294 }
6295 rcu_read_unlock();
6296 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297}
6298
Andre Noll2f9618c2008-04-25 18:57:58 +02006299/*
6300 * We have a problem here : there is no easy way to give a CHS
6301 * virtual geometry. We currently pretend that we have a 2 heads
6302 * 4 sectors (with a BIG number of cylinders...). This drives
6303 * dosfs just mad... ;-)
6304 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006305static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6306{
NeilBrownfd01b882011-10-11 16:47:53 +11006307 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006308
6309 geo->heads = 2;
6310 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006311 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006312 return 0;
6313}
6314
Al Viroa39907f2008-03-02 10:31:15 -05006315static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 unsigned int cmd, unsigned long arg)
6317{
6318 int err = 0;
6319 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006320 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006321 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
NeilBrown506c9e42011-12-23 10:17:26 +11006323 switch (cmd) {
6324 case RAID_VERSION:
6325 case GET_ARRAY_INFO:
6326 case GET_DISK_INFO:
6327 break;
6328 default:
6329 if (!capable(CAP_SYS_ADMIN))
6330 return -EACCES;
6331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
6333 /*
6334 * Commands dealing with the RAID driver but not any
6335 * particular array:
6336 */
6337 switch (cmd)
6338 {
6339 case RAID_VERSION:
6340 err = get_version(argp);
6341 goto done;
6342
6343 case PRINT_RAID_DEBUG:
6344 err = 0;
6345 md_print_devices();
6346 goto done;
6347
6348#ifndef MODULE
6349 case RAID_AUTORUN:
6350 err = 0;
6351 autostart_arrays(arg);
6352 goto done;
6353#endif
6354 default:;
6355 }
6356
6357 /*
6358 * Commands creating/starting a new array:
6359 */
6360
Al Viroa39907f2008-03-02 10:31:15 -05006361 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363 if (!mddev) {
6364 BUG();
6365 goto abort;
6366 }
6367
NeilBrown1ca69c42012-10-11 13:37:33 +11006368 /* Some actions do not requires the mutex */
6369 switch (cmd) {
6370 case GET_ARRAY_INFO:
6371 if (!mddev->raid_disks && !mddev->external)
6372 err = -ENODEV;
6373 else
6374 err = get_array_info(mddev, argp);
6375 goto abort;
6376
6377 case GET_DISK_INFO:
6378 if (!mddev->raid_disks && !mddev->external)
6379 err = -ENODEV;
6380 else
6381 err = get_disk_info(mddev, argp);
6382 goto abort;
6383
6384 case SET_DISK_FAULTY:
6385 err = set_disk_faulty(mddev, new_decode_dev(arg));
6386 goto abort;
6387 }
6388
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 err = mddev_lock(mddev);
6390 if (err) {
6391 printk(KERN_INFO
6392 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6393 err, cmd);
6394 goto abort;
6395 }
6396
6397 switch (cmd)
6398 {
6399 case SET_ARRAY_INFO:
6400 {
6401 mdu_array_info_t info;
6402 if (!arg)
6403 memset(&info, 0, sizeof(info));
6404 else if (copy_from_user(&info, argp, sizeof(info))) {
6405 err = -EFAULT;
6406 goto abort_unlock;
6407 }
6408 if (mddev->pers) {
6409 err = update_array_info(mddev, &info);
6410 if (err) {
6411 printk(KERN_WARNING "md: couldn't update"
6412 " array info. %d\n", err);
6413 goto abort_unlock;
6414 }
6415 goto done_unlock;
6416 }
6417 if (!list_empty(&mddev->disks)) {
6418 printk(KERN_WARNING
6419 "md: array %s already has disks!\n",
6420 mdname(mddev));
6421 err = -EBUSY;
6422 goto abort_unlock;
6423 }
6424 if (mddev->raid_disks) {
6425 printk(KERN_WARNING
6426 "md: array %s already initialised!\n",
6427 mdname(mddev));
6428 err = -EBUSY;
6429 goto abort_unlock;
6430 }
6431 err = set_array_info(mddev, &info);
6432 if (err) {
6433 printk(KERN_WARNING "md: couldn't set"
6434 " array info. %d\n", err);
6435 goto abort_unlock;
6436 }
6437 }
6438 goto done_unlock;
6439
6440 default:;
6441 }
6442
6443 /*
6444 * Commands querying/configuring an existing array:
6445 */
NeilBrown32a76272005-06-21 17:17:14 -07006446 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006447 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006448 if ((!mddev->raid_disks && !mddev->external)
6449 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6450 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6451 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 err = -ENODEV;
6453 goto abort_unlock;
6454 }
6455
6456 /*
6457 * Commands even a read-only array can execute:
6458 */
6459 switch (cmd)
6460 {
NeilBrown32a76272005-06-21 17:17:14 -07006461 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006462 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006463 goto done_unlock;
6464
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 case RESTART_ARRAY_RW:
6466 err = restart_array(mddev);
6467 goto done_unlock;
6468
6469 case STOP_ARRAY:
NeilBrowna05b7ea02012-07-19 15:59:18 +10006470 err = do_md_stop(mddev, 0, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 goto done_unlock;
6472
6473 case STOP_ARRAY_RO:
NeilBrowna05b7ea02012-07-19 15:59:18 +10006474 err = md_set_readonly(mddev, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 goto done_unlock;
6476
Dan Williamse2218352010-05-12 08:25:37 +10006477 case BLKROSET:
6478 if (get_user(ro, (int __user *)(arg))) {
6479 err = -EFAULT;
6480 goto done_unlock;
6481 }
6482 err = -EINVAL;
6483
6484 /* if the bdev is going readonly the value of mddev->ro
6485 * does not matter, no writes are coming
6486 */
6487 if (ro)
6488 goto done_unlock;
6489
6490 /* are we are already prepared for writes? */
6491 if (mddev->ro != 1)
6492 goto done_unlock;
6493
6494 /* transitioning to readauto need only happen for
6495 * arrays that call md_write_start
6496 */
6497 if (mddev->pers) {
6498 err = restart_array(mddev);
6499 if (err == 0) {
6500 mddev->ro = 2;
6501 set_disk_ro(mddev->gendisk, 0);
6502 }
6503 }
6504 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 }
6506
6507 /*
6508 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006509 * superblock, so we do not allow them on read-only arrays.
6510 * However non-MD ioctls (e.g. get-size) will still come through
6511 * here and hit the 'default' below, so only disallow
6512 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006514 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006515 if (mddev->ro == 2) {
6516 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006517 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006518 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6519 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006520 } else {
6521 err = -EROFS;
6522 goto abort_unlock;
6523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 }
6525
6526 switch (cmd)
6527 {
6528 case ADD_NEW_DISK:
6529 {
6530 mdu_disk_info_t info;
6531 if (copy_from_user(&info, argp, sizeof(info)))
6532 err = -EFAULT;
6533 else
6534 err = add_new_disk(mddev, &info);
6535 goto done_unlock;
6536 }
6537
6538 case HOT_REMOVE_DISK:
6539 err = hot_remove_disk(mddev, new_decode_dev(arg));
6540 goto done_unlock;
6541
6542 case HOT_ADD_DISK:
6543 err = hot_add_disk(mddev, new_decode_dev(arg));
6544 goto done_unlock;
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006547 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 goto done_unlock;
6549
NeilBrown32a76272005-06-21 17:17:14 -07006550 case SET_BITMAP_FILE:
6551 err = set_bitmap_file(mddev, (int)arg);
6552 goto done_unlock;
6553
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 err = -EINVAL;
6556 goto abort_unlock;
6557 }
6558
6559done_unlock:
6560abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006561 if (mddev->hold_active == UNTIL_IOCTL &&
6562 err != -EINVAL)
6563 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 mddev_unlock(mddev);
6565
6566 return err;
6567done:
6568 if (err)
6569 MD_BUG();
6570abort:
6571 return err;
6572}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006573#ifdef CONFIG_COMPAT
6574static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6575 unsigned int cmd, unsigned long arg)
6576{
6577 switch (cmd) {
6578 case HOT_REMOVE_DISK:
6579 case HOT_ADD_DISK:
6580 case SET_DISK_FAULTY:
6581 case SET_BITMAP_FILE:
6582 /* These take in integer arg, do not convert */
6583 break;
6584 default:
6585 arg = (unsigned long)compat_ptr(arg);
6586 break;
6587 }
6588
6589 return md_ioctl(bdev, mode, cmd, arg);
6590}
6591#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Al Viroa39907f2008-03-02 10:31:15 -05006593static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594{
6595 /*
6596 * Succeed if we can lock the mddev, which confirms that
6597 * it isn't being stopped right now.
6598 */
NeilBrownfd01b882011-10-11 16:47:53 +11006599 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 int err;
6601
Yuanhan Liu0c098222012-05-22 13:55:32 +10006602 if (!mddev)
6603 return -ENODEV;
6604
NeilBrownd3374822009-01-09 08:31:10 +11006605 if (mddev->gendisk != bdev->bd_disk) {
6606 /* we are racing with mddev_put which is discarding this
6607 * bd_disk.
6608 */
6609 mddev_put(mddev);
6610 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006611 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006612 /* Then retry the open from the top */
6613 return -ERESTARTSYS;
6614 }
6615 BUG_ON(mddev != bdev->bd_disk->private_data);
6616
NeilBrownc8c00a62009-08-10 12:50:52 +10006617 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 goto out;
6619
6620 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006621 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006622 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006624 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 out:
6626 return err;
6627}
6628
Al Viroa39907f2008-03-02 10:31:15 -05006629static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630{
NeilBrownfd01b882011-10-11 16:47:53 +11006631 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006633 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006634 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 mddev_put(mddev);
6636
6637 return 0;
6638}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006639
6640static int md_media_changed(struct gendisk *disk)
6641{
NeilBrownfd01b882011-10-11 16:47:53 +11006642 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006643
6644 return mddev->changed;
6645}
6646
6647static int md_revalidate(struct gendisk *disk)
6648{
NeilBrownfd01b882011-10-11 16:47:53 +11006649 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006650
6651 mddev->changed = 0;
6652 return 0;
6653}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006654static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655{
6656 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006657 .open = md_open,
6658 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006659 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006660#ifdef CONFIG_COMPAT
6661 .compat_ioctl = md_compat_ioctl,
6662#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006663 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006664 .media_changed = md_media_changed,
6665 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666};
6667
Adrian Bunk75c96f82005-05-05 16:16:09 -07006668static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
NeilBrown2b8bf342011-10-11 16:48:23 +11006670 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 /*
6673 * md_thread is a 'system-thread', it's priority should be very
6674 * high. We avoid resource deadlocks individually in each
6675 * raid personality. (RAID5 does preallocation) We also use RR and
6676 * the very same RT priority as kswapd, thus we will never get
6677 * into a priority inversion deadlock.
6678 *
6679 * we definitely have to have equal or higher priority than
6680 * bdflush, otherwise bdflush will deadlock if there are too
6681 * many dirty RAID5 blocks.
6682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
NeilBrown6985c432005-10-19 21:23:47 -07006684 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006685 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
NeilBrown93588e22005-11-15 00:09:12 -08006687 /* We need to wait INTERRUPTIBLE so that
6688 * we don't add to the load-average.
6689 * That means we need to be sure no signals are
6690 * pending
6691 */
6692 if (signal_pending(current))
6693 flush_signals(current);
6694
6695 wait_event_interruptible_timeout
6696 (thread->wqueue,
6697 test_bit(THREAD_WAKEUP, &thread->flags)
6698 || kthread_should_stop(),
6699 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
NeilBrown6c987912011-01-14 09:13:53 +11006701 clear_bit(THREAD_WAKEUP, &thread->flags);
6702 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006703 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 return 0;
6707}
6708
NeilBrown2b8bf342011-10-11 16:48:23 +11006709void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
6711 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006712 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 set_bit(THREAD_WAKEUP, &thread->flags);
6714 wake_up(&thread->wqueue);
6715 }
6716}
6717
Shaohua Li4ed87312012-10-11 13:34:00 +11006718struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6719 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
NeilBrown2b8bf342011-10-11 16:48:23 +11006721 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722
NeilBrown2b8bf342011-10-11 16:48:23 +11006723 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 if (!thread)
6725 return NULL;
6726
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 init_waitqueue_head(&thread->wqueue);
6728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 thread->run = run;
6730 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006731 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006732 thread->tsk = kthread_run(md_thread, thread,
6733 "%s_%s",
6734 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006735 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006736 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 kfree(thread);
6738 return NULL;
6739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 return thread;
6741}
6742
NeilBrown2b8bf342011-10-11 16:48:23 +11006743void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744{
NeilBrown2b8bf342011-10-11 16:48:23 +11006745 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006746 if (!thread)
6747 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006748 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006749 /* Locking ensures that mddev_unlock does not wake_up a
6750 * non-existent thread
6751 */
6752 spin_lock(&pers_lock);
6753 *threadp = NULL;
6754 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006755
6756 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 kfree(thread);
6758}
6759
NeilBrownfd01b882011-10-11 16:47:53 +11006760void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761{
6762 if (!mddev) {
6763 MD_BUG();
6764 return;
6765 }
6766
NeilBrownb2d444d2005-11-08 21:39:31 -08006767 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006769
NeilBrownde393cd2011-07-28 11:31:48 +10006770 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 return;
6772 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006773 if (mddev->degraded)
6774 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006775 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6778 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006779 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006780 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006781 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782}
6783
6784/* seq_file implementation /proc/mdstat */
6785
6786static void status_unused(struct seq_file *seq)
6787{
6788 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006789 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 seq_printf(seq, "unused devices: ");
6792
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006793 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 char b[BDEVNAME_SIZE];
6795 i++;
6796 seq_printf(seq, "%s ",
6797 bdevname(rdev->bdev,b));
6798 }
6799 if (!i)
6800 seq_printf(seq, "<none>");
6801
6802 seq_printf(seq, "\n");
6803}
6804
6805
NeilBrownfd01b882011-10-11 16:47:53 +11006806static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807{
NeilBrowndd71cf62009-05-07 12:49:35 +10006808 sector_t max_sectors, resync, res;
6809 unsigned long dt, db;
6810 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006811 int scale;
6812 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
NeilBrown72f36d52012-10-11 14:25:57 +11006814 if (mddev->curr_resync <= 3)
6815 resync = 0;
6816 else
6817 resync = mddev->curr_resync
6818 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
NeilBrownc804cde2012-05-21 09:28:33 +10006820 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6821 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006822 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006824 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
6826 /*
6827 * Should not happen.
6828 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006829 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 MD_BUG();
6831 return;
6832 }
NeilBrown4588b422006-03-27 01:18:04 -08006833 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006834 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006835 * u32, as those are the requirements for sector_div.
6836 * Thus 'scale' must be at least 10
6837 */
6838 scale = 10;
6839 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006840 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006841 scale++;
6842 }
6843 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006844 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006845
6846 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 {
NeilBrown4588b422006-03-27 01:18:04 -08006848 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 seq_printf(seq, "[");
6850 for (i = 0; i < x; i++)
6851 seq_printf(seq, "=");
6852 seq_printf(seq, ">");
6853 for (i = 0; i < y; i++)
6854 seq_printf(seq, ".");
6855 seq_printf(seq, "] ");
6856 }
NeilBrown4588b422006-03-27 01:18:04 -08006857 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006858 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6859 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006860 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6861 "check" :
6862 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6863 "resync" : "recovery"))),
6864 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006865 (unsigned long long) resync/2,
6866 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
6868 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 * dt: time from mark until now
6870 * db: blocks written from mark until now
6871 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006872 *
6873 * rt is a sector_t, so could be 32bit or 64bit.
6874 * So we divide before multiply in case it is 32bit and close
6875 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006876 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006877 * near the end of resync when the number of remaining sectors
6878 * is close to 'db'.
6879 * We then divide rt by 32 after multiplying by db to compensate.
6880 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 */
6882 dt = ((jiffies - mddev->resync_mark) / HZ);
6883 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006884 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6885 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
NeilBrowndd71cf62009-05-07 12:49:35 +10006887 rt = max_sectors - resync; /* number of remaining sectors */
6888 sector_div(rt, db/32+1);
6889 rt *= dt;
6890 rt >>= 5;
6891
6892 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6893 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
NeilBrownff4e8d92006-07-10 04:44:16 -07006895 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896}
6897
6898static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6899{
6900 struct list_head *tmp;
6901 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006902 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
6904 if (l >= 0x10000)
6905 return NULL;
6906 if (!l--)
6907 /* header */
6908 return (void*)1;
6909
6910 spin_lock(&all_mddevs_lock);
6911 list_for_each(tmp,&all_mddevs)
6912 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006913 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 mddev_get(mddev);
6915 spin_unlock(&all_mddevs_lock);
6916 return mddev;
6917 }
6918 spin_unlock(&all_mddevs_lock);
6919 if (!l--)
6920 return (void*)2;/* tail */
6921 return NULL;
6922}
6923
6924static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6925{
6926 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006927 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
6929 ++*pos;
6930 if (v == (void*)2)
6931 return NULL;
6932
6933 spin_lock(&all_mddevs_lock);
6934 if (v == (void*)1)
6935 tmp = all_mddevs.next;
6936 else
6937 tmp = mddev->all_mddevs.next;
6938 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006939 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 else {
6941 next_mddev = (void*)2;
6942 *pos = 0x10000;
6943 }
6944 spin_unlock(&all_mddevs_lock);
6945
6946 if (v != (void*)1)
6947 mddev_put(mddev);
6948 return next_mddev;
6949
6950}
6951
6952static void md_seq_stop(struct seq_file *seq, void *v)
6953{
NeilBrownfd01b882011-10-11 16:47:53 +11006954 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
6956 if (mddev && v != (void*)1 && v != (void*)2)
6957 mddev_put(mddev);
6958}
6959
6960static int md_seq_show(struct seq_file *seq, void *v)
6961{
NeilBrownfd01b882011-10-11 16:47:53 +11006962 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006963 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006964 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
6966 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006967 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 seq_printf(seq, "Personalities : ");
6969 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006970 list_for_each_entry(pers, &pers_list, list)
6971 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
6973 spin_unlock(&pers_lock);
6974 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006975 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 return 0;
6977 }
6978 if (v == (void*)2) {
6979 status_unused(seq);
6980 return 0;
6981 }
6982
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006983 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6987 seq_printf(seq, "%s : %sactive", mdname(mddev),
6988 mddev->pers ? "" : "in");
6989 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006990 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006992 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006993 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 seq_printf(seq, " %s", mddev->pers->name);
6995 }
6996
Andre Nolldd8ac332009-03-31 14:33:13 +11006997 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006998 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 char b[BDEVNAME_SIZE];
7000 seq_printf(seq, " %s[%d]",
7001 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007002 if (test_bit(WriteMostly, &rdev->flags))
7003 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007004 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 seq_printf(seq, "(F)");
7006 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007007 }
7008 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007009 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007010 if (test_bit(Replacement, &rdev->flags))
7011 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007012 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 }
7014
7015 if (!list_empty(&mddev->disks)) {
7016 if (mddev->pers)
7017 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007018 (unsigned long long)
7019 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 else
7021 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007022 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007024 if (mddev->persistent) {
7025 if (mddev->major_version != 0 ||
7026 mddev->minor_version != 90) {
7027 seq_printf(seq," super %d.%d",
7028 mddev->major_version,
7029 mddev->minor_version);
7030 }
NeilBrowne6910632008-02-06 01:39:51 -08007031 } else if (mddev->external)
7032 seq_printf(seq, " super external:%s",
7033 mddev->metadata_type);
7034 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007035 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
7037 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007038 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007040 if (mddev->pers->sync_request) {
7041 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007042 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007043 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007044 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007045 seq_printf(seq, "\tresync=DELAYED\n ");
7046 else if (mddev->recovery_cp < MaxSector)
7047 seq_printf(seq, "\tresync=PENDING\n ");
7048 }
NeilBrown32a76272005-06-21 17:17:14 -07007049 } else
7050 seq_printf(seq, "\n ");
7051
NeilBrown57148962012-03-19 12:46:40 +11007052 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053
7054 seq_printf(seq, "\n");
7055 }
7056 mddev_unlock(mddev);
7057
7058 return 0;
7059}
7060
Jan Engelhardt110518b2009-05-07 12:49:37 +10007061static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 .start = md_seq_start,
7063 .next = md_seq_next,
7064 .stop = md_seq_stop,
7065 .show = md_seq_show,
7066};
7067
7068static int md_seq_open(struct inode *inode, struct file *file)
7069{
Kay Sieversf1514632011-07-12 20:48:39 +02007070 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 int error;
7072
7073 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007074 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007075 return error;
7076
7077 seq = file->private_data;
7078 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 return error;
7080}
7081
NeilBrownd7603b72006-01-06 00:20:30 -08007082static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7083{
Kay Sieversf1514632011-07-12 20:48:39 +02007084 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007085 int mask;
7086
7087 poll_wait(filp, &md_event_waiters, wait);
7088
7089 /* always allow read */
7090 mask = POLLIN | POLLRDNORM;
7091
Kay Sieversf1514632011-07-12 20:48:39 +02007092 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007093 mask |= POLLERR | POLLPRI;
7094 return mask;
7095}
7096
Arjan van de Venfa027c22007-02-12 00:55:33 -08007097static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007098 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 .open = md_seq_open,
7100 .read = seq_read,
7101 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007102 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007103 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104};
7105
NeilBrown84fc4b52011-10-11 16:49:58 +11007106int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007109 list_add_tail(&p->list, &pers_list);
7110 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 spin_unlock(&pers_lock);
7112 return 0;
7113}
7114
NeilBrown84fc4b52011-10-11 16:49:58 +11007115int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116{
NeilBrown2604b702006-01-06 00:20:36 -08007117 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007119 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 spin_unlock(&pers_lock);
7121 return 0;
7122}
7123
NeilBrownfd01b882011-10-11 16:47:53 +11007124static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125{
NeilBrown3cb03002011-10-11 16:45:26 +11007126 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007128 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
7130 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007131 rcu_read_lock();
7132 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007134 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7135 (int)part_stat_read(&disk->part0, sectors[1]) -
7136 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007137 /* sync IO will cause sync_io to increase before the disk_stats
7138 * as sync_io is counted when a request starts, and
7139 * disk_stats is counted when it completes.
7140 * So resync activity will cause curr_events to be smaller than
7141 * when there was no such activity.
7142 * non-sync IO will cause disk_stat to increase without
7143 * increasing sync_io so curr_events will (eventually)
7144 * be larger than it was before. Once it becomes
7145 * substantially larger, the test below will cause
7146 * the array to appear non-idle, and resync will slow
7147 * down.
7148 * If there is a lot of outstanding resync activity when
7149 * we set last_event to curr_events, then all that activity
7150 * completing might cause the array to appear non-idle
7151 * and resync will be slowed down even though there might
7152 * not have been non-resync activity. This will only
7153 * happen once though. 'last_events' will soon reflect
7154 * the state where there is little or no outstanding
7155 * resync requests, and further resync activity will
7156 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007157 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007159 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 rdev->last_events = curr_events;
7161 idle = 0;
7162 }
7163 }
NeilBrown4b809912008-07-21 17:05:25 +10007164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 return idle;
7166}
7167
NeilBrownfd01b882011-10-11 16:47:53 +11007168void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169{
7170 /* another "blocks" (512byte) blocks have been synced */
7171 atomic_sub(blocks, &mddev->recovery_active);
7172 wake_up(&mddev->recovery_wait);
7173 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007174 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 md_wakeup_thread(mddev->thread);
7176 // stop recovery, signal do_sync ....
7177 }
7178}
7179
7180
NeilBrown06d91a52005-06-21 17:17:12 -07007181/* md_write_start(mddev, bi)
7182 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007183 * in superblock) before writing, schedule a superblock update
7184 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007185 */
NeilBrownfd01b882011-10-11 16:47:53 +11007186void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187{
Neil Brown0fd62b82008-06-28 08:31:36 +10007188 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007189 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007190 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007191
NeilBrownf91de922005-11-08 21:39:36 -08007192 BUG_ON(mddev->ro == 1);
7193 if (mddev->ro == 2) {
7194 /* need to switch to read/write */
7195 mddev->ro = 0;
7196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7197 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007198 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007199 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007200 }
NeilBrown06d91a52005-06-21 17:17:12 -07007201 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007202 if (mddev->safemode == 1)
7203 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007204 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007205 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007206 if (mddev->in_sync) {
7207 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007208 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007209 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007210 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007211 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007212 }
NeilBrowna9701a32005-11-08 21:39:34 -08007213 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007214 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007215 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007216 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007217 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007218 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219}
7220
NeilBrownfd01b882011-10-11 16:47:53 +11007221void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222{
7223 if (atomic_dec_and_test(&mddev->writes_pending)) {
7224 if (mddev->safemode == 2)
7225 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007226 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7228 }
7229}
7230
NeilBrown2a2275d2007-01-26 00:57:11 -08007231/* md_allow_write(mddev)
7232 * Calling this ensures that the array is marked 'active' so that writes
7233 * may proceed without blocking. It is important to call this before
7234 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7235 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007236 *
7237 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7238 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007239 */
NeilBrownfd01b882011-10-11 16:47:53 +11007240int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007241{
7242 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007243 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007244 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007245 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007246 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007247 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007248
7249 spin_lock_irq(&mddev->write_lock);
7250 if (mddev->in_sync) {
7251 mddev->in_sync = 0;
7252 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007253 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007254 if (mddev->safemode_delay &&
7255 mddev->safemode == 0)
7256 mddev->safemode = 1;
7257 spin_unlock_irq(&mddev->write_lock);
7258 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007259 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007260 } else
7261 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007262
NeilBrown070dc6d2010-08-30 17:33:34 +10007263 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007264 return -EAGAIN;
7265 else
7266 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007267}
7268EXPORT_SYMBOL_GPL(md_allow_write);
7269
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270#define SYNC_MARKS 10
7271#define SYNC_MARK_STEP (3*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007272void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273{
Shaohua Li4ed87312012-10-11 13:34:00 +11007274 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007275 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 unsigned int currspeed = 0,
7277 window;
NeilBrown57afd892005-06-21 17:17:13 -07007278 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 unsigned long mark[SYNC_MARKS];
7280 sector_t mark_cnt[SYNC_MARKS];
7281 int last_mark,m;
7282 struct list_head *tmp;
7283 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007284 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007285 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007286 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007287 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
7289 /* just incase thread restarts... */
7290 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7291 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007292 if (mddev->ro) /* never try to sync a read-only array */
7293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
NeilBrown61df9d92006-10-03 01:15:57 -07007295 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7296 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7297 desc = "data-check";
7298 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7299 desc = "requested-resync";
7300 else
7301 desc = "resync";
7302 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7303 desc = "reshape";
7304 else
7305 desc = "recovery";
7306
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 /* we overload curr_resync somewhat here.
7308 * 0 == not engaged in resync at all
7309 * 2 == checking that there is no conflict with another sync
7310 * 1 == like 2, but have yielded to allow conflicting resync to
7311 * commense
7312 * other == active in resync - this many blocks
7313 *
7314 * Before starting a resync we must have set curr_resync to
7315 * 2, and then checked that every "conflicting" array has curr_resync
7316 * less than ours. When we find one that is the same or higher
7317 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7318 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7319 * This will mean we have to start checking from the beginning again.
7320 *
7321 */
7322
7323 do {
7324 mddev->curr_resync = 2;
7325
7326 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007327 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007328 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007329
7330 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007332 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 if (mddev2 == mddev)
7334 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007335 if (!mddev->parallel_resync
7336 && mddev2->curr_resync
7337 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 DEFINE_WAIT(wq);
7339 if (mddev < mddev2 && mddev->curr_resync == 2) {
7340 /* arbitrarily yield */
7341 mddev->curr_resync = 1;
7342 wake_up(&resync_wait);
7343 }
7344 if (mddev > mddev2 && mddev->curr_resync == 1)
7345 /* no need to wait here, we can wait the next
7346 * time 'round when curr_resync == 2
7347 */
7348 continue;
NeilBrown97441972008-09-19 11:49:54 +10007349 /* We need to wait 'interruptible' so as not to
7350 * contribute to the load average, and not to
7351 * be caught by 'softlockup'
7352 */
7353 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007354 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007355 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007356 printk(KERN_INFO "md: delaying %s of %s"
7357 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007359 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007361 if (signal_pending(current))
7362 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 schedule();
7364 finish_wait(&resync_wait, &wq);
7365 goto try_again;
7366 }
7367 finish_wait(&resync_wait, &wq);
7368 }
7369 }
7370 } while (mddev->curr_resync < 2);
7371
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007372 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007373 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007375 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 */
7377 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007378 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007379 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007380 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7381 j = mddev->resync_min;
7382 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007383 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007384
NeilBrownccfcc3c2006-03-27 01:18:09 -08007385 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007386 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007387 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007389 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007390 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007391 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007392 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007393 if (rdev->raid_disk >= 0 &&
7394 !test_bit(Faulty, &rdev->flags) &&
7395 !test_bit(In_sync, &rdev->flags) &&
7396 rdev->recovery_offset < j)
7397 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007398 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
NeilBrown61df9d92006-10-03 01:15:57 -07007401 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7402 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7403 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007404 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007405 "(but not more than %d KB/sec) for %s.\n",
7406 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
NeilBrowneea1bf32009-03-31 14:27:02 +11007408 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007409
NeilBrown57afd892005-06-21 17:17:13 -07007410 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 for (m = 0; m < SYNC_MARKS; m++) {
7412 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007413 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 }
7415 last_mark = 0;
7416 mddev->resync_mark = mark[last_mark];
7417 mddev->resync_mark_cnt = mark_cnt[last_mark];
7418
7419 /*
7420 * Tune reconstruction:
7421 */
7422 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007423 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7424 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
7426 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 last_check = 0;
7428
7429 if (j>2) {
7430 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007431 "md: resuming %s of %s from checkpoint.\n",
7432 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007434 } else
7435 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007436 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007437 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7438 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
majianpeng7c2c57c2012-07-03 12:12:26 +10007440 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007442 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
NeilBrown57afd892005-06-21 17:17:13 -07007444 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007445
NeilBrown7a91ee12009-05-26 12:57:21 +10007446 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7447 ((mddev->curr_resync > mddev->curr_resync_completed &&
7448 (mddev->curr_resync - mddev->curr_resync_completed)
7449 > (max_sectors >> 4)) ||
7450 (j - mddev->curr_resync_completed)*2
7451 >= mddev->resync_max - mddev->curr_resync_completed
7452 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007453 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007454 wait_event(mddev->recovery_wait,
7455 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007456 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007457 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007458 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007459 }
NeilBrownacb180b2009-04-14 16:28:34 +10007460
NeilBrowne62e58a2009-07-01 13:15:35 +10007461 while (j >= mddev->resync_max && !kthread_should_stop()) {
7462 /* As this condition is controlled by user-space,
7463 * we can block indefinitely, so use '_interruptible'
7464 * to avoid triggering warnings.
7465 */
7466 flush_signals(current); /* just in case */
7467 wait_event_interruptible(mddev->recovery_wait,
7468 mddev->resync_max > j
7469 || kthread_should_stop());
7470 }
NeilBrownacb180b2009-04-14 16:28:34 +10007471
7472 if (kthread_should_stop())
7473 goto interrupted;
7474
NeilBrown57afd892005-06-21 17:17:13 -07007475 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007476 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007477 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007478 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 goto out;
7480 }
NeilBrown57afd892005-06-21 17:17:13 -07007481
7482 if (!skipped) { /* actual IO requested */
7483 io_sectors += sectors;
7484 atomic_add(sectors, &mddev->recovery_active);
7485 }
7486
NeilBrowne875ece2011-07-28 11:39:24 +10007487 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7488 break;
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007491 if (j > 2)
7492 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007493 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007494 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007495 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007496 * visible in /proc/mdstat
7497 */
7498 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007499
7500 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 continue;
7502
NeilBrown57afd892005-06-21 17:17:13 -07007503 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 repeat:
7505 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7506 /* step marks */
7507 int next = (last_mark+1) % SYNC_MARKS;
7508
7509 mddev->resync_mark = mark[next];
7510 mddev->resync_mark_cnt = mark_cnt[next];
7511 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007512 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 last_mark = next;
7514 }
7515
7516
NeilBrownc6207272008-02-06 01:39:52 -08007517 if (kthread_should_stop())
7518 goto interrupted;
7519
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
7521 /*
7522 * this loop exits only if either when we are slower than
7523 * the 'hard' speed limit, or the system was IO-idle for
7524 * a jiffy.
7525 * the system might be non-idle CPU-wise, but we only care
7526 * about not overloading the IO subsystem. (things like an
7527 * e2fsck being done on the RAID array should execute fast)
7528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 cond_resched();
7530
NeilBrown57afd892005-06-21 17:17:13 -07007531 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7532 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533
NeilBrown88202a02006-01-06 00:21:36 -08007534 if (currspeed > speed_min(mddev)) {
7535 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007536 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007537 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 goto repeat;
7539 }
7540 }
7541 }
NeilBrown61df9d92006-10-03 01:15:57 -07007542 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 /*
7544 * this also signals 'finished resyncing' to md_stop
7545 */
7546 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007547 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7549
7550 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007551 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
NeilBrowndfc70642008-05-23 13:04:39 -07007553 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007554 mddev->curr_resync > 2) {
7555 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7556 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7557 if (mddev->curr_resync >= mddev->recovery_cp) {
7558 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007559 "md: checkpointing %s of %s.\n",
7560 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007561 mddev->recovery_cp =
7562 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007563 }
7564 } else
7565 mddev->recovery_cp = MaxSector;
7566 } else {
7567 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7568 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007569 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007570 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007571 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007572 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007573 !test_bit(Faulty, &rdev->flags) &&
7574 !test_bit(In_sync, &rdev->flags) &&
7575 rdev->recovery_offset < mddev->curr_resync)
7576 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007577 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007580 skip:
NeilBrown17571282006-12-10 02:20:52 -08007581 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582
NeilBrownc07b70a2009-12-14 12:49:48 +11007583 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7584 /* We completed so min/max setting can be forgotten if used. */
7585 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7586 mddev->resync_min = 0;
7587 mddev->resync_max = MaxSector;
7588 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7589 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 mddev->curr_resync = 0;
7591 wake_up(&resync_wait);
7592 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7593 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007594 return;
7595
7596 interrupted:
7597 /*
7598 * got a signal, exit.
7599 */
7600 printk(KERN_INFO
7601 "md: md_do_sync() got signal ... exiting\n");
7602 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7603 goto out;
7604
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605}
NeilBrown29269552006-03-27 01:18:10 -08007606EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
NeilBrownfd01b882011-10-11 16:47:53 +11007608static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007609{
NeilBrown3cb03002011-10-11 16:45:26 +11007610 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007611 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007612 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007613
NeilBrowndafb20f2012-03-19 12:46:39 +11007614 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007615 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007616 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007617 (test_bit(Faulty, &rdev->flags) ||
7618 ! test_bit(In_sync, &rdev->flags)) &&
7619 atomic_read(&rdev->nr_pending)==0) {
7620 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007621 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007622 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007623 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007624 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007625 }
7626 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007627 if (removed)
7628 sysfs_notify(&mddev->kobj, NULL,
7629 "degraded");
7630
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007631
NeilBrowndafb20f2012-03-19 12:46:39 +11007632 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007633 if (rdev->raid_disk >= 0 &&
7634 !test_bit(In_sync, &rdev->flags) &&
7635 !test_bit(Faulty, &rdev->flags))
7636 spares++;
7637 if (rdev->raid_disk < 0
7638 && !test_bit(Faulty, &rdev->flags)) {
7639 rdev->recovery_offset = 0;
7640 if (mddev->pers->
7641 hot_add_disk(mddev, rdev) == 0) {
7642 if (sysfs_link_rdev(mddev, rdev))
7643 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007644 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007645 md_new_event(mddev);
7646 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007647 }
NeilBrowndfc70642008-05-23 13:04:39 -07007648 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007649 }
NeilBrown6dafab62012-09-19 12:54:22 +10007650 if (removed)
7651 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007652 return spares;
7653}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007654
NeilBrownfd01b882011-10-11 16:47:53 +11007655static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007656{
NeilBrown3cb03002011-10-11 16:45:26 +11007657 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007658
7659 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007660 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007661 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7662 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7663 /* success...*/
7664 /* activate any spares */
NeilBrown6dafab62012-09-19 12:54:22 +10007665 if (mddev->pers->spare_active(mddev)) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007666 sysfs_notify(&mddev->kobj, NULL,
7667 "degraded");
NeilBrown6dafab62012-09-19 12:54:22 +10007668 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7669 }
NeilBrown7ebc0be2011-01-14 09:14:33 +11007670 }
7671 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7672 mddev->pers->finish_reshape)
7673 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007674
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007675 /* If array is no-longer degraded, then any saved_raid_disk
7676 * information must be scrapped. Also if any device is now
7677 * In_sync we must scrape the saved_raid_disk for that device
7678 * do the superblock for an incrementally recovered device
7679 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007680 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007681 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007682 if (!mddev->degraded ||
7683 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007684 rdev->saved_raid_disk = -1;
7685
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007686 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007687 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7688 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7689 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7690 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7691 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7692 /* flag recovery needed just to double check */
7693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7694 sysfs_notify_dirent_safe(mddev->sysfs_action);
7695 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007696 if (mddev->event_work.func)
7697 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007698}
7699
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700/*
7701 * This routine is regularly called by all per-raid-array threads to
7702 * deal with generic issues like resync and super-block update.
7703 * Raid personalities that don't have a thread (linear/raid0) do not
7704 * need this as they never do any recovery or update the superblock.
7705 *
7706 * It does not do any resync itself, but rather "forks" off other threads
7707 * to do that as needed.
7708 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7709 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007710 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 * and wakeups up this thread which will reap the thread and finish up.
7712 * This thread also removes any faulty devices (with nr_pending == 0).
7713 *
7714 * The overall approach is:
7715 * 1/ if the superblock needs updating, update it.
7716 * 2/ If a recovery thread is running, don't do anything else.
7717 * 3/ If recovery has finished, clean up, possibly marking spares active.
7718 * 4/ If there are any faulty devices, remove them.
7719 * 5/ If array is degraded, try to add spares devices
7720 * 6/ If array has spares or is not in-sync, start a resync thread.
7721 */
NeilBrownfd01b882011-10-11 16:47:53 +11007722void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007724 if (mddev->suspended)
7725 return;
7726
NeilBrown5f404022005-06-21 17:17:16 -07007727 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007728 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729
NeilBrownfca4d842005-06-21 17:17:11 -07007730 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007731 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007732 printk(KERN_INFO "md: %s in immediate safe mode\n",
7733 mdname(mddev));
7734 mddev->safemode = 2;
7735 }
7736 flush_signals(current);
7737 }
7738
NeilBrownc89a8ee2008-08-05 15:54:13 +10007739 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007742 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007744 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007745 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007746 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7747 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748 ))
7749 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007750
NeilBrowndf5b89b2006-03-27 01:18:20 -08007751 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007752 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007753
NeilBrownc89a8ee2008-08-05 15:54:13 +10007754 if (mddev->ro) {
7755 /* Only thing we do on a ro array is remove
7756 * failed devices.
7757 */
NeilBrown3cb03002011-10-11 16:45:26 +11007758 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007759 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007760 if (rdev->raid_disk >= 0 &&
7761 !test_bit(Blocked, &rdev->flags) &&
7762 test_bit(Faulty, &rdev->flags) &&
7763 atomic_read(&rdev->nr_pending)==0) {
7764 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007765 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007766 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007767 rdev->raid_disk = -1;
7768 }
7769 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007770 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7771 goto unlock;
7772 }
7773
NeilBrown31a59e32008-04-30 00:52:30 -07007774 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007775 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007776 spin_lock_irq(&mddev->write_lock);
7777 if (mddev->safemode &&
7778 !atomic_read(&mddev->writes_pending) &&
7779 !mddev->in_sync &&
7780 mddev->recovery_cp == MaxSector) {
7781 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007782 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007783 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007784 }
7785 if (mddev->safemode == 1)
7786 mddev->safemode = 0;
7787 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007788 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007789 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007790 }
NeilBrownfca4d842005-06-21 17:17:11 -07007791
NeilBrown850b2b42006-10-03 01:15:46 -07007792 if (mddev->flags)
7793 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007794
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7796 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7797 /* resync/recovery still happening */
7798 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7799 goto unlock;
7800 }
7801 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007802 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 goto unlock;
7804 }
Neil Brown72a23c22008-06-28 08:31:41 +10007805 /* Set RUNNING before clearing NEEDED to avoid
7806 * any transients in the value of "sync_action".
7807 */
NeilBrown72f36d52012-10-11 14:25:57 +11007808 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007809 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007810 /* Clear some bits that don't mean anything, but
7811 * might be left set
7812 */
NeilBrown24dd4692005-11-08 21:39:26 -08007813 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7814 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815
NeilBrowned209582012-04-24 10:23:14 +10007816 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7817 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007818 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 /* no recovery is running.
7820 * remove any failed drives, then
7821 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007822 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 * the personality to fail the re-add.
7824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007826 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007827 if (mddev->pers->check_reshape == NULL ||
7828 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007829 /* Cannot proceed */
7830 goto unlock;
7831 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007832 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007833 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007834 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7835 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007836 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007837 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007838 } else if (mddev->recovery_cp < MaxSector) {
7839 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007840 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007841 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7842 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007844
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007846 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007847 /* We are adding a device or devices to an array
7848 * which has the bitmap stored on all devices.
7849 * So make sure all bitmap pages get written
7850 */
7851 bitmap_write_all(mddev->bitmap);
7852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 mddev->sync_thread = md_register_thread(md_do_sync,
7854 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007855 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 if (!mddev->sync_thread) {
7857 printk(KERN_ERR "%s: could not start resync"
7858 " thread...\n",
7859 mdname(mddev));
7860 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007861 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7862 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7863 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7864 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7865 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007866 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007868 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007869 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 }
7871 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007872 if (!mddev->sync_thread) {
7873 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7874 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7875 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007876 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007877 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 mddev_unlock(mddev);
7880 }
7881}
7882
NeilBrownfd01b882011-10-11 16:47:53 +11007883void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007884{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007885 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007886 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007887 !test_bit(Blocked, &rdev->flags) &&
7888 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007889 msecs_to_jiffies(5000));
7890 rdev_dec_pending(rdev, mddev);
7891}
7892EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7893
NeilBrownc6563a82012-05-21 09:27:00 +10007894void md_finish_reshape(struct mddev *mddev)
7895{
7896 /* called be personality module when reshape completes. */
7897 struct md_rdev *rdev;
7898
7899 rdev_for_each(rdev, mddev) {
7900 if (rdev->data_offset > rdev->new_data_offset)
7901 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7902 else
7903 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7904 rdev->data_offset = rdev->new_data_offset;
7905 }
7906}
7907EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007908
7909/* Bad block management.
7910 * We can record which blocks on each device are 'bad' and so just
7911 * fail those blocks, or that stripe, rather than the whole device.
7912 * Entries in the bad-block table are 64bits wide. This comprises:
7913 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7914 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7915 * A 'shift' can be set so that larger blocks are tracked and
7916 * consequently larger devices can be covered.
7917 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7918 *
7919 * Locking of the bad-block table uses a seqlock so md_is_badblock
7920 * might need to retry if it is very unlucky.
7921 * We will sometimes want to check for bad blocks in a bi_end_io function,
7922 * so we use the write_seqlock_irq variant.
7923 *
7924 * When looking for a bad block we specify a range and want to
7925 * know if any block in the range is bad. So we binary-search
7926 * to the last range that starts at-or-before the given endpoint,
7927 * (or "before the sector after the target range")
7928 * then see if it ends after the given start.
7929 * We return
7930 * 0 if there are no known bad blocks in the range
7931 * 1 if there are known bad block which are all acknowledged
7932 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7933 * plus the start/length of the first bad section we overlap.
7934 */
7935int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7936 sector_t *first_bad, int *bad_sectors)
7937{
7938 int hi;
7939 int lo = 0;
7940 u64 *p = bb->page;
7941 int rv = 0;
7942 sector_t target = s + sectors;
7943 unsigned seq;
7944
7945 if (bb->shift > 0) {
7946 /* round the start down, and the end up */
7947 s >>= bb->shift;
7948 target += (1<<bb->shift) - 1;
7949 target >>= bb->shift;
7950 sectors = target - s;
7951 }
7952 /* 'target' is now the first block after the bad range */
7953
7954retry:
7955 seq = read_seqbegin(&bb->lock);
7956
7957 hi = bb->count;
7958
7959 /* Binary search between lo and hi for 'target'
7960 * i.e. for the last range that starts before 'target'
7961 */
7962 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7963 * are known not to be the last range before target.
7964 * VARIANT: hi-lo is the number of possible
7965 * ranges, and decreases until it reaches 1
7966 */
7967 while (hi - lo > 1) {
7968 int mid = (lo + hi) / 2;
7969 sector_t a = BB_OFFSET(p[mid]);
7970 if (a < target)
7971 /* This could still be the one, earlier ranges
7972 * could not. */
7973 lo = mid;
7974 else
7975 /* This and later ranges are definitely out. */
7976 hi = mid;
7977 }
7978 /* 'lo' might be the last that started before target, but 'hi' isn't */
7979 if (hi > lo) {
7980 /* need to check all range that end after 's' to see if
7981 * any are unacknowledged.
7982 */
7983 while (lo >= 0 &&
7984 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7985 if (BB_OFFSET(p[lo]) < target) {
7986 /* starts before the end, and finishes after
7987 * the start, so they must overlap
7988 */
7989 if (rv != -1 && BB_ACK(p[lo]))
7990 rv = 1;
7991 else
7992 rv = -1;
7993 *first_bad = BB_OFFSET(p[lo]);
7994 *bad_sectors = BB_LEN(p[lo]);
7995 }
7996 lo--;
7997 }
7998 }
7999
8000 if (read_seqretry(&bb->lock, seq))
8001 goto retry;
8002
8003 return rv;
8004}
8005EXPORT_SYMBOL_GPL(md_is_badblock);
8006
8007/*
8008 * Add a range of bad blocks to the table.
8009 * This might extend the table, or might contract it
8010 * if two adjacent ranges can be merged.
8011 * We binary-search to find the 'insertion' point, then
8012 * decide how best to handle it.
8013 */
8014static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8015 int acknowledged)
8016{
8017 u64 *p;
8018 int lo, hi;
8019 int rv = 1;
8020
8021 if (bb->shift < 0)
8022 /* badblocks are disabled */
8023 return 0;
8024
8025 if (bb->shift) {
8026 /* round the start down, and the end up */
8027 sector_t next = s + sectors;
8028 s >>= bb->shift;
8029 next += (1<<bb->shift) - 1;
8030 next >>= bb->shift;
8031 sectors = next - s;
8032 }
8033
8034 write_seqlock_irq(&bb->lock);
8035
8036 p = bb->page;
8037 lo = 0;
8038 hi = bb->count;
8039 /* Find the last range that starts at-or-before 's' */
8040 while (hi - lo > 1) {
8041 int mid = (lo + hi) / 2;
8042 sector_t a = BB_OFFSET(p[mid]);
8043 if (a <= s)
8044 lo = mid;
8045 else
8046 hi = mid;
8047 }
8048 if (hi > lo && BB_OFFSET(p[lo]) > s)
8049 hi = lo;
8050
8051 if (hi > lo) {
8052 /* we found a range that might merge with the start
8053 * of our new range
8054 */
8055 sector_t a = BB_OFFSET(p[lo]);
8056 sector_t e = a + BB_LEN(p[lo]);
8057 int ack = BB_ACK(p[lo]);
8058 if (e >= s) {
8059 /* Yes, we can merge with a previous range */
8060 if (s == a && s + sectors >= e)
8061 /* new range covers old */
8062 ack = acknowledged;
8063 else
8064 ack = ack && acknowledged;
8065
8066 if (e < s + sectors)
8067 e = s + sectors;
8068 if (e - a <= BB_MAX_LEN) {
8069 p[lo] = BB_MAKE(a, e-a, ack);
8070 s = e;
8071 } else {
8072 /* does not all fit in one range,
8073 * make p[lo] maximal
8074 */
8075 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8076 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8077 s = a + BB_MAX_LEN;
8078 }
8079 sectors = e - s;
8080 }
8081 }
8082 if (sectors && hi < bb->count) {
8083 /* 'hi' points to the first range that starts after 's'.
8084 * Maybe we can merge with the start of that range */
8085 sector_t a = BB_OFFSET(p[hi]);
8086 sector_t e = a + BB_LEN(p[hi]);
8087 int ack = BB_ACK(p[hi]);
8088 if (a <= s + sectors) {
8089 /* merging is possible */
8090 if (e <= s + sectors) {
8091 /* full overlap */
8092 e = s + sectors;
8093 ack = acknowledged;
8094 } else
8095 ack = ack && acknowledged;
8096
8097 a = s;
8098 if (e - a <= BB_MAX_LEN) {
8099 p[hi] = BB_MAKE(a, e-a, ack);
8100 s = e;
8101 } else {
8102 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8103 s = a + BB_MAX_LEN;
8104 }
8105 sectors = e - s;
8106 lo = hi;
8107 hi++;
8108 }
8109 }
8110 if (sectors == 0 && hi < bb->count) {
8111 /* we might be able to combine lo and hi */
8112 /* Note: 's' is at the end of 'lo' */
8113 sector_t a = BB_OFFSET(p[hi]);
8114 int lolen = BB_LEN(p[lo]);
8115 int hilen = BB_LEN(p[hi]);
8116 int newlen = lolen + hilen - (s - a);
8117 if (s >= a && newlen < BB_MAX_LEN) {
8118 /* yes, we can combine them */
8119 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8120 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8121 memmove(p + hi, p + hi + 1,
8122 (bb->count - hi - 1) * 8);
8123 bb->count--;
8124 }
8125 }
8126 while (sectors) {
8127 /* didn't merge (it all).
8128 * Need to add a range just before 'hi' */
8129 if (bb->count >= MD_MAX_BADBLOCKS) {
8130 /* No room for more */
8131 rv = 0;
8132 break;
8133 } else {
8134 int this_sectors = sectors;
8135 memmove(p + hi + 1, p + hi,
8136 (bb->count - hi) * 8);
8137 bb->count++;
8138
8139 if (this_sectors > BB_MAX_LEN)
8140 this_sectors = BB_MAX_LEN;
8141 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8142 sectors -= this_sectors;
8143 s += this_sectors;
8144 }
8145 }
8146
8147 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008148 if (!acknowledged)
8149 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008150 write_sequnlock_irq(&bb->lock);
8151
8152 return rv;
8153}
8154
NeilBrown3cb03002011-10-11 16:45:26 +11008155int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008156 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008157{
NeilBrownc6563a82012-05-21 09:27:00 +10008158 int rv;
8159 if (is_new)
8160 s += rdev->new_data_offset;
8161 else
8162 s += rdev->data_offset;
8163 rv = md_set_badblocks(&rdev->badblocks,
8164 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008165 if (rv) {
8166 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008167 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008168 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8169 md_wakeup_thread(rdev->mddev->thread);
8170 }
8171 return rv;
8172}
8173EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8174
8175/*
8176 * Remove a range of bad blocks from the table.
8177 * This may involve extending the table if we spilt a region,
8178 * but it must not fail. So if the table becomes full, we just
8179 * drop the remove request.
8180 */
8181static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8182{
8183 u64 *p;
8184 int lo, hi;
8185 sector_t target = s + sectors;
8186 int rv = 0;
8187
8188 if (bb->shift > 0) {
8189 /* When clearing we round the start up and the end down.
8190 * This should not matter as the shift should align with
8191 * the block size and no rounding should ever be needed.
8192 * However it is better the think a block is bad when it
8193 * isn't than to think a block is not bad when it is.
8194 */
8195 s += (1<<bb->shift) - 1;
8196 s >>= bb->shift;
8197 target >>= bb->shift;
8198 sectors = target - s;
8199 }
8200
8201 write_seqlock_irq(&bb->lock);
8202
8203 p = bb->page;
8204 lo = 0;
8205 hi = bb->count;
8206 /* Find the last range that starts before 'target' */
8207 while (hi - lo > 1) {
8208 int mid = (lo + hi) / 2;
8209 sector_t a = BB_OFFSET(p[mid]);
8210 if (a < target)
8211 lo = mid;
8212 else
8213 hi = mid;
8214 }
8215 if (hi > lo) {
8216 /* p[lo] is the last range that could overlap the
8217 * current range. Earlier ranges could also overlap,
8218 * but only this one can overlap the end of the range.
8219 */
8220 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8221 /* Partial overlap, leave the tail of this range */
8222 int ack = BB_ACK(p[lo]);
8223 sector_t a = BB_OFFSET(p[lo]);
8224 sector_t end = a + BB_LEN(p[lo]);
8225
8226 if (a < s) {
8227 /* we need to split this range */
8228 if (bb->count >= MD_MAX_BADBLOCKS) {
8229 rv = 0;
8230 goto out;
8231 }
8232 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8233 bb->count++;
8234 p[lo] = BB_MAKE(a, s-a, ack);
8235 lo++;
8236 }
8237 p[lo] = BB_MAKE(target, end - target, ack);
8238 /* there is no longer an overlap */
8239 hi = lo;
8240 lo--;
8241 }
8242 while (lo >= 0 &&
8243 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8244 /* This range does overlap */
8245 if (BB_OFFSET(p[lo]) < s) {
8246 /* Keep the early parts of this range. */
8247 int ack = BB_ACK(p[lo]);
8248 sector_t start = BB_OFFSET(p[lo]);
8249 p[lo] = BB_MAKE(start, s - start, ack);
8250 /* now low doesn't overlap, so.. */
8251 break;
8252 }
8253 lo--;
8254 }
8255 /* 'lo' is strictly before, 'hi' is strictly after,
8256 * anything between needs to be discarded
8257 */
8258 if (hi - lo > 1) {
8259 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8260 bb->count -= (hi - lo - 1);
8261 }
8262 }
8263
8264 bb->changed = 1;
8265out:
8266 write_sequnlock_irq(&bb->lock);
8267 return rv;
8268}
8269
NeilBrownc6563a82012-05-21 09:27:00 +10008270int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8271 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008272{
NeilBrownc6563a82012-05-21 09:27:00 +10008273 if (is_new)
8274 s += rdev->new_data_offset;
8275 else
8276 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008277 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008278 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008279}
8280EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8281
8282/*
8283 * Acknowledge all bad blocks in a list.
8284 * This only succeeds if ->changed is clear. It is used by
8285 * in-kernel metadata updates
8286 */
8287void md_ack_all_badblocks(struct badblocks *bb)
8288{
8289 if (bb->page == NULL || bb->changed)
8290 /* no point even trying */
8291 return;
8292 write_seqlock_irq(&bb->lock);
8293
majianpengecb178b2012-03-19 12:46:42 +11008294 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008295 u64 *p = bb->page;
8296 int i;
8297 for (i = 0; i < bb->count ; i++) {
8298 if (!BB_ACK(p[i])) {
8299 sector_t start = BB_OFFSET(p[i]);
8300 int len = BB_LEN(p[i]);
8301 p[i] = BB_MAKE(start, len, 1);
8302 }
8303 }
NeilBrownde393cd2011-07-28 11:31:48 +10008304 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008305 }
8306 write_sequnlock_irq(&bb->lock);
8307}
8308EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8309
NeilBrown16c791a2011-07-28 11:31:47 +10008310/* sysfs access to bad-blocks list.
8311 * We present two files.
8312 * 'bad-blocks' lists sector numbers and lengths of ranges that
8313 * are recorded as bad. The list is truncated to fit within
8314 * the one-page limit of sysfs.
8315 * Writing "sector length" to this file adds an acknowledged
8316 * bad block list.
8317 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8318 * been acknowledged. Writing to this file adds bad blocks
8319 * without acknowledging them. This is largely for testing.
8320 */
8321
8322static ssize_t
8323badblocks_show(struct badblocks *bb, char *page, int unack)
8324{
8325 size_t len;
8326 int i;
8327 u64 *p = bb->page;
8328 unsigned seq;
8329
8330 if (bb->shift < 0)
8331 return 0;
8332
8333retry:
8334 seq = read_seqbegin(&bb->lock);
8335
8336 len = 0;
8337 i = 0;
8338
8339 while (len < PAGE_SIZE && i < bb->count) {
8340 sector_t s = BB_OFFSET(p[i]);
8341 unsigned int length = BB_LEN(p[i]);
8342 int ack = BB_ACK(p[i]);
8343 i++;
8344
8345 if (unack && ack)
8346 continue;
8347
8348 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8349 (unsigned long long)s << bb->shift,
8350 length << bb->shift);
8351 }
NeilBrownde393cd2011-07-28 11:31:48 +10008352 if (unack && len == 0)
8353 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008354
8355 if (read_seqretry(&bb->lock, seq))
8356 goto retry;
8357
8358 return len;
8359}
8360
8361#define DO_DEBUG 1
8362
8363static ssize_t
8364badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8365{
8366 unsigned long long sector;
8367 int length;
8368 char newline;
8369#ifdef DO_DEBUG
8370 /* Allow clearing via sysfs *only* for testing/debugging.
8371 * Normally only a successful write may clear a badblock
8372 */
8373 int clear = 0;
8374 if (page[0] == '-') {
8375 clear = 1;
8376 page++;
8377 }
8378#endif /* DO_DEBUG */
8379
8380 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8381 case 3:
8382 if (newline != '\n')
8383 return -EINVAL;
8384 case 2:
8385 if (length <= 0)
8386 return -EINVAL;
8387 break;
8388 default:
8389 return -EINVAL;
8390 }
8391
8392#ifdef DO_DEBUG
8393 if (clear) {
8394 md_clear_badblocks(bb, sector, length);
8395 return len;
8396 }
8397#endif /* DO_DEBUG */
8398 if (md_set_badblocks(bb, sector, length, !unack))
8399 return len;
8400 else
8401 return -ENOSPC;
8402}
8403
Adrian Bunk75c96f82005-05-05 16:16:09 -07008404static int md_notify_reboot(struct notifier_block *this,
8405 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406{
8407 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008408 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008409 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410
NeilBrownc744a652012-03-19 12:46:37 +11008411 for_each_mddev(mddev, tmp) {
8412 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008413 if (mddev->pers)
8414 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008415 mddev->safemode = 2;
8416 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008417 }
NeilBrownc744a652012-03-19 12:46:37 +11008418 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 }
NeilBrownc744a652012-03-19 12:46:37 +11008420 /*
8421 * certain more exotic SCSI devices are known to be
8422 * volatile wrt too early system reboots. While the
8423 * right place to handle this issue is the given
8424 * driver, we do want to have a safe RAID driver ...
8425 */
8426 if (need_delay)
8427 mdelay(1000*1);
8428
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 return NOTIFY_DONE;
8430}
8431
Adrian Bunk75c96f82005-05-05 16:16:09 -07008432static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 .notifier_call = md_notify_reboot,
8434 .next = NULL,
8435 .priority = INT_MAX, /* before any real devices */
8436};
8437
8438static void md_geninit(void)
8439{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008440 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008442 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443}
8444
Adrian Bunk75c96f82005-05-05 16:16:09 -07008445static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446{
Tejun Heoe804ac72010-10-15 15:36:08 +02008447 int ret = -ENOMEM;
8448
Tejun Heoada609e2011-01-25 14:35:54 +01008449 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008450 if (!md_wq)
8451 goto err_wq;
8452
8453 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8454 if (!md_misc_wq)
8455 goto err_misc_wq;
8456
8457 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8458 goto err_md;
8459
8460 if ((ret = register_blkdev(0, "mdp")) < 0)
8461 goto err_mdp;
8462 mdp_major = ret;
8463
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008464 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008465 md_probe, NULL, NULL);
8466 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467 md_probe, NULL, NULL);
8468
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008470 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471
8472 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474
Tejun Heoe804ac72010-10-15 15:36:08 +02008475err_mdp:
8476 unregister_blkdev(MD_MAJOR, "md");
8477err_md:
8478 destroy_workqueue(md_misc_wq);
8479err_misc_wq:
8480 destroy_workqueue(md_wq);
8481err_wq:
8482 return ret;
8483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484
8485#ifndef MODULE
8486
8487/*
8488 * Searches all registered partitions for autorun RAID arrays
8489 * at boot time.
8490 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008491
8492static LIST_HEAD(all_detected_devices);
8493struct detected_devices_node {
8494 struct list_head list;
8495 dev_t dev;
8496};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497
8498void md_autodetect_dev(dev_t dev)
8499{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008500 struct detected_devices_node *node_detected_dev;
8501
8502 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8503 if (node_detected_dev) {
8504 node_detected_dev->dev = dev;
8505 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8506 } else {
8507 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8508 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510}
8511
8512
8513static void autostart_arrays(int part)
8514{
NeilBrown3cb03002011-10-11 16:45:26 +11008515 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008516 struct detected_devices_node *node_detected_dev;
8517 dev_t dev;
8518 int i_scanned, i_passed;
8519
8520 i_scanned = 0;
8521 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522
8523 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8524
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008525 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8526 i_scanned++;
8527 node_detected_dev = list_entry(all_detected_devices.next,
8528 struct detected_devices_node, list);
8529 list_del(&node_detected_dev->list);
8530 dev = node_detected_dev->dev;
8531 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008532 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 if (IS_ERR(rdev))
8534 continue;
8535
NeilBrownb2d444d2005-11-08 21:39:31 -08008536 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 MD_BUG();
8538 continue;
8539 }
NeilBrownd0fae182008-03-04 14:29:31 -08008540 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008542 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008544
8545 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8546 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547
8548 autorun_devices(part);
8549}
8550
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008551#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552
8553static __exit void md_exit(void)
8554{
NeilBrownfd01b882011-10-11 16:47:53 +11008555 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008557
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008558 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008559 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008561 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 unregister_blkdev(mdp_major, "mdp");
8563 unregister_reboot_notifier(&md_notifier);
8564 unregister_sysctl_table(raid_table_header);
8565 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008566 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008568 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008570 destroy_workqueue(md_misc_wq);
8571 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572}
8573
Dan Williams685784a2007-07-09 11:56:42 -07008574subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008575module_exit(md_exit)
8576
NeilBrownf91de922005-11-08 21:39:36 -08008577static int get_ro(char *buffer, struct kernel_param *kp)
8578{
8579 return sprintf(buffer, "%d", start_readonly);
8580}
8581static int set_ro(const char *val, struct kernel_param *kp)
8582{
8583 char *e;
8584 int num = simple_strtoul(val, &e, 10);
8585 if (*val && (*e == '\0' || *e == '\n')) {
8586 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008587 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008588 }
8589 return -EINVAL;
8590}
8591
NeilBrown80ca3a42006-07-10 04:44:18 -07008592module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8593module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008594
NeilBrownefeb53c2009-01-09 08:31:10 +11008595module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008596
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597EXPORT_SYMBOL(register_md_personality);
8598EXPORT_SYMBOL(unregister_md_personality);
8599EXPORT_SYMBOL(md_error);
8600EXPORT_SYMBOL(md_done_sync);
8601EXPORT_SYMBOL(md_write_start);
8602EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603EXPORT_SYMBOL(md_register_thread);
8604EXPORT_SYMBOL(md_unregister_thread);
8605EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606EXPORT_SYMBOL(md_check_recovery);
8607MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008608MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008609MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008610MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);