blob: 739b1ec54e286083b19d9643dc756f5ad2909605 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb482011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 /* an empty barrier - all done */
398 bio_endio(bio, 0);
399 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200401 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 }
NeilBrown2b74e122010-12-09 15:59:01 +1100403
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
NeilBrownfd01b882011-10-11 16:47:53 +1100408void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100413 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 spin_unlock_irq(&mddev->write_lock);
416
NeilBrowna035fc32010-12-09 16:17:51 +1100417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
Tejun Heoe9c74692010-09-03 11:56:18 +0200420EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100421
NeilBrown74018dc2012-07-31 09:08:15 +0200422void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000423{
NeilBrown9cbb1752012-07-31 09:08:14 +0200424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
426 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000427}
NeilBrown9cbb1752012-07-31 09:08:14 +0200428EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000429
NeilBrownfd01b882011-10-11 16:47:53 +1100430static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 atomic_inc(&mddev->active);
433 return mddev;
434}
435
Dan Williams5fd3a172009-03-04 00:57:25 -0700436static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100437
NeilBrownfd01b882011-10-11 16:47:53 +1100438static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
NeilBrowna167f662010-10-26 18:31:13 +1100440 struct bio_set *bs = NULL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443 return;
NeilBrownd3374822009-01-09 08:31:10 +1100444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
447 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100448 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100449 bs = mddev->bio_set;
450 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100451 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100456 */
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200458 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100459 } else
460 kfree(mddev);
461 }
462 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100463 if (bs)
464 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrownfd01b882011-10-11 16:47:53 +1100467void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000483 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500484 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
488}
NeilBrown390ee602010-06-01 19:37:27 +1000489EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490
NeilBrownfd01b882011-10-11 16:47:53 +1100491static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownfd01b882011-10-11 16:47:53 +1100493 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown8f5f02c2011-02-16 13:58:51 +1100495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retry:
499 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrownefeb53c2009-01-09 08:31:10 +1100501 if (unit) {
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
504 mddev_get(mddev);
505 spin_unlock(&all_mddevs_lock);
506 kfree(new);
507 return mddev;
508 }
509
510 if (new) {
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
514 return new;
515 }
516 } else if (new) {
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
520 int is_free = 0;
521 int dev = 0;
522 while (!is_free) {
523 dev = MKDEV(MD_MAJOR, next_minor);
524 next_minor++;
525 if (next_minor > MINORMASK)
526 next_minor = 0;
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return NULL;
532 }
533
534 is_free = 1;
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
537 is_free = 0;
538 break;
539 }
540 }
541 new->unit = dev;
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 return new;
547 }
548 spin_unlock(&all_mddevs_lock);
549
NeilBrown9ffae0c2006-01-06 00:20:32 -0800550 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!new)
552 return NULL;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new->unit = unit;
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
557 else
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
NeilBrownfafd7fb2010-04-01 15:55:30 +1100560 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto retry;
563}
564
NeilBrown29f097c2013-11-14 17:54:51 +1100565static inline int __must_check mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
NeilBrown29f097c2013-11-14 17:54:51 +1100570/* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
572 */
573static inline void mddev_lock_nointr(struct mddev * mddev)
574{
575 mutex_lock(&mddev->reconfig_mutex);
576}
577
NeilBrownfd01b882011-10-11 16:47:53 +1100578static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100579{
580 return mutex_is_locked(&mddev->reconfig_mutex);
581}
582
NeilBrownfd01b882011-10-11 16:47:53 +1100583static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800585 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrownb6eb1272010-04-15 10:13:47 +1000588static struct attribute_group md_redundancy_group;
589
NeilBrownfd01b882011-10-11 16:47:53 +1100590static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
NeilBrowna64c8762010-04-14 17:15:37 +1000592 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
596 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
603 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 */
NeilBrowna64c8762010-04-14 17:15:37 +1000605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 mutex_unlock(&mddev->reconfig_mutex);
609
NeilBrown00bcb4a2010-06-01 19:37:23 +1000610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
619 }
NeilBrowna64c8762010-04-14 17:15:37 +1000620 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000621 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000622 } else
623 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chris Dunlop751e67c2011-10-19 16:48:26 +1100625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000627 */
628 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700629 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000630 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrownfd01b882011-10-11 16:47:53 +1100633static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
NeilBrown3cb03002011-10-11 16:45:26 +1100635 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
NeilBrowndafb20f2012-03-19 12:46:39 +1100637 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (rdev->desc_nr == nr)
639 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return NULL;
642}
643
NeilBrown1ca69c42012-10-11 13:37:33 +1100644static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
645{
646 struct md_rdev *rdev;
647
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
650 return rdev;
651
652 return NULL;
653}
654
655static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
NeilBrown3cb03002011-10-11 16:45:26 +1100657 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
NeilBrowndafb20f2012-03-19 12:46:39 +1100659 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return NULL;
664}
665
NeilBrown1ca69c42012-10-11 13:37:33 +1100666static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
667{
668 struct md_rdev *rdev;
669
670 rdev_for_each_rcu(rdev, mddev)
671 if (rdev->bdev->bd_dev == dev)
672 return rdev;
673
674 return NULL;
675}
676
NeilBrown84fc4b52011-10-11 16:49:58 +1100677static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800678{
NeilBrown84fc4b52011-10-11 16:49:58 +1100679 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800680 list_for_each_entry(pers, &pers_list, list) {
681 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800682 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800683 if (strcmp(pers->name, clevel)==0)
684 return pers;
685 }
NeilBrown2604b702006-01-06 00:20:36 -0800686 return NULL;
687}
688
Andre Nollb73df2d2008-07-11 22:02:23 +1000689/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100690static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100692 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000693 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrown3cb03002011-10-11 16:45:26 +1100696static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 if (rdev->sb_page)
699 MD_BUG();
700
701 rdev->sb_page = alloc_page(GFP_KERNEL);
702 if (!rdev->sb_page) {
703 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000704 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 return 0;
708}
709
NeilBrown545c8792012-05-22 13:54:30 +1000710void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800713 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 rdev->sb_loaded = 0;
715 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000716 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100717 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
NeilBrown2699b672011-07-28 11:31:47 +1000719 if (rdev->bb_page) {
720 put_page(rdev->bb_page);
721 rdev->bb_page = NULL;
722 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000723 kfree(rdev->badblocks.page);
724 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
NeilBrown545c8792012-05-22 13:54:30 +1000726EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
NeilBrown6712ecf2007-09-27 12:47:43 +0200728static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700729{
NeilBrown3cb03002011-10-11 16:45:26 +1100730 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100731 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700733 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
734 printk("md: super_written gets error=%d, uptodate=%d\n",
735 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
736 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800737 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700738 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739
NeilBrowna9701a32005-11-08 21:39:34 -0800740 if (atomic_dec_and_test(&mddev->pending_writes))
741 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700742 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743}
744
NeilBrownfd01b882011-10-11 16:47:53 +1100745void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 sector_t sector, int size, struct page *page)
747{
748 /* write first size bytes of page to sector of rdev
749 * Increment mddev->pending_writes before returning
750 * and decrement it on completion, waking up sb_wait
751 * if zero is reached.
752 * If an error occurred, call md_error
753 */
NeilBrowna167f662010-10-26 18:31:13 +1100754 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100756 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757 bio->bi_sector = sector;
758 bio_add_page(bio, page, size, 0);
759 bio->bi_private = rdev;
760 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800761
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000763 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800764}
765
NeilBrownfd01b882011-10-11 16:47:53 +1100766void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800767{
Tejun Heoe9c74692010-09-03 11:56:18 +0200768 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800769 DEFINE_WAIT(wq);
770 for(;;) {
771 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
772 if (atomic_read(&mddev->pending_writes)==0)
773 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800774 schedule();
775 }
776 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777}
778
NeilBrown3cb03002011-10-11 16:45:26 +1100779int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
NeilBrowna167f662010-10-26 18:31:13 +1100782 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int ret;
784
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100785 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
786 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100787 if (metadata_op)
788 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000789 else if (rdev->mddev->reshape_position != MaxSector &&
790 (rdev->mddev->reshape_backwards ==
791 (sector >= rdev->mddev->reshape_position)))
792 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100793 else
794 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 bio_add_page(bio, page, size, 0);
Kent Overstreet33879d42013-11-23 22:33:32 -0800796 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
799 bio_put(bio);
800 return ret;
801}
NeilBrowna8745db2006-01-06 00:20:34 -0800802EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
NeilBrown3cb03002011-10-11 16:45:26 +1100804static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 char b[BDEVNAME_SIZE];
807 if (!rdev->sb_page) {
808 MD_BUG();
809 return -EINVAL;
810 }
811 if (rdev->sb_loaded)
812 return 0;
813
814
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100815 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto fail;
817 rdev->sb_loaded = 1;
818 return 0;
819
820fail:
821 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
822 bdevname(rdev->bdev,b));
823 return -EINVAL;
824}
825
826static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827{
Andre Noll05710462008-07-11 22:02:20 +1000828 return sb1->set_uuid0 == sb2->set_uuid0 &&
829 sb1->set_uuid1 == sb2->set_uuid1 &&
830 sb1->set_uuid2 == sb2->set_uuid2 &&
831 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
835{
836 int ret;
837 mdp_super_t *tmp1, *tmp2;
838
839 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
840 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
841
842 if (!tmp1 || !tmp2) {
843 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100844 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto abort;
846 }
847
848 *tmp1 = *sb1;
849 *tmp2 = *sb2;
850
851 /*
852 * nr_disks is not constant
853 */
854 tmp1->nr_disks = 0;
855 tmp2->nr_disks = 0;
856
Andre Nollce0c8e02008-07-11 22:02:20 +1000857 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700859 kfree(tmp1);
860 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return ret;
862}
863
NeilBrown4d167f02007-05-09 02:35:37 -0700864
865static u32 md_csum_fold(u32 csum)
866{
867 csum = (csum & 0xffff) + (csum >> 16);
868 return (csum & 0xffff) + (csum >> 16);
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static unsigned int calc_sb_csum(mdp_super_t * sb)
872{
NeilBrown4d167f02007-05-09 02:35:37 -0700873 u64 newcsum = 0;
874 u32 *sb32 = (u32*)sb;
875 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int disk_csum, csum;
877
878 disk_csum = sb->sb_csum;
879 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700880
881 for (i = 0; i < MD_SB_BYTES/4 ; i++)
882 newcsum += sb32[i];
883 csum = (newcsum & 0xffffffff) + (newcsum>>32);
884
885
886#ifdef CONFIG_ALPHA
887 /* This used to use csum_partial, which was wrong for several
888 * reasons including that different results are returned on
889 * different architectures. It isn't critical that we get exactly
890 * the same return value as before (we always csum_fold before
891 * testing, and that removes any differences). However as we
892 * know that csum_partial always returned a 16bit value on
893 * alphas, do a fold to maximise conformity to previous behaviour.
894 */
895 sb->sb_csum = md_csum_fold(disk_csum);
896#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return csum;
900}
901
902
903/*
904 * Handle superblock details.
905 * We want to be able to handle multiple superblock formats
906 * so we have a common interface to them all, and an array of
907 * different handlers.
908 * We rely on user-space to write the initial superblock, and support
909 * reading and updating of superblocks.
910 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100911 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * loads and validates a superblock on dev.
913 * if refdev != NULL, compare superblocks on both devices
914 * Return:
915 * 0 - dev has a superblock that is compatible with refdev
916 * 1 - dev has a superblock that is compatible and newer than refdev
917 * so dev should be used as the refdev in future
918 * -EINVAL superblock incompatible or invalid
919 * -othererror e.g. -EIO
920 *
NeilBrownfd01b882011-10-11 16:47:53 +1100921 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Verify that dev is acceptable into mddev.
923 * The first time, mddev->raid_disks will be 0, and data from
924 * dev should be merged in. Subsequent calls check that dev
925 * is new enough. Return 0 or -EINVAL
926 *
NeilBrownfd01b882011-10-11 16:47:53 +1100927 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * Update the superblock for rdev with data in mddev
929 * This does not write to disc.
930 *
931 */
932
933struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000934 char *name;
935 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000936 int (*load_super)(struct md_rdev *rdev,
937 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000938 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000939 int (*validate_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
941 void (*sync_super)(struct mddev *mddev,
942 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100943 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000944 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000945 int (*allow_new_offset)(struct md_rdev *rdev,
946 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947};
948
949/*
Andre Noll0894cc32009-06-18 08:49:23 +1000950 * Check that the given mddev has no bitmap.
951 *
952 * This function is called from the run method of all personalities that do not
953 * support bitmaps. It prints an error message and returns non-zero if mddev
954 * has a bitmap. Otherwise, it returns 0.
955 *
956 */
NeilBrownfd01b882011-10-11 16:47:53 +1100957int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000958{
NeilBrownc3d97142009-12-14 12:49:52 +1100959 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000960 return 0;
961 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
962 mdname(mddev), mddev->pers->name);
963 return 1;
964}
965EXPORT_SYMBOL(md_check_no_bitmap);
966
967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * load_super for 0.90.0
969 */
NeilBrown3cb03002011-10-11 16:45:26 +1100970static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
973 mdp_super_t *sb;
974 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /*
Andre Noll0f420352008-07-11 22:02:23 +1000977 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * it's at the end of the disk.
979 *
980 * It also happens to be a multiple of 4Kb.
981 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100982 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrown0002b272005-09-09 16:23:53 -0700984 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ret) return ret;
986
987 ret = -EINVAL;
988
989 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000990 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (sb->md_magic != MD_SB_MAGIC) {
993 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
994 b);
995 goto abort;
996 }
997
998 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800999 sb->minor_version < 90 ||
1000 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1002 sb->major_version, sb->minor_version,
1003 b);
1004 goto abort;
1005 }
1006
1007 if (sb->raid_disks <= 0)
1008 goto abort;
1009
NeilBrown4d167f02007-05-09 02:35:37 -07001010 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1012 b);
1013 goto abort;
1014 }
1015
1016 rdev->preferred_minor = sb->md_minor;
1017 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001018 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001019 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001020 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (sb->level == LEVEL_MULTIPATH)
1023 rdev->desc_nr = -1;
1024 else
1025 rdev->desc_nr = sb->this_disk.number;
1026
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001027 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001031 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!uuid_equal(refsb, sb)) {
1033 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1034 b, bdevname(refdev->bdev,b2));
1035 goto abort;
1036 }
1037 if (!sb_equal(refsb, sb)) {
1038 printk(KERN_WARNING "md: %s has same UUID"
1039 " but different superblock to %s\n",
1040 b, bdevname(refdev->bdev, b2));
1041 goto abort;
1042 }
1043 ev1 = md_event(sb);
1044 ev2 = md_event(refsb);
1045 if (ev1 > ev2)
1046 ret = 1;
1047 else
1048 ret = 0;
1049 }
NeilBrown8190e752009-06-18 08:48:58 +10001050 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001051 /* Limit to 4TB as metadata cannot record more than that.
1052 * (not needed for Linear and RAID0 as metadata doesn't
1053 * record this size)
1054 */
1055 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001056 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
NeilBrown27a7b262011-09-10 17:21:28 +10001058 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001059 /* "this cannot possibly happen" ... */
1060 ret = -EINVAL;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 abort:
1063 return ret;
1064}
1065
1066/*
1067 * validate_super for 0.90.0
1068 */
NeilBrownfd01b882011-10-11 16:47:53 +11001069static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001072 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001073 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
NeilBrown41158c72005-06-21 17:17:25 -07001075 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001076 clear_bit(Faulty, &rdev->flags);
1077 clear_bit(In_sync, &rdev->flags);
1078 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (mddev->raid_disks == 0) {
1081 mddev->major_version = 0;
1082 mddev->minor_version = sb->minor_version;
1083 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001084 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001085 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 mddev->ctime = sb->ctime;
1087 mddev->utime = sb->utime;
1088 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001089 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 mddev->layout = sb->layout;
1091 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001092 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001093 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001094 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001095 mddev->bitmap_info.space = 0;
1096 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001097 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001098 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001099 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
NeilBrownf6705572006-03-27 01:18:11 -08001101 if (mddev->minor_version >= 91) {
1102 mddev->reshape_position = sb->reshape_position;
1103 mddev->delta_disks = sb->delta_disks;
1104 mddev->new_level = sb->new_level;
1105 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001106 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001107 if (mddev->delta_disks < 0)
1108 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001109 } else {
1110 mddev->reshape_position = MaxSector;
1111 mddev->delta_disks = 0;
1112 mddev->new_level = mddev->level;
1113 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001114 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (sb->state & (1<<MD_SB_CLEAN))
1118 mddev->recovery_cp = MaxSector;
1119 else {
1120 if (sb->events_hi == sb->cp_events_hi &&
1121 sb->events_lo == sb->cp_events_lo) {
1122 mddev->recovery_cp = sb->recovery_cp;
1123 } else
1124 mddev->recovery_cp = 0;
1125 }
1126
1127 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1128 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1129 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1130 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1131
1132 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001133
1134 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001135 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001136 mddev->bitmap_info.offset =
1137 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001138 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001139 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001140 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001141
NeilBrown41158c72005-06-21 17:17:25 -07001142 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001143 /* Insist on good event counter while assembling, except
1144 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001146 if (sb->disks[rdev->desc_nr].state & (
1147 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1148 if (ev1 < mddev->events)
1149 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001150 } else if (mddev->bitmap) {
1151 /* if adding to array with a bitmap, then we can accept an
1152 * older device ... but not too old.
1153 */
NeilBrown41158c72005-06-21 17:17:25 -07001154 if (ev1 < mddev->bitmap->events_cleared)
1155 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001156 } else {
1157 if (ev1 < mddev->events)
1158 /* just a hot-add of a new device, leave raid_disk at -1 */
1159 return 0;
1160 }
NeilBrown41158c72005-06-21 17:17:25 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 desc = sb->disks + rdev->desc_nr;
1164
1165 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001166 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001167 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1168 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001171 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1172 /* active but not in sync implies recovery up to
1173 * reshape position. We don't know exactly where
1174 * that is, so set to zero for now */
1175 if (mddev->minor_version >= 91) {
1176 rdev->recovery_offset = 0;
1177 rdev->raid_disk = desc->raid_disk;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001180 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1181 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001182 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001183 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return 0;
1185}
1186
1187/*
1188 * sync_super for 0.90.0
1189 */
NeilBrownfd01b882011-10-11 16:47:53 +11001190static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001193 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* make rdev->sb match mddev data..
1198 *
1199 * 1/ zero out disks
1200 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1201 * 3/ any empty disks < next_spare become removed
1202 *
1203 * disks[0] gets initialised to REMOVED because
1204 * we cannot be sure from other fields if it has
1205 * been initialised or not.
1206 */
1207 int i;
1208 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1209
NeilBrown61181562005-09-09 16:24:02 -07001210 rdev->sb_size = MD_SB_BYTES;
1211
Namhyung Kim65a06f062011-07-27 11:00:36 +10001212 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 memset(sb, 0, sizeof(*sb));
1215
1216 sb->md_magic = MD_SB_MAGIC;
1217 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sb->patch_version = mddev->patch_version;
1219 sb->gvalid_words = 0; /* ignored */
1220 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1221 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1222 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1223 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1224
1225 sb->ctime = mddev->ctime;
1226 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001227 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 sb->raid_disks = mddev->raid_disks;
1229 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001230 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->utime = mddev->utime;
1232 sb->state = 0;
1233 sb->events_hi = (mddev->events>>32);
1234 sb->events_lo = (u32)mddev->events;
1235
NeilBrownf6705572006-03-27 01:18:11 -08001236 if (mddev->reshape_position == MaxSector)
1237 sb->minor_version = 90;
1238 else {
1239 sb->minor_version = 91;
1240 sb->reshape_position = mddev->reshape_position;
1241 sb->new_level = mddev->new_level;
1242 sb->delta_disks = mddev->delta_disks;
1243 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001244 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001245 }
1246 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (mddev->in_sync)
1248 {
1249 sb->recovery_cp = mddev->recovery_cp;
1250 sb->cp_events_hi = (mddev->events>>32);
1251 sb->cp_events_lo = (u32)mddev->events;
1252 if (mddev->recovery_cp == MaxSector)
1253 sb->state = (1<< MD_SB_CLEAN);
1254 } else
1255 sb->recovery_cp = 0;
1256
1257 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001258 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
NeilBrownc3d97142009-12-14 12:49:52 +11001260 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001261 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001264 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001267 int is_active = test_bit(In_sync, &rdev2->flags);
1268
1269 if (rdev2->raid_disk >= 0 &&
1270 sb->minor_version >= 91)
1271 /* we have nowhere to store the recovery_offset,
1272 * but if it is not below the reshape_position,
1273 * we can piggy-back on that.
1274 */
1275 is_active = 1;
1276 if (rdev2->raid_disk < 0 ||
1277 test_bit(Faulty, &rdev2->flags))
1278 is_active = 0;
1279 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001280 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001282 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001283 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 d = &sb->disks[rdev2->desc_nr];
1285 nr_disks++;
1286 d->number = rdev2->desc_nr;
1287 d->major = MAJOR(rdev2->bdev->bd_dev);
1288 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001289 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 d->raid_disk = rdev2->raid_disk;
1291 else
1292 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001293 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001295 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001297 if (test_bit(In_sync, &rdev2->flags))
1298 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 active++;
1300 working++;
1301 } else {
1302 d->state = 0;
1303 spare++;
1304 working++;
1305 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001306 if (test_bit(WriteMostly, &rdev2->flags))
1307 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* now set the "removed" and "faulty" bits on any missing devices */
1310 for (i=0 ; i < mddev->raid_disks ; i++) {
1311 mdp_disk_t *d = &sb->disks[i];
1312 if (d->state == 0 && d->number == 0) {
1313 d->number = i;
1314 d->raid_disk = i;
1315 d->state = (1<<MD_DISK_REMOVED);
1316 d->state |= (1<<MD_DISK_FAULTY);
1317 failed++;
1318 }
1319 }
1320 sb->nr_disks = nr_disks;
1321 sb->active_disks = active;
1322 sb->working_disks = working;
1323 sb->failed_disks = failed;
1324 sb->spare_disks = spare;
1325
1326 sb->this_disk = sb->disks[rdev->desc_nr];
1327 sb->sb_csum = calc_sb_csum(sb);
1328}
1329
1330/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331 * rdev_size_change for 0.90.0
1332 */
1333static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001334super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335{
Andre Noll58c0fed2009-03-31 14:33:13 +11001336 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001338 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001340 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001341 if (!num_sectors || num_sectors > rdev->sb_start)
1342 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001343 /* Limit to 4TB as metadata cannot record more than that.
1344 * 4TB == 2^32 KB, or 2*2^32 sectors.
1345 */
NeilBrown667a5312012-08-16 16:46:12 +10001346 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001347 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001348 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001349 rdev->sb_page);
1350 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001351 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352}
1353
NeilBrownc6563a82012-05-21 09:27:00 +10001354static int
1355super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1356{
1357 /* non-zero offset changes not possible with v0.90 */
1358 return new_offset == 0;
1359}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001360
1361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * version 1 superblock
1363 */
1364
NeilBrown1c05b4b2006-10-21 10:24:08 -07001365static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001367 __le32 disk_csum;
1368 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 unsigned long long newcsum;
1370 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001371 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 disk_csum = sb->sb_csum;
1374 sb->sb_csum = 0;
1375 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001376 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 newcsum += le32_to_cpu(*isuper++);
1378
1379 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001380 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1383 sb->sb_csum = disk_csum;
1384 return cpu_to_le32(csum);
1385}
1386
NeilBrown2699b672011-07-28 11:31:47 +10001387static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1388 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001389static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct mdp_superblock_1 *sb;
1392 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001393 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001394 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001396 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
Andre Noll0f420352008-07-11 22:02:23 +10001399 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * It is always aligned to a 4K boundary and
1401 * depeding on minor_version, it can be:
1402 * 0: At least 8K, but less than 12K, from end of device
1403 * 1: At start of device
1404 * 2: 4K from start of device.
1405 */
1406 switch(minor_version) {
1407 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001408 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001409 sb_start -= 8*2;
1410 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001413 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001416 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
1418 default:
1419 return -EINVAL;
1420 }
Andre Noll0f420352008-07-11 22:02:23 +10001421 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
NeilBrown0002b272005-09-09 16:23:53 -07001423 /* superblock is rarely larger than 1K, but it can be larger,
1424 * and it is safe to read 4k, so we do that
1425 */
1426 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (ret) return ret;
1428
1429
Namhyung Kim65a06f062011-07-27 11:00:36 +10001430 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1433 sb->major_version != cpu_to_le32(1) ||
1434 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001435 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001436 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return -EINVAL;
1438
1439 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1440 printk("md: invalid superblock checksum on %s\n",
1441 bdevname(rdev->bdev,b));
1442 return -EINVAL;
1443 }
1444 if (le64_to_cpu(sb->data_size) < 10) {
1445 printk("md: data_size too small on %s\n",
1446 bdevname(rdev->bdev,b));
1447 return -EINVAL;
1448 }
NeilBrownc6563a82012-05-21 09:27:00 +10001449 if (sb->pad0 ||
1450 sb->pad3[0] ||
1451 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1452 /* Some padding is non-zero, might be a new feature */
1453 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rdev->preferred_minor = 0xffff;
1456 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001457 rdev->new_data_offset = rdev->data_offset;
1458 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1459 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1460 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001461 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
NeilBrown0002b272005-09-09 16:23:53 -07001463 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001464 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001465 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001466 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1467
1468 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001469 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001470 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001471 if (minor_version
1472 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1473 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001474
NeilBrown31b65a02006-07-10 04:44:14 -07001475 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1476 rdev->desc_nr = -1;
1477 else
1478 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1479
NeilBrown2699b672011-07-28 11:31:47 +10001480 if (!rdev->bb_page) {
1481 rdev->bb_page = alloc_page(GFP_KERNEL);
1482 if (!rdev->bb_page)
1483 return -ENOMEM;
1484 }
1485 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1486 rdev->badblocks.count == 0) {
1487 /* need to load the bad block list.
1488 * Currently we limit it to one page.
1489 */
1490 s32 offset;
1491 sector_t bb_sector;
1492 u64 *bbp;
1493 int i;
1494 int sectors = le16_to_cpu(sb->bblog_size);
1495 if (sectors > (PAGE_SIZE / 512))
1496 return -EINVAL;
1497 offset = le32_to_cpu(sb->bblog_offset);
1498 if (offset == 0)
1499 return -EINVAL;
1500 bb_sector = (long long)offset;
1501 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1502 rdev->bb_page, READ, true))
1503 return -EIO;
1504 bbp = (u64 *)page_address(rdev->bb_page);
1505 rdev->badblocks.shift = sb->bblog_shift;
1506 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1507 u64 bb = le64_to_cpu(*bbp);
1508 int count = bb & (0x3ff);
1509 u64 sector = bb >> 10;
1510 sector <<= sb->bblog_shift;
1511 count <<= sb->bblog_shift;
1512 if (bb + 1 == 0)
1513 break;
1514 if (md_set_badblocks(&rdev->badblocks,
1515 sector, count, 1) == 0)
1516 return -EINVAL;
1517 }
NeilBrown486adf72013-04-24 11:42:44 +10001518 } else if (sb->bblog_offset != 0)
1519 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001520
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001521 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001522 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001523 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001525 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1528 sb->level != refsb->level ||
1529 sb->layout != refsb->layout ||
1530 sb->chunksize != refsb->chunksize) {
1531 printk(KERN_WARNING "md: %s has strangely different"
1532 " superblock to %s\n",
1533 bdevname(rdev->bdev,b),
1534 bdevname(refdev->bdev,b2));
1535 return -EINVAL;
1536 }
1537 ev1 = le64_to_cpu(sb->events);
1538 ev2 = le64_to_cpu(refsb->events);
1539
1540 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001541 ret = 1;
1542 else
1543 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
NeilBrownc6563a82012-05-21 09:27:00 +10001545 if (minor_version) {
1546 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1547 sectors -= rdev->data_offset;
1548 } else
1549 sectors = rdev->sb_start;
1550 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001552 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
NeilBrownfd01b882011-10-11 16:47:53 +11001556static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001558 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001559 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
NeilBrown41158c72005-06-21 17:17:25 -07001561 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001562 clear_bit(Faulty, &rdev->flags);
1563 clear_bit(In_sync, &rdev->flags);
1564 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (mddev->raid_disks == 0) {
1567 mddev->major_version = 1;
1568 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001569 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001570 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1572 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1573 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001574 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 mddev->layout = le32_to_cpu(sb->layout);
1576 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001577 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001578 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001579 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001580 mddev->bitmap_info.space = 0;
1581 /* Default location for bitmap is 1K after superblock
1582 * using 3K - total of 4K
1583 */
NeilBrownc3d97142009-12-14 12:49:52 +11001584 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001585 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001586 mddev->reshape_backwards = 0;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1589 memcpy(mddev->uuid, sb->set_uuid, 16);
1590
1591 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001592
NeilBrown71c08052005-09-09 16:23:51 -07001593 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001594 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001595 mddev->bitmap_info.offset =
1596 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001597 /* Metadata doesn't record how much space is available.
1598 * For 1.0, we assume we can use up to the superblock
1599 * if before, else to 4K beyond superblock.
1600 * For others, assume no change is possible.
1601 */
1602 if (mddev->minor_version > 0)
1603 mddev->bitmap_info.space = 0;
1604 else if (mddev->bitmap_info.offset > 0)
1605 mddev->bitmap_info.space =
1606 8 - mddev->bitmap_info.offset;
1607 else
1608 mddev->bitmap_info.space =
1609 -mddev->bitmap_info.offset;
1610 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001611
NeilBrownf6705572006-03-27 01:18:11 -08001612 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1613 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1614 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1615 mddev->new_level = le32_to_cpu(sb->new_level);
1616 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001617 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001618 if (mddev->delta_disks < 0 ||
1619 (mddev->delta_disks == 0 &&
1620 (le32_to_cpu(sb->feature_map)
1621 & MD_FEATURE_RESHAPE_BACKWARDS)))
1622 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001623 } else {
1624 mddev->reshape_position = MaxSector;
1625 mddev->delta_disks = 0;
1626 mddev->new_level = mddev->level;
1627 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001628 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001629 }
1630
NeilBrown41158c72005-06-21 17:17:25 -07001631 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001632 /* Insist of good event counter while assembling, except for
1633 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001635 if (rdev->desc_nr >= 0 &&
1636 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1637 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1638 if (ev1 < mddev->events)
1639 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001640 } else if (mddev->bitmap) {
1641 /* If adding to array with a bitmap, then we can accept an
1642 * older device, but not too old.
1643 */
NeilBrown41158c72005-06-21 17:17:25 -07001644 if (ev1 < mddev->bitmap->events_cleared)
1645 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001646 } else {
1647 if (ev1 < mddev->events)
1648 /* just a hot-add of a new device, leave raid_disk at -1 */
1649 return 0;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (mddev->level != LEVEL_MULTIPATH) {
1652 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001653 if (rdev->desc_nr < 0 ||
1654 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1655 role = 0xffff;
1656 rdev->desc_nr = -1;
1657 } else
1658 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 switch(role) {
1660 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001663 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001666 if ((le32_to_cpu(sb->feature_map) &
1667 MD_FEATURE_RECOVERY_OFFSET))
1668 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1669 else
1670 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 rdev->raid_disk = role;
1672 break;
1673 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001674 if (sb->devflags & WriteMostly1)
1675 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001676 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1677 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001678 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001679 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return 0;
1682}
1683
NeilBrownfd01b882011-10-11 16:47:53 +11001684static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001687 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int max_dev, i;
1689 /* make rdev->sb match mddev and rdev data. */
1690
Namhyung Kim65a06f062011-07-27 11:00:36 +10001691 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 sb->feature_map = 0;
1694 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001695 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memset(sb->pad3, 0, sizeof(sb->pad3));
1697
1698 sb->utime = cpu_to_le64((__u64)mddev->utime);
1699 sb->events = cpu_to_le64(mddev->events);
1700 if (mddev->in_sync)
1701 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1702 else
1703 sb->resync_offset = cpu_to_le64(0);
1704
NeilBrown1c05b4b2006-10-21 10:24:08 -07001705 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001706
NeilBrownf0ca3402006-02-02 14:28:04 -08001707 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001708 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001709 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001710 sb->level = cpu_to_le32(mddev->level);
1711 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001712
NeilBrownaeb9b212011-08-25 14:43:08 +10001713 if (test_bit(WriteMostly, &rdev->flags))
1714 sb->devflags |= WriteMostly1;
1715 else
1716 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001717 sb->data_offset = cpu_to_le64(rdev->data_offset);
1718 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001719
NeilBrownc3d97142009-12-14 12:49:52 +11001720 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1721 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001722 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001723 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001724
1725 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001726 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001727 sb->feature_map |=
1728 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1729 sb->recovery_offset =
1730 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001732 if (test_bit(Replacement, &rdev->flags))
1733 sb->feature_map |=
1734 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735
NeilBrownf6705572006-03-27 01:18:11 -08001736 if (mddev->reshape_position != MaxSector) {
1737 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1738 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1739 sb->new_layout = cpu_to_le32(mddev->new_layout);
1740 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1741 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001742 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001743 if (mddev->delta_disks == 0 &&
1744 mddev->reshape_backwards)
1745 sb->feature_map
1746 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001747 if (rdev->new_data_offset != rdev->data_offset) {
1748 sb->feature_map
1749 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1750 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1751 - rdev->data_offset));
1752 }
NeilBrownf6705572006-03-27 01:18:11 -08001753 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001754
NeilBrown2699b672011-07-28 11:31:47 +10001755 if (rdev->badblocks.count == 0)
1756 /* Nothing to do for bad blocks*/ ;
1757 else if (sb->bblog_offset == 0)
1758 /* Cannot record bad blocks on this device */
1759 md_error(mddev, rdev);
1760 else {
1761 struct badblocks *bb = &rdev->badblocks;
1762 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1763 u64 *p = bb->page;
1764 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1765 if (bb->changed) {
1766 unsigned seq;
1767
1768retry:
1769 seq = read_seqbegin(&bb->lock);
1770
1771 memset(bbp, 0xff, PAGE_SIZE);
1772
1773 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001774 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001775 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1776 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001777 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001778 }
NeilBrownd0962932012-03-19 12:46:41 +11001779 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001780 if (read_seqretry(&bb->lock, seq))
1781 goto retry;
1782
1783 bb->sector = (rdev->sb_start +
1784 (int)le32_to_cpu(sb->bblog_offset));
1785 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001786 }
1787 }
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001790 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (rdev2->desc_nr+1 > max_dev)
1792 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001793
NeilBrown70471da2009-08-03 10:59:57 +10001794 if (max_dev > le32_to_cpu(sb->max_dev)) {
1795 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001796 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001797 rdev->sb_size = max_dev * 2 + 256;
1798 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1799 if (rdev->sb_size & bmask)
1800 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001801 } else
1802 max_dev = le32_to_cpu(sb->max_dev);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 for (i=0; i<max_dev;i++)
1805 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1806
NeilBrowndafb20f2012-03-19 12:46:39 +11001807 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001809 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001811 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001813 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001814 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 else
1816 sb->dev_roles[i] = cpu_to_le16(0xffff);
1817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 sb->sb_csum = calc_sb_1_csum(sb);
1820}
1821
Chris Webb0cd17fe2008-06-28 08:31:46 +10001822static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001823super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824{
1825 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001826 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001827 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001828 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001829 if (rdev->data_offset != rdev->new_data_offset)
1830 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001831 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001832 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001833 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001834 max_sectors -= rdev->data_offset;
1835 if (!num_sectors || num_sectors > max_sectors)
1836 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001837 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001838 /* minor version 0 with bitmap we can't move */
1839 return 0;
1840 } else {
1841 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001842 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001843 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001844 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001845 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001846 if (!num_sectors || num_sectors > max_sectors)
1847 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001848 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001850 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001851 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001852 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001854 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 rdev->sb_page);
1856 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001857 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001858
1859}
1860
1861static int
1862super_1_allow_new_offset(struct md_rdev *rdev,
1863 unsigned long long new_offset)
1864{
1865 /* All necessary checks on new >= old have been done */
1866 struct bitmap *bitmap;
1867 if (new_offset >= rdev->data_offset)
1868 return 1;
1869
1870 /* with 1.0 metadata, there is no metadata to tread on
1871 * so we can always move back */
1872 if (rdev->mddev->minor_version == 0)
1873 return 1;
1874
1875 /* otherwise we must be sure not to step on
1876 * any metadata, so stay:
1877 * 36K beyond start of superblock
1878 * beyond end of badblocks
1879 * beyond write-intent bitmap
1880 */
1881 if (rdev->sb_start + (32+4)*2 > new_offset)
1882 return 0;
1883 bitmap = rdev->mddev->bitmap;
1884 if (bitmap && !rdev->mddev->bitmap_info.file &&
1885 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001886 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001887 return 0;
1888 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1889 return 0;
1890
1891 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001892}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Adrian Bunk75c96f82005-05-05 16:16:09 -07001894static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 [0] = {
1896 .name = "0.90.0",
1897 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 .load_super = super_90_load,
1899 .validate_super = super_90_validate,
1900 .sync_super = super_90_sync,
1901 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001902 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 },
1904 [1] = {
1905 .name = "md-1",
1906 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 .load_super = super_1_load,
1908 .validate_super = super_1_validate,
1909 .sync_super = super_1_sync,
1910 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001911 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 },
1913};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
NeilBrownfd01b882011-10-11 16:47:53 +11001915static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001916{
1917 if (mddev->sync_super) {
1918 mddev->sync_super(mddev, rdev);
1919 return;
1920 }
1921
1922 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1923
1924 super_types[mddev->major_version].sync_super(mddev, rdev);
1925}
1926
NeilBrownfd01b882011-10-11 16:47:53 +11001927static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
NeilBrown3cb03002011-10-11 16:45:26 +11001929 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
NeilBrown4b809912008-07-21 17:05:25 +10001931 rcu_read_lock();
1932 rdev_for_each_rcu(rdev, mddev1)
1933 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001934 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001935 rdev2->bdev->bd_contains) {
1936 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001937 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001938 }
1939 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return 0;
1941}
1942
1943static LIST_HEAD(pending_raid_disks);
1944
Andre Nollac5e7112009-08-03 10:59:47 +10001945/*
1946 * Try to register data integrity profile for an mddev
1947 *
1948 * This is called when an array is started and after a disk has been kicked
1949 * from the array. It only succeeds if all working and active component devices
1950 * are integrity capable with matching profiles.
1951 */
NeilBrownfd01b882011-10-11 16:47:53 +11001952int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001953{
NeilBrown3cb03002011-10-11 16:45:26 +11001954 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001955
Andre Nollac5e7112009-08-03 10:59:47 +10001956 if (list_empty(&mddev->disks))
1957 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001958 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1959 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001960 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001961 /* skip spares and non-functional disks */
1962 if (test_bit(Faulty, &rdev->flags))
1963 continue;
1964 if (rdev->raid_disk < 0)
1965 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001966 if (!reference) {
1967 /* Use the first rdev as the reference */
1968 reference = rdev;
1969 continue;
1970 }
1971 /* does this rdev's profile match the reference profile? */
1972 if (blk_integrity_compare(reference->bdev->bd_disk,
1973 rdev->bdev->bd_disk) < 0)
1974 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001975 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001976 if (!reference || !bdev_get_integrity(reference->bdev))
1977 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001978 /*
1979 * All component devices are integrity capable and have matching
1980 * profiles, register the common profile for the md device.
1981 */
1982 if (blk_integrity_register(mddev->gendisk,
1983 bdev_get_integrity(reference->bdev)) != 0) {
1984 printk(KERN_ERR "md: failed to register integrity for %s\n",
1985 mdname(mddev));
1986 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001987 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001988 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1989 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1990 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1991 mdname(mddev));
1992 return -EINVAL;
1993 }
Andre Nollac5e7112009-08-03 10:59:47 +10001994 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001995}
Andre Nollac5e7112009-08-03 10:59:47 +10001996EXPORT_SYMBOL(md_integrity_register);
1997
1998/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001999void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002000{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002001 struct blk_integrity *bi_rdev;
2002 struct blk_integrity *bi_mddev;
2003
2004 if (!mddev->gendisk)
2005 return;
2006
2007 bi_rdev = bdev_get_integrity(rdev->bdev);
2008 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002009
2010 if (!bi_mddev) /* nothing to do */
2011 return;
2012 if (rdev->raid_disk < 0) /* skip spares */
2013 return;
2014 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2015 rdev->bdev->bd_disk) >= 0)
2016 return;
2017 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2018 blk_integrity_unregister(mddev->gendisk);
2019}
2020EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002021
NeilBrownfd01b882011-10-11 16:47:53 +11002022static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002024 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002025 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002026 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002027 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (rdev->mddev) {
2030 MD_BUG();
2031 return -EINVAL;
2032 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002033
2034 /* prevent duplicates */
2035 if (find_rdev(mddev, rdev->bdev->bd_dev))
2036 return -EEXIST;
2037
Andre Nolldd8ac332009-03-31 14:33:13 +11002038 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2039 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2040 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002041 if (mddev->pers) {
2042 /* Cannot change size, so fail
2043 * If mddev->level <= 0, then we don't care
2044 * about aligning sizes (e.g. linear)
2045 */
2046 if (mddev->level > 0)
2047 return -ENOSPC;
2048 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002049 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 /* Verify rdev->desc_nr is unique.
2053 * If it is -1, assign a free number, else
2054 * check number is not in use
2055 */
2056 if (rdev->desc_nr < 0) {
2057 int choice = 0;
2058 if (mddev->pers) choice = mddev->raid_disks;
2059 while (find_rdev_nr(mddev, choice))
2060 choice++;
2061 rdev->desc_nr = choice;
2062 } else {
2063 if (find_rdev_nr(mddev, rdev->desc_nr))
2064 return -EBUSY;
2065 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002066 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2067 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2068 mdname(mddev), mddev->max_disks);
2069 return -EBUSY;
2070 }
NeilBrown19133a42005-11-08 21:39:35 -08002071 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002072 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002073 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002076 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002077
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002078 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002079 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002080
Tejun Heo0762b8b2008-08-25 19:56:12 +09002081 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002082 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2083 /* failure here is OK */;
2084 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002085
NeilBrown4b809912008-07-21 17:05:25 +10002086 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002087 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002088
2089 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002090 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002093
2094 fail:
2095 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2096 b, mdname(mddev));
2097 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
NeilBrown177a99b2008-02-06 01:39:56 -08002100static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002101{
NeilBrown3cb03002011-10-11 16:45:26 +11002102 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002103 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002104 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002105}
2106
NeilBrown3cb03002011-10-11 16:45:26 +11002107static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 char b[BDEVNAME_SIZE];
2110 if (!rdev->mddev) {
2111 MD_BUG();
2112 return;
2113 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002114 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002115 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2117 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002118 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002119 sysfs_put(rdev->sysfs_state);
2120 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002121 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002122 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002123 * writing to 'remove' to "dev/state". We also need
2124 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002125 */
NeilBrown4b809912008-07-21 17:05:25 +10002126 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002127 INIT_WORK(&rdev->del_work, md_delayed_delete);
2128 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002129 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132/*
2133 * prevent the device from being mounted, repartitioned or
2134 * otherwise reused by a RAID array (or any other kernel
2135 * subsystem), by bd_claiming the device.
2136 */
NeilBrown3cb03002011-10-11 16:45:26 +11002137static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 int err = 0;
2140 struct block_device *bdev;
2141 char b[BDEVNAME_SIZE];
2142
Tejun Heod4d77622010-11-13 11:55:18 +01002143 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002144 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (IS_ERR(bdev)) {
2146 printk(KERN_ERR "md: could not open %s.\n",
2147 __bdevname(dev, b));
2148 return PTR_ERR(bdev);
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 rdev->bdev = bdev;
2151 return err;
2152}
2153
NeilBrown3cb03002011-10-11 16:45:26 +11002154static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct block_device *bdev = rdev->bdev;
2157 rdev->bdev = NULL;
2158 if (!bdev)
2159 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002160 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
2163void md_autodetect_dev(dev_t dev);
2164
NeilBrown3cb03002011-10-11 16:45:26 +11002165static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 char b[BDEVNAME_SIZE];
2168 printk(KERN_INFO "md: export_rdev(%s)\n",
2169 bdevname(rdev->bdev,b));
2170 if (rdev->mddev)
2171 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002172 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002174 if (test_bit(AutoDetected, &rdev->flags))
2175 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#endif
2177 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002178 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
NeilBrown3cb03002011-10-11 16:45:26 +11002181static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 unbind_rdev_from_array(rdev);
2184 export_rdev(rdev);
2185}
2186
NeilBrownfd01b882011-10-11 16:47:53 +11002187static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
NeilBrown3cb03002011-10-11 16:45:26 +11002189 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
NeilBrowndafb20f2012-03-19 12:46:39 +11002191 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (!rdev->mddev) {
2193 MD_BUG();
2194 continue;
2195 }
2196 kick_rdev_from_array(rdev);
2197 }
2198 if (!list_empty(&mddev->disks))
2199 MD_BUG();
2200 mddev->raid_disks = 0;
2201 mddev->major_version = 0;
2202}
2203
2204static void print_desc(mdp_disk_t *desc)
2205{
2206 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2207 desc->major,desc->minor,desc->raid_disk,desc->state);
2208}
2209
Cheng Renquancd2ac932009-01-09 08:31:08 +11002210static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 int i;
2213
2214 printk(KERN_INFO
2215 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2216 sb->major_version, sb->minor_version, sb->patch_version,
2217 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2218 sb->ctime);
2219 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2220 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2221 sb->md_minor, sb->layout, sb->chunk_size);
2222 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2223 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2224 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2225 sb->failed_disks, sb->spare_disks,
2226 sb->sb_csum, (unsigned long)sb->events_lo);
2227
2228 printk(KERN_INFO);
2229 for (i = 0; i < MD_SB_DISKS; i++) {
2230 mdp_disk_t *desc;
2231
2232 desc = sb->disks + i;
2233 if (desc->number || desc->major || desc->minor ||
2234 desc->raid_disk || (desc->state && (desc->state != 4))) {
2235 printk(" D %2d: ", i);
2236 print_desc(desc);
2237 }
2238 }
2239 printk(KERN_INFO "md: THIS: ");
2240 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Cheng Renquancd2ac932009-01-09 08:31:08 +11002243static void print_sb_1(struct mdp_superblock_1 *sb)
2244{
2245 __u8 *uuid;
2246
2247 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002248 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002249 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002250 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002251 le32_to_cpu(sb->major_version),
2252 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002253 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002254 sb->set_name,
2255 (unsigned long long)le64_to_cpu(sb->ctime)
2256 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2257
2258 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002259 printk(KERN_INFO
2260 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002261 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002262 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002263 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2264 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002265 le32_to_cpu(sb->level),
2266 (unsigned long long)le64_to_cpu(sb->size),
2267 le32_to_cpu(sb->raid_disks),
2268 le32_to_cpu(sb->layout),
2269 le32_to_cpu(sb->chunksize),
2270 (unsigned long long)le64_to_cpu(sb->data_offset),
2271 (unsigned long long)le64_to_cpu(sb->data_size),
2272 (unsigned long long)le64_to_cpu(sb->super_offset),
2273 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2274 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002275 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002276 sb->devflags,
2277 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2278 (unsigned long long)le64_to_cpu(sb->events),
2279 (unsigned long long)le64_to_cpu(sb->resync_offset),
2280 le32_to_cpu(sb->sb_csum),
2281 le32_to_cpu(sb->max_dev)
2282 );
2283}
2284
NeilBrown3cb03002011-10-11 16:45:26 +11002285static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002288 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2289 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002290 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2291 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002293 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2294 switch (major_version) {
2295 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002296 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002297 break;
2298 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002299 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002300 break;
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 } else
2303 printk(KERN_INFO "md: no rdev superblock!\n");
2304}
2305
Adrian Bunk5e563412006-06-26 00:27:42 -07002306static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002308 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002309 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002310 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 char b[BDEVNAME_SIZE];
2312
2313 printk("\n");
2314 printk("md: **********************************\n");
2315 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2316 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002317 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
NeilBrown32a76272005-06-21 17:17:14 -07002319 if (mddev->bitmap)
2320 bitmap_print_sb(mddev->bitmap);
2321 else
2322 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002323 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 printk("<%s>", bdevname(rdev->bdev,b));
2325 printk("\n");
2326
NeilBrowndafb20f2012-03-19 12:46:39 +11002327 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002328 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330 printk("md: **********************************\n");
2331 printk("\n");
2332}
2333
2334
NeilBrownfd01b882011-10-11 16:47:53 +11002335static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
NeilBrown42543762006-06-26 00:27:57 -07002337 /* Update each superblock (in-memory image), but
2338 * if we are allowed to, skip spares which already
2339 * have the right event counter, or have one earlier
2340 * (which would mean they aren't being marked as dirty
2341 * with the rest of the array)
2342 */
NeilBrown3cb03002011-10-11 16:45:26 +11002343 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002344 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002345 if (rdev->sb_events == mddev->events ||
2346 (nospares &&
2347 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002348 rdev->sb_events+1 == mddev->events)) {
2349 /* Don't update this superblock */
2350 rdev->sb_loaded = 2;
2351 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002352 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002353 rdev->sb_loaded = 1;
2354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356}
2357
NeilBrownfd01b882011-10-11 16:47:53 +11002358static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
NeilBrown3cb03002011-10-11 16:45:26 +11002360 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002361 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002362 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002363 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
NeilBrownd87f0642013-04-24 11:42:40 +10002365 if (mddev->ro) {
2366 if (force_change)
2367 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2368 return;
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002371 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002372 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002373 if (rdev->raid_disk >= 0 &&
2374 mddev->delta_disks >= 0 &&
2375 !test_bit(In_sync, &rdev->flags) &&
2376 mddev->curr_resync_completed > rdev->recovery_offset)
2377 rdev->recovery_offset = mddev->curr_resync_completed;
2378
2379 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002380 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002381 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002382 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002383 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002384 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002385 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002386 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002387 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002388 md_ack_all_badblocks(&rdev->badblocks);
2389 md_error(mddev, rdev);
2390 }
2391 clear_bit(Blocked, &rdev->flags);
2392 clear_bit(BlockedBadBlocks, &rdev->flags);
2393 wake_up(&rdev->blocked_wait);
2394 }
2395 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002396 wake_up(&mddev->sb_wait);
2397 return;
2398 }
2399
NeilBrowna9701a32005-11-08 21:39:34 -08002400 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002401
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002402 mddev->utime = get_seconds();
2403
NeilBrown850b2b42006-10-03 01:15:46 -07002404 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2405 force_change = 1;
2406 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2407 /* just a clean<-> dirty transition, possibly leave spares alone,
2408 * though if events isn't the right even/odd, we will have to do
2409 * spares after all
2410 */
2411 nospares = 1;
2412 if (force_change)
2413 nospares = 0;
2414 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002415 /* If the array is degraded, then skipping spares is both
2416 * dangerous and fairly pointless.
2417 * Dangerous because a device that was removed from the array
2418 * might have a event_count that still looks up-to-date,
2419 * so it can be re-added without a resync.
2420 * Pointless because if there are any spares to skip,
2421 * then a recovery will happen and soon that array won't
2422 * be degraded any more and the spare can go back to sleep then.
2423 */
NeilBrown850b2b42006-10-03 01:15:46 -07002424 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002425
NeilBrown06d91a52005-06-21 17:17:12 -07002426 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002427
2428 /* If this is just a dirty<->clean transition, and the array is clean
2429 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002430 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002431 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002432 && mddev->can_decrease_events
2433 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002434 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002435 mddev->can_decrease_events = 0;
2436 } else {
NeilBrown42543762006-06-26 00:27:57 -07002437 /* otherwise we have to go forward and ... */
2438 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002439 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (!mddev->events) {
2443 /*
2444 * oops, this 64-bit counter should never wrap.
2445 * Either we are in around ~1 trillion A.C., assuming
2446 * 1 reboot per second, or we have a bug:
2447 */
2448 MD_BUG();
2449 mddev->events --;
2450 }
NeilBrown2699b672011-07-28 11:31:47 +10002451
NeilBrowndafb20f2012-03-19 12:46:39 +11002452 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002453 if (rdev->badblocks.changed)
2454 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002455 if (test_bit(Faulty, &rdev->flags))
2456 set_bit(FaultRecorded, &rdev->flags);
2457 }
NeilBrown2699b672011-07-28 11:31:47 +10002458
NeilBrowne6910632008-02-06 01:39:51 -08002459 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002460 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
NeilBrown36a4e1f2011-10-07 14:23:17 +11002462 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2463 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
NeilBrown4ad13662007-07-17 04:06:13 -07002465 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002466 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002468
NeilBrown42543762006-06-26 00:27:57 -07002469 if (rdev->sb_loaded != 1)
2470 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002472 if (!test_bit(Faulty, &rdev->flags) &&
2473 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002474 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002475 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002476 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002477 pr_debug("md: (write) %s's sb offset: %llu\n",
2478 bdevname(rdev->bdev, b),
2479 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002480 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002481 if (rdev->badblocks.size) {
2482 md_super_write(mddev, rdev,
2483 rdev->badblocks.sector,
2484 rdev->badblocks.size << 9,
2485 rdev->bb_page);
2486 rdev->badblocks.size = 0;
2487 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002488
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002489 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002490 pr_debug("md: %s (skipping faulty)\n",
2491 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002492 else
2493 pr_debug("(skipping incremental s/r ");
2494
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 /* only need to write one superblock... */
2497 break;
2498 }
NeilBrowna9701a32005-11-08 21:39:34 -08002499 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002500 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002501
NeilBrowna9701a32005-11-08 21:39:34 -08002502 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002503 if (mddev->in_sync != sync_req ||
2504 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002505 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002506 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002507 goto repeat;
2508 }
NeilBrown850b2b42006-10-03 01:15:46 -07002509 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002510 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002511 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002512 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2513 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002514
NeilBrowndafb20f2012-03-19 12:46:39 +11002515 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002516 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2517 clear_bit(Blocked, &rdev->flags);
2518
2519 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002520 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002521 clear_bit(BlockedBadBlocks, &rdev->flags);
2522 wake_up(&rdev->blocked_wait);
2523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Andre Noll7f6ce762008-03-23 18:34:54 +01002526/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002527 * We want to accept with case. For this we use cmd_match.
2528 */
2529static int cmd_match(const char *cmd, const char *str)
2530{
2531 /* See if cmd, written into a sysfs file, matches
2532 * str. They must either be the same, or cmd can
2533 * have a trailing newline
2534 */
2535 while (*cmd && *str && *cmd == *str) {
2536 cmd++;
2537 str++;
2538 }
2539 if (*cmd == '\n')
2540 cmd++;
2541 if (*str || *cmd)
2542 return 0;
2543 return 1;
2544}
2545
NeilBrown86e6ffd2005-11-08 21:39:24 -08002546struct rdev_sysfs_entry {
2547 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002548 ssize_t (*show)(struct md_rdev *, char *);
2549 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002550};
2551
2552static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002553state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002554{
2555 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002556 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002557
NeilBrownde393cd2011-07-28 11:31:48 +10002558 if (test_bit(Faulty, &rdev->flags) ||
2559 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002560 len+= sprintf(page+len, "%sfaulty",sep);
2561 sep = ",";
2562 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002563 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564 len += sprintf(page+len, "%sin_sync",sep);
2565 sep = ",";
2566 }
NeilBrownf6556752006-06-26 00:28:01 -07002567 if (test_bit(WriteMostly, &rdev->flags)) {
2568 len += sprintf(page+len, "%swrite_mostly",sep);
2569 sep = ",";
2570 }
NeilBrownde393cd2011-07-28 11:31:48 +10002571 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002572 (rdev->badblocks.unacked_exist
2573 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002574 len += sprintf(page+len, "%sblocked", sep);
2575 sep = ",";
2576 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002577 if (!test_bit(Faulty, &rdev->flags) &&
2578 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002579 len += sprintf(page+len, "%sspare", sep);
2580 sep = ",";
2581 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002582 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2583 len += sprintf(page+len, "%swrite_error", sep);
2584 sep = ",";
2585 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002586 if (test_bit(WantReplacement, &rdev->flags)) {
2587 len += sprintf(page+len, "%swant_replacement", sep);
2588 sep = ",";
2589 }
2590 if (test_bit(Replacement, &rdev->flags)) {
2591 len += sprintf(page+len, "%sreplacement", sep);
2592 sep = ",";
2593 }
2594
NeilBrown86e6ffd2005-11-08 21:39:24 -08002595 return len+sprintf(page+len, "\n");
2596}
2597
NeilBrown45dc2de2006-06-26 00:27:58 -07002598static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002599state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002600{
2601 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002602 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002603 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002604 * writemostly - sets write_mostly
2605 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002606 * blocked - sets the Blocked flags
2607 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002608 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002609 * write_error - sets WriteErrorSeen
2610 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002611 */
2612 int err = -EINVAL;
2613 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2614 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002615 if (test_bit(Faulty, &rdev->flags))
2616 err = 0;
2617 else
2618 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002619 } else if (cmd_match(buf, "remove")) {
2620 if (rdev->raid_disk >= 0)
2621 err = -EBUSY;
2622 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002623 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002624 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002625 if (mddev->pers)
2626 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002627 md_new_event(mddev);
2628 err = 0;
2629 }
NeilBrownf6556752006-06-26 00:28:01 -07002630 } else if (cmd_match(buf, "writemostly")) {
2631 set_bit(WriteMostly, &rdev->flags);
2632 err = 0;
2633 } else if (cmd_match(buf, "-writemostly")) {
2634 clear_bit(WriteMostly, &rdev->flags);
2635 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002636 } else if (cmd_match(buf, "blocked")) {
2637 set_bit(Blocked, &rdev->flags);
2638 err = 0;
2639 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002640 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002641 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002642 /* metadata handler doesn't understand badblocks,
2643 * so we need to fail the device
2644 */
2645 md_error(rdev->mddev, rdev);
2646 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002647 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002648 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002649 wake_up(&rdev->blocked_wait);
2650 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2651 md_wakeup_thread(rdev->mddev->thread);
2652
2653 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002654 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2655 set_bit(In_sync, &rdev->flags);
2656 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002657 } else if (cmd_match(buf, "write_error")) {
2658 set_bit(WriteErrorSeen, &rdev->flags);
2659 err = 0;
2660 } else if (cmd_match(buf, "-write_error")) {
2661 clear_bit(WriteErrorSeen, &rdev->flags);
2662 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002663 } else if (cmd_match(buf, "want_replacement")) {
2664 /* Any non-spare device that is not a replacement can
2665 * become want_replacement at any time, but we then need to
2666 * check if recovery is needed.
2667 */
2668 if (rdev->raid_disk >= 0 &&
2669 !test_bit(Replacement, &rdev->flags))
2670 set_bit(WantReplacement, &rdev->flags);
2671 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2672 md_wakeup_thread(rdev->mddev->thread);
2673 err = 0;
2674 } else if (cmd_match(buf, "-want_replacement")) {
2675 /* Clearing 'want_replacement' is always allowed.
2676 * Once replacements starts it is too late though.
2677 */
2678 err = 0;
2679 clear_bit(WantReplacement, &rdev->flags);
2680 } else if (cmd_match(buf, "replacement")) {
2681 /* Can only set a device as a replacement when array has not
2682 * yet been started. Once running, replacement is automatic
2683 * from spares, or by assigning 'slot'.
2684 */
2685 if (rdev->mddev->pers)
2686 err = -EBUSY;
2687 else {
2688 set_bit(Replacement, &rdev->flags);
2689 err = 0;
2690 }
2691 } else if (cmd_match(buf, "-replacement")) {
2692 /* Similarly, can only clear Replacement before start */
2693 if (rdev->mddev->pers)
2694 err = -EBUSY;
2695 else {
2696 clear_bit(Replacement, &rdev->flags);
2697 err = 0;
2698 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002699 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002700 if (!err)
2701 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002702 return err ? err : len;
2703}
NeilBrown80ca3a42006-07-10 04:44:18 -07002704static struct rdev_sysfs_entry rdev_state =
2705__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002706
2707static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002708errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002709{
2710 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2711}
2712
2713static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002714errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002715{
2716 char *e;
2717 unsigned long n = simple_strtoul(buf, &e, 10);
2718 if (*buf && (*e == 0 || *e == '\n')) {
2719 atomic_set(&rdev->corrected_errors, n);
2720 return len;
2721 }
2722 return -EINVAL;
2723}
2724static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002725__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002726
NeilBrown014236d2006-01-06 00:20:55 -08002727static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002728slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002729{
2730 if (rdev->raid_disk < 0)
2731 return sprintf(page, "none\n");
2732 else
2733 return sprintf(page, "%d\n", rdev->raid_disk);
2734}
2735
2736static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002737slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002738{
2739 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002740 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002741 int slot = simple_strtoul(buf, &e, 10);
2742 if (strncmp(buf, "none", 4)==0)
2743 slot = -1;
2744 else if (e==buf || (*e && *e!= '\n'))
2745 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002746 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002747 /* Setting 'slot' on an active array requires also
2748 * updating the 'rd%d' link, and communicating
2749 * with the personality with ->hot_*_disk.
2750 * For now we only support removing
2751 * failed/spare devices. This normally happens automatically,
2752 * but not when the metadata is externally managed.
2753 */
NeilBrownc303da62008-02-06 01:39:51 -08002754 if (rdev->raid_disk == -1)
2755 return -EEXIST;
2756 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002757 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002758 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002759 clear_bit(Blocked, &rdev->flags);
2760 remove_and_add_spares(rdev->mddev, rdev);
2761 if (rdev->raid_disk >= 0)
2762 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002763 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2764 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002765 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002767 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002768 */
2769
2770 if (rdev->raid_disk != -1)
2771 return -EBUSY;
2772
NeilBrownc6751b22011-02-02 11:57:13 +11002773 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2774 return -EBUSY;
2775
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 if (rdev->mddev->pers->hot_add_disk == NULL)
2777 return -EINVAL;
2778
NeilBrownba1b41b2011-01-14 09:14:34 +11002779 if (slot >= rdev->mddev->raid_disks &&
2780 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2781 return -ENOSPC;
2782
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 rdev->raid_disk = slot;
2784 if (test_bit(In_sync, &rdev->flags))
2785 rdev->saved_raid_disk = slot;
2786 else
2787 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002788 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 err = rdev->mddev->pers->
2790 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002791 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002792 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002793 return err;
Neil Brown52664732008-06-28 08:31:44 +10002794 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002795 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002796 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002797 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002798 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002799 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002800 if (slot >= rdev->mddev->raid_disks &&
2801 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002802 return -ENOSPC;
2803 rdev->raid_disk = slot;
2804 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002805 clear_bit(Faulty, &rdev->flags);
2806 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002807 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002808 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002809 }
NeilBrown014236d2006-01-06 00:20:55 -08002810 return len;
2811}
2812
2813
2814static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002815__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002816
NeilBrown93c8cad2006-01-06 00:20:56 -08002817static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002818offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002819{
Andrew Morton6961ece2006-01-06 00:20:59 -08002820 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002821}
2822
2823static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002824offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002825{
NeilBrownc6563a82012-05-21 09:27:00 +10002826 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002827 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002828 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002829 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002830 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002831 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002832 /* Must set offset before size, so overlap checks
2833 * can be sane */
2834 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002835 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002836 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002837 return len;
2838}
2839
2840static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002841__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002842
NeilBrownc6563a82012-05-21 09:27:00 +10002843static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2844{
2845 return sprintf(page, "%llu\n",
2846 (unsigned long long)rdev->new_data_offset);
2847}
2848
2849static ssize_t new_offset_store(struct md_rdev *rdev,
2850 const char *buf, size_t len)
2851{
2852 unsigned long long new_offset;
2853 struct mddev *mddev = rdev->mddev;
2854
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002855 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002856 return -EINVAL;
2857
2858 if (mddev->sync_thread)
2859 return -EBUSY;
2860 if (new_offset == rdev->data_offset)
2861 /* reset is always permitted */
2862 ;
2863 else if (new_offset > rdev->data_offset) {
2864 /* must not push array size beyond rdev_sectors */
2865 if (new_offset - rdev->data_offset
2866 + mddev->dev_sectors > rdev->sectors)
2867 return -E2BIG;
2868 }
2869 /* Metadata worries about other space details. */
2870
2871 /* decreasing the offset is inconsistent with a backwards
2872 * reshape.
2873 */
2874 if (new_offset < rdev->data_offset &&
2875 mddev->reshape_backwards)
2876 return -EINVAL;
2877 /* Increasing offset is inconsistent with forwards
2878 * reshape. reshape_direction should be set to
2879 * 'backwards' first.
2880 */
2881 if (new_offset > rdev->data_offset &&
2882 !mddev->reshape_backwards)
2883 return -EINVAL;
2884
2885 if (mddev->pers && mddev->persistent &&
2886 !super_types[mddev->major_version]
2887 .allow_new_offset(rdev, new_offset))
2888 return -E2BIG;
2889 rdev->new_data_offset = new_offset;
2890 if (new_offset > rdev->data_offset)
2891 mddev->reshape_backwards = 1;
2892 else if (new_offset < rdev->data_offset)
2893 mddev->reshape_backwards = 0;
2894
2895 return len;
2896}
2897static struct rdev_sysfs_entry rdev_new_offset =
2898__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2899
NeilBrown83303b62006-01-06 00:21:06 -08002900static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002901rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002902{
Andre Nolldd8ac332009-03-31 14:33:13 +11002903 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002904}
2905
NeilBrownc5d79ad2008-02-06 01:39:54 -08002906static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2907{
2908 /* check if two start/length pairs overlap */
2909 if (s1+l1 <= s2)
2910 return 0;
2911 if (s2+l2 <= s1)
2912 return 0;
2913 return 1;
2914}
2915
Dan Williamsb522adc2009-03-31 15:00:31 +11002916static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2917{
2918 unsigned long long blocks;
2919 sector_t new;
2920
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002921 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002922 return -EINVAL;
2923
2924 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2925 return -EINVAL; /* sector conversion overflow */
2926
2927 new = blocks * 2;
2928 if (new != blocks * 2)
2929 return -EINVAL; /* unsigned long long to sector_t overflow */
2930
2931 *sectors = new;
2932 return 0;
2933}
2934
NeilBrown83303b62006-01-06 00:21:06 -08002935static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002936rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002937{
NeilBrownfd01b882011-10-11 16:47:53 +11002938 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002939 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002940 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002941
Dan Williamsb522adc2009-03-31 15:00:31 +11002942 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002943 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002944 if (rdev->data_offset != rdev->new_data_offset)
2945 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002946 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002947 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002948 sectors = super_types[my_mddev->major_version].
2949 rdev_size_change(rdev, sectors);
2950 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002951 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002952 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002953 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002955 if (!my_mddev->pers->resize)
2956 /* Cannot change size for RAID0 or Linear etc */
2957 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002958 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002959 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002960 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002961
Andre Nolldd8ac332009-03-31 14:33:13 +11002962 rdev->sectors = sectors;
2963 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002964 /* need to check that all other rdevs with the same ->bdev
2965 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002967 * we have to change it back, we will have the lock again.
2968 */
NeilBrownfd01b882011-10-11 16:47:53 +11002969 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002970 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002971 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002972
NeilBrown27c529b2008-03-04 14:29:33 -08002973 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002974 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002975 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002976
NeilBrown29f097c2013-11-14 17:54:51 +11002977 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002978 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002979 if (rdev->bdev == rdev2->bdev &&
2980 rdev != rdev2 &&
2981 overlaps(rdev->data_offset, rdev->sectors,
2982 rdev2->data_offset,
2983 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002984 overlap = 1;
2985 break;
2986 }
2987 mddev_unlock(mddev);
2988 if (overlap) {
2989 mddev_put(mddev);
2990 break;
2991 }
2992 }
NeilBrown29f097c2013-11-14 17:54:51 +11002993 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002994 if (overlap) {
2995 /* Someone else could have slipped in a size
2996 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002998 * safe, and trust userspace not to race with
2999 * itself
3000 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003001 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003002 return -EBUSY;
3003 }
3004 }
NeilBrown83303b62006-01-06 00:21:06 -08003005 return len;
3006}
3007
3008static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003009__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003010
Dan Williams06e3c812009-12-12 21:17:12 -07003011
NeilBrown3cb03002011-10-11 16:45:26 +11003012static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003013{
3014 unsigned long long recovery_start = rdev->recovery_offset;
3015
3016 if (test_bit(In_sync, &rdev->flags) ||
3017 recovery_start == MaxSector)
3018 return sprintf(page, "none\n");
3019
3020 return sprintf(page, "%llu\n", recovery_start);
3021}
3022
NeilBrown3cb03002011-10-11 16:45:26 +11003023static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003024{
3025 unsigned long long recovery_start;
3026
3027 if (cmd_match(buf, "none"))
3028 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003029 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003030 return -EINVAL;
3031
3032 if (rdev->mddev->pers &&
3033 rdev->raid_disk >= 0)
3034 return -EBUSY;
3035
3036 rdev->recovery_offset = recovery_start;
3037 if (recovery_start == MaxSector)
3038 set_bit(In_sync, &rdev->flags);
3039 else
3040 clear_bit(In_sync, &rdev->flags);
3041 return len;
3042}
3043
3044static struct rdev_sysfs_entry rdev_recovery_start =
3045__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3046
NeilBrown16c791a2011-07-28 11:31:47 +10003047
3048static ssize_t
3049badblocks_show(struct badblocks *bb, char *page, int unack);
3050static ssize_t
3051badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3052
NeilBrown3cb03002011-10-11 16:45:26 +11003053static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003054{
3055 return badblocks_show(&rdev->badblocks, page, 0);
3056}
NeilBrown3cb03002011-10-11 16:45:26 +11003057static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003058{
NeilBrownde393cd2011-07-28 11:31:48 +10003059 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3060 /* Maybe that ack was all we needed */
3061 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3062 wake_up(&rdev->blocked_wait);
3063 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003064}
3065static struct rdev_sysfs_entry rdev_bad_blocks =
3066__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3067
3068
NeilBrown3cb03002011-10-11 16:45:26 +11003069static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003070{
3071 return badblocks_show(&rdev->badblocks, page, 1);
3072}
NeilBrown3cb03002011-10-11 16:45:26 +11003073static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003074{
3075 return badblocks_store(&rdev->badblocks, page, len, 1);
3076}
3077static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3078__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3079
NeilBrown86e6ffd2005-11-08 21:39:24 -08003080static struct attribute *rdev_default_attrs[] = {
3081 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003082 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003083 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003084 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003085 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003086 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003087 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003088 &rdev_bad_blocks.attr,
3089 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003090 NULL,
3091};
3092static ssize_t
3093rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3094{
3095 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003096 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003097 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003098 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003099
3100 if (!entry->show)
3101 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003102
3103 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3104 if (!rv) {
3105 if (rdev->mddev == NULL)
3106 rv = -EBUSY;
3107 else
3108 rv = entry->show(rdev, page);
3109 mddev_unlock(mddev);
3110 }
3111 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003112}
3113
3114static ssize_t
3115rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3116 const char *page, size_t length)
3117{
3118 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003119 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003120 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003121 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003122
3123 if (!entry->store)
3124 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003125 if (!capable(CAP_SYS_ADMIN))
3126 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003127 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003128 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003129 if (rdev->mddev == NULL)
3130 rv = -EBUSY;
3131 else
3132 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003133 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003134 }
3135 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003136}
3137
3138static void rdev_free(struct kobject *ko)
3139{
NeilBrown3cb03002011-10-11 16:45:26 +11003140 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003141 kfree(rdev);
3142}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003143static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003144 .show = rdev_attr_show,
3145 .store = rdev_attr_store,
3146};
3147static struct kobj_type rdev_ktype = {
3148 .release = rdev_free,
3149 .sysfs_ops = &rdev_sysfs_ops,
3150 .default_attrs = rdev_default_attrs,
3151};
3152
NeilBrown3cb03002011-10-11 16:45:26 +11003153int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003154{
3155 rdev->desc_nr = -1;
3156 rdev->saved_raid_disk = -1;
3157 rdev->raid_disk = -1;
3158 rdev->flags = 0;
3159 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003160 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003161 rdev->sb_events = 0;
3162 rdev->last_read_error.tv_sec = 0;
3163 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003164 rdev->sb_loaded = 0;
3165 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003166 atomic_set(&rdev->nr_pending, 0);
3167 atomic_set(&rdev->read_errors, 0);
3168 atomic_set(&rdev->corrected_errors, 0);
3169
3170 INIT_LIST_HEAD(&rdev->same_set);
3171 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003172
3173 /* Add space to store bad block list.
3174 * This reserves the space even on arrays where it cannot
3175 * be used - I wonder if that matters
3176 */
3177 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003178 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003179 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3180 seqlock_init(&rdev->badblocks.lock);
3181 if (rdev->badblocks.page == NULL)
3182 return -ENOMEM;
3183
3184 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003185}
3186EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187/*
3188 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3189 *
3190 * mark the device faulty if:
3191 *
3192 * - the device is nonexistent (zero size)
3193 * - the device has no valid superblock
3194 *
3195 * a faulty rdev _never_ has rdev->sb set.
3196 */
NeilBrown3cb03002011-10-11 16:45:26 +11003197static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
3199 char b[BDEVNAME_SIZE];
3200 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003201 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 sector_t size;
3203
NeilBrown9ffae0c2006-01-06 00:20:32 -08003204 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (!rdev) {
3206 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3207 return ERR_PTR(-ENOMEM);
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
NeilBrown2230dfe2011-07-28 11:31:46 +10003210 err = md_rdev_init(rdev);
3211 if (err)
3212 goto abort_free;
3213 err = alloc_disk_sb(rdev);
3214 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 goto abort_free;
3216
NeilBrownc5d79ad2008-02-06 01:39:54 -08003217 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (err)
3219 goto abort_free;
3220
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003221 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003222
Mike Snitzer77304d22010-11-08 14:39:12 +01003223 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (!size) {
3225 printk(KERN_WARNING
3226 "md: %s has zero or unknown size, marking faulty!\n",
3227 bdevname(rdev->bdev,b));
3228 err = -EINVAL;
3229 goto abort_free;
3230 }
3231
3232 if (super_format >= 0) {
3233 err = super_types[super_format].
3234 load_super(rdev, NULL, super_minor);
3235 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003236 printk(KERN_WARNING
3237 "md: %s does not have a valid v%d.%d "
3238 "superblock, not importing!\n",
3239 bdevname(rdev->bdev,b),
3240 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto abort_free;
3242 }
3243 if (err < 0) {
3244 printk(KERN_WARNING
3245 "md: could not read %s's sb, not importing!\n",
3246 bdevname(rdev->bdev,b));
3247 goto abort_free;
3248 }
3249 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return rdev;
3252
3253abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003254 if (rdev->bdev)
3255 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003256 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 kfree(rdev);
3258 return ERR_PTR(err);
3259}
3260
3261/*
3262 * Check a full RAID array for plausibility
3263 */
3264
3265
NeilBrownfd01b882011-10-11 16:47:53 +11003266static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003269 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 char b[BDEVNAME_SIZE];
3271
3272 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003273 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 switch (super_types[mddev->major_version].
3275 load_super(rdev, freshest, mddev->minor_version)) {
3276 case 1:
3277 freshest = rdev;
3278 break;
3279 case 0:
3280 break;
3281 default:
3282 printk( KERN_ERR \
3283 "md: fatal superblock inconsistency in %s"
3284 " -- removing from array\n",
3285 bdevname(rdev->bdev,b));
3286 kick_rdev_from_array(rdev);
3287 }
3288
3289
3290 super_types[mddev->major_version].
3291 validate_super(mddev, freshest);
3292
3293 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003294 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003295 if (mddev->max_disks &&
3296 (rdev->desc_nr >= mddev->max_disks ||
3297 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003298 printk(KERN_WARNING
3299 "md: %s: %s: only %d devices permitted\n",
3300 mdname(mddev), bdevname(rdev->bdev, b),
3301 mddev->max_disks);
3302 kick_rdev_from_array(rdev);
3303 continue;
3304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (rdev != freshest)
3306 if (super_types[mddev->major_version].
3307 validate_super(mddev, rdev)) {
3308 printk(KERN_WARNING "md: kicking non-fresh %s"
3309 " from array!\n",
3310 bdevname(rdev->bdev,b));
3311 kick_rdev_from_array(rdev);
3312 continue;
3313 }
3314 if (mddev->level == LEVEL_MULTIPATH) {
3315 rdev->desc_nr = i++;
3316 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003317 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003318 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003319 rdev->raid_disk = -1;
3320 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
NeilBrown72e02072009-12-14 12:49:55 +11003325/* Read a fixed-point number.
3326 * Numbers in sysfs attributes should be in "standard" units where
3327 * possible, so time should be in seconds.
3328 * However we internally use a a much smaller unit such as
3329 * milliseconds or jiffies.
3330 * This function takes a decimal number with a possible fractional
3331 * component, and produces an integer which is the result of
3332 * multiplying that number by 10^'scale'.
3333 * all without any floating-point arithmetic.
3334 */
3335int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3336{
3337 unsigned long result = 0;
3338 long decimals = -1;
3339 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3340 if (*cp == '.')
3341 decimals = 0;
3342 else if (decimals < scale) {
3343 unsigned int value;
3344 value = *cp - '0';
3345 result = result * 10 + value;
3346 if (decimals >= 0)
3347 decimals++;
3348 }
3349 cp++;
3350 }
3351 if (*cp == '\n')
3352 cp++;
3353 if (*cp)
3354 return -EINVAL;
3355 if (decimals < 0)
3356 decimals = 0;
3357 while (decimals < scale) {
3358 result *= 10;
3359 decimals ++;
3360 }
3361 *res = result;
3362 return 0;
3363}
3364
3365
NeilBrown19052c02008-08-05 15:54:13 +10003366static void md_safemode_timeout(unsigned long data);
3367
NeilBrowneae17012005-11-08 21:39:23 -08003368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003369safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003370{
3371 int msec = (mddev->safemode_delay*1000)/HZ;
3372 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3373}
3374static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003375safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003376{
NeilBrown16f17b32006-06-26 00:27:37 -07003377 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003378
NeilBrown72e02072009-12-14 12:49:55 +11003379 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003380 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003381 if (msec == 0)
3382 mddev->safemode_delay = 0;
3383 else {
NeilBrown19052c02008-08-05 15:54:13 +10003384 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003385 mddev->safemode_delay = (msec*HZ)/1000;
3386 if (mddev->safemode_delay == 0)
3387 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003388 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003389 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003390 }
3391 return len;
3392}
3393static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003394__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003395
3396static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003397level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003398{
NeilBrown84fc4b52011-10-11 16:49:58 +11003399 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003400 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003401 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003402 else if (mddev->clevel[0])
3403 return sprintf(page, "%s\n", mddev->clevel);
3404 else if (mddev->level != LEVEL_NONE)
3405 return sprintf(page, "%d\n", mddev->level);
3406 else
3407 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003408}
3409
NeilBrownd9d166c2006-01-06 00:20:51 -08003410static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003411level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003412{
Dan Williamsf2859af2010-05-02 10:04:16 -07003413 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003414 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003415 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003416 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003417 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003418 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003419
3420 if (mddev->pers == NULL) {
3421 if (len == 0)
3422 return 0;
3423 if (len >= sizeof(mddev->clevel))
3424 return -ENOSPC;
3425 strncpy(mddev->clevel, buf, len);
3426 if (mddev->clevel[len-1] == '\n')
3427 len--;
3428 mddev->clevel[len] = 0;
3429 mddev->level = LEVEL_NONE;
3430 return rv;
3431 }
3432
3433 /* request to change the personality. Need to ensure:
3434 * - array is not engaged in resync/recovery/reshape
3435 * - old personality can be suspended
3436 * - new personality will access other array.
3437 */
3438
NeilBrownbb4f1e92010-08-08 21:18:03 +10003439 if (mddev->sync_thread ||
3440 mddev->reshape_position != MaxSector ||
3441 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003442 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003443
3444 if (!mddev->pers->quiesce) {
3445 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3446 mdname(mddev), mddev->pers->name);
3447 return -EINVAL;
3448 }
3449
3450 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003451 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003452 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003453 strncpy(clevel, buf, len);
3454 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003455 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003456 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003457 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003458 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003459
Dan Williamsf2859af2010-05-02 10:04:16 -07003460 if (request_module("md-%s", clevel) != 0)
3461 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003462 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003463 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003464 if (!pers || !try_module_get(pers->owner)) {
3465 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003466 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003467 return -EINVAL;
3468 }
3469 spin_unlock(&pers_lock);
3470
3471 if (pers == mddev->pers) {
3472 /* Nothing to do! */
3473 module_put(pers->owner);
3474 return rv;
3475 }
3476 if (!pers->takeover) {
3477 module_put(pers->owner);
3478 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003479 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003480 return -EINVAL;
3481 }
3482
NeilBrowndafb20f2012-03-19 12:46:39 +11003483 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003484 rdev->new_raid_disk = rdev->raid_disk;
3485
NeilBrown245f46c2009-03-31 14:39:39 +11003486 /* ->takeover must set new_* and/or delta_disks
3487 * if it succeeds, and may set them when it fails.
3488 */
3489 priv = pers->takeover(mddev);
3490 if (IS_ERR(priv)) {
3491 mddev->new_level = mddev->level;
3492 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003493 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003494 mddev->raid_disks -= mddev->delta_disks;
3495 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003496 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003497 module_put(pers->owner);
3498 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003499 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003500 return PTR_ERR(priv);
3501 }
3502
3503 /* Looks like we have a winner */
3504 mddev_suspend(mddev);
3505 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003506
3507 if (mddev->pers->sync_request == NULL &&
3508 pers->sync_request != NULL) {
3509 /* need to add the md_redundancy_group */
3510 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3511 printk(KERN_WARNING
3512 "md: cannot register extra attributes for %s\n",
3513 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003514 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003515 }
3516 if (mddev->pers->sync_request != NULL &&
3517 pers->sync_request == NULL) {
3518 /* need to remove the md_redundancy_group */
3519 if (mddev->to_remove == NULL)
3520 mddev->to_remove = &md_redundancy_group;
3521 }
3522
Trela Maciej54071b32010-03-08 16:02:42 +11003523 if (mddev->pers->sync_request == NULL &&
3524 mddev->external) {
3525 /* We are converting from a no-redundancy array
3526 * to a redundancy array and metadata is managed
3527 * externally so we need to be sure that writes
3528 * won't block due to a need to transition
3529 * clean->dirty
3530 * until external management is started.
3531 */
3532 mddev->in_sync = 0;
3533 mddev->safemode_delay = 0;
3534 mddev->safemode = 0;
3535 }
3536
NeilBrowndafb20f2012-03-19 12:46:39 +11003537 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003538 if (rdev->raid_disk < 0)
3539 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003540 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003541 rdev->new_raid_disk = -1;
3542 if (rdev->new_raid_disk == rdev->raid_disk)
3543 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003544 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003545 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003546 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003547 if (rdev->raid_disk < 0)
3548 continue;
3549 if (rdev->new_raid_disk == rdev->raid_disk)
3550 continue;
3551 rdev->raid_disk = rdev->new_raid_disk;
3552 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003553 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003554 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003555 if (sysfs_link_rdev(mddev, rdev))
3556 printk(KERN_WARNING "md: cannot register rd%d"
3557 " for %s after level change\n",
3558 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003559 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003560 }
3561
3562 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003563 mddev->pers = pers;
3564 mddev->private = priv;
3565 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3566 mddev->level = mddev->new_level;
3567 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003568 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003569 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003570 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003571 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003572 if (mddev->pers->sync_request == NULL) {
3573 /* this is now an array without redundancy, so
3574 * it must always be in_sync
3575 */
3576 mddev->in_sync = 1;
3577 del_timer_sync(&mddev->safemode_timer);
3578 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003579 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003580 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003581 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003582 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003583 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003584 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003585 return rv;
3586}
3587
3588static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003589__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003590
NeilBrownd4dbd022006-06-26 00:27:59 -07003591
3592static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003593layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003594{
3595 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003596 if (mddev->reshape_position != MaxSector &&
3597 mddev->layout != mddev->new_layout)
3598 return sprintf(page, "%d (%d)\n",
3599 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003600 return sprintf(page, "%d\n", mddev->layout);
3601}
3602
3603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003604layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003605{
3606 char *e;
3607 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003608
3609 if (!*buf || (*e && *e != '\n'))
3610 return -EINVAL;
3611
NeilBrownb3546032009-03-31 14:56:41 +11003612 if (mddev->pers) {
3613 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003614 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003615 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003616 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003617 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003618 if (err) {
3619 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003620 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003621 }
NeilBrownb3546032009-03-31 14:56:41 +11003622 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003623 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003624 if (mddev->reshape_position == MaxSector)
3625 mddev->layout = n;
3626 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003627 return len;
3628}
3629static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003630__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003631
3632
NeilBrowneae17012005-11-08 21:39:23 -08003633static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003634raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003635{
NeilBrownbb636542005-11-08 21:39:45 -08003636 if (mddev->raid_disks == 0)
3637 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003638 if (mddev->reshape_position != MaxSector &&
3639 mddev->delta_disks != 0)
3640 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3641 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003642 return sprintf(page, "%d\n", mddev->raid_disks);
3643}
3644
NeilBrownfd01b882011-10-11 16:47:53 +11003645static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003646
3647static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003648raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003649{
NeilBrownda943b992006-01-06 00:20:54 -08003650 char *e;
3651 int rv = 0;
3652 unsigned long n = simple_strtoul(buf, &e, 10);
3653
3654 if (!*buf || (*e && *e != '\n'))
3655 return -EINVAL;
3656
3657 if (mddev->pers)
3658 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003659 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003660 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003661 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003662
3663 rdev_for_each(rdev, mddev) {
3664 if (olddisks < n &&
3665 rdev->data_offset < rdev->new_data_offset)
3666 return -EINVAL;
3667 if (olddisks > n &&
3668 rdev->data_offset > rdev->new_data_offset)
3669 return -EINVAL;
3670 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003671 mddev->delta_disks = n - olddisks;
3672 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003673 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003674 } else
NeilBrownda943b992006-01-06 00:20:54 -08003675 mddev->raid_disks = n;
3676 return rv ? rv : len;
3677}
3678static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003679__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003680
NeilBrown24dd4692005-11-08 21:39:26 -08003681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003682chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003683{
NeilBrown08a02ec2007-05-09 02:35:38 -07003684 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003685 mddev->chunk_sectors != mddev->new_chunk_sectors)
3686 return sprintf(page, "%d (%d)\n",
3687 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003688 mddev->chunk_sectors << 9);
3689 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003690}
3691
3692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003693chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003694{
NeilBrown3b343802006-01-06 00:20:47 -08003695 char *e;
3696 unsigned long n = simple_strtoul(buf, &e, 10);
3697
NeilBrown3b343802006-01-06 00:20:47 -08003698 if (!*buf || (*e && *e != '\n'))
3699 return -EINVAL;
3700
NeilBrownb3546032009-03-31 14:56:41 +11003701 if (mddev->pers) {
3702 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003703 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003704 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003705 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003706 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003707 if (err) {
3708 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003709 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003710 }
NeilBrownb3546032009-03-31 14:56:41 +11003711 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003712 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003713 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003714 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003715 }
NeilBrown3b343802006-01-06 00:20:47 -08003716 return len;
3717}
3718static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003719__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003720
NeilBrowna94213b2006-06-26 00:28:00 -07003721static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003722resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003723{
NeilBrownd1a7c502009-03-31 15:24:32 +11003724 if (mddev->recovery_cp == MaxSector)
3725 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003726 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3727}
3728
3729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003730resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003731{
NeilBrowna94213b2006-06-26 00:28:00 -07003732 char *e;
3733 unsigned long long n = simple_strtoull(buf, &e, 10);
3734
NeilBrownb0986362011-05-11 15:52:21 +10003735 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003736 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003737 if (cmd_match(buf, "none"))
3738 n = MaxSector;
3739 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003740 return -EINVAL;
3741
3742 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003743 if (mddev->pers)
3744 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003745 return len;
3746}
3747static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003748__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003749
NeilBrown9e653b62006-06-26 00:27:58 -07003750/*
3751 * The array state can be:
3752 *
3753 * clear
3754 * No devices, no size, no level
3755 * Equivalent to STOP_ARRAY ioctl
3756 * inactive
3757 * May have some settings, but array is not active
3758 * all IO results in error
3759 * When written, doesn't tear down array, but just stops it
3760 * suspended (not supported yet)
3761 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003762 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003763 * readonly
3764 * no resync can happen. no superblocks get written.
3765 * write requests fail
3766 * read-auto
3767 * like readonly, but behaves like 'clean' on a write request.
3768 *
3769 * clean - no pending writes, but otherwise active.
3770 * When written to inactive array, starts without resync
3771 * If a write request arrives then
3772 * if metadata is known, mark 'dirty' and switch to 'active'.
3773 * if not known, block and switch to write-pending
3774 * If written to an active array that has pending writes, then fails.
3775 * active
3776 * fully active: IO and resync can be happening.
3777 * When written to inactive array, starts with resync
3778 *
3779 * write-pending
3780 * clean, but writes are blocked waiting for 'active' to be written.
3781 *
3782 * active-idle
3783 * like active, but no writes have been seen for a while (100msec).
3784 *
3785 */
3786enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3787 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003788static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003789 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3790 "write-pending", "active-idle", NULL };
3791
3792static int match_word(const char *word, char **list)
3793{
3794 int n;
3795 for (n=0; list[n]; n++)
3796 if (cmd_match(word, list[n]))
3797 break;
3798 return n;
3799}
3800
3801static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003802array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003803{
3804 enum array_state st = inactive;
3805
3806 if (mddev->pers)
3807 switch(mddev->ro) {
3808 case 1:
3809 st = readonly;
3810 break;
3811 case 2:
3812 st = read_auto;
3813 break;
3814 case 0:
3815 if (mddev->in_sync)
3816 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003817 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003818 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003819 else if (mddev->safemode)
3820 st = active_idle;
3821 else
3822 st = active;
3823 }
3824 else {
3825 if (list_empty(&mddev->disks) &&
3826 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003827 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003828 st = clear;
3829 else
3830 st = inactive;
3831 }
3832 return sprintf(page, "%s\n", array_states[st]);
3833}
3834
NeilBrowna05b7ea02012-07-19 15:59:18 +10003835static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3836static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003837static int do_md_run(struct mddev * mddev);
3838static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003839
3840static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003841array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003842{
3843 int err = -EINVAL;
3844 enum array_state st = match_word(buf, array_states);
3845 switch(st) {
3846 case bad_word:
3847 break;
3848 case clear:
3849 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003850 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003851 break;
3852 case inactive:
3853 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003854 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003855 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003856 else
NeilBrowne6910632008-02-06 01:39:51 -08003857 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003858 break;
3859 case suspended:
3860 break; /* not supported yet */
3861 case readonly:
3862 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003863 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003864 else {
3865 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003866 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003867 err = do_md_run(mddev);
3868 }
3869 break;
3870 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003871 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003872 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003873 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003874 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003875 err = restart_array(mddev);
3876 if (err == 0) {
3877 mddev->ro = 2;
3878 set_disk_ro(mddev->gendisk, 0);
3879 }
NeilBrown9e653b62006-06-26 00:27:58 -07003880 } else {
3881 mddev->ro = 2;
3882 err = do_md_run(mddev);
3883 }
3884 break;
3885 case clean:
3886 if (mddev->pers) {
3887 restart_array(mddev);
3888 spin_lock_irq(&mddev->write_lock);
3889 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003890 if (mddev->in_sync == 0) {
3891 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003892 if (mddev->safemode == 1)
3893 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003894 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003895 }
3896 err = 0;
3897 } else
3898 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003899 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003900 } else
3901 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003902 break;
3903 case active:
3904 if (mddev->pers) {
3905 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003906 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003907 wake_up(&mddev->sb_wait);
3908 err = 0;
3909 } else {
3910 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003911 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003912 err = do_md_run(mddev);
3913 }
3914 break;
3915 case write_pending:
3916 case active_idle:
3917 /* these cannot be set */
3918 break;
3919 }
3920 if (err)
3921 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003922 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003923 if (mddev->hold_active == UNTIL_IOCTL)
3924 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003925 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003926 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003927 }
NeilBrown9e653b62006-06-26 00:27:58 -07003928}
NeilBrown80ca3a42006-07-10 04:44:18 -07003929static struct md_sysfs_entry md_array_state =
3930__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003931
NeilBrown6d7ff732006-01-06 00:21:16 -08003932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003933max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003934 return sprintf(page, "%d\n",
3935 atomic_read(&mddev->max_corr_read_errors));
3936}
3937
3938static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003939max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003940{
3941 char *e;
3942 unsigned long n = simple_strtoul(buf, &e, 10);
3943
3944 if (*buf && (*e == 0 || *e == '\n')) {
3945 atomic_set(&mddev->max_corr_read_errors, n);
3946 return len;
3947 }
3948 return -EINVAL;
3949}
3950
3951static struct md_sysfs_entry max_corr_read_errors =
3952__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3953 max_corrected_read_errors_store);
3954
3955static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003956null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003957{
3958 return -EINVAL;
3959}
3960
3961static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003962new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003963{
3964 /* buf must be %d:%d\n? giving major and minor numbers */
3965 /* The new device is added to the array.
3966 * If the array has a persistent superblock, we read the
3967 * superblock to initialise info and check validity.
3968 * Otherwise, only checking done is that in bind_rdev_to_array,
3969 * which mainly checks size.
3970 */
3971 char *e;
3972 int major = simple_strtoul(buf, &e, 10);
3973 int minor;
3974 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003975 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003976 int err;
3977
3978 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3979 return -EINVAL;
3980 minor = simple_strtoul(e+1, &e, 10);
3981 if (*e && *e != '\n')
3982 return -EINVAL;
3983 dev = MKDEV(major, minor);
3984 if (major != MAJOR(dev) ||
3985 minor != MINOR(dev))
3986 return -EOVERFLOW;
3987
3988
3989 if (mddev->persistent) {
3990 rdev = md_import_device(dev, mddev->major_version,
3991 mddev->minor_version);
3992 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003993 struct md_rdev *rdev0
3994 = list_entry(mddev->disks.next,
3995 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003996 err = super_types[mddev->major_version]
3997 .load_super(rdev, rdev0, mddev->minor_version);
3998 if (err < 0)
3999 goto out;
4000 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004001 } else if (mddev->external)
4002 rdev = md_import_device(dev, -2, -1);
4003 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004004 rdev = md_import_device(dev, -1, -1);
4005
4006 if (IS_ERR(rdev))
4007 return PTR_ERR(rdev);
4008 err = bind_rdev_to_array(rdev, mddev);
4009 out:
4010 if (err)
4011 export_rdev(rdev);
4012 return err ? err : len;
4013}
4014
4015static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004016__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004017
4018static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004019bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004020{
4021 char *end;
4022 unsigned long chunk, end_chunk;
4023
4024 if (!mddev->bitmap)
4025 goto out;
4026 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4027 while (*buf) {
4028 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4029 if (buf == end) break;
4030 if (*end == '-') { /* range */
4031 buf = end + 1;
4032 end_chunk = simple_strtoul(buf, &end, 0);
4033 if (buf == end) break;
4034 }
4035 if (*end && !isspace(*end)) break;
4036 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004037 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004038 }
4039 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4040out:
4041 return len;
4042}
4043
4044static struct md_sysfs_entry md_bitmap =
4045__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4046
4047static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004048size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004049{
Andre Noll58c0fed2009-03-31 14:33:13 +11004050 return sprintf(page, "%llu\n",
4051 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004052}
4053
NeilBrownfd01b882011-10-11 16:47:53 +11004054static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004055
4056static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004057size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004058{
4059 /* If array is inactive, we can reduce the component size, but
4060 * not increase it (except from 0).
4061 * If array is active, we can try an on-line resize
4062 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004063 sector_t sectors;
4064 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004065
Andre Noll58c0fed2009-03-31 14:33:13 +11004066 if (err < 0)
4067 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004068 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004069 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004070 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004071 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004072 if (mddev->dev_sectors == 0 ||
4073 mddev->dev_sectors > sectors)
4074 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004075 else
4076 err = -ENOSPC;
4077 }
4078 return err ? err : len;
4079}
4080
4081static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004082__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004083
NeilBrown8bb93aa2006-01-06 00:20:50 -08004084
Masanari Iida83f0d772012-10-30 00:18:08 +09004085/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004086 * This is one of
4087 * 'none' for arrays with no metadata (good luck...)
4088 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004089 * or N.M for internally known formats
4090 */
4091static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004092metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004093{
4094 if (mddev->persistent)
4095 return sprintf(page, "%d.%d\n",
4096 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004097 else if (mddev->external)
4098 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004099 else
4100 return sprintf(page, "none\n");
4101}
4102
4103static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004104metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004105{
4106 int major, minor;
4107 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004108 /* Changing the details of 'external' metadata is
4109 * always permitted. Otherwise there must be
4110 * no devices attached to the array.
4111 */
4112 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4113 ;
4114 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115 return -EBUSY;
4116
4117 if (cmd_match(buf, "none")) {
4118 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004119 mddev->external = 0;
4120 mddev->major_version = 0;
4121 mddev->minor_version = 90;
4122 return len;
4123 }
4124 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004125 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004126 if (namelen >= sizeof(mddev->metadata_type))
4127 namelen = sizeof(mddev->metadata_type)-1;
4128 strncpy(mddev->metadata_type, buf+9, namelen);
4129 mddev->metadata_type[namelen] = 0;
4130 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4131 mddev->metadata_type[--namelen] = 0;
4132 mddev->persistent = 0;
4133 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004134 mddev->major_version = 0;
4135 mddev->minor_version = 90;
4136 return len;
4137 }
4138 major = simple_strtoul(buf, &e, 10);
4139 if (e==buf || *e != '.')
4140 return -EINVAL;
4141 buf = e+1;
4142 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004143 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004144 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004145 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146 return -ENOENT;
4147 mddev->major_version = major;
4148 mddev->minor_version = minor;
4149 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004150 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004151 return len;
4152}
4153
4154static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004155__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156
NeilBrowna35b0d62006-01-06 00:20:49 -08004157static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004158action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004159{
NeilBrown7eec3142005-11-08 21:39:44 -08004160 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004161 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4162 type = "frozen";
4163 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004164 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004165 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4166 type = "reshape";
4167 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004168 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4169 type = "resync";
4170 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4171 type = "check";
4172 else
4173 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004174 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004175 type = "recover";
4176 }
4177 return sprintf(page, "%s\n", type);
4178}
4179
4180static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004181action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004182{
NeilBrown7eec3142005-11-08 21:39:44 -08004183 if (!mddev->pers || !mddev->pers->sync_request)
4184 return -EINVAL;
4185
NeilBrownb6a9ce62009-05-26 09:41:17 +10004186 if (cmd_match(page, "frozen"))
4187 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4188 else
4189 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4190
4191 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004192 if (mddev->sync_thread) {
4193 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004194 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004195 }
NeilBrown03c902e2006-01-06 00:20:46 -08004196 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4197 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004198 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004199 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004200 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004201 else if (cmd_match(page, "recover")) {
4202 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4203 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4204 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004205 int err;
4206 if (mddev->pers->start_reshape == NULL)
4207 return -EINVAL;
4208 err = mddev->pers->start_reshape(mddev);
4209 if (err)
4210 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004211 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004212 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004213 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004214 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004215 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004216 return -EINVAL;
4217 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4218 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004219 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004220 if (mddev->ro == 2) {
4221 /* A write to sync_action is enough to justify
4222 * canceling read-auto mode
4223 */
4224 mddev->ro = 0;
4225 md_wakeup_thread(mddev->sync_thread);
4226 }
NeilBrown03c902e2006-01-06 00:20:46 -08004227 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004228 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004229 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004230 return len;
4231}
4232
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004233static struct md_sysfs_entry md_scan_mode =
4234__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4235
4236static ssize_t
4237last_sync_action_show(struct mddev *mddev, char *page)
4238{
4239 return sprintf(page, "%s\n", mddev->last_sync_action);
4240}
4241
4242static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4243
NeilBrown9d888832005-11-08 21:39:26 -08004244static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004245mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004246{
4247 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004248 (unsigned long long)
4249 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004250}
4251
NeilBrown80ca3a42006-07-10 04:44:18 -07004252static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004253
NeilBrown88202a02006-01-06 00:21:36 -08004254static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004255sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004256{
4257 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4258 mddev->sync_speed_min ? "local": "system");
4259}
4260
4261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004262sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004263{
4264 int min;
4265 char *e;
4266 if (strncmp(buf, "system", 6)==0) {
4267 mddev->sync_speed_min = 0;
4268 return len;
4269 }
4270 min = simple_strtoul(buf, &e, 10);
4271 if (buf == e || (*e && *e != '\n') || min <= 0)
4272 return -EINVAL;
4273 mddev->sync_speed_min = min;
4274 return len;
4275}
4276
4277static struct md_sysfs_entry md_sync_min =
4278__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4279
4280static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004281sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004282{
4283 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4284 mddev->sync_speed_max ? "local": "system");
4285}
4286
4287static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004288sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004289{
4290 int max;
4291 char *e;
4292 if (strncmp(buf, "system", 6)==0) {
4293 mddev->sync_speed_max = 0;
4294 return len;
4295 }
4296 max = simple_strtoul(buf, &e, 10);
4297 if (buf == e || (*e && *e != '\n') || max <= 0)
4298 return -EINVAL;
4299 mddev->sync_speed_max = max;
4300 return len;
4301}
4302
4303static struct md_sysfs_entry md_sync_max =
4304__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4305
Iustin Popd7f3d292007-10-16 23:30:54 -07004306static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004307degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004308{
4309 return sprintf(page, "%d\n", mddev->degraded);
4310}
4311static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004312
4313static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004314sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004315{
4316 return sprintf(page, "%d\n", mddev->parallel_resync);
4317}
4318
4319static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004320sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004321{
4322 long n;
4323
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004324 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004325 return -EINVAL;
4326
4327 if (n != 0 && n != 1)
4328 return -EINVAL;
4329
4330 mddev->parallel_resync = n;
4331
4332 if (mddev->sync_thread)
4333 wake_up(&resync_wait);
4334
4335 return len;
4336}
4337
4338/* force parallel resync, even with shared block devices */
4339static struct md_sysfs_entry md_sync_force_parallel =
4340__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4341 sync_force_parallel_show, sync_force_parallel_store);
4342
4343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004345{
4346 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004347 if (mddev->curr_resync == 0)
4348 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004349 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4350 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004351 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004352 db = resync - mddev->resync_mark_cnt;
4353 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004354}
4355
NeilBrown80ca3a42006-07-10 04:44:18 -07004356static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004357
4358static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004359sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004360{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004361 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004362
NeilBrownacb180b2009-04-14 16:28:34 +10004363 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4364 return sprintf(page, "none\n");
4365
NeilBrown72f36d52012-10-11 14:25:57 +11004366 if (mddev->curr_resync == 1 ||
4367 mddev->curr_resync == 2)
4368 return sprintf(page, "delayed\n");
4369
NeilBrownc804cde2012-05-21 09:28:33 +10004370 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4371 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004372 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004373 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004374 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004375
NeilBrownacb180b2009-04-14 16:28:34 +10004376 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004377 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004378}
4379
NeilBrown80ca3a42006-07-10 04:44:18 -07004380static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004381
NeilBrowne464eaf2006-03-27 01:18:14 -08004382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004383min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004384{
4385 return sprintf(page, "%llu\n",
4386 (unsigned long long)mddev->resync_min);
4387}
4388static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004389min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004390{
4391 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004392 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004393 return -EINVAL;
4394 if (min > mddev->resync_max)
4395 return -EINVAL;
4396 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4397 return -EBUSY;
4398
4399 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004400 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004401 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004402 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004403 return -EINVAL;
4404 }
4405 mddev->resync_min = min;
4406
4407 return len;
4408}
4409
4410static struct md_sysfs_entry md_min_sync =
4411__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4412
4413static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004414max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004415{
4416 if (mddev->resync_max == MaxSector)
4417 return sprintf(page, "max\n");
4418 else
4419 return sprintf(page, "%llu\n",
4420 (unsigned long long)mddev->resync_max);
4421}
4422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004424{
4425 if (strncmp(buf, "max", 3) == 0)
4426 mddev->resync_max = MaxSector;
4427 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004428 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004429 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004430 return -EINVAL;
4431 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004432 return -EINVAL;
4433 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004434 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004435 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4436 return -EBUSY;
4437
4438 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004439 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004440 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004441 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004442 return -EINVAL;
4443 }
4444 mddev->resync_max = max;
4445 }
4446 wake_up(&mddev->recovery_wait);
4447 return len;
4448}
4449
4450static struct md_sysfs_entry md_max_sync =
4451__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4452
4453static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004454suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004455{
4456 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4457}
4458
4459static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004460suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004461{
4462 char *e;
4463 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004464 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004465
NeilBrownb8d966e2009-07-01 11:14:04 +10004466 if (mddev->pers == NULL ||
4467 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004468 return -EINVAL;
4469 if (buf == e || (*e && *e != '\n'))
4470 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004471
4472 mddev->suspend_lo = new;
4473 if (new >= old)
4474 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004475 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004476 else {
4477 /* Expanding suspended region - need to wait */
4478 mddev->pers->quiesce(mddev, 1);
4479 mddev->pers->quiesce(mddev, 0);
4480 }
4481 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004482}
4483static struct md_sysfs_entry md_suspend_lo =
4484__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4485
4486
4487static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004488suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004489{
4490 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4491}
4492
4493static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004494suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004495{
4496 char *e;
4497 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004498 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004499
NeilBrownb8d966e2009-07-01 11:14:04 +10004500 if (mddev->pers == NULL ||
4501 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004502 return -EINVAL;
4503 if (buf == e || (*e && *e != '\n'))
4504 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004505
4506 mddev->suspend_hi = new;
4507 if (new <= old)
4508 /* Shrinking suspended region */
4509 mddev->pers->quiesce(mddev, 2);
4510 else {
4511 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004512 mddev->pers->quiesce(mddev, 1);
4513 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004514 }
4515 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004516}
4517static struct md_sysfs_entry md_suspend_hi =
4518__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4519
NeilBrown08a02ec2007-05-09 02:35:38 -07004520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004521reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004522{
4523 if (mddev->reshape_position != MaxSector)
4524 return sprintf(page, "%llu\n",
4525 (unsigned long long)mddev->reshape_position);
4526 strcpy(page, "none\n");
4527 return 5;
4528}
4529
4530static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004531reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004532{
NeilBrownc6563a82012-05-21 09:27:00 +10004533 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004534 char *e;
4535 unsigned long long new = simple_strtoull(buf, &e, 10);
4536 if (mddev->pers)
4537 return -EBUSY;
4538 if (buf == e || (*e && *e != '\n'))
4539 return -EINVAL;
4540 mddev->reshape_position = new;
4541 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004542 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004543 mddev->new_level = mddev->level;
4544 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004545 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004546 rdev_for_each(rdev, mddev)
4547 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004548 return len;
4549}
4550
4551static struct md_sysfs_entry md_reshape_position =
4552__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4553 reshape_position_store);
4554
Dan Williamsb522adc2009-03-31 15:00:31 +11004555static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004556reshape_direction_show(struct mddev *mddev, char *page)
4557{
4558 return sprintf(page, "%s\n",
4559 mddev->reshape_backwards ? "backwards" : "forwards");
4560}
4561
4562static ssize_t
4563reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4564{
4565 int backwards = 0;
4566 if (cmd_match(buf, "forwards"))
4567 backwards = 0;
4568 else if (cmd_match(buf, "backwards"))
4569 backwards = 1;
4570 else
4571 return -EINVAL;
4572 if (mddev->reshape_backwards == backwards)
4573 return len;
4574
4575 /* check if we are allowed to change */
4576 if (mddev->delta_disks)
4577 return -EBUSY;
4578
4579 if (mddev->persistent &&
4580 mddev->major_version == 0)
4581 return -EINVAL;
4582
4583 mddev->reshape_backwards = backwards;
4584 return len;
4585}
4586
4587static struct md_sysfs_entry md_reshape_direction =
4588__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4589 reshape_direction_store);
4590
4591static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004592array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004593{
4594 if (mddev->external_size)
4595 return sprintf(page, "%llu\n",
4596 (unsigned long long)mddev->array_sectors/2);
4597 else
4598 return sprintf(page, "default\n");
4599}
4600
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004603{
4604 sector_t sectors;
4605
4606 if (strncmp(buf, "default", 7) == 0) {
4607 if (mddev->pers)
4608 sectors = mddev->pers->size(mddev, 0, 0);
4609 else
4610 sectors = mddev->array_sectors;
4611
4612 mddev->external_size = 0;
4613 } else {
4614 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4615 return -EINVAL;
4616 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004617 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004618
4619 mddev->external_size = 1;
4620 }
4621
4622 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004623 if (mddev->pers) {
4624 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004625 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004626 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004627 return len;
4628}
4629
4630static struct md_sysfs_entry md_array_size =
4631__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4632 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004633
NeilBrowneae17012005-11-08 21:39:23 -08004634static struct attribute *md_default_attrs[] = {
4635 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004636 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004637 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004638 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004639 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004640 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004641 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004642 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004643 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004644 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004645 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004646 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004647 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004648 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004649 NULL,
4650};
4651
4652static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004653 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004654 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004655 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004656 &md_sync_min.attr,
4657 &md_sync_max.attr,
4658 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004659 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004660 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004661 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004662 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004663 &md_suspend_lo.attr,
4664 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004665 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004666 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004667 NULL,
4668};
NeilBrown411036f2005-11-08 21:39:40 -08004669static struct attribute_group md_redundancy_group = {
4670 .name = NULL,
4671 .attrs = md_redundancy_attrs,
4672};
4673
NeilBrowneae17012005-11-08 21:39:23 -08004674
4675static ssize_t
4676md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4677{
4678 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004679 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004680 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004681
4682 if (!entry->show)
4683 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004684 spin_lock(&all_mddevs_lock);
4685 if (list_empty(&mddev->all_mddevs)) {
4686 spin_unlock(&all_mddevs_lock);
4687 return -EBUSY;
4688 }
4689 mddev_get(mddev);
4690 spin_unlock(&all_mddevs_lock);
4691
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004692 rv = mddev_lock(mddev);
4693 if (!rv) {
4694 rv = entry->show(mddev, page);
4695 mddev_unlock(mddev);
4696 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004697 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004698 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004699}
4700
4701static ssize_t
4702md_attr_store(struct kobject *kobj, struct attribute *attr,
4703 const char *page, size_t length)
4704{
4705 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004706 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004707 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004708
4709 if (!entry->store)
4710 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004711 if (!capable(CAP_SYS_ADMIN))
4712 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004713 spin_lock(&all_mddevs_lock);
4714 if (list_empty(&mddev->all_mddevs)) {
4715 spin_unlock(&all_mddevs_lock);
4716 return -EBUSY;
4717 }
4718 mddev_get(mddev);
4719 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004720 if (entry->store == new_dev_store)
4721 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004722 rv = mddev_lock(mddev);
4723 if (!rv) {
4724 rv = entry->store(mddev, page, length);
4725 mddev_unlock(mddev);
4726 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004727 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004728 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004729}
4730
4731static void md_free(struct kobject *ko)
4732{
NeilBrownfd01b882011-10-11 16:47:53 +11004733 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004734
4735 if (mddev->sysfs_state)
4736 sysfs_put(mddev->sysfs_state);
4737
4738 if (mddev->gendisk) {
4739 del_gendisk(mddev->gendisk);
4740 put_disk(mddev->gendisk);
4741 }
4742 if (mddev->queue)
4743 blk_cleanup_queue(mddev->queue);
4744
NeilBrowneae17012005-11-08 21:39:23 -08004745 kfree(mddev);
4746}
4747
Emese Revfy52cf25d2010-01-19 02:58:23 +01004748static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004749 .show = md_attr_show,
4750 .store = md_attr_store,
4751};
4752static struct kobj_type md_ktype = {
4753 .release = md_free,
4754 .sysfs_ops = &md_sysfs_ops,
4755 .default_attrs = md_default_attrs,
4756};
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758int mdp_major = 0;
4759
Dan Williams5fd3a172009-03-04 00:57:25 -07004760static void mddev_delayed_delete(struct work_struct *ws)
4761{
NeilBrownfd01b882011-10-11 16:47:53 +11004762 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004763
NeilBrown43a70502009-12-14 12:49:55 +11004764 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004765 kobject_del(&mddev->kobj);
4766 kobject_put(&mddev->kobj);
4767}
4768
NeilBrownefeb53c2009-01-09 08:31:10 +11004769static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004771 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004772 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004774 int partitioned;
4775 int shift;
4776 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004777 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004780 return -ENODEV;
4781
4782 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4783 shift = partitioned ? MdpMinorShift : 0;
4784 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Tejun Heoe804ac72010-10-15 15:36:08 +02004786 /* wait for any previous instance of this device to be
4787 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004788 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004789 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004790
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004791 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004792 error = -EEXIST;
4793 if (mddev->gendisk)
4794 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004795
4796 if (name) {
4797 /* Need to ensure that 'name' is not a duplicate.
4798 */
NeilBrownfd01b882011-10-11 16:47:53 +11004799 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004800 spin_lock(&all_mddevs_lock);
4801
4802 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4803 if (mddev2->gendisk &&
4804 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4805 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004806 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004807 }
4808 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 }
NeilBrown8b765392009-01-09 08:31:08 +11004810
NeilBrown0909dc42009-07-01 12:27:21 +10004811 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004812 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004813 if (!mddev->queue)
4814 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004815 mddev->queue->queuedata = mddev;
4816
NeilBrown409c57f2009-03-31 14:39:39 +11004817 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004818 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 disk = alloc_disk(1 << shift);
4821 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004822 blk_cleanup_queue(mddev->queue);
4823 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004824 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004826 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004828 if (name)
4829 strcpy(disk->disk_name, name);
4830 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004832 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 disk->fops = &md_fops;
4835 disk->private_data = mddev;
4836 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004837 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004838 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004839 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004840 * remove it now.
4841 */
4842 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004844 /* As soon as we call add_disk(), another thread could get
4845 * through to md_open, so make sure it doesn't get too far
4846 */
4847 mutex_lock(&mddev->open_mutex);
4848 add_disk(disk);
4849
Tejun Heoed9e1982008-08-25 19:56:05 +09004850 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4851 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004852 if (error) {
4853 /* This isn't possible, but as kobject_init_and_add is marked
4854 * __must_check, we must do something with the result
4855 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004856 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4857 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004858 error = 0;
4859 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004860 if (mddev->kobj.sd &&
4861 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004862 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004863 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004864 abort:
4865 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004866 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004867 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004868 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004869 }
NeilBrownd3374822009-01-09 08:31:10 +11004870 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004871 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004872}
4873
4874static struct kobject *md_probe(dev_t dev, int *part, void *data)
4875{
4876 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 return NULL;
4878}
4879
NeilBrownefeb53c2009-01-09 08:31:10 +11004880static int add_named_array(const char *val, struct kernel_param *kp)
4881{
4882 /* val must be "md_*" where * is not all digits.
4883 * We allocate an array with a large free minor number, and
4884 * set the name to val. val must not already be an active name.
4885 */
4886 int len = strlen(val);
4887 char buf[DISK_NAME_LEN];
4888
4889 while (len && val[len-1] == '\n')
4890 len--;
4891 if (len >= DISK_NAME_LEN)
4892 return -E2BIG;
4893 strlcpy(buf, val, len+1);
4894 if (strncmp(buf, "md_", 3) != 0)
4895 return -EINVAL;
4896 return md_alloc(0, buf);
4897}
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899static void md_safemode_timeout(unsigned long data)
4900{
NeilBrownfd01b882011-10-11 16:47:53 +11004901 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
Neil Brown0fd62b82008-06-28 08:31:36 +10004903 if (!atomic_read(&mddev->writes_pending)) {
4904 mddev->safemode = 1;
4905 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004906 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 md_wakeup_thread(mddev->thread);
4909}
4910
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004911static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
NeilBrownfd01b882011-10-11 16:47:53 +11004913int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
NeilBrown2604b702006-01-06 00:20:36 -08004915 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004916 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004917 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
NeilBrowna757e642005-04-16 15:26:42 -07004919 if (list_empty(&mddev->disks))
4920 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 if (mddev->pers)
4924 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004925 /* Cannot run until previous stop completes properly */
4926 if (mddev->sysfs_active)
4927 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 /*
4930 * Analyze all RAID superblock(s)
4931 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004932 if (!mddev->raid_disks) {
4933 if (!mddev->persistent)
4934 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004935 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
NeilBrownd9d166c2006-01-06 00:20:51 -08004938 if (mddev->level != LEVEL_NONE)
4939 request_module("md-level-%d", mddev->level);
4940 else if (mddev->clevel[0])
4941 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 /*
4944 * Drop all container device buffers, from now on
4945 * the only valid external interface is through the md
4946 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004948 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004949 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 continue;
4951 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004952 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004953
4954 /* perform some consistency tests on the device.
4955 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004956 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004957 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004958 if (rdev->meta_bdev) {
4959 /* Nothing to check */;
4960 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004961 if (mddev->dev_sectors &&
4962 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004963 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004964 printk("md: %s: data overlaps metadata\n",
4965 mdname(mddev));
4966 return -EINVAL;
4967 }
4968 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004969 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004970 > rdev->data_offset) {
4971 printk("md: %s: metadata overlaps data\n",
4972 mdname(mddev));
4973 return -EINVAL;
4974 }
4975 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004976 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
4978
NeilBrowna167f662010-10-26 18:31:13 +11004979 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004980 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004983 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004984 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004986 if (mddev->level != LEVEL_NONE)
4987 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4988 mddev->level);
4989 else
4990 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4991 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 return -EINVAL;
4993 }
NeilBrown2604b702006-01-06 00:20:36 -08004994 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004996 if (mddev->level != pers->level) {
4997 mddev->level = pers->level;
4998 mddev->new_level = pers->level;
4999 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005000 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
NeilBrownf6705572006-03-27 01:18:11 -08005002 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005003 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005004 /* This personality cannot handle reshaping... */
5005 mddev->pers = NULL;
5006 module_put(pers->owner);
5007 return -EINVAL;
5008 }
5009
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005010 if (pers->sync_request) {
5011 /* Warn if this is a potentially silly
5012 * configuration.
5013 */
5014 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005015 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005016 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005017
NeilBrowndafb20f2012-03-19 12:46:39 +11005018 rdev_for_each(rdev, mddev)
5019 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005020 if (rdev < rdev2 &&
5021 rdev->bdev->bd_contains ==
5022 rdev2->bdev->bd_contains) {
5023 printk(KERN_WARNING
5024 "%s: WARNING: %s appears to be"
5025 " on the same physical disk as"
5026 " %s.\n",
5027 mdname(mddev),
5028 bdevname(rdev->bdev,b),
5029 bdevname(rdev2->bdev,b2));
5030 warned = 1;
5031 }
5032 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005033
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005034 if (warned)
5035 printk(KERN_WARNING
5036 "True protection against single-disk"
5037 " failure might be compromised.\n");
5038 }
5039
NeilBrown657390d2005-08-26 18:34:16 -07005040 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005041 /* may be over-ridden by personality */
5042 mddev->resync_max_sectors = mddev->dev_sectors;
5043
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005044 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
NeilBrown0f9552b2009-12-30 12:08:50 +11005046 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005047 mddev->ro = 2; /* read-only, but switch on first write */
5048
NeilBrownb15c2e52006-01-06 00:20:16 -08005049 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005050 if (err)
5051 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005052 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5053 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5054 " but 'external_size' not in effect?\n", __func__);
5055 printk(KERN_ERR
5056 "md: invalid array_size %llu > default size %llu\n",
5057 (unsigned long long)mddev->array_sectors / 2,
5058 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5059 err = -EINVAL;
5060 mddev->pers->stop(mddev);
5061 }
NeilBrownef99bf42012-05-22 13:55:08 +10005062 if (err == 0 && mddev->pers->sync_request &&
5063 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005064 err = bitmap_create(mddev);
5065 if (err) {
5066 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5067 mdname(mddev), err);
5068 mddev->pers->stop(mddev);
5069 }
5070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 module_put(mddev->pers->owner);
5073 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005074 bitmap_destroy(mddev);
5075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005077 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005078 if (mddev->kobj.sd &&
5079 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005080 printk(KERN_WARNING
5081 "md: cannot register extra attributes for %s\n",
5082 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005083 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005084 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005085 mddev->ro = 0;
5086
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005088 atomic_set(&mddev->max_corr_read_errors,
5089 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 mddev->safemode = 0;
5091 mddev->safemode_timer.function = md_safemode_timeout;
5092 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005093 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005095 smp_wmb();
5096 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005097 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005098 if (rdev->raid_disk >= 0)
5099 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005100 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5103
NeilBrown7a0a5352013-08-27 16:28:23 +10005104 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005105 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrownd7603b72006-01-06 00:20:30 -08005107 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005108 sysfs_notify_dirent_safe(mddev->sysfs_state);
5109 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005110 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return 0;
5112}
NeilBrown390ee602010-06-01 19:37:27 +10005113EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
NeilBrownfd01b882011-10-11 16:47:53 +11005115static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005116{
5117 int err;
5118
5119 err = md_run(mddev);
5120 if (err)
5121 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005122 err = bitmap_load(mddev);
5123 if (err) {
5124 bitmap_destroy(mddev);
5125 goto out;
5126 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005127
5128 md_wakeup_thread(mddev->thread);
5129 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5130
NeilBrownfe60b012010-03-29 11:10:42 +11005131 set_capacity(mddev->gendisk, mddev->array_sectors);
5132 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005133 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005134 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5135out:
5136 return err;
5137}
5138
NeilBrownfd01b882011-10-11 16:47:53 +11005139static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
5141 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
Andre Noll80fab1d2008-07-11 22:02:21 +10005143 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005145 return -ENXIO;
5146 if (!mddev->pers)
5147 return -EINVAL;
5148 if (!mddev->ro)
5149 return -EBUSY;
5150 mddev->safemode = 0;
5151 mddev->ro = 0;
5152 set_disk_ro(disk, 0);
5153 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5154 mdname(mddev));
5155 /* Kick recovery or resync if necessary */
5156 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5157 md_wakeup_thread(mddev->thread);
5158 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005159 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
NeilBrownacc55e22006-06-26 00:27:47 -07005163/* similar to deny_write_access, but accounts for our holding a reference
5164 * to the file ourselves */
5165static int deny_bitmap_write_access(struct file * file)
5166{
5167 struct inode *inode = file->f_mapping->host;
5168
5169 spin_lock(&inode->i_lock);
5170 if (atomic_read(&inode->i_writecount) > 1) {
5171 spin_unlock(&inode->i_lock);
5172 return -ETXTBSY;
5173 }
5174 atomic_set(&inode->i_writecount, -1);
5175 spin_unlock(&inode->i_lock);
5176
5177 return 0;
5178}
5179
NeilBrown43a70502009-12-14 12:49:55 +11005180void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005181{
5182 struct inode *inode = file->f_mapping->host;
5183
5184 spin_lock(&inode->i_lock);
5185 atomic_set(&inode->i_writecount, 1);
5186 spin_unlock(&inode->i_lock);
5187}
5188
NeilBrownfd01b882011-10-11 16:47:53 +11005189static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005190{
5191 mddev->array_sectors = 0;
5192 mddev->external_size = 0;
5193 mddev->dev_sectors = 0;
5194 mddev->raid_disks = 0;
5195 mddev->recovery_cp = 0;
5196 mddev->resync_min = 0;
5197 mddev->resync_max = MaxSector;
5198 mddev->reshape_position = MaxSector;
5199 mddev->external = 0;
5200 mddev->persistent = 0;
5201 mddev->level = LEVEL_NONE;
5202 mddev->clevel[0] = 0;
5203 mddev->flags = 0;
5204 mddev->ro = 0;
5205 mddev->metadata_type[0] = 0;
5206 mddev->chunk_sectors = 0;
5207 mddev->ctime = mddev->utime = 0;
5208 mddev->layout = 0;
5209 mddev->max_disks = 0;
5210 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005211 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005212 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005213 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005214 mddev->new_level = LEVEL_NONE;
5215 mddev->new_layout = 0;
5216 mddev->new_chunk_sectors = 0;
5217 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005218 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005219 mddev->suspend_lo = mddev->suspend_hi = 0;
5220 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5221 mddev->recovery = 0;
5222 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005223 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005224 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005225 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005226 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005227 mddev->bitmap_info.offset = 0;
5228 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005229 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005230 mddev->bitmap_info.chunksize = 0;
5231 mddev->bitmap_info.daemon_sleep = 0;
5232 mddev->bitmap_info.max_write_behind = 0;
5233}
5234
NeilBrownfd01b882011-10-11 16:47:53 +11005235static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005236{
NeilBrown6b6204e2013-05-09 09:48:30 +10005237 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005238 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005239 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005240 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005241 }
5242
5243 del_timer_sync(&mddev->safemode_timer);
5244
5245 bitmap_flush(mddev);
5246 md_super_wait(mddev);
5247
NeilBrownb6d428c2013-04-24 11:42:42 +10005248 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005249 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005250 /* mark array as shutdown cleanly */
5251 mddev->in_sync = 1;
5252 md_update_sb(mddev, 1);
5253 }
5254}
NeilBrowndefad612011-01-14 09:14:33 +11005255
NeilBrownfd01b882011-10-11 16:47:53 +11005256void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005257{
NeilBrown29f097c2013-11-14 17:54:51 +11005258 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005259 __md_stop_writes(mddev);
5260 mddev_unlock(mddev);
5261}
NeilBrown390ee602010-06-01 19:37:27 +10005262EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005263
NeilBrown5eff3c42012-11-19 10:47:48 +11005264static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005265{
NeilBrown0ca69882011-01-14 09:14:33 +11005266 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005267 mddev->pers->stop(mddev);
5268 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5269 mddev->to_remove = &md_redundancy_group;
5270 module_put(mddev->pers->owner);
5271 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005272 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005273}
NeilBrown5eff3c42012-11-19 10:47:48 +11005274
5275void md_stop(struct mddev *mddev)
5276{
5277 /* stop the array and free an attached data structures.
5278 * This is called from dm-raid
5279 */
5280 __md_stop(mddev);
5281 bitmap_destroy(mddev);
5282 if (mddev->bio_set)
5283 bioset_free(mddev->bio_set);
5284}
5285
NeilBrown390ee602010-06-01 19:37:27 +10005286EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005287
NeilBrowna05b7ea02012-07-19 15:59:18 +10005288static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005289{
5290 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005291 int did_freeze = 0;
5292
5293 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5294 did_freeze = 1;
5295 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5296 md_wakeup_thread(mddev->thread);
5297 }
5298 if (mddev->sync_thread) {
5299 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5300 /* Thread might be blocked waiting for metadata update
5301 * which will now never happen */
5302 wake_up_process(mddev->sync_thread->tsk);
5303 }
5304 mddev_unlock(mddev);
5305 wait_event(resync_wait, mddev->sync_thread == NULL);
5306 mddev_lock_nointr(mddev);
5307
NeilBrowna4bd82d2010-03-29 13:23:10 +11005308 mutex_lock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005309 if (atomic_read(&mddev->openers) > !!bdev ||
5310 mddev->sync_thread ||
5311 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005312 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005313 if (did_freeze) {
5314 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5315 md_wakeup_thread(mddev->thread);
5316 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005317 err = -EBUSY;
5318 goto out;
5319 }
5320 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005321 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005322
5323 err = -ENXIO;
5324 if (mddev->ro==1)
5325 goto out;
5326 mddev->ro = 1;
5327 set_disk_ro(mddev->gendisk, 1);
5328 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005329 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005330 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005331 }
5332out:
5333 mutex_unlock(&mddev->open_mutex);
5334 return err;
5335}
5336
NeilBrown9e653b62006-06-26 00:27:58 -07005337/* mode:
5338 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005339 * 2 - stop but do not disassemble array
5340 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005341static int do_md_stop(struct mddev * mddev, int mode,
5342 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005345 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005346 int did_freeze = 0;
5347
5348 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5349 did_freeze = 1;
5350 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5351 md_wakeup_thread(mddev->thread);
5352 }
5353 if (mddev->sync_thread) {
5354 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5355 /* Thread might be blocked waiting for metadata update
5356 * which will now never happen */
5357 wake_up_process(mddev->sync_thread->tsk);
5358 }
5359 mddev_unlock(mddev);
5360 wait_event(resync_wait, mddev->sync_thread == NULL);
5361 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
NeilBrownc8c00a62009-08-10 12:50:52 +10005363 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005364 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005365 mddev->sysfs_active ||
5366 mddev->sync_thread ||
5367 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005368 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005369 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005370 if (did_freeze) {
5371 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5372 md_wakeup_thread(mddev->thread);
5373 }
NeilBrown260fa032013-08-27 16:44:13 +10005374 return -EBUSY;
5375 }
NeilBrown6e17b022010-08-07 21:41:19 +10005376 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005377 if (mddev->ro)
5378 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005379
NeilBrowndefad612011-01-14 09:14:33 +11005380 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005381 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005382 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005383 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005384
NeilBrowna4bd82d2010-03-29 13:23:10 +11005385 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005386 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005387
NeilBrowndafb20f2012-03-19 12:46:39 +11005388 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005389 if (rdev->raid_disk >= 0)
5390 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005391
NeilBrowna4bd82d2010-03-29 13:23:10 +11005392 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005393 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005394 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005395 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005396
NeilBrowna4bd82d2010-03-29 13:23:10 +11005397 if (mddev->ro)
5398 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005399 } else
5400 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 /*
5402 * Free resources if final stop
5403 */
NeilBrown9e653b62006-06-26 00:27:58 -07005404 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5406
NeilBrown978f9462006-02-02 14:28:05 -08005407 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005408 if (mddev->bitmap_info.file) {
5409 restore_bitmap_write_access(mddev->bitmap_info.file);
5410 fput(mddev->bitmap_info.file);
5411 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005412 }
NeilBrownc3d97142009-12-14 12:49:52 +11005413 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 export_array(mddev);
5416
NeilBrown6177b472010-03-29 11:37:13 +11005417 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005418 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005419 if (mddev->hold_active == UNTIL_STOP)
5420 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005421 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005422 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005423 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005424 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426}
5427
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005428#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005429static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430{
NeilBrown3cb03002011-10-11 16:45:26 +11005431 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 int err;
5433
NeilBrowna757e642005-04-16 15:26:42 -07005434 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 printk(KERN_INFO "md: running: ");
5438
NeilBrowndafb20f2012-03-19 12:46:39 +11005439 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 char b[BDEVNAME_SIZE];
5441 printk("<%s>", bdevname(rdev->bdev,b));
5442 }
5443 printk("\n");
5444
NeilBrownd710e132008-10-13 11:55:12 +11005445 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 if (err) {
5447 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005448 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450}
5451
5452/*
5453 * lets try to run arrays based on all disks that have arrived
5454 * until now. (those are in pending_raid_disks)
5455 *
5456 * the method: pick the first pending disk, collect all disks with
5457 * the same UUID, remove all from the pending list and put them into
5458 * the 'same_array' list. Then order this list based on superblock
5459 * update time (freshest comes first), kick out 'old' disks and
5460 * compare superblocks. If everything's fine then run it.
5461 *
5462 * If "unit" is allocated, then bump its reference count
5463 */
5464static void autorun_devices(int part)
5465{
NeilBrown3cb03002011-10-11 16:45:26 +11005466 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005467 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 char b[BDEVNAME_SIZE];
5469
5470 printk(KERN_INFO "md: autorun ...\n");
5471 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005472 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005474 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005476 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 printk(KERN_INFO "md: considering %s ...\n",
5479 bdevname(rdev0->bdev,b));
5480 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005481 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (super_90_load(rdev, rdev0, 0) >= 0) {
5483 printk(KERN_INFO "md: adding %s ...\n",
5484 bdevname(rdev->bdev,b));
5485 list_move(&rdev->same_set, &candidates);
5486 }
5487 /*
5488 * now we have a set of devices, with all of them having
5489 * mostly sane superblocks. It's time to allocate the
5490 * mddev.
5491 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005492 if (part) {
5493 dev = MKDEV(mdp_major,
5494 rdev0->preferred_minor << MdpMinorShift);
5495 unit = MINOR(dev) >> MdpMinorShift;
5496 } else {
5497 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5498 unit = MINOR(dev);
5499 }
5500 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5502 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5503 break;
5504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 md_probe(dev, NULL, NULL);
5507 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005508 if (!mddev || !mddev->gendisk) {
5509 if (mddev)
5510 mddev_put(mddev);
5511 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 "md: cannot allocate memory for md drive.\n");
5513 break;
5514 }
5515 if (mddev_lock(mddev))
5516 printk(KERN_WARNING "md: %s locked, cannot run\n",
5517 mdname(mddev));
5518 else if (mddev->raid_disks || mddev->major_version
5519 || !list_empty(&mddev->disks)) {
5520 printk(KERN_WARNING
5521 "md: %s already running, cannot run %s\n",
5522 mdname(mddev), bdevname(rdev0->bdev,b));
5523 mddev_unlock(mddev);
5524 } else {
5525 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005526 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005527 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 list_del_init(&rdev->same_set);
5529 if (bind_rdev_to_array(rdev, mddev))
5530 export_rdev(rdev);
5531 }
5532 autorun_array(mddev);
5533 mddev_unlock(mddev);
5534 }
5535 /* on success, candidates will be empty, on error
5536 * it won't...
5537 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005538 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005539 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 mddev_put(mddev);
5543 }
5544 printk(KERN_INFO "md: ... autorun DONE.\n");
5545}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005546#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548static int get_version(void __user * arg)
5549{
5550 mdu_version_t ver;
5551
5552 ver.major = MD_MAJOR_VERSION;
5553 ver.minor = MD_MINOR_VERSION;
5554 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5555
5556 if (copy_to_user(arg, &ver, sizeof(ver)))
5557 return -EFAULT;
5558
5559 return 0;
5560}
5561
NeilBrownfd01b882011-10-11 16:47:53 +11005562static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005565 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005566 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
NeilBrown1ca69c42012-10-11 13:37:33 +11005568 nr = working = insync = failed = spare = 0;
5569 rcu_read_lock();
5570 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005572 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 failed++;
5574 else {
5575 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005576 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005577 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 else
5579 spare++;
5580 }
5581 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005582 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
5584 info.major_version = mddev->major_version;
5585 info.minor_version = mddev->minor_version;
5586 info.patch_version = MD_PATCHLEVEL_VERSION;
5587 info.ctime = mddev->ctime;
5588 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005589 info.size = mddev->dev_sectors / 2;
5590 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005591 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 info.nr_disks = nr;
5593 info.raid_disks = mddev->raid_disks;
5594 info.md_minor = mddev->md_minor;
5595 info.not_persistent= !mddev->persistent;
5596
5597 info.utime = mddev->utime;
5598 info.state = 0;
5599 if (mddev->in_sync)
5600 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005601 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005602 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005603 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 info.working_disks = working;
5605 info.failed_disks = failed;
5606 info.spare_disks = spare;
5607
5608 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005609 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 if (copy_to_user(arg, &info, sizeof(info)))
5612 return -EFAULT;
5613
5614 return 0;
5615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005618{
5619 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5620 char *ptr, *buf = NULL;
5621 int err = -ENOMEM;
5622
NeilBrown60559da2013-07-16 16:44:29 +10005623 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005624
NeilBrown32a76272005-06-21 17:17:14 -07005625 if (!file)
5626 goto out;
5627
5628 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005629 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005630 file->pathname[0] = '\0';
5631 goto copy_out;
5632 }
5633
5634 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5635 if (!buf)
5636 goto out;
5637
NeilBrown1ec885c2012-05-22 13:55:10 +10005638 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5639 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005640 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005641 goto out;
5642
5643 strcpy(file->pathname, ptr);
5644
5645copy_out:
5646 err = 0;
5647 if (copy_to_user(arg, file, sizeof(*file)))
5648 err = -EFAULT;
5649out:
5650 kfree(buf);
5651 kfree(file);
5652 return err;
5653}
5654
NeilBrownfd01b882011-10-11 16:47:53 +11005655static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
5657 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 if (copy_from_user(&info, arg, sizeof(info)))
5661 return -EFAULT;
5662
NeilBrown1ca69c42012-10-11 13:37:33 +11005663 rcu_read_lock();
5664 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (rdev) {
5666 info.major = MAJOR(rdev->bdev->bd_dev);
5667 info.minor = MINOR(rdev->bdev->bd_dev);
5668 info.raid_disk = rdev->raid_disk;
5669 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005670 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.state |= (1<<MD_DISK_ACTIVE);
5674 info.state |= (1<<MD_DISK_SYNC);
5675 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005676 if (test_bit(WriteMostly, &rdev->flags))
5677 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 } else {
5679 info.major = info.minor = 0;
5680 info.raid_disk = -1;
5681 info.state = (1<<MD_DISK_REMOVED);
5682 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005683 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
5685 if (copy_to_user(arg, &info, sizeof(info)))
5686 return -EFAULT;
5687
5688 return 0;
5689}
5690
NeilBrownfd01b882011-10-11 16:47:53 +11005691static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
5693 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005694 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 dev_t dev = MKDEV(info->major,info->minor);
5696
5697 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5698 return -EOVERFLOW;
5699
5700 if (!mddev->raid_disks) {
5701 int err;
5702 /* expecting a device which has a superblock */
5703 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5704 if (IS_ERR(rdev)) {
5705 printk(KERN_WARNING
5706 "md: md_import_device returned %ld\n",
5707 PTR_ERR(rdev));
5708 return PTR_ERR(rdev);
5709 }
5710 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005711 struct md_rdev *rdev0
5712 = list_entry(mddev->disks.next,
5713 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005714 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 .load_super(rdev, rdev0, mddev->minor_version);
5716 if (err < 0) {
5717 printk(KERN_WARNING
5718 "md: %s has different UUID to %s\n",
5719 bdevname(rdev->bdev,b),
5720 bdevname(rdev0->bdev,b2));
5721 export_rdev(rdev);
5722 return -EINVAL;
5723 }
5724 }
5725 err = bind_rdev_to_array(rdev, mddev);
5726 if (err)
5727 export_rdev(rdev);
5728 return err;
5729 }
5730
5731 /*
5732 * add_new_disk can be used once the array is assembled
5733 * to add "hot spares". They must already have a superblock
5734 * written
5735 */
5736 if (mddev->pers) {
5737 int err;
5738 if (!mddev->pers->hot_add_disk) {
5739 printk(KERN_WARNING
5740 "%s: personality does not support diskops!\n",
5741 mdname(mddev));
5742 return -EINVAL;
5743 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005744 if (mddev->persistent)
5745 rdev = md_import_device(dev, mddev->major_version,
5746 mddev->minor_version);
5747 else
5748 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 if (IS_ERR(rdev)) {
5750 printk(KERN_WARNING
5751 "md: md_import_device returned %ld\n",
5752 PTR_ERR(rdev));
5753 return PTR_ERR(rdev);
5754 }
NeilBrown1a855a02010-12-09 16:36:28 +11005755 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005756 if (!mddev->persistent) {
5757 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005758 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005759 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005760 set_bit(In_sync, &rdev->flags);
5761 } else
NeilBrown41158c72005-06-21 17:17:25 -07005762 rdev->raid_disk = -1;
5763 } else
5764 super_types[mddev->major_version].
5765 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005766 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005767 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005768 /* This was a hot-add request, but events doesn't
5769 * match, so reject it.
5770 */
5771 export_rdev(rdev);
5772 return -EINVAL;
5773 }
5774
NeilBrown1a855a02010-12-09 16:36:28 +11005775 if (test_bit(In_sync, &rdev->flags))
5776 rdev->saved_raid_disk = rdev->raid_disk;
5777 else
5778 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005779
NeilBrownb2d444d2005-11-08 21:39:31 -08005780 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005781 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5782 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005783 else
5784 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005785
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 rdev->raid_disk = -1;
5787 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005788 if (!err && !mddev->pers->hot_remove_disk) {
5789 /* If there is hot_add_disk but no hot_remove_disk
5790 * then added disks for geometry changes,
5791 * and should be added immediately.
5792 */
5793 super_types[mddev->major_version].
5794 validate_super(mddev, rdev);
5795 err = mddev->pers->hot_add_disk(mddev, rdev);
5796 if (err)
5797 unbind_rdev_from_array(rdev);
5798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 if (err)
5800 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005801 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005802 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005803
NeilBrown7ceb17e2013-04-24 11:42:42 +10005804 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005805 if (mddev->degraded)
5806 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005807 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005808 if (!err)
5809 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005810 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 return err;
5812 }
5813
5814 /* otherwise, add_new_disk is only allowed
5815 * for major_version==0 superblocks
5816 */
5817 if (mddev->major_version != 0) {
5818 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5819 mdname(mddev));
5820 return -EINVAL;
5821 }
5822
5823 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5824 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005825 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (IS_ERR(rdev)) {
5827 printk(KERN_WARNING
5828 "md: error, md_import_device() returned %ld\n",
5829 PTR_ERR(rdev));
5830 return PTR_ERR(rdev);
5831 }
5832 rdev->desc_nr = info->number;
5833 if (info->raid_disk < mddev->raid_disks)
5834 rdev->raid_disk = info->raid_disk;
5835 else
5836 rdev->raid_disk = -1;
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005839 if (info->state & (1<<MD_DISK_SYNC))
5840 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005842 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5843 set_bit(WriteMostly, &rdev->flags);
5844
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 if (!mddev->persistent) {
5846 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005847 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5848 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005849 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005850 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
NeilBrown2bf071b2006-01-06 00:20:55 -08005852 err = bind_rdev_to_array(rdev, mddev);
5853 if (err) {
5854 export_rdev(rdev);
5855 return err;
5856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 }
5858
5859 return 0;
5860}
5861
NeilBrownfd01b882011-10-11 16:47:53 +11005862static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
5864 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005865 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 rdev = find_rdev(mddev, dev);
5868 if (!rdev)
5869 return -ENXIO;
5870
NeilBrown3ea8929d2013-04-24 11:42:41 +10005871 clear_bit(Blocked, &rdev->flags);
5872 remove_and_add_spares(mddev, rdev);
5873
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 if (rdev->raid_disk >= 0)
5875 goto busy;
5876
5877 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005878 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005879 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
5881 return 0;
5882busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005883 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 bdevname(rdev->bdev,b), mdname(mddev));
5885 return -EBUSY;
5886}
5887
NeilBrownfd01b882011-10-11 16:47:53 +11005888static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889{
5890 char b[BDEVNAME_SIZE];
5891 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005892 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894 if (!mddev->pers)
5895 return -ENODEV;
5896
5897 if (mddev->major_version != 0) {
5898 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5899 " version-0 superblocks.\n",
5900 mdname(mddev));
5901 return -EINVAL;
5902 }
5903 if (!mddev->pers->hot_add_disk) {
5904 printk(KERN_WARNING
5905 "%s: personality does not support diskops!\n",
5906 mdname(mddev));
5907 return -EINVAL;
5908 }
5909
NeilBrownd710e132008-10-13 11:55:12 +11005910 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 if (IS_ERR(rdev)) {
5912 printk(KERN_WARNING
5913 "md: error, md_import_device() returned %ld\n",
5914 PTR_ERR(rdev));
5915 return -EINVAL;
5916 }
5917
5918 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005919 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005921 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
NeilBrown8190e752009-06-18 08:48:58 +10005923 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
NeilBrownb2d444d2005-11-08 21:39:31 -08005925 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 printk(KERN_WARNING
5927 "md: can not hot-add faulty %s disk to %s!\n",
5928 bdevname(rdev->bdev,b), mdname(mddev));
5929 err = -EINVAL;
5930 goto abort_export;
5931 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005932 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005934 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005935 err = bind_rdev_to_array(rdev, mddev);
5936 if (err)
5937 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 /*
5940 * The rest should better be atomic, we can have disk failures
5941 * noticed in interrupt contexts ...
5942 */
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 rdev->raid_disk = -1;
5945
NeilBrown850b2b42006-10-03 01:15:46 -07005946 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
5948 /*
5949 * Kick recovery, maybe this spare has to be added to the
5950 * array immediately.
5951 */
5952 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5953 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005954 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return 0;
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957abort_export:
5958 export_rdev(rdev);
5959 return err;
5960}
5961
NeilBrownfd01b882011-10-11 16:47:53 +11005962static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005963{
5964 int err;
5965
NeilBrown36fa3062005-09-09 16:23:45 -07005966 if (mddev->pers) {
5967 if (!mddev->pers->quiesce)
5968 return -EBUSY;
5969 if (mddev->recovery || mddev->sync_thread)
5970 return -EBUSY;
5971 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005972 }
5973
NeilBrown36fa3062005-09-09 16:23:45 -07005974
5975 if (fd >= 0) {
5976 if (mddev->bitmap)
5977 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005978 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005979
NeilBrownc3d97142009-12-14 12:49:52 +11005980 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005981 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5982 mdname(mddev));
5983 return -EBADF;
5984 }
5985
NeilBrownc3d97142009-12-14 12:49:52 +11005986 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005987 if (err) {
5988 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5989 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005990 fput(mddev->bitmap_info.file);
5991 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005992 return err;
5993 }
NeilBrownc3d97142009-12-14 12:49:52 +11005994 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005995 } else if (mddev->bitmap == NULL)
5996 return -ENOENT; /* cannot remove what isn't there */
5997 err = 0;
5998 if (mddev->pers) {
5999 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006000 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006001 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006002 if (!err)
6003 err = bitmap_load(mddev);
6004 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006005 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006006 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006007 fd = -1; /* make sure to put the file */
6008 }
NeilBrown36fa3062005-09-09 16:23:45 -07006009 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006010 }
6011 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006012 if (mddev->bitmap_info.file) {
6013 restore_bitmap_write_access(mddev->bitmap_info.file);
6014 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006015 }
NeilBrownc3d97142009-12-14 12:49:52 +11006016 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006017 }
6018
NeilBrown32a76272005-06-21 17:17:14 -07006019 return err;
6020}
6021
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022/*
6023 * set_array_info is used two different ways
6024 * The original usage is when creating a new array.
6025 * In this usage, raid_disks is > 0 and it together with
6026 * level, size, not_persistent,layout,chunksize determine the
6027 * shape of the array.
6028 * This will always create an array with a type-0.90.0 superblock.
6029 * The newer usage is when assembling an array.
6030 * In this case raid_disks will be 0, and the major_version field is
6031 * use to determine which style super-blocks are to be found on the devices.
6032 * The minor and patch _version numbers are also kept incase the
6033 * super_block handler wishes to interpret them.
6034 */
NeilBrownfd01b882011-10-11 16:47:53 +11006035static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
6037
6038 if (info->raid_disks == 0) {
6039 /* just setting version number for superblock loading */
6040 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006041 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 super_types[info->major_version].name == NULL) {
6043 /* maybe try to auto-load a module? */
6044 printk(KERN_INFO
6045 "md: superblock version %d not known\n",
6046 info->major_version);
6047 return -EINVAL;
6048 }
6049 mddev->major_version = info->major_version;
6050 mddev->minor_version = info->minor_version;
6051 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006052 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006053 /* ensure mddev_put doesn't delete this now that there
6054 * is some minimal configuration.
6055 */
6056 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 return 0;
6058 }
6059 mddev->major_version = MD_MAJOR_VERSION;
6060 mddev->minor_version = MD_MINOR_VERSION;
6061 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6062 mddev->ctime = get_seconds();
6063
6064 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006065 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006066 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 mddev->raid_disks = info->raid_disks;
6068 /* don't set md_minor, it is determined by which /dev/md* was
6069 * openned
6070 */
6071 if (info->state & (1<<MD_SB_CLEAN))
6072 mddev->recovery_cp = MaxSector;
6073 else
6074 mddev->recovery_cp = 0;
6075 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006076 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006079 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 mddev->max_disks = MD_SB_DISKS;
6082
NeilBrowne6910632008-02-06 01:39:51 -08006083 if (mddev->persistent)
6084 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006085 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
NeilBrownc3d97142009-12-14 12:49:52 +11006087 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006088 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006089 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006090
NeilBrownf6705572006-03-27 01:18:11 -08006091 mddev->reshape_position = MaxSector;
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 /*
6094 * Generate a 128 bit UUID
6095 */
6096 get_random_bytes(mddev->uuid, 16);
6097
NeilBrownf6705572006-03-27 01:18:11 -08006098 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006099 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006100 mddev->new_layout = mddev->layout;
6101 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006102 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return 0;
6105}
6106
NeilBrownfd01b882011-10-11 16:47:53 +11006107void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006108{
Dan Williamsb522adc2009-03-31 15:00:31 +11006109 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6110
6111 if (mddev->external_size)
6112 return;
6113
Dan Williams1f403622009-03-31 14:59:03 +11006114 mddev->array_sectors = array_sectors;
6115}
6116EXPORT_SYMBOL(md_set_array_sectors);
6117
NeilBrownfd01b882011-10-11 16:47:53 +11006118static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006119{
NeilBrown3cb03002011-10-11 16:45:26 +11006120 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006121 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006122 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006123
6124 if (mddev->pers->resize == NULL)
6125 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006126 /* The "num_sectors" is the number of sectors of each device that
6127 * is used. This can only make sense for arrays with redundancy.
6128 * linear and raid0 always use whatever space is available. We can only
6129 * consider changing this number if no resync or reconstruction is
6130 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006131 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006132 * of each device. If num_sectors is zero, we find the largest size
6133 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006134 */
6135 if (mddev->sync_thread)
6136 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006137
NeilBrowndafb20f2012-03-19 12:46:39 +11006138 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006139 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006140
Andre Nolld71f9f82008-07-11 22:02:22 +10006141 if (fit && (num_sectors == 0 || num_sectors > avail))
6142 num_sectors = avail;
6143 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006144 return -ENOSPC;
6145 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006146 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006147 if (!rv)
6148 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006149 return rv;
6150}
6151
NeilBrownfd01b882011-10-11 16:47:53 +11006152static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006153{
6154 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006155 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006156 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006157 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006158 return -EINVAL;
6159 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006160 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006161 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006162 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006163 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006164
6165 rdev_for_each(rdev, mddev) {
6166 if (mddev->raid_disks < raid_disks &&
6167 rdev->data_offset < rdev->new_data_offset)
6168 return -EINVAL;
6169 if (mddev->raid_disks > raid_disks &&
6170 rdev->data_offset > rdev->new_data_offset)
6171 return -EINVAL;
6172 }
6173
NeilBrown63c70c42006-03-27 01:18:13 -08006174 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006175 if (mddev->delta_disks < 0)
6176 mddev->reshape_backwards = 1;
6177 else if (mddev->delta_disks > 0)
6178 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006179
6180 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006181 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006182 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006183 mddev->reshape_backwards = 0;
6184 }
NeilBrownda943b992006-01-06 00:20:54 -08006185 return rv;
6186}
6187
6188
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189/*
6190 * update_array_info is used to change the configuration of an
6191 * on-line array.
6192 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6193 * fields in the info are checked against the array.
6194 * Any differences that cannot be handled will cause an error.
6195 * Normally, only one change can be managed at a time.
6196 */
NeilBrownfd01b882011-10-11 16:47:53 +11006197static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198{
6199 int rv = 0;
6200 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006201 int state = 0;
6202
6203 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006204 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006205 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
6207 if (mddev->major_version != info->major_version ||
6208 mddev->minor_version != info->minor_version ||
6209/* mddev->patch_version != info->patch_version || */
6210 mddev->ctime != info->ctime ||
6211 mddev->level != info->level ||
6212/* mddev->layout != info->layout || */
6213 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006214 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006215 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6216 ((state^info->state) & 0xfffffe00)
6217 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 return -EINVAL;
6219 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006220 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6221 cnt++;
6222 if (mddev->raid_disks != info->raid_disks)
6223 cnt++;
6224 if (mddev->layout != info->layout)
6225 cnt++;
6226 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6227 cnt++;
6228 if (cnt == 0)
6229 return 0;
6230 if (cnt > 1)
6231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
6233 if (mddev->layout != info->layout) {
6234 /* Change layout
6235 * we don't need to do anything at the md level, the
6236 * personality will take care of it all.
6237 */
NeilBrown50ac1682009-06-18 08:47:55 +10006238 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006240 else {
6241 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006242 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006243 if (rv)
6244 mddev->new_layout = mddev->layout;
6245 return rv;
6246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006248 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006249 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
NeilBrownda943b992006-01-06 00:20:54 -08006251 if (mddev->raid_disks != info->raid_disks)
6252 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
NeilBrown36fa3062005-09-09 16:23:45 -07006254 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6255 if (mddev->pers->quiesce == NULL)
6256 return -EINVAL;
6257 if (mddev->recovery || mddev->sync_thread)
6258 return -EBUSY;
6259 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6260 /* add the bitmap */
6261 if (mddev->bitmap)
6262 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006263 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006264 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006265 mddev->bitmap_info.offset =
6266 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006267 mddev->bitmap_info.space =
6268 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006269 mddev->pers->quiesce(mddev, 1);
6270 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006271 if (!rv)
6272 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006273 if (rv)
6274 bitmap_destroy(mddev);
6275 mddev->pers->quiesce(mddev, 0);
6276 } else {
6277 /* remove the bitmap */
6278 if (!mddev->bitmap)
6279 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006280 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006281 return -EINVAL;
6282 mddev->pers->quiesce(mddev, 1);
6283 bitmap_destroy(mddev);
6284 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006285 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006286 }
6287 }
NeilBrown850b2b42006-10-03 01:15:46 -07006288 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 return rv;
6290}
6291
NeilBrownfd01b882011-10-11 16:47:53 +11006292static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
NeilBrown3cb03002011-10-11 16:45:26 +11006294 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006295 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297 if (mddev->pers == NULL)
6298 return -ENODEV;
6299
NeilBrown1ca69c42012-10-11 13:37:33 +11006300 rcu_read_lock();
6301 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006303 err = -ENODEV;
6304 else {
6305 md_error(mddev, rdev);
6306 if (!test_bit(Faulty, &rdev->flags))
6307 err = -EBUSY;
6308 }
6309 rcu_read_unlock();
6310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311}
6312
Andre Noll2f9618c2008-04-25 18:57:58 +02006313/*
6314 * We have a problem here : there is no easy way to give a CHS
6315 * virtual geometry. We currently pretend that we have a 2 heads
6316 * 4 sectors (with a BIG number of cylinders...). This drives
6317 * dosfs just mad... ;-)
6318 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006319static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6320{
NeilBrownfd01b882011-10-11 16:47:53 +11006321 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006322
6323 geo->heads = 2;
6324 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006325 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006326 return 0;
6327}
6328
Al Viroa39907f2008-03-02 10:31:15 -05006329static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 unsigned int cmd, unsigned long arg)
6331{
6332 int err = 0;
6333 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006334 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006335 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
NeilBrown506c9e42011-12-23 10:17:26 +11006337 switch (cmd) {
6338 case RAID_VERSION:
6339 case GET_ARRAY_INFO:
6340 case GET_DISK_INFO:
6341 break;
6342 default:
6343 if (!capable(CAP_SYS_ADMIN))
6344 return -EACCES;
6345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
6347 /*
6348 * Commands dealing with the RAID driver but not any
6349 * particular array:
6350 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006351 switch (cmd) {
6352 case RAID_VERSION:
6353 err = get_version(argp);
6354 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
NeilBrownc02c0ae2012-12-11 13:39:21 +11006356 case PRINT_RAID_DEBUG:
6357 err = 0;
6358 md_print_devices();
6359 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
6361#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006362 case RAID_AUTORUN:
6363 err = 0;
6364 autostart_arrays(arg);
6365 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006367 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 }
6369
6370 /*
6371 * Commands creating/starting a new array:
6372 */
6373
Al Viroa39907f2008-03-02 10:31:15 -05006374 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
6376 if (!mddev) {
6377 BUG();
6378 goto abort;
6379 }
6380
NeilBrown1ca69c42012-10-11 13:37:33 +11006381 /* Some actions do not requires the mutex */
6382 switch (cmd) {
6383 case GET_ARRAY_INFO:
6384 if (!mddev->raid_disks && !mddev->external)
6385 err = -ENODEV;
6386 else
6387 err = get_array_info(mddev, argp);
6388 goto abort;
6389
6390 case GET_DISK_INFO:
6391 if (!mddev->raid_disks && !mddev->external)
6392 err = -ENODEV;
6393 else
6394 err = get_disk_info(mddev, argp);
6395 goto abort;
6396
6397 case SET_DISK_FAULTY:
6398 err = set_disk_faulty(mddev, new_decode_dev(arg));
6399 goto abort;
6400 }
6401
NeilBrowna7a3f082012-12-11 13:35:54 +11006402 if (cmd == ADD_NEW_DISK)
6403 /* need to ensure md_delayed_delete() has completed */
6404 flush_workqueue(md_misc_wq);
6405
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006406 if (cmd == HOT_REMOVE_DISK)
6407 /* need to ensure recovery thread has run */
6408 wait_event_interruptible_timeout(mddev->sb_wait,
6409 !test_bit(MD_RECOVERY_NEEDED,
6410 &mddev->flags),
6411 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006412 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6413 /* Need to flush page cache, and ensure no-one else opens
6414 * and writes
6415 */
6416 mutex_lock(&mddev->open_mutex);
6417 if (atomic_read(&mddev->openers) > 1) {
6418 mutex_unlock(&mddev->open_mutex);
6419 err = -EBUSY;
6420 goto abort;
6421 }
6422 set_bit(MD_STILL_CLOSED, &mddev->flags);
6423 mutex_unlock(&mddev->open_mutex);
6424 sync_blockdev(bdev);
6425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 err = mddev_lock(mddev);
6427 if (err) {
6428 printk(KERN_INFO
6429 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6430 err, cmd);
6431 goto abort;
6432 }
6433
NeilBrownc02c0ae2012-12-11 13:39:21 +11006434 if (cmd == SET_ARRAY_INFO) {
6435 mdu_array_info_t info;
6436 if (!arg)
6437 memset(&info, 0, sizeof(info));
6438 else if (copy_from_user(&info, argp, sizeof(info))) {
6439 err = -EFAULT;
6440 goto abort_unlock;
6441 }
6442 if (mddev->pers) {
6443 err = update_array_info(mddev, &info);
6444 if (err) {
6445 printk(KERN_WARNING "md: couldn't update"
6446 " array info. %d\n", err);
6447 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 }
6449 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006450 }
6451 if (!list_empty(&mddev->disks)) {
6452 printk(KERN_WARNING
6453 "md: array %s already has disks!\n",
6454 mdname(mddev));
6455 err = -EBUSY;
6456 goto abort_unlock;
6457 }
6458 if (mddev->raid_disks) {
6459 printk(KERN_WARNING
6460 "md: array %s already initialised!\n",
6461 mdname(mddev));
6462 err = -EBUSY;
6463 goto abort_unlock;
6464 }
6465 err = set_array_info(mddev, &info);
6466 if (err) {
6467 printk(KERN_WARNING "md: couldn't set"
6468 " array info. %d\n", err);
6469 goto abort_unlock;
6470 }
6471 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 }
6473
6474 /*
6475 * Commands querying/configuring an existing array:
6476 */
NeilBrown32a76272005-06-21 17:17:14 -07006477 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006478 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006479 if ((!mddev->raid_disks && !mddev->external)
6480 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6481 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6482 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 err = -ENODEV;
6484 goto abort_unlock;
6485 }
6486
6487 /*
6488 * Commands even a read-only array can execute:
6489 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006490 switch (cmd) {
6491 case GET_BITMAP_FILE:
6492 err = get_bitmap_file(mddev, argp);
6493 goto done_unlock;
6494
6495 case RESTART_ARRAY_RW:
6496 err = restart_array(mddev);
6497 goto done_unlock;
6498
6499 case STOP_ARRAY:
6500 err = do_md_stop(mddev, 0, bdev);
6501 goto done_unlock;
6502
6503 case STOP_ARRAY_RO:
6504 err = md_set_readonly(mddev, bdev);
6505 goto done_unlock;
6506
NeilBrown3ea8929d2013-04-24 11:42:41 +10006507 case HOT_REMOVE_DISK:
6508 err = hot_remove_disk(mddev, new_decode_dev(arg));
6509 goto done_unlock;
6510
NeilBrown7ceb17e2013-04-24 11:42:42 +10006511 case ADD_NEW_DISK:
6512 /* We can support ADD_NEW_DISK on read-only arrays
6513 * on if we are re-adding a preexisting device.
6514 * So require mddev->pers and MD_DISK_SYNC.
6515 */
6516 if (mddev->pers) {
6517 mdu_disk_info_t info;
6518 if (copy_from_user(&info, argp, sizeof(info)))
6519 err = -EFAULT;
6520 else if (!(info.state & (1<<MD_DISK_SYNC)))
6521 /* Need to clear read-only for this */
6522 break;
6523 else
6524 err = add_new_disk(mddev, &info);
6525 goto done_unlock;
6526 }
6527 break;
6528
NeilBrownc02c0ae2012-12-11 13:39:21 +11006529 case BLKROSET:
6530 if (get_user(ro, (int __user *)(arg))) {
6531 err = -EFAULT;
6532 goto done_unlock;
6533 }
6534 err = -EINVAL;
6535
6536 /* if the bdev is going readonly the value of mddev->ro
6537 * does not matter, no writes are coming
6538 */
6539 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006540 goto done_unlock;
6541
NeilBrownc02c0ae2012-12-11 13:39:21 +11006542 /* are we are already prepared for writes? */
6543 if (mddev->ro != 1)
6544 goto done_unlock;
6545
6546 /* transitioning to readauto need only happen for
6547 * arrays that call md_write_start
6548 */
6549 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006551 if (err == 0) {
6552 mddev->ro = 2;
6553 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006554 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006555 }
6556 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
6558
6559 /*
6560 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006561 * superblock, so we do not allow them on read-only arrays.
6562 * However non-MD ioctls (e.g. get-size) will still come through
6563 * here and hit the 'default' below, so only disallow
6564 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006566 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006567 if (mddev->ro == 2) {
6568 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006569 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006571 /* mddev_unlock will wake thread */
6572 /* If a device failed while we were read-only, we
6573 * need to make sure the metadata is updated now.
6574 */
6575 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6576 mddev_unlock(mddev);
6577 wait_event(mddev->sb_wait,
6578 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6579 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006580 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006581 }
NeilBrownf91de922005-11-08 21:39:36 -08006582 } else {
6583 err = -EROFS;
6584 goto abort_unlock;
6585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 }
6587
NeilBrownc02c0ae2012-12-11 13:39:21 +11006588 switch (cmd) {
6589 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006591 mdu_disk_info_t info;
6592 if (copy_from_user(&info, argp, sizeof(info)))
6593 err = -EFAULT;
6594 else
6595 err = add_new_disk(mddev, &info);
6596 goto done_unlock;
6597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
NeilBrownc02c0ae2012-12-11 13:39:21 +11006599 case HOT_ADD_DISK:
6600 err = hot_add_disk(mddev, new_decode_dev(arg));
6601 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
NeilBrownc02c0ae2012-12-11 13:39:21 +11006603 case RUN_ARRAY:
6604 err = do_md_run(mddev);
6605 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606
NeilBrownc02c0ae2012-12-11 13:39:21 +11006607 case SET_BITMAP_FILE:
6608 err = set_bitmap_file(mddev, (int)arg);
6609 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006610
NeilBrownc02c0ae2012-12-11 13:39:21 +11006611 default:
6612 err = -EINVAL;
6613 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 }
6615
6616done_unlock:
6617abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006618 if (mddev->hold_active == UNTIL_IOCTL &&
6619 err != -EINVAL)
6620 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 mddev_unlock(mddev);
6622
6623 return err;
6624done:
6625 if (err)
6626 MD_BUG();
6627abort:
6628 return err;
6629}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006630#ifdef CONFIG_COMPAT
6631static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6632 unsigned int cmd, unsigned long arg)
6633{
6634 switch (cmd) {
6635 case HOT_REMOVE_DISK:
6636 case HOT_ADD_DISK:
6637 case SET_DISK_FAULTY:
6638 case SET_BITMAP_FILE:
6639 /* These take in integer arg, do not convert */
6640 break;
6641 default:
6642 arg = (unsigned long)compat_ptr(arg);
6643 break;
6644 }
6645
6646 return md_ioctl(bdev, mode, cmd, arg);
6647}
6648#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649
Al Viroa39907f2008-03-02 10:31:15 -05006650static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651{
6652 /*
6653 * Succeed if we can lock the mddev, which confirms that
6654 * it isn't being stopped right now.
6655 */
NeilBrownfd01b882011-10-11 16:47:53 +11006656 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 int err;
6658
Yuanhan Liu0c098222012-05-22 13:55:32 +10006659 if (!mddev)
6660 return -ENODEV;
6661
NeilBrownd3374822009-01-09 08:31:10 +11006662 if (mddev->gendisk != bdev->bd_disk) {
6663 /* we are racing with mddev_put which is discarding this
6664 * bd_disk.
6665 */
6666 mddev_put(mddev);
6667 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006668 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006669 /* Then retry the open from the top */
6670 return -ERESTARTSYS;
6671 }
6672 BUG_ON(mddev != bdev->bd_disk->private_data);
6673
NeilBrownc8c00a62009-08-10 12:50:52 +10006674 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 goto out;
6676
6677 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006678 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006679 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006680 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006682 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 out:
6684 return err;
6685}
6686
Al Virodb2a1442013-05-05 21:52:57 -04006687static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688{
NeilBrownfd01b882011-10-11 16:47:53 +11006689 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006691 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006692 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006695
6696static int md_media_changed(struct gendisk *disk)
6697{
NeilBrownfd01b882011-10-11 16:47:53 +11006698 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006699
6700 return mddev->changed;
6701}
6702
6703static int md_revalidate(struct gendisk *disk)
6704{
NeilBrownfd01b882011-10-11 16:47:53 +11006705 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006706
6707 mddev->changed = 0;
6708 return 0;
6709}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006710static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711{
6712 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006713 .open = md_open,
6714 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006715 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006716#ifdef CONFIG_COMPAT
6717 .compat_ioctl = md_compat_ioctl,
6718#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006719 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006720 .media_changed = md_media_changed,
6721 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722};
6723
Adrian Bunk75c96f82005-05-05 16:16:09 -07006724static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725{
NeilBrown2b8bf342011-10-11 16:48:23 +11006726 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 /*
6729 * md_thread is a 'system-thread', it's priority should be very
6730 * high. We avoid resource deadlocks individually in each
6731 * raid personality. (RAID5 does preallocation) We also use RR and
6732 * the very same RT priority as kswapd, thus we will never get
6733 * into a priority inversion deadlock.
6734 *
6735 * we definitely have to have equal or higher priority than
6736 * bdflush, otherwise bdflush will deadlock if there are too
6737 * many dirty RAID5 blocks.
6738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
NeilBrown6985c432005-10-19 21:23:47 -07006740 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006741 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
NeilBrown93588e22005-11-15 00:09:12 -08006743 /* We need to wait INTERRUPTIBLE so that
6744 * we don't add to the load-average.
6745 * That means we need to be sure no signals are
6746 * pending
6747 */
6748 if (signal_pending(current))
6749 flush_signals(current);
6750
6751 wait_event_interruptible_timeout
6752 (thread->wqueue,
6753 test_bit(THREAD_WAKEUP, &thread->flags)
6754 || kthread_should_stop(),
6755 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
NeilBrown6c987912011-01-14 09:13:53 +11006757 clear_bit(THREAD_WAKEUP, &thread->flags);
6758 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006759 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 return 0;
6763}
6764
NeilBrown2b8bf342011-10-11 16:48:23 +11006765void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766{
6767 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006768 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 set_bit(THREAD_WAKEUP, &thread->flags);
6770 wake_up(&thread->wqueue);
6771 }
6772}
6773
Shaohua Li4ed87312012-10-11 13:34:00 +11006774struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6775 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776{
NeilBrown2b8bf342011-10-11 16:48:23 +11006777 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
NeilBrown2b8bf342011-10-11 16:48:23 +11006779 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 if (!thread)
6781 return NULL;
6782
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 init_waitqueue_head(&thread->wqueue);
6784
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 thread->run = run;
6786 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006787 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006788 thread->tsk = kthread_run(md_thread, thread,
6789 "%s_%s",
6790 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006791 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006792 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 kfree(thread);
6794 return NULL;
6795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 return thread;
6797}
6798
NeilBrown2b8bf342011-10-11 16:48:23 +11006799void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800{
NeilBrown2b8bf342011-10-11 16:48:23 +11006801 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006802 if (!thread)
6803 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006804 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006805 /* Locking ensures that mddev_unlock does not wake_up a
6806 * non-existent thread
6807 */
6808 spin_lock(&pers_lock);
6809 *threadp = NULL;
6810 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006811
6812 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 kfree(thread);
6814}
6815
NeilBrownfd01b882011-10-11 16:47:53 +11006816void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817{
6818 if (!mddev) {
6819 MD_BUG();
6820 return;
6821 }
6822
NeilBrownb2d444d2005-11-08 21:39:31 -08006823 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006825
NeilBrownde393cd2011-07-28 11:31:48 +10006826 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 return;
6828 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006829 if (mddev->degraded)
6830 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006831 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6833 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6834 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006835 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006836 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006837 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838}
6839
6840/* seq_file implementation /proc/mdstat */
6841
6842static void status_unused(struct seq_file *seq)
6843{
6844 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006845 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
6847 seq_printf(seq, "unused devices: ");
6848
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006849 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 char b[BDEVNAME_SIZE];
6851 i++;
6852 seq_printf(seq, "%s ",
6853 bdevname(rdev->bdev,b));
6854 }
6855 if (!i)
6856 seq_printf(seq, "<none>");
6857
6858 seq_printf(seq, "\n");
6859}
6860
6861
NeilBrownfd01b882011-10-11 16:47:53 +11006862static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863{
NeilBrowndd71cf62009-05-07 12:49:35 +10006864 sector_t max_sectors, resync, res;
6865 unsigned long dt, db;
6866 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006867 int scale;
6868 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
NeilBrown72f36d52012-10-11 14:25:57 +11006870 if (mddev->curr_resync <= 3)
6871 resync = 0;
6872 else
6873 resync = mddev->curr_resync
6874 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
NeilBrownc804cde2012-05-21 09:28:33 +10006876 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6877 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006878 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006880 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882 /*
6883 * Should not happen.
6884 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006885 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 MD_BUG();
6887 return;
6888 }
NeilBrown4588b422006-03-27 01:18:04 -08006889 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006890 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006891 * u32, as those are the requirements for sector_div.
6892 * Thus 'scale' must be at least 10
6893 */
6894 scale = 10;
6895 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006896 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006897 scale++;
6898 }
6899 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006900 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006901
6902 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 {
NeilBrown4588b422006-03-27 01:18:04 -08006904 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 seq_printf(seq, "[");
6906 for (i = 0; i < x; i++)
6907 seq_printf(seq, "=");
6908 seq_printf(seq, ">");
6909 for (i = 0; i < y; i++)
6910 seq_printf(seq, ".");
6911 seq_printf(seq, "] ");
6912 }
NeilBrown4588b422006-03-27 01:18:04 -08006913 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006914 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6915 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006916 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6917 "check" :
6918 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6919 "resync" : "recovery"))),
6920 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006921 (unsigned long long) resync/2,
6922 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
6924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 * dt: time from mark until now
6926 * db: blocks written from mark until now
6927 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006928 *
6929 * rt is a sector_t, so could be 32bit or 64bit.
6930 * So we divide before multiply in case it is 32bit and close
6931 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006932 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006933 * near the end of resync when the number of remaining sectors
6934 * is close to 'db'.
6935 * We then divide rt by 32 after multiplying by db to compensate.
6936 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 */
6938 dt = ((jiffies - mddev->resync_mark) / HZ);
6939 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006940 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6941 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
NeilBrowndd71cf62009-05-07 12:49:35 +10006943 rt = max_sectors - resync; /* number of remaining sectors */
6944 sector_div(rt, db/32+1);
6945 rt *= dt;
6946 rt >>= 5;
6947
6948 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6949 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
NeilBrownff4e8d92006-07-10 04:44:16 -07006951 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952}
6953
6954static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6955{
6956 struct list_head *tmp;
6957 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006958 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
6960 if (l >= 0x10000)
6961 return NULL;
6962 if (!l--)
6963 /* header */
6964 return (void*)1;
6965
6966 spin_lock(&all_mddevs_lock);
6967 list_for_each(tmp,&all_mddevs)
6968 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006969 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 mddev_get(mddev);
6971 spin_unlock(&all_mddevs_lock);
6972 return mddev;
6973 }
6974 spin_unlock(&all_mddevs_lock);
6975 if (!l--)
6976 return (void*)2;/* tail */
6977 return NULL;
6978}
6979
6980static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6981{
6982 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006983 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
6985 ++*pos;
6986 if (v == (void*)2)
6987 return NULL;
6988
6989 spin_lock(&all_mddevs_lock);
6990 if (v == (void*)1)
6991 tmp = all_mddevs.next;
6992 else
6993 tmp = mddev->all_mddevs.next;
6994 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006995 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 else {
6997 next_mddev = (void*)2;
6998 *pos = 0x10000;
6999 }
7000 spin_unlock(&all_mddevs_lock);
7001
7002 if (v != (void*)1)
7003 mddev_put(mddev);
7004 return next_mddev;
7005
7006}
7007
7008static void md_seq_stop(struct seq_file *seq, void *v)
7009{
NeilBrownfd01b882011-10-11 16:47:53 +11007010 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
7012 if (mddev && v != (void*)1 && v != (void*)2)
7013 mddev_put(mddev);
7014}
7015
7016static int md_seq_show(struct seq_file *seq, void *v)
7017{
NeilBrownfd01b882011-10-11 16:47:53 +11007018 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007019 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007020 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
7022 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007023 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 seq_printf(seq, "Personalities : ");
7025 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007026 list_for_each_entry(pers, &pers_list, list)
7027 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
7029 spin_unlock(&pers_lock);
7030 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007031 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 return 0;
7033 }
7034 if (v == (void*)2) {
7035 status_unused(seq);
7036 return 0;
7037 }
7038
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007039 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007041
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7043 seq_printf(seq, "%s : %sactive", mdname(mddev),
7044 mddev->pers ? "" : "in");
7045 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007046 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007048 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007049 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 seq_printf(seq, " %s", mddev->pers->name);
7051 }
7052
Andre Nolldd8ac332009-03-31 14:33:13 +11007053 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007054 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 char b[BDEVNAME_SIZE];
7056 seq_printf(seq, " %s[%d]",
7057 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007058 if (test_bit(WriteMostly, &rdev->flags))
7059 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007060 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 seq_printf(seq, "(F)");
7062 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007063 }
7064 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007065 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007066 if (test_bit(Replacement, &rdev->flags))
7067 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007068 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 }
7070
7071 if (!list_empty(&mddev->disks)) {
7072 if (mddev->pers)
7073 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007074 (unsigned long long)
7075 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 else
7077 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007078 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007080 if (mddev->persistent) {
7081 if (mddev->major_version != 0 ||
7082 mddev->minor_version != 90) {
7083 seq_printf(seq," super %d.%d",
7084 mddev->major_version,
7085 mddev->minor_version);
7086 }
NeilBrowne6910632008-02-06 01:39:51 -08007087 } else if (mddev->external)
7088 seq_printf(seq, " super external:%s",
7089 mddev->metadata_type);
7090 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007091 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
7093 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007094 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007096 if (mddev->pers->sync_request) {
7097 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007098 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007099 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007100 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007101 seq_printf(seq, "\tresync=DELAYED\n ");
7102 else if (mddev->recovery_cp < MaxSector)
7103 seq_printf(seq, "\tresync=PENDING\n ");
7104 }
NeilBrown32a76272005-06-21 17:17:14 -07007105 } else
7106 seq_printf(seq, "\n ");
7107
NeilBrown57148962012-03-19 12:46:40 +11007108 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109
7110 seq_printf(seq, "\n");
7111 }
7112 mddev_unlock(mddev);
7113
7114 return 0;
7115}
7116
Jan Engelhardt110518b2009-05-07 12:49:37 +10007117static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 .start = md_seq_start,
7119 .next = md_seq_next,
7120 .stop = md_seq_stop,
7121 .show = md_seq_show,
7122};
7123
7124static int md_seq_open(struct inode *inode, struct file *file)
7125{
Kay Sieversf1514632011-07-12 20:48:39 +02007126 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 int error;
7128
7129 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007130 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007131 return error;
7132
7133 seq = file->private_data;
7134 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 return error;
7136}
7137
NeilBrownd7603b72006-01-06 00:20:30 -08007138static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7139{
Kay Sieversf1514632011-07-12 20:48:39 +02007140 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007141 int mask;
7142
7143 poll_wait(filp, &md_event_waiters, wait);
7144
7145 /* always allow read */
7146 mask = POLLIN | POLLRDNORM;
7147
Kay Sieversf1514632011-07-12 20:48:39 +02007148 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007149 mask |= POLLERR | POLLPRI;
7150 return mask;
7151}
7152
Arjan van de Venfa027c22007-02-12 00:55:33 -08007153static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007154 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 .open = md_seq_open,
7156 .read = seq_read,
7157 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007158 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007159 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160};
7161
NeilBrown84fc4b52011-10-11 16:49:58 +11007162int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007165 list_add_tail(&p->list, &pers_list);
7166 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 spin_unlock(&pers_lock);
7168 return 0;
7169}
7170
NeilBrown84fc4b52011-10-11 16:49:58 +11007171int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
NeilBrown2604b702006-01-06 00:20:36 -08007173 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007175 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 spin_unlock(&pers_lock);
7177 return 0;
7178}
7179
NeilBrownfd01b882011-10-11 16:47:53 +11007180static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181{
NeilBrown3cb03002011-10-11 16:45:26 +11007182 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007184 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185
7186 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007187 rcu_read_lock();
7188 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007190 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7191 (int)part_stat_read(&disk->part0, sectors[1]) -
7192 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007193 /* sync IO will cause sync_io to increase before the disk_stats
7194 * as sync_io is counted when a request starts, and
7195 * disk_stats is counted when it completes.
7196 * So resync activity will cause curr_events to be smaller than
7197 * when there was no such activity.
7198 * non-sync IO will cause disk_stat to increase without
7199 * increasing sync_io so curr_events will (eventually)
7200 * be larger than it was before. Once it becomes
7201 * substantially larger, the test below will cause
7202 * the array to appear non-idle, and resync will slow
7203 * down.
7204 * If there is a lot of outstanding resync activity when
7205 * we set last_event to curr_events, then all that activity
7206 * completing might cause the array to appear non-idle
7207 * and resync will be slowed down even though there might
7208 * not have been non-resync activity. This will only
7209 * happen once though. 'last_events' will soon reflect
7210 * the state where there is little or no outstanding
7211 * resync requests, and further resync activity will
7212 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007215 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 rdev->last_events = curr_events;
7217 idle = 0;
7218 }
7219 }
NeilBrown4b809912008-07-21 17:05:25 +10007220 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return idle;
7222}
7223
NeilBrownfd01b882011-10-11 16:47:53 +11007224void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225{
7226 /* another "blocks" (512byte) blocks have been synced */
7227 atomic_sub(blocks, &mddev->recovery_active);
7228 wake_up(&mddev->recovery_wait);
7229 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007230 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007231 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 md_wakeup_thread(mddev->thread);
7233 // stop recovery, signal do_sync ....
7234 }
7235}
7236
7237
NeilBrown06d91a52005-06-21 17:17:12 -07007238/* md_write_start(mddev, bi)
7239 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007240 * in superblock) before writing, schedule a superblock update
7241 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007242 */
NeilBrownfd01b882011-10-11 16:47:53 +11007243void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244{
Neil Brown0fd62b82008-06-28 08:31:36 +10007245 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007246 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007247 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007248
NeilBrownf91de922005-11-08 21:39:36 -08007249 BUG_ON(mddev->ro == 1);
7250 if (mddev->ro == 2) {
7251 /* need to switch to read/write */
7252 mddev->ro = 0;
7253 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7254 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007255 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007256 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007257 }
NeilBrown06d91a52005-06-21 17:17:12 -07007258 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007259 if (mddev->safemode == 1)
7260 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007261 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007262 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007263 if (mddev->in_sync) {
7264 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007265 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007266 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007267 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007268 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007269 }
NeilBrowna9701a32005-11-08 21:39:34 -08007270 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007271 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007272 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007273 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007274 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007275 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276}
7277
NeilBrownfd01b882011-10-11 16:47:53 +11007278void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279{
7280 if (atomic_dec_and_test(&mddev->writes_pending)) {
7281 if (mddev->safemode == 2)
7282 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007283 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7285 }
7286}
7287
NeilBrown2a2275d2007-01-26 00:57:11 -08007288/* md_allow_write(mddev)
7289 * Calling this ensures that the array is marked 'active' so that writes
7290 * may proceed without blocking. It is important to call this before
7291 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7292 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007293 *
7294 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7295 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007296 */
NeilBrownfd01b882011-10-11 16:47:53 +11007297int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007298{
7299 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007300 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007301 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007302 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007303 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007304 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007305
7306 spin_lock_irq(&mddev->write_lock);
7307 if (mddev->in_sync) {
7308 mddev->in_sync = 0;
7309 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007310 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007311 if (mddev->safemode_delay &&
7312 mddev->safemode == 0)
7313 mddev->safemode = 1;
7314 spin_unlock_irq(&mddev->write_lock);
7315 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007316 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007317 } else
7318 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007319
NeilBrown070dc6d2010-08-30 17:33:34 +10007320 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007321 return -EAGAIN;
7322 else
7323 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007324}
7325EXPORT_SYMBOL_GPL(md_allow_write);
7326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327#define SYNC_MARKS 10
7328#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007329#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007330void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331{
Shaohua Li4ed87312012-10-11 13:34:00 +11007332 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007333 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 unsigned int currspeed = 0,
7335 window;
NeilBrown57afd892005-06-21 17:17:13 -07007336 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007338 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 sector_t mark_cnt[SYNC_MARKS];
7340 int last_mark,m;
7341 struct list_head *tmp;
7342 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007343 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007344 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007345 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007346 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347
7348 /* just incase thread restarts... */
7349 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7350 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007351 if (mddev->ro) /* never try to sync a read-only array */
7352 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353
NeilBrown61df9d92006-10-03 01:15:57 -07007354 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007355 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007356 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007357 action = "check";
7358 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007359 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007360 action = "repair";
7361 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007362 desc = "resync";
7363 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7364 desc = "reshape";
7365 else
7366 desc = "recovery";
7367
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007368 mddev->last_sync_action = action ?: desc;
7369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 /* we overload curr_resync somewhat here.
7371 * 0 == not engaged in resync at all
7372 * 2 == checking that there is no conflict with another sync
7373 * 1 == like 2, but have yielded to allow conflicting resync to
7374 * commense
7375 * other == active in resync - this many blocks
7376 *
7377 * Before starting a resync we must have set curr_resync to
7378 * 2, and then checked that every "conflicting" array has curr_resync
7379 * less than ours. When we find one that is the same or higher
7380 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7381 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7382 * This will mean we have to start checking from the beginning again.
7383 *
7384 */
7385
7386 do {
7387 mddev->curr_resync = 2;
7388
7389 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007390 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007392 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 if (mddev2 == mddev)
7394 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007395 if (!mddev->parallel_resync
7396 && mddev2->curr_resync
7397 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 DEFINE_WAIT(wq);
7399 if (mddev < mddev2 && mddev->curr_resync == 2) {
7400 /* arbitrarily yield */
7401 mddev->curr_resync = 1;
7402 wake_up(&resync_wait);
7403 }
7404 if (mddev > mddev2 && mddev->curr_resync == 1)
7405 /* no need to wait here, we can wait the next
7406 * time 'round when curr_resync == 2
7407 */
7408 continue;
NeilBrown97441972008-09-19 11:49:54 +10007409 /* We need to wait 'interruptible' so as not to
7410 * contribute to the load average, and not to
7411 * be caught by 'softlockup'
7412 */
7413 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007414 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007415 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007416 printk(KERN_INFO "md: delaying %s of %s"
7417 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007419 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007421 if (signal_pending(current))
7422 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 schedule();
7424 finish_wait(&resync_wait, &wq);
7425 goto try_again;
7426 }
7427 finish_wait(&resync_wait, &wq);
7428 }
7429 }
7430 } while (mddev->curr_resync < 2);
7431
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007432 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007433 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007435 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 */
7437 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007438 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007439 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007440 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7441 j = mddev->resync_min;
7442 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007443 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007444
NeilBrownccfcc3c2006-03-27 01:18:09 -08007445 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007446 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007447 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007449 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007450 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007451 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007452 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007453 if (rdev->raid_disk >= 0 &&
7454 !test_bit(Faulty, &rdev->flags) &&
7455 !test_bit(In_sync, &rdev->flags) &&
7456 rdev->recovery_offset < j)
7457 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007458 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
NeilBrown61df9d92006-10-03 01:15:57 -07007461 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7462 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7463 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007464 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007465 "(but not more than %d KB/sec) for %s.\n",
7466 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
NeilBrowneea1bf32009-03-31 14:27:02 +11007468 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007469
NeilBrown57afd892005-06-21 17:17:13 -07007470 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 for (m = 0; m < SYNC_MARKS; m++) {
7472 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007473 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 }
7475 last_mark = 0;
7476 mddev->resync_mark = mark[last_mark];
7477 mddev->resync_mark_cnt = mark_cnt[last_mark];
7478
7479 /*
7480 * Tune reconstruction:
7481 */
7482 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007483 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7484 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
7486 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 last_check = 0;
7488
7489 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007490 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007491 "md: resuming %s of %s from checkpoint.\n",
7492 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007494 } else
7495 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007496 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007497 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7498 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007499 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
majianpeng7c2c57c2012-07-03 12:12:26 +10007501 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007503 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504
NeilBrown57afd892005-06-21 17:17:13 -07007505 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007506
NeilBrown7a91ee12009-05-26 12:57:21 +10007507 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7508 ((mddev->curr_resync > mddev->curr_resync_completed &&
7509 (mddev->curr_resync - mddev->curr_resync_completed)
7510 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007511 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007512 (j - mddev->curr_resync_completed)*2
7513 >= mddev->resync_max - mddev->curr_resync_completed
7514 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007515 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007516 wait_event(mddev->recovery_wait,
7517 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007518 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007519 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7520 j > mddev->recovery_cp)
7521 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007522 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007523 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007524 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007525 }
NeilBrownacb180b2009-04-14 16:28:34 +10007526
NeilBrownc91abf52013-11-19 12:02:01 +11007527 while (j >= mddev->resync_max &&
7528 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007529 /* As this condition is controlled by user-space,
7530 * we can block indefinitely, so use '_interruptible'
7531 * to avoid triggering warnings.
7532 */
7533 flush_signals(current); /* just in case */
7534 wait_event_interruptible(mddev->recovery_wait,
7535 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007536 || test_bit(MD_RECOVERY_INTR,
7537 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007538 }
NeilBrownacb180b2009-04-14 16:28:34 +10007539
NeilBrownc91abf52013-11-19 12:02:01 +11007540 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7541 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007542
NeilBrown57afd892005-06-21 17:17:13 -07007543 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007544 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007545 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007546 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 }
NeilBrown57afd892005-06-21 17:17:13 -07007549
7550 if (!skipped) { /* actual IO requested */
7551 io_sectors += sectors;
7552 atomic_add(sectors, &mddev->recovery_active);
7553 }
7554
NeilBrowne875ece2011-07-28 11:39:24 +10007555 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7556 break;
7557
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007559 if (j > 2)
7560 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007561 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007562 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007563 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007564 * visible in /proc/mdstat
7565 */
7566 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007567
7568 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 continue;
7570
NeilBrown57afd892005-06-21 17:17:13 -07007571 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 repeat:
7573 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7574 /* step marks */
7575 int next = (last_mark+1) % SYNC_MARKS;
7576
7577 mddev->resync_mark = mark[next];
7578 mddev->resync_mark_cnt = mark_cnt[next];
7579 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007580 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 last_mark = next;
7582 }
7583
NeilBrownc91abf52013-11-19 12:02:01 +11007584 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586
7587 /*
7588 * this loop exits only if either when we are slower than
7589 * the 'hard' speed limit, or the system was IO-idle for
7590 * a jiffy.
7591 * the system might be non-idle CPU-wise, but we only care
7592 * about not overloading the IO subsystem. (things like an
7593 * e2fsck being done on the RAID array should execute fast)
7594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 cond_resched();
7596
NeilBrown57afd892005-06-21 17:17:13 -07007597 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7598 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599
NeilBrown88202a02006-01-06 00:21:36 -08007600 if (currspeed > speed_min(mddev)) {
7601 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007602 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007603 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 goto repeat;
7605 }
7606 }
7607 }
NeilBrownc91abf52013-11-19 12:02:01 +11007608 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7609 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7610 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 /*
7612 * this also signals 'finished resyncing' to md_stop
7613 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007614 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7616
7617 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007618 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619
NeilBrowndfc70642008-05-23 13:04:39 -07007620 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007621 mddev->curr_resync > 2) {
7622 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7623 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7624 if (mddev->curr_resync >= mddev->recovery_cp) {
7625 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007626 "md: checkpointing %s of %s.\n",
7627 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007628 if (test_bit(MD_RECOVERY_ERROR,
7629 &mddev->recovery))
7630 mddev->recovery_cp =
7631 mddev->curr_resync_completed;
7632 else
7633 mddev->recovery_cp =
7634 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007635 }
7636 } else
7637 mddev->recovery_cp = MaxSector;
7638 } else {
7639 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7640 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007641 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007642 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007643 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007644 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007645 !test_bit(Faulty, &rdev->flags) &&
7646 !test_bit(In_sync, &rdev->flags) &&
7647 rdev->recovery_offset < mddev->curr_resync)
7648 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007649 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007652 skip:
NeilBrown17571282006-12-10 02:20:52 -08007653 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654
NeilBrownc07b70a2009-12-14 12:49:48 +11007655 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7656 /* We completed so min/max setting can be forgotten if used. */
7657 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7658 mddev->resync_min = 0;
7659 mddev->resync_max = MaxSector;
7660 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7661 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662 mddev->curr_resync = 0;
7663 wake_up(&resync_wait);
7664 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7665 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667}
NeilBrown29269552006-03-27 01:18:10 -08007668EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669
NeilBrown746d3202013-04-24 11:42:41 +10007670static int remove_and_add_spares(struct mddev *mddev,
7671 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007672{
NeilBrown3cb03002011-10-11 16:45:26 +11007673 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007674 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007675 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007676
NeilBrowndafb20f2012-03-19 12:46:39 +11007677 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007678 if ((this == NULL || rdev == this) &&
7679 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007680 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007681 (test_bit(Faulty, &rdev->flags) ||
7682 ! test_bit(In_sync, &rdev->flags)) &&
7683 atomic_read(&rdev->nr_pending)==0) {
7684 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007685 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007686 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007687 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007688 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007689 }
7690 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007691 if (removed && mddev->kobj.sd)
7692 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007693
NeilBrown746d3202013-04-24 11:42:41 +10007694 if (this)
7695 goto no_add;
7696
NeilBrowndafb20f2012-03-19 12:46:39 +11007697 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007698 if (rdev->raid_disk >= 0 &&
7699 !test_bit(In_sync, &rdev->flags) &&
7700 !test_bit(Faulty, &rdev->flags))
7701 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007702 if (rdev->raid_disk >= 0)
7703 continue;
7704 if (test_bit(Faulty, &rdev->flags))
7705 continue;
7706 if (mddev->ro &&
7707 rdev->saved_raid_disk < 0)
7708 continue;
7709
7710 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007711 if (mddev->pers->
7712 hot_add_disk(mddev, rdev) == 0) {
7713 if (sysfs_link_rdev(mddev, rdev))
7714 /* failure here is OK */;
7715 spares++;
7716 md_new_event(mddev);
7717 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007718 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007719 }
NeilBrown746d3202013-04-24 11:42:41 +10007720no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007721 if (removed)
7722 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007723 return spares;
7724}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007725
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726/*
7727 * This routine is regularly called by all per-raid-array threads to
7728 * deal with generic issues like resync and super-block update.
7729 * Raid personalities that don't have a thread (linear/raid0) do not
7730 * need this as they never do any recovery or update the superblock.
7731 *
7732 * It does not do any resync itself, but rather "forks" off other threads
7733 * to do that as needed.
7734 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7735 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007736 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 * and wakeups up this thread which will reap the thread and finish up.
7738 * This thread also removes any faulty devices (with nr_pending == 0).
7739 *
7740 * The overall approach is:
7741 * 1/ if the superblock needs updating, update it.
7742 * 2/ If a recovery thread is running, don't do anything else.
7743 * 3/ If recovery has finished, clean up, possibly marking spares active.
7744 * 4/ If there are any faulty devices, remove them.
7745 * 5/ If array is degraded, try to add spares devices
7746 * 6/ If array has spares or is not in-sync, start a resync thread.
7747 */
NeilBrownfd01b882011-10-11 16:47:53 +11007748void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007750 if (mddev->suspended)
7751 return;
7752
NeilBrown5f404022005-06-21 17:17:16 -07007753 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007754 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
NeilBrownfca4d842005-06-21 17:17:11 -07007756 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007757 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007758 printk(KERN_INFO "md: %s in immediate safe mode\n",
7759 mdname(mddev));
7760 mddev->safemode = 2;
7761 }
7762 flush_signals(current);
7763 }
7764
NeilBrownc89a8ee2008-08-05 15:54:13 +10007765 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007768 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007770 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007771 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007772 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7773 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 ))
7775 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007776
NeilBrowndf5b89b2006-03-27 01:18:20 -08007777 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007778 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007779
NeilBrownc89a8ee2008-08-05 15:54:13 +10007780 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007781 /* On a read-only array we can:
7782 * - remove failed devices
7783 * - add already-in_sync devices if the array itself
7784 * is in-sync.
7785 * As we only add devices that are already in-sync,
7786 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007787 */
NeilBrownc89a8ee2008-08-05 15:54:13 +10007788 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown7ceb17e2013-04-24 11:42:42 +10007789 remove_and_add_spares(mddev, NULL);
7790 mddev->pers->spare_active(mddev);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007791 goto unlock;
7792 }
7793
NeilBrown31a59e32008-04-30 00:52:30 -07007794 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007795 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007796 spin_lock_irq(&mddev->write_lock);
7797 if (mddev->safemode &&
7798 !atomic_read(&mddev->writes_pending) &&
7799 !mddev->in_sync &&
7800 mddev->recovery_cp == MaxSector) {
7801 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007802 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007803 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007804 }
7805 if (mddev->safemode == 1)
7806 mddev->safemode = 0;
7807 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007808 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007809 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007810 }
NeilBrownfca4d842005-06-21 17:17:11 -07007811
NeilBrown7a0a5352013-08-27 16:28:23 +10007812 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007813 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007814
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7816 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7817 /* resync/recovery still happening */
7818 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7819 goto unlock;
7820 }
7821 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007822 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 goto unlock;
7824 }
Neil Brown72a23c22008-06-28 08:31:41 +10007825 /* Set RUNNING before clearing NEEDED to avoid
7826 * any transients in the value of "sync_action".
7827 */
NeilBrown72f36d52012-10-11 14:25:57 +11007828 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007829 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007830 /* Clear some bits that don't mean anything, but
7831 * might be left set
7832 */
NeilBrown24dd4692005-11-08 21:39:26 -08007833 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7834 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
NeilBrowned209582012-04-24 10:23:14 +10007836 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7837 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007838 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 /* no recovery is running.
7840 * remove any failed drives, then
7841 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007842 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 * the personality to fail the re-add.
7844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007846 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007847 if (mddev->pers->check_reshape == NULL ||
7848 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007849 /* Cannot proceed */
7850 goto unlock;
7851 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007852 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007853 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007854 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7855 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007856 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007857 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007858 } else if (mddev->recovery_cp < MaxSector) {
7859 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007860 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007861 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7862 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007864
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007866 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007867 /* We are adding a device or devices to an array
7868 * which has the bitmap stored on all devices.
7869 * So make sure all bitmap pages get written
7870 */
7871 bitmap_write_all(mddev->bitmap);
7872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 mddev->sync_thread = md_register_thread(md_do_sync,
7874 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007875 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 if (!mddev->sync_thread) {
7877 printk(KERN_ERR "%s: could not start resync"
7878 " thread...\n",
7879 mdname(mddev));
7880 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007881 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7882 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7883 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7884 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007886 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007888 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007889 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 }
7891 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007892 wake_up(&mddev->sb_wait);
7893
Neil Brown72a23c22008-06-28 08:31:41 +10007894 if (!mddev->sync_thread) {
7895 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7897 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007898 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007899 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 mddev_unlock(mddev);
7902 }
7903}
7904
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007905void md_reap_sync_thread(struct mddev *mddev)
7906{
7907 struct md_rdev *rdev;
7908
7909 /* resync has finished, collect result */
7910 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007911 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007912 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7913 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7914 /* success...*/
7915 /* activate any spares */
7916 if (mddev->pers->spare_active(mddev)) {
7917 sysfs_notify(&mddev->kobj, NULL,
7918 "degraded");
7919 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7920 }
7921 }
7922 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7923 mddev->pers->finish_reshape)
7924 mddev->pers->finish_reshape(mddev);
7925
7926 /* If array is no-longer degraded, then any saved_raid_disk
7927 * information must be scrapped. Also if any device is now
7928 * In_sync we must scrape the saved_raid_disk for that device
7929 * do the superblock for an incrementally recovered device
7930 * written out.
7931 */
7932 rdev_for_each(rdev, mddev)
7933 if (!mddev->degraded ||
7934 test_bit(In_sync, &rdev->flags))
7935 rdev->saved_raid_disk = -1;
7936
7937 md_update_sb(mddev, 1);
7938 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7939 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7940 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7941 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7942 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7943 /* flag recovery needed just to double check */
7944 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7945 sysfs_notify_dirent_safe(mddev->sysfs_action);
7946 md_new_event(mddev);
7947 if (mddev->event_work.func)
7948 queue_work(md_misc_wq, &mddev->event_work);
7949}
7950
NeilBrownfd01b882011-10-11 16:47:53 +11007951void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007952{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007953 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007954 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007955 !test_bit(Blocked, &rdev->flags) &&
7956 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007957 msecs_to_jiffies(5000));
7958 rdev_dec_pending(rdev, mddev);
7959}
7960EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7961
NeilBrownc6563a82012-05-21 09:27:00 +10007962void md_finish_reshape(struct mddev *mddev)
7963{
7964 /* called be personality module when reshape completes. */
7965 struct md_rdev *rdev;
7966
7967 rdev_for_each(rdev, mddev) {
7968 if (rdev->data_offset > rdev->new_data_offset)
7969 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7970 else
7971 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7972 rdev->data_offset = rdev->new_data_offset;
7973 }
7974}
7975EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007976
7977/* Bad block management.
7978 * We can record which blocks on each device are 'bad' and so just
7979 * fail those blocks, or that stripe, rather than the whole device.
7980 * Entries in the bad-block table are 64bits wide. This comprises:
7981 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7982 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7983 * A 'shift' can be set so that larger blocks are tracked and
7984 * consequently larger devices can be covered.
7985 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7986 *
7987 * Locking of the bad-block table uses a seqlock so md_is_badblock
7988 * might need to retry if it is very unlucky.
7989 * We will sometimes want to check for bad blocks in a bi_end_io function,
7990 * so we use the write_seqlock_irq variant.
7991 *
7992 * When looking for a bad block we specify a range and want to
7993 * know if any block in the range is bad. So we binary-search
7994 * to the last range that starts at-or-before the given endpoint,
7995 * (or "before the sector after the target range")
7996 * then see if it ends after the given start.
7997 * We return
7998 * 0 if there are no known bad blocks in the range
7999 * 1 if there are known bad block which are all acknowledged
8000 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8001 * plus the start/length of the first bad section we overlap.
8002 */
8003int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8004 sector_t *first_bad, int *bad_sectors)
8005{
8006 int hi;
majianpengab056132012-11-06 17:13:44 +08008007 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008008 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008009 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008010 sector_t target = s + sectors;
8011 unsigned seq;
8012
8013 if (bb->shift > 0) {
8014 /* round the start down, and the end up */
8015 s >>= bb->shift;
8016 target += (1<<bb->shift) - 1;
8017 target >>= bb->shift;
8018 sectors = target - s;
8019 }
8020 /* 'target' is now the first block after the bad range */
8021
8022retry:
8023 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008024 lo = 0;
8025 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008026 hi = bb->count;
8027
8028 /* Binary search between lo and hi for 'target'
8029 * i.e. for the last range that starts before 'target'
8030 */
8031 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8032 * are known not to be the last range before target.
8033 * VARIANT: hi-lo is the number of possible
8034 * ranges, and decreases until it reaches 1
8035 */
8036 while (hi - lo > 1) {
8037 int mid = (lo + hi) / 2;
8038 sector_t a = BB_OFFSET(p[mid]);
8039 if (a < target)
8040 /* This could still be the one, earlier ranges
8041 * could not. */
8042 lo = mid;
8043 else
8044 /* This and later ranges are definitely out. */
8045 hi = mid;
8046 }
8047 /* 'lo' might be the last that started before target, but 'hi' isn't */
8048 if (hi > lo) {
8049 /* need to check all range that end after 's' to see if
8050 * any are unacknowledged.
8051 */
8052 while (lo >= 0 &&
8053 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8054 if (BB_OFFSET(p[lo]) < target) {
8055 /* starts before the end, and finishes after
8056 * the start, so they must overlap
8057 */
8058 if (rv != -1 && BB_ACK(p[lo]))
8059 rv = 1;
8060 else
8061 rv = -1;
8062 *first_bad = BB_OFFSET(p[lo]);
8063 *bad_sectors = BB_LEN(p[lo]);
8064 }
8065 lo--;
8066 }
8067 }
8068
8069 if (read_seqretry(&bb->lock, seq))
8070 goto retry;
8071
8072 return rv;
8073}
8074EXPORT_SYMBOL_GPL(md_is_badblock);
8075
8076/*
8077 * Add a range of bad blocks to the table.
8078 * This might extend the table, or might contract it
8079 * if two adjacent ranges can be merged.
8080 * We binary-search to find the 'insertion' point, then
8081 * decide how best to handle it.
8082 */
8083static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8084 int acknowledged)
8085{
8086 u64 *p;
8087 int lo, hi;
8088 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008089 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008090
8091 if (bb->shift < 0)
8092 /* badblocks are disabled */
8093 return 0;
8094
8095 if (bb->shift) {
8096 /* round the start down, and the end up */
8097 sector_t next = s + sectors;
8098 s >>= bb->shift;
8099 next += (1<<bb->shift) - 1;
8100 next >>= bb->shift;
8101 sectors = next - s;
8102 }
8103
Bian Yu905b0292013-10-12 01:10:03 -04008104 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008105
8106 p = bb->page;
8107 lo = 0;
8108 hi = bb->count;
8109 /* Find the last range that starts at-or-before 's' */
8110 while (hi - lo > 1) {
8111 int mid = (lo + hi) / 2;
8112 sector_t a = BB_OFFSET(p[mid]);
8113 if (a <= s)
8114 lo = mid;
8115 else
8116 hi = mid;
8117 }
8118 if (hi > lo && BB_OFFSET(p[lo]) > s)
8119 hi = lo;
8120
8121 if (hi > lo) {
8122 /* we found a range that might merge with the start
8123 * of our new range
8124 */
8125 sector_t a = BB_OFFSET(p[lo]);
8126 sector_t e = a + BB_LEN(p[lo]);
8127 int ack = BB_ACK(p[lo]);
8128 if (e >= s) {
8129 /* Yes, we can merge with a previous range */
8130 if (s == a && s + sectors >= e)
8131 /* new range covers old */
8132 ack = acknowledged;
8133 else
8134 ack = ack && acknowledged;
8135
8136 if (e < s + sectors)
8137 e = s + sectors;
8138 if (e - a <= BB_MAX_LEN) {
8139 p[lo] = BB_MAKE(a, e-a, ack);
8140 s = e;
8141 } else {
8142 /* does not all fit in one range,
8143 * make p[lo] maximal
8144 */
8145 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8146 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8147 s = a + BB_MAX_LEN;
8148 }
8149 sectors = e - s;
8150 }
8151 }
8152 if (sectors && hi < bb->count) {
8153 /* 'hi' points to the first range that starts after 's'.
8154 * Maybe we can merge with the start of that range */
8155 sector_t a = BB_OFFSET(p[hi]);
8156 sector_t e = a + BB_LEN(p[hi]);
8157 int ack = BB_ACK(p[hi]);
8158 if (a <= s + sectors) {
8159 /* merging is possible */
8160 if (e <= s + sectors) {
8161 /* full overlap */
8162 e = s + sectors;
8163 ack = acknowledged;
8164 } else
8165 ack = ack && acknowledged;
8166
8167 a = s;
8168 if (e - a <= BB_MAX_LEN) {
8169 p[hi] = BB_MAKE(a, e-a, ack);
8170 s = e;
8171 } else {
8172 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8173 s = a + BB_MAX_LEN;
8174 }
8175 sectors = e - s;
8176 lo = hi;
8177 hi++;
8178 }
8179 }
8180 if (sectors == 0 && hi < bb->count) {
8181 /* we might be able to combine lo and hi */
8182 /* Note: 's' is at the end of 'lo' */
8183 sector_t a = BB_OFFSET(p[hi]);
8184 int lolen = BB_LEN(p[lo]);
8185 int hilen = BB_LEN(p[hi]);
8186 int newlen = lolen + hilen - (s - a);
8187 if (s >= a && newlen < BB_MAX_LEN) {
8188 /* yes, we can combine them */
8189 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8190 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8191 memmove(p + hi, p + hi + 1,
8192 (bb->count - hi - 1) * 8);
8193 bb->count--;
8194 }
8195 }
8196 while (sectors) {
8197 /* didn't merge (it all).
8198 * Need to add a range just before 'hi' */
8199 if (bb->count >= MD_MAX_BADBLOCKS) {
8200 /* No room for more */
8201 rv = 0;
8202 break;
8203 } else {
8204 int this_sectors = sectors;
8205 memmove(p + hi + 1, p + hi,
8206 (bb->count - hi) * 8);
8207 bb->count++;
8208
8209 if (this_sectors > BB_MAX_LEN)
8210 this_sectors = BB_MAX_LEN;
8211 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8212 sectors -= this_sectors;
8213 s += this_sectors;
8214 }
8215 }
8216
8217 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008218 if (!acknowledged)
8219 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008220 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008221
8222 return rv;
8223}
8224
NeilBrown3cb03002011-10-11 16:45:26 +11008225int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008226 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008227{
NeilBrownc6563a82012-05-21 09:27:00 +10008228 int rv;
8229 if (is_new)
8230 s += rdev->new_data_offset;
8231 else
8232 s += rdev->data_offset;
8233 rv = md_set_badblocks(&rdev->badblocks,
8234 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008235 if (rv) {
8236 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008237 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008238 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8239 md_wakeup_thread(rdev->mddev->thread);
8240 }
8241 return rv;
8242}
8243EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8244
8245/*
8246 * Remove a range of bad blocks from the table.
8247 * This may involve extending the table if we spilt a region,
8248 * but it must not fail. So if the table becomes full, we just
8249 * drop the remove request.
8250 */
8251static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8252{
8253 u64 *p;
8254 int lo, hi;
8255 sector_t target = s + sectors;
8256 int rv = 0;
8257
8258 if (bb->shift > 0) {
8259 /* When clearing we round the start up and the end down.
8260 * This should not matter as the shift should align with
8261 * the block size and no rounding should ever be needed.
8262 * However it is better the think a block is bad when it
8263 * isn't than to think a block is not bad when it is.
8264 */
8265 s += (1<<bb->shift) - 1;
8266 s >>= bb->shift;
8267 target >>= bb->shift;
8268 sectors = target - s;
8269 }
8270
8271 write_seqlock_irq(&bb->lock);
8272
8273 p = bb->page;
8274 lo = 0;
8275 hi = bb->count;
8276 /* Find the last range that starts before 'target' */
8277 while (hi - lo > 1) {
8278 int mid = (lo + hi) / 2;
8279 sector_t a = BB_OFFSET(p[mid]);
8280 if (a < target)
8281 lo = mid;
8282 else
8283 hi = mid;
8284 }
8285 if (hi > lo) {
8286 /* p[lo] is the last range that could overlap the
8287 * current range. Earlier ranges could also overlap,
8288 * but only this one can overlap the end of the range.
8289 */
8290 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8291 /* Partial overlap, leave the tail of this range */
8292 int ack = BB_ACK(p[lo]);
8293 sector_t a = BB_OFFSET(p[lo]);
8294 sector_t end = a + BB_LEN(p[lo]);
8295
8296 if (a < s) {
8297 /* we need to split this range */
8298 if (bb->count >= MD_MAX_BADBLOCKS) {
8299 rv = 0;
8300 goto out;
8301 }
8302 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8303 bb->count++;
8304 p[lo] = BB_MAKE(a, s-a, ack);
8305 lo++;
8306 }
8307 p[lo] = BB_MAKE(target, end - target, ack);
8308 /* there is no longer an overlap */
8309 hi = lo;
8310 lo--;
8311 }
8312 while (lo >= 0 &&
8313 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8314 /* This range does overlap */
8315 if (BB_OFFSET(p[lo]) < s) {
8316 /* Keep the early parts of this range. */
8317 int ack = BB_ACK(p[lo]);
8318 sector_t start = BB_OFFSET(p[lo]);
8319 p[lo] = BB_MAKE(start, s - start, ack);
8320 /* now low doesn't overlap, so.. */
8321 break;
8322 }
8323 lo--;
8324 }
8325 /* 'lo' is strictly before, 'hi' is strictly after,
8326 * anything between needs to be discarded
8327 */
8328 if (hi - lo > 1) {
8329 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8330 bb->count -= (hi - lo - 1);
8331 }
8332 }
8333
8334 bb->changed = 1;
8335out:
8336 write_sequnlock_irq(&bb->lock);
8337 return rv;
8338}
8339
NeilBrownc6563a82012-05-21 09:27:00 +10008340int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8341 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008342{
NeilBrownc6563a82012-05-21 09:27:00 +10008343 if (is_new)
8344 s += rdev->new_data_offset;
8345 else
8346 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008347 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008348 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008349}
8350EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8351
8352/*
8353 * Acknowledge all bad blocks in a list.
8354 * This only succeeds if ->changed is clear. It is used by
8355 * in-kernel metadata updates
8356 */
8357void md_ack_all_badblocks(struct badblocks *bb)
8358{
8359 if (bb->page == NULL || bb->changed)
8360 /* no point even trying */
8361 return;
8362 write_seqlock_irq(&bb->lock);
8363
majianpengecb178b2012-03-19 12:46:42 +11008364 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008365 u64 *p = bb->page;
8366 int i;
8367 for (i = 0; i < bb->count ; i++) {
8368 if (!BB_ACK(p[i])) {
8369 sector_t start = BB_OFFSET(p[i]);
8370 int len = BB_LEN(p[i]);
8371 p[i] = BB_MAKE(start, len, 1);
8372 }
8373 }
NeilBrownde393cd2011-07-28 11:31:48 +10008374 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008375 }
8376 write_sequnlock_irq(&bb->lock);
8377}
8378EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8379
NeilBrown16c791a2011-07-28 11:31:47 +10008380/* sysfs access to bad-blocks list.
8381 * We present two files.
8382 * 'bad-blocks' lists sector numbers and lengths of ranges that
8383 * are recorded as bad. The list is truncated to fit within
8384 * the one-page limit of sysfs.
8385 * Writing "sector length" to this file adds an acknowledged
8386 * bad block list.
8387 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8388 * been acknowledged. Writing to this file adds bad blocks
8389 * without acknowledging them. This is largely for testing.
8390 */
8391
8392static ssize_t
8393badblocks_show(struct badblocks *bb, char *page, int unack)
8394{
8395 size_t len;
8396 int i;
8397 u64 *p = bb->page;
8398 unsigned seq;
8399
8400 if (bb->shift < 0)
8401 return 0;
8402
8403retry:
8404 seq = read_seqbegin(&bb->lock);
8405
8406 len = 0;
8407 i = 0;
8408
8409 while (len < PAGE_SIZE && i < bb->count) {
8410 sector_t s = BB_OFFSET(p[i]);
8411 unsigned int length = BB_LEN(p[i]);
8412 int ack = BB_ACK(p[i]);
8413 i++;
8414
8415 if (unack && ack)
8416 continue;
8417
8418 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8419 (unsigned long long)s << bb->shift,
8420 length << bb->shift);
8421 }
NeilBrownde393cd2011-07-28 11:31:48 +10008422 if (unack && len == 0)
8423 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008424
8425 if (read_seqretry(&bb->lock, seq))
8426 goto retry;
8427
8428 return len;
8429}
8430
8431#define DO_DEBUG 1
8432
8433static ssize_t
8434badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8435{
8436 unsigned long long sector;
8437 int length;
8438 char newline;
8439#ifdef DO_DEBUG
8440 /* Allow clearing via sysfs *only* for testing/debugging.
8441 * Normally only a successful write may clear a badblock
8442 */
8443 int clear = 0;
8444 if (page[0] == '-') {
8445 clear = 1;
8446 page++;
8447 }
8448#endif /* DO_DEBUG */
8449
8450 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8451 case 3:
8452 if (newline != '\n')
8453 return -EINVAL;
8454 case 2:
8455 if (length <= 0)
8456 return -EINVAL;
8457 break;
8458 default:
8459 return -EINVAL;
8460 }
8461
8462#ifdef DO_DEBUG
8463 if (clear) {
8464 md_clear_badblocks(bb, sector, length);
8465 return len;
8466 }
8467#endif /* DO_DEBUG */
8468 if (md_set_badblocks(bb, sector, length, !unack))
8469 return len;
8470 else
8471 return -ENOSPC;
8472}
8473
Adrian Bunk75c96f82005-05-05 16:16:09 -07008474static int md_notify_reboot(struct notifier_block *this,
8475 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476{
8477 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008478 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008479 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
NeilBrownc744a652012-03-19 12:46:37 +11008481 for_each_mddev(mddev, tmp) {
8482 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008483 if (mddev->pers)
8484 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008485 mddev->safemode = 2;
8486 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008487 }
NeilBrownc744a652012-03-19 12:46:37 +11008488 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 }
NeilBrownc744a652012-03-19 12:46:37 +11008490 /*
8491 * certain more exotic SCSI devices are known to be
8492 * volatile wrt too early system reboots. While the
8493 * right place to handle this issue is the given
8494 * driver, we do want to have a safe RAID driver ...
8495 */
8496 if (need_delay)
8497 mdelay(1000*1);
8498
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499 return NOTIFY_DONE;
8500}
8501
Adrian Bunk75c96f82005-05-05 16:16:09 -07008502static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503 .notifier_call = md_notify_reboot,
8504 .next = NULL,
8505 .priority = INT_MAX, /* before any real devices */
8506};
8507
8508static void md_geninit(void)
8509{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008510 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008512 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513}
8514
Adrian Bunk75c96f82005-05-05 16:16:09 -07008515static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516{
Tejun Heoe804ac72010-10-15 15:36:08 +02008517 int ret = -ENOMEM;
8518
Tejun Heoada609e2011-01-25 14:35:54 +01008519 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008520 if (!md_wq)
8521 goto err_wq;
8522
8523 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8524 if (!md_misc_wq)
8525 goto err_misc_wq;
8526
8527 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8528 goto err_md;
8529
8530 if ((ret = register_blkdev(0, "mdp")) < 0)
8531 goto err_mdp;
8532 mdp_major = ret;
8533
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008534 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008535 md_probe, NULL, NULL);
8536 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 md_probe, NULL, NULL);
8538
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008540 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541
8542 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544
Tejun Heoe804ac72010-10-15 15:36:08 +02008545err_mdp:
8546 unregister_blkdev(MD_MAJOR, "md");
8547err_md:
8548 destroy_workqueue(md_misc_wq);
8549err_misc_wq:
8550 destroy_workqueue(md_wq);
8551err_wq:
8552 return ret;
8553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554
8555#ifndef MODULE
8556
8557/*
8558 * Searches all registered partitions for autorun RAID arrays
8559 * at boot time.
8560 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008561
8562static LIST_HEAD(all_detected_devices);
8563struct detected_devices_node {
8564 struct list_head list;
8565 dev_t dev;
8566};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567
8568void md_autodetect_dev(dev_t dev)
8569{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008570 struct detected_devices_node *node_detected_dev;
8571
8572 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8573 if (node_detected_dev) {
8574 node_detected_dev->dev = dev;
8575 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8576 } else {
8577 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8578 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580}
8581
8582
8583static void autostart_arrays(int part)
8584{
NeilBrown3cb03002011-10-11 16:45:26 +11008585 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008586 struct detected_devices_node *node_detected_dev;
8587 dev_t dev;
8588 int i_scanned, i_passed;
8589
8590 i_scanned = 0;
8591 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
8593 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8594
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008595 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8596 i_scanned++;
8597 node_detected_dev = list_entry(all_detected_devices.next,
8598 struct detected_devices_node, list);
8599 list_del(&node_detected_dev->list);
8600 dev = node_detected_dev->dev;
8601 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008602 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 if (IS_ERR(rdev))
8604 continue;
8605
NeilBrownb2d444d2005-11-08 21:39:31 -08008606 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607 MD_BUG();
8608 continue;
8609 }
NeilBrownd0fae182008-03-04 14:29:31 -08008610 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008612 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008614
8615 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8616 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617
8618 autorun_devices(part);
8619}
8620
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008621#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622
8623static __exit void md_exit(void)
8624{
NeilBrownfd01b882011-10-11 16:47:53 +11008625 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008627
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008628 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008629 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008631 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 unregister_blkdev(mdp_major, "mdp");
8633 unregister_reboot_notifier(&md_notifier);
8634 unregister_sysctl_table(raid_table_header);
8635 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008636 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008638 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008640 destroy_workqueue(md_misc_wq);
8641 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642}
8643
Dan Williams685784a2007-07-09 11:56:42 -07008644subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645module_exit(md_exit)
8646
NeilBrownf91de922005-11-08 21:39:36 -08008647static int get_ro(char *buffer, struct kernel_param *kp)
8648{
8649 return sprintf(buffer, "%d", start_readonly);
8650}
8651static int set_ro(const char *val, struct kernel_param *kp)
8652{
8653 char *e;
8654 int num = simple_strtoul(val, &e, 10);
8655 if (*val && (*e == '\0' || *e == '\n')) {
8656 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008657 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008658 }
8659 return -EINVAL;
8660}
8661
NeilBrown80ca3a42006-07-10 04:44:18 -07008662module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8663module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008664
NeilBrownefeb53c2009-01-09 08:31:10 +11008665module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008666
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667EXPORT_SYMBOL(register_md_personality);
8668EXPORT_SYMBOL(unregister_md_personality);
8669EXPORT_SYMBOL(md_error);
8670EXPORT_SYMBOL(md_done_sync);
8671EXPORT_SYMBOL(md_write_start);
8672EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673EXPORT_SYMBOL(md_register_thread);
8674EXPORT_SYMBOL(md_unregister_thread);
8675EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008677EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008679MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008680MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008681MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);