blob: 539f08885e7fb2d85de65d25e558d4ba2678e30c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
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
NeilBrown6712ecf2007-09-27 12:47:43 +0200779static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
NeilBrown3cb03002011-10-11 16:45:26 +1100784int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100785 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
NeilBrowna167f662010-10-26 18:31:13 +1100787 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct completion event;
789 int ret;
790
Jens Axboe721a9602011-03-09 11:56:30 +0100791 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100793 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
794 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100795 if (metadata_op)
796 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000797 else if (rdev->mddev->reshape_position != MaxSector &&
798 (rdev->mddev->reshape_backwards ==
799 (sector >= rdev->mddev->reshape_position)))
800 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100801 else
802 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 bio_add_page(bio, page, size, 0);
804 init_completion(&event);
805 bio->bi_private = &event;
806 bio->bi_end_io = bi_complete;
807 submit_bio(rw, bio);
808 wait_for_completion(&event);
809
810 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
811 bio_put(bio);
812 return ret;
813}
NeilBrowna8745db2006-01-06 00:20:34 -0800814EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
NeilBrown3cb03002011-10-11 16:45:26 +1100816static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 char b[BDEVNAME_SIZE];
819 if (!rdev->sb_page) {
820 MD_BUG();
821 return -EINVAL;
822 }
823 if (rdev->sb_loaded)
824 return 0;
825
826
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100827 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto fail;
829 rdev->sb_loaded = 1;
830 return 0;
831
832fail:
833 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
834 bdevname(rdev->bdev,b));
835 return -EINVAL;
836}
837
838static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
839{
Andre Noll05710462008-07-11 22:02:20 +1000840 return sb1->set_uuid0 == sb2->set_uuid0 &&
841 sb1->set_uuid1 == sb2->set_uuid1 &&
842 sb1->set_uuid2 == sb2->set_uuid2 &&
843 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
847{
848 int ret;
849 mdp_super_t *tmp1, *tmp2;
850
851 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
852 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
853
854 if (!tmp1 || !tmp2) {
855 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100856 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 goto abort;
858 }
859
860 *tmp1 = *sb1;
861 *tmp2 = *sb2;
862
863 /*
864 * nr_disks is not constant
865 */
866 tmp1->nr_disks = 0;
867 tmp2->nr_disks = 0;
868
Andre Nollce0c8e02008-07-11 22:02:20 +1000869 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700871 kfree(tmp1);
872 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return ret;
874}
875
NeilBrown4d167f02007-05-09 02:35:37 -0700876
877static u32 md_csum_fold(u32 csum)
878{
879 csum = (csum & 0xffff) + (csum >> 16);
880 return (csum & 0xffff) + (csum >> 16);
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static unsigned int calc_sb_csum(mdp_super_t * sb)
884{
NeilBrown4d167f02007-05-09 02:35:37 -0700885 u64 newcsum = 0;
886 u32 *sb32 = (u32*)sb;
887 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 unsigned int disk_csum, csum;
889
890 disk_csum = sb->sb_csum;
891 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700892
893 for (i = 0; i < MD_SB_BYTES/4 ; i++)
894 newcsum += sb32[i];
895 csum = (newcsum & 0xffffffff) + (newcsum>>32);
896
897
898#ifdef CONFIG_ALPHA
899 /* This used to use csum_partial, which was wrong for several
900 * reasons including that different results are returned on
901 * different architectures. It isn't critical that we get exactly
902 * the same return value as before (we always csum_fold before
903 * testing, and that removes any differences). However as we
904 * know that csum_partial always returned a 16bit value on
905 * alphas, do a fold to maximise conformity to previous behaviour.
906 */
907 sb->sb_csum = md_csum_fold(disk_csum);
908#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700910#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return csum;
912}
913
914
915/*
916 * Handle superblock details.
917 * We want to be able to handle multiple superblock formats
918 * so we have a common interface to them all, and an array of
919 * different handlers.
920 * We rely on user-space to write the initial superblock, and support
921 * reading and updating of superblocks.
922 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100923 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * loads and validates a superblock on dev.
925 * if refdev != NULL, compare superblocks on both devices
926 * Return:
927 * 0 - dev has a superblock that is compatible with refdev
928 * 1 - dev has a superblock that is compatible and newer than refdev
929 * so dev should be used as the refdev in future
930 * -EINVAL superblock incompatible or invalid
931 * -othererror e.g. -EIO
932 *
NeilBrownfd01b882011-10-11 16:47:53 +1100933 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * Verify that dev is acceptable into mddev.
935 * The first time, mddev->raid_disks will be 0, and data from
936 * dev should be merged in. Subsequent calls check that dev
937 * is new enough. Return 0 or -EINVAL
938 *
NeilBrownfd01b882011-10-11 16:47:53 +1100939 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * Update the superblock for rdev with data in mddev
941 * This does not write to disc.
942 *
943 */
944
945struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000946 char *name;
947 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000948 int (*load_super)(struct md_rdev *rdev,
949 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000950 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000951 int (*validate_super)(struct mddev *mddev,
952 struct md_rdev *rdev);
953 void (*sync_super)(struct mddev *mddev,
954 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100955 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000956 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000957 int (*allow_new_offset)(struct md_rdev *rdev,
958 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959};
960
961/*
Andre Noll0894cc32009-06-18 08:49:23 +1000962 * Check that the given mddev has no bitmap.
963 *
964 * This function is called from the run method of all personalities that do not
965 * support bitmaps. It prints an error message and returns non-zero if mddev
966 * has a bitmap. Otherwise, it returns 0.
967 *
968 */
NeilBrownfd01b882011-10-11 16:47:53 +1100969int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000970{
NeilBrownc3d97142009-12-14 12:49:52 +1100971 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000972 return 0;
973 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
974 mdname(mddev), mddev->pers->name);
975 return 1;
976}
977EXPORT_SYMBOL(md_check_no_bitmap);
978
979/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * load_super for 0.90.0
981 */
NeilBrown3cb03002011-10-11 16:45:26 +1100982static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
985 mdp_super_t *sb;
986 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /*
Andre Noll0f420352008-07-11 22:02:23 +1000989 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * it's at the end of the disk.
991 *
992 * It also happens to be a multiple of 4Kb.
993 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100994 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
NeilBrown0002b272005-09-09 16:23:53 -0700996 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ret) return ret;
998
999 ret = -EINVAL;
1000
1001 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001002 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (sb->md_magic != MD_SB_MAGIC) {
1005 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1006 b);
1007 goto abort;
1008 }
1009
1010 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001011 sb->minor_version < 90 ||
1012 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1014 sb->major_version, sb->minor_version,
1015 b);
1016 goto abort;
1017 }
1018
1019 if (sb->raid_disks <= 0)
1020 goto abort;
1021
NeilBrown4d167f02007-05-09 02:35:37 -07001022 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1024 b);
1025 goto abort;
1026 }
1027
1028 rdev->preferred_minor = sb->md_minor;
1029 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001030 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001031 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001032 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (sb->level == LEVEL_MULTIPATH)
1035 rdev->desc_nr = -1;
1036 else
1037 rdev->desc_nr = sb->this_disk.number;
1038
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001039 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001041 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001043 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!uuid_equal(refsb, sb)) {
1045 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1046 b, bdevname(refdev->bdev,b2));
1047 goto abort;
1048 }
1049 if (!sb_equal(refsb, sb)) {
1050 printk(KERN_WARNING "md: %s has same UUID"
1051 " but different superblock to %s\n",
1052 b, bdevname(refdev->bdev, b2));
1053 goto abort;
1054 }
1055 ev1 = md_event(sb);
1056 ev2 = md_event(refsb);
1057 if (ev1 > ev2)
1058 ret = 1;
1059 else
1060 ret = 0;
1061 }
NeilBrown8190e752009-06-18 08:48:58 +10001062 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001063 /* Limit to 4TB as metadata cannot record more than that.
1064 * (not needed for Linear and RAID0 as metadata doesn't
1065 * record this size)
1066 */
1067 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001068 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
NeilBrown27a7b262011-09-10 17:21:28 +10001070 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001071 /* "this cannot possibly happen" ... */
1072 ret = -EINVAL;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 abort:
1075 return ret;
1076}
1077
1078/*
1079 * validate_super for 0.90.0
1080 */
NeilBrownfd01b882011-10-11 16:47:53 +11001081static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001084 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001085 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
NeilBrown41158c72005-06-21 17:17:25 -07001087 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001088 clear_bit(Faulty, &rdev->flags);
1089 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001090 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001091 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (mddev->raid_disks == 0) {
1094 mddev->major_version = 0;
1095 mddev->minor_version = sb->minor_version;
1096 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001097 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001098 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 mddev->ctime = sb->ctime;
1100 mddev->utime = sb->utime;
1101 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001102 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 mddev->layout = sb->layout;
1104 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001105 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001106 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001107 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001108 mddev->bitmap_info.space = 0;
1109 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001110 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001111 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001112 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
NeilBrownf6705572006-03-27 01:18:11 -08001114 if (mddev->minor_version >= 91) {
1115 mddev->reshape_position = sb->reshape_position;
1116 mddev->delta_disks = sb->delta_disks;
1117 mddev->new_level = sb->new_level;
1118 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001119 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001120 if (mddev->delta_disks < 0)
1121 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001122 } else {
1123 mddev->reshape_position = MaxSector;
1124 mddev->delta_disks = 0;
1125 mddev->new_level = mddev->level;
1126 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001127 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (sb->state & (1<<MD_SB_CLEAN))
1131 mddev->recovery_cp = MaxSector;
1132 else {
1133 if (sb->events_hi == sb->cp_events_hi &&
1134 sb->events_lo == sb->cp_events_lo) {
1135 mddev->recovery_cp = sb->recovery_cp;
1136 } else
1137 mddev->recovery_cp = 0;
1138 }
1139
1140 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1141 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1142 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1143 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1144
1145 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001146
1147 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001148 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001149 mddev->bitmap_info.offset =
1150 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001151 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001152 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001153 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001154
NeilBrown41158c72005-06-21 17:17:25 -07001155 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001156 /* Insist on good event counter while assembling, except
1157 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001159 if (sb->disks[rdev->desc_nr].state & (
1160 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1161 if (ev1 < mddev->events)
1162 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001163 } else if (mddev->bitmap) {
1164 /* if adding to array with a bitmap, then we can accept an
1165 * older device ... but not too old.
1166 */
NeilBrown41158c72005-06-21 17:17:25 -07001167 if (ev1 < mddev->bitmap->events_cleared)
1168 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001169 if (ev1 < mddev->events)
1170 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001171 } else {
1172 if (ev1 < mddev->events)
1173 /* just a hot-add of a new device, leave raid_disk at -1 */
1174 return 0;
1175 }
NeilBrown41158c72005-06-21 17:17:25 -07001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 desc = sb->disks + rdev->desc_nr;
1179
1180 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001181 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001182 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1183 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001184 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001186 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001187 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1188 /* active but not in sync implies recovery up to
1189 * reshape position. We don't know exactly where
1190 * that is, so set to zero for now */
1191 if (mddev->minor_version >= 91) {
1192 rdev->recovery_offset = 0;
1193 rdev->raid_disk = desc->raid_disk;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001196 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1197 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001198 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001199 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203/*
1204 * sync_super for 0.90.0
1205 */
NeilBrownfd01b882011-10-11 16:47:53 +11001206static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001209 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* make rdev->sb match mddev data..
1214 *
1215 * 1/ zero out disks
1216 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1217 * 3/ any empty disks < next_spare become removed
1218 *
1219 * disks[0] gets initialised to REMOVED because
1220 * we cannot be sure from other fields if it has
1221 * been initialised or not.
1222 */
1223 int i;
1224 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1225
NeilBrown61181562005-09-09 16:24:02 -07001226 rdev->sb_size = MD_SB_BYTES;
1227
Namhyung Kim65a06f062011-07-27 11:00:36 +10001228 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 memset(sb, 0, sizeof(*sb));
1231
1232 sb->md_magic = MD_SB_MAGIC;
1233 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->patch_version = mddev->patch_version;
1235 sb->gvalid_words = 0; /* ignored */
1236 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1237 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1238 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1239 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1240
1241 sb->ctime = mddev->ctime;
1242 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001243 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sb->raid_disks = mddev->raid_disks;
1245 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001246 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 sb->utime = mddev->utime;
1248 sb->state = 0;
1249 sb->events_hi = (mddev->events>>32);
1250 sb->events_lo = (u32)mddev->events;
1251
NeilBrownf6705572006-03-27 01:18:11 -08001252 if (mddev->reshape_position == MaxSector)
1253 sb->minor_version = 90;
1254 else {
1255 sb->minor_version = 91;
1256 sb->reshape_position = mddev->reshape_position;
1257 sb->new_level = mddev->new_level;
1258 sb->delta_disks = mddev->delta_disks;
1259 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001260 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001261 }
1262 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (mddev->in_sync)
1264 {
1265 sb->recovery_cp = mddev->recovery_cp;
1266 sb->cp_events_hi = (mddev->events>>32);
1267 sb->cp_events_lo = (u32)mddev->events;
1268 if (mddev->recovery_cp == MaxSector)
1269 sb->state = (1<< MD_SB_CLEAN);
1270 } else
1271 sb->recovery_cp = 0;
1272
1273 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001274 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
NeilBrownc3d97142009-12-14 12:49:52 +11001276 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001277 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001280 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001282 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 int is_active = test_bit(In_sync, &rdev2->flags);
1284
1285 if (rdev2->raid_disk >= 0 &&
1286 sb->minor_version >= 91)
1287 /* we have nowhere to store the recovery_offset,
1288 * but if it is not below the reshape_position,
1289 * we can piggy-back on that.
1290 */
1291 is_active = 1;
1292 if (rdev2->raid_disk < 0 ||
1293 test_bit(Faulty, &rdev2->flags))
1294 is_active = 0;
1295 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001296 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001298 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001299 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 d = &sb->disks[rdev2->desc_nr];
1301 nr_disks++;
1302 d->number = rdev2->desc_nr;
1303 d->major = MAJOR(rdev2->bdev->bd_dev);
1304 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001305 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 d->raid_disk = rdev2->raid_disk;
1307 else
1308 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001309 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001311 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001313 if (test_bit(In_sync, &rdev2->flags))
1314 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 active++;
1316 working++;
1317 } else {
1318 d->state = 0;
1319 spare++;
1320 working++;
1321 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001322 if (test_bit(WriteMostly, &rdev2->flags))
1323 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* now set the "removed" and "faulty" bits on any missing devices */
1326 for (i=0 ; i < mddev->raid_disks ; i++) {
1327 mdp_disk_t *d = &sb->disks[i];
1328 if (d->state == 0 && d->number == 0) {
1329 d->number = i;
1330 d->raid_disk = i;
1331 d->state = (1<<MD_DISK_REMOVED);
1332 d->state |= (1<<MD_DISK_FAULTY);
1333 failed++;
1334 }
1335 }
1336 sb->nr_disks = nr_disks;
1337 sb->active_disks = active;
1338 sb->working_disks = working;
1339 sb->failed_disks = failed;
1340 sb->spare_disks = spare;
1341
1342 sb->this_disk = sb->disks[rdev->desc_nr];
1343 sb->sb_csum = calc_sb_csum(sb);
1344}
1345
1346/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347 * rdev_size_change for 0.90.0
1348 */
1349static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001350super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001351{
Andre Noll58c0fed2009-03-31 14:33:13 +11001352 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001353 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001354 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001356 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001357 if (!num_sectors || num_sectors > rdev->sb_start)
1358 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001359 /* Limit to 4TB as metadata cannot record more than that.
1360 * 4TB == 2^32 KB, or 2*2^32 sectors.
1361 */
NeilBrown667a5312012-08-16 16:46:12 +10001362 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001363 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365 rdev->sb_page);
1366 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001367 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001368}
1369
NeilBrownc6563a82012-05-21 09:27:00 +10001370static int
1371super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1372{
1373 /* non-zero offset changes not possible with v0.90 */
1374 return new_offset == 0;
1375}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001376
1377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * version 1 superblock
1379 */
1380
NeilBrown1c05b4b2006-10-21 10:24:08 -07001381static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001383 __le32 disk_csum;
1384 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 unsigned long long newcsum;
1386 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001387 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 disk_csum = sb->sb_csum;
1390 sb->sb_csum = 0;
1391 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001392 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 newcsum += le32_to_cpu(*isuper++);
1394
1395 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001396 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1399 sb->sb_csum = disk_csum;
1400 return cpu_to_le32(csum);
1401}
1402
NeilBrown2699b672011-07-28 11:31:47 +10001403static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1404 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001405static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct mdp_superblock_1 *sb;
1408 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001409 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001410 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001412 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
Andre Noll0f420352008-07-11 22:02:23 +10001415 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * It is always aligned to a 4K boundary and
1417 * depeding on minor_version, it can be:
1418 * 0: At least 8K, but less than 12K, from end of device
1419 * 1: At start of device
1420 * 2: 4K from start of device.
1421 */
1422 switch(minor_version) {
1423 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001424 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001425 sb_start -= 8*2;
1426 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001429 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 break;
1431 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001432 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 break;
1434 default:
1435 return -EINVAL;
1436 }
Andre Noll0f420352008-07-11 22:02:23 +10001437 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
NeilBrown0002b272005-09-09 16:23:53 -07001439 /* superblock is rarely larger than 1K, but it can be larger,
1440 * and it is safe to read 4k, so we do that
1441 */
1442 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (ret) return ret;
1444
1445
Namhyung Kim65a06f062011-07-27 11:00:36 +10001446 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1449 sb->major_version != cpu_to_le32(1) ||
1450 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001451 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001452 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EINVAL;
1454
1455 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1456 printk("md: invalid superblock checksum on %s\n",
1457 bdevname(rdev->bdev,b));
1458 return -EINVAL;
1459 }
1460 if (le64_to_cpu(sb->data_size) < 10) {
1461 printk("md: data_size too small on %s\n",
1462 bdevname(rdev->bdev,b));
1463 return -EINVAL;
1464 }
NeilBrownc6563a82012-05-21 09:27:00 +10001465 if (sb->pad0 ||
1466 sb->pad3[0] ||
1467 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1468 /* Some padding is non-zero, might be a new feature */
1469 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 rdev->preferred_minor = 0xffff;
1472 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001473 rdev->new_data_offset = rdev->data_offset;
1474 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1475 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1476 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001477 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
NeilBrown0002b272005-09-09 16:23:53 -07001479 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001480 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001481 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001482 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1483
1484 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001485 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001486 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001487 if (minor_version
1488 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1489 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001490
NeilBrown31b65a02006-07-10 04:44:14 -07001491 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1492 rdev->desc_nr = -1;
1493 else
1494 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1495
NeilBrown2699b672011-07-28 11:31:47 +10001496 if (!rdev->bb_page) {
1497 rdev->bb_page = alloc_page(GFP_KERNEL);
1498 if (!rdev->bb_page)
1499 return -ENOMEM;
1500 }
1501 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1502 rdev->badblocks.count == 0) {
1503 /* need to load the bad block list.
1504 * Currently we limit it to one page.
1505 */
1506 s32 offset;
1507 sector_t bb_sector;
1508 u64 *bbp;
1509 int i;
1510 int sectors = le16_to_cpu(sb->bblog_size);
1511 if (sectors > (PAGE_SIZE / 512))
1512 return -EINVAL;
1513 offset = le32_to_cpu(sb->bblog_offset);
1514 if (offset == 0)
1515 return -EINVAL;
1516 bb_sector = (long long)offset;
1517 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1518 rdev->bb_page, READ, true))
1519 return -EIO;
1520 bbp = (u64 *)page_address(rdev->bb_page);
1521 rdev->badblocks.shift = sb->bblog_shift;
1522 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1523 u64 bb = le64_to_cpu(*bbp);
1524 int count = bb & (0x3ff);
1525 u64 sector = bb >> 10;
1526 sector <<= sb->bblog_shift;
1527 count <<= sb->bblog_shift;
1528 if (bb + 1 == 0)
1529 break;
1530 if (md_set_badblocks(&rdev->badblocks,
1531 sector, count, 1) == 0)
1532 return -EINVAL;
1533 }
NeilBrown486adf72013-04-24 11:42:44 +10001534 } else if (sb->bblog_offset != 0)
1535 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001536
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001537 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001538 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001539 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001541 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1544 sb->level != refsb->level ||
1545 sb->layout != refsb->layout ||
1546 sb->chunksize != refsb->chunksize) {
1547 printk(KERN_WARNING "md: %s has strangely different"
1548 " superblock to %s\n",
1549 bdevname(rdev->bdev,b),
1550 bdevname(refdev->bdev,b2));
1551 return -EINVAL;
1552 }
1553 ev1 = le64_to_cpu(sb->events);
1554 ev2 = le64_to_cpu(refsb->events);
1555
1556 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001557 ret = 1;
1558 else
1559 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
NeilBrownc6563a82012-05-21 09:27:00 +10001561 if (minor_version) {
1562 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1563 sectors -= rdev->data_offset;
1564 } else
1565 sectors = rdev->sb_start;
1566 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001568 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
NeilBrownfd01b882011-10-11 16:47:53 +11001572static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001574 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001575 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
NeilBrown41158c72005-06-21 17:17:25 -07001577 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001578 clear_bit(Faulty, &rdev->flags);
1579 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001580 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001581 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (mddev->raid_disks == 0) {
1584 mddev->major_version = 1;
1585 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001586 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001587 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1589 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1590 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001591 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 mddev->layout = le32_to_cpu(sb->layout);
1593 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001594 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001595 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001596 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001597 mddev->bitmap_info.space = 0;
1598 /* Default location for bitmap is 1K after superblock
1599 * using 3K - total of 4K
1600 */
NeilBrownc3d97142009-12-14 12:49:52 +11001601 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001602 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001603 mddev->reshape_backwards = 0;
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1606 memcpy(mddev->uuid, sb->set_uuid, 16);
1607
1608 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001609
NeilBrown71c08052005-09-09 16:23:51 -07001610 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001611 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001612 mddev->bitmap_info.offset =
1613 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001614 /* Metadata doesn't record how much space is available.
1615 * For 1.0, we assume we can use up to the superblock
1616 * if before, else to 4K beyond superblock.
1617 * For others, assume no change is possible.
1618 */
1619 if (mddev->minor_version > 0)
1620 mddev->bitmap_info.space = 0;
1621 else if (mddev->bitmap_info.offset > 0)
1622 mddev->bitmap_info.space =
1623 8 - mddev->bitmap_info.offset;
1624 else
1625 mddev->bitmap_info.space =
1626 -mddev->bitmap_info.offset;
1627 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001628
NeilBrownf6705572006-03-27 01:18:11 -08001629 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1630 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1631 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1632 mddev->new_level = le32_to_cpu(sb->new_level);
1633 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001634 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001635 if (mddev->delta_disks < 0 ||
1636 (mddev->delta_disks == 0 &&
1637 (le32_to_cpu(sb->feature_map)
1638 & MD_FEATURE_RESHAPE_BACKWARDS)))
1639 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001640 } else {
1641 mddev->reshape_position = MaxSector;
1642 mddev->delta_disks = 0;
1643 mddev->new_level = mddev->level;
1644 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001645 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001646 }
1647
NeilBrown41158c72005-06-21 17:17:25 -07001648 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001649 /* Insist of good event counter while assembling, except for
1650 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001652 if (rdev->desc_nr >= 0 &&
1653 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1654 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1655 if (ev1 < mddev->events)
1656 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001657 } else if (mddev->bitmap) {
1658 /* If adding to array with a bitmap, then we can accept an
1659 * older device, but not too old.
1660 */
NeilBrown41158c72005-06-21 17:17:25 -07001661 if (ev1 < mddev->bitmap->events_cleared)
1662 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001663 if (ev1 < mddev->events)
1664 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001665 } else {
1666 if (ev1 < mddev->events)
1667 /* just a hot-add of a new device, leave raid_disk at -1 */
1668 return 0;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (mddev->level != LEVEL_MULTIPATH) {
1671 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001672 if (rdev->desc_nr < 0 ||
1673 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1674 role = 0xffff;
1675 rdev->desc_nr = -1;
1676 } else
1677 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 switch(role) {
1679 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
1681 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001682 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
1684 default:
NeilBrownf4667222013-12-09 12:04:56 +11001685 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001686 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001687 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001689 if (!(le32_to_cpu(sb->feature_map) &
1690 MD_FEATURE_RECOVERY_BITMAP))
1691 rdev->saved_raid_disk = -1;
1692 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001693 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 rdev->raid_disk = role;
1695 break;
1696 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001697 if (sb->devflags & WriteMostly1)
1698 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001699 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1700 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001701 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001702 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
1705}
1706
NeilBrownfd01b882011-10-11 16:47:53 +11001707static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001710 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 int max_dev, i;
1712 /* make rdev->sb match mddev and rdev data. */
1713
Namhyung Kim65a06f062011-07-27 11:00:36 +10001714 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 sb->feature_map = 0;
1717 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 memset(sb->pad3, 0, sizeof(sb->pad3));
1720
1721 sb->utime = cpu_to_le64((__u64)mddev->utime);
1722 sb->events = cpu_to_le64(mddev->events);
1723 if (mddev->in_sync)
1724 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1725 else
1726 sb->resync_offset = cpu_to_le64(0);
1727
NeilBrown1c05b4b2006-10-21 10:24:08 -07001728 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001729
NeilBrownf0ca3402006-02-02 14:28:04 -08001730 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001731 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001732 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001733 sb->level = cpu_to_le32(mddev->level);
1734 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001735
NeilBrownaeb9b212011-08-25 14:43:08 +10001736 if (test_bit(WriteMostly, &rdev->flags))
1737 sb->devflags |= WriteMostly1;
1738 else
1739 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001740 sb->data_offset = cpu_to_le64(rdev->data_offset);
1741 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001742
NeilBrownc3d97142009-12-14 12:49:52 +11001743 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1744 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001745 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001746 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001747
1748 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001749 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001750 sb->feature_map |=
1751 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1752 sb->recovery_offset =
1753 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001754 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1755 sb->feature_map |=
1756 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001757 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001758 if (test_bit(Replacement, &rdev->flags))
1759 sb->feature_map |=
1760 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001761
NeilBrownf6705572006-03-27 01:18:11 -08001762 if (mddev->reshape_position != MaxSector) {
1763 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1764 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1765 sb->new_layout = cpu_to_le32(mddev->new_layout);
1766 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1767 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001768 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001769 if (mddev->delta_disks == 0 &&
1770 mddev->reshape_backwards)
1771 sb->feature_map
1772 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001773 if (rdev->new_data_offset != rdev->data_offset) {
1774 sb->feature_map
1775 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1776 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1777 - rdev->data_offset));
1778 }
NeilBrownf6705572006-03-27 01:18:11 -08001779 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001780
NeilBrown2699b672011-07-28 11:31:47 +10001781 if (rdev->badblocks.count == 0)
1782 /* Nothing to do for bad blocks*/ ;
1783 else if (sb->bblog_offset == 0)
1784 /* Cannot record bad blocks on this device */
1785 md_error(mddev, rdev);
1786 else {
1787 struct badblocks *bb = &rdev->badblocks;
1788 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1789 u64 *p = bb->page;
1790 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1791 if (bb->changed) {
1792 unsigned seq;
1793
1794retry:
1795 seq = read_seqbegin(&bb->lock);
1796
1797 memset(bbp, 0xff, PAGE_SIZE);
1798
1799 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001800 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001801 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1802 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001803 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001804 }
NeilBrownd0962932012-03-19 12:46:41 +11001805 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001806 if (read_seqretry(&bb->lock, seq))
1807 goto retry;
1808
1809 bb->sector = (rdev->sb_start +
1810 (int)le32_to_cpu(sb->bblog_offset));
1811 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001812 }
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001816 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (rdev2->desc_nr+1 > max_dev)
1818 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001819
NeilBrown70471da2009-08-03 10:59:57 +10001820 if (max_dev > le32_to_cpu(sb->max_dev)) {
1821 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001822 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001823 rdev->sb_size = max_dev * 2 + 256;
1824 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1825 if (rdev->sb_size & bmask)
1826 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001827 } else
1828 max_dev = le32_to_cpu(sb->max_dev);
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 for (i=0; i<max_dev;i++)
1831 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1832
NeilBrowndafb20f2012-03-19 12:46:39 +11001833 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001837 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001839 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001840 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 else
1842 sb->dev_roles[i] = cpu_to_le16(0xffff);
1843 }
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 sb->sb_csum = calc_sb_1_csum(sb);
1846}
1847
Chris Webb0cd17fe2008-06-28 08:31:46 +10001848static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001849super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850{
1851 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001852 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001853 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001854 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001855 if (rdev->data_offset != rdev->new_data_offset)
1856 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001857 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001859 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 max_sectors -= rdev->data_offset;
1861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001863 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 /* minor version 0 with bitmap we can't move */
1865 return 0;
1866 } else {
1867 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001868 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001869 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001870 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001871 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001872 if (!num_sectors || num_sectors > max_sectors)
1873 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001874 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001876 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001878 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001880 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 rdev->sb_page);
1882 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001883 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001884
1885}
1886
1887static int
1888super_1_allow_new_offset(struct md_rdev *rdev,
1889 unsigned long long new_offset)
1890{
1891 /* All necessary checks on new >= old have been done */
1892 struct bitmap *bitmap;
1893 if (new_offset >= rdev->data_offset)
1894 return 1;
1895
1896 /* with 1.0 metadata, there is no metadata to tread on
1897 * so we can always move back */
1898 if (rdev->mddev->minor_version == 0)
1899 return 1;
1900
1901 /* otherwise we must be sure not to step on
1902 * any metadata, so stay:
1903 * 36K beyond start of superblock
1904 * beyond end of badblocks
1905 * beyond write-intent bitmap
1906 */
1907 if (rdev->sb_start + (32+4)*2 > new_offset)
1908 return 0;
1909 bitmap = rdev->mddev->bitmap;
1910 if (bitmap && !rdev->mddev->bitmap_info.file &&
1911 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001912 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001913 return 0;
1914 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1915 return 0;
1916
1917 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Adrian Bunk75c96f82005-05-05 16:16:09 -07001920static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 [0] = {
1922 .name = "0.90.0",
1923 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001924 .load_super = super_90_load,
1925 .validate_super = super_90_validate,
1926 .sync_super = super_90_sync,
1927 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001928 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 },
1930 [1] = {
1931 .name = "md-1",
1932 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001933 .load_super = super_1_load,
1934 .validate_super = super_1_validate,
1935 .sync_super = super_1_sync,
1936 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001937 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 },
1939};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
NeilBrownfd01b882011-10-11 16:47:53 +11001941static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001942{
1943 if (mddev->sync_super) {
1944 mddev->sync_super(mddev, rdev);
1945 return;
1946 }
1947
1948 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1949
1950 super_types[mddev->major_version].sync_super(mddev, rdev);
1951}
1952
NeilBrownfd01b882011-10-11 16:47:53 +11001953static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
NeilBrown3cb03002011-10-11 16:45:26 +11001955 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
NeilBrown4b809912008-07-21 17:05:25 +10001957 rcu_read_lock();
1958 rdev_for_each_rcu(rdev, mddev1)
1959 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001960 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001961 rdev2->bdev->bd_contains) {
1962 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001963 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001964 }
1965 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return 0;
1967}
1968
1969static LIST_HEAD(pending_raid_disks);
1970
Andre Nollac5e7112009-08-03 10:59:47 +10001971/*
1972 * Try to register data integrity profile for an mddev
1973 *
1974 * This is called when an array is started and after a disk has been kicked
1975 * from the array. It only succeeds if all working and active component devices
1976 * are integrity capable with matching profiles.
1977 */
NeilBrownfd01b882011-10-11 16:47:53 +11001978int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001979{
NeilBrown3cb03002011-10-11 16:45:26 +11001980 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001981
Andre Nollac5e7112009-08-03 10:59:47 +10001982 if (list_empty(&mddev->disks))
1983 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001984 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1985 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001986 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001987 /* skip spares and non-functional disks */
1988 if (test_bit(Faulty, &rdev->flags))
1989 continue;
1990 if (rdev->raid_disk < 0)
1991 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001992 if (!reference) {
1993 /* Use the first rdev as the reference */
1994 reference = rdev;
1995 continue;
1996 }
1997 /* does this rdev's profile match the reference profile? */
1998 if (blk_integrity_compare(reference->bdev->bd_disk,
1999 rdev->bdev->bd_disk) < 0)
2000 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002001 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002002 if (!reference || !bdev_get_integrity(reference->bdev))
2003 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002004 /*
2005 * All component devices are integrity capable and have matching
2006 * profiles, register the common profile for the md device.
2007 */
2008 if (blk_integrity_register(mddev->gendisk,
2009 bdev_get_integrity(reference->bdev)) != 0) {
2010 printk(KERN_ERR "md: failed to register integrity for %s\n",
2011 mdname(mddev));
2012 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002013 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002014 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2015 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2016 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2017 mdname(mddev));
2018 return -EINVAL;
2019 }
Andre Nollac5e7112009-08-03 10:59:47 +10002020 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002021}
Andre Nollac5e7112009-08-03 10:59:47 +10002022EXPORT_SYMBOL(md_integrity_register);
2023
2024/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002025void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002026{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002027 struct blk_integrity *bi_rdev;
2028 struct blk_integrity *bi_mddev;
2029
2030 if (!mddev->gendisk)
2031 return;
2032
2033 bi_rdev = bdev_get_integrity(rdev->bdev);
2034 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002035
2036 if (!bi_mddev) /* nothing to do */
2037 return;
2038 if (rdev->raid_disk < 0) /* skip spares */
2039 return;
2040 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2041 rdev->bdev->bd_disk) >= 0)
2042 return;
2043 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2044 blk_integrity_unregister(mddev->gendisk);
2045}
2046EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002047
NeilBrownfd01b882011-10-11 16:47:53 +11002048static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002050 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002051 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002052 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002053 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (rdev->mddev) {
2056 MD_BUG();
2057 return -EINVAL;
2058 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002059
2060 /* prevent duplicates */
2061 if (find_rdev(mddev, rdev->bdev->bd_dev))
2062 return -EEXIST;
2063
Andre Nolldd8ac332009-03-31 14:33:13 +11002064 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2065 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2066 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002067 if (mddev->pers) {
2068 /* Cannot change size, so fail
2069 * If mddev->level <= 0, then we don't care
2070 * about aligning sizes (e.g. linear)
2071 */
2072 if (mddev->level > 0)
2073 return -ENOSPC;
2074 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002075 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /* Verify rdev->desc_nr is unique.
2079 * If it is -1, assign a free number, else
2080 * check number is not in use
2081 */
2082 if (rdev->desc_nr < 0) {
2083 int choice = 0;
2084 if (mddev->pers) choice = mddev->raid_disks;
2085 while (find_rdev_nr(mddev, choice))
2086 choice++;
2087 rdev->desc_nr = choice;
2088 } else {
2089 if (find_rdev_nr(mddev, rdev->desc_nr))
2090 return -EBUSY;
2091 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002092 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2093 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2094 mdname(mddev), mddev->max_disks);
2095 return -EBUSY;
2096 }
NeilBrown19133a42005-11-08 21:39:35 -08002097 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002098 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002099 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002102 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002103
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002104 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002105 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002106
Tejun Heo0762b8b2008-08-25 19:56:12 +09002107 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002108 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2109 /* failure here is OK */;
2110 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002111
NeilBrown4b809912008-07-21 17:05:25 +10002112 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002113 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002114
2115 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002116 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002119
2120 fail:
2121 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2122 b, mdname(mddev));
2123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
NeilBrown177a99b2008-02-06 01:39:56 -08002126static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002127{
NeilBrown3cb03002011-10-11 16:45:26 +11002128 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002129 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002130 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002131}
2132
NeilBrown3cb03002011-10-11 16:45:26 +11002133static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 char b[BDEVNAME_SIZE];
2136 if (!rdev->mddev) {
2137 MD_BUG();
2138 return;
2139 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002140 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002141 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2143 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002144 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002145 sysfs_put(rdev->sysfs_state);
2146 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002147 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002148 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002149 * writing to 'remove' to "dev/state". We also need
2150 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002151 */
NeilBrown4b809912008-07-21 17:05:25 +10002152 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002153 INIT_WORK(&rdev->del_work, md_delayed_delete);
2154 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002155 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158/*
2159 * prevent the device from being mounted, repartitioned or
2160 * otherwise reused by a RAID array (or any other kernel
2161 * subsystem), by bd_claiming the device.
2162 */
NeilBrown3cb03002011-10-11 16:45:26 +11002163static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 int err = 0;
2166 struct block_device *bdev;
2167 char b[BDEVNAME_SIZE];
2168
Tejun Heod4d77622010-11-13 11:55:18 +01002169 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002170 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (IS_ERR(bdev)) {
2172 printk(KERN_ERR "md: could not open %s.\n",
2173 __bdevname(dev, b));
2174 return PTR_ERR(bdev);
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 rdev->bdev = bdev;
2177 return err;
2178}
2179
NeilBrown3cb03002011-10-11 16:45:26 +11002180static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct block_device *bdev = rdev->bdev;
2183 rdev->bdev = NULL;
2184 if (!bdev)
2185 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002186 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189void md_autodetect_dev(dev_t dev);
2190
NeilBrown3cb03002011-10-11 16:45:26 +11002191static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 char b[BDEVNAME_SIZE];
2194 printk(KERN_INFO "md: export_rdev(%s)\n",
2195 bdevname(rdev->bdev,b));
2196 if (rdev->mddev)
2197 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002198 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002200 if (test_bit(AutoDetected, &rdev->flags))
2201 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202#endif
2203 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002204 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
NeilBrown3cb03002011-10-11 16:45:26 +11002207static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 unbind_rdev_from_array(rdev);
2210 export_rdev(rdev);
2211}
2212
NeilBrownfd01b882011-10-11 16:47:53 +11002213static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
NeilBrown3cb03002011-10-11 16:45:26 +11002215 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
NeilBrowndafb20f2012-03-19 12:46:39 +11002217 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (!rdev->mddev) {
2219 MD_BUG();
2220 continue;
2221 }
2222 kick_rdev_from_array(rdev);
2223 }
2224 if (!list_empty(&mddev->disks))
2225 MD_BUG();
2226 mddev->raid_disks = 0;
2227 mddev->major_version = 0;
2228}
2229
2230static void print_desc(mdp_disk_t *desc)
2231{
2232 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2233 desc->major,desc->minor,desc->raid_disk,desc->state);
2234}
2235
Cheng Renquancd2ac932009-01-09 08:31:08 +11002236static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 int i;
2239
2240 printk(KERN_INFO
2241 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2242 sb->major_version, sb->minor_version, sb->patch_version,
2243 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2244 sb->ctime);
2245 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2246 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2247 sb->md_minor, sb->layout, sb->chunk_size);
2248 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2249 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2250 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2251 sb->failed_disks, sb->spare_disks,
2252 sb->sb_csum, (unsigned long)sb->events_lo);
2253
2254 printk(KERN_INFO);
2255 for (i = 0; i < MD_SB_DISKS; i++) {
2256 mdp_disk_t *desc;
2257
2258 desc = sb->disks + i;
2259 if (desc->number || desc->major || desc->minor ||
2260 desc->raid_disk || (desc->state && (desc->state != 4))) {
2261 printk(" D %2d: ", i);
2262 print_desc(desc);
2263 }
2264 }
2265 printk(KERN_INFO "md: THIS: ");
2266 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Cheng Renquancd2ac932009-01-09 08:31:08 +11002269static void print_sb_1(struct mdp_superblock_1 *sb)
2270{
2271 __u8 *uuid;
2272
2273 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002274 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002275 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002276 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002277 le32_to_cpu(sb->major_version),
2278 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002279 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002280 sb->set_name,
2281 (unsigned long long)le64_to_cpu(sb->ctime)
2282 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2283
2284 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002285 printk(KERN_INFO
2286 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002287 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002288 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002289 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2290 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002291 le32_to_cpu(sb->level),
2292 (unsigned long long)le64_to_cpu(sb->size),
2293 le32_to_cpu(sb->raid_disks),
2294 le32_to_cpu(sb->layout),
2295 le32_to_cpu(sb->chunksize),
2296 (unsigned long long)le64_to_cpu(sb->data_offset),
2297 (unsigned long long)le64_to_cpu(sb->data_size),
2298 (unsigned long long)le64_to_cpu(sb->super_offset),
2299 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2300 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002301 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002302 sb->devflags,
2303 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2304 (unsigned long long)le64_to_cpu(sb->events),
2305 (unsigned long long)le64_to_cpu(sb->resync_offset),
2306 le32_to_cpu(sb->sb_csum),
2307 le32_to_cpu(sb->max_dev)
2308 );
2309}
2310
NeilBrown3cb03002011-10-11 16:45:26 +11002311static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002314 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2315 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002316 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2317 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002319 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2320 switch (major_version) {
2321 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002322 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002323 break;
2324 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002325 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002326 break;
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 } else
2329 printk(KERN_INFO "md: no rdev superblock!\n");
2330}
2331
Adrian Bunk5e563412006-06-26 00:27:42 -07002332static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002334 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002335 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002336 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 char b[BDEVNAME_SIZE];
2338
2339 printk("\n");
2340 printk("md: **********************************\n");
2341 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2342 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002343 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
NeilBrown32a76272005-06-21 17:17:14 -07002345 if (mddev->bitmap)
2346 bitmap_print_sb(mddev->bitmap);
2347 else
2348 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002349 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 printk("<%s>", bdevname(rdev->bdev,b));
2351 printk("\n");
2352
NeilBrowndafb20f2012-03-19 12:46:39 +11002353 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002354 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 printk("md: **********************************\n");
2357 printk("\n");
2358}
2359
2360
NeilBrownfd01b882011-10-11 16:47:53 +11002361static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
NeilBrown42543762006-06-26 00:27:57 -07002363 /* Update each superblock (in-memory image), but
2364 * if we are allowed to, skip spares which already
2365 * have the right event counter, or have one earlier
2366 * (which would mean they aren't being marked as dirty
2367 * with the rest of the array)
2368 */
NeilBrown3cb03002011-10-11 16:45:26 +11002369 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002370 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002371 if (rdev->sb_events == mddev->events ||
2372 (nospares &&
2373 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002374 rdev->sb_events+1 == mddev->events)) {
2375 /* Don't update this superblock */
2376 rdev->sb_loaded = 2;
2377 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002378 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002379 rdev->sb_loaded = 1;
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382}
2383
NeilBrownfd01b882011-10-11 16:47:53 +11002384static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
NeilBrown3cb03002011-10-11 16:45:26 +11002386 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002387 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002388 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002389 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
NeilBrownd87f0642013-04-24 11:42:40 +10002391 if (mddev->ro) {
2392 if (force_change)
2393 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2394 return;
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002397 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002398 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002399 if (rdev->raid_disk >= 0 &&
2400 mddev->delta_disks >= 0 &&
2401 !test_bit(In_sync, &rdev->flags) &&
2402 mddev->curr_resync_completed > rdev->recovery_offset)
2403 rdev->recovery_offset = mddev->curr_resync_completed;
2404
2405 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002406 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002407 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002408 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002409 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002410 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002411 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002412 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002413 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002414 md_ack_all_badblocks(&rdev->badblocks);
2415 md_error(mddev, rdev);
2416 }
2417 clear_bit(Blocked, &rdev->flags);
2418 clear_bit(BlockedBadBlocks, &rdev->flags);
2419 wake_up(&rdev->blocked_wait);
2420 }
2421 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002422 wake_up(&mddev->sb_wait);
2423 return;
2424 }
2425
NeilBrowna9701a32005-11-08 21:39:34 -08002426 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002427
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002428 mddev->utime = get_seconds();
2429
NeilBrown850b2b42006-10-03 01:15:46 -07002430 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2431 force_change = 1;
2432 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2433 /* just a clean<-> dirty transition, possibly leave spares alone,
2434 * though if events isn't the right even/odd, we will have to do
2435 * spares after all
2436 */
2437 nospares = 1;
2438 if (force_change)
2439 nospares = 0;
2440 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002441 /* If the array is degraded, then skipping spares is both
2442 * dangerous and fairly pointless.
2443 * Dangerous because a device that was removed from the array
2444 * might have a event_count that still looks up-to-date,
2445 * so it can be re-added without a resync.
2446 * Pointless because if there are any spares to skip,
2447 * then a recovery will happen and soon that array won't
2448 * be degraded any more and the spare can go back to sleep then.
2449 */
NeilBrown850b2b42006-10-03 01:15:46 -07002450 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002451
NeilBrown06d91a52005-06-21 17:17:12 -07002452 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002453
2454 /* If this is just a dirty<->clean transition, and the array is clean
2455 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002456 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002457 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002458 && mddev->can_decrease_events
2459 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002460 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002461 mddev->can_decrease_events = 0;
2462 } else {
NeilBrown42543762006-06-26 00:27:57 -07002463 /* otherwise we have to go forward and ... */
2464 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002465 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 if (!mddev->events) {
2469 /*
2470 * oops, this 64-bit counter should never wrap.
2471 * Either we are in around ~1 trillion A.C., assuming
2472 * 1 reboot per second, or we have a bug:
2473 */
2474 MD_BUG();
2475 mddev->events --;
2476 }
NeilBrown2699b672011-07-28 11:31:47 +10002477
NeilBrowndafb20f2012-03-19 12:46:39 +11002478 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002479 if (rdev->badblocks.changed)
2480 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002481 if (test_bit(Faulty, &rdev->flags))
2482 set_bit(FaultRecorded, &rdev->flags);
2483 }
NeilBrown2699b672011-07-28 11:31:47 +10002484
NeilBrowne6910632008-02-06 01:39:51 -08002485 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002486 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
NeilBrown36a4e1f2011-10-07 14:23:17 +11002488 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2489 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
NeilBrown4ad13662007-07-17 04:06:13 -07002491 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002492 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002494
NeilBrown42543762006-06-26 00:27:57 -07002495 if (rdev->sb_loaded != 1)
2496 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
NeilBrownf4667222013-12-09 12:04:56 +11002498 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002499 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002500 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002501 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002502 pr_debug("md: (write) %s's sb offset: %llu\n",
2503 bdevname(rdev->bdev, b),
2504 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002505 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002506 if (rdev->badblocks.size) {
2507 md_super_write(mddev, rdev,
2508 rdev->badblocks.sector,
2509 rdev->badblocks.size << 9,
2510 rdev->bb_page);
2511 rdev->badblocks.size = 0;
2512 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002513
NeilBrownf4667222013-12-09 12:04:56 +11002514 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002515 pr_debug("md: %s (skipping faulty)\n",
2516 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002517
NeilBrown7bfa19f2005-06-21 17:17:28 -07002518 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* only need to write one superblock... */
2520 break;
2521 }
NeilBrowna9701a32005-11-08 21:39:34 -08002522 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002523 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002524
NeilBrowna9701a32005-11-08 21:39:34 -08002525 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002526 if (mddev->in_sync != sync_req ||
2527 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002528 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002529 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002530 goto repeat;
2531 }
NeilBrown850b2b42006-10-03 01:15:46 -07002532 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002533 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002534 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002535 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2536 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002537
NeilBrowndafb20f2012-03-19 12:46:39 +11002538 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002539 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2540 clear_bit(Blocked, &rdev->flags);
2541
2542 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002543 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002544 clear_bit(BlockedBadBlocks, &rdev->flags);
2545 wake_up(&rdev->blocked_wait);
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
Andre Noll7f6ce762008-03-23 18:34:54 +01002549/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002550 * We want to accept with case. For this we use cmd_match.
2551 */
2552static int cmd_match(const char *cmd, const char *str)
2553{
2554 /* See if cmd, written into a sysfs file, matches
2555 * str. They must either be the same, or cmd can
2556 * have a trailing newline
2557 */
2558 while (*cmd && *str && *cmd == *str) {
2559 cmd++;
2560 str++;
2561 }
2562 if (*cmd == '\n')
2563 cmd++;
2564 if (*str || *cmd)
2565 return 0;
2566 return 1;
2567}
2568
NeilBrown86e6ffd2005-11-08 21:39:24 -08002569struct rdev_sysfs_entry {
2570 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002571 ssize_t (*show)(struct md_rdev *, char *);
2572 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002573};
2574
2575static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002576state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577{
2578 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002579 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002580
NeilBrownde393cd2011-07-28 11:31:48 +10002581 if (test_bit(Faulty, &rdev->flags) ||
2582 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002583 len+= sprintf(page+len, "%sfaulty",sep);
2584 sep = ",";
2585 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002586 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587 len += sprintf(page+len, "%sin_sync",sep);
2588 sep = ",";
2589 }
NeilBrownf6556752006-06-26 00:28:01 -07002590 if (test_bit(WriteMostly, &rdev->flags)) {
2591 len += sprintf(page+len, "%swrite_mostly",sep);
2592 sep = ",";
2593 }
NeilBrownde393cd2011-07-28 11:31:48 +10002594 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002595 (rdev->badblocks.unacked_exist
2596 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002597 len += sprintf(page+len, "%sblocked", sep);
2598 sep = ",";
2599 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002600 if (!test_bit(Faulty, &rdev->flags) &&
2601 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002602 len += sprintf(page+len, "%sspare", sep);
2603 sep = ",";
2604 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002605 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2606 len += sprintf(page+len, "%swrite_error", sep);
2607 sep = ",";
2608 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002609 if (test_bit(WantReplacement, &rdev->flags)) {
2610 len += sprintf(page+len, "%swant_replacement", sep);
2611 sep = ",";
2612 }
2613 if (test_bit(Replacement, &rdev->flags)) {
2614 len += sprintf(page+len, "%sreplacement", sep);
2615 sep = ",";
2616 }
2617
NeilBrown86e6ffd2005-11-08 21:39:24 -08002618 return len+sprintf(page+len, "\n");
2619}
2620
NeilBrown45dc2de2006-06-26 00:27:58 -07002621static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002622state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002623{
2624 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002625 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002626 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002627 * writemostly - sets write_mostly
2628 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002629 * blocked - sets the Blocked flags
2630 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002631 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002632 * -insync - clear Insync for a device with a slot assigned,
2633 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002634 * write_error - sets WriteErrorSeen
2635 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002636 */
2637 int err = -EINVAL;
2638 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2639 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002640 if (test_bit(Faulty, &rdev->flags))
2641 err = 0;
2642 else
2643 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002644 } else if (cmd_match(buf, "remove")) {
2645 if (rdev->raid_disk >= 0)
2646 err = -EBUSY;
2647 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002648 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002649 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002650 if (mddev->pers)
2651 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002652 md_new_event(mddev);
2653 err = 0;
2654 }
NeilBrownf6556752006-06-26 00:28:01 -07002655 } else if (cmd_match(buf, "writemostly")) {
2656 set_bit(WriteMostly, &rdev->flags);
2657 err = 0;
2658 } else if (cmd_match(buf, "-writemostly")) {
2659 clear_bit(WriteMostly, &rdev->flags);
2660 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002661 } else if (cmd_match(buf, "blocked")) {
2662 set_bit(Blocked, &rdev->flags);
2663 err = 0;
2664 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002665 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002666 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002667 /* metadata handler doesn't understand badblocks,
2668 * so we need to fail the device
2669 */
2670 md_error(rdev->mddev, rdev);
2671 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002672 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002673 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002674 wake_up(&rdev->blocked_wait);
2675 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2676 md_wakeup_thread(rdev->mddev->thread);
2677
2678 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002679 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2680 set_bit(In_sync, &rdev->flags);
2681 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002682 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2683 clear_bit(In_sync, &rdev->flags);
2684 rdev->saved_raid_disk = rdev->raid_disk;
2685 rdev->raid_disk = -1;
2686 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002687 } else if (cmd_match(buf, "write_error")) {
2688 set_bit(WriteErrorSeen, &rdev->flags);
2689 err = 0;
2690 } else if (cmd_match(buf, "-write_error")) {
2691 clear_bit(WriteErrorSeen, &rdev->flags);
2692 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002693 } else if (cmd_match(buf, "want_replacement")) {
2694 /* Any non-spare device that is not a replacement can
2695 * become want_replacement at any time, but we then need to
2696 * check if recovery is needed.
2697 */
2698 if (rdev->raid_disk >= 0 &&
2699 !test_bit(Replacement, &rdev->flags))
2700 set_bit(WantReplacement, &rdev->flags);
2701 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2702 md_wakeup_thread(rdev->mddev->thread);
2703 err = 0;
2704 } else if (cmd_match(buf, "-want_replacement")) {
2705 /* Clearing 'want_replacement' is always allowed.
2706 * Once replacements starts it is too late though.
2707 */
2708 err = 0;
2709 clear_bit(WantReplacement, &rdev->flags);
2710 } else if (cmd_match(buf, "replacement")) {
2711 /* Can only set a device as a replacement when array has not
2712 * yet been started. Once running, replacement is automatic
2713 * from spares, or by assigning 'slot'.
2714 */
2715 if (rdev->mddev->pers)
2716 err = -EBUSY;
2717 else {
2718 set_bit(Replacement, &rdev->flags);
2719 err = 0;
2720 }
2721 } else if (cmd_match(buf, "-replacement")) {
2722 /* Similarly, can only clear Replacement before start */
2723 if (rdev->mddev->pers)
2724 err = -EBUSY;
2725 else {
2726 clear_bit(Replacement, &rdev->flags);
2727 err = 0;
2728 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002729 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002730 if (!err)
2731 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002732 return err ? err : len;
2733}
NeilBrown80ca3a42006-07-10 04:44:18 -07002734static struct rdev_sysfs_entry rdev_state =
2735__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002736
2737static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002738errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002739{
2740 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2741}
2742
2743static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002744errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002745{
2746 char *e;
2747 unsigned long n = simple_strtoul(buf, &e, 10);
2748 if (*buf && (*e == 0 || *e == '\n')) {
2749 atomic_set(&rdev->corrected_errors, n);
2750 return len;
2751 }
2752 return -EINVAL;
2753}
2754static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002755__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002756
NeilBrown014236d2006-01-06 00:20:55 -08002757static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002758slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002759{
2760 if (rdev->raid_disk < 0)
2761 return sprintf(page, "none\n");
2762 else
2763 return sprintf(page, "%d\n", rdev->raid_disk);
2764}
2765
2766static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002767slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002768{
2769 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002770 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002771 int slot = simple_strtoul(buf, &e, 10);
2772 if (strncmp(buf, "none", 4)==0)
2773 slot = -1;
2774 else if (e==buf || (*e && *e!= '\n'))
2775 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002777 /* Setting 'slot' on an active array requires also
2778 * updating the 'rd%d' link, and communicating
2779 * with the personality with ->hot_*_disk.
2780 * For now we only support removing
2781 * failed/spare devices. This normally happens automatically,
2782 * but not when the metadata is externally managed.
2783 */
NeilBrownc303da62008-02-06 01:39:51 -08002784 if (rdev->raid_disk == -1)
2785 return -EEXIST;
2786 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002787 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002788 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002789 clear_bit(Blocked, &rdev->flags);
2790 remove_and_add_spares(rdev->mddev, rdev);
2791 if (rdev->raid_disk >= 0)
2792 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002793 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2794 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002795 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002796 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002797 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002798 */
2799
2800 if (rdev->raid_disk != -1)
2801 return -EBUSY;
2802
NeilBrownc6751b22011-02-02 11:57:13 +11002803 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2804 return -EBUSY;
2805
Neil Brown6c2fce22008-06-28 08:31:31 +10002806 if (rdev->mddev->pers->hot_add_disk == NULL)
2807 return -EINVAL;
2808
NeilBrownba1b41b2011-01-14 09:14:34 +11002809 if (slot >= rdev->mddev->raid_disks &&
2810 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2811 return -ENOSPC;
2812
Neil Brown6c2fce22008-06-28 08:31:31 +10002813 rdev->raid_disk = slot;
2814 if (test_bit(In_sync, &rdev->flags))
2815 rdev->saved_raid_disk = slot;
2816 else
2817 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002818 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002819 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002820 err = rdev->mddev->pers->
2821 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002822 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002823 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002824 return err;
Neil Brown52664732008-06-28 08:31:44 +10002825 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002826 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002827 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002828 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002829 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002830 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002831 if (slot >= rdev->mddev->raid_disks &&
2832 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002833 return -ENOSPC;
2834 rdev->raid_disk = slot;
2835 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002836 clear_bit(Faulty, &rdev->flags);
2837 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002838 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002839 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002840 }
NeilBrown014236d2006-01-06 00:20:55 -08002841 return len;
2842}
2843
2844
2845static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002846__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002847
NeilBrown93c8cad2006-01-06 00:20:56 -08002848static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002849offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002850{
Andrew Morton6961ece2006-01-06 00:20:59 -08002851 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002852}
2853
2854static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002855offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002856{
NeilBrownc6563a82012-05-21 09:27:00 +10002857 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002858 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002859 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002860 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002861 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002862 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002863 /* Must set offset before size, so overlap checks
2864 * can be sane */
2865 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002866 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002867 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002868 return len;
2869}
2870
2871static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002872__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002873
NeilBrownc6563a82012-05-21 09:27:00 +10002874static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2875{
2876 return sprintf(page, "%llu\n",
2877 (unsigned long long)rdev->new_data_offset);
2878}
2879
2880static ssize_t new_offset_store(struct md_rdev *rdev,
2881 const char *buf, size_t len)
2882{
2883 unsigned long long new_offset;
2884 struct mddev *mddev = rdev->mddev;
2885
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002886 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002887 return -EINVAL;
2888
2889 if (mddev->sync_thread)
2890 return -EBUSY;
2891 if (new_offset == rdev->data_offset)
2892 /* reset is always permitted */
2893 ;
2894 else if (new_offset > rdev->data_offset) {
2895 /* must not push array size beyond rdev_sectors */
2896 if (new_offset - rdev->data_offset
2897 + mddev->dev_sectors > rdev->sectors)
2898 return -E2BIG;
2899 }
2900 /* Metadata worries about other space details. */
2901
2902 /* decreasing the offset is inconsistent with a backwards
2903 * reshape.
2904 */
2905 if (new_offset < rdev->data_offset &&
2906 mddev->reshape_backwards)
2907 return -EINVAL;
2908 /* Increasing offset is inconsistent with forwards
2909 * reshape. reshape_direction should be set to
2910 * 'backwards' first.
2911 */
2912 if (new_offset > rdev->data_offset &&
2913 !mddev->reshape_backwards)
2914 return -EINVAL;
2915
2916 if (mddev->pers && mddev->persistent &&
2917 !super_types[mddev->major_version]
2918 .allow_new_offset(rdev, new_offset))
2919 return -E2BIG;
2920 rdev->new_data_offset = new_offset;
2921 if (new_offset > rdev->data_offset)
2922 mddev->reshape_backwards = 1;
2923 else if (new_offset < rdev->data_offset)
2924 mddev->reshape_backwards = 0;
2925
2926 return len;
2927}
2928static struct rdev_sysfs_entry rdev_new_offset =
2929__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2930
NeilBrown83303b62006-01-06 00:21:06 -08002931static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002932rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002933{
Andre Nolldd8ac332009-03-31 14:33:13 +11002934 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002935}
2936
NeilBrownc5d79ad2008-02-06 01:39:54 -08002937static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2938{
2939 /* check if two start/length pairs overlap */
2940 if (s1+l1 <= s2)
2941 return 0;
2942 if (s2+l2 <= s1)
2943 return 0;
2944 return 1;
2945}
2946
Dan Williamsb522adc2009-03-31 15:00:31 +11002947static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2948{
2949 unsigned long long blocks;
2950 sector_t new;
2951
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002952 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002953 return -EINVAL;
2954
2955 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2956 return -EINVAL; /* sector conversion overflow */
2957
2958 new = blocks * 2;
2959 if (new != blocks * 2)
2960 return -EINVAL; /* unsigned long long to sector_t overflow */
2961
2962 *sectors = new;
2963 return 0;
2964}
2965
NeilBrown83303b62006-01-06 00:21:06 -08002966static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002967rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002968{
NeilBrownfd01b882011-10-11 16:47:53 +11002969 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002970 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002971 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002972
Dan Williamsb522adc2009-03-31 15:00:31 +11002973 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002974 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002975 if (rdev->data_offset != rdev->new_data_offset)
2976 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002977 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002978 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002979 sectors = super_types[my_mddev->major_version].
2980 rdev_size_change(rdev, sectors);
2981 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002982 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002983 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002984 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002985 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002986 if (!my_mddev->pers->resize)
2987 /* Cannot change size for RAID0 or Linear etc */
2988 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002989 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002990 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002991 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002992
Andre Nolldd8ac332009-03-31 14:33:13 +11002993 rdev->sectors = sectors;
2994 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002995 /* need to check that all other rdevs with the same ->bdev
2996 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002998 * we have to change it back, we will have the lock again.
2999 */
NeilBrownfd01b882011-10-11 16:47:53 +11003000 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003001 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003002 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003
NeilBrown27c529b2008-03-04 14:29:33 -08003004 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003005 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003006 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003007
NeilBrown29f097c2013-11-14 17:54:51 +11003008 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003009 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003010 if (rdev->bdev == rdev2->bdev &&
3011 rdev != rdev2 &&
3012 overlaps(rdev->data_offset, rdev->sectors,
3013 rdev2->data_offset,
3014 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003015 overlap = 1;
3016 break;
3017 }
3018 mddev_unlock(mddev);
3019 if (overlap) {
3020 mddev_put(mddev);
3021 break;
3022 }
3023 }
NeilBrown29f097c2013-11-14 17:54:51 +11003024 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003025 if (overlap) {
3026 /* Someone else could have slipped in a size
3027 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003028 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003029 * safe, and trust userspace not to race with
3030 * itself
3031 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003032 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003033 return -EBUSY;
3034 }
3035 }
NeilBrown83303b62006-01-06 00:21:06 -08003036 return len;
3037}
3038
3039static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003040__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003041
Dan Williams06e3c812009-12-12 21:17:12 -07003042
NeilBrown3cb03002011-10-11 16:45:26 +11003043static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003044{
3045 unsigned long long recovery_start = rdev->recovery_offset;
3046
3047 if (test_bit(In_sync, &rdev->flags) ||
3048 recovery_start == MaxSector)
3049 return sprintf(page, "none\n");
3050
3051 return sprintf(page, "%llu\n", recovery_start);
3052}
3053
NeilBrown3cb03002011-10-11 16:45:26 +11003054static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003055{
3056 unsigned long long recovery_start;
3057
3058 if (cmd_match(buf, "none"))
3059 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003060 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003061 return -EINVAL;
3062
3063 if (rdev->mddev->pers &&
3064 rdev->raid_disk >= 0)
3065 return -EBUSY;
3066
3067 rdev->recovery_offset = recovery_start;
3068 if (recovery_start == MaxSector)
3069 set_bit(In_sync, &rdev->flags);
3070 else
3071 clear_bit(In_sync, &rdev->flags);
3072 return len;
3073}
3074
3075static struct rdev_sysfs_entry rdev_recovery_start =
3076__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3077
NeilBrown16c791a2011-07-28 11:31:47 +10003078
3079static ssize_t
3080badblocks_show(struct badblocks *bb, char *page, int unack);
3081static ssize_t
3082badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3083
NeilBrown3cb03002011-10-11 16:45:26 +11003084static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003085{
3086 return badblocks_show(&rdev->badblocks, page, 0);
3087}
NeilBrown3cb03002011-10-11 16:45:26 +11003088static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003089{
NeilBrownde393cd2011-07-28 11:31:48 +10003090 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3091 /* Maybe that ack was all we needed */
3092 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3093 wake_up(&rdev->blocked_wait);
3094 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003095}
3096static struct rdev_sysfs_entry rdev_bad_blocks =
3097__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3098
3099
NeilBrown3cb03002011-10-11 16:45:26 +11003100static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003101{
3102 return badblocks_show(&rdev->badblocks, page, 1);
3103}
NeilBrown3cb03002011-10-11 16:45:26 +11003104static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003105{
3106 return badblocks_store(&rdev->badblocks, page, len, 1);
3107}
3108static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3109__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3110
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111static struct attribute *rdev_default_attrs[] = {
3112 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003113 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003114 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003115 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003116 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003117 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003118 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003119 &rdev_bad_blocks.attr,
3120 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003121 NULL,
3122};
3123static ssize_t
3124rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3125{
3126 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003127 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003128 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003129 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003130
3131 if (!entry->show)
3132 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003133
3134 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3135 if (!rv) {
3136 if (rdev->mddev == NULL)
3137 rv = -EBUSY;
3138 else
3139 rv = entry->show(rdev, page);
3140 mddev_unlock(mddev);
3141 }
3142 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003143}
3144
3145static ssize_t
3146rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3147 const char *page, size_t length)
3148{
3149 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003150 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003151 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003152 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003153
3154 if (!entry->store)
3155 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003156 if (!capable(CAP_SYS_ADMIN))
3157 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003158 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003159 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003160 if (rdev->mddev == NULL)
3161 rv = -EBUSY;
3162 else
3163 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003164 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003165 }
3166 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003167}
3168
3169static void rdev_free(struct kobject *ko)
3170{
NeilBrown3cb03002011-10-11 16:45:26 +11003171 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003172 kfree(rdev);
3173}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003174static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003175 .show = rdev_attr_show,
3176 .store = rdev_attr_store,
3177};
3178static struct kobj_type rdev_ktype = {
3179 .release = rdev_free,
3180 .sysfs_ops = &rdev_sysfs_ops,
3181 .default_attrs = rdev_default_attrs,
3182};
3183
NeilBrown3cb03002011-10-11 16:45:26 +11003184int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003185{
3186 rdev->desc_nr = -1;
3187 rdev->saved_raid_disk = -1;
3188 rdev->raid_disk = -1;
3189 rdev->flags = 0;
3190 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003191 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003192 rdev->sb_events = 0;
3193 rdev->last_read_error.tv_sec = 0;
3194 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003195 rdev->sb_loaded = 0;
3196 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003197 atomic_set(&rdev->nr_pending, 0);
3198 atomic_set(&rdev->read_errors, 0);
3199 atomic_set(&rdev->corrected_errors, 0);
3200
3201 INIT_LIST_HEAD(&rdev->same_set);
3202 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003203
3204 /* Add space to store bad block list.
3205 * This reserves the space even on arrays where it cannot
3206 * be used - I wonder if that matters
3207 */
3208 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003209 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003210 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3211 seqlock_init(&rdev->badblocks.lock);
3212 if (rdev->badblocks.page == NULL)
3213 return -ENOMEM;
3214
3215 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003216}
3217EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218/*
3219 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3220 *
3221 * mark the device faulty if:
3222 *
3223 * - the device is nonexistent (zero size)
3224 * - the device has no valid superblock
3225 *
3226 * a faulty rdev _never_ has rdev->sb set.
3227 */
NeilBrown3cb03002011-10-11 16:45:26 +11003228static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 char b[BDEVNAME_SIZE];
3231 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003232 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 sector_t size;
3234
NeilBrown9ffae0c2006-01-06 00:20:32 -08003235 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!rdev) {
3237 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3238 return ERR_PTR(-ENOMEM);
3239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
NeilBrown2230dfe2011-07-28 11:31:46 +10003241 err = md_rdev_init(rdev);
3242 if (err)
3243 goto abort_free;
3244 err = alloc_disk_sb(rdev);
3245 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto abort_free;
3247
NeilBrownc5d79ad2008-02-06 01:39:54 -08003248 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (err)
3250 goto abort_free;
3251
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003252 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003253
Mike Snitzer77304d22010-11-08 14:39:12 +01003254 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (!size) {
3256 printk(KERN_WARNING
3257 "md: %s has zero or unknown size, marking faulty!\n",
3258 bdevname(rdev->bdev,b));
3259 err = -EINVAL;
3260 goto abort_free;
3261 }
3262
3263 if (super_format >= 0) {
3264 err = super_types[super_format].
3265 load_super(rdev, NULL, super_minor);
3266 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003267 printk(KERN_WARNING
3268 "md: %s does not have a valid v%d.%d "
3269 "superblock, not importing!\n",
3270 bdevname(rdev->bdev,b),
3271 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 goto abort_free;
3273 }
3274 if (err < 0) {
3275 printk(KERN_WARNING
3276 "md: could not read %s's sb, not importing!\n",
3277 bdevname(rdev->bdev,b));
3278 goto abort_free;
3279 }
3280 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return rdev;
3283
3284abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003285 if (rdev->bdev)
3286 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003287 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 kfree(rdev);
3289 return ERR_PTR(err);
3290}
3291
3292/*
3293 * Check a full RAID array for plausibility
3294 */
3295
3296
NeilBrownfd01b882011-10-11 16:47:53 +11003297static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003300 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 char b[BDEVNAME_SIZE];
3302
3303 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003304 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 switch (super_types[mddev->major_version].
3306 load_super(rdev, freshest, mddev->minor_version)) {
3307 case 1:
3308 freshest = rdev;
3309 break;
3310 case 0:
3311 break;
3312 default:
3313 printk( KERN_ERR \
3314 "md: fatal superblock inconsistency in %s"
3315 " -- removing from array\n",
3316 bdevname(rdev->bdev,b));
3317 kick_rdev_from_array(rdev);
3318 }
3319
3320
3321 super_types[mddev->major_version].
3322 validate_super(mddev, freshest);
3323
3324 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003325 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003326 if (mddev->max_disks &&
3327 (rdev->desc_nr >= mddev->max_disks ||
3328 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003329 printk(KERN_WARNING
3330 "md: %s: %s: only %d devices permitted\n",
3331 mdname(mddev), bdevname(rdev->bdev, b),
3332 mddev->max_disks);
3333 kick_rdev_from_array(rdev);
3334 continue;
3335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (rdev != freshest)
3337 if (super_types[mddev->major_version].
3338 validate_super(mddev, rdev)) {
3339 printk(KERN_WARNING "md: kicking non-fresh %s"
3340 " from array!\n",
3341 bdevname(rdev->bdev,b));
3342 kick_rdev_from_array(rdev);
3343 continue;
3344 }
3345 if (mddev->level == LEVEL_MULTIPATH) {
3346 rdev->desc_nr = i++;
3347 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003348 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003349 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003350 rdev->raid_disk = -1;
3351 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 }
3353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354}
3355
NeilBrown72e02072009-12-14 12:49:55 +11003356/* Read a fixed-point number.
3357 * Numbers in sysfs attributes should be in "standard" units where
3358 * possible, so time should be in seconds.
3359 * However we internally use a a much smaller unit such as
3360 * milliseconds or jiffies.
3361 * This function takes a decimal number with a possible fractional
3362 * component, and produces an integer which is the result of
3363 * multiplying that number by 10^'scale'.
3364 * all without any floating-point arithmetic.
3365 */
3366int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3367{
3368 unsigned long result = 0;
3369 long decimals = -1;
3370 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3371 if (*cp == '.')
3372 decimals = 0;
3373 else if (decimals < scale) {
3374 unsigned int value;
3375 value = *cp - '0';
3376 result = result * 10 + value;
3377 if (decimals >= 0)
3378 decimals++;
3379 }
3380 cp++;
3381 }
3382 if (*cp == '\n')
3383 cp++;
3384 if (*cp)
3385 return -EINVAL;
3386 if (decimals < 0)
3387 decimals = 0;
3388 while (decimals < scale) {
3389 result *= 10;
3390 decimals ++;
3391 }
3392 *res = result;
3393 return 0;
3394}
3395
3396
NeilBrown19052c02008-08-05 15:54:13 +10003397static void md_safemode_timeout(unsigned long data);
3398
NeilBrowneae17012005-11-08 21:39:23 -08003399static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003400safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003401{
3402 int msec = (mddev->safemode_delay*1000)/HZ;
3403 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3404}
3405static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003406safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003407{
NeilBrown16f17b32006-06-26 00:27:37 -07003408 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003409
NeilBrown72e02072009-12-14 12:49:55 +11003410 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003411 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003412 if (msec == 0)
3413 mddev->safemode_delay = 0;
3414 else {
NeilBrown19052c02008-08-05 15:54:13 +10003415 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003416 mddev->safemode_delay = (msec*HZ)/1000;
3417 if (mddev->safemode_delay == 0)
3418 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003419 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003420 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003421 }
3422 return len;
3423}
3424static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003425__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003426
3427static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003428level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003429{
NeilBrown84fc4b52011-10-11 16:49:58 +11003430 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003431 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003432 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003433 else if (mddev->clevel[0])
3434 return sprintf(page, "%s\n", mddev->clevel);
3435 else if (mddev->level != LEVEL_NONE)
3436 return sprintf(page, "%d\n", mddev->level);
3437 else
3438 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003439}
3440
NeilBrownd9d166c2006-01-06 00:20:51 -08003441static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003442level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003443{
Dan Williamsf2859af2010-05-02 10:04:16 -07003444 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003445 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003446 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003447 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003448 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003449 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003450
3451 if (mddev->pers == NULL) {
3452 if (len == 0)
3453 return 0;
3454 if (len >= sizeof(mddev->clevel))
3455 return -ENOSPC;
3456 strncpy(mddev->clevel, buf, len);
3457 if (mddev->clevel[len-1] == '\n')
3458 len--;
3459 mddev->clevel[len] = 0;
3460 mddev->level = LEVEL_NONE;
3461 return rv;
3462 }
3463
3464 /* request to change the personality. Need to ensure:
3465 * - array is not engaged in resync/recovery/reshape
3466 * - old personality can be suspended
3467 * - new personality will access other array.
3468 */
3469
NeilBrownbb4f1e92010-08-08 21:18:03 +10003470 if (mddev->sync_thread ||
3471 mddev->reshape_position != MaxSector ||
3472 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003473 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003474
3475 if (!mddev->pers->quiesce) {
3476 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3477 mdname(mddev), mddev->pers->name);
3478 return -EINVAL;
3479 }
3480
3481 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003482 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003483 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003484 strncpy(clevel, buf, len);
3485 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003486 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003487 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003488 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003489 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003490
Dan Williamsf2859af2010-05-02 10:04:16 -07003491 if (request_module("md-%s", clevel) != 0)
3492 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003493 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003494 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003495 if (!pers || !try_module_get(pers->owner)) {
3496 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003497 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003498 return -EINVAL;
3499 }
3500 spin_unlock(&pers_lock);
3501
3502 if (pers == mddev->pers) {
3503 /* Nothing to do! */
3504 module_put(pers->owner);
3505 return rv;
3506 }
3507 if (!pers->takeover) {
3508 module_put(pers->owner);
3509 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003510 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003511 return -EINVAL;
3512 }
3513
NeilBrowndafb20f2012-03-19 12:46:39 +11003514 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003515 rdev->new_raid_disk = rdev->raid_disk;
3516
NeilBrown245f46c2009-03-31 14:39:39 +11003517 /* ->takeover must set new_* and/or delta_disks
3518 * if it succeeds, and may set them when it fails.
3519 */
3520 priv = pers->takeover(mddev);
3521 if (IS_ERR(priv)) {
3522 mddev->new_level = mddev->level;
3523 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003524 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003525 mddev->raid_disks -= mddev->delta_disks;
3526 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003527 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003528 module_put(pers->owner);
3529 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003530 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003531 return PTR_ERR(priv);
3532 }
3533
3534 /* Looks like we have a winner */
3535 mddev_suspend(mddev);
3536 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003537
3538 if (mddev->pers->sync_request == NULL &&
3539 pers->sync_request != NULL) {
3540 /* need to add the md_redundancy_group */
3541 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3542 printk(KERN_WARNING
3543 "md: cannot register extra attributes for %s\n",
3544 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003545 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003546 }
3547 if (mddev->pers->sync_request != NULL &&
3548 pers->sync_request == NULL) {
3549 /* need to remove the md_redundancy_group */
3550 if (mddev->to_remove == NULL)
3551 mddev->to_remove = &md_redundancy_group;
3552 }
3553
Trela Maciej54071b32010-03-08 16:02:42 +11003554 if (mddev->pers->sync_request == NULL &&
3555 mddev->external) {
3556 /* We are converting from a no-redundancy array
3557 * to a redundancy array and metadata is managed
3558 * externally so we need to be sure that writes
3559 * won't block due to a need to transition
3560 * clean->dirty
3561 * until external management is started.
3562 */
3563 mddev->in_sync = 0;
3564 mddev->safemode_delay = 0;
3565 mddev->safemode = 0;
3566 }
3567
NeilBrowndafb20f2012-03-19 12:46:39 +11003568 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003569 if (rdev->raid_disk < 0)
3570 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003571 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003572 rdev->new_raid_disk = -1;
3573 if (rdev->new_raid_disk == rdev->raid_disk)
3574 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003575 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003576 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003577 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003578 if (rdev->raid_disk < 0)
3579 continue;
3580 if (rdev->new_raid_disk == rdev->raid_disk)
3581 continue;
3582 rdev->raid_disk = rdev->new_raid_disk;
3583 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003584 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003585 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003586 if (sysfs_link_rdev(mddev, rdev))
3587 printk(KERN_WARNING "md: cannot register rd%d"
3588 " for %s after level change\n",
3589 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003590 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003591 }
3592
3593 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003594 mddev->pers = pers;
3595 mddev->private = priv;
3596 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3597 mddev->level = mddev->new_level;
3598 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003599 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003600 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003601 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003602 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003603 if (mddev->pers->sync_request == NULL) {
3604 /* this is now an array without redundancy, so
3605 * it must always be in_sync
3606 */
3607 mddev->in_sync = 1;
3608 del_timer_sync(&mddev->safemode_timer);
3609 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003610 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003611 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003612 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003613 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003614 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003615 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003616 return rv;
3617}
3618
3619static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003620__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003621
NeilBrownd4dbd022006-06-26 00:27:59 -07003622
3623static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003624layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003625{
3626 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003627 if (mddev->reshape_position != MaxSector &&
3628 mddev->layout != mddev->new_layout)
3629 return sprintf(page, "%d (%d)\n",
3630 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003631 return sprintf(page, "%d\n", mddev->layout);
3632}
3633
3634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003635layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003636{
3637 char *e;
3638 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003639
3640 if (!*buf || (*e && *e != '\n'))
3641 return -EINVAL;
3642
NeilBrownb3546032009-03-31 14:56:41 +11003643 if (mddev->pers) {
3644 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003645 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003646 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003647 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003648 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003649 if (err) {
3650 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003651 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003652 }
NeilBrownb3546032009-03-31 14:56:41 +11003653 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003654 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003655 if (mddev->reshape_position == MaxSector)
3656 mddev->layout = n;
3657 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003658 return len;
3659}
3660static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003661__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003662
3663
NeilBrowneae17012005-11-08 21:39:23 -08003664static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003665raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003666{
NeilBrownbb636542005-11-08 21:39:45 -08003667 if (mddev->raid_disks == 0)
3668 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003669 if (mddev->reshape_position != MaxSector &&
3670 mddev->delta_disks != 0)
3671 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3672 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003673 return sprintf(page, "%d\n", mddev->raid_disks);
3674}
3675
NeilBrownfd01b882011-10-11 16:47:53 +11003676static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003677
3678static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003679raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003680{
NeilBrownda943b992006-01-06 00:20:54 -08003681 char *e;
3682 int rv = 0;
3683 unsigned long n = simple_strtoul(buf, &e, 10);
3684
3685 if (!*buf || (*e && *e != '\n'))
3686 return -EINVAL;
3687
3688 if (mddev->pers)
3689 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003690 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003691 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003692 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003693
3694 rdev_for_each(rdev, mddev) {
3695 if (olddisks < n &&
3696 rdev->data_offset < rdev->new_data_offset)
3697 return -EINVAL;
3698 if (olddisks > n &&
3699 rdev->data_offset > rdev->new_data_offset)
3700 return -EINVAL;
3701 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003702 mddev->delta_disks = n - olddisks;
3703 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003704 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 } else
NeilBrownda943b992006-01-06 00:20:54 -08003706 mddev->raid_disks = n;
3707 return rv ? rv : len;
3708}
3709static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003710__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003711
NeilBrown24dd4692005-11-08 21:39:26 -08003712static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003713chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003714{
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003716 mddev->chunk_sectors != mddev->new_chunk_sectors)
3717 return sprintf(page, "%d (%d)\n",
3718 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003719 mddev->chunk_sectors << 9);
3720 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003721}
3722
3723static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003724chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003725{
NeilBrown3b343802006-01-06 00:20:47 -08003726 char *e;
3727 unsigned long n = simple_strtoul(buf, &e, 10);
3728
NeilBrown3b343802006-01-06 00:20:47 -08003729 if (!*buf || (*e && *e != '\n'))
3730 return -EINVAL;
3731
NeilBrownb3546032009-03-31 14:56:41 +11003732 if (mddev->pers) {
3733 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003734 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003735 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003736 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003737 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003738 if (err) {
3739 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003740 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003741 }
NeilBrownb3546032009-03-31 14:56:41 +11003742 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003743 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003744 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003745 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003746 }
NeilBrown3b343802006-01-06 00:20:47 -08003747 return len;
3748}
3749static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003750__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003751
NeilBrowna94213b2006-06-26 00:28:00 -07003752static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003753resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003754{
NeilBrownd1a7c502009-03-31 15:24:32 +11003755 if (mddev->recovery_cp == MaxSector)
3756 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003757 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3758}
3759
3760static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003761resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003762{
NeilBrowna94213b2006-06-26 00:28:00 -07003763 char *e;
3764 unsigned long long n = simple_strtoull(buf, &e, 10);
3765
NeilBrownb0986362011-05-11 15:52:21 +10003766 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003767 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003768 if (cmd_match(buf, "none"))
3769 n = MaxSector;
3770 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003771 return -EINVAL;
3772
3773 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003774 if (mddev->pers)
3775 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003776 return len;
3777}
3778static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003779__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003780
NeilBrown9e653b62006-06-26 00:27:58 -07003781/*
3782 * The array state can be:
3783 *
3784 * clear
3785 * No devices, no size, no level
3786 * Equivalent to STOP_ARRAY ioctl
3787 * inactive
3788 * May have some settings, but array is not active
3789 * all IO results in error
3790 * When written, doesn't tear down array, but just stops it
3791 * suspended (not supported yet)
3792 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003793 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003794 * readonly
3795 * no resync can happen. no superblocks get written.
3796 * write requests fail
3797 * read-auto
3798 * like readonly, but behaves like 'clean' on a write request.
3799 *
3800 * clean - no pending writes, but otherwise active.
3801 * When written to inactive array, starts without resync
3802 * If a write request arrives then
3803 * if metadata is known, mark 'dirty' and switch to 'active'.
3804 * if not known, block and switch to write-pending
3805 * If written to an active array that has pending writes, then fails.
3806 * active
3807 * fully active: IO and resync can be happening.
3808 * When written to inactive array, starts with resync
3809 *
3810 * write-pending
3811 * clean, but writes are blocked waiting for 'active' to be written.
3812 *
3813 * active-idle
3814 * like active, but no writes have been seen for a while (100msec).
3815 *
3816 */
3817enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3818 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003819static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003820 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3821 "write-pending", "active-idle", NULL };
3822
3823static int match_word(const char *word, char **list)
3824{
3825 int n;
3826 for (n=0; list[n]; n++)
3827 if (cmd_match(word, list[n]))
3828 break;
3829 return n;
3830}
3831
3832static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003833array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003834{
3835 enum array_state st = inactive;
3836
3837 if (mddev->pers)
3838 switch(mddev->ro) {
3839 case 1:
3840 st = readonly;
3841 break;
3842 case 2:
3843 st = read_auto;
3844 break;
3845 case 0:
3846 if (mddev->in_sync)
3847 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003848 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003849 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003850 else if (mddev->safemode)
3851 st = active_idle;
3852 else
3853 st = active;
3854 }
3855 else {
3856 if (list_empty(&mddev->disks) &&
3857 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003858 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003859 st = clear;
3860 else
3861 st = inactive;
3862 }
3863 return sprintf(page, "%s\n", array_states[st]);
3864}
3865
NeilBrowna05b7ea2012-07-19 15:59:18 +10003866static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3867static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003868static int do_md_run(struct mddev * mddev);
3869static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003870
3871static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003872array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003873{
3874 int err = -EINVAL;
3875 enum array_state st = match_word(buf, array_states);
3876 switch(st) {
3877 case bad_word:
3878 break;
3879 case clear:
3880 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003881 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003882 break;
3883 case inactive:
3884 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003885 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003886 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003887 else
NeilBrowne6910632008-02-06 01:39:51 -08003888 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003889 break;
3890 case suspended:
3891 break; /* not supported yet */
3892 case readonly:
3893 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003894 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003895 else {
3896 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003897 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003898 err = do_md_run(mddev);
3899 }
3900 break;
3901 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003902 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003903 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003904 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003905 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003906 err = restart_array(mddev);
3907 if (err == 0) {
3908 mddev->ro = 2;
3909 set_disk_ro(mddev->gendisk, 0);
3910 }
NeilBrown9e653b62006-06-26 00:27:58 -07003911 } else {
3912 mddev->ro = 2;
3913 err = do_md_run(mddev);
3914 }
3915 break;
3916 case clean:
3917 if (mddev->pers) {
3918 restart_array(mddev);
3919 spin_lock_irq(&mddev->write_lock);
3920 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003921 if (mddev->in_sync == 0) {
3922 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003923 if (mddev->safemode == 1)
3924 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003925 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003926 }
3927 err = 0;
3928 } else
3929 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003930 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003931 } else
3932 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003933 break;
3934 case active:
3935 if (mddev->pers) {
3936 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003937 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003938 wake_up(&mddev->sb_wait);
3939 err = 0;
3940 } else {
3941 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003942 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003943 err = do_md_run(mddev);
3944 }
3945 break;
3946 case write_pending:
3947 case active_idle:
3948 /* these cannot be set */
3949 break;
3950 }
3951 if (err)
3952 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003953 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003954 if (mddev->hold_active == UNTIL_IOCTL)
3955 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003956 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003957 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003958 }
NeilBrown9e653b62006-06-26 00:27:58 -07003959}
NeilBrown80ca3a42006-07-10 04:44:18 -07003960static struct md_sysfs_entry md_array_state =
3961__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003962
NeilBrown6d7ff7382006-01-06 00:21:16 -08003963static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003964max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003965 return sprintf(page, "%d\n",
3966 atomic_read(&mddev->max_corr_read_errors));
3967}
3968
3969static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003970max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003971{
3972 char *e;
3973 unsigned long n = simple_strtoul(buf, &e, 10);
3974
3975 if (*buf && (*e == 0 || *e == '\n')) {
3976 atomic_set(&mddev->max_corr_read_errors, n);
3977 return len;
3978 }
3979 return -EINVAL;
3980}
3981
3982static struct md_sysfs_entry max_corr_read_errors =
3983__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3984 max_corrected_read_errors_store);
3985
3986static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003987null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003988{
3989 return -EINVAL;
3990}
3991
3992static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003993new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003994{
3995 /* buf must be %d:%d\n? giving major and minor numbers */
3996 /* The new device is added to the array.
3997 * If the array has a persistent superblock, we read the
3998 * superblock to initialise info and check validity.
3999 * Otherwise, only checking done is that in bind_rdev_to_array,
4000 * which mainly checks size.
4001 */
4002 char *e;
4003 int major = simple_strtoul(buf, &e, 10);
4004 int minor;
4005 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004006 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004007 int err;
4008
4009 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4010 return -EINVAL;
4011 minor = simple_strtoul(e+1, &e, 10);
4012 if (*e && *e != '\n')
4013 return -EINVAL;
4014 dev = MKDEV(major, minor);
4015 if (major != MAJOR(dev) ||
4016 minor != MINOR(dev))
4017 return -EOVERFLOW;
4018
4019
4020 if (mddev->persistent) {
4021 rdev = md_import_device(dev, mddev->major_version,
4022 mddev->minor_version);
4023 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004024 struct md_rdev *rdev0
4025 = list_entry(mddev->disks.next,
4026 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004027 err = super_types[mddev->major_version]
4028 .load_super(rdev, rdev0, mddev->minor_version);
4029 if (err < 0)
4030 goto out;
4031 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004032 } else if (mddev->external)
4033 rdev = md_import_device(dev, -2, -1);
4034 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004035 rdev = md_import_device(dev, -1, -1);
4036
4037 if (IS_ERR(rdev))
4038 return PTR_ERR(rdev);
4039 err = bind_rdev_to_array(rdev, mddev);
4040 out:
4041 if (err)
4042 export_rdev(rdev);
4043 return err ? err : len;
4044}
4045
4046static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004047__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004048
4049static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004050bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004051{
4052 char *end;
4053 unsigned long chunk, end_chunk;
4054
4055 if (!mddev->bitmap)
4056 goto out;
4057 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4058 while (*buf) {
4059 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4060 if (buf == end) break;
4061 if (*end == '-') { /* range */
4062 buf = end + 1;
4063 end_chunk = simple_strtoul(buf, &end, 0);
4064 if (buf == end) break;
4065 }
4066 if (*end && !isspace(*end)) break;
4067 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004068 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004069 }
4070 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4071out:
4072 return len;
4073}
4074
4075static struct md_sysfs_entry md_bitmap =
4076__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4077
4078static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004079size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004080{
Andre Noll58c0fed2009-03-31 14:33:13 +11004081 return sprintf(page, "%llu\n",
4082 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004083}
4084
NeilBrownfd01b882011-10-11 16:47:53 +11004085static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004086
4087static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004088size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004089{
4090 /* If array is inactive, we can reduce the component size, but
4091 * not increase it (except from 0).
4092 * If array is active, we can try an on-line resize
4093 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004094 sector_t sectors;
4095 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004096
Andre Noll58c0fed2009-03-31 14:33:13 +11004097 if (err < 0)
4098 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004099 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004100 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004101 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004102 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004103 if (mddev->dev_sectors == 0 ||
4104 mddev->dev_sectors > sectors)
4105 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004106 else
4107 err = -ENOSPC;
4108 }
4109 return err ? err : len;
4110}
4111
4112static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004113__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004114
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115
Masanari Iida83f0d772012-10-30 00:18:08 +09004116/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004117 * This is one of
4118 * 'none' for arrays with no metadata (good luck...)
4119 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004120 * or N.M for internally known formats
4121 */
4122static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004123metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004124{
4125 if (mddev->persistent)
4126 return sprintf(page, "%d.%d\n",
4127 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004128 else if (mddev->external)
4129 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004130 else
4131 return sprintf(page, "none\n");
4132}
4133
4134static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004135metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004136{
4137 int major, minor;
4138 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004139 /* Changing the details of 'external' metadata is
4140 * always permitted. Otherwise there must be
4141 * no devices attached to the array.
4142 */
4143 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4144 ;
4145 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146 return -EBUSY;
4147
4148 if (cmd_match(buf, "none")) {
4149 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004150 mddev->external = 0;
4151 mddev->major_version = 0;
4152 mddev->minor_version = 90;
4153 return len;
4154 }
4155 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004156 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004157 if (namelen >= sizeof(mddev->metadata_type))
4158 namelen = sizeof(mddev->metadata_type)-1;
4159 strncpy(mddev->metadata_type, buf+9, namelen);
4160 mddev->metadata_type[namelen] = 0;
4161 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4162 mddev->metadata_type[--namelen] = 0;
4163 mddev->persistent = 0;
4164 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004165 mddev->major_version = 0;
4166 mddev->minor_version = 90;
4167 return len;
4168 }
4169 major = simple_strtoul(buf, &e, 10);
4170 if (e==buf || *e != '.')
4171 return -EINVAL;
4172 buf = e+1;
4173 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004174 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004175 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004176 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004177 return -ENOENT;
4178 mddev->major_version = major;
4179 mddev->minor_version = minor;
4180 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004181 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004182 return len;
4183}
4184
4185static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004186__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004187
NeilBrowna35b0d62006-01-06 00:20:49 -08004188static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004189action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004190{
NeilBrown7eec3142005-11-08 21:39:44 -08004191 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004192 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4193 type = "frozen";
4194 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004195 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004196 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4197 type = "reshape";
4198 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004199 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4200 type = "resync";
4201 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4202 type = "check";
4203 else
4204 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004205 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004206 type = "recover";
4207 }
4208 return sprintf(page, "%s\n", type);
4209}
4210
4211static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004212action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004213{
NeilBrown7eec3142005-11-08 21:39:44 -08004214 if (!mddev->pers || !mddev->pers->sync_request)
4215 return -EINVAL;
4216
NeilBrownb6a9ce62009-05-26 09:41:17 +10004217 if (cmd_match(page, "frozen"))
4218 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4219 else
4220 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4221
4222 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004223 if (mddev->sync_thread) {
4224 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004225 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004226 }
NeilBrown03c902e2006-01-06 00:20:46 -08004227 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4228 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004229 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004230 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004232 else if (cmd_match(page, "recover")) {
4233 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4235 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004236 int err;
4237 if (mddev->pers->start_reshape == NULL)
4238 return -EINVAL;
4239 err = mddev->pers->start_reshape(mddev);
4240 if (err)
4241 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004242 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004243 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004244 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004245 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004246 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004247 return -EINVAL;
4248 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4249 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004250 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004251 if (mddev->ro == 2) {
4252 /* A write to sync_action is enough to justify
4253 * canceling read-auto mode
4254 */
4255 mddev->ro = 0;
4256 md_wakeup_thread(mddev->sync_thread);
4257 }
NeilBrown03c902e2006-01-06 00:20:46 -08004258 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004259 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004260 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004261 return len;
4262}
4263
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004264static struct md_sysfs_entry md_scan_mode =
4265__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4266
4267static ssize_t
4268last_sync_action_show(struct mddev *mddev, char *page)
4269{
4270 return sprintf(page, "%s\n", mddev->last_sync_action);
4271}
4272
4273static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4274
NeilBrown9d888832005-11-08 21:39:26 -08004275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004277{
4278 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004279 (unsigned long long)
4280 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004281}
4282
NeilBrown80ca3a42006-07-10 04:44:18 -07004283static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004284
NeilBrown88202a02006-01-06 00:21:36 -08004285static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004286sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004287{
4288 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4289 mddev->sync_speed_min ? "local": "system");
4290}
4291
4292static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004293sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004294{
4295 int min;
4296 char *e;
4297 if (strncmp(buf, "system", 6)==0) {
4298 mddev->sync_speed_min = 0;
4299 return len;
4300 }
4301 min = simple_strtoul(buf, &e, 10);
4302 if (buf == e || (*e && *e != '\n') || min <= 0)
4303 return -EINVAL;
4304 mddev->sync_speed_min = min;
4305 return len;
4306}
4307
4308static struct md_sysfs_entry md_sync_min =
4309__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4310
4311static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004312sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004313{
4314 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4315 mddev->sync_speed_max ? "local": "system");
4316}
4317
4318static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004319sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004320{
4321 int max;
4322 char *e;
4323 if (strncmp(buf, "system", 6)==0) {
4324 mddev->sync_speed_max = 0;
4325 return len;
4326 }
4327 max = simple_strtoul(buf, &e, 10);
4328 if (buf == e || (*e && *e != '\n') || max <= 0)
4329 return -EINVAL;
4330 mddev->sync_speed_max = max;
4331 return len;
4332}
4333
4334static struct md_sysfs_entry md_sync_max =
4335__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4336
Iustin Popd7f3d292007-10-16 23:30:54 -07004337static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004338degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004339{
4340 return sprintf(page, "%d\n", mddev->degraded);
4341}
4342static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004343
4344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004345sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004346{
4347 return sprintf(page, "%d\n", mddev->parallel_resync);
4348}
4349
4350static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004351sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004352{
4353 long n;
4354
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004355 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004356 return -EINVAL;
4357
4358 if (n != 0 && n != 1)
4359 return -EINVAL;
4360
4361 mddev->parallel_resync = n;
4362
4363 if (mddev->sync_thread)
4364 wake_up(&resync_wait);
4365
4366 return len;
4367}
4368
4369/* force parallel resync, even with shared block devices */
4370static struct md_sysfs_entry md_sync_force_parallel =
4371__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4372 sync_force_parallel_show, sync_force_parallel_store);
4373
4374static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004375sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004376{
4377 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004378 if (mddev->curr_resync == 0)
4379 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004380 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4381 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004382 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004383 db = resync - mddev->resync_mark_cnt;
4384 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004385}
4386
NeilBrown80ca3a42006-07-10 04:44:18 -07004387static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004388
4389static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004390sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004391{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004392 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004393
NeilBrownacb180b2009-04-14 16:28:34 +10004394 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4395 return sprintf(page, "none\n");
4396
NeilBrown72f36d52012-10-11 14:25:57 +11004397 if (mddev->curr_resync == 1 ||
4398 mddev->curr_resync == 2)
4399 return sprintf(page, "delayed\n");
4400
NeilBrownc804cde2012-05-21 09:28:33 +10004401 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4402 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004403 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004404 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004405 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004406
NeilBrownacb180b2009-04-14 16:28:34 +10004407 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004408 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004409}
4410
NeilBrown80ca3a42006-07-10 04:44:18 -07004411static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004412
NeilBrowne464eaf2006-03-27 01:18:14 -08004413static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004414min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004415{
4416 return sprintf(page, "%llu\n",
4417 (unsigned long long)mddev->resync_min);
4418}
4419static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004420min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004421{
4422 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004423 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004424 return -EINVAL;
4425 if (min > mddev->resync_max)
4426 return -EINVAL;
4427 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4428 return -EBUSY;
4429
4430 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004431 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004432 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004433 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004434 return -EINVAL;
4435 }
4436 mddev->resync_min = min;
4437
4438 return len;
4439}
4440
4441static struct md_sysfs_entry md_min_sync =
4442__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4443
4444static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004445max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004446{
4447 if (mddev->resync_max == MaxSector)
4448 return sprintf(page, "max\n");
4449 else
4450 return sprintf(page, "%llu\n",
4451 (unsigned long long)mddev->resync_max);
4452}
4453static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004454max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004455{
4456 if (strncmp(buf, "max", 3) == 0)
4457 mddev->resync_max = MaxSector;
4458 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004459 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004460 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004461 return -EINVAL;
4462 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004463 return -EINVAL;
4464 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004465 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004466 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4467 return -EBUSY;
4468
4469 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004470 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004471 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004472 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004473 return -EINVAL;
4474 }
4475 mddev->resync_max = max;
4476 }
4477 wake_up(&mddev->recovery_wait);
4478 return len;
4479}
4480
4481static struct md_sysfs_entry md_max_sync =
4482__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4483
4484static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004485suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004486{
4487 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4488}
4489
4490static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004491suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004492{
4493 char *e;
4494 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004495 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004496
NeilBrownb8d966e2009-07-01 11:14:04 +10004497 if (mddev->pers == NULL ||
4498 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004499 return -EINVAL;
4500 if (buf == e || (*e && *e != '\n'))
4501 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004502
4503 mddev->suspend_lo = new;
4504 if (new >= old)
4505 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004506 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004507 else {
4508 /* Expanding suspended region - need to wait */
4509 mddev->pers->quiesce(mddev, 1);
4510 mddev->pers->quiesce(mddev, 0);
4511 }
4512 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004513}
4514static struct md_sysfs_entry md_suspend_lo =
4515__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4516
4517
4518static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004519suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004520{
4521 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4522}
4523
4524static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004525suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004526{
4527 char *e;
4528 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004529 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004530
NeilBrownb8d966e2009-07-01 11:14:04 +10004531 if (mddev->pers == NULL ||
4532 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004533 return -EINVAL;
4534 if (buf == e || (*e && *e != '\n'))
4535 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004536
4537 mddev->suspend_hi = new;
4538 if (new <= old)
4539 /* Shrinking suspended region */
4540 mddev->pers->quiesce(mddev, 2);
4541 else {
4542 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004543 mddev->pers->quiesce(mddev, 1);
4544 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004545 }
4546 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004547}
4548static struct md_sysfs_entry md_suspend_hi =
4549__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4550
NeilBrown08a02ec2007-05-09 02:35:38 -07004551static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004552reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004553{
4554 if (mddev->reshape_position != MaxSector)
4555 return sprintf(page, "%llu\n",
4556 (unsigned long long)mddev->reshape_position);
4557 strcpy(page, "none\n");
4558 return 5;
4559}
4560
4561static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004562reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004563{
NeilBrownc6563a82012-05-21 09:27:00 +10004564 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004565 char *e;
4566 unsigned long long new = simple_strtoull(buf, &e, 10);
4567 if (mddev->pers)
4568 return -EBUSY;
4569 if (buf == e || (*e && *e != '\n'))
4570 return -EINVAL;
4571 mddev->reshape_position = new;
4572 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004573 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004574 mddev->new_level = mddev->level;
4575 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004576 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004577 rdev_for_each(rdev, mddev)
4578 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004579 return len;
4580}
4581
4582static struct md_sysfs_entry md_reshape_position =
4583__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4584 reshape_position_store);
4585
Dan Williamsb522adc2009-03-31 15:00:31 +11004586static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004587reshape_direction_show(struct mddev *mddev, char *page)
4588{
4589 return sprintf(page, "%s\n",
4590 mddev->reshape_backwards ? "backwards" : "forwards");
4591}
4592
4593static ssize_t
4594reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4595{
4596 int backwards = 0;
4597 if (cmd_match(buf, "forwards"))
4598 backwards = 0;
4599 else if (cmd_match(buf, "backwards"))
4600 backwards = 1;
4601 else
4602 return -EINVAL;
4603 if (mddev->reshape_backwards == backwards)
4604 return len;
4605
4606 /* check if we are allowed to change */
4607 if (mddev->delta_disks)
4608 return -EBUSY;
4609
4610 if (mddev->persistent &&
4611 mddev->major_version == 0)
4612 return -EINVAL;
4613
4614 mddev->reshape_backwards = backwards;
4615 return len;
4616}
4617
4618static struct md_sysfs_entry md_reshape_direction =
4619__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4620 reshape_direction_store);
4621
4622static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004623array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004624{
4625 if (mddev->external_size)
4626 return sprintf(page, "%llu\n",
4627 (unsigned long long)mddev->array_sectors/2);
4628 else
4629 return sprintf(page, "default\n");
4630}
4631
4632static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004633array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004634{
4635 sector_t sectors;
4636
4637 if (strncmp(buf, "default", 7) == 0) {
4638 if (mddev->pers)
4639 sectors = mddev->pers->size(mddev, 0, 0);
4640 else
4641 sectors = mddev->array_sectors;
4642
4643 mddev->external_size = 0;
4644 } else {
4645 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4646 return -EINVAL;
4647 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004648 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004649
4650 mddev->external_size = 1;
4651 }
4652
4653 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004654 if (mddev->pers) {
4655 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004656 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004657 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004658 return len;
4659}
4660
4661static struct md_sysfs_entry md_array_size =
4662__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4663 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004664
NeilBrowneae17012005-11-08 21:39:23 -08004665static struct attribute *md_default_attrs[] = {
4666 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004667 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004668 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004669 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004670 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004671 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004672 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004673 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004674 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004675 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004676 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004677 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004678 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004679 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004680 NULL,
4681};
4682
4683static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004684 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004685 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004686 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004687 &md_sync_min.attr,
4688 &md_sync_max.attr,
4689 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004690 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004691 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004692 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004693 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004694 &md_suspend_lo.attr,
4695 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004696 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004697 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004698 NULL,
4699};
NeilBrown411036f2005-11-08 21:39:40 -08004700static struct attribute_group md_redundancy_group = {
4701 .name = NULL,
4702 .attrs = md_redundancy_attrs,
4703};
4704
NeilBrowneae17012005-11-08 21:39:23 -08004705
4706static ssize_t
4707md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4708{
4709 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004710 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004711 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004712
4713 if (!entry->show)
4714 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004715 spin_lock(&all_mddevs_lock);
4716 if (list_empty(&mddev->all_mddevs)) {
4717 spin_unlock(&all_mddevs_lock);
4718 return -EBUSY;
4719 }
4720 mddev_get(mddev);
4721 spin_unlock(&all_mddevs_lock);
4722
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004723 rv = mddev_lock(mddev);
4724 if (!rv) {
4725 rv = entry->show(mddev, page);
4726 mddev_unlock(mddev);
4727 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004728 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004729 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004730}
4731
4732static ssize_t
4733md_attr_store(struct kobject *kobj, struct attribute *attr,
4734 const char *page, size_t length)
4735{
4736 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004737 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004738 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004739
4740 if (!entry->store)
4741 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004742 if (!capable(CAP_SYS_ADMIN))
4743 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004744 spin_lock(&all_mddevs_lock);
4745 if (list_empty(&mddev->all_mddevs)) {
4746 spin_unlock(&all_mddevs_lock);
4747 return -EBUSY;
4748 }
4749 mddev_get(mddev);
4750 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004751 if (entry->store == new_dev_store)
4752 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004753 rv = mddev_lock(mddev);
4754 if (!rv) {
4755 rv = entry->store(mddev, page, length);
4756 mddev_unlock(mddev);
4757 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004758 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004759 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004760}
4761
4762static void md_free(struct kobject *ko)
4763{
NeilBrownfd01b882011-10-11 16:47:53 +11004764 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004765
4766 if (mddev->sysfs_state)
4767 sysfs_put(mddev->sysfs_state);
4768
4769 if (mddev->gendisk) {
4770 del_gendisk(mddev->gendisk);
4771 put_disk(mddev->gendisk);
4772 }
4773 if (mddev->queue)
4774 blk_cleanup_queue(mddev->queue);
4775
NeilBrowneae17012005-11-08 21:39:23 -08004776 kfree(mddev);
4777}
4778
Emese Revfy52cf25d2010-01-19 02:58:23 +01004779static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004780 .show = md_attr_show,
4781 .store = md_attr_store,
4782};
4783static struct kobj_type md_ktype = {
4784 .release = md_free,
4785 .sysfs_ops = &md_sysfs_ops,
4786 .default_attrs = md_default_attrs,
4787};
4788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789int mdp_major = 0;
4790
Dan Williams5fd3a172009-03-04 00:57:25 -07004791static void mddev_delayed_delete(struct work_struct *ws)
4792{
NeilBrownfd01b882011-10-11 16:47:53 +11004793 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004794
NeilBrown43a70502009-12-14 12:49:55 +11004795 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004796 kobject_del(&mddev->kobj);
4797 kobject_put(&mddev->kobj);
4798}
4799
NeilBrownefeb53c2009-01-09 08:31:10 +11004800static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004802 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004803 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004805 int partitioned;
4806 int shift;
4807 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004808 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004811 return -ENODEV;
4812
4813 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4814 shift = partitioned ? MdpMinorShift : 0;
4815 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Tejun Heoe804ac72010-10-15 15:36:08 +02004817 /* wait for any previous instance of this device to be
4818 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004819 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004820 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004821
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004822 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004823 error = -EEXIST;
4824 if (mddev->gendisk)
4825 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004826
4827 if (name) {
4828 /* Need to ensure that 'name' is not a duplicate.
4829 */
NeilBrownfd01b882011-10-11 16:47:53 +11004830 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004831 spin_lock(&all_mddevs_lock);
4832
4833 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4834 if (mddev2->gendisk &&
4835 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4836 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004837 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004838 }
4839 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
NeilBrown8b765392009-01-09 08:31:08 +11004841
NeilBrown0909dc42009-07-01 12:27:21 +10004842 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004843 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004844 if (!mddev->queue)
4845 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004846 mddev->queue->queuedata = mddev;
4847
NeilBrown409c57f2009-03-31 14:39:39 +11004848 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004849 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 disk = alloc_disk(1 << shift);
4852 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004853 blk_cleanup_queue(mddev->queue);
4854 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004855 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004857 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004859 if (name)
4860 strcpy(disk->disk_name, name);
4861 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004863 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 disk->fops = &md_fops;
4866 disk->private_data = mddev;
4867 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004868 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004869 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004870 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004871 * remove it now.
4872 */
4873 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004875 /* As soon as we call add_disk(), another thread could get
4876 * through to md_open, so make sure it doesn't get too far
4877 */
4878 mutex_lock(&mddev->open_mutex);
4879 add_disk(disk);
4880
Tejun Heoed9e1982008-08-25 19:56:05 +09004881 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4882 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004883 if (error) {
4884 /* This isn't possible, but as kobject_init_and_add is marked
4885 * __must_check, we must do something with the result
4886 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004887 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4888 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004889 error = 0;
4890 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004891 if (mddev->kobj.sd &&
4892 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004893 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004894 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004895 abort:
4896 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004897 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004898 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004899 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004900 }
NeilBrownd3374822009-01-09 08:31:10 +11004901 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004902 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004903}
4904
4905static struct kobject *md_probe(dev_t dev, int *part, void *data)
4906{
4907 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return NULL;
4909}
4910
NeilBrownefeb53c2009-01-09 08:31:10 +11004911static int add_named_array(const char *val, struct kernel_param *kp)
4912{
4913 /* val must be "md_*" where * is not all digits.
4914 * We allocate an array with a large free minor number, and
4915 * set the name to val. val must not already be an active name.
4916 */
4917 int len = strlen(val);
4918 char buf[DISK_NAME_LEN];
4919
4920 while (len && val[len-1] == '\n')
4921 len--;
4922 if (len >= DISK_NAME_LEN)
4923 return -E2BIG;
4924 strlcpy(buf, val, len+1);
4925 if (strncmp(buf, "md_", 3) != 0)
4926 return -EINVAL;
4927 return md_alloc(0, buf);
4928}
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930static void md_safemode_timeout(unsigned long data)
4931{
NeilBrownfd01b882011-10-11 16:47:53 +11004932 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Neil Brown0fd62b82008-06-28 08:31:36 +10004934 if (!atomic_read(&mddev->writes_pending)) {
4935 mddev->safemode = 1;
4936 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004937 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 md_wakeup_thread(mddev->thread);
4940}
4941
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004942static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
NeilBrownfd01b882011-10-11 16:47:53 +11004944int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
NeilBrown2604b702006-01-06 00:20:36 -08004946 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004947 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004948 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
NeilBrowna757e642005-04-16 15:26:42 -07004950 if (list_empty(&mddev->disks))
4951 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954 if (mddev->pers)
4955 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004956 /* Cannot run until previous stop completes properly */
4957 if (mddev->sysfs_active)
4958 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 /*
4961 * Analyze all RAID superblock(s)
4962 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004963 if (!mddev->raid_disks) {
4964 if (!mddev->persistent)
4965 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004966 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
NeilBrownd9d166c2006-01-06 00:20:51 -08004969 if (mddev->level != LEVEL_NONE)
4970 request_module("md-level-%d", mddev->level);
4971 else if (mddev->clevel[0])
4972 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 /*
4975 * Drop all container device buffers, from now on
4976 * the only valid external interface is through the md
4977 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004979 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004980 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 continue;
4982 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004983 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004984
4985 /* perform some consistency tests on the device.
4986 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004987 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004988 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004989 if (rdev->meta_bdev) {
4990 /* Nothing to check */;
4991 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004992 if (mddev->dev_sectors &&
4993 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004994 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004995 printk("md: %s: data overlaps metadata\n",
4996 mdname(mddev));
4997 return -EINVAL;
4998 }
4999 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005000 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005001 > rdev->data_offset) {
5002 printk("md: %s: metadata overlaps data\n",
5003 mdname(mddev));
5004 return -EINVAL;
5005 }
5006 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005007 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009
NeilBrowna167f662010-10-26 18:31:13 +11005010 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005011 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005014 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005015 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005017 if (mddev->level != LEVEL_NONE)
5018 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5019 mddev->level);
5020 else
5021 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5022 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return -EINVAL;
5024 }
NeilBrown2604b702006-01-06 00:20:36 -08005025 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005027 if (mddev->level != pers->level) {
5028 mddev->level = pers->level;
5029 mddev->new_level = pers->level;
5030 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005031 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
NeilBrownf6705572006-03-27 01:18:11 -08005033 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005034 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005035 /* This personality cannot handle reshaping... */
5036 mddev->pers = NULL;
5037 module_put(pers->owner);
5038 return -EINVAL;
5039 }
5040
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005041 if (pers->sync_request) {
5042 /* Warn if this is a potentially silly
5043 * configuration.
5044 */
5045 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005046 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005047 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005048
NeilBrowndafb20f2012-03-19 12:46:39 +11005049 rdev_for_each(rdev, mddev)
5050 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005051 if (rdev < rdev2 &&
5052 rdev->bdev->bd_contains ==
5053 rdev2->bdev->bd_contains) {
5054 printk(KERN_WARNING
5055 "%s: WARNING: %s appears to be"
5056 " on the same physical disk as"
5057 " %s.\n",
5058 mdname(mddev),
5059 bdevname(rdev->bdev,b),
5060 bdevname(rdev2->bdev,b2));
5061 warned = 1;
5062 }
5063 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005064
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005065 if (warned)
5066 printk(KERN_WARNING
5067 "True protection against single-disk"
5068 " failure might be compromised.\n");
5069 }
5070
NeilBrown657390d2005-08-26 18:34:16 -07005071 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005072 /* may be over-ridden by personality */
5073 mddev->resync_max_sectors = mddev->dev_sectors;
5074
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005075 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
NeilBrown0f9552b52009-12-30 12:08:50 +11005077 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005078 mddev->ro = 2; /* read-only, but switch on first write */
5079
NeilBrownb15c2e52006-01-06 00:20:16 -08005080 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005081 if (err)
5082 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005083 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5084 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5085 " but 'external_size' not in effect?\n", __func__);
5086 printk(KERN_ERR
5087 "md: invalid array_size %llu > default size %llu\n",
5088 (unsigned long long)mddev->array_sectors / 2,
5089 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5090 err = -EINVAL;
5091 mddev->pers->stop(mddev);
5092 }
NeilBrownef99bf42012-05-22 13:55:08 +10005093 if (err == 0 && mddev->pers->sync_request &&
5094 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005095 err = bitmap_create(mddev);
5096 if (err) {
5097 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5098 mdname(mddev), err);
5099 mddev->pers->stop(mddev);
5100 }
5101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 module_put(mddev->pers->owner);
5104 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005105 bitmap_destroy(mddev);
5106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005108 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005109 if (mddev->kobj.sd &&
5110 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005111 printk(KERN_WARNING
5112 "md: cannot register extra attributes for %s\n",
5113 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005114 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005115 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005116 mddev->ro = 0;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005119 atomic_set(&mddev->max_corr_read_errors,
5120 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mddev->safemode = 0;
5122 mddev->safemode_timer.function = md_safemode_timeout;
5123 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005124 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005126 smp_wmb();
5127 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005128 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005129 if (rdev->raid_disk >= 0)
5130 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005131 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5134
NeilBrown7a0a5352013-08-27 16:28:23 +10005135 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005136 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
NeilBrownd7603b72006-01-06 00:20:30 -08005138 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005139 sysfs_notify_dirent_safe(mddev->sysfs_state);
5140 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005141 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return 0;
5143}
NeilBrown390ee602010-06-01 19:37:27 +10005144EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
NeilBrownfd01b882011-10-11 16:47:53 +11005146static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005147{
5148 int err;
5149
5150 err = md_run(mddev);
5151 if (err)
5152 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005153 err = bitmap_load(mddev);
5154 if (err) {
5155 bitmap_destroy(mddev);
5156 goto out;
5157 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005158
5159 md_wakeup_thread(mddev->thread);
5160 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5161
NeilBrownfe60b012010-03-29 11:10:42 +11005162 set_capacity(mddev->gendisk, mddev->array_sectors);
5163 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005164 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005165 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5166out:
5167 return err;
5168}
5169
NeilBrownfd01b882011-10-11 16:47:53 +11005170static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
5172 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
Andre Noll80fab1d2008-07-11 22:02:21 +10005174 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005176 return -ENXIO;
5177 if (!mddev->pers)
5178 return -EINVAL;
5179 if (!mddev->ro)
5180 return -EBUSY;
5181 mddev->safemode = 0;
5182 mddev->ro = 0;
5183 set_disk_ro(disk, 0);
5184 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5185 mdname(mddev));
5186 /* Kick recovery or resync if necessary */
5187 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5188 md_wakeup_thread(mddev->thread);
5189 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005190 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
NeilBrownacc55e22006-06-26 00:27:47 -07005194/* similar to deny_write_access, but accounts for our holding a reference
5195 * to the file ourselves */
5196static int deny_bitmap_write_access(struct file * file)
5197{
5198 struct inode *inode = file->f_mapping->host;
5199
5200 spin_lock(&inode->i_lock);
5201 if (atomic_read(&inode->i_writecount) > 1) {
5202 spin_unlock(&inode->i_lock);
5203 return -ETXTBSY;
5204 }
5205 atomic_set(&inode->i_writecount, -1);
5206 spin_unlock(&inode->i_lock);
5207
5208 return 0;
5209}
5210
NeilBrown43a70502009-12-14 12:49:55 +11005211void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005212{
5213 struct inode *inode = file->f_mapping->host;
5214
5215 spin_lock(&inode->i_lock);
5216 atomic_set(&inode->i_writecount, 1);
5217 spin_unlock(&inode->i_lock);
5218}
5219
NeilBrownfd01b882011-10-11 16:47:53 +11005220static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005221{
5222 mddev->array_sectors = 0;
5223 mddev->external_size = 0;
5224 mddev->dev_sectors = 0;
5225 mddev->raid_disks = 0;
5226 mddev->recovery_cp = 0;
5227 mddev->resync_min = 0;
5228 mddev->resync_max = MaxSector;
5229 mddev->reshape_position = MaxSector;
5230 mddev->external = 0;
5231 mddev->persistent = 0;
5232 mddev->level = LEVEL_NONE;
5233 mddev->clevel[0] = 0;
5234 mddev->flags = 0;
5235 mddev->ro = 0;
5236 mddev->metadata_type[0] = 0;
5237 mddev->chunk_sectors = 0;
5238 mddev->ctime = mddev->utime = 0;
5239 mddev->layout = 0;
5240 mddev->max_disks = 0;
5241 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005242 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005243 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005244 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005245 mddev->new_level = LEVEL_NONE;
5246 mddev->new_layout = 0;
5247 mddev->new_chunk_sectors = 0;
5248 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005249 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005250 mddev->suspend_lo = mddev->suspend_hi = 0;
5251 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5252 mddev->recovery = 0;
5253 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005254 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005255 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005256 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005257 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005258 mddev->bitmap_info.offset = 0;
5259 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005260 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->bitmap_info.chunksize = 0;
5262 mddev->bitmap_info.daemon_sleep = 0;
5263 mddev->bitmap_info.max_write_behind = 0;
5264}
5265
NeilBrownfd01b882011-10-11 16:47:53 +11005266static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005267{
NeilBrown6b6204e2013-05-09 09:48:30 +10005268 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005269 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005270 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005271 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005272 }
5273
5274 del_timer_sync(&mddev->safemode_timer);
5275
5276 bitmap_flush(mddev);
5277 md_super_wait(mddev);
5278
NeilBrownb6d428c2013-04-24 11:42:42 +10005279 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005280 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005281 /* mark array as shutdown cleanly */
5282 mddev->in_sync = 1;
5283 md_update_sb(mddev, 1);
5284 }
5285}
NeilBrowndefad612011-01-14 09:14:33 +11005286
NeilBrownfd01b882011-10-11 16:47:53 +11005287void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005288{
NeilBrown29f097c2013-11-14 17:54:51 +11005289 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005290 __md_stop_writes(mddev);
5291 mddev_unlock(mddev);
5292}
NeilBrown390ee602010-06-01 19:37:27 +10005293EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005294
NeilBrown5eff3c42012-11-19 10:47:48 +11005295static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005296{
NeilBrown0ca69882011-01-14 09:14:33 +11005297 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005298 mddev->pers->stop(mddev);
5299 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5300 mddev->to_remove = &md_redundancy_group;
5301 module_put(mddev->pers->owner);
5302 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005303 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005304}
NeilBrown5eff3c42012-11-19 10:47:48 +11005305
5306void md_stop(struct mddev *mddev)
5307{
5308 /* stop the array and free an attached data structures.
5309 * This is called from dm-raid
5310 */
5311 __md_stop(mddev);
5312 bitmap_destroy(mddev);
5313 if (mddev->bio_set)
5314 bioset_free(mddev->bio_set);
5315}
5316
NeilBrown390ee602010-06-01 19:37:27 +10005317EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005318
NeilBrowna05b7ea2012-07-19 15:59:18 +10005319static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d02010-03-29 13:23:10 +11005320{
5321 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005322 int did_freeze = 0;
5323
5324 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5325 did_freeze = 1;
5326 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5327 md_wakeup_thread(mddev->thread);
5328 }
5329 if (mddev->sync_thread) {
5330 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5331 /* Thread might be blocked waiting for metadata update
5332 * which will now never happen */
5333 wake_up_process(mddev->sync_thread->tsk);
5334 }
5335 mddev_unlock(mddev);
5336 wait_event(resync_wait, mddev->sync_thread == NULL);
5337 mddev_lock_nointr(mddev);
5338
NeilBrowna4bd82d02010-03-29 13:23:10 +11005339 mutex_lock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005340 if (atomic_read(&mddev->openers) > !!bdev ||
5341 mddev->sync_thread ||
5342 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005343 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005344 if (did_freeze) {
5345 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5346 md_wakeup_thread(mddev->thread);
5347 }
NeilBrowna4bd82d02010-03-29 13:23:10 +11005348 err = -EBUSY;
5349 goto out;
5350 }
5351 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005352 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005353
5354 err = -ENXIO;
5355 if (mddev->ro==1)
5356 goto out;
5357 mddev->ro = 1;
5358 set_disk_ro(mddev->gendisk, 1);
5359 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005360 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005361 err = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005362 }
5363out:
5364 mutex_unlock(&mddev->open_mutex);
5365 return err;
5366}
5367
NeilBrown9e653b62006-06-26 00:27:58 -07005368/* mode:
5369 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005370 * 2 - stop but do not disassemble array
5371 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005372static int do_md_stop(struct mddev * mddev, int mode,
5373 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005376 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005377 int did_freeze = 0;
5378
5379 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5380 did_freeze = 1;
5381 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5382 md_wakeup_thread(mddev->thread);
5383 }
5384 if (mddev->sync_thread) {
5385 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5386 /* Thread might be blocked waiting for metadata update
5387 * which will now never happen */
5388 wake_up_process(mddev->sync_thread->tsk);
5389 }
5390 mddev_unlock(mddev);
5391 wait_event(resync_wait, mddev->sync_thread == NULL);
5392 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
NeilBrownc8c00a62009-08-10 12:50:52 +10005394 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005395 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005396 mddev->sysfs_active ||
5397 mddev->sync_thread ||
5398 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005399 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005400 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005401 if (did_freeze) {
5402 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5403 md_wakeup_thread(mddev->thread);
5404 }
NeilBrown260fa032013-08-27 16:44:13 +10005405 return -EBUSY;
5406 }
NeilBrown6e17b022010-08-07 21:41:19 +10005407 if (mddev->pers) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005408 if (mddev->ro)
5409 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005410
NeilBrowndefad612011-01-14 09:14:33 +11005411 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005412 __md_stop(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005413 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005414 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005415
NeilBrowna4bd82d02010-03-29 13:23:10 +11005416 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005417 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005418
NeilBrowndafb20f2012-03-19 12:46:39 +11005419 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005420 if (rdev->raid_disk >= 0)
5421 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005422
NeilBrowna4bd82d02010-03-29 13:23:10 +11005423 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005424 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005425 mddev->changed = 1;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005426 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005427
NeilBrowna4bd82d02010-03-29 13:23:10 +11005428 if (mddev->ro)
5429 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005430 } else
5431 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 /*
5433 * Free resources if final stop
5434 */
NeilBrown9e653b62006-06-26 00:27:58 -07005435 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5437
NeilBrown978f9462006-02-02 14:28:05 -08005438 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005439 if (mddev->bitmap_info.file) {
5440 restore_bitmap_write_access(mddev->bitmap_info.file);
5441 fput(mddev->bitmap_info.file);
5442 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005443 }
NeilBrownc3d97142009-12-14 12:49:52 +11005444 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 export_array(mddev);
5447
NeilBrown6177b472010-03-29 11:37:13 +11005448 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005449 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005450 if (mddev->hold_active == UNTIL_STOP)
5451 mddev->hold_active = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005452 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005453 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005454 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005455 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457}
5458
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005459#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005460static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
NeilBrown3cb03002011-10-11 16:45:26 +11005462 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 int err;
5464
NeilBrowna757e642005-04-16 15:26:42 -07005465 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 printk(KERN_INFO "md: running: ");
5469
NeilBrowndafb20f2012-03-19 12:46:39 +11005470 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 char b[BDEVNAME_SIZE];
5472 printk("<%s>", bdevname(rdev->bdev,b));
5473 }
5474 printk("\n");
5475
NeilBrownd710e132008-10-13 11:55:12 +11005476 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 if (err) {
5478 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005479 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 }
5481}
5482
5483/*
5484 * lets try to run arrays based on all disks that have arrived
5485 * until now. (those are in pending_raid_disks)
5486 *
5487 * the method: pick the first pending disk, collect all disks with
5488 * the same UUID, remove all from the pending list and put them into
5489 * the 'same_array' list. Then order this list based on superblock
5490 * update time (freshest comes first), kick out 'old' disks and
5491 * compare superblocks. If everything's fine then run it.
5492 *
5493 * If "unit" is allocated, then bump its reference count
5494 */
5495static void autorun_devices(int part)
5496{
NeilBrown3cb03002011-10-11 16:45:26 +11005497 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005498 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 char b[BDEVNAME_SIZE];
5500
5501 printk(KERN_INFO "md: autorun ...\n");
5502 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005503 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005505 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005507 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
5509 printk(KERN_INFO "md: considering %s ...\n",
5510 bdevname(rdev0->bdev,b));
5511 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005512 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 if (super_90_load(rdev, rdev0, 0) >= 0) {
5514 printk(KERN_INFO "md: adding %s ...\n",
5515 bdevname(rdev->bdev,b));
5516 list_move(&rdev->same_set, &candidates);
5517 }
5518 /*
5519 * now we have a set of devices, with all of them having
5520 * mostly sane superblocks. It's time to allocate the
5521 * mddev.
5522 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005523 if (part) {
5524 dev = MKDEV(mdp_major,
5525 rdev0->preferred_minor << MdpMinorShift);
5526 unit = MINOR(dev) >> MdpMinorShift;
5527 } else {
5528 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5529 unit = MINOR(dev);
5530 }
5531 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5533 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5534 break;
5535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 md_probe(dev, NULL, NULL);
5538 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005539 if (!mddev || !mddev->gendisk) {
5540 if (mddev)
5541 mddev_put(mddev);
5542 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 "md: cannot allocate memory for md drive.\n");
5544 break;
5545 }
5546 if (mddev_lock(mddev))
5547 printk(KERN_WARNING "md: %s locked, cannot run\n",
5548 mdname(mddev));
5549 else if (mddev->raid_disks || mddev->major_version
5550 || !list_empty(&mddev->disks)) {
5551 printk(KERN_WARNING
5552 "md: %s already running, cannot run %s\n",
5553 mdname(mddev), bdevname(rdev0->bdev,b));
5554 mddev_unlock(mddev);
5555 } else {
5556 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005557 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005558 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 list_del_init(&rdev->same_set);
5560 if (bind_rdev_to_array(rdev, mddev))
5561 export_rdev(rdev);
5562 }
5563 autorun_array(mddev);
5564 mddev_unlock(mddev);
5565 }
5566 /* on success, candidates will be empty, on error
5567 * it won't...
5568 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005569 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005570 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 mddev_put(mddev);
5574 }
5575 printk(KERN_INFO "md: ... autorun DONE.\n");
5576}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005577#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579static int get_version(void __user * arg)
5580{
5581 mdu_version_t ver;
5582
5583 ver.major = MD_MAJOR_VERSION;
5584 ver.minor = MD_MINOR_VERSION;
5585 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5586
5587 if (copy_to_user(arg, &ver, sizeof(ver)))
5588 return -EFAULT;
5589
5590 return 0;
5591}
5592
NeilBrownfd01b882011-10-11 16:47:53 +11005593static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594{
5595 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005596 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005597 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
NeilBrown1ca69c42012-10-11 13:37:33 +11005599 nr = working = insync = failed = spare = 0;
5600 rcu_read_lock();
5601 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005603 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 failed++;
5605 else {
5606 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005607 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005608 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 else
5610 spare++;
5611 }
5612 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 info.major_version = mddev->major_version;
5616 info.minor_version = mddev->minor_version;
5617 info.patch_version = MD_PATCHLEVEL_VERSION;
5618 info.ctime = mddev->ctime;
5619 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005620 info.size = mddev->dev_sectors / 2;
5621 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005622 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 info.nr_disks = nr;
5624 info.raid_disks = mddev->raid_disks;
5625 info.md_minor = mddev->md_minor;
5626 info.not_persistent= !mddev->persistent;
5627
5628 info.utime = mddev->utime;
5629 info.state = 0;
5630 if (mddev->in_sync)
5631 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005632 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005633 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005634 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 info.working_disks = working;
5636 info.failed_disks = failed;
5637 info.spare_disks = spare;
5638
5639 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005640 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 if (copy_to_user(arg, &info, sizeof(info)))
5643 return -EFAULT;
5644
5645 return 0;
5646}
5647
NeilBrownfd01b882011-10-11 16:47:53 +11005648static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005649{
5650 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5651 char *ptr, *buf = NULL;
5652 int err = -ENOMEM;
5653
NeilBrown60559da2013-07-16 16:44:29 +10005654 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005655
NeilBrown32a76272005-06-21 17:17:14 -07005656 if (!file)
5657 goto out;
5658
5659 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005660 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005661 file->pathname[0] = '\0';
5662 goto copy_out;
5663 }
5664
5665 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5666 if (!buf)
5667 goto out;
5668
NeilBrown1ec885c2012-05-22 13:55:10 +10005669 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5670 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005671 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005672 goto out;
5673
5674 strcpy(file->pathname, ptr);
5675
5676copy_out:
5677 err = 0;
5678 if (copy_to_user(arg, file, sizeof(*file)))
5679 err = -EFAULT;
5680out:
5681 kfree(buf);
5682 kfree(file);
5683 return err;
5684}
5685
NeilBrownfd01b882011-10-11 16:47:53 +11005686static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
5688 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005689 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 if (copy_from_user(&info, arg, sizeof(info)))
5692 return -EFAULT;
5693
NeilBrown1ca69c42012-10-11 13:37:33 +11005694 rcu_read_lock();
5695 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 if (rdev) {
5697 info.major = MAJOR(rdev->bdev->bd_dev);
5698 info.minor = MINOR(rdev->bdev->bd_dev);
5699 info.raid_disk = rdev->raid_disk;
5700 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005701 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005703 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 info.state |= (1<<MD_DISK_ACTIVE);
5705 info.state |= (1<<MD_DISK_SYNC);
5706 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005707 if (test_bit(WriteMostly, &rdev->flags))
5708 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 } else {
5710 info.major = info.minor = 0;
5711 info.raid_disk = -1;
5712 info.state = (1<<MD_DISK_REMOVED);
5713 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005714 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 if (copy_to_user(arg, &info, sizeof(info)))
5717 return -EFAULT;
5718
5719 return 0;
5720}
5721
NeilBrownfd01b882011-10-11 16:47:53 +11005722static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
5724 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005725 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 dev_t dev = MKDEV(info->major,info->minor);
5727
5728 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5729 return -EOVERFLOW;
5730
5731 if (!mddev->raid_disks) {
5732 int err;
5733 /* expecting a device which has a superblock */
5734 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5735 if (IS_ERR(rdev)) {
5736 printk(KERN_WARNING
5737 "md: md_import_device returned %ld\n",
5738 PTR_ERR(rdev));
5739 return PTR_ERR(rdev);
5740 }
5741 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005742 struct md_rdev *rdev0
5743 = list_entry(mddev->disks.next,
5744 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005745 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 .load_super(rdev, rdev0, mddev->minor_version);
5747 if (err < 0) {
5748 printk(KERN_WARNING
5749 "md: %s has different UUID to %s\n",
5750 bdevname(rdev->bdev,b),
5751 bdevname(rdev0->bdev,b2));
5752 export_rdev(rdev);
5753 return -EINVAL;
5754 }
5755 }
5756 err = bind_rdev_to_array(rdev, mddev);
5757 if (err)
5758 export_rdev(rdev);
5759 return err;
5760 }
5761
5762 /*
5763 * add_new_disk can be used once the array is assembled
5764 * to add "hot spares". They must already have a superblock
5765 * written
5766 */
5767 if (mddev->pers) {
5768 int err;
5769 if (!mddev->pers->hot_add_disk) {
5770 printk(KERN_WARNING
5771 "%s: personality does not support diskops!\n",
5772 mdname(mddev));
5773 return -EINVAL;
5774 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005775 if (mddev->persistent)
5776 rdev = md_import_device(dev, mddev->major_version,
5777 mddev->minor_version);
5778 else
5779 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 if (IS_ERR(rdev)) {
5781 printk(KERN_WARNING
5782 "md: md_import_device returned %ld\n",
5783 PTR_ERR(rdev));
5784 return PTR_ERR(rdev);
5785 }
NeilBrown1a855a02010-12-09 16:36:28 +11005786 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005787 if (!mddev->persistent) {
5788 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005789 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005790 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005791 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005792 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005793 } else
NeilBrown41158c72005-06-21 17:17:25 -07005794 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005795 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005796 } else
5797 super_types[mddev->major_version].
5798 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005799 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005800 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005801 /* This was a hot-add request, but events doesn't
5802 * match, so reject it.
5803 */
5804 export_rdev(rdev);
5805 return -EINVAL;
5806 }
5807
NeilBrownb2d444d2005-11-08 21:39:31 -08005808 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005809 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5810 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005811 else
5812 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 rdev->raid_disk = -1;
5815 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005816 if (!err && !mddev->pers->hot_remove_disk) {
5817 /* If there is hot_add_disk but no hot_remove_disk
5818 * then added disks for geometry changes,
5819 * and should be added immediately.
5820 */
5821 super_types[mddev->major_version].
5822 validate_super(mddev, rdev);
5823 err = mddev->pers->hot_add_disk(mddev, rdev);
5824 if (err)
5825 unbind_rdev_from_array(rdev);
5826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (err)
5828 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005829 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005830 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005831
NeilBrown7ceb17e2013-04-24 11:42:42 +10005832 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005833 if (mddev->degraded)
5834 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005835 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005836 if (!err)
5837 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005838 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 return err;
5840 }
5841
5842 /* otherwise, add_new_disk is only allowed
5843 * for major_version==0 superblocks
5844 */
5845 if (mddev->major_version != 0) {
5846 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5847 mdname(mddev));
5848 return -EINVAL;
5849 }
5850
5851 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5852 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005853 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 if (IS_ERR(rdev)) {
5855 printk(KERN_WARNING
5856 "md: error, md_import_device() returned %ld\n",
5857 PTR_ERR(rdev));
5858 return PTR_ERR(rdev);
5859 }
5860 rdev->desc_nr = info->number;
5861 if (info->raid_disk < mddev->raid_disks)
5862 rdev->raid_disk = info->raid_disk;
5863 else
5864 rdev->raid_disk = -1;
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005867 if (info->state & (1<<MD_DISK_SYNC))
5868 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005870 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5871 set_bit(WriteMostly, &rdev->flags);
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 if (!mddev->persistent) {
5874 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005875 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5876 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005877 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005878 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
NeilBrown2bf071b2006-01-06 00:20:55 -08005880 err = bind_rdev_to_array(rdev, mddev);
5881 if (err) {
5882 export_rdev(rdev);
5883 return err;
5884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
5886
5887 return 0;
5888}
5889
NeilBrownfd01b882011-10-11 16:47:53 +11005890static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
5892 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005893 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 rdev = find_rdev(mddev, dev);
5896 if (!rdev)
5897 return -ENXIO;
5898
NeilBrown3ea8929d2013-04-24 11:42:41 +10005899 clear_bit(Blocked, &rdev->flags);
5900 remove_and_add_spares(mddev, rdev);
5901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 if (rdev->raid_disk >= 0)
5903 goto busy;
5904
5905 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005906 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005907 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 return 0;
5910busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005911 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 bdevname(rdev->bdev,b), mdname(mddev));
5913 return -EBUSY;
5914}
5915
NeilBrownfd01b882011-10-11 16:47:53 +11005916static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917{
5918 char b[BDEVNAME_SIZE];
5919 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005920 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
5922 if (!mddev->pers)
5923 return -ENODEV;
5924
5925 if (mddev->major_version != 0) {
5926 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5927 " version-0 superblocks.\n",
5928 mdname(mddev));
5929 return -EINVAL;
5930 }
5931 if (!mddev->pers->hot_add_disk) {
5932 printk(KERN_WARNING
5933 "%s: personality does not support diskops!\n",
5934 mdname(mddev));
5935 return -EINVAL;
5936 }
5937
NeilBrownd710e132008-10-13 11:55:12 +11005938 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 if (IS_ERR(rdev)) {
5940 printk(KERN_WARNING
5941 "md: error, md_import_device() returned %ld\n",
5942 PTR_ERR(rdev));
5943 return -EINVAL;
5944 }
5945
5946 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005947 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005949 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
NeilBrown8190e752009-06-18 08:48:58 +10005951 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
NeilBrownb2d444d2005-11-08 21:39:31 -08005953 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 printk(KERN_WARNING
5955 "md: can not hot-add faulty %s disk to %s!\n",
5956 bdevname(rdev->bdev,b), mdname(mddev));
5957 err = -EINVAL;
5958 goto abort_export;
5959 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005960 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005962 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005963 err = bind_rdev_to_array(rdev, mddev);
5964 if (err)
5965 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 /*
5968 * The rest should better be atomic, we can have disk failures
5969 * noticed in interrupt contexts ...
5970 */
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 rdev->raid_disk = -1;
5973
NeilBrown850b2b42006-10-03 01:15:46 -07005974 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 /*
5977 * Kick recovery, maybe this spare has to be added to the
5978 * array immediately.
5979 */
5980 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5981 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005982 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 return 0;
5984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985abort_export:
5986 export_rdev(rdev);
5987 return err;
5988}
5989
NeilBrownfd01b882011-10-11 16:47:53 +11005990static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005991{
5992 int err;
5993
NeilBrown36fa3062005-09-09 16:23:45 -07005994 if (mddev->pers) {
5995 if (!mddev->pers->quiesce)
5996 return -EBUSY;
5997 if (mddev->recovery || mddev->sync_thread)
5998 return -EBUSY;
5999 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006000 }
6001
NeilBrown36fa3062005-09-09 16:23:45 -07006002
6003 if (fd >= 0) {
6004 if (mddev->bitmap)
6005 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11006006 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07006007
NeilBrownc3d97142009-12-14 12:49:52 +11006008 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07006009 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
6010 mdname(mddev));
6011 return -EBADF;
6012 }
6013
NeilBrownc3d97142009-12-14 12:49:52 +11006014 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07006015 if (err) {
6016 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
6017 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11006018 fput(mddev->bitmap_info.file);
6019 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006020 return err;
6021 }
NeilBrownc3d97142009-12-14 12:49:52 +11006022 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006023 } else if (mddev->bitmap == NULL)
6024 return -ENOENT; /* cannot remove what isn't there */
6025 err = 0;
6026 if (mddev->pers) {
6027 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006028 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006029 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006030 if (!err)
6031 err = bitmap_load(mddev);
6032 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006033 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006034 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006035 fd = -1; /* make sure to put the file */
6036 }
NeilBrown36fa3062005-09-09 16:23:45 -07006037 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006038 }
6039 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006040 if (mddev->bitmap_info.file) {
6041 restore_bitmap_write_access(mddev->bitmap_info.file);
6042 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006043 }
NeilBrownc3d97142009-12-14 12:49:52 +11006044 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006045 }
6046
NeilBrown32a76272005-06-21 17:17:14 -07006047 return err;
6048}
6049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050/*
6051 * set_array_info is used two different ways
6052 * The original usage is when creating a new array.
6053 * In this usage, raid_disks is > 0 and it together with
6054 * level, size, not_persistent,layout,chunksize determine the
6055 * shape of the array.
6056 * This will always create an array with a type-0.90.0 superblock.
6057 * The newer usage is when assembling an array.
6058 * In this case raid_disks will be 0, and the major_version field is
6059 * use to determine which style super-blocks are to be found on the devices.
6060 * The minor and patch _version numbers are also kept incase the
6061 * super_block handler wishes to interpret them.
6062 */
NeilBrownfd01b882011-10-11 16:47:53 +11006063static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064{
6065
6066 if (info->raid_disks == 0) {
6067 /* just setting version number for superblock loading */
6068 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006069 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 super_types[info->major_version].name == NULL) {
6071 /* maybe try to auto-load a module? */
6072 printk(KERN_INFO
6073 "md: superblock version %d not known\n",
6074 info->major_version);
6075 return -EINVAL;
6076 }
6077 mddev->major_version = info->major_version;
6078 mddev->minor_version = info->minor_version;
6079 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006080 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006081 /* ensure mddev_put doesn't delete this now that there
6082 * is some minimal configuration.
6083 */
6084 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return 0;
6086 }
6087 mddev->major_version = MD_MAJOR_VERSION;
6088 mddev->minor_version = MD_MINOR_VERSION;
6089 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6090 mddev->ctime = get_seconds();
6091
6092 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006093 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006094 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 mddev->raid_disks = info->raid_disks;
6096 /* don't set md_minor, it is determined by which /dev/md* was
6097 * openned
6098 */
6099 if (info->state & (1<<MD_SB_CLEAN))
6100 mddev->recovery_cp = MaxSector;
6101 else
6102 mddev->recovery_cp = 0;
6103 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006104 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
6106 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006107 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
6109 mddev->max_disks = MD_SB_DISKS;
6110
NeilBrowne6910632008-02-06 01:39:51 -08006111 if (mddev->persistent)
6112 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006113 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
NeilBrownc3d97142009-12-14 12:49:52 +11006115 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006116 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006117 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006118
NeilBrownf6705572006-03-27 01:18:11 -08006119 mddev->reshape_position = MaxSector;
6120
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 /*
6122 * Generate a 128 bit UUID
6123 */
6124 get_random_bytes(mddev->uuid, 16);
6125
NeilBrownf6705572006-03-27 01:18:11 -08006126 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006127 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006128 mddev->new_layout = mddev->layout;
6129 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006130 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006131
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 return 0;
6133}
6134
NeilBrownfd01b882011-10-11 16:47:53 +11006135void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006136{
Dan Williamsb522adc2009-03-31 15:00:31 +11006137 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6138
6139 if (mddev->external_size)
6140 return;
6141
Dan Williams1f403622009-03-31 14:59:03 +11006142 mddev->array_sectors = array_sectors;
6143}
6144EXPORT_SYMBOL(md_set_array_sectors);
6145
NeilBrownfd01b882011-10-11 16:47:53 +11006146static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006147{
NeilBrown3cb03002011-10-11 16:45:26 +11006148 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006149 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006150 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006151
6152 if (mddev->pers->resize == NULL)
6153 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006154 /* The "num_sectors" is the number of sectors of each device that
6155 * is used. This can only make sense for arrays with redundancy.
6156 * linear and raid0 always use whatever space is available. We can only
6157 * consider changing this number if no resync or reconstruction is
6158 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006159 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006160 * of each device. If num_sectors is zero, we find the largest size
6161 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006162 */
6163 if (mddev->sync_thread)
6164 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006165
NeilBrowndafb20f2012-03-19 12:46:39 +11006166 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006167 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006168
Andre Nolld71f9f82008-07-11 22:02:22 +10006169 if (fit && (num_sectors == 0 || num_sectors > avail))
6170 num_sectors = avail;
6171 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006172 return -ENOSPC;
6173 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006174 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006175 if (!rv)
6176 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006177 return rv;
6178}
6179
NeilBrownfd01b882011-10-11 16:47:53 +11006180static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006181{
6182 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006183 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006184 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006185 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006186 return -EINVAL;
6187 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006188 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006189 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006190 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006191 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006192
6193 rdev_for_each(rdev, mddev) {
6194 if (mddev->raid_disks < raid_disks &&
6195 rdev->data_offset < rdev->new_data_offset)
6196 return -EINVAL;
6197 if (mddev->raid_disks > raid_disks &&
6198 rdev->data_offset > rdev->new_data_offset)
6199 return -EINVAL;
6200 }
6201
NeilBrown63c70c42006-03-27 01:18:13 -08006202 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006203 if (mddev->delta_disks < 0)
6204 mddev->reshape_backwards = 1;
6205 else if (mddev->delta_disks > 0)
6206 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006207
6208 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006209 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006210 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006211 mddev->reshape_backwards = 0;
6212 }
NeilBrownda943b992006-01-06 00:20:54 -08006213 return rv;
6214}
6215
6216
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217/*
6218 * update_array_info is used to change the configuration of an
6219 * on-line array.
6220 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6221 * fields in the info are checked against the array.
6222 * Any differences that cannot be handled will cause an error.
6223 * Normally, only one change can be managed at a time.
6224 */
NeilBrownfd01b882011-10-11 16:47:53 +11006225static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226{
6227 int rv = 0;
6228 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006229 int state = 0;
6230
6231 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006232 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006233 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
6235 if (mddev->major_version != info->major_version ||
6236 mddev->minor_version != info->minor_version ||
6237/* mddev->patch_version != info->patch_version || */
6238 mddev->ctime != info->ctime ||
6239 mddev->level != info->level ||
6240/* mddev->layout != info->layout || */
6241 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006242 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006243 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6244 ((state^info->state) & 0xfffffe00)
6245 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return -EINVAL;
6247 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006248 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6249 cnt++;
6250 if (mddev->raid_disks != info->raid_disks)
6251 cnt++;
6252 if (mddev->layout != info->layout)
6253 cnt++;
6254 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6255 cnt++;
6256 if (cnt == 0)
6257 return 0;
6258 if (cnt > 1)
6259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 if (mddev->layout != info->layout) {
6262 /* Change layout
6263 * we don't need to do anything at the md level, the
6264 * personality will take care of it all.
6265 */
NeilBrown50ac1682009-06-18 08:47:55 +10006266 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006268 else {
6269 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006270 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006271 if (rv)
6272 mddev->new_layout = mddev->layout;
6273 return rv;
6274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006276 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006277 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278
NeilBrownda943b992006-01-06 00:20:54 -08006279 if (mddev->raid_disks != info->raid_disks)
6280 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
NeilBrown36fa3062005-09-09 16:23:45 -07006282 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6283 if (mddev->pers->quiesce == NULL)
6284 return -EINVAL;
6285 if (mddev->recovery || mddev->sync_thread)
6286 return -EBUSY;
6287 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6288 /* add the bitmap */
6289 if (mddev->bitmap)
6290 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006291 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006292 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006293 mddev->bitmap_info.offset =
6294 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006295 mddev->bitmap_info.space =
6296 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006297 mddev->pers->quiesce(mddev, 1);
6298 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006299 if (!rv)
6300 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006301 if (rv)
6302 bitmap_destroy(mddev);
6303 mddev->pers->quiesce(mddev, 0);
6304 } else {
6305 /* remove the bitmap */
6306 if (!mddev->bitmap)
6307 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006308 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006309 return -EINVAL;
6310 mddev->pers->quiesce(mddev, 1);
6311 bitmap_destroy(mddev);
6312 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006313 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006314 }
6315 }
NeilBrown850b2b42006-10-03 01:15:46 -07006316 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 return rv;
6318}
6319
NeilBrownfd01b882011-10-11 16:47:53 +11006320static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321{
NeilBrown3cb03002011-10-11 16:45:26 +11006322 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006323 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
6325 if (mddev->pers == NULL)
6326 return -ENODEV;
6327
NeilBrown1ca69c42012-10-11 13:37:33 +11006328 rcu_read_lock();
6329 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006331 err = -ENODEV;
6332 else {
6333 md_error(mddev, rdev);
6334 if (!test_bit(Faulty, &rdev->flags))
6335 err = -EBUSY;
6336 }
6337 rcu_read_unlock();
6338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339}
6340
Andre Noll2f9618c2008-04-25 18:57:58 +02006341/*
6342 * We have a problem here : there is no easy way to give a CHS
6343 * virtual geometry. We currently pretend that we have a 2 heads
6344 * 4 sectors (with a BIG number of cylinders...). This drives
6345 * dosfs just mad... ;-)
6346 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006347static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6348{
NeilBrownfd01b882011-10-11 16:47:53 +11006349 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006350
6351 geo->heads = 2;
6352 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006353 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006354 return 0;
6355}
6356
Al Viroa39907f2008-03-02 10:31:15 -05006357static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 unsigned int cmd, unsigned long arg)
6359{
6360 int err = 0;
6361 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006362 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006363 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
NeilBrown506c9e42011-12-23 10:17:26 +11006365 switch (cmd) {
6366 case RAID_VERSION:
6367 case GET_ARRAY_INFO:
6368 case GET_DISK_INFO:
6369 break;
6370 default:
6371 if (!capable(CAP_SYS_ADMIN))
6372 return -EACCES;
6373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 /*
6376 * Commands dealing with the RAID driver but not any
6377 * particular array:
6378 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006379 switch (cmd) {
6380 case RAID_VERSION:
6381 err = get_version(argp);
6382 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
NeilBrownc02c0ae2012-12-11 13:39:21 +11006384 case PRINT_RAID_DEBUG:
6385 err = 0;
6386 md_print_devices();
6387 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
6389#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006390 case RAID_AUTORUN:
6391 err = 0;
6392 autostart_arrays(arg);
6393 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006395 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 }
6397
6398 /*
6399 * Commands creating/starting a new array:
6400 */
6401
Al Viroa39907f2008-03-02 10:31:15 -05006402 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
6404 if (!mddev) {
6405 BUG();
6406 goto abort;
6407 }
6408
NeilBrown1ca69c42012-10-11 13:37:33 +11006409 /* Some actions do not requires the mutex */
6410 switch (cmd) {
6411 case GET_ARRAY_INFO:
6412 if (!mddev->raid_disks && !mddev->external)
6413 err = -ENODEV;
6414 else
6415 err = get_array_info(mddev, argp);
6416 goto abort;
6417
6418 case GET_DISK_INFO:
6419 if (!mddev->raid_disks && !mddev->external)
6420 err = -ENODEV;
6421 else
6422 err = get_disk_info(mddev, argp);
6423 goto abort;
6424
6425 case SET_DISK_FAULTY:
6426 err = set_disk_faulty(mddev, new_decode_dev(arg));
6427 goto abort;
6428 }
6429
NeilBrowna7a3f082012-12-11 13:35:54 +11006430 if (cmd == ADD_NEW_DISK)
6431 /* need to ensure md_delayed_delete() has completed */
6432 flush_workqueue(md_misc_wq);
6433
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006434 if (cmd == HOT_REMOVE_DISK)
6435 /* need to ensure recovery thread has run */
6436 wait_event_interruptible_timeout(mddev->sb_wait,
6437 !test_bit(MD_RECOVERY_NEEDED,
6438 &mddev->flags),
6439 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006440 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6441 /* Need to flush page cache, and ensure no-one else opens
6442 * and writes
6443 */
6444 mutex_lock(&mddev->open_mutex);
6445 if (atomic_read(&mddev->openers) > 1) {
6446 mutex_unlock(&mddev->open_mutex);
6447 err = -EBUSY;
6448 goto abort;
6449 }
6450 set_bit(MD_STILL_CLOSED, &mddev->flags);
6451 mutex_unlock(&mddev->open_mutex);
6452 sync_blockdev(bdev);
6453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 err = mddev_lock(mddev);
6455 if (err) {
6456 printk(KERN_INFO
6457 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6458 err, cmd);
6459 goto abort;
6460 }
6461
NeilBrownc02c0ae2012-12-11 13:39:21 +11006462 if (cmd == SET_ARRAY_INFO) {
6463 mdu_array_info_t info;
6464 if (!arg)
6465 memset(&info, 0, sizeof(info));
6466 else if (copy_from_user(&info, argp, sizeof(info))) {
6467 err = -EFAULT;
6468 goto abort_unlock;
6469 }
6470 if (mddev->pers) {
6471 err = update_array_info(mddev, &info);
6472 if (err) {
6473 printk(KERN_WARNING "md: couldn't update"
6474 " array info. %d\n", err);
6475 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 }
6477 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006478 }
6479 if (!list_empty(&mddev->disks)) {
6480 printk(KERN_WARNING
6481 "md: array %s already has disks!\n",
6482 mdname(mddev));
6483 err = -EBUSY;
6484 goto abort_unlock;
6485 }
6486 if (mddev->raid_disks) {
6487 printk(KERN_WARNING
6488 "md: array %s already initialised!\n",
6489 mdname(mddev));
6490 err = -EBUSY;
6491 goto abort_unlock;
6492 }
6493 err = set_array_info(mddev, &info);
6494 if (err) {
6495 printk(KERN_WARNING "md: couldn't set"
6496 " array info. %d\n", err);
6497 goto abort_unlock;
6498 }
6499 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 }
6501
6502 /*
6503 * Commands querying/configuring an existing array:
6504 */
NeilBrown32a76272005-06-21 17:17:14 -07006505 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006506 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006507 if ((!mddev->raid_disks && !mddev->external)
6508 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6509 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6510 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 err = -ENODEV;
6512 goto abort_unlock;
6513 }
6514
6515 /*
6516 * Commands even a read-only array can execute:
6517 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006518 switch (cmd) {
6519 case GET_BITMAP_FILE:
6520 err = get_bitmap_file(mddev, argp);
6521 goto done_unlock;
6522
6523 case RESTART_ARRAY_RW:
6524 err = restart_array(mddev);
6525 goto done_unlock;
6526
6527 case STOP_ARRAY:
6528 err = do_md_stop(mddev, 0, bdev);
6529 goto done_unlock;
6530
6531 case STOP_ARRAY_RO:
6532 err = md_set_readonly(mddev, bdev);
6533 goto done_unlock;
6534
NeilBrown3ea8929d2013-04-24 11:42:41 +10006535 case HOT_REMOVE_DISK:
6536 err = hot_remove_disk(mddev, new_decode_dev(arg));
6537 goto done_unlock;
6538
NeilBrown7ceb17e2013-04-24 11:42:42 +10006539 case ADD_NEW_DISK:
6540 /* We can support ADD_NEW_DISK on read-only arrays
6541 * on if we are re-adding a preexisting device.
6542 * So require mddev->pers and MD_DISK_SYNC.
6543 */
6544 if (mddev->pers) {
6545 mdu_disk_info_t info;
6546 if (copy_from_user(&info, argp, sizeof(info)))
6547 err = -EFAULT;
6548 else if (!(info.state & (1<<MD_DISK_SYNC)))
6549 /* Need to clear read-only for this */
6550 break;
6551 else
6552 err = add_new_disk(mddev, &info);
6553 goto done_unlock;
6554 }
6555 break;
6556
NeilBrownc02c0ae2012-12-11 13:39:21 +11006557 case BLKROSET:
6558 if (get_user(ro, (int __user *)(arg))) {
6559 err = -EFAULT;
6560 goto done_unlock;
6561 }
6562 err = -EINVAL;
6563
6564 /* if the bdev is going readonly the value of mddev->ro
6565 * does not matter, no writes are coming
6566 */
6567 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006568 goto done_unlock;
6569
NeilBrownc02c0ae2012-12-11 13:39:21 +11006570 /* are we are already prepared for writes? */
6571 if (mddev->ro != 1)
6572 goto done_unlock;
6573
6574 /* transitioning to readauto need only happen for
6575 * arrays that call md_write_start
6576 */
6577 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006579 if (err == 0) {
6580 mddev->ro = 2;
6581 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006582 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006583 }
6584 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 }
6586
6587 /*
6588 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006589 * superblock, so we do not allow them on read-only arrays.
6590 * However non-MD ioctls (e.g. get-size) will still come through
6591 * here and hit the 'default' below, so only disallow
6592 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006594 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006595 if (mddev->ro == 2) {
6596 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006597 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006598 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006599 /* mddev_unlock will wake thread */
6600 /* If a device failed while we were read-only, we
6601 * need to make sure the metadata is updated now.
6602 */
6603 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6604 mddev_unlock(mddev);
6605 wait_event(mddev->sb_wait,
6606 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6607 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006608 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006609 }
NeilBrownf91de922005-11-08 21:39:36 -08006610 } else {
6611 err = -EROFS;
6612 goto abort_unlock;
6613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 }
6615
NeilBrownc02c0ae2012-12-11 13:39:21 +11006616 switch (cmd) {
6617 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006619 mdu_disk_info_t info;
6620 if (copy_from_user(&info, argp, sizeof(info)))
6621 err = -EFAULT;
6622 else
6623 err = add_new_disk(mddev, &info);
6624 goto done_unlock;
6625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626
NeilBrownc02c0ae2012-12-11 13:39:21 +11006627 case HOT_ADD_DISK:
6628 err = hot_add_disk(mddev, new_decode_dev(arg));
6629 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
NeilBrownc02c0ae2012-12-11 13:39:21 +11006631 case RUN_ARRAY:
6632 err = do_md_run(mddev);
6633 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634
NeilBrownc02c0ae2012-12-11 13:39:21 +11006635 case SET_BITMAP_FILE:
6636 err = set_bitmap_file(mddev, (int)arg);
6637 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006638
NeilBrownc02c0ae2012-12-11 13:39:21 +11006639 default:
6640 err = -EINVAL;
6641 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 }
6643
6644done_unlock:
6645abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006646 if (mddev->hold_active == UNTIL_IOCTL &&
6647 err != -EINVAL)
6648 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 mddev_unlock(mddev);
6650
6651 return err;
6652done:
6653 if (err)
6654 MD_BUG();
6655abort:
6656 return err;
6657}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006658#ifdef CONFIG_COMPAT
6659static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6660 unsigned int cmd, unsigned long arg)
6661{
6662 switch (cmd) {
6663 case HOT_REMOVE_DISK:
6664 case HOT_ADD_DISK:
6665 case SET_DISK_FAULTY:
6666 case SET_BITMAP_FILE:
6667 /* These take in integer arg, do not convert */
6668 break;
6669 default:
6670 arg = (unsigned long)compat_ptr(arg);
6671 break;
6672 }
6673
6674 return md_ioctl(bdev, mode, cmd, arg);
6675}
6676#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
Al Viroa39907f2008-03-02 10:31:15 -05006678static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679{
6680 /*
6681 * Succeed if we can lock the mddev, which confirms that
6682 * it isn't being stopped right now.
6683 */
NeilBrownfd01b882011-10-11 16:47:53 +11006684 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 int err;
6686
Yuanhan Liu0c098222012-05-22 13:55:32 +10006687 if (!mddev)
6688 return -ENODEV;
6689
NeilBrownd3374822009-01-09 08:31:10 +11006690 if (mddev->gendisk != bdev->bd_disk) {
6691 /* we are racing with mddev_put which is discarding this
6692 * bd_disk.
6693 */
6694 mddev_put(mddev);
6695 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006696 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006697 /* Then retry the open from the top */
6698 return -ERESTARTSYS;
6699 }
6700 BUG_ON(mddev != bdev->bd_disk->private_data);
6701
NeilBrownc8c00a62009-08-10 12:50:52 +10006702 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 goto out;
6704
6705 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006706 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006707 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006708 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006710 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 out:
6712 return err;
6713}
6714
Al Virodb2a1442013-05-05 21:52:57 -04006715static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
NeilBrownfd01b882011-10-11 16:47:53 +11006717 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006719 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006720 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006723
6724static int md_media_changed(struct gendisk *disk)
6725{
NeilBrownfd01b882011-10-11 16:47:53 +11006726 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006727
6728 return mddev->changed;
6729}
6730
6731static int md_revalidate(struct gendisk *disk)
6732{
NeilBrownfd01b882011-10-11 16:47:53 +11006733 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006734
6735 mddev->changed = 0;
6736 return 0;
6737}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006738static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739{
6740 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006741 .open = md_open,
6742 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006743 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006744#ifdef CONFIG_COMPAT
6745 .compat_ioctl = md_compat_ioctl,
6746#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006747 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006748 .media_changed = md_media_changed,
6749 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750};
6751
Adrian Bunk75c96f82005-05-05 16:16:09 -07006752static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753{
NeilBrown2b8bf342011-10-11 16:48:23 +11006754 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 /*
6757 * md_thread is a 'system-thread', it's priority should be very
6758 * high. We avoid resource deadlocks individually in each
6759 * raid personality. (RAID5 does preallocation) We also use RR and
6760 * the very same RT priority as kswapd, thus we will never get
6761 * into a priority inversion deadlock.
6762 *
6763 * we definitely have to have equal or higher priority than
6764 * bdflush, otherwise bdflush will deadlock if there are too
6765 * many dirty RAID5 blocks.
6766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
NeilBrown6985c432005-10-19 21:23:47 -07006768 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006769 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
NeilBrown93588e22005-11-15 00:09:12 -08006771 /* We need to wait INTERRUPTIBLE so that
6772 * we don't add to the load-average.
6773 * That means we need to be sure no signals are
6774 * pending
6775 */
6776 if (signal_pending(current))
6777 flush_signals(current);
6778
6779 wait_event_interruptible_timeout
6780 (thread->wqueue,
6781 test_bit(THREAD_WAKEUP, &thread->flags)
6782 || kthread_should_stop(),
6783 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
NeilBrown6c987912011-01-14 09:13:53 +11006785 clear_bit(THREAD_WAKEUP, &thread->flags);
6786 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006787 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006789
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 return 0;
6791}
6792
NeilBrown2b8bf342011-10-11 16:48:23 +11006793void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794{
6795 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006796 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 set_bit(THREAD_WAKEUP, &thread->flags);
6798 wake_up(&thread->wqueue);
6799 }
6800}
6801
Shaohua Li4ed87312012-10-11 13:34:00 +11006802struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6803 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804{
NeilBrown2b8bf342011-10-11 16:48:23 +11006805 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
NeilBrown2b8bf342011-10-11 16:48:23 +11006807 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 if (!thread)
6809 return NULL;
6810
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 init_waitqueue_head(&thread->wqueue);
6812
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 thread->run = run;
6814 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006815 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006816 thread->tsk = kthread_run(md_thread, thread,
6817 "%s_%s",
6818 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006819 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006820 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 kfree(thread);
6822 return NULL;
6823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 return thread;
6825}
6826
NeilBrown2b8bf342011-10-11 16:48:23 +11006827void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
NeilBrown2b8bf342011-10-11 16:48:23 +11006829 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006830 if (!thread)
6831 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006832 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006833 /* Locking ensures that mddev_unlock does not wake_up a
6834 * non-existent thread
6835 */
6836 spin_lock(&pers_lock);
6837 *threadp = NULL;
6838 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006839
6840 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 kfree(thread);
6842}
6843
NeilBrownfd01b882011-10-11 16:47:53 +11006844void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845{
6846 if (!mddev) {
6847 MD_BUG();
6848 return;
6849 }
6850
NeilBrownb2d444d2005-11-08 21:39:31 -08006851 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006853
NeilBrownde393cd2011-07-28 11:31:48 +10006854 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 return;
6856 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006857 if (mddev->degraded)
6858 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006859 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6862 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006863 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006864 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006865 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866}
6867
6868/* seq_file implementation /proc/mdstat */
6869
6870static void status_unused(struct seq_file *seq)
6871{
6872 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006873 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
6875 seq_printf(seq, "unused devices: ");
6876
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006877 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 char b[BDEVNAME_SIZE];
6879 i++;
6880 seq_printf(seq, "%s ",
6881 bdevname(rdev->bdev,b));
6882 }
6883 if (!i)
6884 seq_printf(seq, "<none>");
6885
6886 seq_printf(seq, "\n");
6887}
6888
6889
NeilBrownfd01b882011-10-11 16:47:53 +11006890static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891{
NeilBrowndd71cf62009-05-07 12:49:35 +10006892 sector_t max_sectors, resync, res;
6893 unsigned long dt, db;
6894 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006895 int scale;
6896 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
NeilBrown72f36d52012-10-11 14:25:57 +11006898 if (mddev->curr_resync <= 3)
6899 resync = 0;
6900 else
6901 resync = mddev->curr_resync
6902 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
NeilBrownc804cde2012-05-21 09:28:33 +10006904 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6905 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006906 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006908 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
6910 /*
6911 * Should not happen.
6912 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006913 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 MD_BUG();
6915 return;
6916 }
NeilBrown4588b422006-03-27 01:18:04 -08006917 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006918 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006919 * u32, as those are the requirements for sector_div.
6920 * Thus 'scale' must be at least 10
6921 */
6922 scale = 10;
6923 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006924 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006925 scale++;
6926 }
6927 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006928 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006929
6930 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 {
NeilBrown4588b422006-03-27 01:18:04 -08006932 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 seq_printf(seq, "[");
6934 for (i = 0; i < x; i++)
6935 seq_printf(seq, "=");
6936 seq_printf(seq, ">");
6937 for (i = 0; i < y; i++)
6938 seq_printf(seq, ".");
6939 seq_printf(seq, "] ");
6940 }
NeilBrown4588b422006-03-27 01:18:04 -08006941 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006942 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6943 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006944 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6945 "check" :
6946 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6947 "resync" : "recovery"))),
6948 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006949 (unsigned long long) resync/2,
6950 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 * dt: time from mark until now
6954 * db: blocks written from mark until now
6955 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006956 *
6957 * rt is a sector_t, so could be 32bit or 64bit.
6958 * So we divide before multiply in case it is 32bit and close
6959 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006960 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006961 * near the end of resync when the number of remaining sectors
6962 * is close to 'db'.
6963 * We then divide rt by 32 after multiplying by db to compensate.
6964 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 */
6966 dt = ((jiffies - mddev->resync_mark) / HZ);
6967 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006968 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6969 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
NeilBrowndd71cf62009-05-07 12:49:35 +10006971 rt = max_sectors - resync; /* number of remaining sectors */
6972 sector_div(rt, db/32+1);
6973 rt *= dt;
6974 rt >>= 5;
6975
6976 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6977 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
NeilBrownff4e8d92006-07-10 04:44:16 -07006979 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980}
6981
6982static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6983{
6984 struct list_head *tmp;
6985 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006986 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
6988 if (l >= 0x10000)
6989 return NULL;
6990 if (!l--)
6991 /* header */
6992 return (void*)1;
6993
6994 spin_lock(&all_mddevs_lock);
6995 list_for_each(tmp,&all_mddevs)
6996 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006997 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 mddev_get(mddev);
6999 spin_unlock(&all_mddevs_lock);
7000 return mddev;
7001 }
7002 spin_unlock(&all_mddevs_lock);
7003 if (!l--)
7004 return (void*)2;/* tail */
7005 return NULL;
7006}
7007
7008static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7009{
7010 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007011 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
7013 ++*pos;
7014 if (v == (void*)2)
7015 return NULL;
7016
7017 spin_lock(&all_mddevs_lock);
7018 if (v == (void*)1)
7019 tmp = all_mddevs.next;
7020 else
7021 tmp = mddev->all_mddevs.next;
7022 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007023 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 else {
7025 next_mddev = (void*)2;
7026 *pos = 0x10000;
7027 }
7028 spin_unlock(&all_mddevs_lock);
7029
7030 if (v != (void*)1)
7031 mddev_put(mddev);
7032 return next_mddev;
7033
7034}
7035
7036static void md_seq_stop(struct seq_file *seq, void *v)
7037{
NeilBrownfd01b882011-10-11 16:47:53 +11007038 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
7040 if (mddev && v != (void*)1 && v != (void*)2)
7041 mddev_put(mddev);
7042}
7043
7044static int md_seq_show(struct seq_file *seq, void *v)
7045{
NeilBrownfd01b882011-10-11 16:47:53 +11007046 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007047 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007048 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
7050 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007051 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 seq_printf(seq, "Personalities : ");
7053 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007054 list_for_each_entry(pers, &pers_list, list)
7055 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
7057 spin_unlock(&pers_lock);
7058 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007059 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 return 0;
7061 }
7062 if (v == (void*)2) {
7063 status_unused(seq);
7064 return 0;
7065 }
7066
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007067 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007069
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7071 seq_printf(seq, "%s : %sactive", mdname(mddev),
7072 mddev->pers ? "" : "in");
7073 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007074 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007076 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007077 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 seq_printf(seq, " %s", mddev->pers->name);
7079 }
7080
Andre Nolldd8ac332009-03-31 14:33:13 +11007081 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007082 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 char b[BDEVNAME_SIZE];
7084 seq_printf(seq, " %s[%d]",
7085 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007086 if (test_bit(WriteMostly, &rdev->flags))
7087 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007088 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 seq_printf(seq, "(F)");
7090 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007091 }
7092 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007093 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007094 if (test_bit(Replacement, &rdev->flags))
7095 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007096 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 }
7098
7099 if (!list_empty(&mddev->disks)) {
7100 if (mddev->pers)
7101 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007102 (unsigned long long)
7103 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 else
7105 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007106 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007108 if (mddev->persistent) {
7109 if (mddev->major_version != 0 ||
7110 mddev->minor_version != 90) {
7111 seq_printf(seq," super %d.%d",
7112 mddev->major_version,
7113 mddev->minor_version);
7114 }
NeilBrowne6910632008-02-06 01:39:51 -08007115 } else if (mddev->external)
7116 seq_printf(seq, " super external:%s",
7117 mddev->metadata_type);
7118 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007119 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
7121 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007122 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007124 if (mddev->pers->sync_request) {
7125 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007126 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007127 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007128 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007129 seq_printf(seq, "\tresync=DELAYED\n ");
7130 else if (mddev->recovery_cp < MaxSector)
7131 seq_printf(seq, "\tresync=PENDING\n ");
7132 }
NeilBrown32a76272005-06-21 17:17:14 -07007133 } else
7134 seq_printf(seq, "\n ");
7135
NeilBrown57148962012-03-19 12:46:40 +11007136 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137
7138 seq_printf(seq, "\n");
7139 }
7140 mddev_unlock(mddev);
7141
7142 return 0;
7143}
7144
Jan Engelhardt110518b2009-05-07 12:49:37 +10007145static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 .start = md_seq_start,
7147 .next = md_seq_next,
7148 .stop = md_seq_stop,
7149 .show = md_seq_show,
7150};
7151
7152static int md_seq_open(struct inode *inode, struct file *file)
7153{
Kay Sieversf1514632011-07-12 20:48:39 +02007154 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 int error;
7156
7157 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007158 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007159 return error;
7160
7161 seq = file->private_data;
7162 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 return error;
7164}
7165
NeilBrownd7603b72006-01-06 00:20:30 -08007166static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7167{
Kay Sieversf1514632011-07-12 20:48:39 +02007168 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007169 int mask;
7170
7171 poll_wait(filp, &md_event_waiters, wait);
7172
7173 /* always allow read */
7174 mask = POLLIN | POLLRDNORM;
7175
Kay Sieversf1514632011-07-12 20:48:39 +02007176 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007177 mask |= POLLERR | POLLPRI;
7178 return mask;
7179}
7180
Arjan van de Venfa027c22007-02-12 00:55:33 -08007181static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007182 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 .open = md_seq_open,
7184 .read = seq_read,
7185 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007186 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007187 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188};
7189
NeilBrown84fc4b52011-10-11 16:49:58 +11007190int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007193 list_add_tail(&p->list, &pers_list);
7194 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 spin_unlock(&pers_lock);
7196 return 0;
7197}
7198
NeilBrown84fc4b52011-10-11 16:49:58 +11007199int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200{
NeilBrown2604b702006-01-06 00:20:36 -08007201 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007203 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 spin_unlock(&pers_lock);
7205 return 0;
7206}
7207
NeilBrownfd01b882011-10-11 16:47:53 +11007208static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209{
NeilBrown3cb03002011-10-11 16:45:26 +11007210 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007212 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
7214 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007215 rcu_read_lock();
7216 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007218 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7219 (int)part_stat_read(&disk->part0, sectors[1]) -
7220 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007221 /* sync IO will cause sync_io to increase before the disk_stats
7222 * as sync_io is counted when a request starts, and
7223 * disk_stats is counted when it completes.
7224 * So resync activity will cause curr_events to be smaller than
7225 * when there was no such activity.
7226 * non-sync IO will cause disk_stat to increase without
7227 * increasing sync_io so curr_events will (eventually)
7228 * be larger than it was before. Once it becomes
7229 * substantially larger, the test below will cause
7230 * the array to appear non-idle, and resync will slow
7231 * down.
7232 * If there is a lot of outstanding resync activity when
7233 * we set last_event to curr_events, then all that activity
7234 * completing might cause the array to appear non-idle
7235 * and resync will be slowed down even though there might
7236 * not have been non-resync activity. This will only
7237 * happen once though. 'last_events' will soon reflect
7238 * the state where there is little or no outstanding
7239 * resync requests, and further resync activity will
7240 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007243 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 rdev->last_events = curr_events;
7245 idle = 0;
7246 }
7247 }
NeilBrown4b809912008-07-21 17:05:25 +10007248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 return idle;
7250}
7251
NeilBrownfd01b882011-10-11 16:47:53 +11007252void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253{
7254 /* another "blocks" (512byte) blocks have been synced */
7255 atomic_sub(blocks, &mddev->recovery_active);
7256 wake_up(&mddev->recovery_wait);
7257 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007258 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007259 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 md_wakeup_thread(mddev->thread);
7261 // stop recovery, signal do_sync ....
7262 }
7263}
7264
7265
NeilBrown06d91a52005-06-21 17:17:12 -07007266/* md_write_start(mddev, bi)
7267 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007268 * in superblock) before writing, schedule a superblock update
7269 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007270 */
NeilBrownfd01b882011-10-11 16:47:53 +11007271void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272{
Neil Brown0fd62b82008-06-28 08:31:36 +10007273 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007274 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007275 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007276
NeilBrownf91de922005-11-08 21:39:36 -08007277 BUG_ON(mddev->ro == 1);
7278 if (mddev->ro == 2) {
7279 /* need to switch to read/write */
7280 mddev->ro = 0;
7281 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7282 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007283 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007284 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007285 }
NeilBrown06d91a52005-06-21 17:17:12 -07007286 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007287 if (mddev->safemode == 1)
7288 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007289 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007290 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007291 if (mddev->in_sync) {
7292 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007293 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007294 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007295 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007296 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007297 }
NeilBrowna9701a32005-11-08 21:39:34 -08007298 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007299 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007300 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007301 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007302 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007303 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304}
7305
NeilBrownfd01b882011-10-11 16:47:53 +11007306void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307{
7308 if (atomic_dec_and_test(&mddev->writes_pending)) {
7309 if (mddev->safemode == 2)
7310 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007311 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7313 }
7314}
7315
NeilBrown2a2275d2007-01-26 00:57:11 -08007316/* md_allow_write(mddev)
7317 * Calling this ensures that the array is marked 'active' so that writes
7318 * may proceed without blocking. It is important to call this before
7319 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7320 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007321 *
7322 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7323 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007324 */
NeilBrownfd01b882011-10-11 16:47:53 +11007325int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007326{
7327 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007328 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007329 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007330 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007331 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007332 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007333
7334 spin_lock_irq(&mddev->write_lock);
7335 if (mddev->in_sync) {
7336 mddev->in_sync = 0;
7337 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007338 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007339 if (mddev->safemode_delay &&
7340 mddev->safemode == 0)
7341 mddev->safemode = 1;
7342 spin_unlock_irq(&mddev->write_lock);
7343 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007344 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007345 } else
7346 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007347
NeilBrown070dc6d2010-08-30 17:33:34 +10007348 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007349 return -EAGAIN;
7350 else
7351 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007352}
7353EXPORT_SYMBOL_GPL(md_allow_write);
7354
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355#define SYNC_MARKS 10
7356#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007357#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007358void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359{
Shaohua Li4ed87312012-10-11 13:34:00 +11007360 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007361 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 unsigned int currspeed = 0,
7363 window;
NeilBrown57afd892005-06-21 17:17:13 -07007364 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007366 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 sector_t mark_cnt[SYNC_MARKS];
7368 int last_mark,m;
7369 struct list_head *tmp;
7370 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007371 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007372 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007373 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007374 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
7376 /* just incase thread restarts... */
7377 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7378 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007379 if (mddev->ro) /* never try to sync a read-only array */
7380 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrown61df9d92006-10-03 01:15:57 -07007382 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007383 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007384 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007385 action = "check";
7386 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007387 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007388 action = "repair";
7389 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007390 desc = "resync";
7391 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7392 desc = "reshape";
7393 else
7394 desc = "recovery";
7395
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007396 mddev->last_sync_action = action ?: desc;
7397
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 /* we overload curr_resync somewhat here.
7399 * 0 == not engaged in resync at all
7400 * 2 == checking that there is no conflict with another sync
7401 * 1 == like 2, but have yielded to allow conflicting resync to
7402 * commense
7403 * other == active in resync - this many blocks
7404 *
7405 * Before starting a resync we must have set curr_resync to
7406 * 2, and then checked that every "conflicting" array has curr_resync
7407 * less than ours. When we find one that is the same or higher
7408 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7409 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7410 * This will mean we have to start checking from the beginning again.
7411 *
7412 */
7413
7414 do {
7415 mddev->curr_resync = 2;
7416
7417 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007418 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007420 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 if (mddev2 == mddev)
7422 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007423 if (!mddev->parallel_resync
7424 && mddev2->curr_resync
7425 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 DEFINE_WAIT(wq);
7427 if (mddev < mddev2 && mddev->curr_resync == 2) {
7428 /* arbitrarily yield */
7429 mddev->curr_resync = 1;
7430 wake_up(&resync_wait);
7431 }
7432 if (mddev > mddev2 && mddev->curr_resync == 1)
7433 /* no need to wait here, we can wait the next
7434 * time 'round when curr_resync == 2
7435 */
7436 continue;
NeilBrown97441972008-09-19 11:49:54 +10007437 /* We need to wait 'interruptible' so as not to
7438 * contribute to the load average, and not to
7439 * be caught by 'softlockup'
7440 */
7441 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007442 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007443 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007444 printk(KERN_INFO "md: delaying %s of %s"
7445 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007447 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007449 if (signal_pending(current))
7450 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 schedule();
7452 finish_wait(&resync_wait, &wq);
7453 goto try_again;
7454 }
7455 finish_wait(&resync_wait, &wq);
7456 }
7457 }
7458 } while (mddev->curr_resync < 2);
7459
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007460 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007461 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007463 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 */
7465 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007466 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007467 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007468 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7469 j = mddev->resync_min;
7470 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007471 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007472
NeilBrownccfcc3c2006-03-27 01:18:09 -08007473 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007474 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007475 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007477 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007478 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007479 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007480 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007481 if (rdev->raid_disk >= 0 &&
7482 !test_bit(Faulty, &rdev->flags) &&
7483 !test_bit(In_sync, &rdev->flags) &&
7484 rdev->recovery_offset < j)
7485 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007486 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488
NeilBrown61df9d92006-10-03 01:15:57 -07007489 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7490 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7491 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007492 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007493 "(but not more than %d KB/sec) for %s.\n",
7494 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
NeilBrowneea1bf32009-03-31 14:27:02 +11007496 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007497
NeilBrown57afd892005-06-21 17:17:13 -07007498 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 for (m = 0; m < SYNC_MARKS; m++) {
7500 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007501 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 }
7503 last_mark = 0;
7504 mddev->resync_mark = mark[last_mark];
7505 mddev->resync_mark_cnt = mark_cnt[last_mark];
7506
7507 /*
7508 * Tune reconstruction:
7509 */
7510 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007511 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7512 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513
7514 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 last_check = 0;
7516
7517 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007518 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007519 "md: resuming %s of %s from checkpoint.\n",
7520 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007522 } else
7523 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007524 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007525 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7526 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007527 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528
majianpeng7c2c57c2012-07-03 12:12:26 +10007529 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007531 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
NeilBrown57afd892005-06-21 17:17:13 -07007533 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007534
NeilBrown7a91ee12009-05-26 12:57:21 +10007535 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7536 ((mddev->curr_resync > mddev->curr_resync_completed &&
7537 (mddev->curr_resync - mddev->curr_resync_completed)
7538 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007539 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007540 (j - mddev->curr_resync_completed)*2
7541 >= mddev->resync_max - mddev->curr_resync_completed
7542 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007543 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007544 wait_event(mddev->recovery_wait,
7545 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007546 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007547 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7548 j > mddev->recovery_cp)
7549 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007550 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007551 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007552 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007553 }
NeilBrownacb180b2009-04-14 16:28:34 +10007554
NeilBrownc91abf52013-11-19 12:02:01 +11007555 while (j >= mddev->resync_max &&
7556 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007557 /* As this condition is controlled by user-space,
7558 * we can block indefinitely, so use '_interruptible'
7559 * to avoid triggering warnings.
7560 */
7561 flush_signals(current); /* just in case */
7562 wait_event_interruptible(mddev->recovery_wait,
7563 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007564 || test_bit(MD_RECOVERY_INTR,
7565 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007566 }
NeilBrownacb180b2009-04-14 16:28:34 +10007567
NeilBrownc91abf52013-11-19 12:02:01 +11007568 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7569 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007570
NeilBrown57afd892005-06-21 17:17:13 -07007571 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007572 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007573 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007574 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 }
NeilBrown57afd892005-06-21 17:17:13 -07007577
7578 if (!skipped) { /* actual IO requested */
7579 io_sectors += sectors;
7580 atomic_add(sectors, &mddev->recovery_active);
7581 }
7582
NeilBrowne875ece2011-07-28 11:39:24 +10007583 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7584 break;
7585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007587 if (j > 2)
7588 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007589 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007590 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007591 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007592 * visible in /proc/mdstat
7593 */
7594 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007595
7596 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 continue;
7598
NeilBrown57afd892005-06-21 17:17:13 -07007599 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 repeat:
7601 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7602 /* step marks */
7603 int next = (last_mark+1) % SYNC_MARKS;
7604
7605 mddev->resync_mark = mark[next];
7606 mddev->resync_mark_cnt = mark_cnt[next];
7607 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007608 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 last_mark = next;
7610 }
7611
NeilBrownc91abf52013-11-19 12:02:01 +11007612 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
7615 /*
7616 * this loop exits only if either when we are slower than
7617 * the 'hard' speed limit, or the system was IO-idle for
7618 * a jiffy.
7619 * the system might be non-idle CPU-wise, but we only care
7620 * about not overloading the IO subsystem. (things like an
7621 * e2fsck being done on the RAID array should execute fast)
7622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 cond_resched();
7624
NeilBrown57afd892005-06-21 17:17:13 -07007625 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7626 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
NeilBrown88202a02006-01-06 00:21:36 -08007628 if (currspeed > speed_min(mddev)) {
7629 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007630 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007631 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 goto repeat;
7633 }
7634 }
7635 }
NeilBrownc91abf52013-11-19 12:02:01 +11007636 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7637 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7638 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 /*
7640 * this also signals 'finished resyncing' to md_stop
7641 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007642 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7644
7645 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007646 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
NeilBrowndfc70642008-05-23 13:04:39 -07007648 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007649 mddev->curr_resync > 2) {
7650 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7651 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7652 if (mddev->curr_resync >= mddev->recovery_cp) {
7653 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007654 "md: checkpointing %s of %s.\n",
7655 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007656 if (test_bit(MD_RECOVERY_ERROR,
7657 &mddev->recovery))
7658 mddev->recovery_cp =
7659 mddev->curr_resync_completed;
7660 else
7661 mddev->recovery_cp =
7662 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007663 }
7664 } else
7665 mddev->recovery_cp = MaxSector;
7666 } else {
7667 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7668 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007669 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007670 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007671 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007672 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007673 !test_bit(Faulty, &rdev->flags) &&
7674 !test_bit(In_sync, &rdev->flags) &&
7675 rdev->recovery_offset < mddev->curr_resync)
7676 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007677 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007680 skip:
NeilBrown17571282006-12-10 02:20:52 -08007681 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
NeilBrownc07b70a2009-12-14 12:49:48 +11007683 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7684 /* We completed so min/max setting can be forgotten if used. */
7685 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7686 mddev->resync_min = 0;
7687 mddev->resync_max = MaxSector;
7688 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7689 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 mddev->curr_resync = 0;
7691 wake_up(&resync_wait);
7692 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7693 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695}
NeilBrown29269552006-03-27 01:18:10 -08007696EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
NeilBrown746d3202013-04-24 11:42:41 +10007698static int remove_and_add_spares(struct mddev *mddev,
7699 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007700{
NeilBrown3cb03002011-10-11 16:45:26 +11007701 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007702 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007703 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007704
NeilBrowndafb20f2012-03-19 12:46:39 +11007705 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007706 if ((this == NULL || rdev == this) &&
7707 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007708 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007709 (test_bit(Faulty, &rdev->flags) ||
7710 ! test_bit(In_sync, &rdev->flags)) &&
7711 atomic_read(&rdev->nr_pending)==0) {
7712 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007713 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007714 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007715 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007716 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007717 }
7718 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007719 if (removed && mddev->kobj.sd)
7720 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007721
NeilBrown746d3202013-04-24 11:42:41 +10007722 if (this)
7723 goto no_add;
7724
NeilBrowndafb20f2012-03-19 12:46:39 +11007725 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007726 if (rdev->raid_disk >= 0 &&
7727 !test_bit(In_sync, &rdev->flags) &&
7728 !test_bit(Faulty, &rdev->flags))
7729 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007730 if (rdev->raid_disk >= 0)
7731 continue;
7732 if (test_bit(Faulty, &rdev->flags))
7733 continue;
7734 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007735 ! (rdev->saved_raid_disk >= 0 &&
7736 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007737 continue;
7738
7739 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007740 if (mddev->pers->
7741 hot_add_disk(mddev, rdev) == 0) {
7742 if (sysfs_link_rdev(mddev, rdev))
7743 /* failure here is OK */;
7744 spares++;
7745 md_new_event(mddev);
7746 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007747 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007748 }
NeilBrown746d3202013-04-24 11:42:41 +10007749no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007750 if (removed)
7751 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007752 return spares;
7753}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007754
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755/*
7756 * This routine is regularly called by all per-raid-array threads to
7757 * deal with generic issues like resync and super-block update.
7758 * Raid personalities that don't have a thread (linear/raid0) do not
7759 * need this as they never do any recovery or update the superblock.
7760 *
7761 * It does not do any resync itself, but rather "forks" off other threads
7762 * to do that as needed.
7763 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7764 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007765 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 * and wakeups up this thread which will reap the thread and finish up.
7767 * This thread also removes any faulty devices (with nr_pending == 0).
7768 *
7769 * The overall approach is:
7770 * 1/ if the superblock needs updating, update it.
7771 * 2/ If a recovery thread is running, don't do anything else.
7772 * 3/ If recovery has finished, clean up, possibly marking spares active.
7773 * 4/ If there are any faulty devices, remove them.
7774 * 5/ If array is degraded, try to add spares devices
7775 * 6/ If array has spares or is not in-sync, start a resync thread.
7776 */
NeilBrownfd01b882011-10-11 16:47:53 +11007777void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007778{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007779 if (mddev->suspended)
7780 return;
7781
NeilBrown5f404022005-06-21 17:17:16 -07007782 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007783 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784
NeilBrownfca4d842005-06-21 17:17:11 -07007785 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007786 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007787 printk(KERN_INFO "md: %s in immediate safe mode\n",
7788 mdname(mddev));
7789 mddev->safemode = 2;
7790 }
7791 flush_signals(current);
7792 }
7793
NeilBrownc89a8ee2008-08-05 15:54:13 +10007794 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007797 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007799 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007800 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007801 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7802 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 ))
7804 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007805
NeilBrowndf5b89b2006-03-27 01:18:20 -08007806 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007807 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007808
NeilBrownc89a8ee2008-08-05 15:54:13 +10007809 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007810 /* On a read-only array we can:
7811 * - remove failed devices
7812 * - add already-in_sync devices if the array itself
7813 * is in-sync.
7814 * As we only add devices that are already in-sync,
7815 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007816 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007817 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007818 /* There is no thread, but we need to call
7819 * ->spare_active and clear saved_raid_disk
7820 */
7821 md_reap_sync_thread(mddev);
7822 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007823 goto unlock;
7824 }
7825
NeilBrown31a59e32008-04-30 00:52:30 -07007826 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007827 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007828 spin_lock_irq(&mddev->write_lock);
7829 if (mddev->safemode &&
7830 !atomic_read(&mddev->writes_pending) &&
7831 !mddev->in_sync &&
7832 mddev->recovery_cp == MaxSector) {
7833 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007834 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007835 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007836 }
7837 if (mddev->safemode == 1)
7838 mddev->safemode = 0;
7839 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007840 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007841 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007842 }
NeilBrownfca4d842005-06-21 17:17:11 -07007843
NeilBrown7a0a5352013-08-27 16:28:23 +10007844 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007845 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007846
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7848 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7849 /* resync/recovery still happening */
7850 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7851 goto unlock;
7852 }
7853 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007854 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 goto unlock;
7856 }
Neil Brown72a23c22008-06-28 08:31:41 +10007857 /* Set RUNNING before clearing NEEDED to avoid
7858 * any transients in the value of "sync_action".
7859 */
NeilBrown72f36d52012-10-11 14:25:57 +11007860 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007861 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007862 /* Clear some bits that don't mean anything, but
7863 * might be left set
7864 */
NeilBrown24dd4692005-11-08 21:39:26 -08007865 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7866 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867
NeilBrowned209582012-04-24 10:23:14 +10007868 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7869 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007870 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 /* no recovery is running.
7872 * remove any failed drives, then
7873 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007874 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 * the personality to fail the re-add.
7876 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007878 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007879 if (mddev->pers->check_reshape == NULL ||
7880 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007881 /* Cannot proceed */
7882 goto unlock;
7883 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007884 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007885 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007886 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7887 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007888 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007889 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007890 } else if (mddev->recovery_cp < MaxSector) {
7891 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007892 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007893 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7894 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007896
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007898 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007899 /* We are adding a device or devices to an array
7900 * which has the bitmap stored on all devices.
7901 * So make sure all bitmap pages get written
7902 */
7903 bitmap_write_all(mddev->bitmap);
7904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905 mddev->sync_thread = md_register_thread(md_do_sync,
7906 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007907 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007908 if (!mddev->sync_thread) {
7909 printk(KERN_ERR "%s: could not start resync"
7910 " thread...\n",
7911 mdname(mddev));
7912 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007913 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7914 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7915 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7916 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7917 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007918 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007920 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007921 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 }
7923 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007924 wake_up(&mddev->sb_wait);
7925
Neil Brown72a23c22008-06-28 08:31:41 +10007926 if (!mddev->sync_thread) {
7927 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7928 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7929 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007930 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007931 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933 mddev_unlock(mddev);
7934 }
7935}
7936
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007937void md_reap_sync_thread(struct mddev *mddev)
7938{
7939 struct md_rdev *rdev;
7940
7941 /* resync has finished, collect result */
7942 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007943 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007944 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7945 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7946 /* success...*/
7947 /* activate any spares */
7948 if (mddev->pers->spare_active(mddev)) {
7949 sysfs_notify(&mddev->kobj, NULL,
7950 "degraded");
7951 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7952 }
7953 }
7954 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7955 mddev->pers->finish_reshape)
7956 mddev->pers->finish_reshape(mddev);
7957
7958 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007959 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007960 */
NeilBrownf4667222013-12-09 12:04:56 +11007961 if (!mddev->degraded)
7962 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007963 rdev->saved_raid_disk = -1;
7964
7965 md_update_sb(mddev, 1);
7966 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7967 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7968 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7969 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7970 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7971 /* flag recovery needed just to double check */
7972 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7973 sysfs_notify_dirent_safe(mddev->sysfs_action);
7974 md_new_event(mddev);
7975 if (mddev->event_work.func)
7976 queue_work(md_misc_wq, &mddev->event_work);
7977}
7978
NeilBrownfd01b882011-10-11 16:47:53 +11007979void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007980{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007981 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007982 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007983 !test_bit(Blocked, &rdev->flags) &&
7984 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007985 msecs_to_jiffies(5000));
7986 rdev_dec_pending(rdev, mddev);
7987}
7988EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7989
NeilBrownc6563a82012-05-21 09:27:00 +10007990void md_finish_reshape(struct mddev *mddev)
7991{
7992 /* called be personality module when reshape completes. */
7993 struct md_rdev *rdev;
7994
7995 rdev_for_each(rdev, mddev) {
7996 if (rdev->data_offset > rdev->new_data_offset)
7997 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7998 else
7999 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8000 rdev->data_offset = rdev->new_data_offset;
8001 }
8002}
8003EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008004
8005/* Bad block management.
8006 * We can record which blocks on each device are 'bad' and so just
8007 * fail those blocks, or that stripe, rather than the whole device.
8008 * Entries in the bad-block table are 64bits wide. This comprises:
8009 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8010 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8011 * A 'shift' can be set so that larger blocks are tracked and
8012 * consequently larger devices can be covered.
8013 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8014 *
8015 * Locking of the bad-block table uses a seqlock so md_is_badblock
8016 * might need to retry if it is very unlucky.
8017 * We will sometimes want to check for bad blocks in a bi_end_io function,
8018 * so we use the write_seqlock_irq variant.
8019 *
8020 * When looking for a bad block we specify a range and want to
8021 * know if any block in the range is bad. So we binary-search
8022 * to the last range that starts at-or-before the given endpoint,
8023 * (or "before the sector after the target range")
8024 * then see if it ends after the given start.
8025 * We return
8026 * 0 if there are no known bad blocks in the range
8027 * 1 if there are known bad block which are all acknowledged
8028 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8029 * plus the start/length of the first bad section we overlap.
8030 */
8031int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8032 sector_t *first_bad, int *bad_sectors)
8033{
8034 int hi;
majianpengab056132012-11-06 17:13:44 +08008035 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008036 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008037 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008038 sector_t target = s + sectors;
8039 unsigned seq;
8040
8041 if (bb->shift > 0) {
8042 /* round the start down, and the end up */
8043 s >>= bb->shift;
8044 target += (1<<bb->shift) - 1;
8045 target >>= bb->shift;
8046 sectors = target - s;
8047 }
8048 /* 'target' is now the first block after the bad range */
8049
8050retry:
8051 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008052 lo = 0;
8053 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008054 hi = bb->count;
8055
8056 /* Binary search between lo and hi for 'target'
8057 * i.e. for the last range that starts before 'target'
8058 */
8059 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8060 * are known not to be the last range before target.
8061 * VARIANT: hi-lo is the number of possible
8062 * ranges, and decreases until it reaches 1
8063 */
8064 while (hi - lo > 1) {
8065 int mid = (lo + hi) / 2;
8066 sector_t a = BB_OFFSET(p[mid]);
8067 if (a < target)
8068 /* This could still be the one, earlier ranges
8069 * could not. */
8070 lo = mid;
8071 else
8072 /* This and later ranges are definitely out. */
8073 hi = mid;
8074 }
8075 /* 'lo' might be the last that started before target, but 'hi' isn't */
8076 if (hi > lo) {
8077 /* need to check all range that end after 's' to see if
8078 * any are unacknowledged.
8079 */
8080 while (lo >= 0 &&
8081 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8082 if (BB_OFFSET(p[lo]) < target) {
8083 /* starts before the end, and finishes after
8084 * the start, so they must overlap
8085 */
8086 if (rv != -1 && BB_ACK(p[lo]))
8087 rv = 1;
8088 else
8089 rv = -1;
8090 *first_bad = BB_OFFSET(p[lo]);
8091 *bad_sectors = BB_LEN(p[lo]);
8092 }
8093 lo--;
8094 }
8095 }
8096
8097 if (read_seqretry(&bb->lock, seq))
8098 goto retry;
8099
8100 return rv;
8101}
8102EXPORT_SYMBOL_GPL(md_is_badblock);
8103
8104/*
8105 * Add a range of bad blocks to the table.
8106 * This might extend the table, or might contract it
8107 * if two adjacent ranges can be merged.
8108 * We binary-search to find the 'insertion' point, then
8109 * decide how best to handle it.
8110 */
8111static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8112 int acknowledged)
8113{
8114 u64 *p;
8115 int lo, hi;
8116 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008117 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008118
8119 if (bb->shift < 0)
8120 /* badblocks are disabled */
8121 return 0;
8122
8123 if (bb->shift) {
8124 /* round the start down, and the end up */
8125 sector_t next = s + sectors;
8126 s >>= bb->shift;
8127 next += (1<<bb->shift) - 1;
8128 next >>= bb->shift;
8129 sectors = next - s;
8130 }
8131
Bian Yu905b0292013-10-12 01:10:03 -04008132 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008133
8134 p = bb->page;
8135 lo = 0;
8136 hi = bb->count;
8137 /* Find the last range that starts at-or-before 's' */
8138 while (hi - lo > 1) {
8139 int mid = (lo + hi) / 2;
8140 sector_t a = BB_OFFSET(p[mid]);
8141 if (a <= s)
8142 lo = mid;
8143 else
8144 hi = mid;
8145 }
8146 if (hi > lo && BB_OFFSET(p[lo]) > s)
8147 hi = lo;
8148
8149 if (hi > lo) {
8150 /* we found a range that might merge with the start
8151 * of our new range
8152 */
8153 sector_t a = BB_OFFSET(p[lo]);
8154 sector_t e = a + BB_LEN(p[lo]);
8155 int ack = BB_ACK(p[lo]);
8156 if (e >= s) {
8157 /* Yes, we can merge with a previous range */
8158 if (s == a && s + sectors >= e)
8159 /* new range covers old */
8160 ack = acknowledged;
8161 else
8162 ack = ack && acknowledged;
8163
8164 if (e < s + sectors)
8165 e = s + sectors;
8166 if (e - a <= BB_MAX_LEN) {
8167 p[lo] = BB_MAKE(a, e-a, ack);
8168 s = e;
8169 } else {
8170 /* does not all fit in one range,
8171 * make p[lo] maximal
8172 */
8173 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8174 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8175 s = a + BB_MAX_LEN;
8176 }
8177 sectors = e - s;
8178 }
8179 }
8180 if (sectors && hi < bb->count) {
8181 /* 'hi' points to the first range that starts after 's'.
8182 * Maybe we can merge with the start of that range */
8183 sector_t a = BB_OFFSET(p[hi]);
8184 sector_t e = a + BB_LEN(p[hi]);
8185 int ack = BB_ACK(p[hi]);
8186 if (a <= s + sectors) {
8187 /* merging is possible */
8188 if (e <= s + sectors) {
8189 /* full overlap */
8190 e = s + sectors;
8191 ack = acknowledged;
8192 } else
8193 ack = ack && acknowledged;
8194
8195 a = s;
8196 if (e - a <= BB_MAX_LEN) {
8197 p[hi] = BB_MAKE(a, e-a, ack);
8198 s = e;
8199 } else {
8200 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8201 s = a + BB_MAX_LEN;
8202 }
8203 sectors = e - s;
8204 lo = hi;
8205 hi++;
8206 }
8207 }
8208 if (sectors == 0 && hi < bb->count) {
8209 /* we might be able to combine lo and hi */
8210 /* Note: 's' is at the end of 'lo' */
8211 sector_t a = BB_OFFSET(p[hi]);
8212 int lolen = BB_LEN(p[lo]);
8213 int hilen = BB_LEN(p[hi]);
8214 int newlen = lolen + hilen - (s - a);
8215 if (s >= a && newlen < BB_MAX_LEN) {
8216 /* yes, we can combine them */
8217 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8218 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8219 memmove(p + hi, p + hi + 1,
8220 (bb->count - hi - 1) * 8);
8221 bb->count--;
8222 }
8223 }
8224 while (sectors) {
8225 /* didn't merge (it all).
8226 * Need to add a range just before 'hi' */
8227 if (bb->count >= MD_MAX_BADBLOCKS) {
8228 /* No room for more */
8229 rv = 0;
8230 break;
8231 } else {
8232 int this_sectors = sectors;
8233 memmove(p + hi + 1, p + hi,
8234 (bb->count - hi) * 8);
8235 bb->count++;
8236
8237 if (this_sectors > BB_MAX_LEN)
8238 this_sectors = BB_MAX_LEN;
8239 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8240 sectors -= this_sectors;
8241 s += this_sectors;
8242 }
8243 }
8244
8245 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008246 if (!acknowledged)
8247 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008248 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008249
8250 return rv;
8251}
8252
NeilBrown3cb03002011-10-11 16:45:26 +11008253int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008254 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008255{
NeilBrownc6563a82012-05-21 09:27:00 +10008256 int rv;
8257 if (is_new)
8258 s += rdev->new_data_offset;
8259 else
8260 s += rdev->data_offset;
8261 rv = md_set_badblocks(&rdev->badblocks,
8262 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008263 if (rv) {
8264 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008265 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008266 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8267 md_wakeup_thread(rdev->mddev->thread);
8268 }
8269 return rv;
8270}
8271EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8272
8273/*
8274 * Remove a range of bad blocks from the table.
8275 * This may involve extending the table if we spilt a region,
8276 * but it must not fail. So if the table becomes full, we just
8277 * drop the remove request.
8278 */
8279static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8280{
8281 u64 *p;
8282 int lo, hi;
8283 sector_t target = s + sectors;
8284 int rv = 0;
8285
8286 if (bb->shift > 0) {
8287 /* When clearing we round the start up and the end down.
8288 * This should not matter as the shift should align with
8289 * the block size and no rounding should ever be needed.
8290 * However it is better the think a block is bad when it
8291 * isn't than to think a block is not bad when it is.
8292 */
8293 s += (1<<bb->shift) - 1;
8294 s >>= bb->shift;
8295 target >>= bb->shift;
8296 sectors = target - s;
8297 }
8298
8299 write_seqlock_irq(&bb->lock);
8300
8301 p = bb->page;
8302 lo = 0;
8303 hi = bb->count;
8304 /* Find the last range that starts before 'target' */
8305 while (hi - lo > 1) {
8306 int mid = (lo + hi) / 2;
8307 sector_t a = BB_OFFSET(p[mid]);
8308 if (a < target)
8309 lo = mid;
8310 else
8311 hi = mid;
8312 }
8313 if (hi > lo) {
8314 /* p[lo] is the last range that could overlap the
8315 * current range. Earlier ranges could also overlap,
8316 * but only this one can overlap the end of the range.
8317 */
8318 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8319 /* Partial overlap, leave the tail of this range */
8320 int ack = BB_ACK(p[lo]);
8321 sector_t a = BB_OFFSET(p[lo]);
8322 sector_t end = a + BB_LEN(p[lo]);
8323
8324 if (a < s) {
8325 /* we need to split this range */
8326 if (bb->count >= MD_MAX_BADBLOCKS) {
8327 rv = 0;
8328 goto out;
8329 }
8330 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8331 bb->count++;
8332 p[lo] = BB_MAKE(a, s-a, ack);
8333 lo++;
8334 }
8335 p[lo] = BB_MAKE(target, end - target, ack);
8336 /* there is no longer an overlap */
8337 hi = lo;
8338 lo--;
8339 }
8340 while (lo >= 0 &&
8341 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8342 /* This range does overlap */
8343 if (BB_OFFSET(p[lo]) < s) {
8344 /* Keep the early parts of this range. */
8345 int ack = BB_ACK(p[lo]);
8346 sector_t start = BB_OFFSET(p[lo]);
8347 p[lo] = BB_MAKE(start, s - start, ack);
8348 /* now low doesn't overlap, so.. */
8349 break;
8350 }
8351 lo--;
8352 }
8353 /* 'lo' is strictly before, 'hi' is strictly after,
8354 * anything between needs to be discarded
8355 */
8356 if (hi - lo > 1) {
8357 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8358 bb->count -= (hi - lo - 1);
8359 }
8360 }
8361
8362 bb->changed = 1;
8363out:
8364 write_sequnlock_irq(&bb->lock);
8365 return rv;
8366}
8367
NeilBrownc6563a82012-05-21 09:27:00 +10008368int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8369 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008370{
NeilBrownc6563a82012-05-21 09:27:00 +10008371 if (is_new)
8372 s += rdev->new_data_offset;
8373 else
8374 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008375 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008376 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008377}
8378EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8379
8380/*
8381 * Acknowledge all bad blocks in a list.
8382 * This only succeeds if ->changed is clear. It is used by
8383 * in-kernel metadata updates
8384 */
8385void md_ack_all_badblocks(struct badblocks *bb)
8386{
8387 if (bb->page == NULL || bb->changed)
8388 /* no point even trying */
8389 return;
8390 write_seqlock_irq(&bb->lock);
8391
majianpengecb178b2012-03-19 12:46:42 +11008392 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008393 u64 *p = bb->page;
8394 int i;
8395 for (i = 0; i < bb->count ; i++) {
8396 if (!BB_ACK(p[i])) {
8397 sector_t start = BB_OFFSET(p[i]);
8398 int len = BB_LEN(p[i]);
8399 p[i] = BB_MAKE(start, len, 1);
8400 }
8401 }
NeilBrownde393cd2011-07-28 11:31:48 +10008402 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008403 }
8404 write_sequnlock_irq(&bb->lock);
8405}
8406EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8407
NeilBrown16c791a2011-07-28 11:31:47 +10008408/* sysfs access to bad-blocks list.
8409 * We present two files.
8410 * 'bad-blocks' lists sector numbers and lengths of ranges that
8411 * are recorded as bad. The list is truncated to fit within
8412 * the one-page limit of sysfs.
8413 * Writing "sector length" to this file adds an acknowledged
8414 * bad block list.
8415 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8416 * been acknowledged. Writing to this file adds bad blocks
8417 * without acknowledging them. This is largely for testing.
8418 */
8419
8420static ssize_t
8421badblocks_show(struct badblocks *bb, char *page, int unack)
8422{
8423 size_t len;
8424 int i;
8425 u64 *p = bb->page;
8426 unsigned seq;
8427
8428 if (bb->shift < 0)
8429 return 0;
8430
8431retry:
8432 seq = read_seqbegin(&bb->lock);
8433
8434 len = 0;
8435 i = 0;
8436
8437 while (len < PAGE_SIZE && i < bb->count) {
8438 sector_t s = BB_OFFSET(p[i]);
8439 unsigned int length = BB_LEN(p[i]);
8440 int ack = BB_ACK(p[i]);
8441 i++;
8442
8443 if (unack && ack)
8444 continue;
8445
8446 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8447 (unsigned long long)s << bb->shift,
8448 length << bb->shift);
8449 }
NeilBrownde393cd2011-07-28 11:31:48 +10008450 if (unack && len == 0)
8451 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008452
8453 if (read_seqretry(&bb->lock, seq))
8454 goto retry;
8455
8456 return len;
8457}
8458
8459#define DO_DEBUG 1
8460
8461static ssize_t
8462badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8463{
8464 unsigned long long sector;
8465 int length;
8466 char newline;
8467#ifdef DO_DEBUG
8468 /* Allow clearing via sysfs *only* for testing/debugging.
8469 * Normally only a successful write may clear a badblock
8470 */
8471 int clear = 0;
8472 if (page[0] == '-') {
8473 clear = 1;
8474 page++;
8475 }
8476#endif /* DO_DEBUG */
8477
8478 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8479 case 3:
8480 if (newline != '\n')
8481 return -EINVAL;
8482 case 2:
8483 if (length <= 0)
8484 return -EINVAL;
8485 break;
8486 default:
8487 return -EINVAL;
8488 }
8489
8490#ifdef DO_DEBUG
8491 if (clear) {
8492 md_clear_badblocks(bb, sector, length);
8493 return len;
8494 }
8495#endif /* DO_DEBUG */
8496 if (md_set_badblocks(bb, sector, length, !unack))
8497 return len;
8498 else
8499 return -ENOSPC;
8500}
8501
Adrian Bunk75c96f82005-05-05 16:16:09 -07008502static int md_notify_reboot(struct notifier_block *this,
8503 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504{
8505 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008506 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008507 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508
NeilBrownc744a652012-03-19 12:46:37 +11008509 for_each_mddev(mddev, tmp) {
8510 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008511 if (mddev->pers)
8512 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008513 mddev->safemode = 2;
8514 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008515 }
NeilBrownc744a652012-03-19 12:46:37 +11008516 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 }
NeilBrownc744a652012-03-19 12:46:37 +11008518 /*
8519 * certain more exotic SCSI devices are known to be
8520 * volatile wrt too early system reboots. While the
8521 * right place to handle this issue is the given
8522 * driver, we do want to have a safe RAID driver ...
8523 */
8524 if (need_delay)
8525 mdelay(1000*1);
8526
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 return NOTIFY_DONE;
8528}
8529
Adrian Bunk75c96f82005-05-05 16:16:09 -07008530static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 .notifier_call = md_notify_reboot,
8532 .next = NULL,
8533 .priority = INT_MAX, /* before any real devices */
8534};
8535
8536static void md_geninit(void)
8537{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008538 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008540 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541}
8542
Adrian Bunk75c96f82005-05-05 16:16:09 -07008543static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544{
Tejun Heoe804ac72010-10-15 15:36:08 +02008545 int ret = -ENOMEM;
8546
Tejun Heoada609e2011-01-25 14:35:54 +01008547 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008548 if (!md_wq)
8549 goto err_wq;
8550
8551 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8552 if (!md_misc_wq)
8553 goto err_misc_wq;
8554
8555 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8556 goto err_md;
8557
8558 if ((ret = register_blkdev(0, "mdp")) < 0)
8559 goto err_mdp;
8560 mdp_major = ret;
8561
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008562 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008563 md_probe, NULL, NULL);
8564 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 md_probe, NULL, NULL);
8566
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008568 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569
8570 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572
Tejun Heoe804ac72010-10-15 15:36:08 +02008573err_mdp:
8574 unregister_blkdev(MD_MAJOR, "md");
8575err_md:
8576 destroy_workqueue(md_misc_wq);
8577err_misc_wq:
8578 destroy_workqueue(md_wq);
8579err_wq:
8580 return ret;
8581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
8583#ifndef MODULE
8584
8585/*
8586 * Searches all registered partitions for autorun RAID arrays
8587 * at boot time.
8588 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008589
8590static LIST_HEAD(all_detected_devices);
8591struct detected_devices_node {
8592 struct list_head list;
8593 dev_t dev;
8594};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596void md_autodetect_dev(dev_t dev)
8597{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008598 struct detected_devices_node *node_detected_dev;
8599
8600 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8601 if (node_detected_dev) {
8602 node_detected_dev->dev = dev;
8603 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8604 } else {
8605 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8606 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608}
8609
8610
8611static void autostart_arrays(int part)
8612{
NeilBrown3cb03002011-10-11 16:45:26 +11008613 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008614 struct detected_devices_node *node_detected_dev;
8615 dev_t dev;
8616 int i_scanned, i_passed;
8617
8618 i_scanned = 0;
8619 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008620
8621 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8622
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008623 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8624 i_scanned++;
8625 node_detected_dev = list_entry(all_detected_devices.next,
8626 struct detected_devices_node, list);
8627 list_del(&node_detected_dev->list);
8628 dev = node_detected_dev->dev;
8629 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008630 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 if (IS_ERR(rdev))
8632 continue;
8633
NeilBrownb2d444d2005-11-08 21:39:31 -08008634 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635 MD_BUG();
8636 continue;
8637 }
NeilBrownd0fae182008-03-04 14:29:31 -08008638 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008640 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008642
8643 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8644 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645
8646 autorun_devices(part);
8647}
8648
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008649#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650
8651static __exit void md_exit(void)
8652{
NeilBrownfd01b882011-10-11 16:47:53 +11008653 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008655
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008656 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008657 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008659 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 unregister_blkdev(mdp_major, "mdp");
8661 unregister_reboot_notifier(&md_notifier);
8662 unregister_sysctl_table(raid_table_header);
8663 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008664 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008666 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008668 destroy_workqueue(md_misc_wq);
8669 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670}
8671
Dan Williams685784a2007-07-09 11:56:42 -07008672subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673module_exit(md_exit)
8674
NeilBrownf91de922005-11-08 21:39:36 -08008675static int get_ro(char *buffer, struct kernel_param *kp)
8676{
8677 return sprintf(buffer, "%d", start_readonly);
8678}
8679static int set_ro(const char *val, struct kernel_param *kp)
8680{
8681 char *e;
8682 int num = simple_strtoul(val, &e, 10);
8683 if (*val && (*e == '\0' || *e == '\n')) {
8684 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008685 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008686 }
8687 return -EINVAL;
8688}
8689
NeilBrown80ca3a42006-07-10 04:44:18 -07008690module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8691module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008692
NeilBrownefeb53c2009-01-09 08:31:10 +11008693module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008694
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695EXPORT_SYMBOL(register_md_personality);
8696EXPORT_SYMBOL(unregister_md_personality);
8697EXPORT_SYMBOL(md_error);
8698EXPORT_SYMBOL(md_done_sync);
8699EXPORT_SYMBOL(md_write_start);
8700EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008701EXPORT_SYMBOL(md_register_thread);
8702EXPORT_SYMBOL(md_unregister_thread);
8703EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008705EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008707MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008708MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008709MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);