blob: c6e44a836a53a5f10702d5e42ad0ba1a87622831 [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 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100310 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
311 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
312 return;
313 }
NeilBrown0ca69882011-01-14 09:14:33 +1100314 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100315 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200316 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100317 DEFINE_WAIT(__wait);
318 for (;;) {
319 prepare_to_wait(&mddev->sb_wait, &__wait,
320 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200321 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100322 break;
323 rcu_read_unlock();
324 schedule();
325 rcu_read_lock();
326 }
327 finish_wait(&mddev->sb_wait, &__wait);
328 }
329 atomic_inc(&mddev->active_io);
330 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100331
Chris Masone91ece52011-02-07 19:21:48 -0500332 /*
333 * save the sectors now since our bio can
334 * go away inside make_request
335 */
336 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200337 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100338
339 cpu = part_stat_lock();
340 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500341 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100342 part_stat_unlock();
343
NeilBrown409c57f2009-03-31 14:39:39 +1100344 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
345 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
NeilBrown9e35b992010-04-06 14:23:02 +1000348/* mddev_suspend makes sure no new requests are submitted
349 * to the device, and that any requests that have been submitted
350 * are completely handled.
351 * Once ->stop is called and completes, the module will be completely
352 * unused.
353 */
NeilBrownfd01b882011-10-11 16:47:53 +1100354void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100355{
356 BUG_ON(mddev->suspended);
357 mddev->suspended = 1;
358 synchronize_rcu();
359 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
360 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500361
362 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100363}
NeilBrown390ee602010-06-01 19:37:27 +1000364EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100365
NeilBrownfd01b882011-10-11 16:47:53 +1100366void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100367{
368 mddev->suspended = 0;
369 wake_up(&mddev->sb_wait);
370 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500371
Jonathan Brassow47525e52012-05-22 13:55:29 +1000372 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500373 md_wakeup_thread(mddev->thread);
374 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100375}
NeilBrown390ee602010-06-01 19:37:27 +1000376EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100377
NeilBrownfd01b882011-10-11 16:47:53 +1100378int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000379{
380 return mddev->suspended;
381}
382EXPORT_SYMBOL(mddev_congested);
383
NeilBrowna2826aa2009-12-14 12:49:49 +1100384/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200385 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100386 */
387
Tejun Heoe9c74692010-09-03 11:56:18 +0200388static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100389{
NeilBrown3cb03002011-10-11 16:45:26 +1100390 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100391 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100392
393 rdev_dec_pending(rdev, mddev);
394
395 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200397 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100398 }
399 bio_put(bio);
400}
401
NeilBrowna7a07e62010-12-09 16:04:25 +1100402static void md_submit_flush_data(struct work_struct *ws);
403
NeilBrowna035fc32010-12-09 16:17:51 +1100404static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100405{
NeilBrownfd01b882011-10-11 16:47:53 +1100406 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100407 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100408
NeilBrowna7a07e62010-12-09 16:04:25 +1100409 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
410 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100412 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100413 if (rdev->raid_disk >= 0 &&
414 !test_bit(Faulty, &rdev->flags)) {
415 /* Take two references, one is dropped
416 * when request finishes, one after
417 * we reclaim rcu_read_lock
418 */
419 struct bio *bi;
420 atomic_inc(&rdev->nr_pending);
421 atomic_inc(&rdev->nr_pending);
422 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000423 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 bi->bi_private = rdev;
426 bi->bi_bdev = rdev->bdev;
427 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429 rcu_read_lock();
430 rdev_dec_pending(rdev, mddev);
431 }
432 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100433 if (atomic_dec_and_test(&mddev->flush_pending))
434 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435}
436
Tejun Heoe9c74692010-09-03 11:56:18 +0200437static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100438{
NeilBrownfd01b882011-10-11 16:47:53 +1100439 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200440 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100441
Tejun Heoe9c74692010-09-03 11:56:18 +0200442 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100443 /* an empty barrier - all done */
444 bio_endio(bio, 0);
445 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200446 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200447 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448 }
NeilBrown2b74e122010-12-09 15:59:01 +1100449
450 mddev->flush_bio = NULL;
451 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100452}
453
NeilBrownfd01b882011-10-11 16:47:53 +1100454void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100455{
456 spin_lock_irq(&mddev->write_lock);
457 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200458 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100459 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200460 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100461 spin_unlock_irq(&mddev->write_lock);
462
NeilBrowna035fc32010-12-09 16:17:51 +1100463 INIT_WORK(&mddev->flush_work, submit_flushes);
464 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100465}
Tejun Heoe9c74692010-09-03 11:56:18 +0200466EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100467
NeilBrown74018dc2012-07-31 09:08:15 +0200468void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000469{
NeilBrown9cbb1752012-07-31 09:08:14 +0200470 struct mddev *mddev = cb->data;
471 md_wakeup_thread(mddev->thread);
472 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000473}
NeilBrown9cbb1752012-07-31 09:08:14 +0200474EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000475
NeilBrownfd01b882011-10-11 16:47:53 +1100476static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 atomic_inc(&mddev->active);
479 return mddev;
480}
481
Dan Williams5fd3a172009-03-04 00:57:25 -0700482static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100483
NeilBrownfd01b882011-10-11 16:47:53 +1100484static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
NeilBrowna167f662010-10-26 18:31:13 +1100486 struct bio_set *bs = NULL;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
489 return;
NeilBrownd3374822009-01-09 08:31:10 +1100490 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100491 mddev->ctime == 0 && !mddev->hold_active) {
492 /* Array is not configured at all, and not held active,
493 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100494 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100495 bs = mddev->bio_set;
496 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100497 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200498 /* We did a probe so need to clean up. Call
499 * queue_work inside the spinlock so that
500 * flush_workqueue() after mddev_find will
501 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100502 */
503 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200504 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100505 } else
506 kfree(mddev);
507 }
508 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100509 if (bs)
510 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
NeilBrownfd01b882011-10-11 16:47:53 +1100513void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100514{
515 mutex_init(&mddev->open_mutex);
516 mutex_init(&mddev->reconfig_mutex);
517 mutex_init(&mddev->bitmap_info.mutex);
518 INIT_LIST_HEAD(&mddev->disks);
519 INIT_LIST_HEAD(&mddev->all_mddevs);
520 init_timer(&mddev->safemode_timer);
521 atomic_set(&mddev->active, 1);
522 atomic_set(&mddev->openers, 0);
523 atomic_set(&mddev->active_io, 0);
524 spin_lock_init(&mddev->write_lock);
525 atomic_set(&mddev->flush_pending, 0);
526 init_waitqueue_head(&mddev->sb_wait);
527 init_waitqueue_head(&mddev->recovery_wait);
528 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000529 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100530 mddev->resync_min = 0;
531 mddev->resync_max = MaxSector;
532 mddev->level = LEVEL_NONE;
533}
NeilBrown390ee602010-06-01 19:37:27 +1000534EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100535
NeilBrownfd01b882011-10-11 16:47:53 +1100536static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
NeilBrownfd01b882011-10-11 16:47:53 +1100538 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
NeilBrown8f5f02c2011-02-16 13:58:51 +1100540 if (unit && MAJOR(unit) != MD_MAJOR)
541 unit &= ~((1<<MdpMinorShift)-1);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 retry:
544 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
NeilBrownefeb53c2009-01-09 08:31:10 +1100546 if (unit) {
547 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
548 if (mddev->unit == unit) {
549 mddev_get(mddev);
550 spin_unlock(&all_mddevs_lock);
551 kfree(new);
552 return mddev;
553 }
554
555 if (new) {
556 list_add(&new->all_mddevs, &all_mddevs);
557 spin_unlock(&all_mddevs_lock);
558 new->hold_active = UNTIL_IOCTL;
559 return new;
560 }
561 } else if (new) {
562 /* find an unused unit number */
563 static int next_minor = 512;
564 int start = next_minor;
565 int is_free = 0;
566 int dev = 0;
567 while (!is_free) {
568 dev = MKDEV(MD_MAJOR, next_minor);
569 next_minor++;
570 if (next_minor > MINORMASK)
571 next_minor = 0;
572 if (next_minor == start) {
573 /* Oh dear, all in use. */
574 spin_unlock(&all_mddevs_lock);
575 kfree(new);
576 return NULL;
577 }
578
579 is_free = 1;
580 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
581 if (mddev->unit == dev) {
582 is_free = 0;
583 break;
584 }
585 }
586 new->unit = dev;
587 new->md_minor = MINOR(dev);
588 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 list_add(&new->all_mddevs, &all_mddevs);
590 spin_unlock(&all_mddevs_lock);
591 return new;
592 }
593 spin_unlock(&all_mddevs_lock);
594
NeilBrown9ffae0c2006-01-06 00:20:32 -0800595 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!new)
597 return NULL;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 new->unit = unit;
600 if (MAJOR(unit) == MD_MAJOR)
601 new->md_minor = MINOR(unit);
602 else
603 new->md_minor = MINOR(unit) >> MdpMinorShift;
604
NeilBrownfafd7fb2010-04-01 15:55:30 +1100605 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto retry;
608}
609
NeilBrownfd01b882011-10-11 16:47:53 +1100610static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800612 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
NeilBrownfd01b882011-10-11 16:47:53 +1100615static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100616{
617 return mutex_is_locked(&mddev->reconfig_mutex);
618}
619
NeilBrownfd01b882011-10-11 16:47:53 +1100620static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800622 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
NeilBrownb6eb1272010-04-15 10:13:47 +1000625static struct attribute_group md_redundancy_group;
626
NeilBrownfd01b882011-10-11 16:47:53 +1100627static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
NeilBrowna64c8762010-04-14 17:15:37 +1000629 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000630 /* These cannot be removed under reconfig_mutex as
631 * an access to the files will try to take reconfig_mutex
632 * while holding the file unremovable, which leads to
633 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000634 * So hold set sysfs_active while the remove in happeing,
635 * and anything else which might set ->to_remove or my
636 * otherwise change the sysfs namespace will fail with
637 * -EBUSY if sysfs_active is still set.
638 * We set sysfs_active under reconfig_mutex and elsewhere
639 * test it under the same mutex to ensure its correct value
640 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000641 */
NeilBrowna64c8762010-04-14 17:15:37 +1000642 struct attribute_group *to_remove = mddev->to_remove;
643 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000644 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000645 mutex_unlock(&mddev->reconfig_mutex);
646
NeilBrown00bcb4a2010-06-01 19:37:23 +1000647 if (mddev->kobj.sd) {
648 if (to_remove != &md_redundancy_group)
649 sysfs_remove_group(&mddev->kobj, to_remove);
650 if (mddev->pers == NULL ||
651 mddev->pers->sync_request == NULL) {
652 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
653 if (mddev->sysfs_action)
654 sysfs_put(mddev->sysfs_action);
655 mddev->sysfs_action = NULL;
656 }
NeilBrowna64c8762010-04-14 17:15:37 +1000657 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000658 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000659 } else
660 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Chris Dunlop751e67c2011-10-19 16:48:26 +1100662 /* As we've dropped the mutex we need a spinlock to
663 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000664 */
665 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700666 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000667 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
NeilBrownfd01b882011-10-11 16:47:53 +1100670static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
NeilBrown3cb03002011-10-11 16:45:26 +1100672 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
NeilBrowndafb20f2012-03-19 12:46:39 +1100674 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (rdev->desc_nr == nr)
676 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return NULL;
679}
680
NeilBrown1ca69c42012-10-11 13:37:33 +1100681static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
682{
683 struct md_rdev *rdev;
684
685 rdev_for_each_rcu(rdev, mddev)
686 if (rdev->desc_nr == nr)
687 return rdev;
688
689 return NULL;
690}
691
692static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
NeilBrown3cb03002011-10-11 16:45:26 +1100694 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
NeilBrowndafb20f2012-03-19 12:46:39 +1100696 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (rdev->bdev->bd_dev == dev)
698 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return NULL;
701}
702
NeilBrown1ca69c42012-10-11 13:37:33 +1100703static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
704{
705 struct md_rdev *rdev;
706
707 rdev_for_each_rcu(rdev, mddev)
708 if (rdev->bdev->bd_dev == dev)
709 return rdev;
710
711 return NULL;
712}
713
NeilBrown84fc4b52011-10-11 16:49:58 +1100714static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800715{
NeilBrown84fc4b52011-10-11 16:49:58 +1100716 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800717 list_for_each_entry(pers, &pers_list, list) {
718 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800719 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800720 if (strcmp(pers->name, clevel)==0)
721 return pers;
722 }
NeilBrown2604b702006-01-06 00:20:36 -0800723 return NULL;
724}
725
Andre Nollb73df2d2008-07-11 22:02:23 +1000726/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100727static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100729 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000730 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
NeilBrown3cb03002011-10-11 16:45:26 +1100733static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 if (rdev->sb_page)
736 MD_BUG();
737
738 rdev->sb_page = alloc_page(GFP_KERNEL);
739 if (!rdev->sb_page) {
740 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000741 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 return 0;
745}
746
NeilBrown545c8792012-05-22 13:54:30 +1000747void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800750 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rdev->sb_loaded = 0;
752 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000753 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100754 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
NeilBrown2699b672011-07-28 11:31:47 +1000756 if (rdev->bb_page) {
757 put_page(rdev->bb_page);
758 rdev->bb_page = NULL;
759 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000760 kfree(rdev->badblocks.page);
761 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
NeilBrown545c8792012-05-22 13:54:30 +1000763EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
NeilBrown6712ecf2007-09-27 12:47:43 +0200765static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700766{
NeilBrown3cb03002011-10-11 16:45:26 +1100767 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100768 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700769
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700770 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
771 printk("md: super_written gets error=%d, uptodate=%d\n",
772 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
773 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800774 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700775 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700776
NeilBrowna9701a32005-11-08 21:39:34 -0800777 if (atomic_dec_and_test(&mddev->pending_writes))
778 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700779 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700780}
781
NeilBrownfd01b882011-10-11 16:47:53 +1100782void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700783 sector_t sector, int size, struct page *page)
784{
785 /* write first size bytes of page to sector of rdev
786 * Increment mddev->pending_writes before returning
787 * and decrement it on completion, waking up sb_wait
788 * if zero is reached.
789 * If an error occurred, call md_error
790 */
NeilBrowna167f662010-10-26 18:31:13 +1100791 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700792
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100793 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794 bio->bi_sector = sector;
795 bio_add_page(bio, page, size, 0);
796 bio->bi_private = rdev;
797 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800798
NeilBrown7bfa19f2005-06-21 17:17:28 -0700799 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000800 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800801}
802
NeilBrownfd01b882011-10-11 16:47:53 +1100803void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800804{
Tejun Heoe9c74692010-09-03 11:56:18 +0200805 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800806 DEFINE_WAIT(wq);
807 for(;;) {
808 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
809 if (atomic_read(&mddev->pending_writes)==0)
810 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800811 schedule();
812 }
813 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700814}
815
NeilBrown6712ecf2007-09-27 12:47:43 +0200816static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
NeilBrown3cb03002011-10-11 16:45:26 +1100821int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100822 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
NeilBrowna167f662010-10-26 18:31:13 +1100824 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct completion event;
826 int ret;
827
Jens Axboe721a9602011-03-09 11:56:30 +0100828 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100830 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
831 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100832 if (metadata_op)
833 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000834 else if (rdev->mddev->reshape_position != MaxSector &&
835 (rdev->mddev->reshape_backwards ==
836 (sector >= rdev->mddev->reshape_position)))
837 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100838 else
839 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 bio_add_page(bio, page, size, 0);
841 init_completion(&event);
842 bio->bi_private = &event;
843 bio->bi_end_io = bi_complete;
844 submit_bio(rw, bio);
845 wait_for_completion(&event);
846
847 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
848 bio_put(bio);
849 return ret;
850}
NeilBrowna8745db2006-01-06 00:20:34 -0800851EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
NeilBrown3cb03002011-10-11 16:45:26 +1100853static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 char b[BDEVNAME_SIZE];
856 if (!rdev->sb_page) {
857 MD_BUG();
858 return -EINVAL;
859 }
860 if (rdev->sb_loaded)
861 return 0;
862
863
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100864 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto fail;
866 rdev->sb_loaded = 1;
867 return 0;
868
869fail:
870 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
871 bdevname(rdev->bdev,b));
872 return -EINVAL;
873}
874
875static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
876{
Andre Noll05710462008-07-11 22:02:20 +1000877 return sb1->set_uuid0 == sb2->set_uuid0 &&
878 sb1->set_uuid1 == sb2->set_uuid1 &&
879 sb1->set_uuid2 == sb2->set_uuid2 &&
880 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
884{
885 int ret;
886 mdp_super_t *tmp1, *tmp2;
887
888 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
889 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
890
891 if (!tmp1 || !tmp2) {
892 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100893 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto abort;
895 }
896
897 *tmp1 = *sb1;
898 *tmp2 = *sb2;
899
900 /*
901 * nr_disks is not constant
902 */
903 tmp1->nr_disks = 0;
904 tmp2->nr_disks = 0;
905
Andre Nollce0c8e02008-07-11 22:02:20 +1000906 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700908 kfree(tmp1);
909 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return ret;
911}
912
NeilBrown4d167f02007-05-09 02:35:37 -0700913
914static u32 md_csum_fold(u32 csum)
915{
916 csum = (csum & 0xffff) + (csum >> 16);
917 return (csum & 0xffff) + (csum >> 16);
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static unsigned int calc_sb_csum(mdp_super_t * sb)
921{
NeilBrown4d167f02007-05-09 02:35:37 -0700922 u64 newcsum = 0;
923 u32 *sb32 = (u32*)sb;
924 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned int disk_csum, csum;
926
927 disk_csum = sb->sb_csum;
928 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700929
930 for (i = 0; i < MD_SB_BYTES/4 ; i++)
931 newcsum += sb32[i];
932 csum = (newcsum & 0xffffffff) + (newcsum>>32);
933
934
935#ifdef CONFIG_ALPHA
936 /* This used to use csum_partial, which was wrong for several
937 * reasons including that different results are returned on
938 * different architectures. It isn't critical that we get exactly
939 * the same return value as before (we always csum_fold before
940 * testing, and that removes any differences). However as we
941 * know that csum_partial always returned a 16bit value on
942 * alphas, do a fold to maximise conformity to previous behaviour.
943 */
944 sb->sb_csum = md_csum_fold(disk_csum);
945#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return csum;
949}
950
951
952/*
953 * Handle superblock details.
954 * We want to be able to handle multiple superblock formats
955 * so we have a common interface to them all, and an array of
956 * different handlers.
957 * We rely on user-space to write the initial superblock, and support
958 * reading and updating of superblocks.
959 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100960 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * loads and validates a superblock on dev.
962 * if refdev != NULL, compare superblocks on both devices
963 * Return:
964 * 0 - dev has a superblock that is compatible with refdev
965 * 1 - dev has a superblock that is compatible and newer than refdev
966 * so dev should be used as the refdev in future
967 * -EINVAL superblock incompatible or invalid
968 * -othererror e.g. -EIO
969 *
NeilBrownfd01b882011-10-11 16:47:53 +1100970 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * Verify that dev is acceptable into mddev.
972 * The first time, mddev->raid_disks will be 0, and data from
973 * dev should be merged in. Subsequent calls check that dev
974 * is new enough. Return 0 or -EINVAL
975 *
NeilBrownfd01b882011-10-11 16:47:53 +1100976 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * Update the superblock for rdev with data in mddev
978 * This does not write to disc.
979 *
980 */
981
982struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000983 char *name;
984 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000985 int (*load_super)(struct md_rdev *rdev,
986 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000987 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000988 int (*validate_super)(struct mddev *mddev,
989 struct md_rdev *rdev);
990 void (*sync_super)(struct mddev *mddev,
991 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100992 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000993 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000994 int (*allow_new_offset)(struct md_rdev *rdev,
995 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996};
997
998/*
Andre Noll0894cc32009-06-18 08:49:23 +1000999 * Check that the given mddev has no bitmap.
1000 *
1001 * This function is called from the run method of all personalities that do not
1002 * support bitmaps. It prints an error message and returns non-zero if mddev
1003 * has a bitmap. Otherwise, it returns 0.
1004 *
1005 */
NeilBrownfd01b882011-10-11 16:47:53 +11001006int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001007{
NeilBrownc3d97142009-12-14 12:49:52 +11001008 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001009 return 0;
1010 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1011 mdname(mddev), mddev->pers->name);
1012 return 1;
1013}
1014EXPORT_SYMBOL(md_check_no_bitmap);
1015
1016/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * load_super for 0.90.0
1018 */
NeilBrown3cb03002011-10-11 16:45:26 +11001019static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1022 mdp_super_t *sb;
1023 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /*
Andre Noll0f420352008-07-11 22:02:23 +10001026 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * it's at the end of the disk.
1028 *
1029 * It also happens to be a multiple of 4Kb.
1030 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001031 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
NeilBrown0002b272005-09-09 16:23:53 -07001033 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (ret) return ret;
1035
1036 ret = -EINVAL;
1037
1038 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001039 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (sb->md_magic != MD_SB_MAGIC) {
1042 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1043 b);
1044 goto abort;
1045 }
1046
1047 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001048 sb->minor_version < 90 ||
1049 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1051 sb->major_version, sb->minor_version,
1052 b);
1053 goto abort;
1054 }
1055
1056 if (sb->raid_disks <= 0)
1057 goto abort;
1058
NeilBrown4d167f02007-05-09 02:35:37 -07001059 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1061 b);
1062 goto abort;
1063 }
1064
1065 rdev->preferred_minor = sb->md_minor;
1066 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001067 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001068 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001069 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (sb->level == LEVEL_MULTIPATH)
1072 rdev->desc_nr = -1;
1073 else
1074 rdev->desc_nr = sb->this_disk.number;
1075
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001076 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001078 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001080 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!uuid_equal(refsb, sb)) {
1082 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1083 b, bdevname(refdev->bdev,b2));
1084 goto abort;
1085 }
1086 if (!sb_equal(refsb, sb)) {
1087 printk(KERN_WARNING "md: %s has same UUID"
1088 " but different superblock to %s\n",
1089 b, bdevname(refdev->bdev, b2));
1090 goto abort;
1091 }
1092 ev1 = md_event(sb);
1093 ev2 = md_event(refsb);
1094 if (ev1 > ev2)
1095 ret = 1;
1096 else
1097 ret = 0;
1098 }
NeilBrown8190e752009-06-18 08:48:58 +10001099 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001100 /* Limit to 4TB as metadata cannot record more than that.
1101 * (not needed for Linear and RAID0 as metadata doesn't
1102 * record this size)
1103 */
1104 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001105 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
NeilBrown27a7b262011-09-10 17:21:28 +10001107 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001108 /* "this cannot possibly happen" ... */
1109 ret = -EINVAL;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 abort:
1112 return ret;
1113}
1114
1115/*
1116 * validate_super for 0.90.0
1117 */
NeilBrownfd01b882011-10-11 16:47:53 +11001118static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001121 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001122 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
NeilBrown41158c72005-06-21 17:17:25 -07001124 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001125 clear_bit(Faulty, &rdev->flags);
1126 clear_bit(In_sync, &rdev->flags);
1127 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (mddev->raid_disks == 0) {
1130 mddev->major_version = 0;
1131 mddev->minor_version = sb->minor_version;
1132 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001133 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001134 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 mddev->ctime = sb->ctime;
1136 mddev->utime = sb->utime;
1137 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001138 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 mddev->layout = sb->layout;
1140 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001141 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001142 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001143 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001144 mddev->bitmap_info.space = 0;
1145 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001146 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001147 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001148 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
NeilBrownf6705572006-03-27 01:18:11 -08001150 if (mddev->minor_version >= 91) {
1151 mddev->reshape_position = sb->reshape_position;
1152 mddev->delta_disks = sb->delta_disks;
1153 mddev->new_level = sb->new_level;
1154 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001155 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001156 if (mddev->delta_disks < 0)
1157 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001158 } else {
1159 mddev->reshape_position = MaxSector;
1160 mddev->delta_disks = 0;
1161 mddev->new_level = mddev->level;
1162 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001163 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (sb->state & (1<<MD_SB_CLEAN))
1167 mddev->recovery_cp = MaxSector;
1168 else {
1169 if (sb->events_hi == sb->cp_events_hi &&
1170 sb->events_lo == sb->cp_events_lo) {
1171 mddev->recovery_cp = sb->recovery_cp;
1172 } else
1173 mddev->recovery_cp = 0;
1174 }
1175
1176 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1177 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1178 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1179 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1180
1181 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001182
1183 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001184 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001185 mddev->bitmap_info.offset =
1186 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001187 mddev->bitmap_info.space =
1188 mddev->bitmap_info.space;
1189 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001190
NeilBrown41158c72005-06-21 17:17:25 -07001191 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001192 /* Insist on good event counter while assembling, except
1193 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001195 if (sb->disks[rdev->desc_nr].state & (
1196 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1197 if (ev1 < mddev->events)
1198 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001199 } else if (mddev->bitmap) {
1200 /* if adding to array with a bitmap, then we can accept an
1201 * older device ... but not too old.
1202 */
NeilBrown41158c72005-06-21 17:17:25 -07001203 if (ev1 < mddev->bitmap->events_cleared)
1204 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001205 } else {
1206 if (ev1 < mddev->events)
1207 /* just a hot-add of a new device, leave raid_disk at -1 */
1208 return 0;
1209 }
NeilBrown41158c72005-06-21 17:17:25 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 desc = sb->disks + rdev->desc_nr;
1213
1214 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001215 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001216 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1217 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001218 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001220 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1221 /* active but not in sync implies recovery up to
1222 * reshape position. We don't know exactly where
1223 * that is, so set to zero for now */
1224 if (mddev->minor_version >= 91) {
1225 rdev->recovery_offset = 0;
1226 rdev->raid_disk = desc->raid_disk;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001229 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1230 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001231 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001232 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234}
1235
1236/*
1237 * sync_super for 0.90.0
1238 */
NeilBrownfd01b882011-10-11 16:47:53 +11001239static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001242 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* make rdev->sb match mddev data..
1247 *
1248 * 1/ zero out disks
1249 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1250 * 3/ any empty disks < next_spare become removed
1251 *
1252 * disks[0] gets initialised to REMOVED because
1253 * we cannot be sure from other fields if it has
1254 * been initialised or not.
1255 */
1256 int i;
1257 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1258
NeilBrown61181562005-09-09 16:24:02 -07001259 rdev->sb_size = MD_SB_BYTES;
1260
Namhyung Kim65a06f062011-07-27 11:00:36 +10001261 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 memset(sb, 0, sizeof(*sb));
1264
1265 sb->md_magic = MD_SB_MAGIC;
1266 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sb->patch_version = mddev->patch_version;
1268 sb->gvalid_words = 0; /* ignored */
1269 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1270 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1271 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1272 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1273
1274 sb->ctime = mddev->ctime;
1275 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001276 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sb->raid_disks = mddev->raid_disks;
1278 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001279 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 sb->utime = mddev->utime;
1281 sb->state = 0;
1282 sb->events_hi = (mddev->events>>32);
1283 sb->events_lo = (u32)mddev->events;
1284
NeilBrownf6705572006-03-27 01:18:11 -08001285 if (mddev->reshape_position == MaxSector)
1286 sb->minor_version = 90;
1287 else {
1288 sb->minor_version = 91;
1289 sb->reshape_position = mddev->reshape_position;
1290 sb->new_level = mddev->new_level;
1291 sb->delta_disks = mddev->delta_disks;
1292 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001293 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001294 }
1295 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (mddev->in_sync)
1297 {
1298 sb->recovery_cp = mddev->recovery_cp;
1299 sb->cp_events_hi = (mddev->events>>32);
1300 sb->cp_events_lo = (u32)mddev->events;
1301 if (mddev->recovery_cp == MaxSector)
1302 sb->state = (1<< MD_SB_CLEAN);
1303 } else
1304 sb->recovery_cp = 0;
1305
1306 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001307 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
NeilBrownc3d97142009-12-14 12:49:52 +11001309 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001310 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001313 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001315 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001316 int is_active = test_bit(In_sync, &rdev2->flags);
1317
1318 if (rdev2->raid_disk >= 0 &&
1319 sb->minor_version >= 91)
1320 /* we have nowhere to store the recovery_offset,
1321 * but if it is not below the reshape_position,
1322 * we can piggy-back on that.
1323 */
1324 is_active = 1;
1325 if (rdev2->raid_disk < 0 ||
1326 test_bit(Faulty, &rdev2->flags))
1327 is_active = 0;
1328 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001329 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001331 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001332 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 d = &sb->disks[rdev2->desc_nr];
1334 nr_disks++;
1335 d->number = rdev2->desc_nr;
1336 d->major = MAJOR(rdev2->bdev->bd_dev);
1337 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001338 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 d->raid_disk = rdev2->raid_disk;
1340 else
1341 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001342 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001344 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001346 if (test_bit(In_sync, &rdev2->flags))
1347 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 active++;
1349 working++;
1350 } else {
1351 d->state = 0;
1352 spare++;
1353 working++;
1354 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001355 if (test_bit(WriteMostly, &rdev2->flags))
1356 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* now set the "removed" and "faulty" bits on any missing devices */
1359 for (i=0 ; i < mddev->raid_disks ; i++) {
1360 mdp_disk_t *d = &sb->disks[i];
1361 if (d->state == 0 && d->number == 0) {
1362 d->number = i;
1363 d->raid_disk = i;
1364 d->state = (1<<MD_DISK_REMOVED);
1365 d->state |= (1<<MD_DISK_FAULTY);
1366 failed++;
1367 }
1368 }
1369 sb->nr_disks = nr_disks;
1370 sb->active_disks = active;
1371 sb->working_disks = working;
1372 sb->failed_disks = failed;
1373 sb->spare_disks = spare;
1374
1375 sb->this_disk = sb->disks[rdev->desc_nr];
1376 sb->sb_csum = calc_sb_csum(sb);
1377}
1378
1379/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001380 * rdev_size_change for 0.90.0
1381 */
1382static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001383super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001384{
Andre Noll58c0fed2009-03-31 14:33:13 +11001385 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001386 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001387 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001388 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001389 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001390 if (!num_sectors || num_sectors > rdev->sb_start)
1391 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001392 /* Limit to 4TB as metadata cannot record more than that.
1393 * 4TB == 2^32 KB, or 2*2^32 sectors.
1394 */
NeilBrown667a5312012-08-16 16:46:12 +10001395 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001396 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001397 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001398 rdev->sb_page);
1399 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001400 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001401}
1402
NeilBrownc6563a82012-05-21 09:27:00 +10001403static int
1404super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1405{
1406 /* non-zero offset changes not possible with v0.90 */
1407 return new_offset == 0;
1408}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001409
1410/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * version 1 superblock
1412 */
1413
NeilBrown1c05b4b2006-10-21 10:24:08 -07001414static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001416 __le32 disk_csum;
1417 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 unsigned long long newcsum;
1419 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001420 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 disk_csum = sb->sb_csum;
1423 sb->sb_csum = 0;
1424 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001425 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 newcsum += le32_to_cpu(*isuper++);
1427
1428 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001429 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1432 sb->sb_csum = disk_csum;
1433 return cpu_to_le32(csum);
1434}
1435
NeilBrown2699b672011-07-28 11:31:47 +10001436static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1437 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001438static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct mdp_superblock_1 *sb;
1441 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001442 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001443 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001445 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /*
Andre Noll0f420352008-07-11 22:02:23 +10001448 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 * It is always aligned to a 4K boundary and
1450 * depeding on minor_version, it can be:
1451 * 0: At least 8K, but less than 12K, from end of device
1452 * 1: At start of device
1453 * 2: 4K from start of device.
1454 */
1455 switch(minor_version) {
1456 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001457 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001458 sb_start -= 8*2;
1459 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
1461 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001462 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001465 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 default:
1468 return -EINVAL;
1469 }
Andre Noll0f420352008-07-11 22:02:23 +10001470 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
NeilBrown0002b272005-09-09 16:23:53 -07001472 /* superblock is rarely larger than 1K, but it can be larger,
1473 * and it is safe to read 4k, so we do that
1474 */
1475 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (ret) return ret;
1477
1478
Namhyung Kim65a06f062011-07-27 11:00:36 +10001479 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1482 sb->major_version != cpu_to_le32(1) ||
1483 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001484 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001485 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EINVAL;
1487
1488 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1489 printk("md: invalid superblock checksum on %s\n",
1490 bdevname(rdev->bdev,b));
1491 return -EINVAL;
1492 }
1493 if (le64_to_cpu(sb->data_size) < 10) {
1494 printk("md: data_size too small on %s\n",
1495 bdevname(rdev->bdev,b));
1496 return -EINVAL;
1497 }
NeilBrownc6563a82012-05-21 09:27:00 +10001498 if (sb->pad0 ||
1499 sb->pad3[0] ||
1500 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1501 /* Some padding is non-zero, might be a new feature */
1502 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 rdev->preferred_minor = 0xffff;
1505 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001506 rdev->new_data_offset = rdev->data_offset;
1507 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1508 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1509 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001510 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
NeilBrown0002b272005-09-09 16:23:53 -07001512 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001513 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001514 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001515 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1516
1517 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001518 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001519 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001520 if (minor_version
1521 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1522 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001523
NeilBrown31b65a02006-07-10 04:44:14 -07001524 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1525 rdev->desc_nr = -1;
1526 else
1527 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1528
NeilBrown2699b672011-07-28 11:31:47 +10001529 if (!rdev->bb_page) {
1530 rdev->bb_page = alloc_page(GFP_KERNEL);
1531 if (!rdev->bb_page)
1532 return -ENOMEM;
1533 }
1534 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1535 rdev->badblocks.count == 0) {
1536 /* need to load the bad block list.
1537 * Currently we limit it to one page.
1538 */
1539 s32 offset;
1540 sector_t bb_sector;
1541 u64 *bbp;
1542 int i;
1543 int sectors = le16_to_cpu(sb->bblog_size);
1544 if (sectors > (PAGE_SIZE / 512))
1545 return -EINVAL;
1546 offset = le32_to_cpu(sb->bblog_offset);
1547 if (offset == 0)
1548 return -EINVAL;
1549 bb_sector = (long long)offset;
1550 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1551 rdev->bb_page, READ, true))
1552 return -EIO;
1553 bbp = (u64 *)page_address(rdev->bb_page);
1554 rdev->badblocks.shift = sb->bblog_shift;
1555 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1556 u64 bb = le64_to_cpu(*bbp);
1557 int count = bb & (0x3ff);
1558 u64 sector = bb >> 10;
1559 sector <<= sb->bblog_shift;
1560 count <<= sb->bblog_shift;
1561 if (bb + 1 == 0)
1562 break;
1563 if (md_set_badblocks(&rdev->badblocks,
1564 sector, count, 1) == 0)
1565 return -EINVAL;
1566 }
1567 } else if (sb->bblog_offset == 0)
1568 rdev->badblocks.shift = -1;
1569
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001570 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001571 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001572 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001574 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1577 sb->level != refsb->level ||
1578 sb->layout != refsb->layout ||
1579 sb->chunksize != refsb->chunksize) {
1580 printk(KERN_WARNING "md: %s has strangely different"
1581 " superblock to %s\n",
1582 bdevname(rdev->bdev,b),
1583 bdevname(refdev->bdev,b2));
1584 return -EINVAL;
1585 }
1586 ev1 = le64_to_cpu(sb->events);
1587 ev2 = le64_to_cpu(refsb->events);
1588
1589 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001590 ret = 1;
1591 else
1592 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
NeilBrownc6563a82012-05-21 09:27:00 +10001594 if (minor_version) {
1595 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1596 sectors -= rdev->data_offset;
1597 } else
1598 sectors = rdev->sb_start;
1599 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001601 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001602 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
NeilBrownfd01b882011-10-11 16:47:53 +11001605static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001607 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001608 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
NeilBrown41158c72005-06-21 17:17:25 -07001610 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001611 clear_bit(Faulty, &rdev->flags);
1612 clear_bit(In_sync, &rdev->flags);
1613 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (mddev->raid_disks == 0) {
1616 mddev->major_version = 1;
1617 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001618 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001619 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1621 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1622 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001623 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 mddev->layout = le32_to_cpu(sb->layout);
1625 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001626 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001627 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001628 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001629 mddev->bitmap_info.space = 0;
1630 /* Default location for bitmap is 1K after superblock
1631 * using 3K - total of 4K
1632 */
NeilBrownc3d97142009-12-14 12:49:52 +11001633 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001634 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001635 mddev->reshape_backwards = 0;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1638 memcpy(mddev->uuid, sb->set_uuid, 16);
1639
1640 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001641
NeilBrown71c08052005-09-09 16:23:51 -07001642 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001643 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001644 mddev->bitmap_info.offset =
1645 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001646 /* Metadata doesn't record how much space is available.
1647 * For 1.0, we assume we can use up to the superblock
1648 * if before, else to 4K beyond superblock.
1649 * For others, assume no change is possible.
1650 */
1651 if (mddev->minor_version > 0)
1652 mddev->bitmap_info.space = 0;
1653 else if (mddev->bitmap_info.offset > 0)
1654 mddev->bitmap_info.space =
1655 8 - mddev->bitmap_info.offset;
1656 else
1657 mddev->bitmap_info.space =
1658 -mddev->bitmap_info.offset;
1659 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001660
NeilBrownf6705572006-03-27 01:18:11 -08001661 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1662 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1663 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1664 mddev->new_level = le32_to_cpu(sb->new_level);
1665 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001666 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001667 if (mddev->delta_disks < 0 ||
1668 (mddev->delta_disks == 0 &&
1669 (le32_to_cpu(sb->feature_map)
1670 & MD_FEATURE_RESHAPE_BACKWARDS)))
1671 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001672 } else {
1673 mddev->reshape_position = MaxSector;
1674 mddev->delta_disks = 0;
1675 mddev->new_level = mddev->level;
1676 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001677 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001678 }
1679
NeilBrown41158c72005-06-21 17:17:25 -07001680 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001681 /* Insist of good event counter while assembling, except for
1682 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001684 if (rdev->desc_nr >= 0 &&
1685 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1686 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1687 if (ev1 < mddev->events)
1688 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001689 } else if (mddev->bitmap) {
1690 /* If adding to array with a bitmap, then we can accept an
1691 * older device, but not too old.
1692 */
NeilBrown41158c72005-06-21 17:17:25 -07001693 if (ev1 < mddev->bitmap->events_cleared)
1694 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001695 } else {
1696 if (ev1 < mddev->events)
1697 /* just a hot-add of a new device, leave raid_disk at -1 */
1698 return 0;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (mddev->level != LEVEL_MULTIPATH) {
1701 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001702 if (rdev->desc_nr < 0 ||
1703 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1704 role = 0xffff;
1705 rdev->desc_nr = -1;
1706 } else
1707 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 switch(role) {
1709 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
1711 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001712 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001715 if ((le32_to_cpu(sb->feature_map) &
1716 MD_FEATURE_RECOVERY_OFFSET))
1717 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1718 else
1719 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rdev->raid_disk = role;
1721 break;
1722 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001723 if (sb->devflags & WriteMostly1)
1724 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001725 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1726 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001727 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001728 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
1731}
1732
NeilBrownfd01b882011-10-11 16:47:53 +11001733static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001736 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int max_dev, i;
1738 /* make rdev->sb match mddev and rdev data. */
1739
Namhyung Kim65a06f062011-07-27 11:00:36 +10001740 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 sb->feature_map = 0;
1743 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001744 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 memset(sb->pad3, 0, sizeof(sb->pad3));
1746
1747 sb->utime = cpu_to_le64((__u64)mddev->utime);
1748 sb->events = cpu_to_le64(mddev->events);
1749 if (mddev->in_sync)
1750 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1751 else
1752 sb->resync_offset = cpu_to_le64(0);
1753
NeilBrown1c05b4b2006-10-21 10:24:08 -07001754 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001755
NeilBrownf0ca3402006-02-02 14:28:04 -08001756 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001757 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001758 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001759 sb->level = cpu_to_le32(mddev->level);
1760 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001761
NeilBrownaeb9b2112011-08-25 14:43:08 +10001762 if (test_bit(WriteMostly, &rdev->flags))
1763 sb->devflags |= WriteMostly1;
1764 else
1765 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001766 sb->data_offset = cpu_to_le64(rdev->data_offset);
1767 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001768
NeilBrownc3d97142009-12-14 12:49:52 +11001769 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1770 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001771 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001772 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001773
1774 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001775 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001776 sb->feature_map |=
1777 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1778 sb->recovery_offset =
1779 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001780 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001781 if (test_bit(Replacement, &rdev->flags))
1782 sb->feature_map |=
1783 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001784
NeilBrownf6705572006-03-27 01:18:11 -08001785 if (mddev->reshape_position != MaxSector) {
1786 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1787 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1788 sb->new_layout = cpu_to_le32(mddev->new_layout);
1789 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1790 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001791 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001792 if (mddev->delta_disks == 0 &&
1793 mddev->reshape_backwards)
1794 sb->feature_map
1795 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001796 if (rdev->new_data_offset != rdev->data_offset) {
1797 sb->feature_map
1798 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1799 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1800 - rdev->data_offset));
1801 }
NeilBrownf6705572006-03-27 01:18:11 -08001802 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001803
NeilBrown2699b672011-07-28 11:31:47 +10001804 if (rdev->badblocks.count == 0)
1805 /* Nothing to do for bad blocks*/ ;
1806 else if (sb->bblog_offset == 0)
1807 /* Cannot record bad blocks on this device */
1808 md_error(mddev, rdev);
1809 else {
1810 struct badblocks *bb = &rdev->badblocks;
1811 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1812 u64 *p = bb->page;
1813 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1814 if (bb->changed) {
1815 unsigned seq;
1816
1817retry:
1818 seq = read_seqbegin(&bb->lock);
1819
1820 memset(bbp, 0xff, PAGE_SIZE);
1821
1822 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001823 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001824 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1825 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001826 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001827 }
NeilBrownd0962932012-03-19 12:46:41 +11001828 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001829 if (read_seqretry(&bb->lock, seq))
1830 goto retry;
1831
1832 bb->sector = (rdev->sb_start +
1833 (int)le32_to_cpu(sb->bblog_offset));
1834 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001835 }
1836 }
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001839 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (rdev2->desc_nr+1 > max_dev)
1841 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001842
NeilBrown70471da2009-08-03 10:59:57 +10001843 if (max_dev > le32_to_cpu(sb->max_dev)) {
1844 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001845 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001846 rdev->sb_size = max_dev * 2 + 256;
1847 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1848 if (rdev->sb_size & bmask)
1849 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001850 } else
1851 max_dev = le32_to_cpu(sb->max_dev);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 for (i=0; i<max_dev;i++)
1854 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1855
NeilBrowndafb20f2012-03-19 12:46:39 +11001856 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001858 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001860 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001862 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001863 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 else
1865 sb->dev_roles[i] = cpu_to_le16(0xffff);
1866 }
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 sb->sb_csum = calc_sb_1_csum(sb);
1869}
1870
Chris Webb0cd17fe2008-06-28 08:31:46 +10001871static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001872super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873{
1874 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001875 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001876 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001877 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001878 if (rdev->data_offset != rdev->new_data_offset)
1879 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001880 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001882 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001883 max_sectors -= rdev->data_offset;
1884 if (!num_sectors || num_sectors > max_sectors)
1885 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001886 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887 /* minor version 0 with bitmap we can't move */
1888 return 0;
1889 } else {
1890 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001891 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001892 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001893 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001894 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001895 if (!num_sectors || num_sectors > max_sectors)
1896 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001897 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001899 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001900 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001901 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001902 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001903 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001904 rdev->sb_page);
1905 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001906 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001907
1908}
1909
1910static int
1911super_1_allow_new_offset(struct md_rdev *rdev,
1912 unsigned long long new_offset)
1913{
1914 /* All necessary checks on new >= old have been done */
1915 struct bitmap *bitmap;
1916 if (new_offset >= rdev->data_offset)
1917 return 1;
1918
1919 /* with 1.0 metadata, there is no metadata to tread on
1920 * so we can always move back */
1921 if (rdev->mddev->minor_version == 0)
1922 return 1;
1923
1924 /* otherwise we must be sure not to step on
1925 * any metadata, so stay:
1926 * 36K beyond start of superblock
1927 * beyond end of badblocks
1928 * beyond write-intent bitmap
1929 */
1930 if (rdev->sb_start + (32+4)*2 > new_offset)
1931 return 0;
1932 bitmap = rdev->mddev->bitmap;
1933 if (bitmap && !rdev->mddev->bitmap_info.file &&
1934 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001935 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001936 return 0;
1937 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1938 return 0;
1939
1940 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Adrian Bunk75c96f82005-05-05 16:16:09 -07001943static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 [0] = {
1945 .name = "0.90.0",
1946 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001947 .load_super = super_90_load,
1948 .validate_super = super_90_validate,
1949 .sync_super = super_90_sync,
1950 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001951 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 },
1953 [1] = {
1954 .name = "md-1",
1955 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001956 .load_super = super_1_load,
1957 .validate_super = super_1_validate,
1958 .sync_super = super_1_sync,
1959 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001960 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 },
1962};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
NeilBrownfd01b882011-10-11 16:47:53 +11001964static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001965{
1966 if (mddev->sync_super) {
1967 mddev->sync_super(mddev, rdev);
1968 return;
1969 }
1970
1971 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1972
1973 super_types[mddev->major_version].sync_super(mddev, rdev);
1974}
1975
NeilBrownfd01b882011-10-11 16:47:53 +11001976static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
NeilBrown3cb03002011-10-11 16:45:26 +11001978 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
NeilBrown4b809912008-07-21 17:05:25 +10001980 rcu_read_lock();
1981 rdev_for_each_rcu(rdev, mddev1)
1982 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001983 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001984 rdev2->bdev->bd_contains) {
1985 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001986 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001987 }
1988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 0;
1990}
1991
1992static LIST_HEAD(pending_raid_disks);
1993
Andre Nollac5e7112009-08-03 10:59:47 +10001994/*
1995 * Try to register data integrity profile for an mddev
1996 *
1997 * This is called when an array is started and after a disk has been kicked
1998 * from the array. It only succeeds if all working and active component devices
1999 * are integrity capable with matching profiles.
2000 */
NeilBrownfd01b882011-10-11 16:47:53 +11002001int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002002{
NeilBrown3cb03002011-10-11 16:45:26 +11002003 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002004
Andre Nollac5e7112009-08-03 10:59:47 +10002005 if (list_empty(&mddev->disks))
2006 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002007 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2008 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002009 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002010 /* skip spares and non-functional disks */
2011 if (test_bit(Faulty, &rdev->flags))
2012 continue;
2013 if (rdev->raid_disk < 0)
2014 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002015 if (!reference) {
2016 /* Use the first rdev as the reference */
2017 reference = rdev;
2018 continue;
2019 }
2020 /* does this rdev's profile match the reference profile? */
2021 if (blk_integrity_compare(reference->bdev->bd_disk,
2022 rdev->bdev->bd_disk) < 0)
2023 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002024 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002025 if (!reference || !bdev_get_integrity(reference->bdev))
2026 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002027 /*
2028 * All component devices are integrity capable and have matching
2029 * profiles, register the common profile for the md device.
2030 */
2031 if (blk_integrity_register(mddev->gendisk,
2032 bdev_get_integrity(reference->bdev)) != 0) {
2033 printk(KERN_ERR "md: failed to register integrity for %s\n",
2034 mdname(mddev));
2035 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002036 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002037 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2038 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2039 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2040 mdname(mddev));
2041 return -EINVAL;
2042 }
Andre Nollac5e7112009-08-03 10:59:47 +10002043 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002044}
Andre Nollac5e7112009-08-03 10:59:47 +10002045EXPORT_SYMBOL(md_integrity_register);
2046
2047/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002048void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002049{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002050 struct blk_integrity *bi_rdev;
2051 struct blk_integrity *bi_mddev;
2052
2053 if (!mddev->gendisk)
2054 return;
2055
2056 bi_rdev = bdev_get_integrity(rdev->bdev);
2057 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002058
2059 if (!bi_mddev) /* nothing to do */
2060 return;
2061 if (rdev->raid_disk < 0) /* skip spares */
2062 return;
2063 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2064 rdev->bdev->bd_disk) >= 0)
2065 return;
2066 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2067 blk_integrity_unregister(mddev->gendisk);
2068}
2069EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002070
NeilBrownfd01b882011-10-11 16:47:53 +11002071static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002073 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002074 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002075 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 if (rdev->mddev) {
2079 MD_BUG();
2080 return -EINVAL;
2081 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002082
2083 /* prevent duplicates */
2084 if (find_rdev(mddev, rdev->bdev->bd_dev))
2085 return -EEXIST;
2086
Andre Nolldd8ac332009-03-31 14:33:13 +11002087 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2088 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2089 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002090 if (mddev->pers) {
2091 /* Cannot change size, so fail
2092 * If mddev->level <= 0, then we don't care
2093 * about aligning sizes (e.g. linear)
2094 */
2095 if (mddev->level > 0)
2096 return -ENOSPC;
2097 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002098 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /* Verify rdev->desc_nr is unique.
2102 * If it is -1, assign a free number, else
2103 * check number is not in use
2104 */
2105 if (rdev->desc_nr < 0) {
2106 int choice = 0;
2107 if (mddev->pers) choice = mddev->raid_disks;
2108 while (find_rdev_nr(mddev, choice))
2109 choice++;
2110 rdev->desc_nr = choice;
2111 } else {
2112 if (find_rdev_nr(mddev, rdev->desc_nr))
2113 return -EBUSY;
2114 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002115 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2116 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2117 mdname(mddev), mddev->max_disks);
2118 return -EBUSY;
2119 }
NeilBrown19133a42005-11-08 21:39:35 -08002120 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002121 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002122 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002125 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002126
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002127 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002128 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002129
Tejun Heo0762b8b2008-08-25 19:56:12 +09002130 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002131 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2132 /* failure here is OK */;
2133 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002134
NeilBrown4b809912008-07-21 17:05:25 +10002135 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002136 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002137
2138 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002139 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002142
2143 fail:
2144 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2145 b, mdname(mddev));
2146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
NeilBrown177a99b2008-02-06 01:39:56 -08002149static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002150{
NeilBrown3cb03002011-10-11 16:45:26 +11002151 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002152 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002153 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002154}
2155
NeilBrown3cb03002011-10-11 16:45:26 +11002156static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 char b[BDEVNAME_SIZE];
2159 if (!rdev->mddev) {
2160 MD_BUG();
2161 return;
2162 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002163 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002164 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2166 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002167 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002168 sysfs_put(rdev->sysfs_state);
2169 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002170 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002171 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002172 * writing to 'remove' to "dev/state". We also need
2173 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002174 */
NeilBrown4b809912008-07-21 17:05:25 +10002175 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002176 INIT_WORK(&rdev->del_work, md_delayed_delete);
2177 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002178 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181/*
2182 * prevent the device from being mounted, repartitioned or
2183 * otherwise reused by a RAID array (or any other kernel
2184 * subsystem), by bd_claiming the device.
2185 */
NeilBrown3cb03002011-10-11 16:45:26 +11002186static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int err = 0;
2189 struct block_device *bdev;
2190 char b[BDEVNAME_SIZE];
2191
Tejun Heod4d77622010-11-13 11:55:18 +01002192 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002193 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (IS_ERR(bdev)) {
2195 printk(KERN_ERR "md: could not open %s.\n",
2196 __bdevname(dev, b));
2197 return PTR_ERR(bdev);
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 rdev->bdev = bdev;
2200 return err;
2201}
2202
NeilBrown3cb03002011-10-11 16:45:26 +11002203static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 struct block_device *bdev = rdev->bdev;
2206 rdev->bdev = NULL;
2207 if (!bdev)
2208 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002209 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212void md_autodetect_dev(dev_t dev);
2213
NeilBrown3cb03002011-10-11 16:45:26 +11002214static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 char b[BDEVNAME_SIZE];
2217 printk(KERN_INFO "md: export_rdev(%s)\n",
2218 bdevname(rdev->bdev,b));
2219 if (rdev->mddev)
2220 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002221 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002223 if (test_bit(AutoDetected, &rdev->flags))
2224 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#endif
2226 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002227 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
NeilBrown3cb03002011-10-11 16:45:26 +11002230static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 unbind_rdev_from_array(rdev);
2233 export_rdev(rdev);
2234}
2235
NeilBrownfd01b882011-10-11 16:47:53 +11002236static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
NeilBrown3cb03002011-10-11 16:45:26 +11002238 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
NeilBrowndafb20f2012-03-19 12:46:39 +11002240 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (!rdev->mddev) {
2242 MD_BUG();
2243 continue;
2244 }
2245 kick_rdev_from_array(rdev);
2246 }
2247 if (!list_empty(&mddev->disks))
2248 MD_BUG();
2249 mddev->raid_disks = 0;
2250 mddev->major_version = 0;
2251}
2252
2253static void print_desc(mdp_disk_t *desc)
2254{
2255 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2256 desc->major,desc->minor,desc->raid_disk,desc->state);
2257}
2258
Cheng Renquancd2ac932009-01-09 08:31:08 +11002259static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 int i;
2262
2263 printk(KERN_INFO
2264 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2265 sb->major_version, sb->minor_version, sb->patch_version,
2266 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2267 sb->ctime);
2268 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2269 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2270 sb->md_minor, sb->layout, sb->chunk_size);
2271 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2272 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2273 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2274 sb->failed_disks, sb->spare_disks,
2275 sb->sb_csum, (unsigned long)sb->events_lo);
2276
2277 printk(KERN_INFO);
2278 for (i = 0; i < MD_SB_DISKS; i++) {
2279 mdp_disk_t *desc;
2280
2281 desc = sb->disks + i;
2282 if (desc->number || desc->major || desc->minor ||
2283 desc->raid_disk || (desc->state && (desc->state != 4))) {
2284 printk(" D %2d: ", i);
2285 print_desc(desc);
2286 }
2287 }
2288 printk(KERN_INFO "md: THIS: ");
2289 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Cheng Renquancd2ac932009-01-09 08:31:08 +11002292static void print_sb_1(struct mdp_superblock_1 *sb)
2293{
2294 __u8 *uuid;
2295
2296 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002297 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002298 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002299 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002300 le32_to_cpu(sb->major_version),
2301 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002302 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002303 sb->set_name,
2304 (unsigned long long)le64_to_cpu(sb->ctime)
2305 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2306
2307 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002308 printk(KERN_INFO
2309 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002310 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002311 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002312 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2313 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002314 le32_to_cpu(sb->level),
2315 (unsigned long long)le64_to_cpu(sb->size),
2316 le32_to_cpu(sb->raid_disks),
2317 le32_to_cpu(sb->layout),
2318 le32_to_cpu(sb->chunksize),
2319 (unsigned long long)le64_to_cpu(sb->data_offset),
2320 (unsigned long long)le64_to_cpu(sb->data_size),
2321 (unsigned long long)le64_to_cpu(sb->super_offset),
2322 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2323 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002324 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002325 sb->devflags,
2326 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2327 (unsigned long long)le64_to_cpu(sb->events),
2328 (unsigned long long)le64_to_cpu(sb->resync_offset),
2329 le32_to_cpu(sb->sb_csum),
2330 le32_to_cpu(sb->max_dev)
2331 );
2332}
2333
NeilBrown3cb03002011-10-11 16:45:26 +11002334static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002337 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2338 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002339 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2340 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002342 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2343 switch (major_version) {
2344 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002345 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002346 break;
2347 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002348 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002349 break;
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 } else
2352 printk(KERN_INFO "md: no rdev superblock!\n");
2353}
2354
Adrian Bunk5e563412006-06-26 00:27:42 -07002355static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002357 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002358 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002359 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 char b[BDEVNAME_SIZE];
2361
2362 printk("\n");
2363 printk("md: **********************************\n");
2364 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2365 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002366 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
NeilBrown32a76272005-06-21 17:17:14 -07002368 if (mddev->bitmap)
2369 bitmap_print_sb(mddev->bitmap);
2370 else
2371 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002372 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 printk("<%s>", bdevname(rdev->bdev,b));
2374 printk("\n");
2375
NeilBrowndafb20f2012-03-19 12:46:39 +11002376 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002377 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 printk("md: **********************************\n");
2380 printk("\n");
2381}
2382
2383
NeilBrownfd01b882011-10-11 16:47:53 +11002384static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
NeilBrown42543762006-06-26 00:27:57 -07002386 /* Update each superblock (in-memory image), but
2387 * if we are allowed to, skip spares which already
2388 * have the right event counter, or have one earlier
2389 * (which would mean they aren't being marked as dirty
2390 * with the rest of the array)
2391 */
NeilBrown3cb03002011-10-11 16:45:26 +11002392 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002393 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002394 if (rdev->sb_events == mddev->events ||
2395 (nospares &&
2396 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002397 rdev->sb_events+1 == mddev->events)) {
2398 /* Don't update this superblock */
2399 rdev->sb_loaded = 2;
2400 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002401 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002402 rdev->sb_loaded = 1;
2403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405}
2406
NeilBrownfd01b882011-10-11 16:47:53 +11002407static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
NeilBrown3cb03002011-10-11 16:45:26 +11002409 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002410 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002411 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002412 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
NeilBrownd87f0642013-04-24 11:42:40 +10002414 if (mddev->ro) {
2415 if (force_change)
2416 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2417 return;
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002420 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002421 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002422 if (rdev->raid_disk >= 0 &&
2423 mddev->delta_disks >= 0 &&
2424 !test_bit(In_sync, &rdev->flags) &&
2425 mddev->curr_resync_completed > rdev->recovery_offset)
2426 rdev->recovery_offset = mddev->curr_resync_completed;
2427
2428 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002429 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002430 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002431 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002432 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002433 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002434 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002435 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002436 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002437 md_ack_all_badblocks(&rdev->badblocks);
2438 md_error(mddev, rdev);
2439 }
2440 clear_bit(Blocked, &rdev->flags);
2441 clear_bit(BlockedBadBlocks, &rdev->flags);
2442 wake_up(&rdev->blocked_wait);
2443 }
2444 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002445 wake_up(&mddev->sb_wait);
2446 return;
2447 }
2448
NeilBrowna9701a32005-11-08 21:39:34 -08002449 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002450
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002451 mddev->utime = get_seconds();
2452
NeilBrown850b2b422006-10-03 01:15:46 -07002453 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2454 force_change = 1;
2455 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2456 /* just a clean<-> dirty transition, possibly leave spares alone,
2457 * though if events isn't the right even/odd, we will have to do
2458 * spares after all
2459 */
2460 nospares = 1;
2461 if (force_change)
2462 nospares = 0;
2463 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002464 /* If the array is degraded, then skipping spares is both
2465 * dangerous and fairly pointless.
2466 * Dangerous because a device that was removed from the array
2467 * might have a event_count that still looks up-to-date,
2468 * so it can be re-added without a resync.
2469 * Pointless because if there are any spares to skip,
2470 * then a recovery will happen and soon that array won't
2471 * be degraded any more and the spare can go back to sleep then.
2472 */
NeilBrown850b2b422006-10-03 01:15:46 -07002473 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002474
NeilBrown06d91a52005-06-21 17:17:12 -07002475 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002476
2477 /* If this is just a dirty<->clean transition, and the array is clean
2478 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002479 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002480 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002481 && mddev->can_decrease_events
2482 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002483 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002484 mddev->can_decrease_events = 0;
2485 } else {
NeilBrown42543762006-06-26 00:27:57 -07002486 /* otherwise we have to go forward and ... */
2487 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002488 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 if (!mddev->events) {
2492 /*
2493 * oops, this 64-bit counter should never wrap.
2494 * Either we are in around ~1 trillion A.C., assuming
2495 * 1 reboot per second, or we have a bug:
2496 */
2497 MD_BUG();
2498 mddev->events --;
2499 }
NeilBrown2699b672011-07-28 11:31:47 +10002500
NeilBrowndafb20f2012-03-19 12:46:39 +11002501 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002502 if (rdev->badblocks.changed)
2503 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002504 if (test_bit(Faulty, &rdev->flags))
2505 set_bit(FaultRecorded, &rdev->flags);
2506 }
NeilBrown2699b672011-07-28 11:31:47 +10002507
NeilBrowne6910632008-02-06 01:39:51 -08002508 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002509 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
NeilBrown36a4e1f2011-10-07 14:23:17 +11002511 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2512 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
NeilBrown4ad13662007-07-17 04:06:13 -07002514 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002515 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002517
NeilBrown42543762006-06-26 00:27:57 -07002518 if (rdev->sb_loaded != 1)
2519 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002521 if (!test_bit(Faulty, &rdev->flags) &&
2522 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002523 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002524 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002525 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002526 pr_debug("md: (write) %s's sb offset: %llu\n",
2527 bdevname(rdev->bdev, b),
2528 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002529 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002530 if (rdev->badblocks.size) {
2531 md_super_write(mddev, rdev,
2532 rdev->badblocks.sector,
2533 rdev->badblocks.size << 9,
2534 rdev->bb_page);
2535 rdev->badblocks.size = 0;
2536 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002537
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002538 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002539 pr_debug("md: %s (skipping faulty)\n",
2540 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002541 else
2542 pr_debug("(skipping incremental s/r ");
2543
NeilBrown7bfa19f2005-06-21 17:17:28 -07002544 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /* only need to write one superblock... */
2546 break;
2547 }
NeilBrowna9701a32005-11-08 21:39:34 -08002548 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002549 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002550
NeilBrowna9701a32005-11-08 21:39:34 -08002551 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002552 if (mddev->in_sync != sync_req ||
2553 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002554 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002555 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002556 goto repeat;
2557 }
NeilBrown850b2b422006-10-03 01:15:46 -07002558 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002559 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002560 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002561 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2562 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002563
NeilBrowndafb20f2012-03-19 12:46:39 +11002564 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002565 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2566 clear_bit(Blocked, &rdev->flags);
2567
2568 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002569 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002570 clear_bit(BlockedBadBlocks, &rdev->flags);
2571 wake_up(&rdev->blocked_wait);
2572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
Andre Noll7f6ce762008-03-23 18:34:54 +01002575/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002576 * We want to accept with case. For this we use cmd_match.
2577 */
2578static int cmd_match(const char *cmd, const char *str)
2579{
2580 /* See if cmd, written into a sysfs file, matches
2581 * str. They must either be the same, or cmd can
2582 * have a trailing newline
2583 */
2584 while (*cmd && *str && *cmd == *str) {
2585 cmd++;
2586 str++;
2587 }
2588 if (*cmd == '\n')
2589 cmd++;
2590 if (*str || *cmd)
2591 return 0;
2592 return 1;
2593}
2594
NeilBrown86e6ffd2005-11-08 21:39:24 -08002595struct rdev_sysfs_entry {
2596 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002597 ssize_t (*show)(struct md_rdev *, char *);
2598 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002599};
2600
2601static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002602state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002603{
2604 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002605 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002606
NeilBrownde393cd2011-07-28 11:31:48 +10002607 if (test_bit(Faulty, &rdev->flags) ||
2608 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002609 len+= sprintf(page+len, "%sfaulty",sep);
2610 sep = ",";
2611 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002612 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002613 len += sprintf(page+len, "%sin_sync",sep);
2614 sep = ",";
2615 }
NeilBrownf6556752006-06-26 00:28:01 -07002616 if (test_bit(WriteMostly, &rdev->flags)) {
2617 len += sprintf(page+len, "%swrite_mostly",sep);
2618 sep = ",";
2619 }
NeilBrownde393cd2011-07-28 11:31:48 +10002620 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002621 (rdev->badblocks.unacked_exist
2622 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002623 len += sprintf(page+len, "%sblocked", sep);
2624 sep = ",";
2625 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002626 if (!test_bit(Faulty, &rdev->flags) &&
2627 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002628 len += sprintf(page+len, "%sspare", sep);
2629 sep = ",";
2630 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002631 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2632 len += sprintf(page+len, "%swrite_error", sep);
2633 sep = ",";
2634 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002635 if (test_bit(WantReplacement, &rdev->flags)) {
2636 len += sprintf(page+len, "%swant_replacement", sep);
2637 sep = ",";
2638 }
2639 if (test_bit(Replacement, &rdev->flags)) {
2640 len += sprintf(page+len, "%sreplacement", sep);
2641 sep = ",";
2642 }
2643
NeilBrown86e6ffd2005-11-08 21:39:24 -08002644 return len+sprintf(page+len, "\n");
2645}
2646
NeilBrown45dc2de2006-06-26 00:27:58 -07002647static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002648state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002649{
2650 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002651 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002652 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002653 * writemostly - sets write_mostly
2654 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002655 * blocked - sets the Blocked flags
2656 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002657 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002658 * write_error - sets WriteErrorSeen
2659 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002660 */
2661 int err = -EINVAL;
2662 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2663 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002664 if (test_bit(Faulty, &rdev->flags))
2665 err = 0;
2666 else
2667 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002668 } else if (cmd_match(buf, "remove")) {
2669 if (rdev->raid_disk >= 0)
2670 err = -EBUSY;
2671 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002672 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002673 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002674 if (mddev->pers)
2675 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002676 md_new_event(mddev);
2677 err = 0;
2678 }
NeilBrownf6556752006-06-26 00:28:01 -07002679 } else if (cmd_match(buf, "writemostly")) {
2680 set_bit(WriteMostly, &rdev->flags);
2681 err = 0;
2682 } else if (cmd_match(buf, "-writemostly")) {
2683 clear_bit(WriteMostly, &rdev->flags);
2684 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002685 } else if (cmd_match(buf, "blocked")) {
2686 set_bit(Blocked, &rdev->flags);
2687 err = 0;
2688 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002689 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002690 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002691 /* metadata handler doesn't understand badblocks,
2692 * so we need to fail the device
2693 */
2694 md_error(rdev->mddev, rdev);
2695 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002696 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002697 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002698 wake_up(&rdev->blocked_wait);
2699 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2700 md_wakeup_thread(rdev->mddev->thread);
2701
2702 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002703 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2704 set_bit(In_sync, &rdev->flags);
2705 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002706 } else if (cmd_match(buf, "write_error")) {
2707 set_bit(WriteErrorSeen, &rdev->flags);
2708 err = 0;
2709 } else if (cmd_match(buf, "-write_error")) {
2710 clear_bit(WriteErrorSeen, &rdev->flags);
2711 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002712 } else if (cmd_match(buf, "want_replacement")) {
2713 /* Any non-spare device that is not a replacement can
2714 * become want_replacement at any time, but we then need to
2715 * check if recovery is needed.
2716 */
2717 if (rdev->raid_disk >= 0 &&
2718 !test_bit(Replacement, &rdev->flags))
2719 set_bit(WantReplacement, &rdev->flags);
2720 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2721 md_wakeup_thread(rdev->mddev->thread);
2722 err = 0;
2723 } else if (cmd_match(buf, "-want_replacement")) {
2724 /* Clearing 'want_replacement' is always allowed.
2725 * Once replacements starts it is too late though.
2726 */
2727 err = 0;
2728 clear_bit(WantReplacement, &rdev->flags);
2729 } else if (cmd_match(buf, "replacement")) {
2730 /* Can only set a device as a replacement when array has not
2731 * yet been started. Once running, replacement is automatic
2732 * from spares, or by assigning 'slot'.
2733 */
2734 if (rdev->mddev->pers)
2735 err = -EBUSY;
2736 else {
2737 set_bit(Replacement, &rdev->flags);
2738 err = 0;
2739 }
2740 } else if (cmd_match(buf, "-replacement")) {
2741 /* Similarly, can only clear Replacement before start */
2742 if (rdev->mddev->pers)
2743 err = -EBUSY;
2744 else {
2745 clear_bit(Replacement, &rdev->flags);
2746 err = 0;
2747 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002748 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002749 if (!err)
2750 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002751 return err ? err : len;
2752}
NeilBrown80ca3a42006-07-10 04:44:18 -07002753static struct rdev_sysfs_entry rdev_state =
2754__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002755
2756static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002757errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002758{
2759 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2760}
2761
2762static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002763errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002764{
2765 char *e;
2766 unsigned long n = simple_strtoul(buf, &e, 10);
2767 if (*buf && (*e == 0 || *e == '\n')) {
2768 atomic_set(&rdev->corrected_errors, n);
2769 return len;
2770 }
2771 return -EINVAL;
2772}
2773static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002774__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002775
NeilBrown014236d2006-01-06 00:20:55 -08002776static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002777slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002778{
2779 if (rdev->raid_disk < 0)
2780 return sprintf(page, "none\n");
2781 else
2782 return sprintf(page, "%d\n", rdev->raid_disk);
2783}
2784
2785static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002786slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002787{
2788 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002789 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002790 int slot = simple_strtoul(buf, &e, 10);
2791 if (strncmp(buf, "none", 4)==0)
2792 slot = -1;
2793 else if (e==buf || (*e && *e!= '\n'))
2794 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002795 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002796 /* Setting 'slot' on an active array requires also
2797 * updating the 'rd%d' link, and communicating
2798 * with the personality with ->hot_*_disk.
2799 * For now we only support removing
2800 * failed/spare devices. This normally happens automatically,
2801 * but not when the metadata is externally managed.
2802 */
NeilBrownc303da62008-02-06 01:39:51 -08002803 if (rdev->raid_disk == -1)
2804 return -EEXIST;
2805 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002806 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002807 return -EINVAL;
2808 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002809 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002810 if (err)
2811 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002812 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002813 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002814 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2815 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002816 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002818 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002819 */
2820
2821 if (rdev->raid_disk != -1)
2822 return -EBUSY;
2823
NeilBrownc6751b22011-02-02 11:57:13 +11002824 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2825 return -EBUSY;
2826
Neil Brown6c2fce22008-06-28 08:31:31 +10002827 if (rdev->mddev->pers->hot_add_disk == NULL)
2828 return -EINVAL;
2829
NeilBrownba1b41b2011-01-14 09:14:34 +11002830 if (slot >= rdev->mddev->raid_disks &&
2831 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2832 return -ENOSPC;
2833
Neil Brown6c2fce22008-06-28 08:31:31 +10002834 rdev->raid_disk = slot;
2835 if (test_bit(In_sync, &rdev->flags))
2836 rdev->saved_raid_disk = slot;
2837 else
2838 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002839 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002840 err = rdev->mddev->pers->
2841 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002842 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002843 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002844 return err;
Neil Brown52664732008-06-28 08:31:44 +10002845 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002846 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002847 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002848 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002849 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002850 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002851 if (slot >= rdev->mddev->raid_disks &&
2852 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002853 return -ENOSPC;
2854 rdev->raid_disk = slot;
2855 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002856 clear_bit(Faulty, &rdev->flags);
2857 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002858 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002859 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002860 }
NeilBrown014236d2006-01-06 00:20:55 -08002861 return len;
2862}
2863
2864
2865static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002866__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002867
NeilBrown93c8cad2006-01-06 00:20:56 -08002868static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002869offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002870{
Andrew Morton6961ece2006-01-06 00:20:59 -08002871 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002872}
2873
2874static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002875offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002876{
NeilBrownc6563a82012-05-21 09:27:00 +10002877 unsigned long long offset;
2878 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002879 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002880 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002881 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002882 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002883 /* Must set offset before size, so overlap checks
2884 * can be sane */
2885 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002886 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002887 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002888 return len;
2889}
2890
2891static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002892__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002893
NeilBrownc6563a82012-05-21 09:27:00 +10002894static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2895{
2896 return sprintf(page, "%llu\n",
2897 (unsigned long long)rdev->new_data_offset);
2898}
2899
2900static ssize_t new_offset_store(struct md_rdev *rdev,
2901 const char *buf, size_t len)
2902{
2903 unsigned long long new_offset;
2904 struct mddev *mddev = rdev->mddev;
2905
2906 if (strict_strtoull(buf, 10, &new_offset) < 0)
2907 return -EINVAL;
2908
2909 if (mddev->sync_thread)
2910 return -EBUSY;
2911 if (new_offset == rdev->data_offset)
2912 /* reset is always permitted */
2913 ;
2914 else if (new_offset > rdev->data_offset) {
2915 /* must not push array size beyond rdev_sectors */
2916 if (new_offset - rdev->data_offset
2917 + mddev->dev_sectors > rdev->sectors)
2918 return -E2BIG;
2919 }
2920 /* Metadata worries about other space details. */
2921
2922 /* decreasing the offset is inconsistent with a backwards
2923 * reshape.
2924 */
2925 if (new_offset < rdev->data_offset &&
2926 mddev->reshape_backwards)
2927 return -EINVAL;
2928 /* Increasing offset is inconsistent with forwards
2929 * reshape. reshape_direction should be set to
2930 * 'backwards' first.
2931 */
2932 if (new_offset > rdev->data_offset &&
2933 !mddev->reshape_backwards)
2934 return -EINVAL;
2935
2936 if (mddev->pers && mddev->persistent &&
2937 !super_types[mddev->major_version]
2938 .allow_new_offset(rdev, new_offset))
2939 return -E2BIG;
2940 rdev->new_data_offset = new_offset;
2941 if (new_offset > rdev->data_offset)
2942 mddev->reshape_backwards = 1;
2943 else if (new_offset < rdev->data_offset)
2944 mddev->reshape_backwards = 0;
2945
2946 return len;
2947}
2948static struct rdev_sysfs_entry rdev_new_offset =
2949__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2950
NeilBrown83303b62006-01-06 00:21:06 -08002951static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002952rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002953{
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002955}
2956
NeilBrownc5d79ad2008-02-06 01:39:54 -08002957static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2958{
2959 /* check if two start/length pairs overlap */
2960 if (s1+l1 <= s2)
2961 return 0;
2962 if (s2+l2 <= s1)
2963 return 0;
2964 return 1;
2965}
2966
Dan Williamsb522adc2009-03-31 15:00:31 +11002967static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2968{
2969 unsigned long long blocks;
2970 sector_t new;
2971
2972 if (strict_strtoull(buf, 10, &blocks) < 0)
2973 return -EINVAL;
2974
2975 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2976 return -EINVAL; /* sector conversion overflow */
2977
2978 new = blocks * 2;
2979 if (new != blocks * 2)
2980 return -EINVAL; /* unsigned long long to sector_t overflow */
2981
2982 *sectors = new;
2983 return 0;
2984}
2985
NeilBrown83303b62006-01-06 00:21:06 -08002986static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002987rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002988{
NeilBrownfd01b882011-10-11 16:47:53 +11002989 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002990 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002991 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002992
Dan Williamsb522adc2009-03-31 15:00:31 +11002993 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002994 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002995 if (rdev->data_offset != rdev->new_data_offset)
2996 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002997 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002998 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002999 sectors = super_types[my_mddev->major_version].
3000 rdev_size_change(rdev, sectors);
3001 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003002 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003003 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003004 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003005 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003006 if (!my_mddev->pers->resize)
3007 /* Cannot change size for RAID0 or Linear etc */
3008 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003009 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003010 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003011 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003012
Andre Nolldd8ac332009-03-31 14:33:13 +11003013 rdev->sectors = sectors;
3014 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003015 /* need to check that all other rdevs with the same ->bdev
3016 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003017 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018 * we have to change it back, we will have the lock again.
3019 */
NeilBrownfd01b882011-10-11 16:47:53 +11003020 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003021 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003022 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003023
NeilBrown27c529b2008-03-04 14:29:33 -08003024 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003025 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003026 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003027
3028 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003029 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003030 if (rdev->bdev == rdev2->bdev &&
3031 rdev != rdev2 &&
3032 overlaps(rdev->data_offset, rdev->sectors,
3033 rdev2->data_offset,
3034 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003035 overlap = 1;
3036 break;
3037 }
3038 mddev_unlock(mddev);
3039 if (overlap) {
3040 mddev_put(mddev);
3041 break;
3042 }
3043 }
NeilBrown27c529b2008-03-04 14:29:33 -08003044 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003045 if (overlap) {
3046 /* Someone else could have slipped in a size
3047 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003048 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003049 * safe, and trust userspace not to race with
3050 * itself
3051 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003052 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003053 return -EBUSY;
3054 }
3055 }
NeilBrown83303b62006-01-06 00:21:06 -08003056 return len;
3057}
3058
3059static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003060__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003061
Dan Williams06e3c812009-12-12 21:17:12 -07003062
NeilBrown3cb03002011-10-11 16:45:26 +11003063static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003064{
3065 unsigned long long recovery_start = rdev->recovery_offset;
3066
3067 if (test_bit(In_sync, &rdev->flags) ||
3068 recovery_start == MaxSector)
3069 return sprintf(page, "none\n");
3070
3071 return sprintf(page, "%llu\n", recovery_start);
3072}
3073
NeilBrown3cb03002011-10-11 16:45:26 +11003074static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003075{
3076 unsigned long long recovery_start;
3077
3078 if (cmd_match(buf, "none"))
3079 recovery_start = MaxSector;
3080 else if (strict_strtoull(buf, 10, &recovery_start))
3081 return -EINVAL;
3082
3083 if (rdev->mddev->pers &&
3084 rdev->raid_disk >= 0)
3085 return -EBUSY;
3086
3087 rdev->recovery_offset = recovery_start;
3088 if (recovery_start == MaxSector)
3089 set_bit(In_sync, &rdev->flags);
3090 else
3091 clear_bit(In_sync, &rdev->flags);
3092 return len;
3093}
3094
3095static struct rdev_sysfs_entry rdev_recovery_start =
3096__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3097
NeilBrown16c791a2011-07-28 11:31:47 +10003098
3099static ssize_t
3100badblocks_show(struct badblocks *bb, char *page, int unack);
3101static ssize_t
3102badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3103
NeilBrown3cb03002011-10-11 16:45:26 +11003104static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003105{
3106 return badblocks_show(&rdev->badblocks, page, 0);
3107}
NeilBrown3cb03002011-10-11 16:45:26 +11003108static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003109{
NeilBrownde393cd2011-07-28 11:31:48 +10003110 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3111 /* Maybe that ack was all we needed */
3112 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3113 wake_up(&rdev->blocked_wait);
3114 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003115}
3116static struct rdev_sysfs_entry rdev_bad_blocks =
3117__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3118
3119
NeilBrown3cb03002011-10-11 16:45:26 +11003120static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003121{
3122 return badblocks_show(&rdev->badblocks, page, 1);
3123}
NeilBrown3cb03002011-10-11 16:45:26 +11003124static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003125{
3126 return badblocks_store(&rdev->badblocks, page, len, 1);
3127}
3128static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3129__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3130
NeilBrown86e6ffd2005-11-08 21:39:24 -08003131static struct attribute *rdev_default_attrs[] = {
3132 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003133 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003134 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003135 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003136 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003137 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003138 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003139 &rdev_bad_blocks.attr,
3140 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003141 NULL,
3142};
3143static ssize_t
3144rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3145{
3146 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003147 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003148 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003149 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003150
3151 if (!entry->show)
3152 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003153
3154 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3155 if (!rv) {
3156 if (rdev->mddev == NULL)
3157 rv = -EBUSY;
3158 else
3159 rv = entry->show(rdev, page);
3160 mddev_unlock(mddev);
3161 }
3162 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003163}
3164
3165static ssize_t
3166rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3167 const char *page, size_t length)
3168{
3169 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003170 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003171 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003172 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003173
3174 if (!entry->store)
3175 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003176 if (!capable(CAP_SYS_ADMIN))
3177 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003178 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003179 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003180 if (rdev->mddev == NULL)
3181 rv = -EBUSY;
3182 else
3183 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003184 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003185 }
3186 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003187}
3188
3189static void rdev_free(struct kobject *ko)
3190{
NeilBrown3cb03002011-10-11 16:45:26 +11003191 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003192 kfree(rdev);
3193}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003194static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003195 .show = rdev_attr_show,
3196 .store = rdev_attr_store,
3197};
3198static struct kobj_type rdev_ktype = {
3199 .release = rdev_free,
3200 .sysfs_ops = &rdev_sysfs_ops,
3201 .default_attrs = rdev_default_attrs,
3202};
3203
NeilBrown3cb03002011-10-11 16:45:26 +11003204int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003205{
3206 rdev->desc_nr = -1;
3207 rdev->saved_raid_disk = -1;
3208 rdev->raid_disk = -1;
3209 rdev->flags = 0;
3210 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003211 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003212 rdev->sb_events = 0;
3213 rdev->last_read_error.tv_sec = 0;
3214 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003215 rdev->sb_loaded = 0;
3216 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003217 atomic_set(&rdev->nr_pending, 0);
3218 atomic_set(&rdev->read_errors, 0);
3219 atomic_set(&rdev->corrected_errors, 0);
3220
3221 INIT_LIST_HEAD(&rdev->same_set);
3222 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003223
3224 /* Add space to store bad block list.
3225 * This reserves the space even on arrays where it cannot
3226 * be used - I wonder if that matters
3227 */
3228 rdev->badblocks.count = 0;
3229 rdev->badblocks.shift = 0;
3230 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3231 seqlock_init(&rdev->badblocks.lock);
3232 if (rdev->badblocks.page == NULL)
3233 return -ENOMEM;
3234
3235 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003236}
3237EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238/*
3239 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3240 *
3241 * mark the device faulty if:
3242 *
3243 * - the device is nonexistent (zero size)
3244 * - the device has no valid superblock
3245 *
3246 * a faulty rdev _never_ has rdev->sb set.
3247 */
NeilBrown3cb03002011-10-11 16:45:26 +11003248static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
3250 char b[BDEVNAME_SIZE];
3251 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003252 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 sector_t size;
3254
NeilBrown9ffae0c2006-01-06 00:20:32 -08003255 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (!rdev) {
3257 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3258 return ERR_PTR(-ENOMEM);
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
NeilBrown2230dfe2011-07-28 11:31:46 +10003261 err = md_rdev_init(rdev);
3262 if (err)
3263 goto abort_free;
3264 err = alloc_disk_sb(rdev);
3265 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 goto abort_free;
3267
NeilBrownc5d79ad2008-02-06 01:39:54 -08003268 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (err)
3270 goto abort_free;
3271
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003272 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003273
Mike Snitzer77304d22010-11-08 14:39:12 +01003274 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (!size) {
3276 printk(KERN_WARNING
3277 "md: %s has zero or unknown size, marking faulty!\n",
3278 bdevname(rdev->bdev,b));
3279 err = -EINVAL;
3280 goto abort_free;
3281 }
3282
3283 if (super_format >= 0) {
3284 err = super_types[super_format].
3285 load_super(rdev, NULL, super_minor);
3286 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003287 printk(KERN_WARNING
3288 "md: %s does not have a valid v%d.%d "
3289 "superblock, not importing!\n",
3290 bdevname(rdev->bdev,b),
3291 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 goto abort_free;
3293 }
3294 if (err < 0) {
3295 printk(KERN_WARNING
3296 "md: could not read %s's sb, not importing!\n",
3297 bdevname(rdev->bdev,b));
3298 goto abort_free;
3299 }
3300 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003301 if (super_format == -1)
3302 /* hot-add for 0.90, or non-persistent: so no badblocks */
3303 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 return rdev;
3306
3307abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003308 if (rdev->bdev)
3309 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003310 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 kfree(rdev);
3312 return ERR_PTR(err);
3313}
3314
3315/*
3316 * Check a full RAID array for plausibility
3317 */
3318
3319
NeilBrownfd01b882011-10-11 16:47:53 +11003320static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
3322 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003323 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 char b[BDEVNAME_SIZE];
3325
3326 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003327 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 switch (super_types[mddev->major_version].
3329 load_super(rdev, freshest, mddev->minor_version)) {
3330 case 1:
3331 freshest = rdev;
3332 break;
3333 case 0:
3334 break;
3335 default:
3336 printk( KERN_ERR \
3337 "md: fatal superblock inconsistency in %s"
3338 " -- removing from array\n",
3339 bdevname(rdev->bdev,b));
3340 kick_rdev_from_array(rdev);
3341 }
3342
3343
3344 super_types[mddev->major_version].
3345 validate_super(mddev, freshest);
3346
3347 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003348 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003349 if (mddev->max_disks &&
3350 (rdev->desc_nr >= mddev->max_disks ||
3351 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003352 printk(KERN_WARNING
3353 "md: %s: %s: only %d devices permitted\n",
3354 mdname(mddev), bdevname(rdev->bdev, b),
3355 mddev->max_disks);
3356 kick_rdev_from_array(rdev);
3357 continue;
3358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (rdev != freshest)
3360 if (super_types[mddev->major_version].
3361 validate_super(mddev, rdev)) {
3362 printk(KERN_WARNING "md: kicking non-fresh %s"
3363 " from array!\n",
3364 bdevname(rdev->bdev,b));
3365 kick_rdev_from_array(rdev);
3366 continue;
3367 }
3368 if (mddev->level == LEVEL_MULTIPATH) {
3369 rdev->desc_nr = i++;
3370 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003371 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003372 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003373 rdev->raid_disk = -1;
3374 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
NeilBrown72e02072009-12-14 12:49:55 +11003379/* Read a fixed-point number.
3380 * Numbers in sysfs attributes should be in "standard" units where
3381 * possible, so time should be in seconds.
3382 * However we internally use a a much smaller unit such as
3383 * milliseconds or jiffies.
3384 * This function takes a decimal number with a possible fractional
3385 * component, and produces an integer which is the result of
3386 * multiplying that number by 10^'scale'.
3387 * all without any floating-point arithmetic.
3388 */
3389int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3390{
3391 unsigned long result = 0;
3392 long decimals = -1;
3393 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3394 if (*cp == '.')
3395 decimals = 0;
3396 else if (decimals < scale) {
3397 unsigned int value;
3398 value = *cp - '0';
3399 result = result * 10 + value;
3400 if (decimals >= 0)
3401 decimals++;
3402 }
3403 cp++;
3404 }
3405 if (*cp == '\n')
3406 cp++;
3407 if (*cp)
3408 return -EINVAL;
3409 if (decimals < 0)
3410 decimals = 0;
3411 while (decimals < scale) {
3412 result *= 10;
3413 decimals ++;
3414 }
3415 *res = result;
3416 return 0;
3417}
3418
3419
NeilBrown19052c02008-08-05 15:54:13 +10003420static void md_safemode_timeout(unsigned long data);
3421
NeilBrowneae17012005-11-08 21:39:23 -08003422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003423safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003424{
3425 int msec = (mddev->safemode_delay*1000)/HZ;
3426 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3427}
3428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003429safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003430{
NeilBrown16f17b32006-06-26 00:27:37 -07003431 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003432
NeilBrown72e02072009-12-14 12:49:55 +11003433 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003434 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003435 if (msec == 0)
3436 mddev->safemode_delay = 0;
3437 else {
NeilBrown19052c02008-08-05 15:54:13 +10003438 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003439 mddev->safemode_delay = (msec*HZ)/1000;
3440 if (mddev->safemode_delay == 0)
3441 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003442 if (mddev->safemode_delay < old_delay)
3443 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003444 }
3445 return len;
3446}
3447static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003448__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003449
3450static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003451level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003452{
NeilBrown84fc4b52011-10-11 16:49:58 +11003453 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003454 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003455 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003456 else if (mddev->clevel[0])
3457 return sprintf(page, "%s\n", mddev->clevel);
3458 else if (mddev->level != LEVEL_NONE)
3459 return sprintf(page, "%d\n", mddev->level);
3460 else
3461 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003462}
3463
NeilBrownd9d166c2006-01-06 00:20:51 -08003464static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003465level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003466{
Dan Williamsf2859af2010-05-02 10:04:16 -07003467 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003468 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003469 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003470 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003471 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003472 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003473
3474 if (mddev->pers == NULL) {
3475 if (len == 0)
3476 return 0;
3477 if (len >= sizeof(mddev->clevel))
3478 return -ENOSPC;
3479 strncpy(mddev->clevel, buf, len);
3480 if (mddev->clevel[len-1] == '\n')
3481 len--;
3482 mddev->clevel[len] = 0;
3483 mddev->level = LEVEL_NONE;
3484 return rv;
3485 }
3486
3487 /* request to change the personality. Need to ensure:
3488 * - array is not engaged in resync/recovery/reshape
3489 * - old personality can be suspended
3490 * - new personality will access other array.
3491 */
3492
NeilBrownbb4f1e92010-08-08 21:18:03 +10003493 if (mddev->sync_thread ||
3494 mddev->reshape_position != MaxSector ||
3495 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003496 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003497
3498 if (!mddev->pers->quiesce) {
3499 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3500 mdname(mddev), mddev->pers->name);
3501 return -EINVAL;
3502 }
3503
3504 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003506 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003507 strncpy(clevel, buf, len);
3508 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003509 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003510 clevel[len] = 0;
3511 if (strict_strtol(clevel, 10, &level))
3512 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003513
Dan Williamsf2859af2010-05-02 10:04:16 -07003514 if (request_module("md-%s", clevel) != 0)
3515 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003516 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003517 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003518 if (!pers || !try_module_get(pers->owner)) {
3519 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003520 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003521 return -EINVAL;
3522 }
3523 spin_unlock(&pers_lock);
3524
3525 if (pers == mddev->pers) {
3526 /* Nothing to do! */
3527 module_put(pers->owner);
3528 return rv;
3529 }
3530 if (!pers->takeover) {
3531 module_put(pers->owner);
3532 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003533 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003534 return -EINVAL;
3535 }
3536
NeilBrowndafb20f2012-03-19 12:46:39 +11003537 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003538 rdev->new_raid_disk = rdev->raid_disk;
3539
NeilBrown245f46c2009-03-31 14:39:39 +11003540 /* ->takeover must set new_* and/or delta_disks
3541 * if it succeeds, and may set them when it fails.
3542 */
3543 priv = pers->takeover(mddev);
3544 if (IS_ERR(priv)) {
3545 mddev->new_level = mddev->level;
3546 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003547 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003548 mddev->raid_disks -= mddev->delta_disks;
3549 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003550 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003551 module_put(pers->owner);
3552 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003553 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003554 return PTR_ERR(priv);
3555 }
3556
3557 /* Looks like we have a winner */
3558 mddev_suspend(mddev);
3559 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003560
3561 if (mddev->pers->sync_request == NULL &&
3562 pers->sync_request != NULL) {
3563 /* need to add the md_redundancy_group */
3564 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3565 printk(KERN_WARNING
3566 "md: cannot register extra attributes for %s\n",
3567 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003568 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003569 }
3570 if (mddev->pers->sync_request != NULL &&
3571 pers->sync_request == NULL) {
3572 /* need to remove the md_redundancy_group */
3573 if (mddev->to_remove == NULL)
3574 mddev->to_remove = &md_redundancy_group;
3575 }
3576
Trela Maciej54071b32010-03-08 16:02:42 +11003577 if (mddev->pers->sync_request == NULL &&
3578 mddev->external) {
3579 /* We are converting from a no-redundancy array
3580 * to a redundancy array and metadata is managed
3581 * externally so we need to be sure that writes
3582 * won't block due to a need to transition
3583 * clean->dirty
3584 * until external management is started.
3585 */
3586 mddev->in_sync = 0;
3587 mddev->safemode_delay = 0;
3588 mddev->safemode = 0;
3589 }
3590
NeilBrowndafb20f2012-03-19 12:46:39 +11003591 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003592 if (rdev->raid_disk < 0)
3593 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003594 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003595 rdev->new_raid_disk = -1;
3596 if (rdev->new_raid_disk == rdev->raid_disk)
3597 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003598 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003599 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003600 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003601 if (rdev->raid_disk < 0)
3602 continue;
3603 if (rdev->new_raid_disk == rdev->raid_disk)
3604 continue;
3605 rdev->raid_disk = rdev->new_raid_disk;
3606 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003607 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003608 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003609 if (sysfs_link_rdev(mddev, rdev))
3610 printk(KERN_WARNING "md: cannot register rd%d"
3611 " for %s after level change\n",
3612 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003613 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003614 }
3615
3616 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003617 mddev->pers = pers;
3618 mddev->private = priv;
3619 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3620 mddev->level = mddev->new_level;
3621 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003622 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003623 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003624 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003625 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003626 if (mddev->pers->sync_request == NULL) {
3627 /* this is now an array without redundancy, so
3628 * it must always be in_sync
3629 */
3630 mddev->in_sync = 1;
3631 del_timer_sync(&mddev->safemode_timer);
3632 }
NeilBrown245f46c2009-03-31 14:39:39 +11003633 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003634 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003635 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003636 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003637 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003638 return rv;
3639}
3640
3641static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003642__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003643
NeilBrownd4dbd022006-06-26 00:27:59 -07003644
3645static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003646layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003647{
3648 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003649 if (mddev->reshape_position != MaxSector &&
3650 mddev->layout != mddev->new_layout)
3651 return sprintf(page, "%d (%d)\n",
3652 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003653 return sprintf(page, "%d\n", mddev->layout);
3654}
3655
3656static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003657layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003658{
3659 char *e;
3660 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003661
3662 if (!*buf || (*e && *e != '\n'))
3663 return -EINVAL;
3664
NeilBrownb3546032009-03-31 14:56:41 +11003665 if (mddev->pers) {
3666 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003667 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003668 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003669 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003670 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003671 if (err) {
3672 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003673 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003674 }
NeilBrownb3546032009-03-31 14:56:41 +11003675 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003676 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003677 if (mddev->reshape_position == MaxSector)
3678 mddev->layout = n;
3679 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003680 return len;
3681}
3682static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003683__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003684
3685
NeilBrowneae17012005-11-08 21:39:23 -08003686static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003687raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003688{
NeilBrownbb636542005-11-08 21:39:45 -08003689 if (mddev->raid_disks == 0)
3690 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003691 if (mddev->reshape_position != MaxSector &&
3692 mddev->delta_disks != 0)
3693 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3694 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003695 return sprintf(page, "%d\n", mddev->raid_disks);
3696}
3697
NeilBrownfd01b882011-10-11 16:47:53 +11003698static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003699
3700static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003701raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003702{
NeilBrownda943b992006-01-06 00:20:54 -08003703 char *e;
3704 int rv = 0;
3705 unsigned long n = simple_strtoul(buf, &e, 10);
3706
3707 if (!*buf || (*e && *e != '\n'))
3708 return -EINVAL;
3709
3710 if (mddev->pers)
3711 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003712 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003713 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003714 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003715
3716 rdev_for_each(rdev, mddev) {
3717 if (olddisks < n &&
3718 rdev->data_offset < rdev->new_data_offset)
3719 return -EINVAL;
3720 if (olddisks > n &&
3721 rdev->data_offset > rdev->new_data_offset)
3722 return -EINVAL;
3723 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003724 mddev->delta_disks = n - olddisks;
3725 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003726 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003727 } else
NeilBrownda943b992006-01-06 00:20:54 -08003728 mddev->raid_disks = n;
3729 return rv ? rv : len;
3730}
3731static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003732__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003733
NeilBrown24dd4692005-11-08 21:39:26 -08003734static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003735chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003736{
NeilBrown08a02ec2007-05-09 02:35:38 -07003737 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003738 mddev->chunk_sectors != mddev->new_chunk_sectors)
3739 return sprintf(page, "%d (%d)\n",
3740 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003741 mddev->chunk_sectors << 9);
3742 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003743}
3744
3745static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003746chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003747{
NeilBrown3b343802006-01-06 00:20:47 -08003748 char *e;
3749 unsigned long n = simple_strtoul(buf, &e, 10);
3750
NeilBrown3b343802006-01-06 00:20:47 -08003751 if (!*buf || (*e && *e != '\n'))
3752 return -EINVAL;
3753
NeilBrownb3546032009-03-31 14:56:41 +11003754 if (mddev->pers) {
3755 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003756 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003757 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003758 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003759 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003760 if (err) {
3761 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003762 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003763 }
NeilBrownb3546032009-03-31 14:56:41 +11003764 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003765 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003766 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003767 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003768 }
NeilBrown3b343802006-01-06 00:20:47 -08003769 return len;
3770}
3771static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003772__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003773
NeilBrowna94213b2006-06-26 00:28:00 -07003774static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003775resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003776{
NeilBrownd1a7c502009-03-31 15:24:32 +11003777 if (mddev->recovery_cp == MaxSector)
3778 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003779 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3780}
3781
3782static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003783resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003784{
NeilBrowna94213b2006-06-26 00:28:00 -07003785 char *e;
3786 unsigned long long n = simple_strtoull(buf, &e, 10);
3787
NeilBrownb0986362011-05-11 15:52:21 +10003788 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003789 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003790 if (cmd_match(buf, "none"))
3791 n = MaxSector;
3792 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003793 return -EINVAL;
3794
3795 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003796 if (mddev->pers)
3797 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003798 return len;
3799}
3800static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003801__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003802
NeilBrown9e653b62006-06-26 00:27:58 -07003803/*
3804 * The array state can be:
3805 *
3806 * clear
3807 * No devices, no size, no level
3808 * Equivalent to STOP_ARRAY ioctl
3809 * inactive
3810 * May have some settings, but array is not active
3811 * all IO results in error
3812 * When written, doesn't tear down array, but just stops it
3813 * suspended (not supported yet)
3814 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003815 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003816 * readonly
3817 * no resync can happen. no superblocks get written.
3818 * write requests fail
3819 * read-auto
3820 * like readonly, but behaves like 'clean' on a write request.
3821 *
3822 * clean - no pending writes, but otherwise active.
3823 * When written to inactive array, starts without resync
3824 * If a write request arrives then
3825 * if metadata is known, mark 'dirty' and switch to 'active'.
3826 * if not known, block and switch to write-pending
3827 * If written to an active array that has pending writes, then fails.
3828 * active
3829 * fully active: IO and resync can be happening.
3830 * When written to inactive array, starts with resync
3831 *
3832 * write-pending
3833 * clean, but writes are blocked waiting for 'active' to be written.
3834 *
3835 * active-idle
3836 * like active, but no writes have been seen for a while (100msec).
3837 *
3838 */
3839enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3840 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003841static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003842 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3843 "write-pending", "active-idle", NULL };
3844
3845static int match_word(const char *word, char **list)
3846{
3847 int n;
3848 for (n=0; list[n]; n++)
3849 if (cmd_match(word, list[n]))
3850 break;
3851 return n;
3852}
3853
3854static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003855array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003856{
3857 enum array_state st = inactive;
3858
3859 if (mddev->pers)
3860 switch(mddev->ro) {
3861 case 1:
3862 st = readonly;
3863 break;
3864 case 2:
3865 st = read_auto;
3866 break;
3867 case 0:
3868 if (mddev->in_sync)
3869 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003870 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003871 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003872 else if (mddev->safemode)
3873 st = active_idle;
3874 else
3875 st = active;
3876 }
3877 else {
3878 if (list_empty(&mddev->disks) &&
3879 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003880 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003881 st = clear;
3882 else
3883 st = inactive;
3884 }
3885 return sprintf(page, "%s\n", array_states[st]);
3886}
3887
NeilBrowna05b7ea2012-07-19 15:59:18 +10003888static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3889static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003890static int do_md_run(struct mddev * mddev);
3891static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003892
3893static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003894array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003895{
3896 int err = -EINVAL;
3897 enum array_state st = match_word(buf, array_states);
3898 switch(st) {
3899 case bad_word:
3900 break;
3901 case clear:
3902 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003903 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003904 break;
3905 case inactive:
3906 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003907 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003908 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003909 else
NeilBrowne6910632008-02-06 01:39:51 -08003910 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003911 break;
3912 case suspended:
3913 break; /* not supported yet */
3914 case readonly:
3915 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003916 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003917 else {
3918 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003919 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003920 err = do_md_run(mddev);
3921 }
3922 break;
3923 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003924 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003925 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003926 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003927 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003928 err = restart_array(mddev);
3929 if (err == 0) {
3930 mddev->ro = 2;
3931 set_disk_ro(mddev->gendisk, 0);
3932 }
NeilBrown9e653b62006-06-26 00:27:58 -07003933 } else {
3934 mddev->ro = 2;
3935 err = do_md_run(mddev);
3936 }
3937 break;
3938 case clean:
3939 if (mddev->pers) {
3940 restart_array(mddev);
3941 spin_lock_irq(&mddev->write_lock);
3942 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003943 if (mddev->in_sync == 0) {
3944 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003945 if (mddev->safemode == 1)
3946 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003947 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003948 }
3949 err = 0;
3950 } else
3951 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003952 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003953 } else
3954 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003955 break;
3956 case active:
3957 if (mddev->pers) {
3958 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003959 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003960 wake_up(&mddev->sb_wait);
3961 err = 0;
3962 } else {
3963 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003964 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003965 err = do_md_run(mddev);
3966 }
3967 break;
3968 case write_pending:
3969 case active_idle:
3970 /* these cannot be set */
3971 break;
3972 }
3973 if (err)
3974 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003975 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003976 if (mddev->hold_active == UNTIL_IOCTL)
3977 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003978 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003979 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003980 }
NeilBrown9e653b62006-06-26 00:27:58 -07003981}
NeilBrown80ca3a42006-07-10 04:44:18 -07003982static struct md_sysfs_entry md_array_state =
3983__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003984
NeilBrown6d7ff7382006-01-06 00:21:16 -08003985static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003986max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003987 return sprintf(page, "%d\n",
3988 atomic_read(&mddev->max_corr_read_errors));
3989}
3990
3991static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003992max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003993{
3994 char *e;
3995 unsigned long n = simple_strtoul(buf, &e, 10);
3996
3997 if (*buf && (*e == 0 || *e == '\n')) {
3998 atomic_set(&mddev->max_corr_read_errors, n);
3999 return len;
4000 }
4001 return -EINVAL;
4002}
4003
4004static struct md_sysfs_entry max_corr_read_errors =
4005__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4006 max_corrected_read_errors_store);
4007
4008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004009null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004010{
4011 return -EINVAL;
4012}
4013
4014static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004015new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004016{
4017 /* buf must be %d:%d\n? giving major and minor numbers */
4018 /* The new device is added to the array.
4019 * If the array has a persistent superblock, we read the
4020 * superblock to initialise info and check validity.
4021 * Otherwise, only checking done is that in bind_rdev_to_array,
4022 * which mainly checks size.
4023 */
4024 char *e;
4025 int major = simple_strtoul(buf, &e, 10);
4026 int minor;
4027 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004028 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004029 int err;
4030
4031 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4032 return -EINVAL;
4033 minor = simple_strtoul(e+1, &e, 10);
4034 if (*e && *e != '\n')
4035 return -EINVAL;
4036 dev = MKDEV(major, minor);
4037 if (major != MAJOR(dev) ||
4038 minor != MINOR(dev))
4039 return -EOVERFLOW;
4040
4041
4042 if (mddev->persistent) {
4043 rdev = md_import_device(dev, mddev->major_version,
4044 mddev->minor_version);
4045 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004046 struct md_rdev *rdev0
4047 = list_entry(mddev->disks.next,
4048 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004049 err = super_types[mddev->major_version]
4050 .load_super(rdev, rdev0, mddev->minor_version);
4051 if (err < 0)
4052 goto out;
4053 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004054 } else if (mddev->external)
4055 rdev = md_import_device(dev, -2, -1);
4056 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004057 rdev = md_import_device(dev, -1, -1);
4058
4059 if (IS_ERR(rdev))
4060 return PTR_ERR(rdev);
4061 err = bind_rdev_to_array(rdev, mddev);
4062 out:
4063 if (err)
4064 export_rdev(rdev);
4065 return err ? err : len;
4066}
4067
4068static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004069__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004070
4071static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004072bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004073{
4074 char *end;
4075 unsigned long chunk, end_chunk;
4076
4077 if (!mddev->bitmap)
4078 goto out;
4079 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4080 while (*buf) {
4081 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4082 if (buf == end) break;
4083 if (*end == '-') { /* range */
4084 buf = end + 1;
4085 end_chunk = simple_strtoul(buf, &end, 0);
4086 if (buf == end) break;
4087 }
4088 if (*end && !isspace(*end)) break;
4089 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004090 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004091 }
4092 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4093out:
4094 return len;
4095}
4096
4097static struct md_sysfs_entry md_bitmap =
4098__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4099
4100static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004101size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004102{
Andre Noll58c0fed2009-03-31 14:33:13 +11004103 return sprintf(page, "%llu\n",
4104 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004105}
4106
NeilBrownfd01b882011-10-11 16:47:53 +11004107static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004108
4109static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004110size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004111{
4112 /* If array is inactive, we can reduce the component size, but
4113 * not increase it (except from 0).
4114 * If array is active, we can try an on-line resize
4115 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004116 sector_t sectors;
4117 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004118
Andre Noll58c0fed2009-03-31 14:33:13 +11004119 if (err < 0)
4120 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004121 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004122 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004123 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004124 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004125 if (mddev->dev_sectors == 0 ||
4126 mddev->dev_sectors > sectors)
4127 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004128 else
4129 err = -ENOSPC;
4130 }
4131 return err ? err : len;
4132}
4133
4134static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004135__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004136
NeilBrown8bb93aa2006-01-06 00:20:50 -08004137
Masanari Iida83f0d772012-10-30 00:18:08 +09004138/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004139 * This is one of
4140 * 'none' for arrays with no metadata (good luck...)
4141 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004142 * or N.M for internally known formats
4143 */
4144static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004145metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146{
4147 if (mddev->persistent)
4148 return sprintf(page, "%d.%d\n",
4149 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004150 else if (mddev->external)
4151 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004152 else
4153 return sprintf(page, "none\n");
4154}
4155
4156static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004157metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004158{
4159 int major, minor;
4160 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004161 /* Changing the details of 'external' metadata is
4162 * always permitted. Otherwise there must be
4163 * no devices attached to the array.
4164 */
4165 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4166 ;
4167 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004168 return -EBUSY;
4169
4170 if (cmd_match(buf, "none")) {
4171 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004172 mddev->external = 0;
4173 mddev->major_version = 0;
4174 mddev->minor_version = 90;
4175 return len;
4176 }
4177 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004178 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004179 if (namelen >= sizeof(mddev->metadata_type))
4180 namelen = sizeof(mddev->metadata_type)-1;
4181 strncpy(mddev->metadata_type, buf+9, namelen);
4182 mddev->metadata_type[namelen] = 0;
4183 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4184 mddev->metadata_type[--namelen] = 0;
4185 mddev->persistent = 0;
4186 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004187 mddev->major_version = 0;
4188 mddev->minor_version = 90;
4189 return len;
4190 }
4191 major = simple_strtoul(buf, &e, 10);
4192 if (e==buf || *e != '.')
4193 return -EINVAL;
4194 buf = e+1;
4195 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004196 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004197 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004198 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004199 return -ENOENT;
4200 mddev->major_version = major;
4201 mddev->minor_version = minor;
4202 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004203 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004204 return len;
4205}
4206
4207static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004208__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004209
NeilBrowna35b0d62006-01-06 00:20:49 -08004210static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004211action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004212{
NeilBrown7eec3142005-11-08 21:39:44 -08004213 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004214 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4215 type = "frozen";
4216 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004217 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004218 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4219 type = "reshape";
4220 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004221 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4222 type = "resync";
4223 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4224 type = "check";
4225 else
4226 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004227 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004228 type = "recover";
4229 }
4230 return sprintf(page, "%s\n", type);
4231}
4232
NeilBrownfd01b882011-10-11 16:47:53 +11004233static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004234
NeilBrown24dd4692005-11-08 21:39:26 -08004235static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004236action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004237{
NeilBrown7eec3142005-11-08 21:39:44 -08004238 if (!mddev->pers || !mddev->pers->sync_request)
4239 return -EINVAL;
4240
NeilBrownb6a9ce62009-05-26 09:41:17 +10004241 if (cmd_match(page, "frozen"))
4242 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4243 else
4244 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4245
4246 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004247 if (mddev->sync_thread) {
4248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004249 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004250 }
NeilBrown03c902e2006-01-06 00:20:46 -08004251 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4252 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004253 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004254 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004256 else if (cmd_match(page, "recover")) {
4257 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4258 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4259 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004260 int err;
4261 if (mddev->pers->start_reshape == NULL)
4262 return -EINVAL;
4263 err = mddev->pers->start_reshape(mddev);
4264 if (err)
4265 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004266 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004267 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004268 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004269 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004270 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004271 return -EINVAL;
4272 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4273 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004274 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004275 if (mddev->ro == 2) {
4276 /* A write to sync_action is enough to justify
4277 * canceling read-auto mode
4278 */
4279 mddev->ro = 0;
4280 md_wakeup_thread(mddev->sync_thread);
4281 }
NeilBrown03c902e2006-01-06 00:20:46 -08004282 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004283 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004284 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004285 return len;
4286}
4287
NeilBrown9d888832005-11-08 21:39:26 -08004288static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004289mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004290{
4291 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004292 (unsigned long long)
4293 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004294}
4295
NeilBrown80ca3a42006-07-10 04:44:18 -07004296static struct md_sysfs_entry md_scan_mode =
4297__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004298
NeilBrown96de1e62005-11-08 21:39:39 -08004299
NeilBrown80ca3a42006-07-10 04:44:18 -07004300static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004301
NeilBrown88202a02006-01-06 00:21:36 -08004302static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004303sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004304{
4305 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4306 mddev->sync_speed_min ? "local": "system");
4307}
4308
4309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004310sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004311{
4312 int min;
4313 char *e;
4314 if (strncmp(buf, "system", 6)==0) {
4315 mddev->sync_speed_min = 0;
4316 return len;
4317 }
4318 min = simple_strtoul(buf, &e, 10);
4319 if (buf == e || (*e && *e != '\n') || min <= 0)
4320 return -EINVAL;
4321 mddev->sync_speed_min = min;
4322 return len;
4323}
4324
4325static struct md_sysfs_entry md_sync_min =
4326__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4327
4328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004329sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004330{
4331 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4332 mddev->sync_speed_max ? "local": "system");
4333}
4334
4335static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004336sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004337{
4338 int max;
4339 char *e;
4340 if (strncmp(buf, "system", 6)==0) {
4341 mddev->sync_speed_max = 0;
4342 return len;
4343 }
4344 max = simple_strtoul(buf, &e, 10);
4345 if (buf == e || (*e && *e != '\n') || max <= 0)
4346 return -EINVAL;
4347 mddev->sync_speed_max = max;
4348 return len;
4349}
4350
4351static struct md_sysfs_entry md_sync_max =
4352__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4353
Iustin Popd7f3d292007-10-16 23:30:54 -07004354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004356{
4357 return sprintf(page, "%d\n", mddev->degraded);
4358}
4359static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004360
4361static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004362sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004363{
4364 return sprintf(page, "%d\n", mddev->parallel_resync);
4365}
4366
4367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004368sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004369{
4370 long n;
4371
4372 if (strict_strtol(buf, 10, &n))
4373 return -EINVAL;
4374
4375 if (n != 0 && n != 1)
4376 return -EINVAL;
4377
4378 mddev->parallel_resync = n;
4379
4380 if (mddev->sync_thread)
4381 wake_up(&resync_wait);
4382
4383 return len;
4384}
4385
4386/* force parallel resync, even with shared block devices */
4387static struct md_sysfs_entry md_sync_force_parallel =
4388__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4389 sync_force_parallel_show, sync_force_parallel_store);
4390
4391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004393{
4394 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004395 if (mddev->curr_resync == 0)
4396 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004397 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4398 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004399 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004400 db = resync - mddev->resync_mark_cnt;
4401 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004402}
4403
NeilBrown80ca3a42006-07-10 04:44:18 -07004404static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004405
4406static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004407sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004408{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004409 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004410
NeilBrownacb180b2009-04-14 16:28:34 +10004411 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4412 return sprintf(page, "none\n");
4413
NeilBrown72f36d52012-10-11 14:25:57 +11004414 if (mddev->curr_resync == 1 ||
4415 mddev->curr_resync == 2)
4416 return sprintf(page, "delayed\n");
4417
NeilBrownc804cde2012-05-21 09:28:33 +10004418 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4419 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004420 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004421 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004422 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004423
NeilBrownacb180b2009-04-14 16:28:34 +10004424 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004425 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004426}
4427
NeilBrown80ca3a42006-07-10 04:44:18 -07004428static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004429
NeilBrowne464eaf2006-03-27 01:18:14 -08004430static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004431min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004432{
4433 return sprintf(page, "%llu\n",
4434 (unsigned long long)mddev->resync_min);
4435}
4436static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004437min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004438{
4439 unsigned long long min;
4440 if (strict_strtoull(buf, 10, &min))
4441 return -EINVAL;
4442 if (min > mddev->resync_max)
4443 return -EINVAL;
4444 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4445 return -EBUSY;
4446
4447 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004448 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004449 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004450 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004451 return -EINVAL;
4452 }
4453 mddev->resync_min = min;
4454
4455 return len;
4456}
4457
4458static struct md_sysfs_entry md_min_sync =
4459__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004463{
4464 if (mddev->resync_max == MaxSector)
4465 return sprintf(page, "max\n");
4466 else
4467 return sprintf(page, "%llu\n",
4468 (unsigned long long)mddev->resync_max);
4469}
4470static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004471max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004472{
4473 if (strncmp(buf, "max", 3) == 0)
4474 mddev->resync_max = MaxSector;
4475 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004476 unsigned long long max;
4477 if (strict_strtoull(buf, 10, &max))
4478 return -EINVAL;
4479 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004480 return -EINVAL;
4481 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004482 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004483 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4484 return -EBUSY;
4485
4486 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004487 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004488 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004489 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004490 return -EINVAL;
4491 }
4492 mddev->resync_max = max;
4493 }
4494 wake_up(&mddev->recovery_wait);
4495 return len;
4496}
4497
4498static struct md_sysfs_entry md_max_sync =
4499__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4500
4501static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004502suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004503{
4504 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4505}
4506
4507static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004508suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004509{
4510 char *e;
4511 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004512 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004513
NeilBrownb8d966e2009-07-01 11:14:04 +10004514 if (mddev->pers == NULL ||
4515 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004516 return -EINVAL;
4517 if (buf == e || (*e && *e != '\n'))
4518 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004519
4520 mddev->suspend_lo = new;
4521 if (new >= old)
4522 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004523 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004524 else {
4525 /* Expanding suspended region - need to wait */
4526 mddev->pers->quiesce(mddev, 1);
4527 mddev->pers->quiesce(mddev, 0);
4528 }
4529 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004530}
4531static struct md_sysfs_entry md_suspend_lo =
4532__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4533
4534
4535static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004536suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004537{
4538 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4539}
4540
4541static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004542suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004543{
4544 char *e;
4545 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004546 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004547
NeilBrownb8d966e2009-07-01 11:14:04 +10004548 if (mddev->pers == NULL ||
4549 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004550 return -EINVAL;
4551 if (buf == e || (*e && *e != '\n'))
4552 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004553
4554 mddev->suspend_hi = new;
4555 if (new <= old)
4556 /* Shrinking suspended region */
4557 mddev->pers->quiesce(mddev, 2);
4558 else {
4559 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004560 mddev->pers->quiesce(mddev, 1);
4561 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004562 }
4563 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004564}
4565static struct md_sysfs_entry md_suspend_hi =
4566__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4567
NeilBrown08a02ec2007-05-09 02:35:38 -07004568static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004569reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004570{
4571 if (mddev->reshape_position != MaxSector)
4572 return sprintf(page, "%llu\n",
4573 (unsigned long long)mddev->reshape_position);
4574 strcpy(page, "none\n");
4575 return 5;
4576}
4577
4578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004579reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004580{
NeilBrownc6563a82012-05-21 09:27:00 +10004581 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004582 char *e;
4583 unsigned long long new = simple_strtoull(buf, &e, 10);
4584 if (mddev->pers)
4585 return -EBUSY;
4586 if (buf == e || (*e && *e != '\n'))
4587 return -EINVAL;
4588 mddev->reshape_position = new;
4589 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004590 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004591 mddev->new_level = mddev->level;
4592 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004593 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004594 rdev_for_each(rdev, mddev)
4595 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004596 return len;
4597}
4598
4599static struct md_sysfs_entry md_reshape_position =
4600__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4601 reshape_position_store);
4602
Dan Williamsb522adc2009-03-31 15:00:31 +11004603static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004604reshape_direction_show(struct mddev *mddev, char *page)
4605{
4606 return sprintf(page, "%s\n",
4607 mddev->reshape_backwards ? "backwards" : "forwards");
4608}
4609
4610static ssize_t
4611reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4612{
4613 int backwards = 0;
4614 if (cmd_match(buf, "forwards"))
4615 backwards = 0;
4616 else if (cmd_match(buf, "backwards"))
4617 backwards = 1;
4618 else
4619 return -EINVAL;
4620 if (mddev->reshape_backwards == backwards)
4621 return len;
4622
4623 /* check if we are allowed to change */
4624 if (mddev->delta_disks)
4625 return -EBUSY;
4626
4627 if (mddev->persistent &&
4628 mddev->major_version == 0)
4629 return -EINVAL;
4630
4631 mddev->reshape_backwards = backwards;
4632 return len;
4633}
4634
4635static struct md_sysfs_entry md_reshape_direction =
4636__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4637 reshape_direction_store);
4638
4639static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004640array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004641{
4642 if (mddev->external_size)
4643 return sprintf(page, "%llu\n",
4644 (unsigned long long)mddev->array_sectors/2);
4645 else
4646 return sprintf(page, "default\n");
4647}
4648
4649static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004650array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004651{
4652 sector_t sectors;
4653
4654 if (strncmp(buf, "default", 7) == 0) {
4655 if (mddev->pers)
4656 sectors = mddev->pers->size(mddev, 0, 0);
4657 else
4658 sectors = mddev->array_sectors;
4659
4660 mddev->external_size = 0;
4661 } else {
4662 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4663 return -EINVAL;
4664 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004665 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004666
4667 mddev->external_size = 1;
4668 }
4669
4670 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004671 if (mddev->pers) {
4672 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004673 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004674 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004675 return len;
4676}
4677
4678static struct md_sysfs_entry md_array_size =
4679__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4680 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004681
NeilBrowneae17012005-11-08 21:39:23 -08004682static struct attribute *md_default_attrs[] = {
4683 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004684 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004685 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004686 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004687 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004688 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004689 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004690 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004691 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004692 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004693 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004694 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004695 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004696 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004697 NULL,
4698};
4699
4700static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004701 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004702 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004703 &md_sync_min.attr,
4704 &md_sync_max.attr,
4705 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004706 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004707 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004708 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004709 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004710 &md_suspend_lo.attr,
4711 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004712 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004713 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004714 NULL,
4715};
NeilBrown411036f2005-11-08 21:39:40 -08004716static struct attribute_group md_redundancy_group = {
4717 .name = NULL,
4718 .attrs = md_redundancy_attrs,
4719};
4720
NeilBrowneae17012005-11-08 21:39:23 -08004721
4722static ssize_t
4723md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4724{
4725 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004726 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004727 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004728
4729 if (!entry->show)
4730 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004731 spin_lock(&all_mddevs_lock);
4732 if (list_empty(&mddev->all_mddevs)) {
4733 spin_unlock(&all_mddevs_lock);
4734 return -EBUSY;
4735 }
4736 mddev_get(mddev);
4737 spin_unlock(&all_mddevs_lock);
4738
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004739 rv = mddev_lock(mddev);
4740 if (!rv) {
4741 rv = entry->show(mddev, page);
4742 mddev_unlock(mddev);
4743 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004744 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004745 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004746}
4747
4748static ssize_t
4749md_attr_store(struct kobject *kobj, struct attribute *attr,
4750 const char *page, size_t length)
4751{
4752 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004753 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004754 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004755
4756 if (!entry->store)
4757 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004758 if (!capable(CAP_SYS_ADMIN))
4759 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004760 spin_lock(&all_mddevs_lock);
4761 if (list_empty(&mddev->all_mddevs)) {
4762 spin_unlock(&all_mddevs_lock);
4763 return -EBUSY;
4764 }
4765 mddev_get(mddev);
4766 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004767 if (entry->store == new_dev_store)
4768 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004769 rv = mddev_lock(mddev);
4770 if (!rv) {
4771 rv = entry->store(mddev, page, length);
4772 mddev_unlock(mddev);
4773 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004774 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004775 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004776}
4777
4778static void md_free(struct kobject *ko)
4779{
NeilBrownfd01b882011-10-11 16:47:53 +11004780 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004781
4782 if (mddev->sysfs_state)
4783 sysfs_put(mddev->sysfs_state);
4784
4785 if (mddev->gendisk) {
4786 del_gendisk(mddev->gendisk);
4787 put_disk(mddev->gendisk);
4788 }
4789 if (mddev->queue)
4790 blk_cleanup_queue(mddev->queue);
4791
NeilBrowneae17012005-11-08 21:39:23 -08004792 kfree(mddev);
4793}
4794
Emese Revfy52cf25d2010-01-19 02:58:23 +01004795static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004796 .show = md_attr_show,
4797 .store = md_attr_store,
4798};
4799static struct kobj_type md_ktype = {
4800 .release = md_free,
4801 .sysfs_ops = &md_sysfs_ops,
4802 .default_attrs = md_default_attrs,
4803};
4804
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805int mdp_major = 0;
4806
Dan Williams5fd3a172009-03-04 00:57:25 -07004807static void mddev_delayed_delete(struct work_struct *ws)
4808{
NeilBrownfd01b882011-10-11 16:47:53 +11004809 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004810
NeilBrown43a70502009-12-14 12:49:55 +11004811 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004812 kobject_del(&mddev->kobj);
4813 kobject_put(&mddev->kobj);
4814}
4815
NeilBrownefeb53c2009-01-09 08:31:10 +11004816static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004818 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004819 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004821 int partitioned;
4822 int shift;
4823 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004824 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004827 return -ENODEV;
4828
4829 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4830 shift = partitioned ? MdpMinorShift : 0;
4831 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Tejun Heoe804ac72010-10-15 15:36:08 +02004833 /* wait for any previous instance of this device to be
4834 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004835 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004836 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004837
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004838 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004839 error = -EEXIST;
4840 if (mddev->gendisk)
4841 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004842
4843 if (name) {
4844 /* Need to ensure that 'name' is not a duplicate.
4845 */
NeilBrownfd01b882011-10-11 16:47:53 +11004846 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004847 spin_lock(&all_mddevs_lock);
4848
4849 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4850 if (mddev2->gendisk &&
4851 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4852 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004853 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004854 }
4855 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
NeilBrown8b765392009-01-09 08:31:08 +11004857
NeilBrown0909dc42009-07-01 12:27:21 +10004858 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004859 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004860 if (!mddev->queue)
4861 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004862 mddev->queue->queuedata = mddev;
4863
NeilBrown409c57f2009-03-31 14:39:39 +11004864 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004865 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 disk = alloc_disk(1 << shift);
4868 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004869 blk_cleanup_queue(mddev->queue);
4870 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004871 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004873 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004875 if (name)
4876 strcpy(disk->disk_name, name);
4877 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004879 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 disk->fops = &md_fops;
4882 disk->private_data = mddev;
4883 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004884 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004885 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004886 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004887 * remove it now.
4888 */
4889 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004891 /* As soon as we call add_disk(), another thread could get
4892 * through to md_open, so make sure it doesn't get too far
4893 */
4894 mutex_lock(&mddev->open_mutex);
4895 add_disk(disk);
4896
Tejun Heoed9e1982008-08-25 19:56:05 +09004897 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4898 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004899 if (error) {
4900 /* This isn't possible, but as kobject_init_and_add is marked
4901 * __must_check, we must do something with the result
4902 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004903 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4904 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004905 error = 0;
4906 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004907 if (mddev->kobj.sd &&
4908 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004909 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004910 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004911 abort:
4912 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004913 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004914 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004915 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004916 }
NeilBrownd3374822009-01-09 08:31:10 +11004917 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004918 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004919}
4920
4921static struct kobject *md_probe(dev_t dev, int *part, void *data)
4922{
4923 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return NULL;
4925}
4926
NeilBrownefeb53c2009-01-09 08:31:10 +11004927static int add_named_array(const char *val, struct kernel_param *kp)
4928{
4929 /* val must be "md_*" where * is not all digits.
4930 * We allocate an array with a large free minor number, and
4931 * set the name to val. val must not already be an active name.
4932 */
4933 int len = strlen(val);
4934 char buf[DISK_NAME_LEN];
4935
4936 while (len && val[len-1] == '\n')
4937 len--;
4938 if (len >= DISK_NAME_LEN)
4939 return -E2BIG;
4940 strlcpy(buf, val, len+1);
4941 if (strncmp(buf, "md_", 3) != 0)
4942 return -EINVAL;
4943 return md_alloc(0, buf);
4944}
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946static void md_safemode_timeout(unsigned long data)
4947{
NeilBrownfd01b882011-10-11 16:47:53 +11004948 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Neil Brown0fd62b82008-06-28 08:31:36 +10004950 if (!atomic_read(&mddev->writes_pending)) {
4951 mddev->safemode = 1;
4952 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004953 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 md_wakeup_thread(mddev->thread);
4956}
4957
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004958static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
NeilBrownfd01b882011-10-11 16:47:53 +11004960int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
NeilBrown2604b702006-01-06 00:20:36 -08004962 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004963 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004964 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
NeilBrowna757e642005-04-16 15:26:42 -07004966 if (list_empty(&mddev->disks))
4967 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 if (mddev->pers)
4971 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004972 /* Cannot run until previous stop completes properly */
4973 if (mddev->sysfs_active)
4974 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 /*
4977 * Analyze all RAID superblock(s)
4978 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004979 if (!mddev->raid_disks) {
4980 if (!mddev->persistent)
4981 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004982 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
NeilBrownd9d166c2006-01-06 00:20:51 -08004985 if (mddev->level != LEVEL_NONE)
4986 request_module("md-level-%d", mddev->level);
4987 else if (mddev->clevel[0])
4988 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
4990 /*
4991 * Drop all container device buffers, from now on
4992 * the only valid external interface is through the md
4993 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004995 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004996 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 continue;
4998 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004999 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005000
5001 /* perform some consistency tests on the device.
5002 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005003 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005004 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005005 if (rdev->meta_bdev) {
5006 /* Nothing to check */;
5007 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005008 if (mddev->dev_sectors &&
5009 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005010 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005011 printk("md: %s: data overlaps metadata\n",
5012 mdname(mddev));
5013 return -EINVAL;
5014 }
5015 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005016 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005017 > rdev->data_offset) {
5018 printk("md: %s: metadata overlaps data\n",
5019 mdname(mddev));
5020 return -EINVAL;
5021 }
5022 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005023 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 }
5025
NeilBrowna167f662010-10-26 18:31:13 +11005026 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005027 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005030 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005031 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005033 if (mddev->level != LEVEL_NONE)
5034 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5035 mddev->level);
5036 else
5037 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5038 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 return -EINVAL;
5040 }
NeilBrown2604b702006-01-06 00:20:36 -08005041 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005043 if (mddev->level != pers->level) {
5044 mddev->level = pers->level;
5045 mddev->new_level = pers->level;
5046 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005047 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
NeilBrownf6705572006-03-27 01:18:11 -08005049 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005050 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005051 /* This personality cannot handle reshaping... */
5052 mddev->pers = NULL;
5053 module_put(pers->owner);
5054 return -EINVAL;
5055 }
5056
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005057 if (pers->sync_request) {
5058 /* Warn if this is a potentially silly
5059 * configuration.
5060 */
5061 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005062 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005063 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005064
NeilBrowndafb20f2012-03-19 12:46:39 +11005065 rdev_for_each(rdev, mddev)
5066 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005067 if (rdev < rdev2 &&
5068 rdev->bdev->bd_contains ==
5069 rdev2->bdev->bd_contains) {
5070 printk(KERN_WARNING
5071 "%s: WARNING: %s appears to be"
5072 " on the same physical disk as"
5073 " %s.\n",
5074 mdname(mddev),
5075 bdevname(rdev->bdev,b),
5076 bdevname(rdev2->bdev,b2));
5077 warned = 1;
5078 }
5079 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005080
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005081 if (warned)
5082 printk(KERN_WARNING
5083 "True protection against single-disk"
5084 " failure might be compromised.\n");
5085 }
5086
NeilBrown657390d2005-08-26 18:34:16 -07005087 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005088 /* may be over-ridden by personality */
5089 mddev->resync_max_sectors = mddev->dev_sectors;
5090
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005091 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
NeilBrown0f9552b52009-12-30 12:08:50 +11005093 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005094 mddev->ro = 2; /* read-only, but switch on first write */
5095
NeilBrownb15c2e52006-01-06 00:20:16 -08005096 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005097 if (err)
5098 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005099 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5100 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5101 " but 'external_size' not in effect?\n", __func__);
5102 printk(KERN_ERR
5103 "md: invalid array_size %llu > default size %llu\n",
5104 (unsigned long long)mddev->array_sectors / 2,
5105 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5106 err = -EINVAL;
5107 mddev->pers->stop(mddev);
5108 }
NeilBrownef99bf42012-05-22 13:55:08 +10005109 if (err == 0 && mddev->pers->sync_request &&
5110 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005111 err = bitmap_create(mddev);
5112 if (err) {
5113 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5114 mdname(mddev), err);
5115 mddev->pers->stop(mddev);
5116 }
5117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 module_put(mddev->pers->owner);
5120 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005121 bitmap_destroy(mddev);
5122 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005124 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005125 if (mddev->kobj.sd &&
5126 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005127 printk(KERN_WARNING
5128 "md: cannot register extra attributes for %s\n",
5129 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005130 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005131 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005132 mddev->ro = 0;
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005135 atomic_set(&mddev->max_corr_read_errors,
5136 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 mddev->safemode = 0;
5138 mddev->safemode_timer.function = md_safemode_timeout;
5139 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005140 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005142 smp_wmb();
5143 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005144 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005145 if (rdev->raid_disk >= 0)
5146 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005147 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5150
NeilBrown850b2b422006-10-03 01:15:46 -07005151 if (mddev->flags)
5152 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
NeilBrownd7603b72006-01-06 00:20:30 -08005154 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005155 sysfs_notify_dirent_safe(mddev->sysfs_state);
5156 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005157 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 return 0;
5159}
NeilBrown390ee602010-06-01 19:37:27 +10005160EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
NeilBrownfd01b882011-10-11 16:47:53 +11005162static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005163{
5164 int err;
5165
5166 err = md_run(mddev);
5167 if (err)
5168 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005169 err = bitmap_load(mddev);
5170 if (err) {
5171 bitmap_destroy(mddev);
5172 goto out;
5173 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005174
5175 md_wakeup_thread(mddev->thread);
5176 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5177
NeilBrownfe60b012010-03-29 11:10:42 +11005178 set_capacity(mddev->gendisk, mddev->array_sectors);
5179 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005180 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005181 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5182out:
5183 return err;
5184}
5185
NeilBrownfd01b882011-10-11 16:47:53 +11005186static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
5188 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Andre Noll80fab1d2008-07-11 22:02:21 +10005190 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005192 return -ENXIO;
5193 if (!mddev->pers)
5194 return -EINVAL;
5195 if (!mddev->ro)
5196 return -EBUSY;
5197 mddev->safemode = 0;
5198 mddev->ro = 0;
5199 set_disk_ro(disk, 0);
5200 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5201 mdname(mddev));
5202 /* Kick recovery or resync if necessary */
5203 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5204 md_wakeup_thread(mddev->thread);
5205 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005206 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208}
5209
NeilBrownacc55e22006-06-26 00:27:47 -07005210/* similar to deny_write_access, but accounts for our holding a reference
5211 * to the file ourselves */
5212static int deny_bitmap_write_access(struct file * file)
5213{
5214 struct inode *inode = file->f_mapping->host;
5215
5216 spin_lock(&inode->i_lock);
5217 if (atomic_read(&inode->i_writecount) > 1) {
5218 spin_unlock(&inode->i_lock);
5219 return -ETXTBSY;
5220 }
5221 atomic_set(&inode->i_writecount, -1);
5222 spin_unlock(&inode->i_lock);
5223
5224 return 0;
5225}
5226
NeilBrown43a70502009-12-14 12:49:55 +11005227void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005228{
5229 struct inode *inode = file->f_mapping->host;
5230
5231 spin_lock(&inode->i_lock);
5232 atomic_set(&inode->i_writecount, 1);
5233 spin_unlock(&inode->i_lock);
5234}
5235
NeilBrownfd01b882011-10-11 16:47:53 +11005236static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005237{
5238 mddev->array_sectors = 0;
5239 mddev->external_size = 0;
5240 mddev->dev_sectors = 0;
5241 mddev->raid_disks = 0;
5242 mddev->recovery_cp = 0;
5243 mddev->resync_min = 0;
5244 mddev->resync_max = MaxSector;
5245 mddev->reshape_position = MaxSector;
5246 mddev->external = 0;
5247 mddev->persistent = 0;
5248 mddev->level = LEVEL_NONE;
5249 mddev->clevel[0] = 0;
5250 mddev->flags = 0;
5251 mddev->ro = 0;
5252 mddev->metadata_type[0] = 0;
5253 mddev->chunk_sectors = 0;
5254 mddev->ctime = mddev->utime = 0;
5255 mddev->layout = 0;
5256 mddev->max_disks = 0;
5257 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005258 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005259 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005260 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->new_level = LEVEL_NONE;
5262 mddev->new_layout = 0;
5263 mddev->new_chunk_sectors = 0;
5264 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005265 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005266 mddev->suspend_lo = mddev->suspend_hi = 0;
5267 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5268 mddev->recovery = 0;
5269 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005270 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005271 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005272 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005273 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005274 mddev->bitmap_info.offset = 0;
5275 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005276 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005277 mddev->bitmap_info.chunksize = 0;
5278 mddev->bitmap_info.daemon_sleep = 0;
5279 mddev->bitmap_info.max_write_behind = 0;
5280}
5281
NeilBrownfd01b882011-10-11 16:47:53 +11005282static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005283{
5284 if (mddev->sync_thread) {
5285 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5286 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005287 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005288 }
5289
5290 del_timer_sync(&mddev->safemode_timer);
5291
5292 bitmap_flush(mddev);
5293 md_super_wait(mddev);
5294
5295 if (!mddev->in_sync || mddev->flags) {
5296 /* mark array as shutdown cleanly */
5297 mddev->in_sync = 1;
5298 md_update_sb(mddev, 1);
5299 }
5300}
NeilBrowndefad612011-01-14 09:14:33 +11005301
NeilBrownfd01b882011-10-11 16:47:53 +11005302void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005303{
5304 mddev_lock(mddev);
5305 __md_stop_writes(mddev);
5306 mddev_unlock(mddev);
5307}
NeilBrown390ee602010-06-01 19:37:27 +10005308EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005309
NeilBrown5eff3c42012-11-19 10:47:48 +11005310static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005311{
NeilBrown0ca69882011-01-14 09:14:33 +11005312 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005313 mddev->pers->stop(mddev);
5314 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5315 mddev->to_remove = &md_redundancy_group;
5316 module_put(mddev->pers->owner);
5317 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005318 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005319}
NeilBrown5eff3c42012-11-19 10:47:48 +11005320
5321void md_stop(struct mddev *mddev)
5322{
5323 /* stop the array and free an attached data structures.
5324 * This is called from dm-raid
5325 */
5326 __md_stop(mddev);
5327 bitmap_destroy(mddev);
5328 if (mddev->bio_set)
5329 bioset_free(mddev->bio_set);
5330}
5331
NeilBrown390ee602010-06-01 19:37:27 +10005332EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005333
NeilBrowna05b7ea2012-07-19 15:59:18 +10005334static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005335{
5336 int err = 0;
5337 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005338 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005339 printk("md: %s still in use.\n",mdname(mddev));
5340 err = -EBUSY;
5341 goto out;
5342 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005343 if (bdev)
5344 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005345 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005346 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005347
5348 err = -ENXIO;
5349 if (mddev->ro==1)
5350 goto out;
5351 mddev->ro = 1;
5352 set_disk_ro(mddev->gendisk, 1);
5353 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005354 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005355 err = 0;
5356 }
5357out:
5358 mutex_unlock(&mddev->open_mutex);
5359 return err;
5360}
5361
NeilBrown9e653b62006-06-26 00:27:58 -07005362/* mode:
5363 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005364 * 2 - stop but do not disassemble array
5365 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005366static int do_md_stop(struct mddev * mddev, int mode,
5367 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005370 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
NeilBrownc8c00a62009-08-10 12:50:52 +10005372 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005373 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005374 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005375 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005376 mutex_unlock(&mddev->open_mutex);
5377 return -EBUSY;
5378 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005379 if (bdev)
5380 /* It is possible IO was issued on some other
5381 * open file which was closed before we took ->open_mutex.
5382 * As that was not the last close __blkdev_put will not
5383 * have called sync_blockdev, so we must.
5384 */
5385 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
NeilBrown6e17b022010-08-07 21:41:19 +10005387 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005388 if (mddev->ro)
5389 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005390
NeilBrowndefad612011-01-14 09:14:33 +11005391 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005392 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005393 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005394 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005395
NeilBrowna4bd82d2010-03-29 13:23:10 +11005396 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005397 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005398
NeilBrowndafb20f2012-03-19 12:46:39 +11005399 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005400 if (rdev->raid_disk >= 0)
5401 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005402
NeilBrowna4bd82d2010-03-29 13:23:10 +11005403 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005404 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005405 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005406 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005407
NeilBrowna4bd82d2010-03-29 13:23:10 +11005408 if (mddev->ro)
5409 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005410 } else
5411 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 /*
5413 * Free resources if final stop
5414 */
NeilBrown9e653b62006-06-26 00:27:58 -07005415 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5417
NeilBrown978f9462006-02-02 14:28:05 -08005418 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005419 if (mddev->bitmap_info.file) {
5420 restore_bitmap_write_access(mddev->bitmap_info.file);
5421 fput(mddev->bitmap_info.file);
5422 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005423 }
NeilBrownc3d97142009-12-14 12:49:52 +11005424 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 export_array(mddev);
5427
NeilBrown6177b472010-03-29 11:37:13 +11005428 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005429 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005430 if (mddev->hold_active == UNTIL_STOP)
5431 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005432 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005433 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005434 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005435 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005439#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005440static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
NeilBrown3cb03002011-10-11 16:45:26 +11005442 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 int err;
5444
NeilBrowna757e642005-04-16 15:26:42 -07005445 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 printk(KERN_INFO "md: running: ");
5449
NeilBrowndafb20f2012-03-19 12:46:39 +11005450 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 char b[BDEVNAME_SIZE];
5452 printk("<%s>", bdevname(rdev->bdev,b));
5453 }
5454 printk("\n");
5455
NeilBrownd710e132008-10-13 11:55:12 +11005456 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 if (err) {
5458 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005459 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
5461}
5462
5463/*
5464 * lets try to run arrays based on all disks that have arrived
5465 * until now. (those are in pending_raid_disks)
5466 *
5467 * the method: pick the first pending disk, collect all disks with
5468 * the same UUID, remove all from the pending list and put them into
5469 * the 'same_array' list. Then order this list based on superblock
5470 * update time (freshest comes first), kick out 'old' disks and
5471 * compare superblocks. If everything's fine then run it.
5472 *
5473 * If "unit" is allocated, then bump its reference count
5474 */
5475static void autorun_devices(int part)
5476{
NeilBrown3cb03002011-10-11 16:45:26 +11005477 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005478 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 char b[BDEVNAME_SIZE];
5480
5481 printk(KERN_INFO "md: autorun ...\n");
5482 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005483 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005485 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005487 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 printk(KERN_INFO "md: considering %s ...\n",
5490 bdevname(rdev0->bdev,b));
5491 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005492 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 if (super_90_load(rdev, rdev0, 0) >= 0) {
5494 printk(KERN_INFO "md: adding %s ...\n",
5495 bdevname(rdev->bdev,b));
5496 list_move(&rdev->same_set, &candidates);
5497 }
5498 /*
5499 * now we have a set of devices, with all of them having
5500 * mostly sane superblocks. It's time to allocate the
5501 * mddev.
5502 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005503 if (part) {
5504 dev = MKDEV(mdp_major,
5505 rdev0->preferred_minor << MdpMinorShift);
5506 unit = MINOR(dev) >> MdpMinorShift;
5507 } else {
5508 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5509 unit = MINOR(dev);
5510 }
5511 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5513 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5514 break;
5515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517 md_probe(dev, NULL, NULL);
5518 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005519 if (!mddev || !mddev->gendisk) {
5520 if (mddev)
5521 mddev_put(mddev);
5522 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 "md: cannot allocate memory for md drive.\n");
5524 break;
5525 }
5526 if (mddev_lock(mddev))
5527 printk(KERN_WARNING "md: %s locked, cannot run\n",
5528 mdname(mddev));
5529 else if (mddev->raid_disks || mddev->major_version
5530 || !list_empty(&mddev->disks)) {
5531 printk(KERN_WARNING
5532 "md: %s already running, cannot run %s\n",
5533 mdname(mddev), bdevname(rdev0->bdev,b));
5534 mddev_unlock(mddev);
5535 } else {
5536 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005537 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005538 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 list_del_init(&rdev->same_set);
5540 if (bind_rdev_to_array(rdev, mddev))
5541 export_rdev(rdev);
5542 }
5543 autorun_array(mddev);
5544 mddev_unlock(mddev);
5545 }
5546 /* on success, candidates will be empty, on error
5547 * it won't...
5548 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005549 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005550 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 mddev_put(mddev);
5554 }
5555 printk(KERN_INFO "md: ... autorun DONE.\n");
5556}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005557#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559static int get_version(void __user * arg)
5560{
5561 mdu_version_t ver;
5562
5563 ver.major = MD_MAJOR_VERSION;
5564 ver.minor = MD_MINOR_VERSION;
5565 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5566
5567 if (copy_to_user(arg, &ver, sizeof(ver)))
5568 return -EFAULT;
5569
5570 return 0;
5571}
5572
NeilBrownfd01b882011-10-11 16:47:53 +11005573static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574{
5575 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005576 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005577 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
NeilBrown1ca69c42012-10-11 13:37:33 +11005579 nr = working = insync = failed = spare = 0;
5580 rcu_read_lock();
5581 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005583 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 failed++;
5585 else {
5586 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005587 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005588 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 else
5590 spare++;
5591 }
5592 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005593 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
5595 info.major_version = mddev->major_version;
5596 info.minor_version = mddev->minor_version;
5597 info.patch_version = MD_PATCHLEVEL_VERSION;
5598 info.ctime = mddev->ctime;
5599 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005600 info.size = mddev->dev_sectors / 2;
5601 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005602 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 info.nr_disks = nr;
5604 info.raid_disks = mddev->raid_disks;
5605 info.md_minor = mddev->md_minor;
5606 info.not_persistent= !mddev->persistent;
5607
5608 info.utime = mddev->utime;
5609 info.state = 0;
5610 if (mddev->in_sync)
5611 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005612 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005613 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005614 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 info.working_disks = working;
5616 info.failed_disks = failed;
5617 info.spare_disks = spare;
5618
5619 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005620 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
5622 if (copy_to_user(arg, &info, sizeof(info)))
5623 return -EFAULT;
5624
5625 return 0;
5626}
5627
NeilBrownfd01b882011-10-11 16:47:53 +11005628static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005629{
5630 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5631 char *ptr, *buf = NULL;
5632 int err = -ENOMEM;
5633
Dan Williamsb5470dc2008-06-27 21:44:04 -07005634 if (md_allow_write(mddev))
5635 file = kmalloc(sizeof(*file), GFP_NOIO);
5636 else
5637 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005638
NeilBrown32a76272005-06-21 17:17:14 -07005639 if (!file)
5640 goto out;
5641
5642 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005643 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005644 file->pathname[0] = '\0';
5645 goto copy_out;
5646 }
5647
5648 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5649 if (!buf)
5650 goto out;
5651
NeilBrown1ec885c2012-05-22 13:55:10 +10005652 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5653 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005654 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005655 goto out;
5656
5657 strcpy(file->pathname, ptr);
5658
5659copy_out:
5660 err = 0;
5661 if (copy_to_user(arg, file, sizeof(*file)))
5662 err = -EFAULT;
5663out:
5664 kfree(buf);
5665 kfree(file);
5666 return err;
5667}
5668
NeilBrownfd01b882011-10-11 16:47:53 +11005669static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670{
5671 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005672 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 if (copy_from_user(&info, arg, sizeof(info)))
5675 return -EFAULT;
5676
NeilBrown1ca69c42012-10-11 13:37:33 +11005677 rcu_read_lock();
5678 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (rdev) {
5680 info.major = MAJOR(rdev->bdev->bd_dev);
5681 info.minor = MINOR(rdev->bdev->bd_dev);
5682 info.raid_disk = rdev->raid_disk;
5683 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005684 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005686 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 info.state |= (1<<MD_DISK_ACTIVE);
5688 info.state |= (1<<MD_DISK_SYNC);
5689 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005690 if (test_bit(WriteMostly, &rdev->flags))
5691 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 } else {
5693 info.major = info.minor = 0;
5694 info.raid_disk = -1;
5695 info.state = (1<<MD_DISK_REMOVED);
5696 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005697 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
5699 if (copy_to_user(arg, &info, sizeof(info)))
5700 return -EFAULT;
5701
5702 return 0;
5703}
5704
NeilBrownfd01b882011-10-11 16:47:53 +11005705static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
5707 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005708 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 dev_t dev = MKDEV(info->major,info->minor);
5710
5711 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5712 return -EOVERFLOW;
5713
5714 if (!mddev->raid_disks) {
5715 int err;
5716 /* expecting a device which has a superblock */
5717 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5718 if (IS_ERR(rdev)) {
5719 printk(KERN_WARNING
5720 "md: md_import_device returned %ld\n",
5721 PTR_ERR(rdev));
5722 return PTR_ERR(rdev);
5723 }
5724 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005725 struct md_rdev *rdev0
5726 = list_entry(mddev->disks.next,
5727 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005728 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 .load_super(rdev, rdev0, mddev->minor_version);
5730 if (err < 0) {
5731 printk(KERN_WARNING
5732 "md: %s has different UUID to %s\n",
5733 bdevname(rdev->bdev,b),
5734 bdevname(rdev0->bdev,b2));
5735 export_rdev(rdev);
5736 return -EINVAL;
5737 }
5738 }
5739 err = bind_rdev_to_array(rdev, mddev);
5740 if (err)
5741 export_rdev(rdev);
5742 return err;
5743 }
5744
5745 /*
5746 * add_new_disk can be used once the array is assembled
5747 * to add "hot spares". They must already have a superblock
5748 * written
5749 */
5750 if (mddev->pers) {
5751 int err;
5752 if (!mddev->pers->hot_add_disk) {
5753 printk(KERN_WARNING
5754 "%s: personality does not support diskops!\n",
5755 mdname(mddev));
5756 return -EINVAL;
5757 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005758 if (mddev->persistent)
5759 rdev = md_import_device(dev, mddev->major_version,
5760 mddev->minor_version);
5761 else
5762 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 if (IS_ERR(rdev)) {
5764 printk(KERN_WARNING
5765 "md: md_import_device returned %ld\n",
5766 PTR_ERR(rdev));
5767 return PTR_ERR(rdev);
5768 }
NeilBrown1a855a02010-12-09 16:36:28 +11005769 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005770 if (!mddev->persistent) {
5771 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005772 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005773 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005774 set_bit(In_sync, &rdev->flags);
5775 } else
NeilBrown41158c72005-06-21 17:17:25 -07005776 rdev->raid_disk = -1;
5777 } else
5778 super_types[mddev->major_version].
5779 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005780 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005781 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005782 /* This was a hot-add request, but events doesn't
5783 * match, so reject it.
5784 */
5785 export_rdev(rdev);
5786 return -EINVAL;
5787 }
5788
NeilBrown1a855a02010-12-09 16:36:28 +11005789 if (test_bit(In_sync, &rdev->flags))
5790 rdev->saved_raid_disk = rdev->raid_disk;
5791 else
5792 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005793
NeilBrownb2d444d2005-11-08 21:39:31 -08005794 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005795 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5796 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005797 else
5798 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 rdev->raid_disk = -1;
5801 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005802 if (!err && !mddev->pers->hot_remove_disk) {
5803 /* If there is hot_add_disk but no hot_remove_disk
5804 * then added disks for geometry changes,
5805 * and should be added immediately.
5806 */
5807 super_types[mddev->major_version].
5808 validate_super(mddev, rdev);
5809 err = mddev->pers->hot_add_disk(mddev, rdev);
5810 if (err)
5811 unbind_rdev_from_array(rdev);
5812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 if (err)
5814 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005815 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005816 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005817
NeilBrown17571282006-12-10 02:20:52 -08005818 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005819 if (mddev->degraded)
5820 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005821 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005822 if (!err)
5823 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005824 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return err;
5826 }
5827
5828 /* otherwise, add_new_disk is only allowed
5829 * for major_version==0 superblocks
5830 */
5831 if (mddev->major_version != 0) {
5832 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5833 mdname(mddev));
5834 return -EINVAL;
5835 }
5836
5837 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5838 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005839 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (IS_ERR(rdev)) {
5841 printk(KERN_WARNING
5842 "md: error, md_import_device() returned %ld\n",
5843 PTR_ERR(rdev));
5844 return PTR_ERR(rdev);
5845 }
5846 rdev->desc_nr = info->number;
5847 if (info->raid_disk < mddev->raid_disks)
5848 rdev->raid_disk = info->raid_disk;
5849 else
5850 rdev->raid_disk = -1;
5851
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005853 if (info->state & (1<<MD_DISK_SYNC))
5854 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005856 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5857 set_bit(WriteMostly, &rdev->flags);
5858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 if (!mddev->persistent) {
5860 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005861 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5862 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005863 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005864 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
NeilBrown2bf071b2006-01-06 00:20:55 -08005866 err = bind_rdev_to_array(rdev, mddev);
5867 if (err) {
5868 export_rdev(rdev);
5869 return err;
5870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 }
5872
5873 return 0;
5874}
5875
NeilBrownfd01b882011-10-11 16:47:53 +11005876static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
5878 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005879 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 rdev = find_rdev(mddev, dev);
5882 if (!rdev)
5883 return -ENXIO;
5884
5885 if (rdev->raid_disk >= 0)
5886 goto busy;
5887
5888 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005889 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005890 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 return 0;
5893busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005894 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 bdevname(rdev->bdev,b), mdname(mddev));
5896 return -EBUSY;
5897}
5898
NeilBrownfd01b882011-10-11 16:47:53 +11005899static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900{
5901 char b[BDEVNAME_SIZE];
5902 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005903 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
5905 if (!mddev->pers)
5906 return -ENODEV;
5907
5908 if (mddev->major_version != 0) {
5909 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5910 " version-0 superblocks.\n",
5911 mdname(mddev));
5912 return -EINVAL;
5913 }
5914 if (!mddev->pers->hot_add_disk) {
5915 printk(KERN_WARNING
5916 "%s: personality does not support diskops!\n",
5917 mdname(mddev));
5918 return -EINVAL;
5919 }
5920
NeilBrownd710e132008-10-13 11:55:12 +11005921 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (IS_ERR(rdev)) {
5923 printk(KERN_WARNING
5924 "md: error, md_import_device() returned %ld\n",
5925 PTR_ERR(rdev));
5926 return -EINVAL;
5927 }
5928
5929 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005930 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005932 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
NeilBrown8190e752009-06-18 08:48:58 +10005934 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
NeilBrownb2d444d2005-11-08 21:39:31 -08005936 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 printk(KERN_WARNING
5938 "md: can not hot-add faulty %s disk to %s!\n",
5939 bdevname(rdev->bdev,b), mdname(mddev));
5940 err = -EINVAL;
5941 goto abort_export;
5942 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005943 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005945 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005946 err = bind_rdev_to_array(rdev, mddev);
5947 if (err)
5948 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /*
5951 * The rest should better be atomic, we can have disk failures
5952 * noticed in interrupt contexts ...
5953 */
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 rdev->raid_disk = -1;
5956
NeilBrown850b2b422006-10-03 01:15:46 -07005957 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
5959 /*
5960 * Kick recovery, maybe this spare has to be added to the
5961 * array immediately.
5962 */
5963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5964 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005965 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 return 0;
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968abort_export:
5969 export_rdev(rdev);
5970 return err;
5971}
5972
NeilBrownfd01b882011-10-11 16:47:53 +11005973static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005974{
5975 int err;
5976
NeilBrown36fa3062005-09-09 16:23:45 -07005977 if (mddev->pers) {
5978 if (!mddev->pers->quiesce)
5979 return -EBUSY;
5980 if (mddev->recovery || mddev->sync_thread)
5981 return -EBUSY;
5982 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005983 }
5984
NeilBrown36fa3062005-09-09 16:23:45 -07005985
5986 if (fd >= 0) {
5987 if (mddev->bitmap)
5988 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005989 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005990
NeilBrownc3d97142009-12-14 12:49:52 +11005991 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005992 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5993 mdname(mddev));
5994 return -EBADF;
5995 }
5996
NeilBrownc3d97142009-12-14 12:49:52 +11005997 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005998 if (err) {
5999 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6000 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11006001 fput(mddev->bitmap_info.file);
6002 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006003 return err;
6004 }
NeilBrownc3d97142009-12-14 12:49:52 +11006005 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006006 } else if (mddev->bitmap == NULL)
6007 return -ENOENT; /* cannot remove what isn't there */
6008 err = 0;
6009 if (mddev->pers) {
6010 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006011 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006012 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006013 if (!err)
6014 err = bitmap_load(mddev);
6015 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006016 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006017 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006018 fd = -1; /* make sure to put the file */
6019 }
NeilBrown36fa3062005-09-09 16:23:45 -07006020 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006021 }
6022 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006023 if (mddev->bitmap_info.file) {
6024 restore_bitmap_write_access(mddev->bitmap_info.file);
6025 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006026 }
NeilBrownc3d97142009-12-14 12:49:52 +11006027 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006028 }
6029
NeilBrown32a76272005-06-21 17:17:14 -07006030 return err;
6031}
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033/*
6034 * set_array_info is used two different ways
6035 * The original usage is when creating a new array.
6036 * In this usage, raid_disks is > 0 and it together with
6037 * level, size, not_persistent,layout,chunksize determine the
6038 * shape of the array.
6039 * This will always create an array with a type-0.90.0 superblock.
6040 * The newer usage is when assembling an array.
6041 * In this case raid_disks will be 0, and the major_version field is
6042 * use to determine which style super-blocks are to be found on the devices.
6043 * The minor and patch _version numbers are also kept incase the
6044 * super_block handler wishes to interpret them.
6045 */
NeilBrownfd01b882011-10-11 16:47:53 +11006046static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048
6049 if (info->raid_disks == 0) {
6050 /* just setting version number for superblock loading */
6051 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006052 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 super_types[info->major_version].name == NULL) {
6054 /* maybe try to auto-load a module? */
6055 printk(KERN_INFO
6056 "md: superblock version %d not known\n",
6057 info->major_version);
6058 return -EINVAL;
6059 }
6060 mddev->major_version = info->major_version;
6061 mddev->minor_version = info->minor_version;
6062 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006063 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006064 /* ensure mddev_put doesn't delete this now that there
6065 * is some minimal configuration.
6066 */
6067 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 return 0;
6069 }
6070 mddev->major_version = MD_MAJOR_VERSION;
6071 mddev->minor_version = MD_MINOR_VERSION;
6072 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6073 mddev->ctime = get_seconds();
6074
6075 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006076 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006077 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 mddev->raid_disks = info->raid_disks;
6079 /* don't set md_minor, it is determined by which /dev/md* was
6080 * openned
6081 */
6082 if (info->state & (1<<MD_SB_CLEAN))
6083 mddev->recovery_cp = MaxSector;
6084 else
6085 mddev->recovery_cp = 0;
6086 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006087 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
6089 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006090 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
6092 mddev->max_disks = MD_SB_DISKS;
6093
NeilBrowne6910632008-02-06 01:39:51 -08006094 if (mddev->persistent)
6095 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006096 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
NeilBrownc3d97142009-12-14 12:49:52 +11006098 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006099 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006100 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006101
NeilBrownf6705572006-03-27 01:18:11 -08006102 mddev->reshape_position = MaxSector;
6103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 /*
6105 * Generate a 128 bit UUID
6106 */
6107 get_random_bytes(mddev->uuid, 16);
6108
NeilBrownf6705572006-03-27 01:18:11 -08006109 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006110 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006111 mddev->new_layout = mddev->layout;
6112 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006113 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return 0;
6116}
6117
NeilBrownfd01b882011-10-11 16:47:53 +11006118void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006119{
Dan Williamsb522adc2009-03-31 15:00:31 +11006120 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6121
6122 if (mddev->external_size)
6123 return;
6124
Dan Williams1f403622009-03-31 14:59:03 +11006125 mddev->array_sectors = array_sectors;
6126}
6127EXPORT_SYMBOL(md_set_array_sectors);
6128
NeilBrownfd01b882011-10-11 16:47:53 +11006129static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006130{
NeilBrown3cb03002011-10-11 16:45:26 +11006131 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006132 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006133 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006134
6135 if (mddev->pers->resize == NULL)
6136 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006137 /* The "num_sectors" is the number of sectors of each device that
6138 * is used. This can only make sense for arrays with redundancy.
6139 * linear and raid0 always use whatever space is available. We can only
6140 * consider changing this number if no resync or reconstruction is
6141 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006142 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006143 * of each device. If num_sectors is zero, we find the largest size
6144 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006145 */
6146 if (mddev->sync_thread)
6147 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006148
NeilBrowndafb20f2012-03-19 12:46:39 +11006149 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006150 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006151
Andre Nolld71f9f82008-07-11 22:02:22 +10006152 if (fit && (num_sectors == 0 || num_sectors > avail))
6153 num_sectors = avail;
6154 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006155 return -ENOSPC;
6156 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006157 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006158 if (!rv)
6159 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006160 return rv;
6161}
6162
NeilBrownfd01b882011-10-11 16:47:53 +11006163static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006164{
6165 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006166 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006167 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006168 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006169 return -EINVAL;
6170 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006171 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006172 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006173 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006174 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006175
6176 rdev_for_each(rdev, mddev) {
6177 if (mddev->raid_disks < raid_disks &&
6178 rdev->data_offset < rdev->new_data_offset)
6179 return -EINVAL;
6180 if (mddev->raid_disks > raid_disks &&
6181 rdev->data_offset > rdev->new_data_offset)
6182 return -EINVAL;
6183 }
6184
NeilBrown63c70c42006-03-27 01:18:13 -08006185 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006186 if (mddev->delta_disks < 0)
6187 mddev->reshape_backwards = 1;
6188 else if (mddev->delta_disks > 0)
6189 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006190
6191 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006192 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006193 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006194 mddev->reshape_backwards = 0;
6195 }
NeilBrownda943b992006-01-06 00:20:54 -08006196 return rv;
6197}
6198
6199
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200/*
6201 * update_array_info is used to change the configuration of an
6202 * on-line array.
6203 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6204 * fields in the info are checked against the array.
6205 * Any differences that cannot be handled will cause an error.
6206 * Normally, only one change can be managed at a time.
6207 */
NeilBrownfd01b882011-10-11 16:47:53 +11006208static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
6210 int rv = 0;
6211 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006212 int state = 0;
6213
6214 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006215 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006216 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
6218 if (mddev->major_version != info->major_version ||
6219 mddev->minor_version != info->minor_version ||
6220/* mddev->patch_version != info->patch_version || */
6221 mddev->ctime != info->ctime ||
6222 mddev->level != info->level ||
6223/* mddev->layout != info->layout || */
6224 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006225 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006226 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6227 ((state^info->state) & 0xfffffe00)
6228 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return -EINVAL;
6230 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006231 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6232 cnt++;
6233 if (mddev->raid_disks != info->raid_disks)
6234 cnt++;
6235 if (mddev->layout != info->layout)
6236 cnt++;
6237 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6238 cnt++;
6239 if (cnt == 0)
6240 return 0;
6241 if (cnt > 1)
6242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 if (mddev->layout != info->layout) {
6245 /* Change layout
6246 * we don't need to do anything at the md level, the
6247 * personality will take care of it all.
6248 */
NeilBrown50ac1682009-06-18 08:47:55 +10006249 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006251 else {
6252 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006253 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006254 if (rv)
6255 mddev->new_layout = mddev->layout;
6256 return rv;
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006259 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006260 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
NeilBrownda943b992006-01-06 00:20:54 -08006262 if (mddev->raid_disks != info->raid_disks)
6263 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
NeilBrown36fa3062005-09-09 16:23:45 -07006265 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6266 if (mddev->pers->quiesce == NULL)
6267 return -EINVAL;
6268 if (mddev->recovery || mddev->sync_thread)
6269 return -EBUSY;
6270 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6271 /* add the bitmap */
6272 if (mddev->bitmap)
6273 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006274 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006275 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006276 mddev->bitmap_info.offset =
6277 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006278 mddev->bitmap_info.space =
6279 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006280 mddev->pers->quiesce(mddev, 1);
6281 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006282 if (!rv)
6283 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006284 if (rv)
6285 bitmap_destroy(mddev);
6286 mddev->pers->quiesce(mddev, 0);
6287 } else {
6288 /* remove the bitmap */
6289 if (!mddev->bitmap)
6290 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006291 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006292 return -EINVAL;
6293 mddev->pers->quiesce(mddev, 1);
6294 bitmap_destroy(mddev);
6295 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006296 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006297 }
6298 }
NeilBrown850b2b422006-10-03 01:15:46 -07006299 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 return rv;
6301}
6302
NeilBrownfd01b882011-10-11 16:47:53 +11006303static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304{
NeilBrown3cb03002011-10-11 16:45:26 +11006305 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006306 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
6308 if (mddev->pers == NULL)
6309 return -ENODEV;
6310
NeilBrown1ca69c42012-10-11 13:37:33 +11006311 rcu_read_lock();
6312 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006314 err = -ENODEV;
6315 else {
6316 md_error(mddev, rdev);
6317 if (!test_bit(Faulty, &rdev->flags))
6318 err = -EBUSY;
6319 }
6320 rcu_read_unlock();
6321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Andre Noll2f9618c2008-04-25 18:57:58 +02006324/*
6325 * We have a problem here : there is no easy way to give a CHS
6326 * virtual geometry. We currently pretend that we have a 2 heads
6327 * 4 sectors (with a BIG number of cylinders...). This drives
6328 * dosfs just mad... ;-)
6329 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006330static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6331{
NeilBrownfd01b882011-10-11 16:47:53 +11006332 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006333
6334 geo->heads = 2;
6335 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006336 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006337 return 0;
6338}
6339
Al Viroa39907f2008-03-02 10:31:15 -05006340static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 unsigned int cmd, unsigned long arg)
6342{
6343 int err = 0;
6344 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006345 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006346 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
NeilBrown506c9e42011-12-23 10:17:26 +11006348 switch (cmd) {
6349 case RAID_VERSION:
6350 case GET_ARRAY_INFO:
6351 case GET_DISK_INFO:
6352 break;
6353 default:
6354 if (!capable(CAP_SYS_ADMIN))
6355 return -EACCES;
6356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
6358 /*
6359 * Commands dealing with the RAID driver but not any
6360 * particular array:
6361 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006362 switch (cmd) {
6363 case RAID_VERSION:
6364 err = get_version(argp);
6365 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
NeilBrownc02c0ae2012-12-11 13:39:21 +11006367 case PRINT_RAID_DEBUG:
6368 err = 0;
6369 md_print_devices();
6370 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006373 case RAID_AUTORUN:
6374 err = 0;
6375 autostart_arrays(arg);
6376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006378 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
6380
6381 /*
6382 * Commands creating/starting a new array:
6383 */
6384
Al Viroa39907f2008-03-02 10:31:15 -05006385 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387 if (!mddev) {
6388 BUG();
6389 goto abort;
6390 }
6391
NeilBrown1ca69c42012-10-11 13:37:33 +11006392 /* Some actions do not requires the mutex */
6393 switch (cmd) {
6394 case GET_ARRAY_INFO:
6395 if (!mddev->raid_disks && !mddev->external)
6396 err = -ENODEV;
6397 else
6398 err = get_array_info(mddev, argp);
6399 goto abort;
6400
6401 case GET_DISK_INFO:
6402 if (!mddev->raid_disks && !mddev->external)
6403 err = -ENODEV;
6404 else
6405 err = get_disk_info(mddev, argp);
6406 goto abort;
6407
6408 case SET_DISK_FAULTY:
6409 err = set_disk_faulty(mddev, new_decode_dev(arg));
6410 goto abort;
6411 }
6412
NeilBrowna7a3f082012-12-11 13:35:54 +11006413 if (cmd == ADD_NEW_DISK)
6414 /* need to ensure md_delayed_delete() has completed */
6415 flush_workqueue(md_misc_wq);
6416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 err = mddev_lock(mddev);
6418 if (err) {
6419 printk(KERN_INFO
6420 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6421 err, cmd);
6422 goto abort;
6423 }
6424
NeilBrownc02c0ae2012-12-11 13:39:21 +11006425 if (cmd == SET_ARRAY_INFO) {
6426 mdu_array_info_t info;
6427 if (!arg)
6428 memset(&info, 0, sizeof(info));
6429 else if (copy_from_user(&info, argp, sizeof(info))) {
6430 err = -EFAULT;
6431 goto abort_unlock;
6432 }
6433 if (mddev->pers) {
6434 err = update_array_info(mddev, &info);
6435 if (err) {
6436 printk(KERN_WARNING "md: couldn't update"
6437 " array info. %d\n", err);
6438 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 }
6440 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006441 }
6442 if (!list_empty(&mddev->disks)) {
6443 printk(KERN_WARNING
6444 "md: array %s already has disks!\n",
6445 mdname(mddev));
6446 err = -EBUSY;
6447 goto abort_unlock;
6448 }
6449 if (mddev->raid_disks) {
6450 printk(KERN_WARNING
6451 "md: array %s already initialised!\n",
6452 mdname(mddev));
6453 err = -EBUSY;
6454 goto abort_unlock;
6455 }
6456 err = set_array_info(mddev, &info);
6457 if (err) {
6458 printk(KERN_WARNING "md: couldn't set"
6459 " array info. %d\n", err);
6460 goto abort_unlock;
6461 }
6462 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 }
6464
6465 /*
6466 * Commands querying/configuring an existing array:
6467 */
NeilBrown32a76272005-06-21 17:17:14 -07006468 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006469 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006470 if ((!mddev->raid_disks && !mddev->external)
6471 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6472 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6473 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 err = -ENODEV;
6475 goto abort_unlock;
6476 }
6477
6478 /*
6479 * Commands even a read-only array can execute:
6480 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006481 switch (cmd) {
6482 case GET_BITMAP_FILE:
6483 err = get_bitmap_file(mddev, argp);
6484 goto done_unlock;
6485
6486 case RESTART_ARRAY_RW:
6487 err = restart_array(mddev);
6488 goto done_unlock;
6489
6490 case STOP_ARRAY:
6491 err = do_md_stop(mddev, 0, bdev);
6492 goto done_unlock;
6493
6494 case STOP_ARRAY_RO:
6495 err = md_set_readonly(mddev, bdev);
6496 goto done_unlock;
6497
6498 case BLKROSET:
6499 if (get_user(ro, (int __user *)(arg))) {
6500 err = -EFAULT;
6501 goto done_unlock;
6502 }
6503 err = -EINVAL;
6504
6505 /* if the bdev is going readonly the value of mddev->ro
6506 * does not matter, no writes are coming
6507 */
6508 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006509 goto done_unlock;
6510
NeilBrownc02c0ae2012-12-11 13:39:21 +11006511 /* are we are already prepared for writes? */
6512 if (mddev->ro != 1)
6513 goto done_unlock;
6514
6515 /* transitioning to readauto need only happen for
6516 * arrays that call md_write_start
6517 */
6518 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006520 if (err == 0) {
6521 mddev->ro = 2;
6522 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006523 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006524 }
6525 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 }
6527
6528 /*
6529 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006530 * superblock, so we do not allow them on read-only arrays.
6531 * However non-MD ioctls (e.g. get-size) will still come through
6532 * here and hit the 'default' below, so only disallow
6533 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006535 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006536 if (mddev->ro == 2) {
6537 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006538 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006540 /* mddev_unlock will wake thread */
6541 /* If a device failed while we were read-only, we
6542 * need to make sure the metadata is updated now.
6543 */
6544 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6545 mddev_unlock(mddev);
6546 wait_event(mddev->sb_wait,
6547 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6548 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6549 mddev_lock(mddev);
6550 }
NeilBrownf91de922005-11-08 21:39:36 -08006551 } else {
6552 err = -EROFS;
6553 goto abort_unlock;
6554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 }
6556
NeilBrownc02c0ae2012-12-11 13:39:21 +11006557 switch (cmd) {
6558 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006560 mdu_disk_info_t info;
6561 if (copy_from_user(&info, argp, sizeof(info)))
6562 err = -EFAULT;
6563 else
6564 err = add_new_disk(mddev, &info);
6565 goto done_unlock;
6566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
NeilBrownc02c0ae2012-12-11 13:39:21 +11006568 case HOT_REMOVE_DISK:
6569 err = hot_remove_disk(mddev, new_decode_dev(arg));
6570 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
NeilBrownc02c0ae2012-12-11 13:39:21 +11006572 case HOT_ADD_DISK:
6573 err = hot_add_disk(mddev, new_decode_dev(arg));
6574 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
NeilBrownc02c0ae2012-12-11 13:39:21 +11006576 case RUN_ARRAY:
6577 err = do_md_run(mddev);
6578 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
NeilBrownc02c0ae2012-12-11 13:39:21 +11006580 case SET_BITMAP_FILE:
6581 err = set_bitmap_file(mddev, (int)arg);
6582 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006583
NeilBrownc02c0ae2012-12-11 13:39:21 +11006584 default:
6585 err = -EINVAL;
6586 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 }
6588
6589done_unlock:
6590abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006591 if (mddev->hold_active == UNTIL_IOCTL &&
6592 err != -EINVAL)
6593 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 mddev_unlock(mddev);
6595
6596 return err;
6597done:
6598 if (err)
6599 MD_BUG();
6600abort:
6601 return err;
6602}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006603#ifdef CONFIG_COMPAT
6604static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6605 unsigned int cmd, unsigned long arg)
6606{
6607 switch (cmd) {
6608 case HOT_REMOVE_DISK:
6609 case HOT_ADD_DISK:
6610 case SET_DISK_FAULTY:
6611 case SET_BITMAP_FILE:
6612 /* These take in integer arg, do not convert */
6613 break;
6614 default:
6615 arg = (unsigned long)compat_ptr(arg);
6616 break;
6617 }
6618
6619 return md_ioctl(bdev, mode, cmd, arg);
6620}
6621#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Al Viroa39907f2008-03-02 10:31:15 -05006623static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624{
6625 /*
6626 * Succeed if we can lock the mddev, which confirms that
6627 * it isn't being stopped right now.
6628 */
NeilBrownfd01b882011-10-11 16:47:53 +11006629 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 int err;
6631
Yuanhan Liu0c098222012-05-22 13:55:32 +10006632 if (!mddev)
6633 return -ENODEV;
6634
NeilBrownd3374822009-01-09 08:31:10 +11006635 if (mddev->gendisk != bdev->bd_disk) {
6636 /* we are racing with mddev_put which is discarding this
6637 * bd_disk.
6638 */
6639 mddev_put(mddev);
6640 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006641 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006642 /* Then retry the open from the top */
6643 return -ERESTARTSYS;
6644 }
6645 BUG_ON(mddev != bdev->bd_disk->private_data);
6646
NeilBrownc8c00a62009-08-10 12:50:52 +10006647 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 goto out;
6649
6650 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006651 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006652 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006654 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 out:
6656 return err;
6657}
6658
Al Viroa39907f2008-03-02 10:31:15 -05006659static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660{
NeilBrownfd01b882011-10-11 16:47:53 +11006661 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006663 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006664 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 mddev_put(mddev);
6666
6667 return 0;
6668}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006669
6670static int md_media_changed(struct gendisk *disk)
6671{
NeilBrownfd01b882011-10-11 16:47:53 +11006672 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006673
6674 return mddev->changed;
6675}
6676
6677static int md_revalidate(struct gendisk *disk)
6678{
NeilBrownfd01b882011-10-11 16:47:53 +11006679 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006680
6681 mddev->changed = 0;
6682 return 0;
6683}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006684static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685{
6686 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006687 .open = md_open,
6688 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006689 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006690#ifdef CONFIG_COMPAT
6691 .compat_ioctl = md_compat_ioctl,
6692#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006693 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006694 .media_changed = md_media_changed,
6695 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696};
6697
Adrian Bunk75c96f82005-05-05 16:16:09 -07006698static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699{
NeilBrown2b8bf342011-10-11 16:48:23 +11006700 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 /*
6703 * md_thread is a 'system-thread', it's priority should be very
6704 * high. We avoid resource deadlocks individually in each
6705 * raid personality. (RAID5 does preallocation) We also use RR and
6706 * the very same RT priority as kswapd, thus we will never get
6707 * into a priority inversion deadlock.
6708 *
6709 * we definitely have to have equal or higher priority than
6710 * bdflush, otherwise bdflush will deadlock if there are too
6711 * many dirty RAID5 blocks.
6712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
NeilBrown6985c432005-10-19 21:23:47 -07006714 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006715 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
NeilBrown93588e22005-11-15 00:09:12 -08006717 /* We need to wait INTERRUPTIBLE so that
6718 * we don't add to the load-average.
6719 * That means we need to be sure no signals are
6720 * pending
6721 */
6722 if (signal_pending(current))
6723 flush_signals(current);
6724
6725 wait_event_interruptible_timeout
6726 (thread->wqueue,
6727 test_bit(THREAD_WAKEUP, &thread->flags)
6728 || kthread_should_stop(),
6729 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
NeilBrown6c987912011-01-14 09:13:53 +11006731 clear_bit(THREAD_WAKEUP, &thread->flags);
6732 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006733 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 return 0;
6737}
6738
NeilBrown2b8bf342011-10-11 16:48:23 +11006739void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740{
6741 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006742 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 set_bit(THREAD_WAKEUP, &thread->flags);
6744 wake_up(&thread->wqueue);
6745 }
6746}
6747
Shaohua Li4ed87312012-10-11 13:34:00 +11006748struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6749 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750{
NeilBrown2b8bf342011-10-11 16:48:23 +11006751 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752
NeilBrown2b8bf342011-10-11 16:48:23 +11006753 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 if (!thread)
6755 return NULL;
6756
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 init_waitqueue_head(&thread->wqueue);
6758
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 thread->run = run;
6760 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006761 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006762 thread->tsk = kthread_run(md_thread, thread,
6763 "%s_%s",
6764 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006765 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006766 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 kfree(thread);
6768 return NULL;
6769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 return thread;
6771}
6772
NeilBrown2b8bf342011-10-11 16:48:23 +11006773void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774{
NeilBrown2b8bf342011-10-11 16:48:23 +11006775 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006776 if (!thread)
6777 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006778 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006779 /* Locking ensures that mddev_unlock does not wake_up a
6780 * non-existent thread
6781 */
6782 spin_lock(&pers_lock);
6783 *threadp = NULL;
6784 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006785
6786 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 kfree(thread);
6788}
6789
NeilBrownfd01b882011-10-11 16:47:53 +11006790void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791{
6792 if (!mddev) {
6793 MD_BUG();
6794 return;
6795 }
6796
NeilBrownb2d444d2005-11-08 21:39:31 -08006797 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006799
NeilBrownde393cd2011-07-28 11:31:48 +10006800 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 return;
6802 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006803 if (mddev->degraded)
6804 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006805 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6807 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6808 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006809 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006810 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006811 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812}
6813
6814/* seq_file implementation /proc/mdstat */
6815
6816static void status_unused(struct seq_file *seq)
6817{
6818 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006819 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
6821 seq_printf(seq, "unused devices: ");
6822
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006823 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 char b[BDEVNAME_SIZE];
6825 i++;
6826 seq_printf(seq, "%s ",
6827 bdevname(rdev->bdev,b));
6828 }
6829 if (!i)
6830 seq_printf(seq, "<none>");
6831
6832 seq_printf(seq, "\n");
6833}
6834
6835
NeilBrownfd01b882011-10-11 16:47:53 +11006836static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837{
NeilBrowndd71cf62009-05-07 12:49:35 +10006838 sector_t max_sectors, resync, res;
6839 unsigned long dt, db;
6840 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006841 int scale;
6842 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
NeilBrown72f36d52012-10-11 14:25:57 +11006844 if (mddev->curr_resync <= 3)
6845 resync = 0;
6846 else
6847 resync = mddev->curr_resync
6848 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
NeilBrownc804cde2012-05-21 09:28:33 +10006850 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6851 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006852 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006854 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
6856 /*
6857 * Should not happen.
6858 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006859 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 MD_BUG();
6861 return;
6862 }
NeilBrown4588b422006-03-27 01:18:04 -08006863 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006864 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006865 * u32, as those are the requirements for sector_div.
6866 * Thus 'scale' must be at least 10
6867 */
6868 scale = 10;
6869 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006870 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006871 scale++;
6872 }
6873 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006874 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006875
6876 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 {
NeilBrown4588b422006-03-27 01:18:04 -08006878 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 seq_printf(seq, "[");
6880 for (i = 0; i < x; i++)
6881 seq_printf(seq, "=");
6882 seq_printf(seq, ">");
6883 for (i = 0; i < y; i++)
6884 seq_printf(seq, ".");
6885 seq_printf(seq, "] ");
6886 }
NeilBrown4588b422006-03-27 01:18:04 -08006887 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006888 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6889 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006890 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6891 "check" :
6892 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6893 "resync" : "recovery"))),
6894 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006895 (unsigned long long) resync/2,
6896 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
6898 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 * dt: time from mark until now
6900 * db: blocks written from mark until now
6901 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006902 *
6903 * rt is a sector_t, so could be 32bit or 64bit.
6904 * So we divide before multiply in case it is 32bit and close
6905 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006906 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006907 * near the end of resync when the number of remaining sectors
6908 * is close to 'db'.
6909 * We then divide rt by 32 after multiplying by db to compensate.
6910 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 */
6912 dt = ((jiffies - mddev->resync_mark) / HZ);
6913 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006914 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6915 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
NeilBrowndd71cf62009-05-07 12:49:35 +10006917 rt = max_sectors - resync; /* number of remaining sectors */
6918 sector_div(rt, db/32+1);
6919 rt *= dt;
6920 rt >>= 5;
6921
6922 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6923 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
NeilBrownff4e8d92006-07-10 04:44:16 -07006925 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926}
6927
6928static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6929{
6930 struct list_head *tmp;
6931 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006932 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
6934 if (l >= 0x10000)
6935 return NULL;
6936 if (!l--)
6937 /* header */
6938 return (void*)1;
6939
6940 spin_lock(&all_mddevs_lock);
6941 list_for_each(tmp,&all_mddevs)
6942 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006943 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 mddev_get(mddev);
6945 spin_unlock(&all_mddevs_lock);
6946 return mddev;
6947 }
6948 spin_unlock(&all_mddevs_lock);
6949 if (!l--)
6950 return (void*)2;/* tail */
6951 return NULL;
6952}
6953
6954static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6955{
6956 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006957 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
6959 ++*pos;
6960 if (v == (void*)2)
6961 return NULL;
6962
6963 spin_lock(&all_mddevs_lock);
6964 if (v == (void*)1)
6965 tmp = all_mddevs.next;
6966 else
6967 tmp = mddev->all_mddevs.next;
6968 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006969 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 else {
6971 next_mddev = (void*)2;
6972 *pos = 0x10000;
6973 }
6974 spin_unlock(&all_mddevs_lock);
6975
6976 if (v != (void*)1)
6977 mddev_put(mddev);
6978 return next_mddev;
6979
6980}
6981
6982static void md_seq_stop(struct seq_file *seq, void *v)
6983{
NeilBrownfd01b882011-10-11 16:47:53 +11006984 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
6986 if (mddev && v != (void*)1 && v != (void*)2)
6987 mddev_put(mddev);
6988}
6989
6990static int md_seq_show(struct seq_file *seq, void *v)
6991{
NeilBrownfd01b882011-10-11 16:47:53 +11006992 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006993 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006994 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006997 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 seq_printf(seq, "Personalities : ");
6999 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007000 list_for_each_entry(pers, &pers_list, list)
7001 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
7003 spin_unlock(&pers_lock);
7004 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007005 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 return 0;
7007 }
7008 if (v == (void*)2) {
7009 status_unused(seq);
7010 return 0;
7011 }
7012
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007013 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007015
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7017 seq_printf(seq, "%s : %sactive", mdname(mddev),
7018 mddev->pers ? "" : "in");
7019 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007020 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007022 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007023 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 seq_printf(seq, " %s", mddev->pers->name);
7025 }
7026
Andre Nolldd8ac332009-03-31 14:33:13 +11007027 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007028 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 char b[BDEVNAME_SIZE];
7030 seq_printf(seq, " %s[%d]",
7031 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007032 if (test_bit(WriteMostly, &rdev->flags))
7033 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007034 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 seq_printf(seq, "(F)");
7036 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007037 }
7038 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007039 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007040 if (test_bit(Replacement, &rdev->flags))
7041 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007042 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 }
7044
7045 if (!list_empty(&mddev->disks)) {
7046 if (mddev->pers)
7047 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007048 (unsigned long long)
7049 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 else
7051 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007052 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007054 if (mddev->persistent) {
7055 if (mddev->major_version != 0 ||
7056 mddev->minor_version != 90) {
7057 seq_printf(seq," super %d.%d",
7058 mddev->major_version,
7059 mddev->minor_version);
7060 }
NeilBrowne6910632008-02-06 01:39:51 -08007061 } else if (mddev->external)
7062 seq_printf(seq, " super external:%s",
7063 mddev->metadata_type);
7064 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007065 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
7067 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007068 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007070 if (mddev->pers->sync_request) {
7071 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007072 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007073 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007074 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007075 seq_printf(seq, "\tresync=DELAYED\n ");
7076 else if (mddev->recovery_cp < MaxSector)
7077 seq_printf(seq, "\tresync=PENDING\n ");
7078 }
NeilBrown32a76272005-06-21 17:17:14 -07007079 } else
7080 seq_printf(seq, "\n ");
7081
NeilBrown57148962012-03-19 12:46:40 +11007082 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
7084 seq_printf(seq, "\n");
7085 }
7086 mddev_unlock(mddev);
7087
7088 return 0;
7089}
7090
Jan Engelhardt110518b2009-05-07 12:49:37 +10007091static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 .start = md_seq_start,
7093 .next = md_seq_next,
7094 .stop = md_seq_stop,
7095 .show = md_seq_show,
7096};
7097
7098static int md_seq_open(struct inode *inode, struct file *file)
7099{
Kay Sieversf1514632011-07-12 20:48:39 +02007100 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 int error;
7102
7103 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007104 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007105 return error;
7106
7107 seq = file->private_data;
7108 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 return error;
7110}
7111
NeilBrownd7603b72006-01-06 00:20:30 -08007112static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7113{
Kay Sieversf1514632011-07-12 20:48:39 +02007114 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007115 int mask;
7116
7117 poll_wait(filp, &md_event_waiters, wait);
7118
7119 /* always allow read */
7120 mask = POLLIN | POLLRDNORM;
7121
Kay Sieversf1514632011-07-12 20:48:39 +02007122 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007123 mask |= POLLERR | POLLPRI;
7124 return mask;
7125}
7126
Arjan van de Venfa027c22007-02-12 00:55:33 -08007127static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007128 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 .open = md_seq_open,
7130 .read = seq_read,
7131 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007132 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007133 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134};
7135
NeilBrown84fc4b52011-10-11 16:49:58 +11007136int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007139 list_add_tail(&p->list, &pers_list);
7140 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 spin_unlock(&pers_lock);
7142 return 0;
7143}
7144
NeilBrown84fc4b52011-10-11 16:49:58 +11007145int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146{
NeilBrown2604b702006-01-06 00:20:36 -08007147 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007149 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 spin_unlock(&pers_lock);
7151 return 0;
7152}
7153
NeilBrownfd01b882011-10-11 16:47:53 +11007154static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155{
NeilBrown3cb03002011-10-11 16:45:26 +11007156 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007158 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
7160 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007161 rcu_read_lock();
7162 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007164 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7165 (int)part_stat_read(&disk->part0, sectors[1]) -
7166 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007167 /* sync IO will cause sync_io to increase before the disk_stats
7168 * as sync_io is counted when a request starts, and
7169 * disk_stats is counted when it completes.
7170 * So resync activity will cause curr_events to be smaller than
7171 * when there was no such activity.
7172 * non-sync IO will cause disk_stat to increase without
7173 * increasing sync_io so curr_events will (eventually)
7174 * be larger than it was before. Once it becomes
7175 * substantially larger, the test below will cause
7176 * the array to appear non-idle, and resync will slow
7177 * down.
7178 * If there is a lot of outstanding resync activity when
7179 * we set last_event to curr_events, then all that activity
7180 * completing might cause the array to appear non-idle
7181 * and resync will be slowed down even though there might
7182 * not have been non-resync activity. This will only
7183 * happen once though. 'last_events' will soon reflect
7184 * the state where there is little or no outstanding
7185 * resync requests, and further resync activity will
7186 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007187 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007189 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 rdev->last_events = curr_events;
7191 idle = 0;
7192 }
7193 }
NeilBrown4b809912008-07-21 17:05:25 +10007194 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 return idle;
7196}
7197
NeilBrownfd01b882011-10-11 16:47:53 +11007198void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199{
7200 /* another "blocks" (512byte) blocks have been synced */
7201 atomic_sub(blocks, &mddev->recovery_active);
7202 wake_up(&mddev->recovery_wait);
7203 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007204 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007205 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 md_wakeup_thread(mddev->thread);
7207 // stop recovery, signal do_sync ....
7208 }
7209}
7210
7211
NeilBrown06d91a52005-06-21 17:17:12 -07007212/* md_write_start(mddev, bi)
7213 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007214 * in superblock) before writing, schedule a superblock update
7215 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007216 */
NeilBrownfd01b882011-10-11 16:47:53 +11007217void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218{
Neil Brown0fd62b82008-06-28 08:31:36 +10007219 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007220 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007221 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007222
NeilBrownf91de922005-11-08 21:39:36 -08007223 BUG_ON(mddev->ro == 1);
7224 if (mddev->ro == 2) {
7225 /* need to switch to read/write */
7226 mddev->ro = 0;
7227 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7228 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007229 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007230 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007231 }
NeilBrown06d91a52005-06-21 17:17:12 -07007232 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007233 if (mddev->safemode == 1)
7234 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007235 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007236 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007237 if (mddev->in_sync) {
7238 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007239 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007240 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007241 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007242 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007243 }
NeilBrowna9701a32005-11-08 21:39:34 -08007244 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007245 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007246 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007247 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007248 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007249 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250}
7251
NeilBrownfd01b882011-10-11 16:47:53 +11007252void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253{
7254 if (atomic_dec_and_test(&mddev->writes_pending)) {
7255 if (mddev->safemode == 2)
7256 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007257 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7259 }
7260}
7261
NeilBrown2a2275d2007-01-26 00:57:11 -08007262/* md_allow_write(mddev)
7263 * Calling this ensures that the array is marked 'active' so that writes
7264 * may proceed without blocking. It is important to call this before
7265 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7266 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007267 *
7268 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7269 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007270 */
NeilBrownfd01b882011-10-11 16:47:53 +11007271int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007272{
7273 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007274 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007275 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007276 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007277 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007278 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007279
7280 spin_lock_irq(&mddev->write_lock);
7281 if (mddev->in_sync) {
7282 mddev->in_sync = 0;
7283 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007284 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007285 if (mddev->safemode_delay &&
7286 mddev->safemode == 0)
7287 mddev->safemode = 1;
7288 spin_unlock_irq(&mddev->write_lock);
7289 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007290 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007291 } else
7292 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007293
NeilBrown070dc6d2010-08-30 17:33:34 +10007294 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007295 return -EAGAIN;
7296 else
7297 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007298}
7299EXPORT_SYMBOL_GPL(md_allow_write);
7300
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301#define SYNC_MARKS 10
7302#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007303#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007304void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305{
Shaohua Li4ed87312012-10-11 13:34:00 +11007306 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007307 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 unsigned int currspeed = 0,
7309 window;
NeilBrown57afd892005-06-21 17:17:13 -07007310 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007312 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 sector_t mark_cnt[SYNC_MARKS];
7314 int last_mark,m;
7315 struct list_head *tmp;
7316 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007317 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007318 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007319 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007320 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321
7322 /* just incase thread restarts... */
7323 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7324 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007325 if (mddev->ro) /* never try to sync a read-only array */
7326 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
NeilBrown61df9d92006-10-03 01:15:57 -07007328 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7329 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7330 desc = "data-check";
7331 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7332 desc = "requested-resync";
7333 else
7334 desc = "resync";
7335 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7336 desc = "reshape";
7337 else
7338 desc = "recovery";
7339
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 /* we overload curr_resync somewhat here.
7341 * 0 == not engaged in resync at all
7342 * 2 == checking that there is no conflict with another sync
7343 * 1 == like 2, but have yielded to allow conflicting resync to
7344 * commense
7345 * other == active in resync - this many blocks
7346 *
7347 * Before starting a resync we must have set curr_resync to
7348 * 2, and then checked that every "conflicting" array has curr_resync
7349 * less than ours. When we find one that is the same or higher
7350 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7351 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7352 * This will mean we have to start checking from the beginning again.
7353 *
7354 */
7355
7356 do {
7357 mddev->curr_resync = 2;
7358
7359 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007360 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007361 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007362
7363 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007365 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 if (mddev2 == mddev)
7367 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007368 if (!mddev->parallel_resync
7369 && mddev2->curr_resync
7370 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 DEFINE_WAIT(wq);
7372 if (mddev < mddev2 && mddev->curr_resync == 2) {
7373 /* arbitrarily yield */
7374 mddev->curr_resync = 1;
7375 wake_up(&resync_wait);
7376 }
7377 if (mddev > mddev2 && mddev->curr_resync == 1)
7378 /* no need to wait here, we can wait the next
7379 * time 'round when curr_resync == 2
7380 */
7381 continue;
NeilBrown97441972008-09-19 11:49:54 +10007382 /* We need to wait 'interruptible' so as not to
7383 * contribute to the load average, and not to
7384 * be caught by 'softlockup'
7385 */
7386 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007387 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007388 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007389 printk(KERN_INFO "md: delaying %s of %s"
7390 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007392 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007394 if (signal_pending(current))
7395 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 schedule();
7397 finish_wait(&resync_wait, &wq);
7398 goto try_again;
7399 }
7400 finish_wait(&resync_wait, &wq);
7401 }
7402 }
7403 } while (mddev->curr_resync < 2);
7404
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007405 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007406 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007408 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 */
7410 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007411 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007412 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007413 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7414 j = mddev->resync_min;
7415 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007416 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007417
NeilBrownccfcc3c2006-03-27 01:18:09 -08007418 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007419 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007420 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007422 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007423 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007424 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007425 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007426 if (rdev->raid_disk >= 0 &&
7427 !test_bit(Faulty, &rdev->flags) &&
7428 !test_bit(In_sync, &rdev->flags) &&
7429 rdev->recovery_offset < j)
7430 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007431 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433
NeilBrown61df9d92006-10-03 01:15:57 -07007434 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7435 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7436 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007437 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007438 "(but not more than %d KB/sec) for %s.\n",
7439 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
NeilBrowneea1bf32009-03-31 14:27:02 +11007441 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007442
NeilBrown57afd892005-06-21 17:17:13 -07007443 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 for (m = 0; m < SYNC_MARKS; m++) {
7445 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007446 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 }
7448 last_mark = 0;
7449 mddev->resync_mark = mark[last_mark];
7450 mddev->resync_mark_cnt = mark_cnt[last_mark];
7451
7452 /*
7453 * Tune reconstruction:
7454 */
7455 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007456 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7457 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
7459 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 last_check = 0;
7461
7462 if (j>2) {
7463 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007464 "md: resuming %s of %s from checkpoint.\n",
7465 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007467 } else
7468 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007469 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007470 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7471 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007472 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
majianpeng7c2c57c2012-07-03 12:12:26 +10007474 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007476 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477
NeilBrown57afd892005-06-21 17:17:13 -07007478 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007479
NeilBrown7a91ee12009-05-26 12:57:21 +10007480 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7481 ((mddev->curr_resync > mddev->curr_resync_completed &&
7482 (mddev->curr_resync - mddev->curr_resync_completed)
7483 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007484 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007485 (j - mddev->curr_resync_completed)*2
7486 >= mddev->resync_max - mddev->curr_resync_completed
7487 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007488 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007489 wait_event(mddev->recovery_wait,
7490 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007491 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007492 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7493 j > mddev->recovery_cp)
7494 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007495 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007496 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007497 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007498 }
NeilBrownacb180b2009-04-14 16:28:34 +10007499
NeilBrowne62e58a2009-07-01 13:15:35 +10007500 while (j >= mddev->resync_max && !kthread_should_stop()) {
7501 /* As this condition is controlled by user-space,
7502 * we can block indefinitely, so use '_interruptible'
7503 * to avoid triggering warnings.
7504 */
7505 flush_signals(current); /* just in case */
7506 wait_event_interruptible(mddev->recovery_wait,
7507 mddev->resync_max > j
7508 || kthread_should_stop());
7509 }
NeilBrownacb180b2009-04-14 16:28:34 +10007510
7511 if (kthread_should_stop())
7512 goto interrupted;
7513
NeilBrown57afd892005-06-21 17:17:13 -07007514 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007515 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007516 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007517 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 goto out;
7519 }
NeilBrown57afd892005-06-21 17:17:13 -07007520
7521 if (!skipped) { /* actual IO requested */
7522 io_sectors += sectors;
7523 atomic_add(sectors, &mddev->recovery_active);
7524 }
7525
NeilBrowne875ece2011-07-28 11:39:24 +10007526 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7527 break;
7528
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007530 if (j > 2)
7531 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007532 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007533 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007534 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007535 * visible in /proc/mdstat
7536 */
7537 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007538
7539 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540 continue;
7541
NeilBrown57afd892005-06-21 17:17:13 -07007542 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 repeat:
7544 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7545 /* step marks */
7546 int next = (last_mark+1) % SYNC_MARKS;
7547
7548 mddev->resync_mark = mark[next];
7549 mddev->resync_mark_cnt = mark_cnt[next];
7550 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007551 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 last_mark = next;
7553 }
7554
7555
NeilBrownc6207272008-02-06 01:39:52 -08007556 if (kthread_should_stop())
7557 goto interrupted;
7558
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559
7560 /*
7561 * this loop exits only if either when we are slower than
7562 * the 'hard' speed limit, or the system was IO-idle for
7563 * a jiffy.
7564 * the system might be non-idle CPU-wise, but we only care
7565 * about not overloading the IO subsystem. (things like an
7566 * e2fsck being done on the RAID array should execute fast)
7567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 cond_resched();
7569
NeilBrown57afd892005-06-21 17:17:13 -07007570 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7571 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
NeilBrown88202a02006-01-06 00:21:36 -08007573 if (currspeed > speed_min(mddev)) {
7574 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007575 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007576 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 goto repeat;
7578 }
7579 }
7580 }
NeilBrown61df9d92006-10-03 01:15:57 -07007581 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 /*
7583 * this also signals 'finished resyncing' to md_stop
7584 */
7585 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007586 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7588
7589 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007590 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591
NeilBrowndfc70642008-05-23 13:04:39 -07007592 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007593 mddev->curr_resync > 2) {
7594 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7595 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7596 if (mddev->curr_resync >= mddev->recovery_cp) {
7597 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007598 "md: checkpointing %s of %s.\n",
7599 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007600 if (test_bit(MD_RECOVERY_ERROR,
7601 &mddev->recovery))
7602 mddev->recovery_cp =
7603 mddev->curr_resync_completed;
7604 else
7605 mddev->recovery_cp =
7606 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007607 }
7608 } else
7609 mddev->recovery_cp = MaxSector;
7610 } else {
7611 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7612 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007613 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007614 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007615 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007616 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007617 !test_bit(Faulty, &rdev->flags) &&
7618 !test_bit(In_sync, &rdev->flags) &&
7619 rdev->recovery_offset < mddev->curr_resync)
7620 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007621 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007624 skip:
NeilBrown17571282006-12-10 02:20:52 -08007625 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
NeilBrownc07b70a2009-12-14 12:49:48 +11007627 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7628 /* We completed so min/max setting can be forgotten if used. */
7629 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7630 mddev->resync_min = 0;
7631 mddev->resync_max = MaxSector;
7632 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7633 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 mddev->curr_resync = 0;
7635 wake_up(&resync_wait);
7636 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7637 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007638 return;
7639
7640 interrupted:
7641 /*
7642 * got a signal, exit.
7643 */
7644 printk(KERN_INFO
7645 "md: md_do_sync() got signal ... exiting\n");
7646 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7647 goto out;
7648
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649}
NeilBrown29269552006-03-27 01:18:10 -08007650EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
NeilBrownfd01b882011-10-11 16:47:53 +11007652static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007653{
NeilBrown3cb03002011-10-11 16:45:26 +11007654 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007655 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007656 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007657
NeilBrowndafb20f2012-03-19 12:46:39 +11007658 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007659 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007660 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007661 (test_bit(Faulty, &rdev->flags) ||
7662 ! test_bit(In_sync, &rdev->flags)) &&
7663 atomic_read(&rdev->nr_pending)==0) {
7664 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007665 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007666 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007667 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007668 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007669 }
7670 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007671 if (removed && mddev->kobj.sd)
7672 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007673
NeilBrowndafb20f2012-03-19 12:46:39 +11007674 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007675 if (rdev->raid_disk >= 0 &&
7676 !test_bit(In_sync, &rdev->flags) &&
7677 !test_bit(Faulty, &rdev->flags))
7678 spares++;
7679 if (rdev->raid_disk < 0
7680 && !test_bit(Faulty, &rdev->flags)) {
7681 rdev->recovery_offset = 0;
7682 if (mddev->pers->
7683 hot_add_disk(mddev, rdev) == 0) {
7684 if (sysfs_link_rdev(mddev, rdev))
7685 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007686 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007687 md_new_event(mddev);
7688 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007689 }
NeilBrowndfc70642008-05-23 13:04:39 -07007690 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007691 }
NeilBrown6dafab62012-09-19 12:54:22 +10007692 if (removed)
7693 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007694 return spares;
7695}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007696
NeilBrownfd01b882011-10-11 16:47:53 +11007697static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007698{
NeilBrown3cb03002011-10-11 16:45:26 +11007699 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007700
7701 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007702 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007703 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7704 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7705 /* success...*/
7706 /* activate any spares */
NeilBrown6dafab62012-09-19 12:54:22 +10007707 if (mddev->pers->spare_active(mddev)) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007708 sysfs_notify(&mddev->kobj, NULL,
7709 "degraded");
NeilBrown6dafab62012-09-19 12:54:22 +10007710 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7711 }
NeilBrown7ebc0be2011-01-14 09:14:33 +11007712 }
7713 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7714 mddev->pers->finish_reshape)
7715 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007716
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007717 /* If array is no-longer degraded, then any saved_raid_disk
7718 * information must be scrapped. Also if any device is now
7719 * In_sync we must scrape the saved_raid_disk for that device
7720 * do the superblock for an incrementally recovered device
7721 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007722 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007723 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007724 if (!mddev->degraded ||
7725 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007726 rdev->saved_raid_disk = -1;
7727
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007728 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007729 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7730 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7731 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7732 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7733 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7734 /* flag recovery needed just to double check */
7735 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7736 sysfs_notify_dirent_safe(mddev->sysfs_action);
7737 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007738 if (mddev->event_work.func)
7739 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007740}
7741
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742/*
7743 * This routine is regularly called by all per-raid-array threads to
7744 * deal with generic issues like resync and super-block update.
7745 * Raid personalities that don't have a thread (linear/raid0) do not
7746 * need this as they never do any recovery or update the superblock.
7747 *
7748 * It does not do any resync itself, but rather "forks" off other threads
7749 * to do that as needed.
7750 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7751 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007752 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 * and wakeups up this thread which will reap the thread and finish up.
7754 * This thread also removes any faulty devices (with nr_pending == 0).
7755 *
7756 * The overall approach is:
7757 * 1/ if the superblock needs updating, update it.
7758 * 2/ If a recovery thread is running, don't do anything else.
7759 * 3/ If recovery has finished, clean up, possibly marking spares active.
7760 * 4/ If there are any faulty devices, remove them.
7761 * 5/ If array is degraded, try to add spares devices
7762 * 6/ If array has spares or is not in-sync, start a resync thread.
7763 */
NeilBrownfd01b882011-10-11 16:47:53 +11007764void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007766 if (mddev->suspended)
7767 return;
7768
NeilBrown5f404022005-06-21 17:17:16 -07007769 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007770 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
NeilBrownfca4d842005-06-21 17:17:11 -07007772 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007773 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007774 printk(KERN_INFO "md: %s in immediate safe mode\n",
7775 mdname(mddev));
7776 mddev->safemode = 2;
7777 }
7778 flush_signals(current);
7779 }
7780
NeilBrownc89a8ee2008-08-05 15:54:13 +10007781 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007784 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007786 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007787 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007788 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7789 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 ))
7791 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007792
NeilBrowndf5b89b2006-03-27 01:18:20 -08007793 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007794 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007795
NeilBrownc89a8ee2008-08-05 15:54:13 +10007796 if (mddev->ro) {
7797 /* Only thing we do on a ro array is remove
7798 * failed devices.
7799 */
NeilBrown3cb03002011-10-11 16:45:26 +11007800 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007801 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007802 if (rdev->raid_disk >= 0 &&
7803 !test_bit(Blocked, &rdev->flags) &&
7804 test_bit(Faulty, &rdev->flags) &&
7805 atomic_read(&rdev->nr_pending)==0) {
7806 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007807 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007808 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007809 rdev->raid_disk = -1;
7810 }
7811 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007812 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7813 goto unlock;
7814 }
7815
NeilBrown31a59e32008-04-30 00:52:30 -07007816 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007817 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007818 spin_lock_irq(&mddev->write_lock);
7819 if (mddev->safemode &&
7820 !atomic_read(&mddev->writes_pending) &&
7821 !mddev->in_sync &&
7822 mddev->recovery_cp == MaxSector) {
7823 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007824 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007825 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007826 }
7827 if (mddev->safemode == 1)
7828 mddev->safemode = 0;
7829 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007830 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007831 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007832 }
NeilBrownfca4d842005-06-21 17:17:11 -07007833
NeilBrown850b2b422006-10-03 01:15:46 -07007834 if (mddev->flags)
7835 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007836
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7838 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7839 /* resync/recovery still happening */
7840 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7841 goto unlock;
7842 }
7843 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007844 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 goto unlock;
7846 }
Neil Brown72a23c22008-06-28 08:31:41 +10007847 /* Set RUNNING before clearing NEEDED to avoid
7848 * any transients in the value of "sync_action".
7849 */
NeilBrown72f36d52012-10-11 14:25:57 +11007850 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007851 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007852 /* Clear some bits that don't mean anything, but
7853 * might be left set
7854 */
NeilBrown24dd4692005-11-08 21:39:26 -08007855 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7856 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
NeilBrowned209582012-04-24 10:23:14 +10007858 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7859 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007860 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861 /* no recovery is running.
7862 * remove any failed drives, then
7863 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007864 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 * the personality to fail the re-add.
7866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007868 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007869 if (mddev->pers->check_reshape == NULL ||
7870 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007871 /* Cannot proceed */
7872 goto unlock;
7873 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007874 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007875 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007876 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7877 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007878 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007879 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007880 } else if (mddev->recovery_cp < MaxSector) {
7881 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007882 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007883 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7884 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007886
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007888 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007889 /* We are adding a device or devices to an array
7890 * which has the bitmap stored on all devices.
7891 * So make sure all bitmap pages get written
7892 */
7893 bitmap_write_all(mddev->bitmap);
7894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 mddev->sync_thread = md_register_thread(md_do_sync,
7896 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007897 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 if (!mddev->sync_thread) {
7899 printk(KERN_ERR "%s: could not start resync"
7900 " thread...\n",
7901 mdname(mddev));
7902 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007903 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7904 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7905 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7906 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7907 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007908 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007910 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007911 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912 }
7913 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007914 if (!mddev->sync_thread) {
7915 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7916 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7917 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007918 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007919 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 mddev_unlock(mddev);
7922 }
7923}
7924
NeilBrownfd01b882011-10-11 16:47:53 +11007925void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007926{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007927 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007928 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007929 !test_bit(Blocked, &rdev->flags) &&
7930 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007931 msecs_to_jiffies(5000));
7932 rdev_dec_pending(rdev, mddev);
7933}
7934EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7935
NeilBrownc6563a82012-05-21 09:27:00 +10007936void md_finish_reshape(struct mddev *mddev)
7937{
7938 /* called be personality module when reshape completes. */
7939 struct md_rdev *rdev;
7940
7941 rdev_for_each(rdev, mddev) {
7942 if (rdev->data_offset > rdev->new_data_offset)
7943 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7944 else
7945 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7946 rdev->data_offset = rdev->new_data_offset;
7947 }
7948}
7949EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007950
7951/* Bad block management.
7952 * We can record which blocks on each device are 'bad' and so just
7953 * fail those blocks, or that stripe, rather than the whole device.
7954 * Entries in the bad-block table are 64bits wide. This comprises:
7955 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7956 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7957 * A 'shift' can be set so that larger blocks are tracked and
7958 * consequently larger devices can be covered.
7959 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7960 *
7961 * Locking of the bad-block table uses a seqlock so md_is_badblock
7962 * might need to retry if it is very unlucky.
7963 * We will sometimes want to check for bad blocks in a bi_end_io function,
7964 * so we use the write_seqlock_irq variant.
7965 *
7966 * When looking for a bad block we specify a range and want to
7967 * know if any block in the range is bad. So we binary-search
7968 * to the last range that starts at-or-before the given endpoint,
7969 * (or "before the sector after the target range")
7970 * then see if it ends after the given start.
7971 * We return
7972 * 0 if there are no known bad blocks in the range
7973 * 1 if there are known bad block which are all acknowledged
7974 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7975 * plus the start/length of the first bad section we overlap.
7976 */
7977int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7978 sector_t *first_bad, int *bad_sectors)
7979{
7980 int hi;
majianpengab056132012-11-06 17:13:44 +08007981 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007982 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08007983 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10007984 sector_t target = s + sectors;
7985 unsigned seq;
7986
7987 if (bb->shift > 0) {
7988 /* round the start down, and the end up */
7989 s >>= bb->shift;
7990 target += (1<<bb->shift) - 1;
7991 target >>= bb->shift;
7992 sectors = target - s;
7993 }
7994 /* 'target' is now the first block after the bad range */
7995
7996retry:
7997 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08007998 lo = 0;
7999 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008000 hi = bb->count;
8001
8002 /* Binary search between lo and hi for 'target'
8003 * i.e. for the last range that starts before 'target'
8004 */
8005 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8006 * are known not to be the last range before target.
8007 * VARIANT: hi-lo is the number of possible
8008 * ranges, and decreases until it reaches 1
8009 */
8010 while (hi - lo > 1) {
8011 int mid = (lo + hi) / 2;
8012 sector_t a = BB_OFFSET(p[mid]);
8013 if (a < target)
8014 /* This could still be the one, earlier ranges
8015 * could not. */
8016 lo = mid;
8017 else
8018 /* This and later ranges are definitely out. */
8019 hi = mid;
8020 }
8021 /* 'lo' might be the last that started before target, but 'hi' isn't */
8022 if (hi > lo) {
8023 /* need to check all range that end after 's' to see if
8024 * any are unacknowledged.
8025 */
8026 while (lo >= 0 &&
8027 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8028 if (BB_OFFSET(p[lo]) < target) {
8029 /* starts before the end, and finishes after
8030 * the start, so they must overlap
8031 */
8032 if (rv != -1 && BB_ACK(p[lo]))
8033 rv = 1;
8034 else
8035 rv = -1;
8036 *first_bad = BB_OFFSET(p[lo]);
8037 *bad_sectors = BB_LEN(p[lo]);
8038 }
8039 lo--;
8040 }
8041 }
8042
8043 if (read_seqretry(&bb->lock, seq))
8044 goto retry;
8045
8046 return rv;
8047}
8048EXPORT_SYMBOL_GPL(md_is_badblock);
8049
8050/*
8051 * Add a range of bad blocks to the table.
8052 * This might extend the table, or might contract it
8053 * if two adjacent ranges can be merged.
8054 * We binary-search to find the 'insertion' point, then
8055 * decide how best to handle it.
8056 */
8057static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8058 int acknowledged)
8059{
8060 u64 *p;
8061 int lo, hi;
8062 int rv = 1;
8063
8064 if (bb->shift < 0)
8065 /* badblocks are disabled */
8066 return 0;
8067
8068 if (bb->shift) {
8069 /* round the start down, and the end up */
8070 sector_t next = s + sectors;
8071 s >>= bb->shift;
8072 next += (1<<bb->shift) - 1;
8073 next >>= bb->shift;
8074 sectors = next - s;
8075 }
8076
8077 write_seqlock_irq(&bb->lock);
8078
8079 p = bb->page;
8080 lo = 0;
8081 hi = bb->count;
8082 /* Find the last range that starts at-or-before 's' */
8083 while (hi - lo > 1) {
8084 int mid = (lo + hi) / 2;
8085 sector_t a = BB_OFFSET(p[mid]);
8086 if (a <= s)
8087 lo = mid;
8088 else
8089 hi = mid;
8090 }
8091 if (hi > lo && BB_OFFSET(p[lo]) > s)
8092 hi = lo;
8093
8094 if (hi > lo) {
8095 /* we found a range that might merge with the start
8096 * of our new range
8097 */
8098 sector_t a = BB_OFFSET(p[lo]);
8099 sector_t e = a + BB_LEN(p[lo]);
8100 int ack = BB_ACK(p[lo]);
8101 if (e >= s) {
8102 /* Yes, we can merge with a previous range */
8103 if (s == a && s + sectors >= e)
8104 /* new range covers old */
8105 ack = acknowledged;
8106 else
8107 ack = ack && acknowledged;
8108
8109 if (e < s + sectors)
8110 e = s + sectors;
8111 if (e - a <= BB_MAX_LEN) {
8112 p[lo] = BB_MAKE(a, e-a, ack);
8113 s = e;
8114 } else {
8115 /* does not all fit in one range,
8116 * make p[lo] maximal
8117 */
8118 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8119 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8120 s = a + BB_MAX_LEN;
8121 }
8122 sectors = e - s;
8123 }
8124 }
8125 if (sectors && hi < bb->count) {
8126 /* 'hi' points to the first range that starts after 's'.
8127 * Maybe we can merge with the start of that range */
8128 sector_t a = BB_OFFSET(p[hi]);
8129 sector_t e = a + BB_LEN(p[hi]);
8130 int ack = BB_ACK(p[hi]);
8131 if (a <= s + sectors) {
8132 /* merging is possible */
8133 if (e <= s + sectors) {
8134 /* full overlap */
8135 e = s + sectors;
8136 ack = acknowledged;
8137 } else
8138 ack = ack && acknowledged;
8139
8140 a = s;
8141 if (e - a <= BB_MAX_LEN) {
8142 p[hi] = BB_MAKE(a, e-a, ack);
8143 s = e;
8144 } else {
8145 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8146 s = a + BB_MAX_LEN;
8147 }
8148 sectors = e - s;
8149 lo = hi;
8150 hi++;
8151 }
8152 }
8153 if (sectors == 0 && hi < bb->count) {
8154 /* we might be able to combine lo and hi */
8155 /* Note: 's' is at the end of 'lo' */
8156 sector_t a = BB_OFFSET(p[hi]);
8157 int lolen = BB_LEN(p[lo]);
8158 int hilen = BB_LEN(p[hi]);
8159 int newlen = lolen + hilen - (s - a);
8160 if (s >= a && newlen < BB_MAX_LEN) {
8161 /* yes, we can combine them */
8162 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8163 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8164 memmove(p + hi, p + hi + 1,
8165 (bb->count - hi - 1) * 8);
8166 bb->count--;
8167 }
8168 }
8169 while (sectors) {
8170 /* didn't merge (it all).
8171 * Need to add a range just before 'hi' */
8172 if (bb->count >= MD_MAX_BADBLOCKS) {
8173 /* No room for more */
8174 rv = 0;
8175 break;
8176 } else {
8177 int this_sectors = sectors;
8178 memmove(p + hi + 1, p + hi,
8179 (bb->count - hi) * 8);
8180 bb->count++;
8181
8182 if (this_sectors > BB_MAX_LEN)
8183 this_sectors = BB_MAX_LEN;
8184 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8185 sectors -= this_sectors;
8186 s += this_sectors;
8187 }
8188 }
8189
8190 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008191 if (!acknowledged)
8192 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008193 write_sequnlock_irq(&bb->lock);
8194
8195 return rv;
8196}
8197
NeilBrown3cb03002011-10-11 16:45:26 +11008198int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008199 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008200{
NeilBrownc6563a82012-05-21 09:27:00 +10008201 int rv;
8202 if (is_new)
8203 s += rdev->new_data_offset;
8204 else
8205 s += rdev->data_offset;
8206 rv = md_set_badblocks(&rdev->badblocks,
8207 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008208 if (rv) {
8209 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008210 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008211 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8212 md_wakeup_thread(rdev->mddev->thread);
8213 }
8214 return rv;
8215}
8216EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8217
8218/*
8219 * Remove a range of bad blocks from the table.
8220 * This may involve extending the table if we spilt a region,
8221 * but it must not fail. So if the table becomes full, we just
8222 * drop the remove request.
8223 */
8224static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8225{
8226 u64 *p;
8227 int lo, hi;
8228 sector_t target = s + sectors;
8229 int rv = 0;
8230
8231 if (bb->shift > 0) {
8232 /* When clearing we round the start up and the end down.
8233 * This should not matter as the shift should align with
8234 * the block size and no rounding should ever be needed.
8235 * However it is better the think a block is bad when it
8236 * isn't than to think a block is not bad when it is.
8237 */
8238 s += (1<<bb->shift) - 1;
8239 s >>= bb->shift;
8240 target >>= bb->shift;
8241 sectors = target - s;
8242 }
8243
8244 write_seqlock_irq(&bb->lock);
8245
8246 p = bb->page;
8247 lo = 0;
8248 hi = bb->count;
8249 /* Find the last range that starts before 'target' */
8250 while (hi - lo > 1) {
8251 int mid = (lo + hi) / 2;
8252 sector_t a = BB_OFFSET(p[mid]);
8253 if (a < target)
8254 lo = mid;
8255 else
8256 hi = mid;
8257 }
8258 if (hi > lo) {
8259 /* p[lo] is the last range that could overlap the
8260 * current range. Earlier ranges could also overlap,
8261 * but only this one can overlap the end of the range.
8262 */
8263 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8264 /* Partial overlap, leave the tail of this range */
8265 int ack = BB_ACK(p[lo]);
8266 sector_t a = BB_OFFSET(p[lo]);
8267 sector_t end = a + BB_LEN(p[lo]);
8268
8269 if (a < s) {
8270 /* we need to split this range */
8271 if (bb->count >= MD_MAX_BADBLOCKS) {
8272 rv = 0;
8273 goto out;
8274 }
8275 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8276 bb->count++;
8277 p[lo] = BB_MAKE(a, s-a, ack);
8278 lo++;
8279 }
8280 p[lo] = BB_MAKE(target, end - target, ack);
8281 /* there is no longer an overlap */
8282 hi = lo;
8283 lo--;
8284 }
8285 while (lo >= 0 &&
8286 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8287 /* This range does overlap */
8288 if (BB_OFFSET(p[lo]) < s) {
8289 /* Keep the early parts of this range. */
8290 int ack = BB_ACK(p[lo]);
8291 sector_t start = BB_OFFSET(p[lo]);
8292 p[lo] = BB_MAKE(start, s - start, ack);
8293 /* now low doesn't overlap, so.. */
8294 break;
8295 }
8296 lo--;
8297 }
8298 /* 'lo' is strictly before, 'hi' is strictly after,
8299 * anything between needs to be discarded
8300 */
8301 if (hi - lo > 1) {
8302 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8303 bb->count -= (hi - lo - 1);
8304 }
8305 }
8306
8307 bb->changed = 1;
8308out:
8309 write_sequnlock_irq(&bb->lock);
8310 return rv;
8311}
8312
NeilBrownc6563a82012-05-21 09:27:00 +10008313int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8314 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008315{
NeilBrownc6563a82012-05-21 09:27:00 +10008316 if (is_new)
8317 s += rdev->new_data_offset;
8318 else
8319 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008320 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008321 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008322}
8323EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8324
8325/*
8326 * Acknowledge all bad blocks in a list.
8327 * This only succeeds if ->changed is clear. It is used by
8328 * in-kernel metadata updates
8329 */
8330void md_ack_all_badblocks(struct badblocks *bb)
8331{
8332 if (bb->page == NULL || bb->changed)
8333 /* no point even trying */
8334 return;
8335 write_seqlock_irq(&bb->lock);
8336
majianpengecb178b2012-03-19 12:46:42 +11008337 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008338 u64 *p = bb->page;
8339 int i;
8340 for (i = 0; i < bb->count ; i++) {
8341 if (!BB_ACK(p[i])) {
8342 sector_t start = BB_OFFSET(p[i]);
8343 int len = BB_LEN(p[i]);
8344 p[i] = BB_MAKE(start, len, 1);
8345 }
8346 }
NeilBrownde393cd2011-07-28 11:31:48 +10008347 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008348 }
8349 write_sequnlock_irq(&bb->lock);
8350}
8351EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8352
NeilBrown16c791a2011-07-28 11:31:47 +10008353/* sysfs access to bad-blocks list.
8354 * We present two files.
8355 * 'bad-blocks' lists sector numbers and lengths of ranges that
8356 * are recorded as bad. The list is truncated to fit within
8357 * the one-page limit of sysfs.
8358 * Writing "sector length" to this file adds an acknowledged
8359 * bad block list.
8360 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8361 * been acknowledged. Writing to this file adds bad blocks
8362 * without acknowledging them. This is largely for testing.
8363 */
8364
8365static ssize_t
8366badblocks_show(struct badblocks *bb, char *page, int unack)
8367{
8368 size_t len;
8369 int i;
8370 u64 *p = bb->page;
8371 unsigned seq;
8372
8373 if (bb->shift < 0)
8374 return 0;
8375
8376retry:
8377 seq = read_seqbegin(&bb->lock);
8378
8379 len = 0;
8380 i = 0;
8381
8382 while (len < PAGE_SIZE && i < bb->count) {
8383 sector_t s = BB_OFFSET(p[i]);
8384 unsigned int length = BB_LEN(p[i]);
8385 int ack = BB_ACK(p[i]);
8386 i++;
8387
8388 if (unack && ack)
8389 continue;
8390
8391 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8392 (unsigned long long)s << bb->shift,
8393 length << bb->shift);
8394 }
NeilBrownde393cd2011-07-28 11:31:48 +10008395 if (unack && len == 0)
8396 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008397
8398 if (read_seqretry(&bb->lock, seq))
8399 goto retry;
8400
8401 return len;
8402}
8403
8404#define DO_DEBUG 1
8405
8406static ssize_t
8407badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8408{
8409 unsigned long long sector;
8410 int length;
8411 char newline;
8412#ifdef DO_DEBUG
8413 /* Allow clearing via sysfs *only* for testing/debugging.
8414 * Normally only a successful write may clear a badblock
8415 */
8416 int clear = 0;
8417 if (page[0] == '-') {
8418 clear = 1;
8419 page++;
8420 }
8421#endif /* DO_DEBUG */
8422
8423 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8424 case 3:
8425 if (newline != '\n')
8426 return -EINVAL;
8427 case 2:
8428 if (length <= 0)
8429 return -EINVAL;
8430 break;
8431 default:
8432 return -EINVAL;
8433 }
8434
8435#ifdef DO_DEBUG
8436 if (clear) {
8437 md_clear_badblocks(bb, sector, length);
8438 return len;
8439 }
8440#endif /* DO_DEBUG */
8441 if (md_set_badblocks(bb, sector, length, !unack))
8442 return len;
8443 else
8444 return -ENOSPC;
8445}
8446
Adrian Bunk75c96f82005-05-05 16:16:09 -07008447static int md_notify_reboot(struct notifier_block *this,
8448 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449{
8450 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008451 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008452 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453
NeilBrownc744a652012-03-19 12:46:37 +11008454 for_each_mddev(mddev, tmp) {
8455 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008456 if (mddev->pers)
8457 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008458 mddev->safemode = 2;
8459 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008460 }
NeilBrownc744a652012-03-19 12:46:37 +11008461 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 }
NeilBrownc744a652012-03-19 12:46:37 +11008463 /*
8464 * certain more exotic SCSI devices are known to be
8465 * volatile wrt too early system reboots. While the
8466 * right place to handle this issue is the given
8467 * driver, we do want to have a safe RAID driver ...
8468 */
8469 if (need_delay)
8470 mdelay(1000*1);
8471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 return NOTIFY_DONE;
8473}
8474
Adrian Bunk75c96f82005-05-05 16:16:09 -07008475static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 .notifier_call = md_notify_reboot,
8477 .next = NULL,
8478 .priority = INT_MAX, /* before any real devices */
8479};
8480
8481static void md_geninit(void)
8482{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008483 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008485 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486}
8487
Adrian Bunk75c96f82005-05-05 16:16:09 -07008488static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489{
Tejun Heoe804ac72010-10-15 15:36:08 +02008490 int ret = -ENOMEM;
8491
Tejun Heoada609e2011-01-25 14:35:54 +01008492 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008493 if (!md_wq)
8494 goto err_wq;
8495
8496 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8497 if (!md_misc_wq)
8498 goto err_misc_wq;
8499
8500 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8501 goto err_md;
8502
8503 if ((ret = register_blkdev(0, "mdp")) < 0)
8504 goto err_mdp;
8505 mdp_major = ret;
8506
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008507 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008508 md_probe, NULL, NULL);
8509 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 md_probe, NULL, NULL);
8511
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008513 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514
8515 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517
Tejun Heoe804ac72010-10-15 15:36:08 +02008518err_mdp:
8519 unregister_blkdev(MD_MAJOR, "md");
8520err_md:
8521 destroy_workqueue(md_misc_wq);
8522err_misc_wq:
8523 destroy_workqueue(md_wq);
8524err_wq:
8525 return ret;
8526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527
8528#ifndef MODULE
8529
8530/*
8531 * Searches all registered partitions for autorun RAID arrays
8532 * at boot time.
8533 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008534
8535static LIST_HEAD(all_detected_devices);
8536struct detected_devices_node {
8537 struct list_head list;
8538 dev_t dev;
8539};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540
8541void md_autodetect_dev(dev_t dev)
8542{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008543 struct detected_devices_node *node_detected_dev;
8544
8545 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8546 if (node_detected_dev) {
8547 node_detected_dev->dev = dev;
8548 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8549 } else {
8550 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8551 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553}
8554
8555
8556static void autostart_arrays(int part)
8557{
NeilBrown3cb03002011-10-11 16:45:26 +11008558 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008559 struct detected_devices_node *node_detected_dev;
8560 dev_t dev;
8561 int i_scanned, i_passed;
8562
8563 i_scanned = 0;
8564 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565
8566 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8567
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008568 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8569 i_scanned++;
8570 node_detected_dev = list_entry(all_detected_devices.next,
8571 struct detected_devices_node, list);
8572 list_del(&node_detected_dev->list);
8573 dev = node_detected_dev->dev;
8574 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008575 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 if (IS_ERR(rdev))
8577 continue;
8578
NeilBrownb2d444d2005-11-08 21:39:31 -08008579 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580 MD_BUG();
8581 continue;
8582 }
NeilBrownd0fae182008-03-04 14:29:31 -08008583 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008585 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008587
8588 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8589 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
8591 autorun_devices(part);
8592}
8593
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008594#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596static __exit void md_exit(void)
8597{
NeilBrownfd01b882011-10-11 16:47:53 +11008598 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008600
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008601 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008602 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008604 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 unregister_blkdev(mdp_major, "mdp");
8606 unregister_reboot_notifier(&md_notifier);
8607 unregister_sysctl_table(raid_table_header);
8608 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008609 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008611 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008613 destroy_workqueue(md_misc_wq);
8614 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615}
8616
Dan Williams685784a2007-07-09 11:56:42 -07008617subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618module_exit(md_exit)
8619
NeilBrownf91de922005-11-08 21:39:36 -08008620static int get_ro(char *buffer, struct kernel_param *kp)
8621{
8622 return sprintf(buffer, "%d", start_readonly);
8623}
8624static int set_ro(const char *val, struct kernel_param *kp)
8625{
8626 char *e;
8627 int num = simple_strtoul(val, &e, 10);
8628 if (*val && (*e == '\0' || *e == '\n')) {
8629 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008630 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008631 }
8632 return -EINVAL;
8633}
8634
NeilBrown80ca3a42006-07-10 04:44:18 -07008635module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8636module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008637
NeilBrownefeb53c2009-01-09 08:31:10 +11008638module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640EXPORT_SYMBOL(register_md_personality);
8641EXPORT_SYMBOL(unregister_md_personality);
8642EXPORT_SYMBOL(md_error);
8643EXPORT_SYMBOL(md_done_sync);
8644EXPORT_SYMBOL(md_write_start);
8645EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646EXPORT_SYMBOL(md_register_thread);
8647EXPORT_SYMBOL(md_unregister_thread);
8648EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649EXPORT_SYMBOL(md_check_recovery);
8650MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008651MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008652MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008653MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);