blob: 7a2b0793f66e835e63d9892689504d5b6789c83d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
NeilBrowna167f662010-10-26 18:31:13 +1100158struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100159 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100160{
161 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100162
163 if (!mddev || !mddev->bio_set)
164 return bio_alloc(gfp_mask, nr_iovecs);
165
Kent Overstreet395c72a2012-09-06 15:34:55 -0700166 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100167 if (!b)
168 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100169 return b;
170}
171EXPORT_SYMBOL_GPL(bio_alloc_mddev);
172
173struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100174 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100175{
NeilBrowna167f662010-10-26 18:31:13 +1100176 if (!mddev || !mddev->bio_set)
177 return bio_clone(bio, gfp_mask);
178
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700179 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100180}
181EXPORT_SYMBOL_GPL(bio_clone_mddev);
182
NeilBrownd2eb35a2011-07-28 11:31:48 +1000183void md_trim_bio(struct bio *bio, int offset, int size)
184{
185 /* 'bio' is a cloned bio which we need to trim to match
186 * the given offset and size.
187 * This requires adjusting bi_sector, bi_size, and bi_io_vec
188 */
189 int i;
190 struct bio_vec *bvec;
191 int sofar = 0;
192
193 size <<= 9;
194 if (offset == 0 && size == bio->bi_size)
195 return;
196
197 bio->bi_sector += offset;
198 bio->bi_size = size;
199 offset <<= 9;
200 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
201
202 while (bio->bi_idx < bio->bi_vcnt &&
203 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
204 /* remove this whole bio_vec */
205 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
206 bio->bi_idx++;
207 }
208 if (bio->bi_idx < bio->bi_vcnt) {
209 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
210 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
211 }
212 /* avoid any complications with bi_idx being non-zero*/
213 if (bio->bi_idx) {
214 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
215 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
216 bio->bi_vcnt -= bio->bi_idx;
217 bio->bi_idx = 0;
218 }
219 /* Make sure vcnt and last bv are not too big */
220 bio_for_each_segment(bvec, bio, i) {
221 if (sofar + bvec->bv_len > size)
222 bvec->bv_len = size - sofar;
223 if (bvec->bv_len == 0) {
224 bio->bi_vcnt = i;
225 break;
226 }
227 sofar += bvec->bv_len;
228 }
229}
230EXPORT_SYMBOL_GPL(md_trim_bio);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
NeilBrownd7603b72006-01-06 00:20:30 -0800233 * We have a system wide 'event count' that is incremented
234 * on any 'interesting' event, and readers of /proc/mdstat
235 * can use 'poll' or 'select' to find out when the event
236 * count increases.
237 *
238 * Events are:
239 * start array, stop array, error, add device, remove device,
240 * start build, activate spare
241 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800242static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800243static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100244void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800245{
246 atomic_inc(&md_event_count);
247 wake_up(&md_event_waiters);
248}
NeilBrown29269552006-03-27 01:18:10 -0800249EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800250
NeilBrownc331eb02006-05-30 21:27:13 -0700251/* Alternate version that can be called from interrupts
252 * when calling sysfs_notify isn't needed.
253 */
NeilBrownfd01b882011-10-11 16:47:53 +1100254static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700255{
256 atomic_inc(&md_event_count);
257 wake_up(&md_event_waiters);
258}
259
NeilBrownd7603b72006-01-06 00:20:30 -0800260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * Enables to iterate over all existing md arrays
262 * all_mddevs_lock protects this list.
263 */
264static LIST_HEAD(all_mddevs);
265static DEFINE_SPINLOCK(all_mddevs_lock);
266
267
268/*
269 * iterates through all used mddevs in the system.
270 * We take care to grab the all_mddevs_lock whenever navigating
271 * the list, and to always hold a refcount when unlocked.
272 * Any code which breaks out of this loop while own
273 * a reference to the current mddev and must mddev_put it.
274 */
NeilBrownfd01b882011-10-11 16:47:53 +1100275#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 \
277 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100278 _tmp = all_mddevs.next; \
279 _mddev = NULL;}); \
280 ({ if (_tmp != &all_mddevs) \
281 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100283 if (_mddev) mddev_put(_mddev); \
284 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
285 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100287 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 )
289
290
NeilBrown409c57f2009-03-31 14:39:39 +1100291/* Rather than calling directly into the personality make_request function,
292 * IO requests come here first so that we can check if the device is
293 * being suspended pending a reconfiguration.
294 * We hold a refcount over the call to ->make_request. By the time that
295 * call has finished, the bio has been linked into some internal structure
296 * and so is visible to ->quiesce(), so we don't need the refcount any more.
297 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200298static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
NeilBrown49077322010-03-25 16:20:56 +1100300 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100301 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100302 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500303 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100304
NeilBrown0ca69882011-01-14 09:14:33 +1100305 if (mddev == NULL || mddev->pers == NULL
306 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100307 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200308 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100309 }
NeilBrown0ca69882011-01-14 09:14:33 +1100310 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100311 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200312 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100313 DEFINE_WAIT(__wait);
314 for (;;) {
315 prepare_to_wait(&mddev->sb_wait, &__wait,
316 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200317 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100318 break;
319 rcu_read_unlock();
320 schedule();
321 rcu_read_lock();
322 }
323 finish_wait(&mddev->sb_wait, &__wait);
324 }
325 atomic_inc(&mddev->active_io);
326 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100327
Chris Masone91ece52011-02-07 19:21:48 -0500328 /*
329 * save the sectors now since our bio can
330 * go away inside make_request
331 */
332 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200333 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100334
335 cpu = part_stat_lock();
336 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500337 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100338 part_stat_unlock();
339
NeilBrown409c57f2009-03-31 14:39:39 +1100340 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
341 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
NeilBrown9e35b992010-04-06 14:23:02 +1000344/* mddev_suspend makes sure no new requests are submitted
345 * to the device, and that any requests that have been submitted
346 * are completely handled.
347 * Once ->stop is called and completes, the module will be completely
348 * unused.
349 */
NeilBrownfd01b882011-10-11 16:47:53 +1100350void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100351{
352 BUG_ON(mddev->suspended);
353 mddev->suspended = 1;
354 synchronize_rcu();
355 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
356 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500357
358 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100359}
NeilBrown390ee602010-06-01 19:37:27 +1000360EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100361
NeilBrownfd01b882011-10-11 16:47:53 +1100362void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100363{
364 mddev->suspended = 0;
365 wake_up(&mddev->sb_wait);
366 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500367
Jonathan Brassow47525e52012-05-22 13:55:29 +1000368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500369 md_wakeup_thread(mddev->thread);
370 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100371}
NeilBrown390ee602010-06-01 19:37:27 +1000372EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100373
NeilBrownfd01b882011-10-11 16:47:53 +1100374int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000375{
376 return mddev->suspended;
377}
378EXPORT_SYMBOL(mddev_congested);
379
NeilBrowna2826aa2009-12-14 12:49:49 +1100380/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200381 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100382 */
383
Tejun Heoe9c74692010-09-03 11:56:18 +0200384static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100385{
NeilBrown3cb03002011-10-11 16:45:26 +1100386 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100387 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100388
389 rdev_dec_pending(rdev, mddev);
390
391 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200392 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200393 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100394 }
395 bio_put(bio);
396}
397
NeilBrowna7a07e62010-12-09 16:04:25 +1100398static void md_submit_flush_data(struct work_struct *ws);
399
NeilBrowna035fc32010-12-09 16:17:51 +1100400static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100401{
NeilBrownfd01b882011-10-11 16:47:53 +1100402 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100403 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100404
NeilBrowna7a07e62010-12-09 16:04:25 +1100405 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
406 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100408 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409 if (rdev->raid_disk >= 0 &&
410 !test_bit(Faulty, &rdev->flags)) {
411 /* Take two references, one is dropped
412 * when request finishes, one after
413 * we reclaim rcu_read_lock
414 */
415 struct bio *bi;
416 atomic_inc(&rdev->nr_pending);
417 atomic_inc(&rdev->nr_pending);
418 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000419 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 bi->bi_private = rdev;
422 bi->bi_bdev = rdev->bdev;
423 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 rcu_read_lock();
426 rdev_dec_pending(rdev, mddev);
427 }
428 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100429 if (atomic_dec_and_test(&mddev->flush_pending))
430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431}
432
Tejun Heoe9c74692010-09-03 11:56:18 +0200433static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100434{
NeilBrownfd01b882011-10-11 16:47:53 +1100435 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200436 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100437
Tejun Heoe9c74692010-09-03 11:56:18 +0200438 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 /* an empty barrier - all done */
440 bio_endio(bio, 0);
441 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200443 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 }
NeilBrown2b74e122010-12-09 15:59:01 +1100445
446 mddev->flush_bio = NULL;
447 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448}
449
NeilBrownfd01b882011-10-11 16:47:53 +1100450void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100451{
452 spin_lock_irq(&mddev->write_lock);
453 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200454 !mddev->flush_bio,
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
NeilBrownfd01b882011-10-11 16:47:53 +1100677static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
NeilBrown3cb03002011-10-11 16:45:26 +1100679 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
NeilBrowndafb20f2012-03-19 12:46:39 +1100681 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (rdev->bdev->bd_dev == dev)
683 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return NULL;
686}
687
NeilBrown84fc4b52011-10-11 16:49:58 +1100688static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800689{
NeilBrown84fc4b52011-10-11 16:49:58 +1100690 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800691 list_for_each_entry(pers, &pers_list, list) {
692 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800693 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800694 if (strcmp(pers->name, clevel)==0)
695 return pers;
696 }
NeilBrown2604b702006-01-06 00:20:36 -0800697 return NULL;
698}
699
Andre Nollb73df2d2008-07-11 22:02:23 +1000700/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100701static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100703 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000704 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
NeilBrown3cb03002011-10-11 16:45:26 +1100707static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 if (rdev->sb_page)
710 MD_BUG();
711
712 rdev->sb_page = alloc_page(GFP_KERNEL);
713 if (!rdev->sb_page) {
714 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000715 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
718 return 0;
719}
720
NeilBrown545c8792012-05-22 13:54:30 +1000721void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800724 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 rdev->sb_loaded = 0;
726 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000727 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100728 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
NeilBrown2699b672011-07-28 11:31:47 +1000730 if (rdev->bb_page) {
731 put_page(rdev->bb_page);
732 rdev->bb_page = NULL;
733 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000734 kfree(rdev->badblocks.page);
735 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
NeilBrown545c8792012-05-22 13:54:30 +1000737EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
NeilBrown6712ecf2007-09-27 12:47:43 +0200739static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740{
NeilBrown3cb03002011-10-11 16:45:26 +1100741 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100742 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700744 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
745 printk("md: super_written gets error=%d, uptodate=%d\n",
746 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
747 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800748 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700749 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750
NeilBrowna9701a32005-11-08 21:39:34 -0800751 if (atomic_dec_and_test(&mddev->pending_writes))
752 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700753 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700754}
755
NeilBrownfd01b882011-10-11 16:47:53 +1100756void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757 sector_t sector, int size, struct page *page)
758{
759 /* write first size bytes of page to sector of rdev
760 * Increment mddev->pending_writes before returning
761 * and decrement it on completion, waking up sb_wait
762 * if zero is reached.
763 * If an error occurred, call md_error
764 */
NeilBrowna167f662010-10-26 18:31:13 +1100765 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700766
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100767 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700768 bio->bi_sector = sector;
769 bio_add_page(bio, page, size, 0);
770 bio->bi_private = rdev;
771 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800772
NeilBrown7bfa19f2005-06-21 17:17:28 -0700773 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000774 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800775}
776
NeilBrownfd01b882011-10-11 16:47:53 +1100777void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800778{
Tejun Heoe9c74692010-09-03 11:56:18 +0200779 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800780 DEFINE_WAIT(wq);
781 for(;;) {
782 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
783 if (atomic_read(&mddev->pending_writes)==0)
784 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800785 schedule();
786 }
787 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700788}
789
NeilBrown6712ecf2007-09-27 12:47:43 +0200790static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
NeilBrown3cb03002011-10-11 16:45:26 +1100795int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100796 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
NeilBrowna167f662010-10-26 18:31:13 +1100798 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct completion event;
800 int ret;
801
Jens Axboe721a9602011-03-09 11:56:30 +0100802 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100804 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
805 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100806 if (metadata_op)
807 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000808 else if (rdev->mddev->reshape_position != MaxSector &&
809 (rdev->mddev->reshape_backwards ==
810 (sector >= rdev->mddev->reshape_position)))
811 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100812 else
813 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 bio_add_page(bio, page, size, 0);
815 init_completion(&event);
816 bio->bi_private = &event;
817 bio->bi_end_io = bi_complete;
818 submit_bio(rw, bio);
819 wait_for_completion(&event);
820
821 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
822 bio_put(bio);
823 return ret;
824}
NeilBrowna8745db2006-01-06 00:20:34 -0800825EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
NeilBrown3cb03002011-10-11 16:45:26 +1100827static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 char b[BDEVNAME_SIZE];
830 if (!rdev->sb_page) {
831 MD_BUG();
832 return -EINVAL;
833 }
834 if (rdev->sb_loaded)
835 return 0;
836
837
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100838 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto fail;
840 rdev->sb_loaded = 1;
841 return 0;
842
843fail:
844 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
845 bdevname(rdev->bdev,b));
846 return -EINVAL;
847}
848
849static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
850{
Andre Noll05710462008-07-11 22:02:20 +1000851 return sb1->set_uuid0 == sb2->set_uuid0 &&
852 sb1->set_uuid1 == sb2->set_uuid1 &&
853 sb1->set_uuid2 == sb2->set_uuid2 &&
854 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
858{
859 int ret;
860 mdp_super_t *tmp1, *tmp2;
861
862 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
863 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
864
865 if (!tmp1 || !tmp2) {
866 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100867 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto abort;
869 }
870
871 *tmp1 = *sb1;
872 *tmp2 = *sb2;
873
874 /*
875 * nr_disks is not constant
876 */
877 tmp1->nr_disks = 0;
878 tmp2->nr_disks = 0;
879
Andre Nollce0c8e02008-07-11 22:02:20 +1000880 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700882 kfree(tmp1);
883 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return ret;
885}
886
NeilBrown4d167f02007-05-09 02:35:37 -0700887
888static u32 md_csum_fold(u32 csum)
889{
890 csum = (csum & 0xffff) + (csum >> 16);
891 return (csum & 0xffff) + (csum >> 16);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static unsigned int calc_sb_csum(mdp_super_t * sb)
895{
NeilBrown4d167f02007-05-09 02:35:37 -0700896 u64 newcsum = 0;
897 u32 *sb32 = (u32*)sb;
898 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned int disk_csum, csum;
900
901 disk_csum = sb->sb_csum;
902 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700903
904 for (i = 0; i < MD_SB_BYTES/4 ; i++)
905 newcsum += sb32[i];
906 csum = (newcsum & 0xffffffff) + (newcsum>>32);
907
908
909#ifdef CONFIG_ALPHA
910 /* This used to use csum_partial, which was wrong for several
911 * reasons including that different results are returned on
912 * different architectures. It isn't critical that we get exactly
913 * the same return value as before (we always csum_fold before
914 * testing, and that removes any differences). However as we
915 * know that csum_partial always returned a 16bit value on
916 * alphas, do a fold to maximise conformity to previous behaviour.
917 */
918 sb->sb_csum = md_csum_fold(disk_csum);
919#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return csum;
923}
924
925
926/*
927 * Handle superblock details.
928 * We want to be able to handle multiple superblock formats
929 * so we have a common interface to them all, and an array of
930 * different handlers.
931 * We rely on user-space to write the initial superblock, and support
932 * reading and updating of superblocks.
933 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100934 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * loads and validates a superblock on dev.
936 * if refdev != NULL, compare superblocks on both devices
937 * Return:
938 * 0 - dev has a superblock that is compatible with refdev
939 * 1 - dev has a superblock that is compatible and newer than refdev
940 * so dev should be used as the refdev in future
941 * -EINVAL superblock incompatible or invalid
942 * -othererror e.g. -EIO
943 *
NeilBrownfd01b882011-10-11 16:47:53 +1100944 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * Verify that dev is acceptable into mddev.
946 * The first time, mddev->raid_disks will be 0, and data from
947 * dev should be merged in. Subsequent calls check that dev
948 * is new enough. Return 0 or -EINVAL
949 *
NeilBrownfd01b882011-10-11 16:47:53 +1100950 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * Update the superblock for rdev with data in mddev
952 * This does not write to disc.
953 *
954 */
955
956struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000957 char *name;
958 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000959 int (*load_super)(struct md_rdev *rdev,
960 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000961 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000962 int (*validate_super)(struct mddev *mddev,
963 struct md_rdev *rdev);
964 void (*sync_super)(struct mddev *mddev,
965 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100966 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000967 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000968 int (*allow_new_offset)(struct md_rdev *rdev,
969 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970};
971
972/*
Andre Noll0894cc32009-06-18 08:49:23 +1000973 * Check that the given mddev has no bitmap.
974 *
975 * This function is called from the run method of all personalities that do not
976 * support bitmaps. It prints an error message and returns non-zero if mddev
977 * has a bitmap. Otherwise, it returns 0.
978 *
979 */
NeilBrownfd01b882011-10-11 16:47:53 +1100980int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000981{
NeilBrownc3d97142009-12-14 12:49:52 +1100982 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000983 return 0;
984 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
985 mdname(mddev), mddev->pers->name);
986 return 1;
987}
988EXPORT_SYMBOL(md_check_no_bitmap);
989
990/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * load_super for 0.90.0
992 */
NeilBrown3cb03002011-10-11 16:45:26 +1100993static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
996 mdp_super_t *sb;
997 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
Andre Noll0f420352008-07-11 22:02:23 +10001000 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * it's at the end of the disk.
1002 *
1003 * It also happens to be a multiple of 4Kb.
1004 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001005 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
NeilBrown0002b272005-09-09 16:23:53 -07001007 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (ret) return ret;
1009
1010 ret = -EINVAL;
1011
1012 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001013 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (sb->md_magic != MD_SB_MAGIC) {
1016 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1017 b);
1018 goto abort;
1019 }
1020
1021 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001022 sb->minor_version < 90 ||
1023 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1025 sb->major_version, sb->minor_version,
1026 b);
1027 goto abort;
1028 }
1029
1030 if (sb->raid_disks <= 0)
1031 goto abort;
1032
NeilBrown4d167f02007-05-09 02:35:37 -07001033 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1035 b);
1036 goto abort;
1037 }
1038
1039 rdev->preferred_minor = sb->md_minor;
1040 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001041 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001042 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001043 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (sb->level == LEVEL_MULTIPATH)
1046 rdev->desc_nr = -1;
1047 else
1048 rdev->desc_nr = sb->this_disk.number;
1049
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001050 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001054 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!uuid_equal(refsb, sb)) {
1056 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1057 b, bdevname(refdev->bdev,b2));
1058 goto abort;
1059 }
1060 if (!sb_equal(refsb, sb)) {
1061 printk(KERN_WARNING "md: %s has same UUID"
1062 " but different superblock to %s\n",
1063 b, bdevname(refdev->bdev, b2));
1064 goto abort;
1065 }
1066 ev1 = md_event(sb);
1067 ev2 = md_event(refsb);
1068 if (ev1 > ev2)
1069 ret = 1;
1070 else
1071 ret = 0;
1072 }
NeilBrown8190e752009-06-18 08:48:58 +10001073 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001074 /* Limit to 4TB as metadata cannot record more than that.
1075 * (not needed for Linear and RAID0 as metadata doesn't
1076 * record this size)
1077 */
1078 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001079 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
NeilBrown27a7b262011-09-10 17:21:28 +10001081 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001082 /* "this cannot possibly happen" ... */
1083 ret = -EINVAL;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 abort:
1086 return ret;
1087}
1088
1089/*
1090 * validate_super for 0.90.0
1091 */
NeilBrownfd01b882011-10-11 16:47:53 +11001092static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001095 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001096 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
NeilBrown41158c72005-06-21 17:17:25 -07001098 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001099 clear_bit(Faulty, &rdev->flags);
1100 clear_bit(In_sync, &rdev->flags);
1101 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (mddev->raid_disks == 0) {
1104 mddev->major_version = 0;
1105 mddev->minor_version = sb->minor_version;
1106 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001107 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001108 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 mddev->ctime = sb->ctime;
1110 mddev->utime = sb->utime;
1111 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001112 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 mddev->layout = sb->layout;
1114 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001115 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001116 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001117 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001118 mddev->bitmap_info.space = 0;
1119 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001120 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001121 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001122 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
NeilBrownf6705572006-03-27 01:18:11 -08001124 if (mddev->minor_version >= 91) {
1125 mddev->reshape_position = sb->reshape_position;
1126 mddev->delta_disks = sb->delta_disks;
1127 mddev->new_level = sb->new_level;
1128 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001129 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001130 if (mddev->delta_disks < 0)
1131 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001132 } else {
1133 mddev->reshape_position = MaxSector;
1134 mddev->delta_disks = 0;
1135 mddev->new_level = mddev->level;
1136 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001137 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001138 }
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (sb->state & (1<<MD_SB_CLEAN))
1141 mddev->recovery_cp = MaxSector;
1142 else {
1143 if (sb->events_hi == sb->cp_events_hi &&
1144 sb->events_lo == sb->cp_events_lo) {
1145 mddev->recovery_cp = sb->recovery_cp;
1146 } else
1147 mddev->recovery_cp = 0;
1148 }
1149
1150 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1151 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1152 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1153 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1154
1155 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001156
1157 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001158 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001159 mddev->bitmap_info.offset =
1160 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001161 mddev->bitmap_info.space =
1162 mddev->bitmap_info.space;
1163 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001164
NeilBrown41158c72005-06-21 17:17:25 -07001165 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001166 /* Insist on good event counter while assembling, except
1167 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001169 if (sb->disks[rdev->desc_nr].state & (
1170 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1171 if (ev1 < mddev->events)
1172 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001173 } else if (mddev->bitmap) {
1174 /* if adding to array with a bitmap, then we can accept an
1175 * older device ... but not too old.
1176 */
NeilBrown41158c72005-06-21 17:17:25 -07001177 if (ev1 < mddev->bitmap->events_cleared)
1178 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001179 } else {
1180 if (ev1 < mddev->events)
1181 /* just a hot-add of a new device, leave raid_disk at -1 */
1182 return 0;
1183 }
NeilBrown41158c72005-06-21 17:17:25 -07001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 desc = sb->disks + rdev->desc_nr;
1187
1188 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001190 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1191 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001192 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001194 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1195 /* active but not in sync implies recovery up to
1196 * reshape position. We don't know exactly where
1197 * that is, so set to zero for now */
1198 if (mddev->minor_version >= 91) {
1199 rdev->recovery_offset = 0;
1200 rdev->raid_disk = desc->raid_disk;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001203 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1204 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001205 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001206 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return 0;
1208}
1209
1210/*
1211 * sync_super for 0.90.0
1212 */
NeilBrownfd01b882011-10-11 16:47:53 +11001213static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001216 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* make rdev->sb match mddev data..
1221 *
1222 * 1/ zero out disks
1223 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1224 * 3/ any empty disks < next_spare become removed
1225 *
1226 * disks[0] gets initialised to REMOVED because
1227 * we cannot be sure from other fields if it has
1228 * been initialised or not.
1229 */
1230 int i;
1231 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1232
NeilBrown61181562005-09-09 16:24:02 -07001233 rdev->sb_size = MD_SB_BYTES;
1234
Namhyung Kim65a06f062011-07-27 11:00:36 +10001235 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 memset(sb, 0, sizeof(*sb));
1238
1239 sb->md_magic = MD_SB_MAGIC;
1240 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 sb->patch_version = mddev->patch_version;
1242 sb->gvalid_words = 0; /* ignored */
1243 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1244 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1245 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1246 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1247
1248 sb->ctime = mddev->ctime;
1249 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001250 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 sb->raid_disks = mddev->raid_disks;
1252 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001253 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sb->utime = mddev->utime;
1255 sb->state = 0;
1256 sb->events_hi = (mddev->events>>32);
1257 sb->events_lo = (u32)mddev->events;
1258
NeilBrownf6705572006-03-27 01:18:11 -08001259 if (mddev->reshape_position == MaxSector)
1260 sb->minor_version = 90;
1261 else {
1262 sb->minor_version = 91;
1263 sb->reshape_position = mddev->reshape_position;
1264 sb->new_level = mddev->new_level;
1265 sb->delta_disks = mddev->delta_disks;
1266 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001267 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001268 }
1269 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (mddev->in_sync)
1271 {
1272 sb->recovery_cp = mddev->recovery_cp;
1273 sb->cp_events_hi = (mddev->events>>32);
1274 sb->cp_events_lo = (u32)mddev->events;
1275 if (mddev->recovery_cp == MaxSector)
1276 sb->state = (1<< MD_SB_CLEAN);
1277 } else
1278 sb->recovery_cp = 0;
1279
1280 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001281 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
NeilBrownc3d97142009-12-14 12:49:52 +11001283 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001284 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001287 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001289 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001290 int is_active = test_bit(In_sync, &rdev2->flags);
1291
1292 if (rdev2->raid_disk >= 0 &&
1293 sb->minor_version >= 91)
1294 /* we have nowhere to store the recovery_offset,
1295 * but if it is not below the reshape_position,
1296 * we can piggy-back on that.
1297 */
1298 is_active = 1;
1299 if (rdev2->raid_disk < 0 ||
1300 test_bit(Faulty, &rdev2->flags))
1301 is_active = 0;
1302 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001303 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001305 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001306 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 d = &sb->disks[rdev2->desc_nr];
1308 nr_disks++;
1309 d->number = rdev2->desc_nr;
1310 d->major = MAJOR(rdev2->bdev->bd_dev);
1311 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001312 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 d->raid_disk = rdev2->raid_disk;
1314 else
1315 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001316 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001318 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001320 if (test_bit(In_sync, &rdev2->flags))
1321 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 active++;
1323 working++;
1324 } else {
1325 d->state = 0;
1326 spare++;
1327 working++;
1328 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001329 if (test_bit(WriteMostly, &rdev2->flags))
1330 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* now set the "removed" and "faulty" bits on any missing devices */
1333 for (i=0 ; i < mddev->raid_disks ; i++) {
1334 mdp_disk_t *d = &sb->disks[i];
1335 if (d->state == 0 && d->number == 0) {
1336 d->number = i;
1337 d->raid_disk = i;
1338 d->state = (1<<MD_DISK_REMOVED);
1339 d->state |= (1<<MD_DISK_FAULTY);
1340 failed++;
1341 }
1342 }
1343 sb->nr_disks = nr_disks;
1344 sb->active_disks = active;
1345 sb->working_disks = working;
1346 sb->failed_disks = failed;
1347 sb->spare_disks = spare;
1348
1349 sb->this_disk = sb->disks[rdev->desc_nr];
1350 sb->sb_csum = calc_sb_csum(sb);
1351}
1352
1353/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354 * rdev_size_change for 0.90.0
1355 */
1356static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001357super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001358{
Andre Noll58c0fed2009-03-31 14:33:13 +11001359 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001360 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001361 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001362 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001363 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001364 if (!num_sectors || num_sectors > rdev->sb_start)
1365 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001366 /* Limit to 4TB as metadata cannot record more than that.
1367 * 4TB == 2^32 KB, or 2*2^32 sectors.
1368 */
NeilBrown667a5312012-08-16 16:46:12 +10001369 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001370 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001371 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001372 rdev->sb_page);
1373 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001374 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001375}
1376
NeilBrownc6563a82012-05-21 09:27:00 +10001377static int
1378super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1379{
1380 /* non-zero offset changes not possible with v0.90 */
1381 return new_offset == 0;
1382}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001383
1384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * version 1 superblock
1386 */
1387
NeilBrown1c05b4b2006-10-21 10:24:08 -07001388static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001390 __le32 disk_csum;
1391 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 unsigned long long newcsum;
1393 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001394 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int i;
1396
1397 disk_csum = sb->sb_csum;
1398 sb->sb_csum = 0;
1399 newcsum = 0;
1400 for (i=0; size>=4; size -= 4 )
1401 newcsum += le32_to_cpu(*isuper++);
1402
1403 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001404 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1407 sb->sb_csum = disk_csum;
1408 return cpu_to_le32(csum);
1409}
1410
NeilBrown2699b672011-07-28 11:31:47 +10001411static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1412 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001413static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct mdp_superblock_1 *sb;
1416 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001417 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001418 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001420 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
Andre Noll0f420352008-07-11 22:02:23 +10001423 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * It is always aligned to a 4K boundary and
1425 * depeding on minor_version, it can be:
1426 * 0: At least 8K, but less than 12K, from end of device
1427 * 1: At start of device
1428 * 2: 4K from start of device.
1429 */
1430 switch(minor_version) {
1431 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001432 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001433 sb_start -= 8*2;
1434 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001437 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001440 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 break;
1442 default:
1443 return -EINVAL;
1444 }
Andre Noll0f420352008-07-11 22:02:23 +10001445 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
NeilBrown0002b272005-09-09 16:23:53 -07001447 /* superblock is rarely larger than 1K, but it can be larger,
1448 * and it is safe to read 4k, so we do that
1449 */
1450 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (ret) return ret;
1452
1453
Namhyung Kim65a06f062011-07-27 11:00:36 +10001454 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1457 sb->major_version != cpu_to_le32(1) ||
1458 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001459 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001460 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
1462
1463 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1464 printk("md: invalid superblock checksum on %s\n",
1465 bdevname(rdev->bdev,b));
1466 return -EINVAL;
1467 }
1468 if (le64_to_cpu(sb->data_size) < 10) {
1469 printk("md: data_size too small on %s\n",
1470 bdevname(rdev->bdev,b));
1471 return -EINVAL;
1472 }
NeilBrownc6563a82012-05-21 09:27:00 +10001473 if (sb->pad0 ||
1474 sb->pad3[0] ||
1475 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1476 /* Some padding is non-zero, might be a new feature */
1477 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 rdev->preferred_minor = 0xffff;
1480 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001481 rdev->new_data_offset = rdev->data_offset;
1482 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1483 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1484 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001485 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
NeilBrown0002b272005-09-09 16:23:53 -07001487 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001488 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001489 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001490 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1491
1492 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001493 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001494 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001495 if (minor_version
1496 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1497 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001498
NeilBrown31b65a02006-07-10 04:44:14 -07001499 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1500 rdev->desc_nr = -1;
1501 else
1502 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1503
NeilBrown2699b672011-07-28 11:31:47 +10001504 if (!rdev->bb_page) {
1505 rdev->bb_page = alloc_page(GFP_KERNEL);
1506 if (!rdev->bb_page)
1507 return -ENOMEM;
1508 }
1509 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1510 rdev->badblocks.count == 0) {
1511 /* need to load the bad block list.
1512 * Currently we limit it to one page.
1513 */
1514 s32 offset;
1515 sector_t bb_sector;
1516 u64 *bbp;
1517 int i;
1518 int sectors = le16_to_cpu(sb->bblog_size);
1519 if (sectors > (PAGE_SIZE / 512))
1520 return -EINVAL;
1521 offset = le32_to_cpu(sb->bblog_offset);
1522 if (offset == 0)
1523 return -EINVAL;
1524 bb_sector = (long long)offset;
1525 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1526 rdev->bb_page, READ, true))
1527 return -EIO;
1528 bbp = (u64 *)page_address(rdev->bb_page);
1529 rdev->badblocks.shift = sb->bblog_shift;
1530 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1531 u64 bb = le64_to_cpu(*bbp);
1532 int count = bb & (0x3ff);
1533 u64 sector = bb >> 10;
1534 sector <<= sb->bblog_shift;
1535 count <<= sb->bblog_shift;
1536 if (bb + 1 == 0)
1537 break;
1538 if (md_set_badblocks(&rdev->badblocks,
1539 sector, count, 1) == 0)
1540 return -EINVAL;
1541 }
1542 } else if (sb->bblog_offset == 0)
1543 rdev->badblocks.shift = -1;
1544
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001545 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001546 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001549 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1552 sb->level != refsb->level ||
1553 sb->layout != refsb->layout ||
1554 sb->chunksize != refsb->chunksize) {
1555 printk(KERN_WARNING "md: %s has strangely different"
1556 " superblock to %s\n",
1557 bdevname(rdev->bdev,b),
1558 bdevname(refdev->bdev,b2));
1559 return -EINVAL;
1560 }
1561 ev1 = le64_to_cpu(sb->events);
1562 ev2 = le64_to_cpu(refsb->events);
1563
1564 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001565 ret = 1;
1566 else
1567 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
NeilBrownc6563a82012-05-21 09:27:00 +10001569 if (minor_version) {
1570 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1571 sectors -= rdev->data_offset;
1572 } else
1573 sectors = rdev->sb_start;
1574 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001576 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
NeilBrownfd01b882011-10-11 16:47:53 +11001580static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001582 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001583 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
NeilBrown41158c72005-06-21 17:17:25 -07001585 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001586 clear_bit(Faulty, &rdev->flags);
1587 clear_bit(In_sync, &rdev->flags);
1588 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (mddev->raid_disks == 0) {
1591 mddev->major_version = 1;
1592 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001593 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001594 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1596 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1597 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001598 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 mddev->layout = le32_to_cpu(sb->layout);
1600 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001601 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001602 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001603 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001604 mddev->bitmap_info.space = 0;
1605 /* Default location for bitmap is 1K after superblock
1606 * using 3K - total of 4K
1607 */
NeilBrownc3d97142009-12-14 12:49:52 +11001608 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001609 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001610 mddev->reshape_backwards = 0;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1613 memcpy(mddev->uuid, sb->set_uuid, 16);
1614
1615 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001616
NeilBrown71c08052005-09-09 16:23:51 -07001617 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001618 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001619 mddev->bitmap_info.offset =
1620 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001621 /* Metadata doesn't record how much space is available.
1622 * For 1.0, we assume we can use up to the superblock
1623 * if before, else to 4K beyond superblock.
1624 * For others, assume no change is possible.
1625 */
1626 if (mddev->minor_version > 0)
1627 mddev->bitmap_info.space = 0;
1628 else if (mddev->bitmap_info.offset > 0)
1629 mddev->bitmap_info.space =
1630 8 - mddev->bitmap_info.offset;
1631 else
1632 mddev->bitmap_info.space =
1633 -mddev->bitmap_info.offset;
1634 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001635
NeilBrownf6705572006-03-27 01:18:11 -08001636 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1637 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1638 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1639 mddev->new_level = le32_to_cpu(sb->new_level);
1640 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001641 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001642 if (mddev->delta_disks < 0 ||
1643 (mddev->delta_disks == 0 &&
1644 (le32_to_cpu(sb->feature_map)
1645 & MD_FEATURE_RESHAPE_BACKWARDS)))
1646 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001647 } else {
1648 mddev->reshape_position = MaxSector;
1649 mddev->delta_disks = 0;
1650 mddev->new_level = mddev->level;
1651 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001652 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001653 }
1654
NeilBrown41158c72005-06-21 17:17:25 -07001655 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001656 /* Insist of good event counter while assembling, except for
1657 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001659 if (rdev->desc_nr >= 0 &&
1660 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1661 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1662 if (ev1 < mddev->events)
1663 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001664 } else if (mddev->bitmap) {
1665 /* If adding to array with a bitmap, then we can accept an
1666 * older device, but not too old.
1667 */
NeilBrown41158c72005-06-21 17:17:25 -07001668 if (ev1 < mddev->bitmap->events_cleared)
1669 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001670 } else {
1671 if (ev1 < mddev->events)
1672 /* just a hot-add of a new device, leave raid_disk at -1 */
1673 return 0;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (mddev->level != LEVEL_MULTIPATH) {
1676 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001677 if (rdev->desc_nr < 0 ||
1678 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1679 role = 0xffff;
1680 rdev->desc_nr = -1;
1681 } else
1682 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 switch(role) {
1684 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001687 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001690 if ((le32_to_cpu(sb->feature_map) &
1691 MD_FEATURE_RECOVERY_OFFSET))
1692 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1693 else
1694 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 rdev->raid_disk = role;
1696 break;
1697 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001698 if (sb->devflags & WriteMostly1)
1699 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001700 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1701 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001702 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001703 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return 0;
1706}
1707
NeilBrownfd01b882011-10-11 16:47:53 +11001708static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001711 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int max_dev, i;
1713 /* make rdev->sb match mddev and rdev data. */
1714
Namhyung Kim65a06f062011-07-27 11:00:36 +10001715 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 sb->feature_map = 0;
1718 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001719 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 memset(sb->pad3, 0, sizeof(sb->pad3));
1721
1722 sb->utime = cpu_to_le64((__u64)mddev->utime);
1723 sb->events = cpu_to_le64(mddev->events);
1724 if (mddev->in_sync)
1725 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1726 else
1727 sb->resync_offset = cpu_to_le64(0);
1728
NeilBrown1c05b4b2006-10-21 10:24:08 -07001729 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001730
NeilBrownf0ca3402006-02-02 14:28:04 -08001731 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001732 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001733 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001734 sb->level = cpu_to_le32(mddev->level);
1735 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001736
NeilBrownaeb9b212011-08-25 14:43:08 +10001737 if (test_bit(WriteMostly, &rdev->flags))
1738 sb->devflags |= WriteMostly1;
1739 else
1740 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001741 sb->data_offset = cpu_to_le64(rdev->data_offset);
1742 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001743
NeilBrownc3d97142009-12-14 12:49:52 +11001744 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1745 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001746 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001747 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001748
1749 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001750 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001751 sb->feature_map |=
1752 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1753 sb->recovery_offset =
1754 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001755 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001756 if (test_bit(Replacement, &rdev->flags))
1757 sb->feature_map |=
1758 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001759
NeilBrownf6705572006-03-27 01:18:11 -08001760 if (mddev->reshape_position != MaxSector) {
1761 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1762 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1763 sb->new_layout = cpu_to_le32(mddev->new_layout);
1764 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1765 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001766 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001767 if (mddev->delta_disks == 0 &&
1768 mddev->reshape_backwards)
1769 sb->feature_map
1770 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001771 if (rdev->new_data_offset != rdev->data_offset) {
1772 sb->feature_map
1773 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1774 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1775 - rdev->data_offset));
1776 }
NeilBrownf6705572006-03-27 01:18:11 -08001777 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001778
NeilBrown2699b672011-07-28 11:31:47 +10001779 if (rdev->badblocks.count == 0)
1780 /* Nothing to do for bad blocks*/ ;
1781 else if (sb->bblog_offset == 0)
1782 /* Cannot record bad blocks on this device */
1783 md_error(mddev, rdev);
1784 else {
1785 struct badblocks *bb = &rdev->badblocks;
1786 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1787 u64 *p = bb->page;
1788 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1789 if (bb->changed) {
1790 unsigned seq;
1791
1792retry:
1793 seq = read_seqbegin(&bb->lock);
1794
1795 memset(bbp, 0xff, PAGE_SIZE);
1796
1797 for (i = 0 ; i < bb->count ; i++) {
1798 u64 internal_bb = *p++;
1799 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1800 | BB_LEN(internal_bb));
1801 *bbp++ = cpu_to_le64(store_bb);
1802 }
NeilBrownd0962932012-03-19 12:46:41 +11001803 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001804 if (read_seqretry(&bb->lock, seq))
1805 goto retry;
1806
1807 bb->sector = (rdev->sb_start +
1808 (int)le32_to_cpu(sb->bblog_offset));
1809 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001810 }
1811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001814 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (rdev2->desc_nr+1 > max_dev)
1816 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001817
NeilBrown70471da2009-08-03 10:59:57 +10001818 if (max_dev > le32_to_cpu(sb->max_dev)) {
1819 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001820 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001821 rdev->sb_size = max_dev * 2 + 256;
1822 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1823 if (rdev->sb_size & bmask)
1824 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001825 } else
1826 max_dev = le32_to_cpu(sb->max_dev);
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 for (i=0; i<max_dev;i++)
1829 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1830
NeilBrowndafb20f2012-03-19 12:46:39 +11001831 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001833 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001837 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001838 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 else
1840 sb->dev_roles[i] = cpu_to_le16(0xffff);
1841 }
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 sb->sb_csum = calc_sb_1_csum(sb);
1844}
1845
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001847super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001848{
1849 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001850 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001851 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001853 if (rdev->data_offset != rdev->new_data_offset)
1854 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001855 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001857 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001858 max_sectors -= rdev->data_offset;
1859 if (!num_sectors || num_sectors > max_sectors)
1860 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001861 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001862 /* minor version 0 with bitmap we can't move */
1863 return 0;
1864 } else {
1865 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001866 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001867 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001868 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001869 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001870 if (!num_sectors || num_sectors > max_sectors)
1871 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001872 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001874 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001875 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001876 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001878 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 rdev->sb_page);
1880 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001881 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001882
1883}
1884
1885static int
1886super_1_allow_new_offset(struct md_rdev *rdev,
1887 unsigned long long new_offset)
1888{
1889 /* All necessary checks on new >= old have been done */
1890 struct bitmap *bitmap;
1891 if (new_offset >= rdev->data_offset)
1892 return 1;
1893
1894 /* with 1.0 metadata, there is no metadata to tread on
1895 * so we can always move back */
1896 if (rdev->mddev->minor_version == 0)
1897 return 1;
1898
1899 /* otherwise we must be sure not to step on
1900 * any metadata, so stay:
1901 * 36K beyond start of superblock
1902 * beyond end of badblocks
1903 * beyond write-intent bitmap
1904 */
1905 if (rdev->sb_start + (32+4)*2 > new_offset)
1906 return 0;
1907 bitmap = rdev->mddev->bitmap;
1908 if (bitmap && !rdev->mddev->bitmap_info.file &&
1909 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001910 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001911 return 0;
1912 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1913 return 0;
1914
1915 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Adrian Bunk75c96f82005-05-05 16:16:09 -07001918static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 [0] = {
1920 .name = "0.90.0",
1921 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001922 .load_super = super_90_load,
1923 .validate_super = super_90_validate,
1924 .sync_super = super_90_sync,
1925 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001926 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 },
1928 [1] = {
1929 .name = "md-1",
1930 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001931 .load_super = super_1_load,
1932 .validate_super = super_1_validate,
1933 .sync_super = super_1_sync,
1934 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001935 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 },
1937};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrownfd01b882011-10-11 16:47:53 +11001939static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001940{
1941 if (mddev->sync_super) {
1942 mddev->sync_super(mddev, rdev);
1943 return;
1944 }
1945
1946 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1947
1948 super_types[mddev->major_version].sync_super(mddev, rdev);
1949}
1950
NeilBrownfd01b882011-10-11 16:47:53 +11001951static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
NeilBrown3cb03002011-10-11 16:45:26 +11001953 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
NeilBrown4b809912008-07-21 17:05:25 +10001955 rcu_read_lock();
1956 rdev_for_each_rcu(rdev, mddev1)
1957 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001958 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001959 rdev2->bdev->bd_contains) {
1960 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001961 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001962 }
1963 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965}
1966
1967static LIST_HEAD(pending_raid_disks);
1968
Andre Nollac5e7112009-08-03 10:59:47 +10001969/*
1970 * Try to register data integrity profile for an mddev
1971 *
1972 * This is called when an array is started and after a disk has been kicked
1973 * from the array. It only succeeds if all working and active component devices
1974 * are integrity capable with matching profiles.
1975 */
NeilBrownfd01b882011-10-11 16:47:53 +11001976int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977{
NeilBrown3cb03002011-10-11 16:45:26 +11001978 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001979
Andre Nollac5e7112009-08-03 10:59:47 +10001980 if (list_empty(&mddev->disks))
1981 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001982 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1983 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001984 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001985 /* skip spares and non-functional disks */
1986 if (test_bit(Faulty, &rdev->flags))
1987 continue;
1988 if (rdev->raid_disk < 0)
1989 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001990 if (!reference) {
1991 /* Use the first rdev as the reference */
1992 reference = rdev;
1993 continue;
1994 }
1995 /* does this rdev's profile match the reference profile? */
1996 if (blk_integrity_compare(reference->bdev->bd_disk,
1997 rdev->bdev->bd_disk) < 0)
1998 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001999 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002000 if (!reference || !bdev_get_integrity(reference->bdev))
2001 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002002 /*
2003 * All component devices are integrity capable and have matching
2004 * profiles, register the common profile for the md device.
2005 */
2006 if (blk_integrity_register(mddev->gendisk,
2007 bdev_get_integrity(reference->bdev)) != 0) {
2008 printk(KERN_ERR "md: failed to register integrity for %s\n",
2009 mdname(mddev));
2010 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002012 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2013 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2014 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2015 mdname(mddev));
2016 return -EINVAL;
2017 }
Andre Nollac5e7112009-08-03 10:59:47 +10002018 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002019}
Andre Nollac5e7112009-08-03 10:59:47 +10002020EXPORT_SYMBOL(md_integrity_register);
2021
2022/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002023void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002024{
2025 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
2026 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
2027
2028 if (!bi_mddev) /* nothing to do */
2029 return;
2030 if (rdev->raid_disk < 0) /* skip spares */
2031 return;
2032 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2033 rdev->bdev->bd_disk) >= 0)
2034 return;
2035 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2036 blk_integrity_unregister(mddev->gendisk);
2037}
2038EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002039
NeilBrownfd01b882011-10-11 16:47:53 +11002040static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002042 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002043 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002044 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002045 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if (rdev->mddev) {
2048 MD_BUG();
2049 return -EINVAL;
2050 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002051
2052 /* prevent duplicates */
2053 if (find_rdev(mddev, rdev->bdev->bd_dev))
2054 return -EEXIST;
2055
Andre Nolldd8ac332009-03-31 14:33:13 +11002056 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2057 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2058 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002059 if (mddev->pers) {
2060 /* Cannot change size, so fail
2061 * If mddev->level <= 0, then we don't care
2062 * about aligning sizes (e.g. linear)
2063 */
2064 if (mddev->level > 0)
2065 return -ENOSPC;
2066 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002067 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* Verify rdev->desc_nr is unique.
2071 * If it is -1, assign a free number, else
2072 * check number is not in use
2073 */
2074 if (rdev->desc_nr < 0) {
2075 int choice = 0;
2076 if (mddev->pers) choice = mddev->raid_disks;
2077 while (find_rdev_nr(mddev, choice))
2078 choice++;
2079 rdev->desc_nr = choice;
2080 } else {
2081 if (find_rdev_nr(mddev, rdev->desc_nr))
2082 return -EBUSY;
2083 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002084 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2085 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2086 mdname(mddev), mddev->max_disks);
2087 return -EBUSY;
2088 }
NeilBrown19133a42005-11-08 21:39:35 -08002089 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002090 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002091 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002094 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002095
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002096 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002097 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002098
Tejun Heo0762b8b2008-08-25 19:56:12 +09002099 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002100 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2101 /* failure here is OK */;
2102 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002103
NeilBrown4b809912008-07-21 17:05:25 +10002104 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002105 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002106
2107 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002108 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002111
2112 fail:
2113 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2114 b, mdname(mddev));
2115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
NeilBrown177a99b2008-02-06 01:39:56 -08002118static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002119{
NeilBrown3cb03002011-10-11 16:45:26 +11002120 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002121 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002122 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002123}
2124
NeilBrown3cb03002011-10-11 16:45:26 +11002125static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 char b[BDEVNAME_SIZE];
2128 if (!rdev->mddev) {
2129 MD_BUG();
2130 return;
2131 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002132 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002133 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2135 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002136 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002137 sysfs_put(rdev->sysfs_state);
2138 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002139 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002140 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002141 * writing to 'remove' to "dev/state". We also need
2142 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002143 */
NeilBrown4b809912008-07-21 17:05:25 +10002144 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002145 INIT_WORK(&rdev->del_work, md_delayed_delete);
2146 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002147 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
2151 * prevent the device from being mounted, repartitioned or
2152 * otherwise reused by a RAID array (or any other kernel
2153 * subsystem), by bd_claiming the device.
2154 */
NeilBrown3cb03002011-10-11 16:45:26 +11002155static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 int err = 0;
2158 struct block_device *bdev;
2159 char b[BDEVNAME_SIZE];
2160
Tejun Heod4d77622010-11-13 11:55:18 +01002161 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002162 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (IS_ERR(bdev)) {
2164 printk(KERN_ERR "md: could not open %s.\n",
2165 __bdevname(dev, b));
2166 return PTR_ERR(bdev);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 rdev->bdev = bdev;
2169 return err;
2170}
2171
NeilBrown3cb03002011-10-11 16:45:26 +11002172static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct block_device *bdev = rdev->bdev;
2175 rdev->bdev = NULL;
2176 if (!bdev)
2177 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002178 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181void md_autodetect_dev(dev_t dev);
2182
NeilBrown3cb03002011-10-11 16:45:26 +11002183static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 char b[BDEVNAME_SIZE];
2186 printk(KERN_INFO "md: export_rdev(%s)\n",
2187 bdevname(rdev->bdev,b));
2188 if (rdev->mddev)
2189 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002190 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002192 if (test_bit(AutoDetected, &rdev->flags))
2193 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194#endif
2195 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002196 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
NeilBrown3cb03002011-10-11 16:45:26 +11002199static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 unbind_rdev_from_array(rdev);
2202 export_rdev(rdev);
2203}
2204
NeilBrownfd01b882011-10-11 16:47:53 +11002205static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
NeilBrown3cb03002011-10-11 16:45:26 +11002207 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
NeilBrowndafb20f2012-03-19 12:46:39 +11002209 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!rdev->mddev) {
2211 MD_BUG();
2212 continue;
2213 }
2214 kick_rdev_from_array(rdev);
2215 }
2216 if (!list_empty(&mddev->disks))
2217 MD_BUG();
2218 mddev->raid_disks = 0;
2219 mddev->major_version = 0;
2220}
2221
2222static void print_desc(mdp_disk_t *desc)
2223{
2224 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2225 desc->major,desc->minor,desc->raid_disk,desc->state);
2226}
2227
Cheng Renquancd2ac932009-01-09 08:31:08 +11002228static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 int i;
2231
2232 printk(KERN_INFO
2233 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2234 sb->major_version, sb->minor_version, sb->patch_version,
2235 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2236 sb->ctime);
2237 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2238 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2239 sb->md_minor, sb->layout, sb->chunk_size);
2240 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2241 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2242 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2243 sb->failed_disks, sb->spare_disks,
2244 sb->sb_csum, (unsigned long)sb->events_lo);
2245
2246 printk(KERN_INFO);
2247 for (i = 0; i < MD_SB_DISKS; i++) {
2248 mdp_disk_t *desc;
2249
2250 desc = sb->disks + i;
2251 if (desc->number || desc->major || desc->minor ||
2252 desc->raid_disk || (desc->state && (desc->state != 4))) {
2253 printk(" D %2d: ", i);
2254 print_desc(desc);
2255 }
2256 }
2257 printk(KERN_INFO "md: THIS: ");
2258 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
Cheng Renquancd2ac932009-01-09 08:31:08 +11002261static void print_sb_1(struct mdp_superblock_1 *sb)
2262{
2263 __u8 *uuid;
2264
2265 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002266 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002267 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002268 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002269 le32_to_cpu(sb->major_version),
2270 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002271 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002272 sb->set_name,
2273 (unsigned long long)le64_to_cpu(sb->ctime)
2274 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2275
2276 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002277 printk(KERN_INFO
2278 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002279 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002280 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002281 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2282 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002283 le32_to_cpu(sb->level),
2284 (unsigned long long)le64_to_cpu(sb->size),
2285 le32_to_cpu(sb->raid_disks),
2286 le32_to_cpu(sb->layout),
2287 le32_to_cpu(sb->chunksize),
2288 (unsigned long long)le64_to_cpu(sb->data_offset),
2289 (unsigned long long)le64_to_cpu(sb->data_size),
2290 (unsigned long long)le64_to_cpu(sb->super_offset),
2291 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2292 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002293 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002294 sb->devflags,
2295 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2296 (unsigned long long)le64_to_cpu(sb->events),
2297 (unsigned long long)le64_to_cpu(sb->resync_offset),
2298 le32_to_cpu(sb->sb_csum),
2299 le32_to_cpu(sb->max_dev)
2300 );
2301}
2302
NeilBrown3cb03002011-10-11 16:45:26 +11002303static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002306 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2307 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002308 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2309 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002311 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2312 switch (major_version) {
2313 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002314 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002315 break;
2316 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002317 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002318 break;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 } else
2321 printk(KERN_INFO "md: no rdev superblock!\n");
2322}
2323
Adrian Bunk5e563412006-06-26 00:27:42 -07002324static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002326 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002327 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002328 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 char b[BDEVNAME_SIZE];
2330
2331 printk("\n");
2332 printk("md: **********************************\n");
2333 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2334 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002335 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
NeilBrown32a76272005-06-21 17:17:14 -07002337 if (mddev->bitmap)
2338 bitmap_print_sb(mddev->bitmap);
2339 else
2340 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002341 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 printk("<%s>", bdevname(rdev->bdev,b));
2343 printk("\n");
2344
NeilBrowndafb20f2012-03-19 12:46:39 +11002345 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002346 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 printk("md: **********************************\n");
2349 printk("\n");
2350}
2351
2352
NeilBrownfd01b882011-10-11 16:47:53 +11002353static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
NeilBrown42543762006-06-26 00:27:57 -07002355 /* Update each superblock (in-memory image), but
2356 * if we are allowed to, skip spares which already
2357 * have the right event counter, or have one earlier
2358 * (which would mean they aren't being marked as dirty
2359 * with the rest of the array)
2360 */
NeilBrown3cb03002011-10-11 16:45:26 +11002361 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002362 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002363 if (rdev->sb_events == mddev->events ||
2364 (nospares &&
2365 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002366 rdev->sb_events+1 == mddev->events)) {
2367 /* Don't update this superblock */
2368 rdev->sb_loaded = 2;
2369 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002370 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002371 rdev->sb_loaded = 1;
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374}
2375
NeilBrownfd01b882011-10-11 16:47:53 +11002376static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
NeilBrown3cb03002011-10-11 16:45:26 +11002378 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002379 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002380 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002381 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002384 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002385 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002386 if (rdev->raid_disk >= 0 &&
2387 mddev->delta_disks >= 0 &&
2388 !test_bit(In_sync, &rdev->flags) &&
2389 mddev->curr_resync_completed > rdev->recovery_offset)
2390 rdev->recovery_offset = mddev->curr_resync_completed;
2391
2392 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002393 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002394 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002395 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002396 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002397 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002398 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002399 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002400 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002401 md_ack_all_badblocks(&rdev->badblocks);
2402 md_error(mddev, rdev);
2403 }
2404 clear_bit(Blocked, &rdev->flags);
2405 clear_bit(BlockedBadBlocks, &rdev->flags);
2406 wake_up(&rdev->blocked_wait);
2407 }
2408 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002409 wake_up(&mddev->sb_wait);
2410 return;
2411 }
2412
NeilBrowna9701a32005-11-08 21:39:34 -08002413 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002414
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002415 mddev->utime = get_seconds();
2416
NeilBrown850b2b42006-10-03 01:15:46 -07002417 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2418 force_change = 1;
2419 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2420 /* just a clean<-> dirty transition, possibly leave spares alone,
2421 * though if events isn't the right even/odd, we will have to do
2422 * spares after all
2423 */
2424 nospares = 1;
2425 if (force_change)
2426 nospares = 0;
2427 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002428 /* If the array is degraded, then skipping spares is both
2429 * dangerous and fairly pointless.
2430 * Dangerous because a device that was removed from the array
2431 * might have a event_count that still looks up-to-date,
2432 * so it can be re-added without a resync.
2433 * Pointless because if there are any spares to skip,
2434 * then a recovery will happen and soon that array won't
2435 * be degraded any more and the spare can go back to sleep then.
2436 */
NeilBrown850b2b42006-10-03 01:15:46 -07002437 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002438
NeilBrown06d91a52005-06-21 17:17:12 -07002439 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002440
2441 /* If this is just a dirty<->clean transition, and the array is clean
2442 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002443 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002444 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002445 && mddev->can_decrease_events
2446 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002447 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002448 mddev->can_decrease_events = 0;
2449 } else {
NeilBrown42543762006-06-26 00:27:57 -07002450 /* otherwise we have to go forward and ... */
2451 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002452 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 if (!mddev->events) {
2456 /*
2457 * oops, this 64-bit counter should never wrap.
2458 * Either we are in around ~1 trillion A.C., assuming
2459 * 1 reboot per second, or we have a bug:
2460 */
2461 MD_BUG();
2462 mddev->events --;
2463 }
NeilBrown2699b672011-07-28 11:31:47 +10002464
NeilBrowndafb20f2012-03-19 12:46:39 +11002465 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002466 if (rdev->badblocks.changed)
2467 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002468 if (test_bit(Faulty, &rdev->flags))
2469 set_bit(FaultRecorded, &rdev->flags);
2470 }
NeilBrown2699b672011-07-28 11:31:47 +10002471
NeilBrowne6910632008-02-06 01:39:51 -08002472 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002473 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
NeilBrown36a4e1f2011-10-07 14:23:17 +11002475 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2476 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
NeilBrown4ad13662007-07-17 04:06:13 -07002478 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002479 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002481
NeilBrown42543762006-06-26 00:27:57 -07002482 if (rdev->sb_loaded != 1)
2483 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002485 if (!test_bit(Faulty, &rdev->flags) &&
2486 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002487 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002488 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002489 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002490 pr_debug("md: (write) %s's sb offset: %llu\n",
2491 bdevname(rdev->bdev, b),
2492 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002493 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002494 if (rdev->badblocks.size) {
2495 md_super_write(mddev, rdev,
2496 rdev->badblocks.sector,
2497 rdev->badblocks.size << 9,
2498 rdev->bb_page);
2499 rdev->badblocks.size = 0;
2500 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002501
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002502 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002503 pr_debug("md: %s (skipping faulty)\n",
2504 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002505 else
2506 pr_debug("(skipping incremental s/r ");
2507
NeilBrown7bfa19f2005-06-21 17:17:28 -07002508 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /* only need to write one superblock... */
2510 break;
2511 }
NeilBrowna9701a32005-11-08 21:39:34 -08002512 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002513 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002514
NeilBrowna9701a32005-11-08 21:39:34 -08002515 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002516 if (mddev->in_sync != sync_req ||
2517 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002518 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002519 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002520 goto repeat;
2521 }
NeilBrown850b2b42006-10-03 01:15:46 -07002522 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002523 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002524 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002525 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2526 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002527
NeilBrowndafb20f2012-03-19 12:46:39 +11002528 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002529 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2530 clear_bit(Blocked, &rdev->flags);
2531
2532 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002533 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002534 clear_bit(BlockedBadBlocks, &rdev->flags);
2535 wake_up(&rdev->blocked_wait);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Andre Noll7f6ce762008-03-23 18:34:54 +01002539/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002540 * We want to accept with case. For this we use cmd_match.
2541 */
2542static int cmd_match(const char *cmd, const char *str)
2543{
2544 /* See if cmd, written into a sysfs file, matches
2545 * str. They must either be the same, or cmd can
2546 * have a trailing newline
2547 */
2548 while (*cmd && *str && *cmd == *str) {
2549 cmd++;
2550 str++;
2551 }
2552 if (*cmd == '\n')
2553 cmd++;
2554 if (*str || *cmd)
2555 return 0;
2556 return 1;
2557}
2558
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559struct rdev_sysfs_entry {
2560 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002561 ssize_t (*show)(struct md_rdev *, char *);
2562 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002563};
2564
2565static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002566state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002567{
2568 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002569 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002570
NeilBrownde393cd2011-07-28 11:31:48 +10002571 if (test_bit(Faulty, &rdev->flags) ||
2572 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002573 len+= sprintf(page+len, "%sfaulty",sep);
2574 sep = ",";
2575 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002576 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577 len += sprintf(page+len, "%sin_sync",sep);
2578 sep = ",";
2579 }
NeilBrownf6556752006-06-26 00:28:01 -07002580 if (test_bit(WriteMostly, &rdev->flags)) {
2581 len += sprintf(page+len, "%swrite_mostly",sep);
2582 sep = ",";
2583 }
NeilBrownde393cd2011-07-28 11:31:48 +10002584 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002585 (rdev->badblocks.unacked_exist
2586 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002587 len += sprintf(page+len, "%sblocked", sep);
2588 sep = ",";
2589 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002590 if (!test_bit(Faulty, &rdev->flags) &&
2591 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002592 len += sprintf(page+len, "%sspare", sep);
2593 sep = ",";
2594 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002595 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2596 len += sprintf(page+len, "%swrite_error", sep);
2597 sep = ",";
2598 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002599 if (test_bit(WantReplacement, &rdev->flags)) {
2600 len += sprintf(page+len, "%swant_replacement", sep);
2601 sep = ",";
2602 }
2603 if (test_bit(Replacement, &rdev->flags)) {
2604 len += sprintf(page+len, "%sreplacement", sep);
2605 sep = ",";
2606 }
2607
NeilBrown86e6ffd2005-11-08 21:39:24 -08002608 return len+sprintf(page+len, "\n");
2609}
2610
NeilBrown45dc2de2006-06-26 00:27:58 -07002611static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002612state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002613{
2614 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002615 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002616 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002617 * writemostly - sets write_mostly
2618 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002619 * blocked - sets the Blocked flags
2620 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002621 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002622 * write_error - sets WriteErrorSeen
2623 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002624 */
2625 int err = -EINVAL;
2626 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2627 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002628 if (test_bit(Faulty, &rdev->flags))
2629 err = 0;
2630 else
2631 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002632 } else if (cmd_match(buf, "remove")) {
2633 if (rdev->raid_disk >= 0)
2634 err = -EBUSY;
2635 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002636 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002637 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002638 if (mddev->pers)
2639 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002640 md_new_event(mddev);
2641 err = 0;
2642 }
NeilBrownf6556752006-06-26 00:28:01 -07002643 } else if (cmd_match(buf, "writemostly")) {
2644 set_bit(WriteMostly, &rdev->flags);
2645 err = 0;
2646 } else if (cmd_match(buf, "-writemostly")) {
2647 clear_bit(WriteMostly, &rdev->flags);
2648 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002649 } else if (cmd_match(buf, "blocked")) {
2650 set_bit(Blocked, &rdev->flags);
2651 err = 0;
2652 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002653 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002654 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002655 /* metadata handler doesn't understand badblocks,
2656 * so we need to fail the device
2657 */
2658 md_error(rdev->mddev, rdev);
2659 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002660 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002661 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002662 wake_up(&rdev->blocked_wait);
2663 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2664 md_wakeup_thread(rdev->mddev->thread);
2665
2666 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002667 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2668 set_bit(In_sync, &rdev->flags);
2669 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002670 } else if (cmd_match(buf, "write_error")) {
2671 set_bit(WriteErrorSeen, &rdev->flags);
2672 err = 0;
2673 } else if (cmd_match(buf, "-write_error")) {
2674 clear_bit(WriteErrorSeen, &rdev->flags);
2675 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002676 } else if (cmd_match(buf, "want_replacement")) {
2677 /* Any non-spare device that is not a replacement can
2678 * become want_replacement at any time, but we then need to
2679 * check if recovery is needed.
2680 */
2681 if (rdev->raid_disk >= 0 &&
2682 !test_bit(Replacement, &rdev->flags))
2683 set_bit(WantReplacement, &rdev->flags);
2684 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2685 md_wakeup_thread(rdev->mddev->thread);
2686 err = 0;
2687 } else if (cmd_match(buf, "-want_replacement")) {
2688 /* Clearing 'want_replacement' is always allowed.
2689 * Once replacements starts it is too late though.
2690 */
2691 err = 0;
2692 clear_bit(WantReplacement, &rdev->flags);
2693 } else if (cmd_match(buf, "replacement")) {
2694 /* Can only set a device as a replacement when array has not
2695 * yet been started. Once running, replacement is automatic
2696 * from spares, or by assigning 'slot'.
2697 */
2698 if (rdev->mddev->pers)
2699 err = -EBUSY;
2700 else {
2701 set_bit(Replacement, &rdev->flags);
2702 err = 0;
2703 }
2704 } else if (cmd_match(buf, "-replacement")) {
2705 /* Similarly, can only clear Replacement before start */
2706 if (rdev->mddev->pers)
2707 err = -EBUSY;
2708 else {
2709 clear_bit(Replacement, &rdev->flags);
2710 err = 0;
2711 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002712 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002713 if (!err)
2714 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002715 return err ? err : len;
2716}
NeilBrown80ca3a42006-07-10 04:44:18 -07002717static struct rdev_sysfs_entry rdev_state =
2718__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002719
2720static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002721errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002722{
2723 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2724}
2725
2726static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002727errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002728{
2729 char *e;
2730 unsigned long n = simple_strtoul(buf, &e, 10);
2731 if (*buf && (*e == 0 || *e == '\n')) {
2732 atomic_set(&rdev->corrected_errors, n);
2733 return len;
2734 }
2735 return -EINVAL;
2736}
2737static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002738__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002739
NeilBrown014236d2006-01-06 00:20:55 -08002740static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002741slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002742{
2743 if (rdev->raid_disk < 0)
2744 return sprintf(page, "none\n");
2745 else
2746 return sprintf(page, "%d\n", rdev->raid_disk);
2747}
2748
2749static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002750slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002751{
2752 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002753 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002754 int slot = simple_strtoul(buf, &e, 10);
2755 if (strncmp(buf, "none", 4)==0)
2756 slot = -1;
2757 else if (e==buf || (*e && *e!= '\n'))
2758 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002759 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002760 /* Setting 'slot' on an active array requires also
2761 * updating the 'rd%d' link, and communicating
2762 * with the personality with ->hot_*_disk.
2763 * For now we only support removing
2764 * failed/spare devices. This normally happens automatically,
2765 * but not when the metadata is externally managed.
2766 */
NeilBrownc303da62008-02-06 01:39:51 -08002767 if (rdev->raid_disk == -1)
2768 return -EEXIST;
2769 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002770 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002771 return -EINVAL;
2772 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002773 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002774 if (err)
2775 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002776 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002777 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002778 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2779 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002780 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002781 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002782 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 */
2784
2785 if (rdev->raid_disk != -1)
2786 return -EBUSY;
2787
NeilBrownc6751b22011-02-02 11:57:13 +11002788 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2789 return -EBUSY;
2790
Neil Brown6c2fce22008-06-28 08:31:31 +10002791 if (rdev->mddev->pers->hot_add_disk == NULL)
2792 return -EINVAL;
2793
NeilBrownba1b41b2011-01-14 09:14:34 +11002794 if (slot >= rdev->mddev->raid_disks &&
2795 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2796 return -ENOSPC;
2797
Neil Brown6c2fce22008-06-28 08:31:31 +10002798 rdev->raid_disk = slot;
2799 if (test_bit(In_sync, &rdev->flags))
2800 rdev->saved_raid_disk = slot;
2801 else
2802 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002803 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002804 err = rdev->mddev->pers->
2805 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002806 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002807 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 return err;
Neil Brown52664732008-06-28 08:31:44 +10002809 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002810 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002811 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002812 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002813 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002814 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002815 if (slot >= rdev->mddev->raid_disks &&
2816 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002817 return -ENOSPC;
2818 rdev->raid_disk = slot;
2819 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002820 clear_bit(Faulty, &rdev->flags);
2821 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002822 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002823 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002824 }
NeilBrown014236d2006-01-06 00:20:55 -08002825 return len;
2826}
2827
2828
2829static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002830__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002831
NeilBrown93c8cad2006-01-06 00:20:56 -08002832static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002833offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002834{
Andrew Morton6961ece2006-01-06 00:20:59 -08002835 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002836}
2837
2838static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002839offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002840{
NeilBrownc6563a82012-05-21 09:27:00 +10002841 unsigned long long offset;
2842 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002843 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002844 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002845 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002846 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847 /* Must set offset before size, so overlap checks
2848 * can be sane */
2849 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002850 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002851 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002852 return len;
2853}
2854
2855static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002856__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002857
NeilBrownc6563a82012-05-21 09:27:00 +10002858static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2859{
2860 return sprintf(page, "%llu\n",
2861 (unsigned long long)rdev->new_data_offset);
2862}
2863
2864static ssize_t new_offset_store(struct md_rdev *rdev,
2865 const char *buf, size_t len)
2866{
2867 unsigned long long new_offset;
2868 struct mddev *mddev = rdev->mddev;
2869
2870 if (strict_strtoull(buf, 10, &new_offset) < 0)
2871 return -EINVAL;
2872
2873 if (mddev->sync_thread)
2874 return -EBUSY;
2875 if (new_offset == rdev->data_offset)
2876 /* reset is always permitted */
2877 ;
2878 else if (new_offset > rdev->data_offset) {
2879 /* must not push array size beyond rdev_sectors */
2880 if (new_offset - rdev->data_offset
2881 + mddev->dev_sectors > rdev->sectors)
2882 return -E2BIG;
2883 }
2884 /* Metadata worries about other space details. */
2885
2886 /* decreasing the offset is inconsistent with a backwards
2887 * reshape.
2888 */
2889 if (new_offset < rdev->data_offset &&
2890 mddev->reshape_backwards)
2891 return -EINVAL;
2892 /* Increasing offset is inconsistent with forwards
2893 * reshape. reshape_direction should be set to
2894 * 'backwards' first.
2895 */
2896 if (new_offset > rdev->data_offset &&
2897 !mddev->reshape_backwards)
2898 return -EINVAL;
2899
2900 if (mddev->pers && mddev->persistent &&
2901 !super_types[mddev->major_version]
2902 .allow_new_offset(rdev, new_offset))
2903 return -E2BIG;
2904 rdev->new_data_offset = new_offset;
2905 if (new_offset > rdev->data_offset)
2906 mddev->reshape_backwards = 1;
2907 else if (new_offset < rdev->data_offset)
2908 mddev->reshape_backwards = 0;
2909
2910 return len;
2911}
2912static struct rdev_sysfs_entry rdev_new_offset =
2913__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2914
NeilBrown83303b62006-01-06 00:21:06 -08002915static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002916rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002917{
Andre Nolldd8ac332009-03-31 14:33:13 +11002918 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002919}
2920
NeilBrownc5d79ad2008-02-06 01:39:54 -08002921static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2922{
2923 /* check if two start/length pairs overlap */
2924 if (s1+l1 <= s2)
2925 return 0;
2926 if (s2+l2 <= s1)
2927 return 0;
2928 return 1;
2929}
2930
Dan Williamsb522adc2009-03-31 15:00:31 +11002931static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2932{
2933 unsigned long long blocks;
2934 sector_t new;
2935
2936 if (strict_strtoull(buf, 10, &blocks) < 0)
2937 return -EINVAL;
2938
2939 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2940 return -EINVAL; /* sector conversion overflow */
2941
2942 new = blocks * 2;
2943 if (new != blocks * 2)
2944 return -EINVAL; /* unsigned long long to sector_t overflow */
2945
2946 *sectors = new;
2947 return 0;
2948}
2949
NeilBrown83303b62006-01-06 00:21:06 -08002950static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002951rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002952{
NeilBrownfd01b882011-10-11 16:47:53 +11002953 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002955 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002956
Dan Williamsb522adc2009-03-31 15:00:31 +11002957 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002958 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002959 if (rdev->data_offset != rdev->new_data_offset)
2960 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002961 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002962 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002963 sectors = super_types[my_mddev->major_version].
2964 rdev_size_change(rdev, sectors);
2965 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002966 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002967 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002968 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002969 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002972 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002973
Andre Nolldd8ac332009-03-31 14:33:13 +11002974 rdev->sectors = sectors;
2975 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002976 /* need to check that all other rdevs with the same ->bdev
2977 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002979 * we have to change it back, we will have the lock again.
2980 */
NeilBrownfd01b882011-10-11 16:47:53 +11002981 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002982 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002983 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002984
NeilBrown27c529b2008-03-04 14:29:33 -08002985 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002986 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002987 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002988
2989 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002990 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002991 if (rdev->bdev == rdev2->bdev &&
2992 rdev != rdev2 &&
2993 overlaps(rdev->data_offset, rdev->sectors,
2994 rdev2->data_offset,
2995 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 overlap = 1;
2997 break;
2998 }
2999 mddev_unlock(mddev);
3000 if (overlap) {
3001 mddev_put(mddev);
3002 break;
3003 }
3004 }
NeilBrown27c529b2008-03-04 14:29:33 -08003005 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003006 if (overlap) {
3007 /* Someone else could have slipped in a size
3008 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003009 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 * safe, and trust userspace not to race with
3011 * itself
3012 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003013 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 return -EBUSY;
3015 }
3016 }
NeilBrown83303b62006-01-06 00:21:06 -08003017 return len;
3018}
3019
3020static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003021__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003022
Dan Williams06e3c812009-12-12 21:17:12 -07003023
NeilBrown3cb03002011-10-11 16:45:26 +11003024static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003025{
3026 unsigned long long recovery_start = rdev->recovery_offset;
3027
3028 if (test_bit(In_sync, &rdev->flags) ||
3029 recovery_start == MaxSector)
3030 return sprintf(page, "none\n");
3031
3032 return sprintf(page, "%llu\n", recovery_start);
3033}
3034
NeilBrown3cb03002011-10-11 16:45:26 +11003035static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003036{
3037 unsigned long long recovery_start;
3038
3039 if (cmd_match(buf, "none"))
3040 recovery_start = MaxSector;
3041 else if (strict_strtoull(buf, 10, &recovery_start))
3042 return -EINVAL;
3043
3044 if (rdev->mddev->pers &&
3045 rdev->raid_disk >= 0)
3046 return -EBUSY;
3047
3048 rdev->recovery_offset = recovery_start;
3049 if (recovery_start == MaxSector)
3050 set_bit(In_sync, &rdev->flags);
3051 else
3052 clear_bit(In_sync, &rdev->flags);
3053 return len;
3054}
3055
3056static struct rdev_sysfs_entry rdev_recovery_start =
3057__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3058
NeilBrown16c791a2011-07-28 11:31:47 +10003059
3060static ssize_t
3061badblocks_show(struct badblocks *bb, char *page, int unack);
3062static ssize_t
3063badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3064
NeilBrown3cb03002011-10-11 16:45:26 +11003065static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003066{
3067 return badblocks_show(&rdev->badblocks, page, 0);
3068}
NeilBrown3cb03002011-10-11 16:45:26 +11003069static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003070{
NeilBrownde393cd2011-07-28 11:31:48 +10003071 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3072 /* Maybe that ack was all we needed */
3073 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3074 wake_up(&rdev->blocked_wait);
3075 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003076}
3077static struct rdev_sysfs_entry rdev_bad_blocks =
3078__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3079
3080
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
3083 return badblocks_show(&rdev->badblocks, page, 1);
3084}
NeilBrown3cb03002011-10-11 16:45:26 +11003085static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003086{
3087 return badblocks_store(&rdev->badblocks, page, len, 1);
3088}
3089static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3090__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3091
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092static struct attribute *rdev_default_attrs[] = {
3093 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003094 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003095 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003096 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003097 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003098 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003099 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003100 &rdev_bad_blocks.attr,
3101 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003102 NULL,
3103};
3104static ssize_t
3105rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3106{
3107 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003109 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003110 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111
3112 if (!entry->show)
3113 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003114
3115 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3116 if (!rv) {
3117 if (rdev->mddev == NULL)
3118 rv = -EBUSY;
3119 else
3120 rv = entry->show(rdev, page);
3121 mddev_unlock(mddev);
3122 }
3123 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003124}
3125
3126static ssize_t
3127rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3128 const char *page, size_t length)
3129{
3130 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003131 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003132 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003133 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003134
3135 if (!entry->store)
3136 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003137 if (!capable(CAP_SYS_ADMIN))
3138 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003139 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003140 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003141 if (rdev->mddev == NULL)
3142 rv = -EBUSY;
3143 else
3144 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003145 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003146 }
3147 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003148}
3149
3150static void rdev_free(struct kobject *ko)
3151{
NeilBrown3cb03002011-10-11 16:45:26 +11003152 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003153 kfree(rdev);
3154}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003155static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003156 .show = rdev_attr_show,
3157 .store = rdev_attr_store,
3158};
3159static struct kobj_type rdev_ktype = {
3160 .release = rdev_free,
3161 .sysfs_ops = &rdev_sysfs_ops,
3162 .default_attrs = rdev_default_attrs,
3163};
3164
NeilBrown3cb03002011-10-11 16:45:26 +11003165int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003166{
3167 rdev->desc_nr = -1;
3168 rdev->saved_raid_disk = -1;
3169 rdev->raid_disk = -1;
3170 rdev->flags = 0;
3171 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003172 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003173 rdev->sb_events = 0;
3174 rdev->last_read_error.tv_sec = 0;
3175 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003176 rdev->sb_loaded = 0;
3177 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003178 atomic_set(&rdev->nr_pending, 0);
3179 atomic_set(&rdev->read_errors, 0);
3180 atomic_set(&rdev->corrected_errors, 0);
3181
3182 INIT_LIST_HEAD(&rdev->same_set);
3183 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003184
3185 /* Add space to store bad block list.
3186 * This reserves the space even on arrays where it cannot
3187 * be used - I wonder if that matters
3188 */
3189 rdev->badblocks.count = 0;
3190 rdev->badblocks.shift = 0;
3191 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3192 seqlock_init(&rdev->badblocks.lock);
3193 if (rdev->badblocks.page == NULL)
3194 return -ENOMEM;
3195
3196 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003197}
3198EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
3200 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3201 *
3202 * mark the device faulty if:
3203 *
3204 * - the device is nonexistent (zero size)
3205 * - the device has no valid superblock
3206 *
3207 * a faulty rdev _never_ has rdev->sb set.
3208 */
NeilBrown3cb03002011-10-11 16:45:26 +11003209static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 char b[BDEVNAME_SIZE];
3212 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003213 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 sector_t size;
3215
NeilBrown9ffae0c2006-01-06 00:20:32 -08003216 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (!rdev) {
3218 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3219 return ERR_PTR(-ENOMEM);
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
NeilBrown2230dfe2011-07-28 11:31:46 +10003222 err = md_rdev_init(rdev);
3223 if (err)
3224 goto abort_free;
3225 err = alloc_disk_sb(rdev);
3226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto abort_free;
3228
NeilBrownc5d79ad2008-02-06 01:39:54 -08003229 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (err)
3231 goto abort_free;
3232
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003233 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003234
Mike Snitzer77304d22010-11-08 14:39:12 +01003235 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!size) {
3237 printk(KERN_WARNING
3238 "md: %s has zero or unknown size, marking faulty!\n",
3239 bdevname(rdev->bdev,b));
3240 err = -EINVAL;
3241 goto abort_free;
3242 }
3243
3244 if (super_format >= 0) {
3245 err = super_types[super_format].
3246 load_super(rdev, NULL, super_minor);
3247 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003248 printk(KERN_WARNING
3249 "md: %s does not have a valid v%d.%d "
3250 "superblock, not importing!\n",
3251 bdevname(rdev->bdev,b),
3252 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto abort_free;
3254 }
3255 if (err < 0) {
3256 printk(KERN_WARNING
3257 "md: could not read %s's sb, not importing!\n",
3258 bdevname(rdev->bdev,b));
3259 goto abort_free;
3260 }
3261 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003262 if (super_format == -1)
3263 /* hot-add for 0.90, or non-persistent: so no badblocks */
3264 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return rdev;
3267
3268abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003269 if (rdev->bdev)
3270 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003271 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 kfree(rdev);
3273 return ERR_PTR(err);
3274}
3275
3276/*
3277 * Check a full RAID array for plausibility
3278 */
3279
3280
NeilBrownfd01b882011-10-11 16:47:53 +11003281static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003284 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 char b[BDEVNAME_SIZE];
3286
3287 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003288 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 switch (super_types[mddev->major_version].
3290 load_super(rdev, freshest, mddev->minor_version)) {
3291 case 1:
3292 freshest = rdev;
3293 break;
3294 case 0:
3295 break;
3296 default:
3297 printk( KERN_ERR \
3298 "md: fatal superblock inconsistency in %s"
3299 " -- removing from array\n",
3300 bdevname(rdev->bdev,b));
3301 kick_rdev_from_array(rdev);
3302 }
3303
3304
3305 super_types[mddev->major_version].
3306 validate_super(mddev, freshest);
3307
3308 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003309 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003310 if (mddev->max_disks &&
3311 (rdev->desc_nr >= mddev->max_disks ||
3312 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003313 printk(KERN_WARNING
3314 "md: %s: %s: only %d devices permitted\n",
3315 mdname(mddev), bdevname(rdev->bdev, b),
3316 mddev->max_disks);
3317 kick_rdev_from_array(rdev);
3318 continue;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (rdev != freshest)
3321 if (super_types[mddev->major_version].
3322 validate_super(mddev, rdev)) {
3323 printk(KERN_WARNING "md: kicking non-fresh %s"
3324 " from array!\n",
3325 bdevname(rdev->bdev,b));
3326 kick_rdev_from_array(rdev);
3327 continue;
3328 }
3329 if (mddev->level == LEVEL_MULTIPATH) {
3330 rdev->desc_nr = i++;
3331 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003332 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003333 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003334 rdev->raid_disk = -1;
3335 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
NeilBrown72e02072009-12-14 12:49:55 +11003340/* Read a fixed-point number.
3341 * Numbers in sysfs attributes should be in "standard" units where
3342 * possible, so time should be in seconds.
3343 * However we internally use a a much smaller unit such as
3344 * milliseconds or jiffies.
3345 * This function takes a decimal number with a possible fractional
3346 * component, and produces an integer which is the result of
3347 * multiplying that number by 10^'scale'.
3348 * all without any floating-point arithmetic.
3349 */
3350int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3351{
3352 unsigned long result = 0;
3353 long decimals = -1;
3354 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3355 if (*cp == '.')
3356 decimals = 0;
3357 else if (decimals < scale) {
3358 unsigned int value;
3359 value = *cp - '0';
3360 result = result * 10 + value;
3361 if (decimals >= 0)
3362 decimals++;
3363 }
3364 cp++;
3365 }
3366 if (*cp == '\n')
3367 cp++;
3368 if (*cp)
3369 return -EINVAL;
3370 if (decimals < 0)
3371 decimals = 0;
3372 while (decimals < scale) {
3373 result *= 10;
3374 decimals ++;
3375 }
3376 *res = result;
3377 return 0;
3378}
3379
3380
NeilBrown19052c02008-08-05 15:54:13 +10003381static void md_safemode_timeout(unsigned long data);
3382
NeilBrowneae17012005-11-08 21:39:23 -08003383static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003384safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003385{
3386 int msec = (mddev->safemode_delay*1000)/HZ;
3387 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3388}
3389static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003390safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003391{
NeilBrown16f17b32006-06-26 00:27:37 -07003392 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003393
NeilBrown72e02072009-12-14 12:49:55 +11003394 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003395 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003396 if (msec == 0)
3397 mddev->safemode_delay = 0;
3398 else {
NeilBrown19052c02008-08-05 15:54:13 +10003399 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003400 mddev->safemode_delay = (msec*HZ)/1000;
3401 if (mddev->safemode_delay == 0)
3402 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003403 if (mddev->safemode_delay < old_delay)
3404 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003405 }
3406 return len;
3407}
3408static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003409__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003410
3411static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003412level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003413{
NeilBrown84fc4b52011-10-11 16:49:58 +11003414 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003415 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003416 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003417 else if (mddev->clevel[0])
3418 return sprintf(page, "%s\n", mddev->clevel);
3419 else if (mddev->level != LEVEL_NONE)
3420 return sprintf(page, "%d\n", mddev->level);
3421 else
3422 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003423}
3424
NeilBrownd9d166c2006-01-06 00:20:51 -08003425static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003426level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003427{
Dan Williamsf2859af2010-05-02 10:04:16 -07003428 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003429 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003430 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003431 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003432 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003433 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003434
3435 if (mddev->pers == NULL) {
3436 if (len == 0)
3437 return 0;
3438 if (len >= sizeof(mddev->clevel))
3439 return -ENOSPC;
3440 strncpy(mddev->clevel, buf, len);
3441 if (mddev->clevel[len-1] == '\n')
3442 len--;
3443 mddev->clevel[len] = 0;
3444 mddev->level = LEVEL_NONE;
3445 return rv;
3446 }
3447
3448 /* request to change the personality. Need to ensure:
3449 * - array is not engaged in resync/recovery/reshape
3450 * - old personality can be suspended
3451 * - new personality will access other array.
3452 */
3453
NeilBrownbb4f1e92010-08-08 21:18:03 +10003454 if (mddev->sync_thread ||
3455 mddev->reshape_position != MaxSector ||
3456 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003457 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003458
3459 if (!mddev->pers->quiesce) {
3460 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3461 mdname(mddev), mddev->pers->name);
3462 return -EINVAL;
3463 }
3464
3465 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003466 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003467 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 strncpy(clevel, buf, len);
3469 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003470 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003471 clevel[len] = 0;
3472 if (strict_strtol(clevel, 10, &level))
3473 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003474
Dan Williamsf2859af2010-05-02 10:04:16 -07003475 if (request_module("md-%s", clevel) != 0)
3476 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003477 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003478 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003479 if (!pers || !try_module_get(pers->owner)) {
3480 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003481 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003482 return -EINVAL;
3483 }
3484 spin_unlock(&pers_lock);
3485
3486 if (pers == mddev->pers) {
3487 /* Nothing to do! */
3488 module_put(pers->owner);
3489 return rv;
3490 }
3491 if (!pers->takeover) {
3492 module_put(pers->owner);
3493 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003495 return -EINVAL;
3496 }
3497
NeilBrowndafb20f2012-03-19 12:46:39 +11003498 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003499 rdev->new_raid_disk = rdev->raid_disk;
3500
NeilBrown245f46c2009-03-31 14:39:39 +11003501 /* ->takeover must set new_* and/or delta_disks
3502 * if it succeeds, and may set them when it fails.
3503 */
3504 priv = pers->takeover(mddev);
3505 if (IS_ERR(priv)) {
3506 mddev->new_level = mddev->level;
3507 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003508 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003509 mddev->raid_disks -= mddev->delta_disks;
3510 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003511 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003512 module_put(pers->owner);
3513 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003514 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003515 return PTR_ERR(priv);
3516 }
3517
3518 /* Looks like we have a winner */
3519 mddev_suspend(mddev);
3520 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003521
3522 if (mddev->pers->sync_request == NULL &&
3523 pers->sync_request != NULL) {
3524 /* need to add the md_redundancy_group */
3525 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3526 printk(KERN_WARNING
3527 "md: cannot register extra attributes for %s\n",
3528 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003529 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003530 }
3531 if (mddev->pers->sync_request != NULL &&
3532 pers->sync_request == NULL) {
3533 /* need to remove the md_redundancy_group */
3534 if (mddev->to_remove == NULL)
3535 mddev->to_remove = &md_redundancy_group;
3536 }
3537
Trela Maciej54071b32010-03-08 16:02:42 +11003538 if (mddev->pers->sync_request == NULL &&
3539 mddev->external) {
3540 /* We are converting from a no-redundancy array
3541 * to a redundancy array and metadata is managed
3542 * externally so we need to be sure that writes
3543 * won't block due to a need to transition
3544 * clean->dirty
3545 * until external management is started.
3546 */
3547 mddev->in_sync = 0;
3548 mddev->safemode_delay = 0;
3549 mddev->safemode = 0;
3550 }
3551
NeilBrowndafb20f2012-03-19 12:46:39 +11003552 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003553 if (rdev->raid_disk < 0)
3554 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003555 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003556 rdev->new_raid_disk = -1;
3557 if (rdev->new_raid_disk == rdev->raid_disk)
3558 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003559 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003560 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003561 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003562 if (rdev->raid_disk < 0)
3563 continue;
3564 if (rdev->new_raid_disk == rdev->raid_disk)
3565 continue;
3566 rdev->raid_disk = rdev->new_raid_disk;
3567 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003568 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003569 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003570 if (sysfs_link_rdev(mddev, rdev))
3571 printk(KERN_WARNING "md: cannot register rd%d"
3572 " for %s after level change\n",
3573 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003574 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003575 }
3576
3577 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003578 mddev->pers = pers;
3579 mddev->private = priv;
3580 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3581 mddev->level = mddev->new_level;
3582 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003583 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003584 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003585 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003586 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003587 if (mddev->pers->sync_request == NULL) {
3588 /* this is now an array without redundancy, so
3589 * it must always be in_sync
3590 */
3591 mddev->in_sync = 1;
3592 del_timer_sync(&mddev->safemode_timer);
3593 }
NeilBrown245f46c2009-03-31 14:39:39 +11003594 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003595 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003596 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003597 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003598 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003599 return rv;
3600}
3601
3602static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003603__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003604
NeilBrownd4dbd022006-06-26 00:27:59 -07003605
3606static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003607layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003608{
3609 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003610 if (mddev->reshape_position != MaxSector &&
3611 mddev->layout != mddev->new_layout)
3612 return sprintf(page, "%d (%d)\n",
3613 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003614 return sprintf(page, "%d\n", mddev->layout);
3615}
3616
3617static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003618layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003619{
3620 char *e;
3621 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003622
3623 if (!*buf || (*e && *e != '\n'))
3624 return -EINVAL;
3625
NeilBrownb3546032009-03-31 14:56:41 +11003626 if (mddev->pers) {
3627 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003628 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003629 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003630 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003631 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003632 if (err) {
3633 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003634 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003635 }
NeilBrownb3546032009-03-31 14:56:41 +11003636 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003637 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003638 if (mddev->reshape_position == MaxSector)
3639 mddev->layout = n;
3640 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003641 return len;
3642}
3643static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003644__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003645
3646
NeilBrowneae17012005-11-08 21:39:23 -08003647static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003648raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003649{
NeilBrownbb636542005-11-08 21:39:45 -08003650 if (mddev->raid_disks == 0)
3651 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003652 if (mddev->reshape_position != MaxSector &&
3653 mddev->delta_disks != 0)
3654 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3655 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003656 return sprintf(page, "%d\n", mddev->raid_disks);
3657}
3658
NeilBrownfd01b882011-10-11 16:47:53 +11003659static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003660
3661static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003662raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003663{
NeilBrownda943b992006-01-06 00:20:54 -08003664 char *e;
3665 int rv = 0;
3666 unsigned long n = simple_strtoul(buf, &e, 10);
3667
3668 if (!*buf || (*e && *e != '\n'))
3669 return -EINVAL;
3670
3671 if (mddev->pers)
3672 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003673 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003674 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003675 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003676
3677 rdev_for_each(rdev, mddev) {
3678 if (olddisks < n &&
3679 rdev->data_offset < rdev->new_data_offset)
3680 return -EINVAL;
3681 if (olddisks > n &&
3682 rdev->data_offset > rdev->new_data_offset)
3683 return -EINVAL;
3684 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003685 mddev->delta_disks = n - olddisks;
3686 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003687 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003688 } else
NeilBrownda943b992006-01-06 00:20:54 -08003689 mddev->raid_disks = n;
3690 return rv ? rv : len;
3691}
3692static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003693__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003694
NeilBrown24dd4692005-11-08 21:39:26 -08003695static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003696chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003697{
NeilBrown08a02ec2007-05-09 02:35:38 -07003698 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003699 mddev->chunk_sectors != mddev->new_chunk_sectors)
3700 return sprintf(page, "%d (%d)\n",
3701 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003702 mddev->chunk_sectors << 9);
3703 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003704}
3705
3706static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003707chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003708{
NeilBrown3b343802006-01-06 00:20:47 -08003709 char *e;
3710 unsigned long n = simple_strtoul(buf, &e, 10);
3711
NeilBrown3b343802006-01-06 00:20:47 -08003712 if (!*buf || (*e && *e != '\n'))
3713 return -EINVAL;
3714
NeilBrownb3546032009-03-31 14:56:41 +11003715 if (mddev->pers) {
3716 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003717 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003718 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003719 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003720 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003721 if (err) {
3722 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003723 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003724 }
NeilBrownb3546032009-03-31 14:56:41 +11003725 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003726 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003727 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003728 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003729 }
NeilBrown3b343802006-01-06 00:20:47 -08003730 return len;
3731}
3732static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003733__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003734
NeilBrowna94213b2006-06-26 00:28:00 -07003735static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003736resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003737{
NeilBrownd1a7c502009-03-31 15:24:32 +11003738 if (mddev->recovery_cp == MaxSector)
3739 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003740 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3741}
3742
3743static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003744resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003745{
NeilBrowna94213b2006-06-26 00:28:00 -07003746 char *e;
3747 unsigned long long n = simple_strtoull(buf, &e, 10);
3748
NeilBrownb0986362011-05-11 15:52:21 +10003749 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003750 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003751 if (cmd_match(buf, "none"))
3752 n = MaxSector;
3753 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003754 return -EINVAL;
3755
3756 mddev->recovery_cp = n;
3757 return len;
3758}
3759static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003760__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003761
NeilBrown9e653b62006-06-26 00:27:58 -07003762/*
3763 * The array state can be:
3764 *
3765 * clear
3766 * No devices, no size, no level
3767 * Equivalent to STOP_ARRAY ioctl
3768 * inactive
3769 * May have some settings, but array is not active
3770 * all IO results in error
3771 * When written, doesn't tear down array, but just stops it
3772 * suspended (not supported yet)
3773 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003774 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003775 * readonly
3776 * no resync can happen. no superblocks get written.
3777 * write requests fail
3778 * read-auto
3779 * like readonly, but behaves like 'clean' on a write request.
3780 *
3781 * clean - no pending writes, but otherwise active.
3782 * When written to inactive array, starts without resync
3783 * If a write request arrives then
3784 * if metadata is known, mark 'dirty' and switch to 'active'.
3785 * if not known, block and switch to write-pending
3786 * If written to an active array that has pending writes, then fails.
3787 * active
3788 * fully active: IO and resync can be happening.
3789 * When written to inactive array, starts with resync
3790 *
3791 * write-pending
3792 * clean, but writes are blocked waiting for 'active' to be written.
3793 *
3794 * active-idle
3795 * like active, but no writes have been seen for a while (100msec).
3796 *
3797 */
3798enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3799 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003800static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003801 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3802 "write-pending", "active-idle", NULL };
3803
3804static int match_word(const char *word, char **list)
3805{
3806 int n;
3807 for (n=0; list[n]; n++)
3808 if (cmd_match(word, list[n]))
3809 break;
3810 return n;
3811}
3812
3813static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003814array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003815{
3816 enum array_state st = inactive;
3817
3818 if (mddev->pers)
3819 switch(mddev->ro) {
3820 case 1:
3821 st = readonly;
3822 break;
3823 case 2:
3824 st = read_auto;
3825 break;
3826 case 0:
3827 if (mddev->in_sync)
3828 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003829 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003830 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003831 else if (mddev->safemode)
3832 st = active_idle;
3833 else
3834 st = active;
3835 }
3836 else {
3837 if (list_empty(&mddev->disks) &&
3838 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003839 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003840 st = clear;
3841 else
3842 st = inactive;
3843 }
3844 return sprintf(page, "%s\n", array_states[st]);
3845}
3846
NeilBrowna05b7ea2012-07-19 15:59:18 +10003847static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3848static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003849static int do_md_run(struct mddev * mddev);
3850static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003851
3852static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003853array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003854{
3855 int err = -EINVAL;
3856 enum array_state st = match_word(buf, array_states);
3857 switch(st) {
3858 case bad_word:
3859 break;
3860 case clear:
3861 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003862 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003863 break;
3864 case inactive:
3865 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003866 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003867 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003868 else
NeilBrowne6910632008-02-06 01:39:51 -08003869 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003870 break;
3871 case suspended:
3872 break; /* not supported yet */
3873 case readonly:
3874 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003875 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003876 else {
3877 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003878 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003879 err = do_md_run(mddev);
3880 }
3881 break;
3882 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003883 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003884 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003885 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003886 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003887 err = restart_array(mddev);
3888 if (err == 0) {
3889 mddev->ro = 2;
3890 set_disk_ro(mddev->gendisk, 0);
3891 }
NeilBrown9e653b62006-06-26 00:27:58 -07003892 } else {
3893 mddev->ro = 2;
3894 err = do_md_run(mddev);
3895 }
3896 break;
3897 case clean:
3898 if (mddev->pers) {
3899 restart_array(mddev);
3900 spin_lock_irq(&mddev->write_lock);
3901 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003902 if (mddev->in_sync == 0) {
3903 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003904 if (mddev->safemode == 1)
3905 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003906 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003907 }
3908 err = 0;
3909 } else
3910 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003911 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003912 } else
3913 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003914 break;
3915 case active:
3916 if (mddev->pers) {
3917 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003918 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003919 wake_up(&mddev->sb_wait);
3920 err = 0;
3921 } else {
3922 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003923 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003924 err = do_md_run(mddev);
3925 }
3926 break;
3927 case write_pending:
3928 case active_idle:
3929 /* these cannot be set */
3930 break;
3931 }
3932 if (err)
3933 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003934 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003935 if (mddev->hold_active == UNTIL_IOCTL)
3936 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003937 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003938 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003939 }
NeilBrown9e653b62006-06-26 00:27:58 -07003940}
NeilBrown80ca3a42006-07-10 04:44:18 -07003941static struct md_sysfs_entry md_array_state =
3942__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003943
NeilBrown6d7ff7382006-01-06 00:21:16 -08003944static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003945max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003946 return sprintf(page, "%d\n",
3947 atomic_read(&mddev->max_corr_read_errors));
3948}
3949
3950static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003951max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003952{
3953 char *e;
3954 unsigned long n = simple_strtoul(buf, &e, 10);
3955
3956 if (*buf && (*e == 0 || *e == '\n')) {
3957 atomic_set(&mddev->max_corr_read_errors, n);
3958 return len;
3959 }
3960 return -EINVAL;
3961}
3962
3963static struct md_sysfs_entry max_corr_read_errors =
3964__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3965 max_corrected_read_errors_store);
3966
3967static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003968null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003969{
3970 return -EINVAL;
3971}
3972
3973static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003974new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003975{
3976 /* buf must be %d:%d\n? giving major and minor numbers */
3977 /* The new device is added to the array.
3978 * If the array has a persistent superblock, we read the
3979 * superblock to initialise info and check validity.
3980 * Otherwise, only checking done is that in bind_rdev_to_array,
3981 * which mainly checks size.
3982 */
3983 char *e;
3984 int major = simple_strtoul(buf, &e, 10);
3985 int minor;
3986 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003987 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003988 int err;
3989
3990 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3991 return -EINVAL;
3992 minor = simple_strtoul(e+1, &e, 10);
3993 if (*e && *e != '\n')
3994 return -EINVAL;
3995 dev = MKDEV(major, minor);
3996 if (major != MAJOR(dev) ||
3997 minor != MINOR(dev))
3998 return -EOVERFLOW;
3999
4000
4001 if (mddev->persistent) {
4002 rdev = md_import_device(dev, mddev->major_version,
4003 mddev->minor_version);
4004 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004005 struct md_rdev *rdev0
4006 = list_entry(mddev->disks.next,
4007 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004008 err = super_types[mddev->major_version]
4009 .load_super(rdev, rdev0, mddev->minor_version);
4010 if (err < 0)
4011 goto out;
4012 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004013 } else if (mddev->external)
4014 rdev = md_import_device(dev, -2, -1);
4015 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004016 rdev = md_import_device(dev, -1, -1);
4017
4018 if (IS_ERR(rdev))
4019 return PTR_ERR(rdev);
4020 err = bind_rdev_to_array(rdev, mddev);
4021 out:
4022 if (err)
4023 export_rdev(rdev);
4024 return err ? err : len;
4025}
4026
4027static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004028__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004029
4030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004031bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004032{
4033 char *end;
4034 unsigned long chunk, end_chunk;
4035
4036 if (!mddev->bitmap)
4037 goto out;
4038 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4039 while (*buf) {
4040 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4041 if (buf == end) break;
4042 if (*end == '-') { /* range */
4043 buf = end + 1;
4044 end_chunk = simple_strtoul(buf, &end, 0);
4045 if (buf == end) break;
4046 }
4047 if (*end && !isspace(*end)) break;
4048 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004049 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004050 }
4051 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4052out:
4053 return len;
4054}
4055
4056static struct md_sysfs_entry md_bitmap =
4057__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4058
4059static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004060size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004061{
Andre Noll58c0fed2009-03-31 14:33:13 +11004062 return sprintf(page, "%llu\n",
4063 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004064}
4065
NeilBrownfd01b882011-10-11 16:47:53 +11004066static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004067
4068static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004069size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004070{
4071 /* If array is inactive, we can reduce the component size, but
4072 * not increase it (except from 0).
4073 * If array is active, we can try an on-line resize
4074 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004075 sector_t sectors;
4076 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004077
Andre Noll58c0fed2009-03-31 14:33:13 +11004078 if (err < 0)
4079 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004080 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004081 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004082 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004083 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004084 if (mddev->dev_sectors == 0 ||
4085 mddev->dev_sectors > sectors)
4086 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004087 else
4088 err = -ENOSPC;
4089 }
4090 return err ? err : len;
4091}
4092
4093static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004094__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004095
NeilBrown8bb93aa2006-01-06 00:20:50 -08004096
4097/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004098 * This is one of
4099 * 'none' for arrays with no metadata (good luck...)
4100 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004101 * or N.M for internally known formats
4102 */
4103static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004104metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004105{
4106 if (mddev->persistent)
4107 return sprintf(page, "%d.%d\n",
4108 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004109 else if (mddev->external)
4110 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004111 else
4112 return sprintf(page, "none\n");
4113}
4114
4115static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004116metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004117{
4118 int major, minor;
4119 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004120 /* Changing the details of 'external' metadata is
4121 * always permitted. Otherwise there must be
4122 * no devices attached to the array.
4123 */
4124 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4125 ;
4126 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004127 return -EBUSY;
4128
4129 if (cmd_match(buf, "none")) {
4130 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004131 mddev->external = 0;
4132 mddev->major_version = 0;
4133 mddev->minor_version = 90;
4134 return len;
4135 }
4136 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004137 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004138 if (namelen >= sizeof(mddev->metadata_type))
4139 namelen = sizeof(mddev->metadata_type)-1;
4140 strncpy(mddev->metadata_type, buf+9, namelen);
4141 mddev->metadata_type[namelen] = 0;
4142 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4143 mddev->metadata_type[--namelen] = 0;
4144 mddev->persistent = 0;
4145 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146 mddev->major_version = 0;
4147 mddev->minor_version = 90;
4148 return len;
4149 }
4150 major = simple_strtoul(buf, &e, 10);
4151 if (e==buf || *e != '.')
4152 return -EINVAL;
4153 buf = e+1;
4154 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004155 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004157 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004158 return -ENOENT;
4159 mddev->major_version = major;
4160 mddev->minor_version = minor;
4161 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004162 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004163 return len;
4164}
4165
4166static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004167__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004168
NeilBrowna35b0d62006-01-06 00:20:49 -08004169static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004170action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004171{
NeilBrown7eec3142005-11-08 21:39:44 -08004172 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004173 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4174 type = "frozen";
4175 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004176 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004177 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4178 type = "reshape";
4179 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004180 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4181 type = "resync";
4182 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4183 type = "check";
4184 else
4185 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004186 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004187 type = "recover";
4188 }
4189 return sprintf(page, "%s\n", type);
4190}
4191
NeilBrownfd01b882011-10-11 16:47:53 +11004192static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004193
NeilBrown24dd4692005-11-08 21:39:26 -08004194static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004195action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004196{
NeilBrown7eec3142005-11-08 21:39:44 -08004197 if (!mddev->pers || !mddev->pers->sync_request)
4198 return -EINVAL;
4199
NeilBrownb6a9ce62009-05-26 09:41:17 +10004200 if (cmd_match(page, "frozen"))
4201 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4202 else
4203 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4204
4205 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004206 if (mddev->sync_thread) {
4207 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004208 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004209 }
NeilBrown03c902e2006-01-06 00:20:46 -08004210 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4211 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004212 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004213 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004214 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004215 else if (cmd_match(page, "recover")) {
4216 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4217 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4218 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004219 int err;
4220 if (mddev->pers->start_reshape == NULL)
4221 return -EINVAL;
4222 err = mddev->pers->start_reshape(mddev);
4223 if (err)
4224 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004225 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004226 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004227 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004228 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004229 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004230 return -EINVAL;
4231 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4232 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004233 }
NeilBrown03c902e2006-01-06 00:20:46 -08004234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004235 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004236 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004237 return len;
4238}
4239
NeilBrown9d888832005-11-08 21:39:26 -08004240static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004241mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004242{
4243 return sprintf(page, "%llu\n",
4244 (unsigned long long) mddev->resync_mismatches);
4245}
4246
NeilBrown80ca3a42006-07-10 04:44:18 -07004247static struct md_sysfs_entry md_scan_mode =
4248__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004249
NeilBrown96de1e62005-11-08 21:39:39 -08004250
NeilBrown80ca3a42006-07-10 04:44:18 -07004251static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004252
NeilBrown88202a02006-01-06 00:21:36 -08004253static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004254sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004255{
4256 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4257 mddev->sync_speed_min ? "local": "system");
4258}
4259
4260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004261sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004262{
4263 int min;
4264 char *e;
4265 if (strncmp(buf, "system", 6)==0) {
4266 mddev->sync_speed_min = 0;
4267 return len;
4268 }
4269 min = simple_strtoul(buf, &e, 10);
4270 if (buf == e || (*e && *e != '\n') || min <= 0)
4271 return -EINVAL;
4272 mddev->sync_speed_min = min;
4273 return len;
4274}
4275
4276static struct md_sysfs_entry md_sync_min =
4277__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4278
4279static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004280sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004281{
4282 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4283 mddev->sync_speed_max ? "local": "system");
4284}
4285
4286static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004287sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004288{
4289 int max;
4290 char *e;
4291 if (strncmp(buf, "system", 6)==0) {
4292 mddev->sync_speed_max = 0;
4293 return len;
4294 }
4295 max = simple_strtoul(buf, &e, 10);
4296 if (buf == e || (*e && *e != '\n') || max <= 0)
4297 return -EINVAL;
4298 mddev->sync_speed_max = max;
4299 return len;
4300}
4301
4302static struct md_sysfs_entry md_sync_max =
4303__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4304
Iustin Popd7f3d292007-10-16 23:30:54 -07004305static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004306degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004307{
4308 return sprintf(page, "%d\n", mddev->degraded);
4309}
4310static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004311
4312static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004313sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004314{
4315 return sprintf(page, "%d\n", mddev->parallel_resync);
4316}
4317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004320{
4321 long n;
4322
4323 if (strict_strtol(buf, 10, &n))
4324 return -EINVAL;
4325
4326 if (n != 0 && n != 1)
4327 return -EINVAL;
4328
4329 mddev->parallel_resync = n;
4330
4331 if (mddev->sync_thread)
4332 wake_up(&resync_wait);
4333
4334 return len;
4335}
4336
4337/* force parallel resync, even with shared block devices */
4338static struct md_sysfs_entry md_sync_force_parallel =
4339__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4340 sync_force_parallel_show, sync_force_parallel_store);
4341
4342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004344{
4345 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004346 if (mddev->curr_resync == 0)
4347 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004348 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4349 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004350 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004351 db = resync - mddev->resync_mark_cnt;
4352 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004353}
4354
NeilBrown80ca3a42006-07-10 04:44:18 -07004355static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004356
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004359{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004360 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004361
NeilBrownacb180b2009-04-14 16:28:34 +10004362 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4363 return sprintf(page, "none\n");
4364
NeilBrownc804cde2012-05-21 09:28:33 +10004365 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4366 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004367 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004368 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004369 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004370
NeilBrownacb180b2009-04-14 16:28:34 +10004371 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004372 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004373}
4374
NeilBrown80ca3a42006-07-10 04:44:18 -07004375static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004376
NeilBrowne464eaf2006-03-27 01:18:14 -08004377static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004378min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004379{
4380 return sprintf(page, "%llu\n",
4381 (unsigned long long)mddev->resync_min);
4382}
4383static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004384min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004385{
4386 unsigned long long min;
4387 if (strict_strtoull(buf, 10, &min))
4388 return -EINVAL;
4389 if (min > mddev->resync_max)
4390 return -EINVAL;
4391 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4392 return -EBUSY;
4393
4394 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004395 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004396 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004397 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004398 return -EINVAL;
4399 }
4400 mddev->resync_min = min;
4401
4402 return len;
4403}
4404
4405static struct md_sysfs_entry md_min_sync =
4406__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4407
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004410{
4411 if (mddev->resync_max == MaxSector)
4412 return sprintf(page, "max\n");
4413 else
4414 return sprintf(page, "%llu\n",
4415 (unsigned long long)mddev->resync_max);
4416}
4417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004418max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004419{
4420 if (strncmp(buf, "max", 3) == 0)
4421 mddev->resync_max = MaxSector;
4422 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004423 unsigned long long max;
4424 if (strict_strtoull(buf, 10, &max))
4425 return -EINVAL;
4426 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004427 return -EINVAL;
4428 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004429 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004430 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4431 return -EBUSY;
4432
4433 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004434 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004435 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004436 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004437 return -EINVAL;
4438 }
4439 mddev->resync_max = max;
4440 }
4441 wake_up(&mddev->recovery_wait);
4442 return len;
4443}
4444
4445static struct md_sysfs_entry md_max_sync =
4446__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4447
4448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004449suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004450{
4451 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4452}
4453
4454static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004455suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004456{
4457 char *e;
4458 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004459 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004460
NeilBrownb8d966e2009-07-01 11:14:04 +10004461 if (mddev->pers == NULL ||
4462 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004463 return -EINVAL;
4464 if (buf == e || (*e && *e != '\n'))
4465 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004466
4467 mddev->suspend_lo = new;
4468 if (new >= old)
4469 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004470 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004471 else {
4472 /* Expanding suspended region - need to wait */
4473 mddev->pers->quiesce(mddev, 1);
4474 mddev->pers->quiesce(mddev, 0);
4475 }
4476 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004477}
4478static struct md_sysfs_entry md_suspend_lo =
4479__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4480
4481
4482static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004483suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004484{
4485 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4486}
4487
4488static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004489suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004490{
4491 char *e;
4492 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004493 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004494
NeilBrownb8d966e2009-07-01 11:14:04 +10004495 if (mddev->pers == NULL ||
4496 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004497 return -EINVAL;
4498 if (buf == e || (*e && *e != '\n'))
4499 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004500
4501 mddev->suspend_hi = new;
4502 if (new <= old)
4503 /* Shrinking suspended region */
4504 mddev->pers->quiesce(mddev, 2);
4505 else {
4506 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004507 mddev->pers->quiesce(mddev, 1);
4508 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004509 }
4510 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004511}
4512static struct md_sysfs_entry md_suspend_hi =
4513__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4514
NeilBrown08a02ec2007-05-09 02:35:38 -07004515static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004516reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004517{
4518 if (mddev->reshape_position != MaxSector)
4519 return sprintf(page, "%llu\n",
4520 (unsigned long long)mddev->reshape_position);
4521 strcpy(page, "none\n");
4522 return 5;
4523}
4524
4525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004526reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004527{
NeilBrownc6563a82012-05-21 09:27:00 +10004528 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004529 char *e;
4530 unsigned long long new = simple_strtoull(buf, &e, 10);
4531 if (mddev->pers)
4532 return -EBUSY;
4533 if (buf == e || (*e && *e != '\n'))
4534 return -EINVAL;
4535 mddev->reshape_position = new;
4536 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004537 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004538 mddev->new_level = mddev->level;
4539 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004540 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004541 rdev_for_each(rdev, mddev)
4542 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004543 return len;
4544}
4545
4546static struct md_sysfs_entry md_reshape_position =
4547__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4548 reshape_position_store);
4549
Dan Williamsb522adc2009-03-31 15:00:31 +11004550static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004551reshape_direction_show(struct mddev *mddev, char *page)
4552{
4553 return sprintf(page, "%s\n",
4554 mddev->reshape_backwards ? "backwards" : "forwards");
4555}
4556
4557static ssize_t
4558reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4559{
4560 int backwards = 0;
4561 if (cmd_match(buf, "forwards"))
4562 backwards = 0;
4563 else if (cmd_match(buf, "backwards"))
4564 backwards = 1;
4565 else
4566 return -EINVAL;
4567 if (mddev->reshape_backwards == backwards)
4568 return len;
4569
4570 /* check if we are allowed to change */
4571 if (mddev->delta_disks)
4572 return -EBUSY;
4573
4574 if (mddev->persistent &&
4575 mddev->major_version == 0)
4576 return -EINVAL;
4577
4578 mddev->reshape_backwards = backwards;
4579 return len;
4580}
4581
4582static struct md_sysfs_entry md_reshape_direction =
4583__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4584 reshape_direction_store);
4585
4586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004587array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004588{
4589 if (mddev->external_size)
4590 return sprintf(page, "%llu\n",
4591 (unsigned long long)mddev->array_sectors/2);
4592 else
4593 return sprintf(page, "default\n");
4594}
4595
4596static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004597array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004598{
4599 sector_t sectors;
4600
4601 if (strncmp(buf, "default", 7) == 0) {
4602 if (mddev->pers)
4603 sectors = mddev->pers->size(mddev, 0, 0);
4604 else
4605 sectors = mddev->array_sectors;
4606
4607 mddev->external_size = 0;
4608 } else {
4609 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4610 return -EINVAL;
4611 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004612 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004613
4614 mddev->external_size = 1;
4615 }
4616
4617 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004618 if (mddev->pers) {
4619 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004620 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004621 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004622 return len;
4623}
4624
4625static struct md_sysfs_entry md_array_size =
4626__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4627 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004628
NeilBrowneae17012005-11-08 21:39:23 -08004629static struct attribute *md_default_attrs[] = {
4630 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004631 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004632 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004633 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004634 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004635 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004636 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004637 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004638 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004639 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004640 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004641 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004642 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004643 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004644 NULL,
4645};
4646
4647static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004648 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004649 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004650 &md_sync_min.attr,
4651 &md_sync_max.attr,
4652 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004653 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004654 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004655 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004656 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004657 &md_suspend_lo.attr,
4658 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004659 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004660 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004661 NULL,
4662};
NeilBrown411036f2005-11-08 21:39:40 -08004663static struct attribute_group md_redundancy_group = {
4664 .name = NULL,
4665 .attrs = md_redundancy_attrs,
4666};
4667
NeilBrowneae17012005-11-08 21:39:23 -08004668
4669static ssize_t
4670md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4671{
4672 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004673 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004674 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004675
4676 if (!entry->show)
4677 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004678 spin_lock(&all_mddevs_lock);
4679 if (list_empty(&mddev->all_mddevs)) {
4680 spin_unlock(&all_mddevs_lock);
4681 return -EBUSY;
4682 }
4683 mddev_get(mddev);
4684 spin_unlock(&all_mddevs_lock);
4685
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004686 rv = mddev_lock(mddev);
4687 if (!rv) {
4688 rv = entry->show(mddev, page);
4689 mddev_unlock(mddev);
4690 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004691 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004692 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004693}
4694
4695static ssize_t
4696md_attr_store(struct kobject *kobj, struct attribute *attr,
4697 const char *page, size_t length)
4698{
4699 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004700 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004701 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004702
4703 if (!entry->store)
4704 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004705 if (!capable(CAP_SYS_ADMIN))
4706 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004707 spin_lock(&all_mddevs_lock);
4708 if (list_empty(&mddev->all_mddevs)) {
4709 spin_unlock(&all_mddevs_lock);
4710 return -EBUSY;
4711 }
4712 mddev_get(mddev);
4713 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004714 rv = mddev_lock(mddev);
4715 if (!rv) {
4716 rv = entry->store(mddev, page, length);
4717 mddev_unlock(mddev);
4718 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004719 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004720 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004721}
4722
4723static void md_free(struct kobject *ko)
4724{
NeilBrownfd01b882011-10-11 16:47:53 +11004725 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004726
4727 if (mddev->sysfs_state)
4728 sysfs_put(mddev->sysfs_state);
4729
4730 if (mddev->gendisk) {
4731 del_gendisk(mddev->gendisk);
4732 put_disk(mddev->gendisk);
4733 }
4734 if (mddev->queue)
4735 blk_cleanup_queue(mddev->queue);
4736
NeilBrowneae17012005-11-08 21:39:23 -08004737 kfree(mddev);
4738}
4739
Emese Revfy52cf25d2010-01-19 02:58:23 +01004740static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004741 .show = md_attr_show,
4742 .store = md_attr_store,
4743};
4744static struct kobj_type md_ktype = {
4745 .release = md_free,
4746 .sysfs_ops = &md_sysfs_ops,
4747 .default_attrs = md_default_attrs,
4748};
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750int mdp_major = 0;
4751
Dan Williams5fd3a172009-03-04 00:57:25 -07004752static void mddev_delayed_delete(struct work_struct *ws)
4753{
NeilBrownfd01b882011-10-11 16:47:53 +11004754 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004755
NeilBrown43a70502009-12-14 12:49:55 +11004756 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004757 kobject_del(&mddev->kobj);
4758 kobject_put(&mddev->kobj);
4759}
4760
NeilBrownefeb53c2009-01-09 08:31:10 +11004761static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004763 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004764 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004766 int partitioned;
4767 int shift;
4768 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004769 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004772 return -ENODEV;
4773
4774 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4775 shift = partitioned ? MdpMinorShift : 0;
4776 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Tejun Heoe804ac72010-10-15 15:36:08 +02004778 /* wait for any previous instance of this device to be
4779 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004780 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004781 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004782
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004783 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004784 error = -EEXIST;
4785 if (mddev->gendisk)
4786 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004787
4788 if (name) {
4789 /* Need to ensure that 'name' is not a duplicate.
4790 */
NeilBrownfd01b882011-10-11 16:47:53 +11004791 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004792 spin_lock(&all_mddevs_lock);
4793
4794 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4795 if (mddev2->gendisk &&
4796 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4797 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004798 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004799 }
4800 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 }
NeilBrown8b765392009-01-09 08:31:08 +11004802
NeilBrown0909dc42009-07-01 12:27:21 +10004803 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004804 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004805 if (!mddev->queue)
4806 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004807 mddev->queue->queuedata = mddev;
4808
NeilBrown409c57f2009-03-31 14:39:39 +11004809 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004810 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 disk = alloc_disk(1 << shift);
4813 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004814 blk_cleanup_queue(mddev->queue);
4815 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004816 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004818 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004820 if (name)
4821 strcpy(disk->disk_name, name);
4822 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004824 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 disk->fops = &md_fops;
4827 disk->private_data = mddev;
4828 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004829 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004830 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004831 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004832 * remove it now.
4833 */
4834 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004836 /* As soon as we call add_disk(), another thread could get
4837 * through to md_open, so make sure it doesn't get too far
4838 */
4839 mutex_lock(&mddev->open_mutex);
4840 add_disk(disk);
4841
Tejun Heoed9e1982008-08-25 19:56:05 +09004842 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4843 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004844 if (error) {
4845 /* This isn't possible, but as kobject_init_and_add is marked
4846 * __must_check, we must do something with the result
4847 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004848 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4849 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004850 error = 0;
4851 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004852 if (mddev->kobj.sd &&
4853 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004854 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004855 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004856 abort:
4857 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004858 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004859 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004860 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004861 }
NeilBrownd3374822009-01-09 08:31:10 +11004862 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004863 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004864}
4865
4866static struct kobject *md_probe(dev_t dev, int *part, void *data)
4867{
4868 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return NULL;
4870}
4871
NeilBrownefeb53c2009-01-09 08:31:10 +11004872static int add_named_array(const char *val, struct kernel_param *kp)
4873{
4874 /* val must be "md_*" where * is not all digits.
4875 * We allocate an array with a large free minor number, and
4876 * set the name to val. val must not already be an active name.
4877 */
4878 int len = strlen(val);
4879 char buf[DISK_NAME_LEN];
4880
4881 while (len && val[len-1] == '\n')
4882 len--;
4883 if (len >= DISK_NAME_LEN)
4884 return -E2BIG;
4885 strlcpy(buf, val, len+1);
4886 if (strncmp(buf, "md_", 3) != 0)
4887 return -EINVAL;
4888 return md_alloc(0, buf);
4889}
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891static void md_safemode_timeout(unsigned long data)
4892{
NeilBrownfd01b882011-10-11 16:47:53 +11004893 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Neil Brown0fd62b82008-06-28 08:31:36 +10004895 if (!atomic_read(&mddev->writes_pending)) {
4896 mddev->safemode = 1;
4897 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004898 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 md_wakeup_thread(mddev->thread);
4901}
4902
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004903static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
NeilBrownfd01b882011-10-11 16:47:53 +11004905int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906{
NeilBrown2604b702006-01-06 00:20:36 -08004907 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004908 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004909 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
NeilBrowna757e642005-04-16 15:26:42 -07004911 if (list_empty(&mddev->disks))
4912 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 if (mddev->pers)
4916 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004917 /* Cannot run until previous stop completes properly */
4918 if (mddev->sysfs_active)
4919 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 /*
4922 * Analyze all RAID superblock(s)
4923 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004924 if (!mddev->raid_disks) {
4925 if (!mddev->persistent)
4926 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004927 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
NeilBrownd9d166c2006-01-06 00:20:51 -08004930 if (mddev->level != LEVEL_NONE)
4931 request_module("md-level-%d", mddev->level);
4932 else if (mddev->clevel[0])
4933 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
4935 /*
4936 * Drop all container device buffers, from now on
4937 * the only valid external interface is through the md
4938 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004940 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004941 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 continue;
4943 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004944 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004945
4946 /* perform some consistency tests on the device.
4947 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004948 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004949 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004950 if (rdev->meta_bdev) {
4951 /* Nothing to check */;
4952 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004953 if (mddev->dev_sectors &&
4954 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004955 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004956 printk("md: %s: data overlaps metadata\n",
4957 mdname(mddev));
4958 return -EINVAL;
4959 }
4960 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004961 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004962 > rdev->data_offset) {
4963 printk("md: %s: metadata overlaps data\n",
4964 mdname(mddev));
4965 return -EINVAL;
4966 }
4967 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004968 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970
NeilBrowna167f662010-10-26 18:31:13 +11004971 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004972 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004975 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004976 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004978 if (mddev->level != LEVEL_NONE)
4979 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4980 mddev->level);
4981 else
4982 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4983 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 return -EINVAL;
4985 }
NeilBrown2604b702006-01-06 00:20:36 -08004986 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004988 if (mddev->level != pers->level) {
4989 mddev->level = pers->level;
4990 mddev->new_level = pers->level;
4991 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004992 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
NeilBrownf6705572006-03-27 01:18:11 -08004994 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004995 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004996 /* This personality cannot handle reshaping... */
4997 mddev->pers = NULL;
4998 module_put(pers->owner);
4999 return -EINVAL;
5000 }
5001
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005002 if (pers->sync_request) {
5003 /* Warn if this is a potentially silly
5004 * configuration.
5005 */
5006 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005007 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005008 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005009
NeilBrowndafb20f2012-03-19 12:46:39 +11005010 rdev_for_each(rdev, mddev)
5011 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005012 if (rdev < rdev2 &&
5013 rdev->bdev->bd_contains ==
5014 rdev2->bdev->bd_contains) {
5015 printk(KERN_WARNING
5016 "%s: WARNING: %s appears to be"
5017 " on the same physical disk as"
5018 " %s.\n",
5019 mdname(mddev),
5020 bdevname(rdev->bdev,b),
5021 bdevname(rdev2->bdev,b2));
5022 warned = 1;
5023 }
5024 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005025
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005026 if (warned)
5027 printk(KERN_WARNING
5028 "True protection against single-disk"
5029 " failure might be compromised.\n");
5030 }
5031
NeilBrown657390d2005-08-26 18:34:16 -07005032 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005033 /* may be over-ridden by personality */
5034 mddev->resync_max_sectors = mddev->dev_sectors;
5035
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005036 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
NeilBrown0f9552b52009-12-30 12:08:50 +11005038 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005039 mddev->ro = 2; /* read-only, but switch on first write */
5040
NeilBrownb15c2e52006-01-06 00:20:16 -08005041 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005042 if (err)
5043 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005044 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5045 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5046 " but 'external_size' not in effect?\n", __func__);
5047 printk(KERN_ERR
5048 "md: invalid array_size %llu > default size %llu\n",
5049 (unsigned long long)mddev->array_sectors / 2,
5050 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5051 err = -EINVAL;
5052 mddev->pers->stop(mddev);
5053 }
NeilBrownef99bf42012-05-22 13:55:08 +10005054 if (err == 0 && mddev->pers->sync_request &&
5055 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005056 err = bitmap_create(mddev);
5057 if (err) {
5058 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5059 mdname(mddev), err);
5060 mddev->pers->stop(mddev);
5061 }
5062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 module_put(mddev->pers->owner);
5065 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005066 bitmap_destroy(mddev);
5067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005069 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005070 if (mddev->kobj.sd &&
5071 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005072 printk(KERN_WARNING
5073 "md: cannot register extra attributes for %s\n",
5074 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005075 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005076 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005077 mddev->ro = 0;
5078
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005080 atomic_set(&mddev->max_corr_read_errors,
5081 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 mddev->safemode = 0;
5083 mddev->safemode_timer.function = md_safemode_timeout;
5084 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005085 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005087 smp_wmb();
5088 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005089 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005090 if (rdev->raid_disk >= 0)
5091 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005092 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5095
NeilBrown850b2b42006-10-03 01:15:46 -07005096 if (mddev->flags)
5097 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
NeilBrownd7603b72006-01-06 00:20:30 -08005099 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005100 sysfs_notify_dirent_safe(mddev->sysfs_state);
5101 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005102 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return 0;
5104}
NeilBrown390ee602010-06-01 19:37:27 +10005105EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrownfd01b882011-10-11 16:47:53 +11005107static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005108{
5109 int err;
5110
5111 err = md_run(mddev);
5112 if (err)
5113 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005114 err = bitmap_load(mddev);
5115 if (err) {
5116 bitmap_destroy(mddev);
5117 goto out;
5118 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005119
5120 md_wakeup_thread(mddev->thread);
5121 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5122
NeilBrownfe60b012010-03-29 11:10:42 +11005123 set_capacity(mddev->gendisk, mddev->array_sectors);
5124 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005125 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005126 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5127out:
5128 return err;
5129}
5130
NeilBrownfd01b882011-10-11 16:47:53 +11005131static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132{
5133 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Andre Noll80fab1d2008-07-11 22:02:21 +10005135 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005137 return -ENXIO;
5138 if (!mddev->pers)
5139 return -EINVAL;
5140 if (!mddev->ro)
5141 return -EBUSY;
5142 mddev->safemode = 0;
5143 mddev->ro = 0;
5144 set_disk_ro(disk, 0);
5145 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5146 mdname(mddev));
5147 /* Kick recovery or resync if necessary */
5148 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5149 md_wakeup_thread(mddev->thread);
5150 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005151 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153}
5154
NeilBrownacc55e22006-06-26 00:27:47 -07005155/* similar to deny_write_access, but accounts for our holding a reference
5156 * to the file ourselves */
5157static int deny_bitmap_write_access(struct file * file)
5158{
5159 struct inode *inode = file->f_mapping->host;
5160
5161 spin_lock(&inode->i_lock);
5162 if (atomic_read(&inode->i_writecount) > 1) {
5163 spin_unlock(&inode->i_lock);
5164 return -ETXTBSY;
5165 }
5166 atomic_set(&inode->i_writecount, -1);
5167 spin_unlock(&inode->i_lock);
5168
5169 return 0;
5170}
5171
NeilBrown43a70502009-12-14 12:49:55 +11005172void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005173{
5174 struct inode *inode = file->f_mapping->host;
5175
5176 spin_lock(&inode->i_lock);
5177 atomic_set(&inode->i_writecount, 1);
5178 spin_unlock(&inode->i_lock);
5179}
5180
NeilBrownfd01b882011-10-11 16:47:53 +11005181static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005182{
5183 mddev->array_sectors = 0;
5184 mddev->external_size = 0;
5185 mddev->dev_sectors = 0;
5186 mddev->raid_disks = 0;
5187 mddev->recovery_cp = 0;
5188 mddev->resync_min = 0;
5189 mddev->resync_max = MaxSector;
5190 mddev->reshape_position = MaxSector;
5191 mddev->external = 0;
5192 mddev->persistent = 0;
5193 mddev->level = LEVEL_NONE;
5194 mddev->clevel[0] = 0;
5195 mddev->flags = 0;
5196 mddev->ro = 0;
5197 mddev->metadata_type[0] = 0;
5198 mddev->chunk_sectors = 0;
5199 mddev->ctime = mddev->utime = 0;
5200 mddev->layout = 0;
5201 mddev->max_disks = 0;
5202 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005203 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005204 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005205 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005206 mddev->new_level = LEVEL_NONE;
5207 mddev->new_layout = 0;
5208 mddev->new_chunk_sectors = 0;
5209 mddev->curr_resync = 0;
5210 mddev->resync_mismatches = 0;
5211 mddev->suspend_lo = mddev->suspend_hi = 0;
5212 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5213 mddev->recovery = 0;
5214 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005215 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005216 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005217 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005218 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005219 mddev->bitmap_info.offset = 0;
5220 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005221 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005222 mddev->bitmap_info.chunksize = 0;
5223 mddev->bitmap_info.daemon_sleep = 0;
5224 mddev->bitmap_info.max_write_behind = 0;
5225}
5226
NeilBrownfd01b882011-10-11 16:47:53 +11005227static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005228{
5229 if (mddev->sync_thread) {
5230 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5231 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005232 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005233 }
5234
5235 del_timer_sync(&mddev->safemode_timer);
5236
5237 bitmap_flush(mddev);
5238 md_super_wait(mddev);
5239
5240 if (!mddev->in_sync || mddev->flags) {
5241 /* mark array as shutdown cleanly */
5242 mddev->in_sync = 1;
5243 md_update_sb(mddev, 1);
5244 }
5245}
NeilBrowndefad612011-01-14 09:14:33 +11005246
NeilBrownfd01b882011-10-11 16:47:53 +11005247void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005248{
5249 mddev_lock(mddev);
5250 __md_stop_writes(mddev);
5251 mddev_unlock(mddev);
5252}
NeilBrown390ee602010-06-01 19:37:27 +10005253EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005254
NeilBrownfd01b882011-10-11 16:47:53 +11005255void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005256{
NeilBrown0ca69882011-01-14 09:14:33 +11005257 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005258 mddev->pers->stop(mddev);
5259 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5260 mddev->to_remove = &md_redundancy_group;
5261 module_put(mddev->pers->owner);
5262 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005263 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005264}
NeilBrown390ee602010-06-01 19:37:27 +10005265EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005266
NeilBrowna05b7ea2012-07-19 15:59:18 +10005267static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d02010-03-29 13:23:10 +11005268{
5269 int err = 0;
5270 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005271 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005272 printk("md: %s still in use.\n",mdname(mddev));
5273 err = -EBUSY;
5274 goto out;
5275 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005276 if (bdev)
5277 sync_blockdev(bdev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005278 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005279 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005280
5281 err = -ENXIO;
5282 if (mddev->ro==1)
5283 goto out;
5284 mddev->ro = 1;
5285 set_disk_ro(mddev->gendisk, 1);
5286 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005287 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005288 err = 0;
5289 }
5290out:
5291 mutex_unlock(&mddev->open_mutex);
5292 return err;
5293}
5294
NeilBrown9e653b62006-06-26 00:27:58 -07005295/* mode:
5296 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005297 * 2 - stop but do not disassemble array
5298 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005299static int do_md_stop(struct mddev * mddev, int mode,
5300 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005303 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
NeilBrownc8c00a62009-08-10 12:50:52 +10005305 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005306 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005307 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005308 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005309 mutex_unlock(&mddev->open_mutex);
5310 return -EBUSY;
5311 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005312 if (bdev)
5313 /* It is possible IO was issued on some other
5314 * open file which was closed before we took ->open_mutex.
5315 * As that was not the last close __blkdev_put will not
5316 * have called sync_blockdev, so we must.
5317 */
5318 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
NeilBrown6e17b022010-08-07 21:41:19 +10005320 if (mddev->pers) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005321 if (mddev->ro)
5322 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005323
NeilBrowndefad612011-01-14 09:14:33 +11005324 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005325 md_stop(mddev);
5326 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005327 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005328
NeilBrowna4bd82d02010-03-29 13:23:10 +11005329 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005330 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005331
NeilBrowndafb20f2012-03-19 12:46:39 +11005332 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005333 if (rdev->raid_disk >= 0)
5334 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005335
NeilBrowna4bd82d02010-03-29 13:23:10 +11005336 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005337 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005338 mddev->changed = 1;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005339 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005340
NeilBrowna4bd82d02010-03-29 13:23:10 +11005341 if (mddev->ro)
5342 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005343 } else
5344 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 /*
5346 * Free resources if final stop
5347 */
NeilBrown9e653b62006-06-26 00:27:58 -07005348 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5350
NeilBrown978f9462006-02-02 14:28:05 -08005351 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005352 if (mddev->bitmap_info.file) {
5353 restore_bitmap_write_access(mddev->bitmap_info.file);
5354 fput(mddev->bitmap_info.file);
5355 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005356 }
NeilBrownc3d97142009-12-14 12:49:52 +11005357 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005358
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 export_array(mddev);
5360
NeilBrown6177b472010-03-29 11:37:13 +11005361 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005362 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005363 if (mddev->hold_active == UNTIL_STOP)
5364 mddev->hold_active = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005365 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005366 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005367 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005368 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005372#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005373static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
NeilBrown3cb03002011-10-11 16:45:26 +11005375 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 int err;
5377
NeilBrowna757e642005-04-16 15:26:42 -07005378 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 printk(KERN_INFO "md: running: ");
5382
NeilBrowndafb20f2012-03-19 12:46:39 +11005383 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 char b[BDEVNAME_SIZE];
5385 printk("<%s>", bdevname(rdev->bdev,b));
5386 }
5387 printk("\n");
5388
NeilBrownd710e132008-10-13 11:55:12 +11005389 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (err) {
5391 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005392 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 }
5394}
5395
5396/*
5397 * lets try to run arrays based on all disks that have arrived
5398 * until now. (those are in pending_raid_disks)
5399 *
5400 * the method: pick the first pending disk, collect all disks with
5401 * the same UUID, remove all from the pending list and put them into
5402 * the 'same_array' list. Then order this list based on superblock
5403 * update time (freshest comes first), kick out 'old' disks and
5404 * compare superblocks. If everything's fine then run it.
5405 *
5406 * If "unit" is allocated, then bump its reference count
5407 */
5408static void autorun_devices(int part)
5409{
NeilBrown3cb03002011-10-11 16:45:26 +11005410 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005411 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 char b[BDEVNAME_SIZE];
5413
5414 printk(KERN_INFO "md: autorun ...\n");
5415 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005416 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005418 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005420 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
5422 printk(KERN_INFO "md: considering %s ...\n",
5423 bdevname(rdev0->bdev,b));
5424 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005425 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 if (super_90_load(rdev, rdev0, 0) >= 0) {
5427 printk(KERN_INFO "md: adding %s ...\n",
5428 bdevname(rdev->bdev,b));
5429 list_move(&rdev->same_set, &candidates);
5430 }
5431 /*
5432 * now we have a set of devices, with all of them having
5433 * mostly sane superblocks. It's time to allocate the
5434 * mddev.
5435 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005436 if (part) {
5437 dev = MKDEV(mdp_major,
5438 rdev0->preferred_minor << MdpMinorShift);
5439 unit = MINOR(dev) >> MdpMinorShift;
5440 } else {
5441 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5442 unit = MINOR(dev);
5443 }
5444 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5446 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5447 break;
5448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
5450 md_probe(dev, NULL, NULL);
5451 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005452 if (!mddev || !mddev->gendisk) {
5453 if (mddev)
5454 mddev_put(mddev);
5455 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 "md: cannot allocate memory for md drive.\n");
5457 break;
5458 }
5459 if (mddev_lock(mddev))
5460 printk(KERN_WARNING "md: %s locked, cannot run\n",
5461 mdname(mddev));
5462 else if (mddev->raid_disks || mddev->major_version
5463 || !list_empty(&mddev->disks)) {
5464 printk(KERN_WARNING
5465 "md: %s already running, cannot run %s\n",
5466 mdname(mddev), bdevname(rdev0->bdev,b));
5467 mddev_unlock(mddev);
5468 } else {
5469 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005470 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005471 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 list_del_init(&rdev->same_set);
5473 if (bind_rdev_to_array(rdev, mddev))
5474 export_rdev(rdev);
5475 }
5476 autorun_array(mddev);
5477 mddev_unlock(mddev);
5478 }
5479 /* on success, candidates will be empty, on error
5480 * it won't...
5481 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005482 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005483 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 mddev_put(mddev);
5487 }
5488 printk(KERN_INFO "md: ... autorun DONE.\n");
5489}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005490#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492static int get_version(void __user * arg)
5493{
5494 mdu_version_t ver;
5495
5496 ver.major = MD_MAJOR_VERSION;
5497 ver.minor = MD_MINOR_VERSION;
5498 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5499
5500 if (copy_to_user(arg, &ver, sizeof(ver)))
5501 return -EFAULT;
5502
5503 return 0;
5504}
5505
NeilBrownfd01b882011-10-11 16:47:53 +11005506static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
5508 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005509 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005510 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
NeilBrowna9f326e2009-09-23 18:06:41 +10005512 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005513 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005515 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 failed++;
5517 else {
5518 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005519 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005520 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 else
5522 spare++;
5523 }
5524 }
5525
5526 info.major_version = mddev->major_version;
5527 info.minor_version = mddev->minor_version;
5528 info.patch_version = MD_PATCHLEVEL_VERSION;
5529 info.ctime = mddev->ctime;
5530 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005531 info.size = mddev->dev_sectors / 2;
5532 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005533 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 info.nr_disks = nr;
5535 info.raid_disks = mddev->raid_disks;
5536 info.md_minor = mddev->md_minor;
5537 info.not_persistent= !mddev->persistent;
5538
5539 info.utime = mddev->utime;
5540 info.state = 0;
5541 if (mddev->in_sync)
5542 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005543 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005544 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005545 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 info.working_disks = working;
5547 info.failed_disks = failed;
5548 info.spare_disks = spare;
5549
5550 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005551 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 if (copy_to_user(arg, &info, sizeof(info)))
5554 return -EFAULT;
5555
5556 return 0;
5557}
5558
NeilBrownfd01b882011-10-11 16:47:53 +11005559static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005560{
5561 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5562 char *ptr, *buf = NULL;
5563 int err = -ENOMEM;
5564
Dan Williamsb5470dc2008-06-27 21:44:04 -07005565 if (md_allow_write(mddev))
5566 file = kmalloc(sizeof(*file), GFP_NOIO);
5567 else
5568 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005569
NeilBrown32a76272005-06-21 17:17:14 -07005570 if (!file)
5571 goto out;
5572
5573 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005574 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005575 file->pathname[0] = '\0';
5576 goto copy_out;
5577 }
5578
5579 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5580 if (!buf)
5581 goto out;
5582
NeilBrown1ec885c2012-05-22 13:55:10 +10005583 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5584 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005585 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005586 goto out;
5587
5588 strcpy(file->pathname, ptr);
5589
5590copy_out:
5591 err = 0;
5592 if (copy_to_user(arg, file, sizeof(*file)))
5593 err = -EFAULT;
5594out:
5595 kfree(buf);
5596 kfree(file);
5597 return err;
5598}
5599
NeilBrownfd01b882011-10-11 16:47:53 +11005600static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005603 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 if (copy_from_user(&info, arg, sizeof(info)))
5606 return -EFAULT;
5607
Andre Noll26ef3792008-07-11 22:02:21 +10005608 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 if (rdev) {
5610 info.major = MAJOR(rdev->bdev->bd_dev);
5611 info.minor = MINOR(rdev->bdev->bd_dev);
5612 info.raid_disk = rdev->raid_disk;
5613 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005614 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005616 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 info.state |= (1<<MD_DISK_ACTIVE);
5618 info.state |= (1<<MD_DISK_SYNC);
5619 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005620 if (test_bit(WriteMostly, &rdev->flags))
5621 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 } else {
5623 info.major = info.minor = 0;
5624 info.raid_disk = -1;
5625 info.state = (1<<MD_DISK_REMOVED);
5626 }
5627
5628 if (copy_to_user(arg, &info, sizeof(info)))
5629 return -EFAULT;
5630
5631 return 0;
5632}
5633
NeilBrownfd01b882011-10-11 16:47:53 +11005634static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635{
5636 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005637 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 dev_t dev = MKDEV(info->major,info->minor);
5639
5640 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5641 return -EOVERFLOW;
5642
5643 if (!mddev->raid_disks) {
5644 int err;
5645 /* expecting a device which has a superblock */
5646 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5647 if (IS_ERR(rdev)) {
5648 printk(KERN_WARNING
5649 "md: md_import_device returned %ld\n",
5650 PTR_ERR(rdev));
5651 return PTR_ERR(rdev);
5652 }
5653 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005654 struct md_rdev *rdev0
5655 = list_entry(mddev->disks.next,
5656 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005657 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 .load_super(rdev, rdev0, mddev->minor_version);
5659 if (err < 0) {
5660 printk(KERN_WARNING
5661 "md: %s has different UUID to %s\n",
5662 bdevname(rdev->bdev,b),
5663 bdevname(rdev0->bdev,b2));
5664 export_rdev(rdev);
5665 return -EINVAL;
5666 }
5667 }
5668 err = bind_rdev_to_array(rdev, mddev);
5669 if (err)
5670 export_rdev(rdev);
5671 return err;
5672 }
5673
5674 /*
5675 * add_new_disk can be used once the array is assembled
5676 * to add "hot spares". They must already have a superblock
5677 * written
5678 */
5679 if (mddev->pers) {
5680 int err;
5681 if (!mddev->pers->hot_add_disk) {
5682 printk(KERN_WARNING
5683 "%s: personality does not support diskops!\n",
5684 mdname(mddev));
5685 return -EINVAL;
5686 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005687 if (mddev->persistent)
5688 rdev = md_import_device(dev, mddev->major_version,
5689 mddev->minor_version);
5690 else
5691 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 if (IS_ERR(rdev)) {
5693 printk(KERN_WARNING
5694 "md: md_import_device returned %ld\n",
5695 PTR_ERR(rdev));
5696 return PTR_ERR(rdev);
5697 }
NeilBrown1a855a02010-12-09 16:36:28 +11005698 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005699 if (!mddev->persistent) {
5700 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005701 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005702 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005703 set_bit(In_sync, &rdev->flags);
5704 } else
NeilBrown41158c72005-06-21 17:17:25 -07005705 rdev->raid_disk = -1;
5706 } else
5707 super_types[mddev->major_version].
5708 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005709 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005710 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005711 /* This was a hot-add request, but events doesn't
5712 * match, so reject it.
5713 */
5714 export_rdev(rdev);
5715 return -EINVAL;
5716 }
5717
NeilBrown1a855a02010-12-09 16:36:28 +11005718 if (test_bit(In_sync, &rdev->flags))
5719 rdev->saved_raid_disk = rdev->raid_disk;
5720 else
5721 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005722
NeilBrownb2d444d2005-11-08 21:39:31 -08005723 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005724 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5725 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005726 else
5727 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 rdev->raid_disk = -1;
5730 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005731 if (!err && !mddev->pers->hot_remove_disk) {
5732 /* If there is hot_add_disk but no hot_remove_disk
5733 * then added disks for geometry changes,
5734 * and should be added immediately.
5735 */
5736 super_types[mddev->major_version].
5737 validate_super(mddev, rdev);
5738 err = mddev->pers->hot_add_disk(mddev, rdev);
5739 if (err)
5740 unbind_rdev_from_array(rdev);
5741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 if (err)
5743 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005744 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005745 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005746
NeilBrown17571282006-12-10 02:20:52 -08005747 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005748 if (mddev->degraded)
5749 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005750 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005751 if (!err)
5752 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005753 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 return err;
5755 }
5756
5757 /* otherwise, add_new_disk is only allowed
5758 * for major_version==0 superblocks
5759 */
5760 if (mddev->major_version != 0) {
5761 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5762 mdname(mddev));
5763 return -EINVAL;
5764 }
5765
5766 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5767 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005768 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 if (IS_ERR(rdev)) {
5770 printk(KERN_WARNING
5771 "md: error, md_import_device() returned %ld\n",
5772 PTR_ERR(rdev));
5773 return PTR_ERR(rdev);
5774 }
5775 rdev->desc_nr = info->number;
5776 if (info->raid_disk < mddev->raid_disks)
5777 rdev->raid_disk = info->raid_disk;
5778 else
5779 rdev->raid_disk = -1;
5780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005782 if (info->state & (1<<MD_DISK_SYNC))
5783 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005785 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5786 set_bit(WriteMostly, &rdev->flags);
5787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 if (!mddev->persistent) {
5789 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005790 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5791 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005792 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005793 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
NeilBrown2bf071b2006-01-06 00:20:55 -08005795 err = bind_rdev_to_array(rdev, mddev);
5796 if (err) {
5797 export_rdev(rdev);
5798 return err;
5799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 }
5801
5802 return 0;
5803}
5804
NeilBrownfd01b882011-10-11 16:47:53 +11005805static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806{
5807 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005808 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 rdev = find_rdev(mddev, dev);
5811 if (!rdev)
5812 return -ENXIO;
5813
5814 if (rdev->raid_disk >= 0)
5815 goto busy;
5816
5817 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005818 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005819 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 return 0;
5822busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005823 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 bdevname(rdev->bdev,b), mdname(mddev));
5825 return -EBUSY;
5826}
5827
NeilBrownfd01b882011-10-11 16:47:53 +11005828static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
5830 char b[BDEVNAME_SIZE];
5831 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005832 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 if (!mddev->pers)
5835 return -ENODEV;
5836
5837 if (mddev->major_version != 0) {
5838 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5839 " version-0 superblocks.\n",
5840 mdname(mddev));
5841 return -EINVAL;
5842 }
5843 if (!mddev->pers->hot_add_disk) {
5844 printk(KERN_WARNING
5845 "%s: personality does not support diskops!\n",
5846 mdname(mddev));
5847 return -EINVAL;
5848 }
5849
NeilBrownd710e132008-10-13 11:55:12 +11005850 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 if (IS_ERR(rdev)) {
5852 printk(KERN_WARNING
5853 "md: error, md_import_device() returned %ld\n",
5854 PTR_ERR(rdev));
5855 return -EINVAL;
5856 }
5857
5858 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005859 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005861 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
NeilBrown8190e752009-06-18 08:48:58 +10005863 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
NeilBrownb2d444d2005-11-08 21:39:31 -08005865 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 printk(KERN_WARNING
5867 "md: can not hot-add faulty %s disk to %s!\n",
5868 bdevname(rdev->bdev,b), mdname(mddev));
5869 err = -EINVAL;
5870 goto abort_export;
5871 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005872 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005874 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005875 err = bind_rdev_to_array(rdev, mddev);
5876 if (err)
5877 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
5879 /*
5880 * The rest should better be atomic, we can have disk failures
5881 * noticed in interrupt contexts ...
5882 */
5883
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 rdev->raid_disk = -1;
5885
NeilBrown850b2b42006-10-03 01:15:46 -07005886 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
5888 /*
5889 * Kick recovery, maybe this spare has to be added to the
5890 * array immediately.
5891 */
5892 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5893 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005894 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 return 0;
5896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897abort_export:
5898 export_rdev(rdev);
5899 return err;
5900}
5901
NeilBrownfd01b882011-10-11 16:47:53 +11005902static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005903{
5904 int err;
5905
NeilBrown36fa3062005-09-09 16:23:45 -07005906 if (mddev->pers) {
5907 if (!mddev->pers->quiesce)
5908 return -EBUSY;
5909 if (mddev->recovery || mddev->sync_thread)
5910 return -EBUSY;
5911 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005912 }
5913
NeilBrown36fa3062005-09-09 16:23:45 -07005914
5915 if (fd >= 0) {
5916 if (mddev->bitmap)
5917 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005918 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005919
NeilBrownc3d97142009-12-14 12:49:52 +11005920 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005921 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5922 mdname(mddev));
5923 return -EBADF;
5924 }
5925
NeilBrownc3d97142009-12-14 12:49:52 +11005926 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005927 if (err) {
5928 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5929 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005930 fput(mddev->bitmap_info.file);
5931 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005932 return err;
5933 }
NeilBrownc3d97142009-12-14 12:49:52 +11005934 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005935 } else if (mddev->bitmap == NULL)
5936 return -ENOENT; /* cannot remove what isn't there */
5937 err = 0;
5938 if (mddev->pers) {
5939 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005940 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005941 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005942 if (!err)
5943 err = bitmap_load(mddev);
5944 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005945 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005946 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005947 fd = -1; /* make sure to put the file */
5948 }
NeilBrown36fa3062005-09-09 16:23:45 -07005949 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005950 }
5951 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005952 if (mddev->bitmap_info.file) {
5953 restore_bitmap_write_access(mddev->bitmap_info.file);
5954 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005955 }
NeilBrownc3d97142009-12-14 12:49:52 +11005956 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005957 }
5958
NeilBrown32a76272005-06-21 17:17:14 -07005959 return err;
5960}
5961
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962/*
5963 * set_array_info is used two different ways
5964 * The original usage is when creating a new array.
5965 * In this usage, raid_disks is > 0 and it together with
5966 * level, size, not_persistent,layout,chunksize determine the
5967 * shape of the array.
5968 * This will always create an array with a type-0.90.0 superblock.
5969 * The newer usage is when assembling an array.
5970 * In this case raid_disks will be 0, and the major_version field is
5971 * use to determine which style super-blocks are to be found on the devices.
5972 * The minor and patch _version numbers are also kept incase the
5973 * super_block handler wishes to interpret them.
5974 */
NeilBrownfd01b882011-10-11 16:47:53 +11005975static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976{
5977
5978 if (info->raid_disks == 0) {
5979 /* just setting version number for superblock loading */
5980 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005981 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 super_types[info->major_version].name == NULL) {
5983 /* maybe try to auto-load a module? */
5984 printk(KERN_INFO
5985 "md: superblock version %d not known\n",
5986 info->major_version);
5987 return -EINVAL;
5988 }
5989 mddev->major_version = info->major_version;
5990 mddev->minor_version = info->minor_version;
5991 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005992 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005993 /* ensure mddev_put doesn't delete this now that there
5994 * is some minimal configuration.
5995 */
5996 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 return 0;
5998 }
5999 mddev->major_version = MD_MAJOR_VERSION;
6000 mddev->minor_version = MD_MINOR_VERSION;
6001 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6002 mddev->ctime = get_seconds();
6003
6004 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006005 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006006 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 mddev->raid_disks = info->raid_disks;
6008 /* don't set md_minor, it is determined by which /dev/md* was
6009 * openned
6010 */
6011 if (info->state & (1<<MD_SB_CLEAN))
6012 mddev->recovery_cp = MaxSector;
6013 else
6014 mddev->recovery_cp = 0;
6015 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006016 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
6018 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006019 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 mddev->max_disks = MD_SB_DISKS;
6022
NeilBrowne6910632008-02-06 01:39:51 -08006023 if (mddev->persistent)
6024 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006025 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
NeilBrownc3d97142009-12-14 12:49:52 +11006027 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006028 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006029 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006030
NeilBrownf6705572006-03-27 01:18:11 -08006031 mddev->reshape_position = MaxSector;
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 /*
6034 * Generate a 128 bit UUID
6035 */
6036 get_random_bytes(mddev->uuid, 16);
6037
NeilBrownf6705572006-03-27 01:18:11 -08006038 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006039 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006040 mddev->new_layout = mddev->layout;
6041 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006042 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006043
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 return 0;
6045}
6046
NeilBrownfd01b882011-10-11 16:47:53 +11006047void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006048{
Dan Williamsb522adc2009-03-31 15:00:31 +11006049 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6050
6051 if (mddev->external_size)
6052 return;
6053
Dan Williams1f403622009-03-31 14:59:03 +11006054 mddev->array_sectors = array_sectors;
6055}
6056EXPORT_SYMBOL(md_set_array_sectors);
6057
NeilBrownfd01b882011-10-11 16:47:53 +11006058static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006059{
NeilBrown3cb03002011-10-11 16:45:26 +11006060 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006061 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006062 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006063
6064 if (mddev->pers->resize == NULL)
6065 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006066 /* The "num_sectors" is the number of sectors of each device that
6067 * is used. This can only make sense for arrays with redundancy.
6068 * linear and raid0 always use whatever space is available. We can only
6069 * consider changing this number if no resync or reconstruction is
6070 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006071 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006072 * of each device. If num_sectors is zero, we find the largest size
6073 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006074 */
6075 if (mddev->sync_thread)
6076 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006077
NeilBrowndafb20f2012-03-19 12:46:39 +11006078 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006079 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006080
Andre Nolld71f9f82008-07-11 22:02:22 +10006081 if (fit && (num_sectors == 0 || num_sectors > avail))
6082 num_sectors = avail;
6083 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006084 return -ENOSPC;
6085 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006086 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006087 if (!rv)
6088 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006089 return rv;
6090}
6091
NeilBrownfd01b882011-10-11 16:47:53 +11006092static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006093{
6094 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006095 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006096 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006097 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006098 return -EINVAL;
6099 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006100 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006101 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006102 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006103 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006104
6105 rdev_for_each(rdev, mddev) {
6106 if (mddev->raid_disks < raid_disks &&
6107 rdev->data_offset < rdev->new_data_offset)
6108 return -EINVAL;
6109 if (mddev->raid_disks > raid_disks &&
6110 rdev->data_offset > rdev->new_data_offset)
6111 return -EINVAL;
6112 }
6113
NeilBrown63c70c42006-03-27 01:18:13 -08006114 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006115 if (mddev->delta_disks < 0)
6116 mddev->reshape_backwards = 1;
6117 else if (mddev->delta_disks > 0)
6118 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006119
6120 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006121 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006122 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006123 mddev->reshape_backwards = 0;
6124 }
NeilBrownda943b992006-01-06 00:20:54 -08006125 return rv;
6126}
6127
6128
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129/*
6130 * update_array_info is used to change the configuration of an
6131 * on-line array.
6132 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6133 * fields in the info are checked against the array.
6134 * Any differences that cannot be handled will cause an error.
6135 * Normally, only one change can be managed at a time.
6136 */
NeilBrownfd01b882011-10-11 16:47:53 +11006137static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138{
6139 int rv = 0;
6140 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006141 int state = 0;
6142
6143 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006144 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006145 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
6147 if (mddev->major_version != info->major_version ||
6148 mddev->minor_version != info->minor_version ||
6149/* mddev->patch_version != info->patch_version || */
6150 mddev->ctime != info->ctime ||
6151 mddev->level != info->level ||
6152/* mddev->layout != info->layout || */
6153 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006154 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006155 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6156 ((state^info->state) & 0xfffffe00)
6157 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 return -EINVAL;
6159 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006160 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6161 cnt++;
6162 if (mddev->raid_disks != info->raid_disks)
6163 cnt++;
6164 if (mddev->layout != info->layout)
6165 cnt++;
6166 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6167 cnt++;
6168 if (cnt == 0)
6169 return 0;
6170 if (cnt > 1)
6171 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 if (mddev->layout != info->layout) {
6174 /* Change layout
6175 * we don't need to do anything at the md level, the
6176 * personality will take care of it all.
6177 */
NeilBrown50ac1682009-06-18 08:47:55 +10006178 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006180 else {
6181 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006182 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006183 if (rv)
6184 mddev->new_layout = mddev->layout;
6185 return rv;
6186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006188 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006189 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
NeilBrownda943b992006-01-06 00:20:54 -08006191 if (mddev->raid_disks != info->raid_disks)
6192 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
NeilBrown36fa3062005-09-09 16:23:45 -07006194 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6195 if (mddev->pers->quiesce == NULL)
6196 return -EINVAL;
6197 if (mddev->recovery || mddev->sync_thread)
6198 return -EBUSY;
6199 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6200 /* add the bitmap */
6201 if (mddev->bitmap)
6202 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006203 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006204 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006205 mddev->bitmap_info.offset =
6206 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006207 mddev->bitmap_info.space =
6208 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006209 mddev->pers->quiesce(mddev, 1);
6210 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006211 if (!rv)
6212 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006213 if (rv)
6214 bitmap_destroy(mddev);
6215 mddev->pers->quiesce(mddev, 0);
6216 } else {
6217 /* remove the bitmap */
6218 if (!mddev->bitmap)
6219 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006220 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006221 return -EINVAL;
6222 mddev->pers->quiesce(mddev, 1);
6223 bitmap_destroy(mddev);
6224 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006225 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006226 }
6227 }
NeilBrown850b2b42006-10-03 01:15:46 -07006228 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return rv;
6230}
6231
NeilBrownfd01b882011-10-11 16:47:53 +11006232static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233{
NeilBrown3cb03002011-10-11 16:45:26 +11006234 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
6236 if (mddev->pers == NULL)
6237 return -ENODEV;
6238
6239 rdev = find_rdev(mddev, dev);
6240 if (!rdev)
6241 return -ENODEV;
6242
6243 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006244 if (!test_bit(Faulty, &rdev->flags))
6245 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return 0;
6247}
6248
Andre Noll2f9618c2008-04-25 18:57:58 +02006249/*
6250 * We have a problem here : there is no easy way to give a CHS
6251 * virtual geometry. We currently pretend that we have a 2 heads
6252 * 4 sectors (with a BIG number of cylinders...). This drives
6253 * dosfs just mad... ;-)
6254 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006255static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6256{
NeilBrownfd01b882011-10-11 16:47:53 +11006257 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006258
6259 geo->heads = 2;
6260 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006261 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006262 return 0;
6263}
6264
Al Viroa39907f2008-03-02 10:31:15 -05006265static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 unsigned int cmd, unsigned long arg)
6267{
6268 int err = 0;
6269 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006270 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006271 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
NeilBrown506c9e42011-12-23 10:17:26 +11006273 switch (cmd) {
6274 case RAID_VERSION:
6275 case GET_ARRAY_INFO:
6276 case GET_DISK_INFO:
6277 break;
6278 default:
6279 if (!capable(CAP_SYS_ADMIN))
6280 return -EACCES;
6281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 /*
6284 * Commands dealing with the RAID driver but not any
6285 * particular array:
6286 */
6287 switch (cmd)
6288 {
6289 case RAID_VERSION:
6290 err = get_version(argp);
6291 goto done;
6292
6293 case PRINT_RAID_DEBUG:
6294 err = 0;
6295 md_print_devices();
6296 goto done;
6297
6298#ifndef MODULE
6299 case RAID_AUTORUN:
6300 err = 0;
6301 autostart_arrays(arg);
6302 goto done;
6303#endif
6304 default:;
6305 }
6306
6307 /*
6308 * Commands creating/starting a new array:
6309 */
6310
Al Viroa39907f2008-03-02 10:31:15 -05006311 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 if (!mddev) {
6314 BUG();
6315 goto abort;
6316 }
6317
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 err = mddev_lock(mddev);
6319 if (err) {
6320 printk(KERN_INFO
6321 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6322 err, cmd);
6323 goto abort;
6324 }
6325
6326 switch (cmd)
6327 {
6328 case SET_ARRAY_INFO:
6329 {
6330 mdu_array_info_t info;
6331 if (!arg)
6332 memset(&info, 0, sizeof(info));
6333 else if (copy_from_user(&info, argp, sizeof(info))) {
6334 err = -EFAULT;
6335 goto abort_unlock;
6336 }
6337 if (mddev->pers) {
6338 err = update_array_info(mddev, &info);
6339 if (err) {
6340 printk(KERN_WARNING "md: couldn't update"
6341 " array info. %d\n", err);
6342 goto abort_unlock;
6343 }
6344 goto done_unlock;
6345 }
6346 if (!list_empty(&mddev->disks)) {
6347 printk(KERN_WARNING
6348 "md: array %s already has disks!\n",
6349 mdname(mddev));
6350 err = -EBUSY;
6351 goto abort_unlock;
6352 }
6353 if (mddev->raid_disks) {
6354 printk(KERN_WARNING
6355 "md: array %s already initialised!\n",
6356 mdname(mddev));
6357 err = -EBUSY;
6358 goto abort_unlock;
6359 }
6360 err = set_array_info(mddev, &info);
6361 if (err) {
6362 printk(KERN_WARNING "md: couldn't set"
6363 " array info. %d\n", err);
6364 goto abort_unlock;
6365 }
6366 }
6367 goto done_unlock;
6368
6369 default:;
6370 }
6371
6372 /*
6373 * Commands querying/configuring an existing array:
6374 */
NeilBrown32a76272005-06-21 17:17:14 -07006375 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006376 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006377 if ((!mddev->raid_disks && !mddev->external)
6378 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6379 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6380 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 err = -ENODEV;
6382 goto abort_unlock;
6383 }
6384
6385 /*
6386 * Commands even a read-only array can execute:
6387 */
6388 switch (cmd)
6389 {
6390 case GET_ARRAY_INFO:
6391 err = get_array_info(mddev, argp);
6392 goto done_unlock;
6393
NeilBrown32a76272005-06-21 17:17:14 -07006394 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006395 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006396 goto done_unlock;
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 case GET_DISK_INFO:
6399 err = get_disk_info(mddev, argp);
6400 goto done_unlock;
6401
6402 case RESTART_ARRAY_RW:
6403 err = restart_array(mddev);
6404 goto done_unlock;
6405
6406 case STOP_ARRAY:
NeilBrowna05b7ea2012-07-19 15:59:18 +10006407 err = do_md_stop(mddev, 0, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 goto done_unlock;
6409
6410 case STOP_ARRAY_RO:
NeilBrowna05b7ea2012-07-19 15:59:18 +10006411 err = md_set_readonly(mddev, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 goto done_unlock;
6413
Dan Williamse2218352010-05-12 08:25:37 +10006414 case BLKROSET:
6415 if (get_user(ro, (int __user *)(arg))) {
6416 err = -EFAULT;
6417 goto done_unlock;
6418 }
6419 err = -EINVAL;
6420
6421 /* if the bdev is going readonly the value of mddev->ro
6422 * does not matter, no writes are coming
6423 */
6424 if (ro)
6425 goto done_unlock;
6426
6427 /* are we are already prepared for writes? */
6428 if (mddev->ro != 1)
6429 goto done_unlock;
6430
6431 /* transitioning to readauto need only happen for
6432 * arrays that call md_write_start
6433 */
6434 if (mddev->pers) {
6435 err = restart_array(mddev);
6436 if (err == 0) {
6437 mddev->ro = 2;
6438 set_disk_ro(mddev->gendisk, 0);
6439 }
6440 }
6441 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 }
6443
6444 /*
6445 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006446 * superblock, so we do not allow them on read-only arrays.
6447 * However non-MD ioctls (e.g. get-size) will still come through
6448 * here and hit the 'default' below, so only disallow
6449 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006451 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006452 if (mddev->ro == 2) {
6453 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006454 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6456 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006457 } else {
6458 err = -EROFS;
6459 goto abort_unlock;
6460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 }
6462
6463 switch (cmd)
6464 {
6465 case ADD_NEW_DISK:
6466 {
6467 mdu_disk_info_t info;
6468 if (copy_from_user(&info, argp, sizeof(info)))
6469 err = -EFAULT;
6470 else
6471 err = add_new_disk(mddev, &info);
6472 goto done_unlock;
6473 }
6474
6475 case HOT_REMOVE_DISK:
6476 err = hot_remove_disk(mddev, new_decode_dev(arg));
6477 goto done_unlock;
6478
6479 case HOT_ADD_DISK:
6480 err = hot_add_disk(mddev, new_decode_dev(arg));
6481 goto done_unlock;
6482
6483 case SET_DISK_FAULTY:
6484 err = set_disk_faulty(mddev, new_decode_dev(arg));
6485 goto done_unlock;
6486
6487 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006488 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 goto done_unlock;
6490
NeilBrown32a76272005-06-21 17:17:14 -07006491 case SET_BITMAP_FILE:
6492 err = set_bitmap_file(mddev, (int)arg);
6493 goto done_unlock;
6494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 err = -EINVAL;
6497 goto abort_unlock;
6498 }
6499
6500done_unlock:
6501abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006502 if (mddev->hold_active == UNTIL_IOCTL &&
6503 err != -EINVAL)
6504 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 mddev_unlock(mddev);
6506
6507 return err;
6508done:
6509 if (err)
6510 MD_BUG();
6511abort:
6512 return err;
6513}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006514#ifdef CONFIG_COMPAT
6515static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6516 unsigned int cmd, unsigned long arg)
6517{
6518 switch (cmd) {
6519 case HOT_REMOVE_DISK:
6520 case HOT_ADD_DISK:
6521 case SET_DISK_FAULTY:
6522 case SET_BITMAP_FILE:
6523 /* These take in integer arg, do not convert */
6524 break;
6525 default:
6526 arg = (unsigned long)compat_ptr(arg);
6527 break;
6528 }
6529
6530 return md_ioctl(bdev, mode, cmd, arg);
6531}
6532#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533
Al Viroa39907f2008-03-02 10:31:15 -05006534static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535{
6536 /*
6537 * Succeed if we can lock the mddev, which confirms that
6538 * it isn't being stopped right now.
6539 */
NeilBrownfd01b882011-10-11 16:47:53 +11006540 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 int err;
6542
Yuanhan Liu0c098222012-05-22 13:55:32 +10006543 if (!mddev)
6544 return -ENODEV;
6545
NeilBrownd3374822009-01-09 08:31:10 +11006546 if (mddev->gendisk != bdev->bd_disk) {
6547 /* we are racing with mddev_put which is discarding this
6548 * bd_disk.
6549 */
6550 mddev_put(mddev);
6551 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006552 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006553 /* Then retry the open from the top */
6554 return -ERESTARTSYS;
6555 }
6556 BUG_ON(mddev != bdev->bd_disk->private_data);
6557
NeilBrownc8c00a62009-08-10 12:50:52 +10006558 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 goto out;
6560
6561 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006562 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006563 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006565 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 out:
6567 return err;
6568}
6569
Al Viroa39907f2008-03-02 10:31:15 -05006570static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571{
NeilBrownfd01b882011-10-11 16:47:53 +11006572 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006574 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006575 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 mddev_put(mddev);
6577
6578 return 0;
6579}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006580
6581static int md_media_changed(struct gendisk *disk)
6582{
NeilBrownfd01b882011-10-11 16:47:53 +11006583 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006584
6585 return mddev->changed;
6586}
6587
6588static int md_revalidate(struct gendisk *disk)
6589{
NeilBrownfd01b882011-10-11 16:47:53 +11006590 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006591
6592 mddev->changed = 0;
6593 return 0;
6594}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006595static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596{
6597 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006598 .open = md_open,
6599 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006600 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006601#ifdef CONFIG_COMPAT
6602 .compat_ioctl = md_compat_ioctl,
6603#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006604 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006605 .media_changed = md_media_changed,
6606 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607};
6608
Adrian Bunk75c96f82005-05-05 16:16:09 -07006609static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610{
NeilBrown2b8bf342011-10-11 16:48:23 +11006611 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 /*
6614 * md_thread is a 'system-thread', it's priority should be very
6615 * high. We avoid resource deadlocks individually in each
6616 * raid personality. (RAID5 does preallocation) We also use RR and
6617 * the very same RT priority as kswapd, thus we will never get
6618 * into a priority inversion deadlock.
6619 *
6620 * we definitely have to have equal or higher priority than
6621 * bdflush, otherwise bdflush will deadlock if there are too
6622 * many dirty RAID5 blocks.
6623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
NeilBrown6985c432005-10-19 21:23:47 -07006625 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006626 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
NeilBrown93588e22005-11-15 00:09:12 -08006628 /* We need to wait INTERRUPTIBLE so that
6629 * we don't add to the load-average.
6630 * That means we need to be sure no signals are
6631 * pending
6632 */
6633 if (signal_pending(current))
6634 flush_signals(current);
6635
6636 wait_event_interruptible_timeout
6637 (thread->wqueue,
6638 test_bit(THREAD_WAKEUP, &thread->flags)
6639 || kthread_should_stop(),
6640 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
NeilBrown6c987912011-01-14 09:13:53 +11006642 clear_bit(THREAD_WAKEUP, &thread->flags);
6643 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006644 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006646
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 return 0;
6648}
6649
NeilBrown2b8bf342011-10-11 16:48:23 +11006650void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651{
6652 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006653 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 set_bit(THREAD_WAKEUP, &thread->flags);
6655 wake_up(&thread->wqueue);
6656 }
6657}
6658
NeilBrown2b8bf342011-10-11 16:48:23 +11006659struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 const char *name)
6661{
NeilBrown2b8bf342011-10-11 16:48:23 +11006662 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
NeilBrown2b8bf342011-10-11 16:48:23 +11006664 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 if (!thread)
6666 return NULL;
6667
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 init_waitqueue_head(&thread->wqueue);
6669
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 thread->run = run;
6671 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006672 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006673 thread->tsk = kthread_run(md_thread, thread,
6674 "%s_%s",
6675 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006676 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006677 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 kfree(thread);
6679 return NULL;
6680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 return thread;
6682}
6683
NeilBrown2b8bf342011-10-11 16:48:23 +11006684void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685{
NeilBrown2b8bf342011-10-11 16:48:23 +11006686 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006687 if (!thread)
6688 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006689 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006690 /* Locking ensures that mddev_unlock does not wake_up a
6691 * non-existent thread
6692 */
6693 spin_lock(&pers_lock);
6694 *threadp = NULL;
6695 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006696
6697 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 kfree(thread);
6699}
6700
NeilBrownfd01b882011-10-11 16:47:53 +11006701void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702{
6703 if (!mddev) {
6704 MD_BUG();
6705 return;
6706 }
6707
NeilBrownb2d444d2005-11-08 21:39:31 -08006708 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006710
NeilBrownde393cd2011-07-28 11:31:48 +10006711 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 return;
6713 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006714 if (mddev->degraded)
6715 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006716 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6718 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6719 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006720 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006721 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006722 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723}
6724
6725/* seq_file implementation /proc/mdstat */
6726
6727static void status_unused(struct seq_file *seq)
6728{
6729 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006730 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
6732 seq_printf(seq, "unused devices: ");
6733
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006734 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 char b[BDEVNAME_SIZE];
6736 i++;
6737 seq_printf(seq, "%s ",
6738 bdevname(rdev->bdev,b));
6739 }
6740 if (!i)
6741 seq_printf(seq, "<none>");
6742
6743 seq_printf(seq, "\n");
6744}
6745
6746
NeilBrownfd01b882011-10-11 16:47:53 +11006747static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
NeilBrowndd71cf62009-05-07 12:49:35 +10006749 sector_t max_sectors, resync, res;
6750 unsigned long dt, db;
6751 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006752 int scale;
6753 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
NeilBrowndd71cf62009-05-07 12:49:35 +10006755 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
NeilBrownc804cde2012-05-21 09:28:33 +10006757 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6758 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006759 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006761 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
6763 /*
6764 * Should not happen.
6765 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006766 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 MD_BUG();
6768 return;
6769 }
NeilBrown4588b422006-03-27 01:18:04 -08006770 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006771 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006772 * u32, as those are the requirements for sector_div.
6773 * Thus 'scale' must be at least 10
6774 */
6775 scale = 10;
6776 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006777 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006778 scale++;
6779 }
6780 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006781 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006782
6783 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 {
NeilBrown4588b422006-03-27 01:18:04 -08006785 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 seq_printf(seq, "[");
6787 for (i = 0; i < x; i++)
6788 seq_printf(seq, "=");
6789 seq_printf(seq, ">");
6790 for (i = 0; i < y; i++)
6791 seq_printf(seq, ".");
6792 seq_printf(seq, "] ");
6793 }
NeilBrown4588b422006-03-27 01:18:04 -08006794 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006795 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6796 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006797 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6798 "check" :
6799 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6800 "resync" : "recovery"))),
6801 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006802 (unsigned long long) resync/2,
6803 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
6805 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 * dt: time from mark until now
6807 * db: blocks written from mark until now
6808 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006809 *
6810 * rt is a sector_t, so could be 32bit or 64bit.
6811 * So we divide before multiply in case it is 32bit and close
6812 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006813 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006814 * near the end of resync when the number of remaining sectors
6815 * is close to 'db'.
6816 * We then divide rt by 32 after multiplying by db to compensate.
6817 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 */
6819 dt = ((jiffies - mddev->resync_mark) / HZ);
6820 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006821 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6822 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
NeilBrowndd71cf62009-05-07 12:49:35 +10006824 rt = max_sectors - resync; /* number of remaining sectors */
6825 sector_div(rt, db/32+1);
6826 rt *= dt;
6827 rt >>= 5;
6828
6829 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6830 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
NeilBrownff4e8d92006-07-10 04:44:16 -07006832 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833}
6834
6835static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6836{
6837 struct list_head *tmp;
6838 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006839 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
6841 if (l >= 0x10000)
6842 return NULL;
6843 if (!l--)
6844 /* header */
6845 return (void*)1;
6846
6847 spin_lock(&all_mddevs_lock);
6848 list_for_each(tmp,&all_mddevs)
6849 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006850 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 mddev_get(mddev);
6852 spin_unlock(&all_mddevs_lock);
6853 return mddev;
6854 }
6855 spin_unlock(&all_mddevs_lock);
6856 if (!l--)
6857 return (void*)2;/* tail */
6858 return NULL;
6859}
6860
6861static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6862{
6863 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006864 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
6866 ++*pos;
6867 if (v == (void*)2)
6868 return NULL;
6869
6870 spin_lock(&all_mddevs_lock);
6871 if (v == (void*)1)
6872 tmp = all_mddevs.next;
6873 else
6874 tmp = mddev->all_mddevs.next;
6875 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006876 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 else {
6878 next_mddev = (void*)2;
6879 *pos = 0x10000;
6880 }
6881 spin_unlock(&all_mddevs_lock);
6882
6883 if (v != (void*)1)
6884 mddev_put(mddev);
6885 return next_mddev;
6886
6887}
6888
6889static void md_seq_stop(struct seq_file *seq, void *v)
6890{
NeilBrownfd01b882011-10-11 16:47:53 +11006891 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
6893 if (mddev && v != (void*)1 && v != (void*)2)
6894 mddev_put(mddev);
6895}
6896
6897static int md_seq_show(struct seq_file *seq, void *v)
6898{
NeilBrownfd01b882011-10-11 16:47:53 +11006899 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006900 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006901 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006904 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 seq_printf(seq, "Personalities : ");
6906 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006907 list_for_each_entry(pers, &pers_list, list)
6908 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
6910 spin_unlock(&pers_lock);
6911 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006912 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 return 0;
6914 }
6915 if (v == (void*)2) {
6916 status_unused(seq);
6917 return 0;
6918 }
6919
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006920 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6924 seq_printf(seq, "%s : %sactive", mdname(mddev),
6925 mddev->pers ? "" : "in");
6926 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006927 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006929 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006930 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 seq_printf(seq, " %s", mddev->pers->name);
6932 }
6933
Andre Nolldd8ac332009-03-31 14:33:13 +11006934 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006935 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 char b[BDEVNAME_SIZE];
6937 seq_printf(seq, " %s[%d]",
6938 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006939 if (test_bit(WriteMostly, &rdev->flags))
6940 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006941 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 seq_printf(seq, "(F)");
6943 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006944 }
6945 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006946 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006947 if (test_bit(Replacement, &rdev->flags))
6948 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006949 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 }
6951
6952 if (!list_empty(&mddev->disks)) {
6953 if (mddev->pers)
6954 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006955 (unsigned long long)
6956 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 else
6958 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006959 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006961 if (mddev->persistent) {
6962 if (mddev->major_version != 0 ||
6963 mddev->minor_version != 90) {
6964 seq_printf(seq," super %d.%d",
6965 mddev->major_version,
6966 mddev->minor_version);
6967 }
NeilBrowne6910632008-02-06 01:39:51 -08006968 } else if (mddev->external)
6969 seq_printf(seq, " super external:%s",
6970 mddev->metadata_type);
6971 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006972 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
6974 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006975 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006977 if (mddev->pers->sync_request) {
6978 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006979 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006980 seq_printf(seq, "\n ");
6981 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6982 seq_printf(seq, "\tresync=DELAYED\n ");
6983 else if (mddev->recovery_cp < MaxSector)
6984 seq_printf(seq, "\tresync=PENDING\n ");
6985 }
NeilBrown32a76272005-06-21 17:17:14 -07006986 } else
6987 seq_printf(seq, "\n ");
6988
NeilBrown57148962012-03-19 12:46:40 +11006989 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
6991 seq_printf(seq, "\n");
6992 }
6993 mddev_unlock(mddev);
6994
6995 return 0;
6996}
6997
Jan Engelhardt110518b2009-05-07 12:49:37 +10006998static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 .start = md_seq_start,
7000 .next = md_seq_next,
7001 .stop = md_seq_stop,
7002 .show = md_seq_show,
7003};
7004
7005static int md_seq_open(struct inode *inode, struct file *file)
7006{
Kay Sieversf1514632011-07-12 20:48:39 +02007007 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 int error;
7009
7010 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007011 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007012 return error;
7013
7014 seq = file->private_data;
7015 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 return error;
7017}
7018
NeilBrownd7603b72006-01-06 00:20:30 -08007019static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7020{
Kay Sieversf1514632011-07-12 20:48:39 +02007021 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007022 int mask;
7023
7024 poll_wait(filp, &md_event_waiters, wait);
7025
7026 /* always allow read */
7027 mask = POLLIN | POLLRDNORM;
7028
Kay Sieversf1514632011-07-12 20:48:39 +02007029 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007030 mask |= POLLERR | POLLPRI;
7031 return mask;
7032}
7033
Arjan van de Venfa027c22007-02-12 00:55:33 -08007034static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007035 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 .open = md_seq_open,
7037 .read = seq_read,
7038 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007039 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007040 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041};
7042
NeilBrown84fc4b52011-10-11 16:49:58 +11007043int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007046 list_add_tail(&p->list, &pers_list);
7047 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 spin_unlock(&pers_lock);
7049 return 0;
7050}
7051
NeilBrown84fc4b52011-10-11 16:49:58 +11007052int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
NeilBrown2604b702006-01-06 00:20:36 -08007054 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007056 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 spin_unlock(&pers_lock);
7058 return 0;
7059}
7060
NeilBrownfd01b882011-10-11 16:47:53 +11007061static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062{
NeilBrown3cb03002011-10-11 16:45:26 +11007063 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007065 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
7067 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007068 rcu_read_lock();
7069 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007071 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7072 (int)part_stat_read(&disk->part0, sectors[1]) -
7073 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007074 /* sync IO will cause sync_io to increase before the disk_stats
7075 * as sync_io is counted when a request starts, and
7076 * disk_stats is counted when it completes.
7077 * So resync activity will cause curr_events to be smaller than
7078 * when there was no such activity.
7079 * non-sync IO will cause disk_stat to increase without
7080 * increasing sync_io so curr_events will (eventually)
7081 * be larger than it was before. Once it becomes
7082 * substantially larger, the test below will cause
7083 * the array to appear non-idle, and resync will slow
7084 * down.
7085 * If there is a lot of outstanding resync activity when
7086 * we set last_event to curr_events, then all that activity
7087 * completing might cause the array to appear non-idle
7088 * and resync will be slowed down even though there might
7089 * not have been non-resync activity. This will only
7090 * happen once though. 'last_events' will soon reflect
7091 * the state where there is little or no outstanding
7092 * resync requests, and further resync activity will
7093 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007094 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007096 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 rdev->last_events = curr_events;
7098 idle = 0;
7099 }
7100 }
NeilBrown4b809912008-07-21 17:05:25 +10007101 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 return idle;
7103}
7104
NeilBrownfd01b882011-10-11 16:47:53 +11007105void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106{
7107 /* another "blocks" (512byte) blocks have been synced */
7108 atomic_sub(blocks, &mddev->recovery_active);
7109 wake_up(&mddev->recovery_wait);
7110 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007111 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 md_wakeup_thread(mddev->thread);
7113 // stop recovery, signal do_sync ....
7114 }
7115}
7116
7117
NeilBrown06d91a52005-06-21 17:17:12 -07007118/* md_write_start(mddev, bi)
7119 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007120 * in superblock) before writing, schedule a superblock update
7121 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007122 */
NeilBrownfd01b882011-10-11 16:47:53 +11007123void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124{
Neil Brown0fd62b82008-06-28 08:31:36 +10007125 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007126 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007127 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007128
NeilBrownf91de922005-11-08 21:39:36 -08007129 BUG_ON(mddev->ro == 1);
7130 if (mddev->ro == 2) {
7131 /* need to switch to read/write */
7132 mddev->ro = 0;
7133 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7134 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007135 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007136 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007137 }
NeilBrown06d91a52005-06-21 17:17:12 -07007138 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007139 if (mddev->safemode == 1)
7140 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007141 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007142 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007143 if (mddev->in_sync) {
7144 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007145 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007146 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007147 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007148 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007149 }
NeilBrowna9701a32005-11-08 21:39:34 -08007150 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007151 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007152 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007153 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007154 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007155 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156}
7157
NeilBrownfd01b882011-10-11 16:47:53 +11007158void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159{
7160 if (atomic_dec_and_test(&mddev->writes_pending)) {
7161 if (mddev->safemode == 2)
7162 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007163 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7165 }
7166}
7167
NeilBrown2a2275d2007-01-26 00:57:11 -08007168/* md_allow_write(mddev)
7169 * Calling this ensures that the array is marked 'active' so that writes
7170 * may proceed without blocking. It is important to call this before
7171 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7172 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007173 *
7174 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7175 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007176 */
NeilBrownfd01b882011-10-11 16:47:53 +11007177int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007178{
7179 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007180 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007181 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007182 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007183 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007184 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007185
7186 spin_lock_irq(&mddev->write_lock);
7187 if (mddev->in_sync) {
7188 mddev->in_sync = 0;
7189 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007190 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007191 if (mddev->safemode_delay &&
7192 mddev->safemode == 0)
7193 mddev->safemode = 1;
7194 spin_unlock_irq(&mddev->write_lock);
7195 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007196 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007197 } else
7198 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007199
NeilBrown070dc6d2010-08-30 17:33:34 +10007200 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007201 return -EAGAIN;
7202 else
7203 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007204}
7205EXPORT_SYMBOL_GPL(md_allow_write);
7206
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207#define SYNC_MARKS 10
7208#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007209void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210{
NeilBrownfd01b882011-10-11 16:47:53 +11007211 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 unsigned int currspeed = 0,
7213 window;
NeilBrown57afd892005-06-21 17:17:13 -07007214 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 unsigned long mark[SYNC_MARKS];
7216 sector_t mark_cnt[SYNC_MARKS];
7217 int last_mark,m;
7218 struct list_head *tmp;
7219 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007220 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007221 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007222 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007223 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224
7225 /* just incase thread restarts... */
7226 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7227 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007228 if (mddev->ro) /* never try to sync a read-only array */
7229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
NeilBrown61df9d92006-10-03 01:15:57 -07007231 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7232 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7233 desc = "data-check";
7234 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7235 desc = "requested-resync";
7236 else
7237 desc = "resync";
7238 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7239 desc = "reshape";
7240 else
7241 desc = "recovery";
7242
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 /* we overload curr_resync somewhat here.
7244 * 0 == not engaged in resync at all
7245 * 2 == checking that there is no conflict with another sync
7246 * 1 == like 2, but have yielded to allow conflicting resync to
7247 * commense
7248 * other == active in resync - this many blocks
7249 *
7250 * Before starting a resync we must have set curr_resync to
7251 * 2, and then checked that every "conflicting" array has curr_resync
7252 * less than ours. When we find one that is the same or higher
7253 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7254 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7255 * This will mean we have to start checking from the beginning again.
7256 *
7257 */
7258
7259 do {
7260 mddev->curr_resync = 2;
7261
7262 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007263 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007264 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007265
7266 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007268 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 if (mddev2 == mddev)
7270 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007271 if (!mddev->parallel_resync
7272 && mddev2->curr_resync
7273 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 DEFINE_WAIT(wq);
7275 if (mddev < mddev2 && mddev->curr_resync == 2) {
7276 /* arbitrarily yield */
7277 mddev->curr_resync = 1;
7278 wake_up(&resync_wait);
7279 }
7280 if (mddev > mddev2 && mddev->curr_resync == 1)
7281 /* no need to wait here, we can wait the next
7282 * time 'round when curr_resync == 2
7283 */
7284 continue;
NeilBrown97441972008-09-19 11:49:54 +10007285 /* We need to wait 'interruptible' so as not to
7286 * contribute to the load average, and not to
7287 * be caught by 'softlockup'
7288 */
7289 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007290 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007291 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007292 printk(KERN_INFO "md: delaying %s of %s"
7293 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007295 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007297 if (signal_pending(current))
7298 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 schedule();
7300 finish_wait(&resync_wait, &wq);
7301 goto try_again;
7302 }
7303 finish_wait(&resync_wait, &wq);
7304 }
7305 }
7306 } while (mddev->curr_resync < 2);
7307
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007308 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007309 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007311 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 */
7313 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007314 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007315 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007316 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7317 j = mddev->resync_min;
7318 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007319 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007320
NeilBrownccfcc3c2006-03-27 01:18:09 -08007321 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007322 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007323 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007325 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007326 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007327 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007328 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007329 if (rdev->raid_disk >= 0 &&
7330 !test_bit(Faulty, &rdev->flags) &&
7331 !test_bit(In_sync, &rdev->flags) &&
7332 rdev->recovery_offset < j)
7333 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007334 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336
NeilBrown61df9d92006-10-03 01:15:57 -07007337 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7338 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7339 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007340 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007341 "(but not more than %d KB/sec) for %s.\n",
7342 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343
NeilBrowneea1bf32009-03-31 14:27:02 +11007344 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007345
NeilBrown57afd892005-06-21 17:17:13 -07007346 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 for (m = 0; m < SYNC_MARKS; m++) {
7348 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007349 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 }
7351 last_mark = 0;
7352 mddev->resync_mark = mark[last_mark];
7353 mddev->resync_mark_cnt = mark_cnt[last_mark];
7354
7355 /*
7356 * Tune reconstruction:
7357 */
7358 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007359 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7360 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361
7362 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 last_check = 0;
7364
7365 if (j>2) {
7366 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007367 "md: resuming %s of %s from checkpoint.\n",
7368 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 mddev->curr_resync = j;
7370 }
NeilBrown75d3da42011-01-14 09:14:34 +11007371 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
majianpeng7c2c57c2012-07-03 12:12:26 +10007373 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007375 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
NeilBrown57afd892005-06-21 17:17:13 -07007377 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007378
NeilBrown7a91ee12009-05-26 12:57:21 +10007379 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7380 ((mddev->curr_resync > mddev->curr_resync_completed &&
7381 (mddev->curr_resync - mddev->curr_resync_completed)
7382 > (max_sectors >> 4)) ||
7383 (j - mddev->curr_resync_completed)*2
7384 >= mddev->resync_max - mddev->curr_resync_completed
7385 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007386 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007387 wait_event(mddev->recovery_wait,
7388 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007389 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007390 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007391 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007392 }
NeilBrownacb180b2009-04-14 16:28:34 +10007393
NeilBrowne62e58a2009-07-01 13:15:35 +10007394 while (j >= mddev->resync_max && !kthread_should_stop()) {
7395 /* As this condition is controlled by user-space,
7396 * we can block indefinitely, so use '_interruptible'
7397 * to avoid triggering warnings.
7398 */
7399 flush_signals(current); /* just in case */
7400 wait_event_interruptible(mddev->recovery_wait,
7401 mddev->resync_max > j
7402 || kthread_should_stop());
7403 }
NeilBrownacb180b2009-04-14 16:28:34 +10007404
7405 if (kthread_should_stop())
7406 goto interrupted;
7407
NeilBrown57afd892005-06-21 17:17:13 -07007408 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007409 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007410 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007411 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 goto out;
7413 }
NeilBrown57afd892005-06-21 17:17:13 -07007414
7415 if (!skipped) { /* actual IO requested */
7416 io_sectors += sectors;
7417 atomic_add(sectors, &mddev->recovery_active);
7418 }
7419
NeilBrowne875ece2011-07-28 11:39:24 +10007420 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7421 break;
7422
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 j += sectors;
7424 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007425 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007426 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007427 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007428 * visible in /proc/mdstat
7429 */
7430 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007431
7432 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 continue;
7434
NeilBrown57afd892005-06-21 17:17:13 -07007435 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 repeat:
7437 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7438 /* step marks */
7439 int next = (last_mark+1) % SYNC_MARKS;
7440
7441 mddev->resync_mark = mark[next];
7442 mddev->resync_mark_cnt = mark_cnt[next];
7443 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007444 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 last_mark = next;
7446 }
7447
7448
NeilBrownc6207272008-02-06 01:39:52 -08007449 if (kthread_should_stop())
7450 goto interrupted;
7451
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
7453 /*
7454 * this loop exits only if either when we are slower than
7455 * the 'hard' speed limit, or the system was IO-idle for
7456 * a jiffy.
7457 * the system might be non-idle CPU-wise, but we only care
7458 * about not overloading the IO subsystem. (things like an
7459 * e2fsck being done on the RAID array should execute fast)
7460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 cond_resched();
7462
NeilBrown57afd892005-06-21 17:17:13 -07007463 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7464 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
NeilBrown88202a02006-01-06 00:21:36 -08007466 if (currspeed > speed_min(mddev)) {
7467 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007468 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007469 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470 goto repeat;
7471 }
7472 }
7473 }
NeilBrown61df9d92006-10-03 01:15:57 -07007474 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 /*
7476 * this also signals 'finished resyncing' to md_stop
7477 */
7478 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007479 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7481
7482 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007483 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484
NeilBrowndfc70642008-05-23 13:04:39 -07007485 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007486 mddev->curr_resync > 2) {
7487 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7488 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7489 if (mddev->curr_resync >= mddev->recovery_cp) {
7490 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007491 "md: checkpointing %s of %s.\n",
7492 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007493 mddev->recovery_cp =
7494 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007495 }
7496 } else
7497 mddev->recovery_cp = MaxSector;
7498 } else {
7499 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7500 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007501 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007502 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007503 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007504 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007505 !test_bit(Faulty, &rdev->flags) &&
7506 !test_bit(In_sync, &rdev->flags) &&
7507 rdev->recovery_offset < mddev->curr_resync)
7508 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007509 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007512 skip:
NeilBrown17571282006-12-10 02:20:52 -08007513 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
NeilBrownc07b70a2009-12-14 12:49:48 +11007515 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7516 /* We completed so min/max setting can be forgotten if used. */
7517 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7518 mddev->resync_min = 0;
7519 mddev->resync_max = MaxSector;
7520 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7521 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 mddev->curr_resync = 0;
7523 wake_up(&resync_wait);
7524 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7525 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007526 return;
7527
7528 interrupted:
7529 /*
7530 * got a signal, exit.
7531 */
7532 printk(KERN_INFO
7533 "md: md_do_sync() got signal ... exiting\n");
7534 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7535 goto out;
7536
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537}
NeilBrown29269552006-03-27 01:18:10 -08007538EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
NeilBrownfd01b882011-10-11 16:47:53 +11007540static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007541{
NeilBrown3cb03002011-10-11 16:45:26 +11007542 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007543 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007544 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007545
NeilBrown97e4f422009-03-31 14:33:13 +11007546 mddev->curr_resync_completed = 0;
7547
NeilBrowndafb20f2012-03-19 12:46:39 +11007548 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007549 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007550 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007551 (test_bit(Faulty, &rdev->flags) ||
7552 ! test_bit(In_sync, &rdev->flags)) &&
7553 atomic_read(&rdev->nr_pending)==0) {
7554 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007555 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007556 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007557 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007558 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007559 }
7560 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007561 if (removed)
7562 sysfs_notify(&mddev->kobj, NULL,
7563 "degraded");
7564
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007565
NeilBrowndafb20f2012-03-19 12:46:39 +11007566 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007567 if (rdev->raid_disk >= 0 &&
7568 !test_bit(In_sync, &rdev->flags) &&
7569 !test_bit(Faulty, &rdev->flags))
7570 spares++;
7571 if (rdev->raid_disk < 0
7572 && !test_bit(Faulty, &rdev->flags)) {
7573 rdev->recovery_offset = 0;
7574 if (mddev->pers->
7575 hot_add_disk(mddev, rdev) == 0) {
7576 if (sysfs_link_rdev(mddev, rdev))
7577 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007578 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007579 md_new_event(mddev);
7580 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007581 }
NeilBrowndfc70642008-05-23 13:04:39 -07007582 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007583 }
7584 return spares;
7585}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007586
NeilBrownfd01b882011-10-11 16:47:53 +11007587static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007588{
NeilBrown3cb03002011-10-11 16:45:26 +11007589 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007590
7591 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007592 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007593 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7594 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7595 /* success...*/
7596 /* activate any spares */
7597 if (mddev->pers->spare_active(mddev))
7598 sysfs_notify(&mddev->kobj, NULL,
7599 "degraded");
7600 }
7601 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7602 mddev->pers->finish_reshape)
7603 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007604
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007605 /* If array is no-longer degraded, then any saved_raid_disk
7606 * information must be scrapped. Also if any device is now
7607 * In_sync we must scrape the saved_raid_disk for that device
7608 * do the superblock for an incrementally recovered device
7609 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007610 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007611 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007612 if (!mddev->degraded ||
7613 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007614 rdev->saved_raid_disk = -1;
7615
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007616 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007617 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7618 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7619 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7620 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7621 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7622 /* flag recovery needed just to double check */
7623 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7624 sysfs_notify_dirent_safe(mddev->sysfs_action);
7625 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007626 if (mddev->event_work.func)
7627 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007628}
7629
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630/*
7631 * This routine is regularly called by all per-raid-array threads to
7632 * deal with generic issues like resync and super-block update.
7633 * Raid personalities that don't have a thread (linear/raid0) do not
7634 * need this as they never do any recovery or update the superblock.
7635 *
7636 * It does not do any resync itself, but rather "forks" off other threads
7637 * to do that as needed.
7638 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7639 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007640 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 * and wakeups up this thread which will reap the thread and finish up.
7642 * This thread also removes any faulty devices (with nr_pending == 0).
7643 *
7644 * The overall approach is:
7645 * 1/ if the superblock needs updating, update it.
7646 * 2/ If a recovery thread is running, don't do anything else.
7647 * 3/ If recovery has finished, clean up, possibly marking spares active.
7648 * 4/ If there are any faulty devices, remove them.
7649 * 5/ If array is degraded, try to add spares devices
7650 * 6/ If array has spares or is not in-sync, start a resync thread.
7651 */
NeilBrownfd01b882011-10-11 16:47:53 +11007652void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007654 if (mddev->suspended)
7655 return;
7656
NeilBrown5f404022005-06-21 17:17:16 -07007657 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007658 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659
NeilBrownfca4d842005-06-21 17:17:11 -07007660 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007661 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007662 printk(KERN_INFO "md: %s in immediate safe mode\n",
7663 mdname(mddev));
7664 mddev->safemode = 2;
7665 }
7666 flush_signals(current);
7667 }
7668
NeilBrownc89a8ee2008-08-05 15:54:13 +10007669 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007672 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007674 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007675 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007676 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7677 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678 ))
7679 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007680
NeilBrowndf5b89b2006-03-27 01:18:20 -08007681 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007682 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007683
NeilBrownc89a8ee2008-08-05 15:54:13 +10007684 if (mddev->ro) {
7685 /* Only thing we do on a ro array is remove
7686 * failed devices.
7687 */
NeilBrown3cb03002011-10-11 16:45:26 +11007688 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007689 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007690 if (rdev->raid_disk >= 0 &&
7691 !test_bit(Blocked, &rdev->flags) &&
7692 test_bit(Faulty, &rdev->flags) &&
7693 atomic_read(&rdev->nr_pending)==0) {
7694 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007695 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007696 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007697 rdev->raid_disk = -1;
7698 }
7699 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007700 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7701 goto unlock;
7702 }
7703
NeilBrown31a59e32008-04-30 00:52:30 -07007704 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007705 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007706 spin_lock_irq(&mddev->write_lock);
7707 if (mddev->safemode &&
7708 !atomic_read(&mddev->writes_pending) &&
7709 !mddev->in_sync &&
7710 mddev->recovery_cp == MaxSector) {
7711 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007712 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007713 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007714 }
7715 if (mddev->safemode == 1)
7716 mddev->safemode = 0;
7717 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007718 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007719 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007720 }
NeilBrownfca4d842005-06-21 17:17:11 -07007721
NeilBrown850b2b42006-10-03 01:15:46 -07007722 if (mddev->flags)
7723 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007724
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7726 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7727 /* resync/recovery still happening */
7728 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7729 goto unlock;
7730 }
7731 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007732 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 goto unlock;
7734 }
Neil Brown72a23c22008-06-28 08:31:41 +10007735 /* Set RUNNING before clearing NEEDED to avoid
7736 * any transients in the value of "sync_action".
7737 */
7738 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007739 /* Clear some bits that don't mean anything, but
7740 * might be left set
7741 */
NeilBrown24dd4692005-11-08 21:39:26 -08007742 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7743 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744
NeilBrowned209582012-04-24 10:23:14 +10007745 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7746 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007747 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748 /* no recovery is running.
7749 * remove any failed drives, then
7750 * add spares if possible.
7751 * Spare are also removed and re-added, to allow
7752 * the personality to fail the re-add.
7753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007755 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007756 if (mddev->pers->check_reshape == NULL ||
7757 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007758 /* Cannot proceed */
7759 goto unlock;
7760 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007761 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007762 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007763 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7764 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007765 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007766 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007767 } else if (mddev->recovery_cp < MaxSector) {
7768 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007769 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007770 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7771 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007773
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007775 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007776 /* We are adding a device or devices to an array
7777 * which has the bitmap stored on all devices.
7778 * So make sure all bitmap pages get written
7779 */
7780 bitmap_write_all(mddev->bitmap);
7781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782 mddev->sync_thread = md_register_thread(md_do_sync,
7783 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007784 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 if (!mddev->sync_thread) {
7786 printk(KERN_ERR "%s: could not start resync"
7787 " thread...\n",
7788 mdname(mddev));
7789 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007790 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7791 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7792 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7793 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7794 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007795 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007797 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007798 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799 }
7800 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007801 if (!mddev->sync_thread) {
7802 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7803 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7804 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007805 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007806 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808 mddev_unlock(mddev);
7809 }
7810}
7811
NeilBrownfd01b882011-10-11 16:47:53 +11007812void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007813{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007814 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007815 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007816 !test_bit(Blocked, &rdev->flags) &&
7817 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007818 msecs_to_jiffies(5000));
7819 rdev_dec_pending(rdev, mddev);
7820}
7821EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7822
NeilBrownc6563a82012-05-21 09:27:00 +10007823void md_finish_reshape(struct mddev *mddev)
7824{
7825 /* called be personality module when reshape completes. */
7826 struct md_rdev *rdev;
7827
7828 rdev_for_each(rdev, mddev) {
7829 if (rdev->data_offset > rdev->new_data_offset)
7830 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7831 else
7832 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7833 rdev->data_offset = rdev->new_data_offset;
7834 }
7835}
7836EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007837
7838/* Bad block management.
7839 * We can record which blocks on each device are 'bad' and so just
7840 * fail those blocks, or that stripe, rather than the whole device.
7841 * Entries in the bad-block table are 64bits wide. This comprises:
7842 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7843 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7844 * A 'shift' can be set so that larger blocks are tracked and
7845 * consequently larger devices can be covered.
7846 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7847 *
7848 * Locking of the bad-block table uses a seqlock so md_is_badblock
7849 * might need to retry if it is very unlucky.
7850 * We will sometimes want to check for bad blocks in a bi_end_io function,
7851 * so we use the write_seqlock_irq variant.
7852 *
7853 * When looking for a bad block we specify a range and want to
7854 * know if any block in the range is bad. So we binary-search
7855 * to the last range that starts at-or-before the given endpoint,
7856 * (or "before the sector after the target range")
7857 * then see if it ends after the given start.
7858 * We return
7859 * 0 if there are no known bad blocks in the range
7860 * 1 if there are known bad block which are all acknowledged
7861 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7862 * plus the start/length of the first bad section we overlap.
7863 */
7864int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7865 sector_t *first_bad, int *bad_sectors)
7866{
7867 int hi;
7868 int lo = 0;
7869 u64 *p = bb->page;
7870 int rv = 0;
7871 sector_t target = s + sectors;
7872 unsigned seq;
7873
7874 if (bb->shift > 0) {
7875 /* round the start down, and the end up */
7876 s >>= bb->shift;
7877 target += (1<<bb->shift) - 1;
7878 target >>= bb->shift;
7879 sectors = target - s;
7880 }
7881 /* 'target' is now the first block after the bad range */
7882
7883retry:
7884 seq = read_seqbegin(&bb->lock);
7885
7886 hi = bb->count;
7887
7888 /* Binary search between lo and hi for 'target'
7889 * i.e. for the last range that starts before 'target'
7890 */
7891 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7892 * are known not to be the last range before target.
7893 * VARIANT: hi-lo is the number of possible
7894 * ranges, and decreases until it reaches 1
7895 */
7896 while (hi - lo > 1) {
7897 int mid = (lo + hi) / 2;
7898 sector_t a = BB_OFFSET(p[mid]);
7899 if (a < target)
7900 /* This could still be the one, earlier ranges
7901 * could not. */
7902 lo = mid;
7903 else
7904 /* This and later ranges are definitely out. */
7905 hi = mid;
7906 }
7907 /* 'lo' might be the last that started before target, but 'hi' isn't */
7908 if (hi > lo) {
7909 /* need to check all range that end after 's' to see if
7910 * any are unacknowledged.
7911 */
7912 while (lo >= 0 &&
7913 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7914 if (BB_OFFSET(p[lo]) < target) {
7915 /* starts before the end, and finishes after
7916 * the start, so they must overlap
7917 */
7918 if (rv != -1 && BB_ACK(p[lo]))
7919 rv = 1;
7920 else
7921 rv = -1;
7922 *first_bad = BB_OFFSET(p[lo]);
7923 *bad_sectors = BB_LEN(p[lo]);
7924 }
7925 lo--;
7926 }
7927 }
7928
7929 if (read_seqretry(&bb->lock, seq))
7930 goto retry;
7931
7932 return rv;
7933}
7934EXPORT_SYMBOL_GPL(md_is_badblock);
7935
7936/*
7937 * Add a range of bad blocks to the table.
7938 * This might extend the table, or might contract it
7939 * if two adjacent ranges can be merged.
7940 * We binary-search to find the 'insertion' point, then
7941 * decide how best to handle it.
7942 */
7943static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7944 int acknowledged)
7945{
7946 u64 *p;
7947 int lo, hi;
7948 int rv = 1;
7949
7950 if (bb->shift < 0)
7951 /* badblocks are disabled */
7952 return 0;
7953
7954 if (bb->shift) {
7955 /* round the start down, and the end up */
7956 sector_t next = s + sectors;
7957 s >>= bb->shift;
7958 next += (1<<bb->shift) - 1;
7959 next >>= bb->shift;
7960 sectors = next - s;
7961 }
7962
7963 write_seqlock_irq(&bb->lock);
7964
7965 p = bb->page;
7966 lo = 0;
7967 hi = bb->count;
7968 /* Find the last range that starts at-or-before 's' */
7969 while (hi - lo > 1) {
7970 int mid = (lo + hi) / 2;
7971 sector_t a = BB_OFFSET(p[mid]);
7972 if (a <= s)
7973 lo = mid;
7974 else
7975 hi = mid;
7976 }
7977 if (hi > lo && BB_OFFSET(p[lo]) > s)
7978 hi = lo;
7979
7980 if (hi > lo) {
7981 /* we found a range that might merge with the start
7982 * of our new range
7983 */
7984 sector_t a = BB_OFFSET(p[lo]);
7985 sector_t e = a + BB_LEN(p[lo]);
7986 int ack = BB_ACK(p[lo]);
7987 if (e >= s) {
7988 /* Yes, we can merge with a previous range */
7989 if (s == a && s + sectors >= e)
7990 /* new range covers old */
7991 ack = acknowledged;
7992 else
7993 ack = ack && acknowledged;
7994
7995 if (e < s + sectors)
7996 e = s + sectors;
7997 if (e - a <= BB_MAX_LEN) {
7998 p[lo] = BB_MAKE(a, e-a, ack);
7999 s = e;
8000 } else {
8001 /* does not all fit in one range,
8002 * make p[lo] maximal
8003 */
8004 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8005 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8006 s = a + BB_MAX_LEN;
8007 }
8008 sectors = e - s;
8009 }
8010 }
8011 if (sectors && hi < bb->count) {
8012 /* 'hi' points to the first range that starts after 's'.
8013 * Maybe we can merge with the start of that range */
8014 sector_t a = BB_OFFSET(p[hi]);
8015 sector_t e = a + BB_LEN(p[hi]);
8016 int ack = BB_ACK(p[hi]);
8017 if (a <= s + sectors) {
8018 /* merging is possible */
8019 if (e <= s + sectors) {
8020 /* full overlap */
8021 e = s + sectors;
8022 ack = acknowledged;
8023 } else
8024 ack = ack && acknowledged;
8025
8026 a = s;
8027 if (e - a <= BB_MAX_LEN) {
8028 p[hi] = BB_MAKE(a, e-a, ack);
8029 s = e;
8030 } else {
8031 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8032 s = a + BB_MAX_LEN;
8033 }
8034 sectors = e - s;
8035 lo = hi;
8036 hi++;
8037 }
8038 }
8039 if (sectors == 0 && hi < bb->count) {
8040 /* we might be able to combine lo and hi */
8041 /* Note: 's' is at the end of 'lo' */
8042 sector_t a = BB_OFFSET(p[hi]);
8043 int lolen = BB_LEN(p[lo]);
8044 int hilen = BB_LEN(p[hi]);
8045 int newlen = lolen + hilen - (s - a);
8046 if (s >= a && newlen < BB_MAX_LEN) {
8047 /* yes, we can combine them */
8048 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8049 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8050 memmove(p + hi, p + hi + 1,
8051 (bb->count - hi - 1) * 8);
8052 bb->count--;
8053 }
8054 }
8055 while (sectors) {
8056 /* didn't merge (it all).
8057 * Need to add a range just before 'hi' */
8058 if (bb->count >= MD_MAX_BADBLOCKS) {
8059 /* No room for more */
8060 rv = 0;
8061 break;
8062 } else {
8063 int this_sectors = sectors;
8064 memmove(p + hi + 1, p + hi,
8065 (bb->count - hi) * 8);
8066 bb->count++;
8067
8068 if (this_sectors > BB_MAX_LEN)
8069 this_sectors = BB_MAX_LEN;
8070 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8071 sectors -= this_sectors;
8072 s += this_sectors;
8073 }
8074 }
8075
8076 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008077 if (!acknowledged)
8078 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008079 write_sequnlock_irq(&bb->lock);
8080
8081 return rv;
8082}
8083
NeilBrown3cb03002011-10-11 16:45:26 +11008084int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008085 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008086{
NeilBrownc6563a82012-05-21 09:27:00 +10008087 int rv;
8088 if (is_new)
8089 s += rdev->new_data_offset;
8090 else
8091 s += rdev->data_offset;
8092 rv = md_set_badblocks(&rdev->badblocks,
8093 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008094 if (rv) {
8095 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008096 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008097 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8098 md_wakeup_thread(rdev->mddev->thread);
8099 }
8100 return rv;
8101}
8102EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8103
8104/*
8105 * Remove a range of bad blocks from the table.
8106 * This may involve extending the table if we spilt a region,
8107 * but it must not fail. So if the table becomes full, we just
8108 * drop the remove request.
8109 */
8110static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8111{
8112 u64 *p;
8113 int lo, hi;
8114 sector_t target = s + sectors;
8115 int rv = 0;
8116
8117 if (bb->shift > 0) {
8118 /* When clearing we round the start up and the end down.
8119 * This should not matter as the shift should align with
8120 * the block size and no rounding should ever be needed.
8121 * However it is better the think a block is bad when it
8122 * isn't than to think a block is not bad when it is.
8123 */
8124 s += (1<<bb->shift) - 1;
8125 s >>= bb->shift;
8126 target >>= bb->shift;
8127 sectors = target - s;
8128 }
8129
8130 write_seqlock_irq(&bb->lock);
8131
8132 p = bb->page;
8133 lo = 0;
8134 hi = bb->count;
8135 /* Find the last range that starts before 'target' */
8136 while (hi - lo > 1) {
8137 int mid = (lo + hi) / 2;
8138 sector_t a = BB_OFFSET(p[mid]);
8139 if (a < target)
8140 lo = mid;
8141 else
8142 hi = mid;
8143 }
8144 if (hi > lo) {
8145 /* p[lo] is the last range that could overlap the
8146 * current range. Earlier ranges could also overlap,
8147 * but only this one can overlap the end of the range.
8148 */
8149 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8150 /* Partial overlap, leave the tail of this range */
8151 int ack = BB_ACK(p[lo]);
8152 sector_t a = BB_OFFSET(p[lo]);
8153 sector_t end = a + BB_LEN(p[lo]);
8154
8155 if (a < s) {
8156 /* we need to split this range */
8157 if (bb->count >= MD_MAX_BADBLOCKS) {
8158 rv = 0;
8159 goto out;
8160 }
8161 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8162 bb->count++;
8163 p[lo] = BB_MAKE(a, s-a, ack);
8164 lo++;
8165 }
8166 p[lo] = BB_MAKE(target, end - target, ack);
8167 /* there is no longer an overlap */
8168 hi = lo;
8169 lo--;
8170 }
8171 while (lo >= 0 &&
8172 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8173 /* This range does overlap */
8174 if (BB_OFFSET(p[lo]) < s) {
8175 /* Keep the early parts of this range. */
8176 int ack = BB_ACK(p[lo]);
8177 sector_t start = BB_OFFSET(p[lo]);
8178 p[lo] = BB_MAKE(start, s - start, ack);
8179 /* now low doesn't overlap, so.. */
8180 break;
8181 }
8182 lo--;
8183 }
8184 /* 'lo' is strictly before, 'hi' is strictly after,
8185 * anything between needs to be discarded
8186 */
8187 if (hi - lo > 1) {
8188 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8189 bb->count -= (hi - lo - 1);
8190 }
8191 }
8192
8193 bb->changed = 1;
8194out:
8195 write_sequnlock_irq(&bb->lock);
8196 return rv;
8197}
8198
NeilBrownc6563a82012-05-21 09:27:00 +10008199int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8200 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008201{
NeilBrownc6563a82012-05-21 09:27:00 +10008202 if (is_new)
8203 s += rdev->new_data_offset;
8204 else
8205 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008206 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008207 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008208}
8209EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8210
8211/*
8212 * Acknowledge all bad blocks in a list.
8213 * This only succeeds if ->changed is clear. It is used by
8214 * in-kernel metadata updates
8215 */
8216void md_ack_all_badblocks(struct badblocks *bb)
8217{
8218 if (bb->page == NULL || bb->changed)
8219 /* no point even trying */
8220 return;
8221 write_seqlock_irq(&bb->lock);
8222
majianpengecb178b2012-03-19 12:46:42 +11008223 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008224 u64 *p = bb->page;
8225 int i;
8226 for (i = 0; i < bb->count ; i++) {
8227 if (!BB_ACK(p[i])) {
8228 sector_t start = BB_OFFSET(p[i]);
8229 int len = BB_LEN(p[i]);
8230 p[i] = BB_MAKE(start, len, 1);
8231 }
8232 }
NeilBrownde393cd2011-07-28 11:31:48 +10008233 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008234 }
8235 write_sequnlock_irq(&bb->lock);
8236}
8237EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8238
NeilBrown16c791a2011-07-28 11:31:47 +10008239/* sysfs access to bad-blocks list.
8240 * We present two files.
8241 * 'bad-blocks' lists sector numbers and lengths of ranges that
8242 * are recorded as bad. The list is truncated to fit within
8243 * the one-page limit of sysfs.
8244 * Writing "sector length" to this file adds an acknowledged
8245 * bad block list.
8246 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8247 * been acknowledged. Writing to this file adds bad blocks
8248 * without acknowledging them. This is largely for testing.
8249 */
8250
8251static ssize_t
8252badblocks_show(struct badblocks *bb, char *page, int unack)
8253{
8254 size_t len;
8255 int i;
8256 u64 *p = bb->page;
8257 unsigned seq;
8258
8259 if (bb->shift < 0)
8260 return 0;
8261
8262retry:
8263 seq = read_seqbegin(&bb->lock);
8264
8265 len = 0;
8266 i = 0;
8267
8268 while (len < PAGE_SIZE && i < bb->count) {
8269 sector_t s = BB_OFFSET(p[i]);
8270 unsigned int length = BB_LEN(p[i]);
8271 int ack = BB_ACK(p[i]);
8272 i++;
8273
8274 if (unack && ack)
8275 continue;
8276
8277 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8278 (unsigned long long)s << bb->shift,
8279 length << bb->shift);
8280 }
NeilBrownde393cd2011-07-28 11:31:48 +10008281 if (unack && len == 0)
8282 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008283
8284 if (read_seqretry(&bb->lock, seq))
8285 goto retry;
8286
8287 return len;
8288}
8289
8290#define DO_DEBUG 1
8291
8292static ssize_t
8293badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8294{
8295 unsigned long long sector;
8296 int length;
8297 char newline;
8298#ifdef DO_DEBUG
8299 /* Allow clearing via sysfs *only* for testing/debugging.
8300 * Normally only a successful write may clear a badblock
8301 */
8302 int clear = 0;
8303 if (page[0] == '-') {
8304 clear = 1;
8305 page++;
8306 }
8307#endif /* DO_DEBUG */
8308
8309 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8310 case 3:
8311 if (newline != '\n')
8312 return -EINVAL;
8313 case 2:
8314 if (length <= 0)
8315 return -EINVAL;
8316 break;
8317 default:
8318 return -EINVAL;
8319 }
8320
8321#ifdef DO_DEBUG
8322 if (clear) {
8323 md_clear_badblocks(bb, sector, length);
8324 return len;
8325 }
8326#endif /* DO_DEBUG */
8327 if (md_set_badblocks(bb, sector, length, !unack))
8328 return len;
8329 else
8330 return -ENOSPC;
8331}
8332
Adrian Bunk75c96f82005-05-05 16:16:09 -07008333static int md_notify_reboot(struct notifier_block *this,
8334 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335{
8336 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008337 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008338 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
NeilBrownc744a652012-03-19 12:46:37 +11008340 for_each_mddev(mddev, tmp) {
8341 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008342 if (mddev->pers)
8343 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008344 mddev->safemode = 2;
8345 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008346 }
NeilBrownc744a652012-03-19 12:46:37 +11008347 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 }
NeilBrownc744a652012-03-19 12:46:37 +11008349 /*
8350 * certain more exotic SCSI devices are known to be
8351 * volatile wrt too early system reboots. While the
8352 * right place to handle this issue is the given
8353 * driver, we do want to have a safe RAID driver ...
8354 */
8355 if (need_delay)
8356 mdelay(1000*1);
8357
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 return NOTIFY_DONE;
8359}
8360
Adrian Bunk75c96f82005-05-05 16:16:09 -07008361static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 .notifier_call = md_notify_reboot,
8363 .next = NULL,
8364 .priority = INT_MAX, /* before any real devices */
8365};
8366
8367static void md_geninit(void)
8368{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008369 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008371 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372}
8373
Adrian Bunk75c96f82005-05-05 16:16:09 -07008374static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375{
Tejun Heoe804ac72010-10-15 15:36:08 +02008376 int ret = -ENOMEM;
8377
Tejun Heoada609e2011-01-25 14:35:54 +01008378 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008379 if (!md_wq)
8380 goto err_wq;
8381
8382 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8383 if (!md_misc_wq)
8384 goto err_misc_wq;
8385
8386 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8387 goto err_md;
8388
8389 if ((ret = register_blkdev(0, "mdp")) < 0)
8390 goto err_mdp;
8391 mdp_major = ret;
8392
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008393 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008394 md_probe, NULL, NULL);
8395 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 md_probe, NULL, NULL);
8397
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008399 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400
8401 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403
Tejun Heoe804ac72010-10-15 15:36:08 +02008404err_mdp:
8405 unregister_blkdev(MD_MAJOR, "md");
8406err_md:
8407 destroy_workqueue(md_misc_wq);
8408err_misc_wq:
8409 destroy_workqueue(md_wq);
8410err_wq:
8411 return ret;
8412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
8414#ifndef MODULE
8415
8416/*
8417 * Searches all registered partitions for autorun RAID arrays
8418 * at boot time.
8419 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008420
8421static LIST_HEAD(all_detected_devices);
8422struct detected_devices_node {
8423 struct list_head list;
8424 dev_t dev;
8425};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426
8427void md_autodetect_dev(dev_t dev)
8428{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008429 struct detected_devices_node *node_detected_dev;
8430
8431 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8432 if (node_detected_dev) {
8433 node_detected_dev->dev = dev;
8434 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8435 } else {
8436 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8437 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439}
8440
8441
8442static void autostart_arrays(int part)
8443{
NeilBrown3cb03002011-10-11 16:45:26 +11008444 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008445 struct detected_devices_node *node_detected_dev;
8446 dev_t dev;
8447 int i_scanned, i_passed;
8448
8449 i_scanned = 0;
8450 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451
8452 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8453
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008454 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8455 i_scanned++;
8456 node_detected_dev = list_entry(all_detected_devices.next,
8457 struct detected_devices_node, list);
8458 list_del(&node_detected_dev->list);
8459 dev = node_detected_dev->dev;
8460 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008461 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 if (IS_ERR(rdev))
8463 continue;
8464
NeilBrownb2d444d2005-11-08 21:39:31 -08008465 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 MD_BUG();
8467 continue;
8468 }
NeilBrownd0fae182008-03-04 14:29:31 -08008469 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008471 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008473
8474 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8475 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476
8477 autorun_devices(part);
8478}
8479
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008480#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481
8482static __exit void md_exit(void)
8483{
NeilBrownfd01b882011-10-11 16:47:53 +11008484 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008486
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008487 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008488 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008490 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 unregister_blkdev(mdp_major, "mdp");
8492 unregister_reboot_notifier(&md_notifier);
8493 unregister_sysctl_table(raid_table_header);
8494 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008495 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008497 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008499 destroy_workqueue(md_misc_wq);
8500 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501}
8502
Dan Williams685784a2007-07-09 11:56:42 -07008503subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504module_exit(md_exit)
8505
NeilBrownf91de922005-11-08 21:39:36 -08008506static int get_ro(char *buffer, struct kernel_param *kp)
8507{
8508 return sprintf(buffer, "%d", start_readonly);
8509}
8510static int set_ro(const char *val, struct kernel_param *kp)
8511{
8512 char *e;
8513 int num = simple_strtoul(val, &e, 10);
8514 if (*val && (*e == '\0' || *e == '\n')) {
8515 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008516 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008517 }
8518 return -EINVAL;
8519}
8520
NeilBrown80ca3a42006-07-10 04:44:18 -07008521module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8522module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008523
NeilBrownefeb53c2009-01-09 08:31:10 +11008524module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008525
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526EXPORT_SYMBOL(register_md_personality);
8527EXPORT_SYMBOL(unregister_md_personality);
8528EXPORT_SYMBOL(md_error);
8529EXPORT_SYMBOL(md_done_sync);
8530EXPORT_SYMBOL(md_write_start);
8531EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532EXPORT_SYMBOL(md_register_thread);
8533EXPORT_SYMBOL(md_unregister_thread);
8534EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535EXPORT_SYMBOL(md_check_recovery);
8536MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008537MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008538MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008539MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);