blob: 21f4d7ff0da22ee16e7556958502a2fb9ea74502 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb482011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 /* an empty barrier - all done */
398 bio_endio(bio, 0);
399 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200401 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 }
NeilBrown2b74e122010-12-09 15:59:01 +1100403
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
NeilBrownfd01b882011-10-11 16:47:53 +1100408void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100413 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 spin_unlock_irq(&mddev->write_lock);
416
NeilBrowna035fc32010-12-09 16:17:51 +1100417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
Tejun Heoe9c74692010-09-03 11:56:18 +0200420EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100421
NeilBrown74018dc2012-07-31 09:08:15 +0200422void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000423{
NeilBrown9cbb1752012-07-31 09:08:14 +0200424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
426 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000427}
NeilBrown9cbb1752012-07-31 09:08:14 +0200428EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000429
NeilBrownfd01b882011-10-11 16:47:53 +1100430static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 atomic_inc(&mddev->active);
433 return mddev;
434}
435
Dan Williams5fd3a172009-03-04 00:57:25 -0700436static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100437
NeilBrownfd01b882011-10-11 16:47:53 +1100438static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
NeilBrowna167f662010-10-26 18:31:13 +1100440 struct bio_set *bs = NULL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443 return;
NeilBrownd3374822009-01-09 08:31:10 +1100444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
447 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100448 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100449 bs = mddev->bio_set;
450 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100451 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100456 */
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200458 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100459 } else
460 kfree(mddev);
461 }
462 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100463 if (bs)
464 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrownfd01b882011-10-11 16:47:53 +1100467void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000483 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500484 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
488}
NeilBrown390ee602010-06-01 19:37:27 +1000489EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490
NeilBrownfd01b882011-10-11 16:47:53 +1100491static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownfd01b882011-10-11 16:47:53 +1100493 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown8f5f02c2011-02-16 13:58:51 +1100495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retry:
499 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrownefeb53c2009-01-09 08:31:10 +1100501 if (unit) {
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
504 mddev_get(mddev);
505 spin_unlock(&all_mddevs_lock);
506 kfree(new);
507 return mddev;
508 }
509
510 if (new) {
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
514 return new;
515 }
516 } else if (new) {
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
520 int is_free = 0;
521 int dev = 0;
522 while (!is_free) {
523 dev = MKDEV(MD_MAJOR, next_minor);
524 next_minor++;
525 if (next_minor > MINORMASK)
526 next_minor = 0;
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return NULL;
532 }
533
534 is_free = 1;
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
537 is_free = 0;
538 break;
539 }
540 }
541 new->unit = dev;
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 return new;
547 }
548 spin_unlock(&all_mddevs_lock);
549
NeilBrown9ffae0c2006-01-06 00:20:32 -0800550 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!new)
552 return NULL;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new->unit = unit;
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
557 else
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
NeilBrownfafd7fb2010-04-01 15:55:30 +1100560 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto retry;
563}
564
NeilBrown29f097c2013-11-14 17:54:51 +1100565static inline int __must_check mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
NeilBrown29f097c2013-11-14 17:54:51 +1100570/* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
572 */
573static inline void mddev_lock_nointr(struct mddev * mddev)
574{
575 mutex_lock(&mddev->reconfig_mutex);
576}
577
NeilBrownfd01b882011-10-11 16:47:53 +1100578static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100579{
580 return mutex_is_locked(&mddev->reconfig_mutex);
581}
582
NeilBrownfd01b882011-10-11 16:47:53 +1100583static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800585 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrownb6eb1272010-04-15 10:13:47 +1000588static struct attribute_group md_redundancy_group;
589
NeilBrownfd01b882011-10-11 16:47:53 +1100590static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
NeilBrowna64c8762010-04-14 17:15:37 +1000592 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
596 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
603 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 */
NeilBrowna64c8762010-04-14 17:15:37 +1000605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 mutex_unlock(&mddev->reconfig_mutex);
609
NeilBrown00bcb4a2010-06-01 19:37:23 +1000610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
619 }
NeilBrowna64c8762010-04-14 17:15:37 +1000620 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000621 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000622 } else
623 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chris Dunlop751e67c2011-10-19 16:48:26 +1100625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000627 */
628 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700629 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000630 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrownfd01b882011-10-11 16:47:53 +1100633static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
NeilBrown3cb03002011-10-11 16:45:26 +1100635 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
NeilBrowndafb20f2012-03-19 12:46:39 +1100637 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (rdev->desc_nr == nr)
639 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return NULL;
642}
643
NeilBrown1ca69c42012-10-11 13:37:33 +1100644static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
645{
646 struct md_rdev *rdev;
647
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
650 return rdev;
651
652 return NULL;
653}
654
655static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
NeilBrown3cb03002011-10-11 16:45:26 +1100657 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
NeilBrowndafb20f2012-03-19 12:46:39 +1100659 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return NULL;
664}
665
NeilBrown1ca69c42012-10-11 13:37:33 +1100666static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
667{
668 struct md_rdev *rdev;
669
670 rdev_for_each_rcu(rdev, mddev)
671 if (rdev->bdev->bd_dev == dev)
672 return rdev;
673
674 return NULL;
675}
676
NeilBrown84fc4b52011-10-11 16:49:58 +1100677static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800678{
NeilBrown84fc4b52011-10-11 16:49:58 +1100679 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800680 list_for_each_entry(pers, &pers_list, list) {
681 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800682 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800683 if (strcmp(pers->name, clevel)==0)
684 return pers;
685 }
NeilBrown2604b702006-01-06 00:20:36 -0800686 return NULL;
687}
688
Andre Nollb73df2d2008-07-11 22:02:23 +1000689/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100690static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100692 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000693 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrown3cb03002011-10-11 16:45:26 +1100696static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 if (rdev->sb_page)
699 MD_BUG();
700
701 rdev->sb_page = alloc_page(GFP_KERNEL);
702 if (!rdev->sb_page) {
703 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000704 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 return 0;
708}
709
NeilBrown545c8792012-05-22 13:54:30 +1000710void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800713 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 rdev->sb_loaded = 0;
715 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000716 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100717 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
NeilBrown2699b672011-07-28 11:31:47 +1000719 if (rdev->bb_page) {
720 put_page(rdev->bb_page);
721 rdev->bb_page = NULL;
722 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000723 kfree(rdev->badblocks.page);
724 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
NeilBrown545c8792012-05-22 13:54:30 +1000726EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
NeilBrown6712ecf2007-09-27 12:47:43 +0200728static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700729{
NeilBrown3cb03002011-10-11 16:45:26 +1100730 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100731 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700733 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
734 printk("md: super_written gets error=%d, uptodate=%d\n",
735 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
736 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800737 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700738 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739
NeilBrowna9701a32005-11-08 21:39:34 -0800740 if (atomic_dec_and_test(&mddev->pending_writes))
741 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700742 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743}
744
NeilBrownfd01b882011-10-11 16:47:53 +1100745void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 sector_t sector, int size, struct page *page)
747{
748 /* write first size bytes of page to sector of rdev
749 * Increment mddev->pending_writes before returning
750 * and decrement it on completion, waking up sb_wait
751 * if zero is reached.
752 * If an error occurred, call md_error
753 */
NeilBrowna167f662010-10-26 18:31:13 +1100754 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100756 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700757 bio->bi_sector = sector;
758 bio_add_page(bio, page, size, 0);
759 bio->bi_private = rdev;
760 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800761
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000763 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800764}
765
NeilBrownfd01b882011-10-11 16:47:53 +1100766void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800767{
Tejun Heoe9c74692010-09-03 11:56:18 +0200768 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800769 DEFINE_WAIT(wq);
770 for(;;) {
771 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
772 if (atomic_read(&mddev->pending_writes)==0)
773 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800774 schedule();
775 }
776 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777}
778
NeilBrown3cb03002011-10-11 16:45:26 +1100779int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
NeilBrowna167f662010-10-26 18:31:13 +1100782 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int ret;
784
Jens Axboe721a9602011-03-09 11:56:30 +0100785 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100787 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
788 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100789 if (metadata_op)
790 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000791 else if (rdev->mddev->reshape_position != MaxSector &&
792 (rdev->mddev->reshape_backwards ==
793 (sector >= rdev->mddev->reshape_position)))
794 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100795 else
796 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700798 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
801 bio_put(bio);
802 return ret;
803}
NeilBrowna8745db2006-01-06 00:20:34 -0800804EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
NeilBrown3cb03002011-10-11 16:45:26 +1100806static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 char b[BDEVNAME_SIZE];
809 if (!rdev->sb_page) {
810 MD_BUG();
811 return -EINVAL;
812 }
813 if (rdev->sb_loaded)
814 return 0;
815
816
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100817 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto fail;
819 rdev->sb_loaded = 1;
820 return 0;
821
822fail:
823 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
824 bdevname(rdev->bdev,b));
825 return -EINVAL;
826}
827
828static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
829{
Andre Noll05710462008-07-11 22:02:20 +1000830 return sb1->set_uuid0 == sb2->set_uuid0 &&
831 sb1->set_uuid1 == sb2->set_uuid1 &&
832 sb1->set_uuid2 == sb2->set_uuid2 &&
833 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
837{
838 int ret;
839 mdp_super_t *tmp1, *tmp2;
840
841 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
843
844 if (!tmp1 || !tmp2) {
845 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100846 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto abort;
848 }
849
850 *tmp1 = *sb1;
851 *tmp2 = *sb2;
852
853 /*
854 * nr_disks is not constant
855 */
856 tmp1->nr_disks = 0;
857 tmp2->nr_disks = 0;
858
Andre Nollce0c8e02008-07-11 22:02:20 +1000859 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700861 kfree(tmp1);
862 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864}
865
NeilBrown4d167f02007-05-09 02:35:37 -0700866
867static u32 md_csum_fold(u32 csum)
868{
869 csum = (csum & 0xffff) + (csum >> 16);
870 return (csum & 0xffff) + (csum >> 16);
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873static unsigned int calc_sb_csum(mdp_super_t * sb)
874{
NeilBrown4d167f02007-05-09 02:35:37 -0700875 u64 newcsum = 0;
876 u32 *sb32 = (u32*)sb;
877 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int disk_csum, csum;
879
880 disk_csum = sb->sb_csum;
881 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700882
883 for (i = 0; i < MD_SB_BYTES/4 ; i++)
884 newcsum += sb32[i];
885 csum = (newcsum & 0xffffffff) + (newcsum>>32);
886
887
888#ifdef CONFIG_ALPHA
889 /* This used to use csum_partial, which was wrong for several
890 * reasons including that different results are returned on
891 * different architectures. It isn't critical that we get exactly
892 * the same return value as before (we always csum_fold before
893 * testing, and that removes any differences). However as we
894 * know that csum_partial always returned a 16bit value on
895 * alphas, do a fold to maximise conformity to previous behaviour.
896 */
897 sb->sb_csum = md_csum_fold(disk_csum);
898#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return csum;
902}
903
904
905/*
906 * Handle superblock details.
907 * We want to be able to handle multiple superblock formats
908 * so we have a common interface to them all, and an array of
909 * different handlers.
910 * We rely on user-space to write the initial superblock, and support
911 * reading and updating of superblocks.
912 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100913 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * loads and validates a superblock on dev.
915 * if refdev != NULL, compare superblocks on both devices
916 * Return:
917 * 0 - dev has a superblock that is compatible with refdev
918 * 1 - dev has a superblock that is compatible and newer than refdev
919 * so dev should be used as the refdev in future
920 * -EINVAL superblock incompatible or invalid
921 * -othererror e.g. -EIO
922 *
NeilBrownfd01b882011-10-11 16:47:53 +1100923 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * Verify that dev is acceptable into mddev.
925 * The first time, mddev->raid_disks will be 0, and data from
926 * dev should be merged in. Subsequent calls check that dev
927 * is new enough. Return 0 or -EINVAL
928 *
NeilBrownfd01b882011-10-11 16:47:53 +1100929 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * Update the superblock for rdev with data in mddev
931 * This does not write to disc.
932 *
933 */
934
935struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000936 char *name;
937 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000938 int (*load_super)(struct md_rdev *rdev,
939 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000940 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000941 int (*validate_super)(struct mddev *mddev,
942 struct md_rdev *rdev);
943 void (*sync_super)(struct mddev *mddev,
944 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100945 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000946 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000947 int (*allow_new_offset)(struct md_rdev *rdev,
948 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949};
950
951/*
Andre Noll0894cc32009-06-18 08:49:23 +1000952 * Check that the given mddev has no bitmap.
953 *
954 * This function is called from the run method of all personalities that do not
955 * support bitmaps. It prints an error message and returns non-zero if mddev
956 * has a bitmap. Otherwise, it returns 0.
957 *
958 */
NeilBrownfd01b882011-10-11 16:47:53 +1100959int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000960{
NeilBrownc3d97142009-12-14 12:49:52 +1100961 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000962 return 0;
963 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
964 mdname(mddev), mddev->pers->name);
965 return 1;
966}
967EXPORT_SYMBOL(md_check_no_bitmap);
968
969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * load_super for 0.90.0
971 */
NeilBrown3cb03002011-10-11 16:45:26 +1100972static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
975 mdp_super_t *sb;
976 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /*
Andre Noll0f420352008-07-11 22:02:23 +1000979 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * it's at the end of the disk.
981 *
982 * It also happens to be a multiple of 4Kb.
983 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100984 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
NeilBrown0002b272005-09-09 16:23:53 -0700986 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ret) return ret;
988
989 ret = -EINVAL;
990
991 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +1000992 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (sb->md_magic != MD_SB_MAGIC) {
995 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
996 b);
997 goto abort;
998 }
999
1000 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001001 sb->minor_version < 90 ||
1002 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1004 sb->major_version, sb->minor_version,
1005 b);
1006 goto abort;
1007 }
1008
1009 if (sb->raid_disks <= 0)
1010 goto abort;
1011
NeilBrown4d167f02007-05-09 02:35:37 -07001012 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1014 b);
1015 goto abort;
1016 }
1017
1018 rdev->preferred_minor = sb->md_minor;
1019 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001020 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001021 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001022 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (sb->level == LEVEL_MULTIPATH)
1025 rdev->desc_nr = -1;
1026 else
1027 rdev->desc_nr = sb->this_disk.number;
1028
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001029 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001031 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001033 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (!uuid_equal(refsb, sb)) {
1035 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1036 b, bdevname(refdev->bdev,b2));
1037 goto abort;
1038 }
1039 if (!sb_equal(refsb, sb)) {
1040 printk(KERN_WARNING "md: %s has same UUID"
1041 " but different superblock to %s\n",
1042 b, bdevname(refdev->bdev, b2));
1043 goto abort;
1044 }
1045 ev1 = md_event(sb);
1046 ev2 = md_event(refsb);
1047 if (ev1 > ev2)
1048 ret = 1;
1049 else
1050 ret = 0;
1051 }
NeilBrown8190e752009-06-18 08:48:58 +10001052 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001053 /* Limit to 4TB as metadata cannot record more than that.
1054 * (not needed for Linear and RAID0 as metadata doesn't
1055 * record this size)
1056 */
1057 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001058 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
NeilBrown27a7b262011-09-10 17:21:28 +10001060 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001061 /* "this cannot possibly happen" ... */
1062 ret = -EINVAL;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 abort:
1065 return ret;
1066}
1067
1068/*
1069 * validate_super for 0.90.0
1070 */
NeilBrownfd01b882011-10-11 16:47:53 +11001071static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001074 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001075 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
NeilBrown41158c72005-06-21 17:17:25 -07001077 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001078 clear_bit(Faulty, &rdev->flags);
1079 clear_bit(In_sync, &rdev->flags);
1080 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (mddev->raid_disks == 0) {
1083 mddev->major_version = 0;
1084 mddev->minor_version = sb->minor_version;
1085 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001086 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001087 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 mddev->ctime = sb->ctime;
1089 mddev->utime = sb->utime;
1090 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001091 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 mddev->layout = sb->layout;
1093 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001094 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001095 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001096 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001097 mddev->bitmap_info.space = 0;
1098 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001099 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001100 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001101 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
NeilBrownf6705572006-03-27 01:18:11 -08001103 if (mddev->minor_version >= 91) {
1104 mddev->reshape_position = sb->reshape_position;
1105 mddev->delta_disks = sb->delta_disks;
1106 mddev->new_level = sb->new_level;
1107 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001108 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001109 if (mddev->delta_disks < 0)
1110 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001111 } else {
1112 mddev->reshape_position = MaxSector;
1113 mddev->delta_disks = 0;
1114 mddev->new_level = mddev->level;
1115 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001116 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001117 }
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (sb->state & (1<<MD_SB_CLEAN))
1120 mddev->recovery_cp = MaxSector;
1121 else {
1122 if (sb->events_hi == sb->cp_events_hi &&
1123 sb->events_lo == sb->cp_events_lo) {
1124 mddev->recovery_cp = sb->recovery_cp;
1125 } else
1126 mddev->recovery_cp = 0;
1127 }
1128
1129 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1130 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1131 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1132 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1133
1134 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001135
1136 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001137 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001138 mddev->bitmap_info.offset =
1139 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001140 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001141 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001142 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001143
NeilBrown41158c72005-06-21 17:17:25 -07001144 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001145 /* Insist on good event counter while assembling, except
1146 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001148 if (sb->disks[rdev->desc_nr].state & (
1149 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1150 if (ev1 < mddev->events)
1151 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001152 } else if (mddev->bitmap) {
1153 /* if adding to array with a bitmap, then we can accept an
1154 * older device ... but not too old.
1155 */
NeilBrown41158c72005-06-21 17:17:25 -07001156 if (ev1 < mddev->bitmap->events_cleared)
1157 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001158 } else {
1159 if (ev1 < mddev->events)
1160 /* just a hot-add of a new device, leave raid_disk at -1 */
1161 return 0;
1162 }
NeilBrown41158c72005-06-21 17:17:25 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 desc = sb->disks + rdev->desc_nr;
1166
1167 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001168 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001169 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1170 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001171 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001173 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1174 /* active but not in sync implies recovery up to
1175 * reshape position. We don't know exactly where
1176 * that is, so set to zero for now */
1177 if (mddev->minor_version >= 91) {
1178 rdev->recovery_offset = 0;
1179 rdev->raid_disk = desc->raid_disk;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001182 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1183 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001184 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001185 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187}
1188
1189/*
1190 * sync_super for 0.90.0
1191 */
NeilBrownfd01b882011-10-11 16:47:53 +11001192static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001195 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /* make rdev->sb match mddev data..
1200 *
1201 * 1/ zero out disks
1202 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1203 * 3/ any empty disks < next_spare become removed
1204 *
1205 * disks[0] gets initialised to REMOVED because
1206 * we cannot be sure from other fields if it has
1207 * been initialised or not.
1208 */
1209 int i;
1210 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1211
NeilBrown61181562005-09-09 16:24:02 -07001212 rdev->sb_size = MD_SB_BYTES;
1213
Namhyung Kim65a06f02011-07-27 11:00:36 +10001214 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 memset(sb, 0, sizeof(*sb));
1217
1218 sb->md_magic = MD_SB_MAGIC;
1219 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sb->patch_version = mddev->patch_version;
1221 sb->gvalid_words = 0; /* ignored */
1222 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1223 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1224 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1225 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1226
1227 sb->ctime = mddev->ctime;
1228 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001229 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sb->raid_disks = mddev->raid_disks;
1231 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001232 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 sb->utime = mddev->utime;
1234 sb->state = 0;
1235 sb->events_hi = (mddev->events>>32);
1236 sb->events_lo = (u32)mddev->events;
1237
NeilBrownf6705572006-03-27 01:18:11 -08001238 if (mddev->reshape_position == MaxSector)
1239 sb->minor_version = 90;
1240 else {
1241 sb->minor_version = 91;
1242 sb->reshape_position = mddev->reshape_position;
1243 sb->new_level = mddev->new_level;
1244 sb->delta_disks = mddev->delta_disks;
1245 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001246 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001247 }
1248 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (mddev->in_sync)
1250 {
1251 sb->recovery_cp = mddev->recovery_cp;
1252 sb->cp_events_hi = (mddev->events>>32);
1253 sb->cp_events_lo = (u32)mddev->events;
1254 if (mddev->recovery_cp == MaxSector)
1255 sb->state = (1<< MD_SB_CLEAN);
1256 } else
1257 sb->recovery_cp = 0;
1258
1259 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001260 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
NeilBrownc3d97142009-12-14 12:49:52 +11001262 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001263 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001266 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001269 int is_active = test_bit(In_sync, &rdev2->flags);
1270
1271 if (rdev2->raid_disk >= 0 &&
1272 sb->minor_version >= 91)
1273 /* we have nowhere to store the recovery_offset,
1274 * but if it is not below the reshape_position,
1275 * we can piggy-back on that.
1276 */
1277 is_active = 1;
1278 if (rdev2->raid_disk < 0 ||
1279 test_bit(Faulty, &rdev2->flags))
1280 is_active = 0;
1281 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001282 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001284 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001285 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 d = &sb->disks[rdev2->desc_nr];
1287 nr_disks++;
1288 d->number = rdev2->desc_nr;
1289 d->major = MAJOR(rdev2->bdev->bd_dev);
1290 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001291 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 d->raid_disk = rdev2->raid_disk;
1293 else
1294 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001295 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001297 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001299 if (test_bit(In_sync, &rdev2->flags))
1300 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 active++;
1302 working++;
1303 } else {
1304 d->state = 0;
1305 spare++;
1306 working++;
1307 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001308 if (test_bit(WriteMostly, &rdev2->flags))
1309 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /* now set the "removed" and "faulty" bits on any missing devices */
1312 for (i=0 ; i < mddev->raid_disks ; i++) {
1313 mdp_disk_t *d = &sb->disks[i];
1314 if (d->state == 0 && d->number == 0) {
1315 d->number = i;
1316 d->raid_disk = i;
1317 d->state = (1<<MD_DISK_REMOVED);
1318 d->state |= (1<<MD_DISK_FAULTY);
1319 failed++;
1320 }
1321 }
1322 sb->nr_disks = nr_disks;
1323 sb->active_disks = active;
1324 sb->working_disks = working;
1325 sb->failed_disks = failed;
1326 sb->spare_disks = spare;
1327
1328 sb->this_disk = sb->disks[rdev->desc_nr];
1329 sb->sb_csum = calc_sb_csum(sb);
1330}
1331
1332/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333 * rdev_size_change for 0.90.0
1334 */
1335static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001336super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337{
Andre Noll58c0fed2009-03-31 14:33:13 +11001338 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001340 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001342 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001343 if (!num_sectors || num_sectors > rdev->sb_start)
1344 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001345 /* Limit to 4TB as metadata cannot record more than that.
1346 * 4TB == 2^32 KB, or 2*2^32 sectors.
1347 */
NeilBrown667a5312012-08-16 16:46:12 +10001348 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001349 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001350 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001351 rdev->sb_page);
1352 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001353 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354}
1355
NeilBrownc6563a82012-05-21 09:27:00 +10001356static int
1357super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1358{
1359 /* non-zero offset changes not possible with v0.90 */
1360 return new_offset == 0;
1361}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001362
1363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * version 1 superblock
1365 */
1366
NeilBrown1c05b4b2006-10-21 10:24:08 -07001367static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001369 __le32 disk_csum;
1370 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 unsigned long long newcsum;
1372 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001373 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 disk_csum = sb->sb_csum;
1376 sb->sb_csum = 0;
1377 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001378 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 newcsum += le32_to_cpu(*isuper++);
1380
1381 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001382 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1385 sb->sb_csum = disk_csum;
1386 return cpu_to_le32(csum);
1387}
1388
NeilBrown2699b672011-07-28 11:31:47 +10001389static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1390 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001391static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct mdp_superblock_1 *sb;
1394 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001395 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001396 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001398 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
Andre Noll0f420352008-07-11 22:02:23 +10001401 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * It is always aligned to a 4K boundary and
1403 * depeding on minor_version, it can be:
1404 * 0: At least 8K, but less than 12K, from end of device
1405 * 1: At start of device
1406 * 2: 4K from start of device.
1407 */
1408 switch(minor_version) {
1409 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001410 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001411 sb_start -= 8*2;
1412 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001415 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001418 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 default:
1421 return -EINVAL;
1422 }
Andre Noll0f420352008-07-11 22:02:23 +10001423 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
NeilBrown0002b272005-09-09 16:23:53 -07001425 /* superblock is rarely larger than 1K, but it can be larger,
1426 * and it is safe to read 4k, so we do that
1427 */
1428 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (ret) return ret;
1430
1431
Namhyung Kim65a06f02011-07-27 11:00:36 +10001432 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1435 sb->major_version != cpu_to_le32(1) ||
1436 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001437 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001438 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -EINVAL;
1440
1441 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1442 printk("md: invalid superblock checksum on %s\n",
1443 bdevname(rdev->bdev,b));
1444 return -EINVAL;
1445 }
1446 if (le64_to_cpu(sb->data_size) < 10) {
1447 printk("md: data_size too small on %s\n",
1448 bdevname(rdev->bdev,b));
1449 return -EINVAL;
1450 }
NeilBrownc6563a82012-05-21 09:27:00 +10001451 if (sb->pad0 ||
1452 sb->pad3[0] ||
1453 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1454 /* Some padding is non-zero, might be a new feature */
1455 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rdev->preferred_minor = 0xffff;
1458 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001459 rdev->new_data_offset = rdev->data_offset;
1460 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1461 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1462 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001463 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
NeilBrown0002b272005-09-09 16:23:53 -07001465 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001466 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001467 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001468 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1469
1470 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001471 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001472 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001473 if (minor_version
1474 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1475 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001476
NeilBrown31b65a02006-07-10 04:44:14 -07001477 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1478 rdev->desc_nr = -1;
1479 else
1480 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1481
NeilBrown2699b672011-07-28 11:31:47 +10001482 if (!rdev->bb_page) {
1483 rdev->bb_page = alloc_page(GFP_KERNEL);
1484 if (!rdev->bb_page)
1485 return -ENOMEM;
1486 }
1487 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1488 rdev->badblocks.count == 0) {
1489 /* need to load the bad block list.
1490 * Currently we limit it to one page.
1491 */
1492 s32 offset;
1493 sector_t bb_sector;
1494 u64 *bbp;
1495 int i;
1496 int sectors = le16_to_cpu(sb->bblog_size);
1497 if (sectors > (PAGE_SIZE / 512))
1498 return -EINVAL;
1499 offset = le32_to_cpu(sb->bblog_offset);
1500 if (offset == 0)
1501 return -EINVAL;
1502 bb_sector = (long long)offset;
1503 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1504 rdev->bb_page, READ, true))
1505 return -EIO;
1506 bbp = (u64 *)page_address(rdev->bb_page);
1507 rdev->badblocks.shift = sb->bblog_shift;
1508 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1509 u64 bb = le64_to_cpu(*bbp);
1510 int count = bb & (0x3ff);
1511 u64 sector = bb >> 10;
1512 sector <<= sb->bblog_shift;
1513 count <<= sb->bblog_shift;
1514 if (bb + 1 == 0)
1515 break;
1516 if (md_set_badblocks(&rdev->badblocks,
1517 sector, count, 1) == 0)
1518 return -EINVAL;
1519 }
NeilBrown486adf72013-04-24 11:42:44 +10001520 } else if (sb->bblog_offset != 0)
1521 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001522
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001523 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001524 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001525 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001527 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1530 sb->level != refsb->level ||
1531 sb->layout != refsb->layout ||
1532 sb->chunksize != refsb->chunksize) {
1533 printk(KERN_WARNING "md: %s has strangely different"
1534 " superblock to %s\n",
1535 bdevname(rdev->bdev,b),
1536 bdevname(refdev->bdev,b2));
1537 return -EINVAL;
1538 }
1539 ev1 = le64_to_cpu(sb->events);
1540 ev2 = le64_to_cpu(refsb->events);
1541
1542 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001543 ret = 1;
1544 else
1545 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
NeilBrownc6563a82012-05-21 09:27:00 +10001547 if (minor_version) {
1548 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1549 sectors -= rdev->data_offset;
1550 } else
1551 sectors = rdev->sb_start;
1552 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001554 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
NeilBrownfd01b882011-10-11 16:47:53 +11001558static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001560 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001561 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
NeilBrown41158c72005-06-21 17:17:25 -07001563 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001564 clear_bit(Faulty, &rdev->flags);
1565 clear_bit(In_sync, &rdev->flags);
1566 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (mddev->raid_disks == 0) {
1569 mddev->major_version = 1;
1570 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001571 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001572 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1574 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1575 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001576 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 mddev->layout = le32_to_cpu(sb->layout);
1578 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001579 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001580 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001581 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001582 mddev->bitmap_info.space = 0;
1583 /* Default location for bitmap is 1K after superblock
1584 * using 3K - total of 4K
1585 */
NeilBrownc3d97142009-12-14 12:49:52 +11001586 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001587 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001588 mddev->reshape_backwards = 0;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1591 memcpy(mddev->uuid, sb->set_uuid, 16);
1592
1593 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001594
NeilBrown71c08052005-09-09 16:23:51 -07001595 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001596 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001597 mddev->bitmap_info.offset =
1598 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001599 /* Metadata doesn't record how much space is available.
1600 * For 1.0, we assume we can use up to the superblock
1601 * if before, else to 4K beyond superblock.
1602 * For others, assume no change is possible.
1603 */
1604 if (mddev->minor_version > 0)
1605 mddev->bitmap_info.space = 0;
1606 else if (mddev->bitmap_info.offset > 0)
1607 mddev->bitmap_info.space =
1608 8 - mddev->bitmap_info.offset;
1609 else
1610 mddev->bitmap_info.space =
1611 -mddev->bitmap_info.offset;
1612 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001613
NeilBrownf6705572006-03-27 01:18:11 -08001614 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1615 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1616 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1617 mddev->new_level = le32_to_cpu(sb->new_level);
1618 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001619 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001620 if (mddev->delta_disks < 0 ||
1621 (mddev->delta_disks == 0 &&
1622 (le32_to_cpu(sb->feature_map)
1623 & MD_FEATURE_RESHAPE_BACKWARDS)))
1624 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001625 } else {
1626 mddev->reshape_position = MaxSector;
1627 mddev->delta_disks = 0;
1628 mddev->new_level = mddev->level;
1629 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001630 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001631 }
1632
NeilBrown41158c72005-06-21 17:17:25 -07001633 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001634 /* Insist of good event counter while assembling, except for
1635 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001637 if (rdev->desc_nr >= 0 &&
1638 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1639 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1640 if (ev1 < mddev->events)
1641 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001642 } else if (mddev->bitmap) {
1643 /* If adding to array with a bitmap, then we can accept an
1644 * older device, but not too old.
1645 */
NeilBrown41158c72005-06-21 17:17:25 -07001646 if (ev1 < mddev->bitmap->events_cleared)
1647 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001648 } else {
1649 if (ev1 < mddev->events)
1650 /* just a hot-add of a new device, leave raid_disk at -1 */
1651 return 0;
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (mddev->level != LEVEL_MULTIPATH) {
1654 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001655 if (rdev->desc_nr < 0 ||
1656 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1657 role = 0xffff;
1658 rdev->desc_nr = -1;
1659 } else
1660 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 switch(role) {
1662 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 break;
1664 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001665 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001668 if ((le32_to_cpu(sb->feature_map) &
1669 MD_FEATURE_RECOVERY_OFFSET))
1670 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1671 else
1672 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 rdev->raid_disk = role;
1674 break;
1675 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001676 if (sb->devflags & WriteMostly1)
1677 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001678 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1679 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001680 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001681 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
1684}
1685
NeilBrownfd01b882011-10-11 16:47:53 +11001686static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001689 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int max_dev, i;
1691 /* make rdev->sb match mddev and rdev data. */
1692
Namhyung Kim65a06f02011-07-27 11:00:36 +10001693 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 sb->feature_map = 0;
1696 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001697 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 memset(sb->pad3, 0, sizeof(sb->pad3));
1699
1700 sb->utime = cpu_to_le64((__u64)mddev->utime);
1701 sb->events = cpu_to_le64(mddev->events);
1702 if (mddev->in_sync)
1703 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1704 else
1705 sb->resync_offset = cpu_to_le64(0);
1706
NeilBrown1c05b4b2006-10-21 10:24:08 -07001707 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001708
NeilBrownf0ca3402006-02-02 14:28:04 -08001709 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001710 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001711 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001712 sb->level = cpu_to_le32(mddev->level);
1713 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001714
NeilBrownaeb9b212011-08-25 14:43:08 +10001715 if (test_bit(WriteMostly, &rdev->flags))
1716 sb->devflags |= WriteMostly1;
1717 else
1718 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001719 sb->data_offset = cpu_to_le64(rdev->data_offset);
1720 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001721
NeilBrownc3d97142009-12-14 12:49:52 +11001722 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1723 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001724 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001725 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001726
1727 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001728 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1731 sb->recovery_offset =
1732 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001733 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001734 if (test_bit(Replacement, &rdev->flags))
1735 sb->feature_map |=
1736 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001737
NeilBrownf6705572006-03-27 01:18:11 -08001738 if (mddev->reshape_position != MaxSector) {
1739 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1740 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1741 sb->new_layout = cpu_to_le32(mddev->new_layout);
1742 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1743 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001744 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001745 if (mddev->delta_disks == 0 &&
1746 mddev->reshape_backwards)
1747 sb->feature_map
1748 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001749 if (rdev->new_data_offset != rdev->data_offset) {
1750 sb->feature_map
1751 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1752 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1753 - rdev->data_offset));
1754 }
NeilBrownf6705572006-03-27 01:18:11 -08001755 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001756
NeilBrown2699b672011-07-28 11:31:47 +10001757 if (rdev->badblocks.count == 0)
1758 /* Nothing to do for bad blocks*/ ;
1759 else if (sb->bblog_offset == 0)
1760 /* Cannot record bad blocks on this device */
1761 md_error(mddev, rdev);
1762 else {
1763 struct badblocks *bb = &rdev->badblocks;
1764 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1765 u64 *p = bb->page;
1766 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1767 if (bb->changed) {
1768 unsigned seq;
1769
1770retry:
1771 seq = read_seqbegin(&bb->lock);
1772
1773 memset(bbp, 0xff, PAGE_SIZE);
1774
1775 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001776 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001777 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1778 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001779 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001780 }
NeilBrownd0962932012-03-19 12:46:41 +11001781 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001782 if (read_seqretry(&bb->lock, seq))
1783 goto retry;
1784
1785 bb->sector = (rdev->sb_start +
1786 (int)le32_to_cpu(sb->bblog_offset));
1787 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001788 }
1789 }
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001792 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (rdev2->desc_nr+1 > max_dev)
1794 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001795
NeilBrown70471da2009-08-03 10:59:57 +10001796 if (max_dev > le32_to_cpu(sb->max_dev)) {
1797 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001798 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001799 rdev->sb_size = max_dev * 2 + 256;
1800 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1801 if (rdev->sb_size & bmask)
1802 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001803 } else
1804 max_dev = le32_to_cpu(sb->max_dev);
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 for (i=0; i<max_dev;i++)
1807 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1808
NeilBrowndafb20f2012-03-19 12:46:39 +11001809 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001811 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001813 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001815 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001816 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 else
1818 sb->dev_roles[i] = cpu_to_le16(0xffff);
1819 }
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 sb->sb_csum = calc_sb_1_csum(sb);
1822}
1823
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001825super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001826{
1827 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001828 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001829 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001830 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001831 if (rdev->data_offset != rdev->new_data_offset)
1832 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001833 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001835 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001836 max_sectors -= rdev->data_offset;
1837 if (!num_sectors || num_sectors > max_sectors)
1838 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001839 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001840 /* minor version 0 with bitmap we can't move */
1841 return 0;
1842 } else {
1843 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001844 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001845 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001846 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001847 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001848 if (!num_sectors || num_sectors > max_sectors)
1849 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001850 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001852 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001853 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001854 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001856 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001857 rdev->sb_page);
1858 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001859 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001860
1861}
1862
1863static int
1864super_1_allow_new_offset(struct md_rdev *rdev,
1865 unsigned long long new_offset)
1866{
1867 /* All necessary checks on new >= old have been done */
1868 struct bitmap *bitmap;
1869 if (new_offset >= rdev->data_offset)
1870 return 1;
1871
1872 /* with 1.0 metadata, there is no metadata to tread on
1873 * so we can always move back */
1874 if (rdev->mddev->minor_version == 0)
1875 return 1;
1876
1877 /* otherwise we must be sure not to step on
1878 * any metadata, so stay:
1879 * 36K beyond start of superblock
1880 * beyond end of badblocks
1881 * beyond write-intent bitmap
1882 */
1883 if (rdev->sb_start + (32+4)*2 > new_offset)
1884 return 0;
1885 bitmap = rdev->mddev->bitmap;
1886 if (bitmap && !rdev->mddev->bitmap_info.file &&
1887 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001888 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001889 return 0;
1890 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1891 return 0;
1892
1893 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Adrian Bunk75c96f82005-05-05 16:16:09 -07001896static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 [0] = {
1898 .name = "0.90.0",
1899 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001900 .load_super = super_90_load,
1901 .validate_super = super_90_validate,
1902 .sync_super = super_90_sync,
1903 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001904 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 },
1906 [1] = {
1907 .name = "md-1",
1908 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001909 .load_super = super_1_load,
1910 .validate_super = super_1_validate,
1911 .sync_super = super_1_sync,
1912 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001913 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 },
1915};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
NeilBrownfd01b882011-10-11 16:47:53 +11001917static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001918{
1919 if (mddev->sync_super) {
1920 mddev->sync_super(mddev, rdev);
1921 return;
1922 }
1923
1924 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1925
1926 super_types[mddev->major_version].sync_super(mddev, rdev);
1927}
1928
NeilBrownfd01b882011-10-11 16:47:53 +11001929static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
NeilBrown3cb03002011-10-11 16:45:26 +11001931 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
NeilBrown4b809912008-07-21 17:05:25 +10001933 rcu_read_lock();
1934 rdev_for_each_rcu(rdev, mddev1)
1935 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001936 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001937 rdev2->bdev->bd_contains) {
1938 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001939 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001940 }
1941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return 0;
1943}
1944
1945static LIST_HEAD(pending_raid_disks);
1946
Andre Nollac5e7112009-08-03 10:59:47 +10001947/*
1948 * Try to register data integrity profile for an mddev
1949 *
1950 * This is called when an array is started and after a disk has been kicked
1951 * from the array. It only succeeds if all working and active component devices
1952 * are integrity capable with matching profiles.
1953 */
NeilBrownfd01b882011-10-11 16:47:53 +11001954int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001955{
NeilBrown3cb03002011-10-11 16:45:26 +11001956 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001957
Andre Nollac5e7112009-08-03 10:59:47 +10001958 if (list_empty(&mddev->disks))
1959 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001960 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1961 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001962 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001963 /* skip spares and non-functional disks */
1964 if (test_bit(Faulty, &rdev->flags))
1965 continue;
1966 if (rdev->raid_disk < 0)
1967 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001968 if (!reference) {
1969 /* Use the first rdev as the reference */
1970 reference = rdev;
1971 continue;
1972 }
1973 /* does this rdev's profile match the reference profile? */
1974 if (blk_integrity_compare(reference->bdev->bd_disk,
1975 rdev->bdev->bd_disk) < 0)
1976 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001978 if (!reference || !bdev_get_integrity(reference->bdev))
1979 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001980 /*
1981 * All component devices are integrity capable and have matching
1982 * profiles, register the common profile for the md device.
1983 */
1984 if (blk_integrity_register(mddev->gendisk,
1985 bdev_get_integrity(reference->bdev)) != 0) {
1986 printk(KERN_ERR "md: failed to register integrity for %s\n",
1987 mdname(mddev));
1988 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001989 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001990 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1991 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1992 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1993 mdname(mddev));
1994 return -EINVAL;
1995 }
Andre Nollac5e7112009-08-03 10:59:47 +10001996 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001997}
Andre Nollac5e7112009-08-03 10:59:47 +10001998EXPORT_SYMBOL(md_integrity_register);
1999
2000/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002001void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002002{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002003 struct blk_integrity *bi_rdev;
2004 struct blk_integrity *bi_mddev;
2005
2006 if (!mddev->gendisk)
2007 return;
2008
2009 bi_rdev = bdev_get_integrity(rdev->bdev);
2010 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002011
2012 if (!bi_mddev) /* nothing to do */
2013 return;
2014 if (rdev->raid_disk < 0) /* skip spares */
2015 return;
2016 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2017 rdev->bdev->bd_disk) >= 0)
2018 return;
2019 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2020 blk_integrity_unregister(mddev->gendisk);
2021}
2022EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002023
NeilBrownfd01b882011-10-11 16:47:53 +11002024static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002026 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002027 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002028 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002029 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 if (rdev->mddev) {
2032 MD_BUG();
2033 return -EINVAL;
2034 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002035
2036 /* prevent duplicates */
2037 if (find_rdev(mddev, rdev->bdev->bd_dev))
2038 return -EEXIST;
2039
Andre Nolldd8ac332009-03-31 14:33:13 +11002040 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2041 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2042 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002043 if (mddev->pers) {
2044 /* Cannot change size, so fail
2045 * If mddev->level <= 0, then we don't care
2046 * about aligning sizes (e.g. linear)
2047 */
2048 if (mddev->level > 0)
2049 return -ENOSPC;
2050 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002051 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /* Verify rdev->desc_nr is unique.
2055 * If it is -1, assign a free number, else
2056 * check number is not in use
2057 */
2058 if (rdev->desc_nr < 0) {
2059 int choice = 0;
2060 if (mddev->pers) choice = mddev->raid_disks;
2061 while (find_rdev_nr(mddev, choice))
2062 choice++;
2063 rdev->desc_nr = choice;
2064 } else {
2065 if (find_rdev_nr(mddev, rdev->desc_nr))
2066 return -EBUSY;
2067 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002068 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2069 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2070 mdname(mddev), mddev->max_disks);
2071 return -EBUSY;
2072 }
NeilBrown19133a42005-11-08 21:39:35 -08002073 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002074 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002075 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002078 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002079
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002080 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002081 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002082
Tejun Heo0762b8b2008-08-25 19:56:12 +09002083 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002084 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2085 /* failure here is OK */;
2086 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002087
NeilBrown4b809912008-07-21 17:05:25 +10002088 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002089 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002090
2091 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002092 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002095
2096 fail:
2097 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2098 b, mdname(mddev));
2099 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
NeilBrown177a99b2008-02-06 01:39:56 -08002102static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002103{
NeilBrown3cb03002011-10-11 16:45:26 +11002104 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002105 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002106 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002107}
2108
NeilBrown3cb03002011-10-11 16:45:26 +11002109static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 char b[BDEVNAME_SIZE];
2112 if (!rdev->mddev) {
2113 MD_BUG();
2114 return;
2115 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002116 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002117 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2119 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002120 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002121 sysfs_put(rdev->sysfs_state);
2122 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002123 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002124 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002125 * writing to 'remove' to "dev/state". We also need
2126 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002127 */
NeilBrown4b809912008-07-21 17:05:25 +10002128 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002129 INIT_WORK(&rdev->del_work, md_delayed_delete);
2130 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002131 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134/*
2135 * prevent the device from being mounted, repartitioned or
2136 * otherwise reused by a RAID array (or any other kernel
2137 * subsystem), by bd_claiming the device.
2138 */
NeilBrown3cb03002011-10-11 16:45:26 +11002139static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 int err = 0;
2142 struct block_device *bdev;
2143 char b[BDEVNAME_SIZE];
2144
Tejun Heod4d77622010-11-13 11:55:18 +01002145 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002146 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (IS_ERR(bdev)) {
2148 printk(KERN_ERR "md: could not open %s.\n",
2149 __bdevname(dev, b));
2150 return PTR_ERR(bdev);
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 rdev->bdev = bdev;
2153 return err;
2154}
2155
NeilBrown3cb03002011-10-11 16:45:26 +11002156static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 struct block_device *bdev = rdev->bdev;
2159 rdev->bdev = NULL;
2160 if (!bdev)
2161 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002162 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165void md_autodetect_dev(dev_t dev);
2166
NeilBrown3cb03002011-10-11 16:45:26 +11002167static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 char b[BDEVNAME_SIZE];
2170 printk(KERN_INFO "md: export_rdev(%s)\n",
2171 bdevname(rdev->bdev,b));
2172 if (rdev->mddev)
2173 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002174 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002176 if (test_bit(AutoDetected, &rdev->flags))
2177 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178#endif
2179 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002180 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
NeilBrown3cb03002011-10-11 16:45:26 +11002183static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 unbind_rdev_from_array(rdev);
2186 export_rdev(rdev);
2187}
2188
NeilBrownfd01b882011-10-11 16:47:53 +11002189static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
NeilBrown3cb03002011-10-11 16:45:26 +11002191 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
NeilBrowndafb20f2012-03-19 12:46:39 +11002193 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!rdev->mddev) {
2195 MD_BUG();
2196 continue;
2197 }
2198 kick_rdev_from_array(rdev);
2199 }
2200 if (!list_empty(&mddev->disks))
2201 MD_BUG();
2202 mddev->raid_disks = 0;
2203 mddev->major_version = 0;
2204}
2205
2206static void print_desc(mdp_disk_t *desc)
2207{
2208 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2209 desc->major,desc->minor,desc->raid_disk,desc->state);
2210}
2211
Cheng Renquancd2ac932009-01-09 08:31:08 +11002212static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int i;
2215
2216 printk(KERN_INFO
2217 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2218 sb->major_version, sb->minor_version, sb->patch_version,
2219 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2220 sb->ctime);
2221 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2222 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2223 sb->md_minor, sb->layout, sb->chunk_size);
2224 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2225 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2226 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2227 sb->failed_disks, sb->spare_disks,
2228 sb->sb_csum, (unsigned long)sb->events_lo);
2229
2230 printk(KERN_INFO);
2231 for (i = 0; i < MD_SB_DISKS; i++) {
2232 mdp_disk_t *desc;
2233
2234 desc = sb->disks + i;
2235 if (desc->number || desc->major || desc->minor ||
2236 desc->raid_disk || (desc->state && (desc->state != 4))) {
2237 printk(" D %2d: ", i);
2238 print_desc(desc);
2239 }
2240 }
2241 printk(KERN_INFO "md: THIS: ");
2242 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Cheng Renquancd2ac932009-01-09 08:31:08 +11002245static void print_sb_1(struct mdp_superblock_1 *sb)
2246{
2247 __u8 *uuid;
2248
2249 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002250 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002251 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002252 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002253 le32_to_cpu(sb->major_version),
2254 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002255 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002256 sb->set_name,
2257 (unsigned long long)le64_to_cpu(sb->ctime)
2258 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2259
2260 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002261 printk(KERN_INFO
2262 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002263 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002264 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002265 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2266 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002267 le32_to_cpu(sb->level),
2268 (unsigned long long)le64_to_cpu(sb->size),
2269 le32_to_cpu(sb->raid_disks),
2270 le32_to_cpu(sb->layout),
2271 le32_to_cpu(sb->chunksize),
2272 (unsigned long long)le64_to_cpu(sb->data_offset),
2273 (unsigned long long)le64_to_cpu(sb->data_size),
2274 (unsigned long long)le64_to_cpu(sb->super_offset),
2275 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2276 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002277 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002278 sb->devflags,
2279 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2280 (unsigned long long)le64_to_cpu(sb->events),
2281 (unsigned long long)le64_to_cpu(sb->resync_offset),
2282 le32_to_cpu(sb->sb_csum),
2283 le32_to_cpu(sb->max_dev)
2284 );
2285}
2286
NeilBrown3cb03002011-10-11 16:45:26 +11002287static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002290 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2291 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002292 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2293 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002295 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2296 switch (major_version) {
2297 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002298 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002299 break;
2300 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002301 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002302 break;
2303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 } else
2305 printk(KERN_INFO "md: no rdev superblock!\n");
2306}
2307
Adrian Bunk5e563412006-06-26 00:27:42 -07002308static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002310 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002311 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002312 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 char b[BDEVNAME_SIZE];
2314
2315 printk("\n");
2316 printk("md: **********************************\n");
2317 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2318 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002319 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
NeilBrown32a76272005-06-21 17:17:14 -07002321 if (mddev->bitmap)
2322 bitmap_print_sb(mddev->bitmap);
2323 else
2324 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002325 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 printk("<%s>", bdevname(rdev->bdev,b));
2327 printk("\n");
2328
NeilBrowndafb20f2012-03-19 12:46:39 +11002329 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002330 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332 printk("md: **********************************\n");
2333 printk("\n");
2334}
2335
2336
NeilBrownfd01b882011-10-11 16:47:53 +11002337static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
NeilBrown42543762006-06-26 00:27:57 -07002339 /* Update each superblock (in-memory image), but
2340 * if we are allowed to, skip spares which already
2341 * have the right event counter, or have one earlier
2342 * (which would mean they aren't being marked as dirty
2343 * with the rest of the array)
2344 */
NeilBrown3cb03002011-10-11 16:45:26 +11002345 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002346 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002347 if (rdev->sb_events == mddev->events ||
2348 (nospares &&
2349 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002350 rdev->sb_events+1 == mddev->events)) {
2351 /* Don't update this superblock */
2352 rdev->sb_loaded = 2;
2353 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002354 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002355 rdev->sb_loaded = 1;
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358}
2359
NeilBrownfd01b882011-10-11 16:47:53 +11002360static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
NeilBrown3cb03002011-10-11 16:45:26 +11002362 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002363 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002364 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002365 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
NeilBrownd87f0642013-04-24 11:42:40 +10002367 if (mddev->ro) {
2368 if (force_change)
2369 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2370 return;
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002373 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002374 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002375 if (rdev->raid_disk >= 0 &&
2376 mddev->delta_disks >= 0 &&
2377 !test_bit(In_sync, &rdev->flags) &&
2378 mddev->curr_resync_completed > rdev->recovery_offset)
2379 rdev->recovery_offset = mddev->curr_resync_completed;
2380
2381 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002382 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002383 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002384 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002385 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002386 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002387 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002388 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002389 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002390 md_ack_all_badblocks(&rdev->badblocks);
2391 md_error(mddev, rdev);
2392 }
2393 clear_bit(Blocked, &rdev->flags);
2394 clear_bit(BlockedBadBlocks, &rdev->flags);
2395 wake_up(&rdev->blocked_wait);
2396 }
2397 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002398 wake_up(&mddev->sb_wait);
2399 return;
2400 }
2401
NeilBrowna9701a32005-11-08 21:39:34 -08002402 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002403
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002404 mddev->utime = get_seconds();
2405
NeilBrown850b2b42006-10-03 01:15:46 -07002406 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2407 force_change = 1;
2408 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2409 /* just a clean<-> dirty transition, possibly leave spares alone,
2410 * though if events isn't the right even/odd, we will have to do
2411 * spares after all
2412 */
2413 nospares = 1;
2414 if (force_change)
2415 nospares = 0;
2416 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002417 /* If the array is degraded, then skipping spares is both
2418 * dangerous and fairly pointless.
2419 * Dangerous because a device that was removed from the array
2420 * might have a event_count that still looks up-to-date,
2421 * so it can be re-added without a resync.
2422 * Pointless because if there are any spares to skip,
2423 * then a recovery will happen and soon that array won't
2424 * be degraded any more and the spare can go back to sleep then.
2425 */
NeilBrown850b2b42006-10-03 01:15:46 -07002426 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002427
NeilBrown06d91a52005-06-21 17:17:12 -07002428 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002429
2430 /* If this is just a dirty<->clean transition, and the array is clean
2431 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002432 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002433 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002434 && mddev->can_decrease_events
2435 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002436 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002437 mddev->can_decrease_events = 0;
2438 } else {
NeilBrown42543762006-06-26 00:27:57 -07002439 /* otherwise we have to go forward and ... */
2440 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002441 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 if (!mddev->events) {
2445 /*
2446 * oops, this 64-bit counter should never wrap.
2447 * Either we are in around ~1 trillion A.C., assuming
2448 * 1 reboot per second, or we have a bug:
2449 */
2450 MD_BUG();
2451 mddev->events --;
2452 }
NeilBrown2699b672011-07-28 11:31:47 +10002453
NeilBrowndafb20f2012-03-19 12:46:39 +11002454 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002455 if (rdev->badblocks.changed)
2456 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002457 if (test_bit(Faulty, &rdev->flags))
2458 set_bit(FaultRecorded, &rdev->flags);
2459 }
NeilBrown2699b672011-07-28 11:31:47 +10002460
NeilBrowne6910632008-02-06 01:39:51 -08002461 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002462 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
NeilBrown36a4e1f2011-10-07 14:23:17 +11002464 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2465 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
NeilBrown4ad13662007-07-17 04:06:13 -07002467 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002468 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002470
NeilBrown42543762006-06-26 00:27:57 -07002471 if (rdev->sb_loaded != 1)
2472 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002474 if (!test_bit(Faulty, &rdev->flags) &&
2475 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002476 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002477 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002478 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002479 pr_debug("md: (write) %s's sb offset: %llu\n",
2480 bdevname(rdev->bdev, b),
2481 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002482 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002483 if (rdev->badblocks.size) {
2484 md_super_write(mddev, rdev,
2485 rdev->badblocks.sector,
2486 rdev->badblocks.size << 9,
2487 rdev->bb_page);
2488 rdev->badblocks.size = 0;
2489 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002490
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002491 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002492 pr_debug("md: %s (skipping faulty)\n",
2493 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002494 else
2495 pr_debug("(skipping incremental s/r ");
2496
NeilBrown7bfa19f2005-06-21 17:17:28 -07002497 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* only need to write one superblock... */
2499 break;
2500 }
NeilBrowna9701a32005-11-08 21:39:34 -08002501 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002502 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002503
NeilBrowna9701a32005-11-08 21:39:34 -08002504 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002505 if (mddev->in_sync != sync_req ||
2506 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002507 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002508 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002509 goto repeat;
2510 }
NeilBrown850b2b42006-10-03 01:15:46 -07002511 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002512 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002513 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002514 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2515 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002516
NeilBrowndafb20f2012-03-19 12:46:39 +11002517 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002518 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2519 clear_bit(Blocked, &rdev->flags);
2520
2521 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002522 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002523 clear_bit(BlockedBadBlocks, &rdev->flags);
2524 wake_up(&rdev->blocked_wait);
2525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Andre Noll7f6ce762008-03-23 18:34:54 +01002528/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002529 * We want to accept with case. For this we use cmd_match.
2530 */
2531static int cmd_match(const char *cmd, const char *str)
2532{
2533 /* See if cmd, written into a sysfs file, matches
2534 * str. They must either be the same, or cmd can
2535 * have a trailing newline
2536 */
2537 while (*cmd && *str && *cmd == *str) {
2538 cmd++;
2539 str++;
2540 }
2541 if (*cmd == '\n')
2542 cmd++;
2543 if (*str || *cmd)
2544 return 0;
2545 return 1;
2546}
2547
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548struct rdev_sysfs_entry {
2549 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002550 ssize_t (*show)(struct md_rdev *, char *);
2551 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002552};
2553
2554static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002555state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002556{
2557 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002558 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002559
NeilBrownde393cd2011-07-28 11:31:48 +10002560 if (test_bit(Faulty, &rdev->flags) ||
2561 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002562 len+= sprintf(page+len, "%sfaulty",sep);
2563 sep = ",";
2564 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002565 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002566 len += sprintf(page+len, "%sin_sync",sep);
2567 sep = ",";
2568 }
NeilBrownf6556752006-06-26 00:28:01 -07002569 if (test_bit(WriteMostly, &rdev->flags)) {
2570 len += sprintf(page+len, "%swrite_mostly",sep);
2571 sep = ",";
2572 }
NeilBrownde393cd2011-07-28 11:31:48 +10002573 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002574 (rdev->badblocks.unacked_exist
2575 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002576 len += sprintf(page+len, "%sblocked", sep);
2577 sep = ",";
2578 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002579 if (!test_bit(Faulty, &rdev->flags) &&
2580 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002581 len += sprintf(page+len, "%sspare", sep);
2582 sep = ",";
2583 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002584 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2585 len += sprintf(page+len, "%swrite_error", sep);
2586 sep = ",";
2587 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002588 if (test_bit(WantReplacement, &rdev->flags)) {
2589 len += sprintf(page+len, "%swant_replacement", sep);
2590 sep = ",";
2591 }
2592 if (test_bit(Replacement, &rdev->flags)) {
2593 len += sprintf(page+len, "%sreplacement", sep);
2594 sep = ",";
2595 }
2596
NeilBrown86e6ffd2005-11-08 21:39:24 -08002597 return len+sprintf(page+len, "\n");
2598}
2599
NeilBrown45dc2de2006-06-26 00:27:58 -07002600static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002601state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002602{
2603 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002604 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002605 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002606 * writemostly - sets write_mostly
2607 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002608 * blocked - sets the Blocked flags
2609 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002610 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002611 * write_error - sets WriteErrorSeen
2612 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002613 */
2614 int err = -EINVAL;
2615 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2616 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002617 if (test_bit(Faulty, &rdev->flags))
2618 err = 0;
2619 else
2620 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002621 } else if (cmd_match(buf, "remove")) {
2622 if (rdev->raid_disk >= 0)
2623 err = -EBUSY;
2624 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002625 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002626 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002627 if (mddev->pers)
2628 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002629 md_new_event(mddev);
2630 err = 0;
2631 }
NeilBrownf6556752006-06-26 00:28:01 -07002632 } else if (cmd_match(buf, "writemostly")) {
2633 set_bit(WriteMostly, &rdev->flags);
2634 err = 0;
2635 } else if (cmd_match(buf, "-writemostly")) {
2636 clear_bit(WriteMostly, &rdev->flags);
2637 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002638 } else if (cmd_match(buf, "blocked")) {
2639 set_bit(Blocked, &rdev->flags);
2640 err = 0;
2641 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002642 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002643 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002644 /* metadata handler doesn't understand badblocks,
2645 * so we need to fail the device
2646 */
2647 md_error(rdev->mddev, rdev);
2648 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002649 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002650 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002651 wake_up(&rdev->blocked_wait);
2652 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2653 md_wakeup_thread(rdev->mddev->thread);
2654
2655 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002656 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2657 set_bit(In_sync, &rdev->flags);
2658 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002659 } else if (cmd_match(buf, "write_error")) {
2660 set_bit(WriteErrorSeen, &rdev->flags);
2661 err = 0;
2662 } else if (cmd_match(buf, "-write_error")) {
2663 clear_bit(WriteErrorSeen, &rdev->flags);
2664 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002665 } else if (cmd_match(buf, "want_replacement")) {
2666 /* Any non-spare device that is not a replacement can
2667 * become want_replacement at any time, but we then need to
2668 * check if recovery is needed.
2669 */
2670 if (rdev->raid_disk >= 0 &&
2671 !test_bit(Replacement, &rdev->flags))
2672 set_bit(WantReplacement, &rdev->flags);
2673 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2674 md_wakeup_thread(rdev->mddev->thread);
2675 err = 0;
2676 } else if (cmd_match(buf, "-want_replacement")) {
2677 /* Clearing 'want_replacement' is always allowed.
2678 * Once replacements starts it is too late though.
2679 */
2680 err = 0;
2681 clear_bit(WantReplacement, &rdev->flags);
2682 } else if (cmd_match(buf, "replacement")) {
2683 /* Can only set a device as a replacement when array has not
2684 * yet been started. Once running, replacement is automatic
2685 * from spares, or by assigning 'slot'.
2686 */
2687 if (rdev->mddev->pers)
2688 err = -EBUSY;
2689 else {
2690 set_bit(Replacement, &rdev->flags);
2691 err = 0;
2692 }
2693 } else if (cmd_match(buf, "-replacement")) {
2694 /* Similarly, can only clear Replacement before start */
2695 if (rdev->mddev->pers)
2696 err = -EBUSY;
2697 else {
2698 clear_bit(Replacement, &rdev->flags);
2699 err = 0;
2700 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002701 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002702 if (!err)
2703 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002704 return err ? err : len;
2705}
NeilBrown80ca3a42006-07-10 04:44:18 -07002706static struct rdev_sysfs_entry rdev_state =
2707__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002708
2709static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002710errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002711{
2712 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2713}
2714
2715static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002716errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002717{
2718 char *e;
2719 unsigned long n = simple_strtoul(buf, &e, 10);
2720 if (*buf && (*e == 0 || *e == '\n')) {
2721 atomic_set(&rdev->corrected_errors, n);
2722 return len;
2723 }
2724 return -EINVAL;
2725}
2726static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002727__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002728
NeilBrown014236d2006-01-06 00:20:55 -08002729static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002730slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002731{
2732 if (rdev->raid_disk < 0)
2733 return sprintf(page, "none\n");
2734 else
2735 return sprintf(page, "%d\n", rdev->raid_disk);
2736}
2737
2738static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002739slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002740{
2741 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002742 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002743 int slot = simple_strtoul(buf, &e, 10);
2744 if (strncmp(buf, "none", 4)==0)
2745 slot = -1;
2746 else if (e==buf || (*e && *e!= '\n'))
2747 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002748 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002749 /* Setting 'slot' on an active array requires also
2750 * updating the 'rd%d' link, and communicating
2751 * with the personality with ->hot_*_disk.
2752 * For now we only support removing
2753 * failed/spare devices. This normally happens automatically,
2754 * but not when the metadata is externally managed.
2755 */
NeilBrownc303da62008-02-06 01:39:51 -08002756 if (rdev->raid_disk == -1)
2757 return -EEXIST;
2758 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002759 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002760 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002761 clear_bit(Blocked, &rdev->flags);
2762 remove_and_add_spares(rdev->mddev, rdev);
2763 if (rdev->raid_disk >= 0)
2764 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002765 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2766 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002767 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002768 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002769 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002770 */
2771
2772 if (rdev->raid_disk != -1)
2773 return -EBUSY;
2774
NeilBrownc6751b22011-02-02 11:57:13 +11002775 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2776 return -EBUSY;
2777
Neil Brown6c2fce22008-06-28 08:31:31 +10002778 if (rdev->mddev->pers->hot_add_disk == NULL)
2779 return -EINVAL;
2780
NeilBrownba1b41b2011-01-14 09:14:34 +11002781 if (slot >= rdev->mddev->raid_disks &&
2782 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2783 return -ENOSPC;
2784
Neil Brown6c2fce22008-06-28 08:31:31 +10002785 rdev->raid_disk = slot;
2786 if (test_bit(In_sync, &rdev->flags))
2787 rdev->saved_raid_disk = slot;
2788 else
2789 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002790 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002791 err = rdev->mddev->pers->
2792 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002793 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002795 return err;
Neil Brown52664732008-06-28 08:31:44 +10002796 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002797 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002798 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002799 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002800 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002801 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002802 if (slot >= rdev->mddev->raid_disks &&
2803 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002804 return -ENOSPC;
2805 rdev->raid_disk = slot;
2806 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002807 clear_bit(Faulty, &rdev->flags);
2808 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002809 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002810 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002811 }
NeilBrown014236d2006-01-06 00:20:55 -08002812 return len;
2813}
2814
2815
2816static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002817__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002818
NeilBrown93c8cad2006-01-06 00:20:56 -08002819static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002820offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002821{
Andrew Morton6961ece2006-01-06 00:20:59 -08002822 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002823}
2824
2825static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002826offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002827{
NeilBrownc6563a82012-05-21 09:27:00 +10002828 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002829 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002830 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002831 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002834 /* Must set offset before size, so overlap checks
2835 * can be sane */
2836 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002837 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002838 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002839 return len;
2840}
2841
2842static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002843__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002844
NeilBrownc6563a82012-05-21 09:27:00 +10002845static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2846{
2847 return sprintf(page, "%llu\n",
2848 (unsigned long long)rdev->new_data_offset);
2849}
2850
2851static ssize_t new_offset_store(struct md_rdev *rdev,
2852 const char *buf, size_t len)
2853{
2854 unsigned long long new_offset;
2855 struct mddev *mddev = rdev->mddev;
2856
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002857 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002858 return -EINVAL;
2859
2860 if (mddev->sync_thread)
2861 return -EBUSY;
2862 if (new_offset == rdev->data_offset)
2863 /* reset is always permitted */
2864 ;
2865 else if (new_offset > rdev->data_offset) {
2866 /* must not push array size beyond rdev_sectors */
2867 if (new_offset - rdev->data_offset
2868 + mddev->dev_sectors > rdev->sectors)
2869 return -E2BIG;
2870 }
2871 /* Metadata worries about other space details. */
2872
2873 /* decreasing the offset is inconsistent with a backwards
2874 * reshape.
2875 */
2876 if (new_offset < rdev->data_offset &&
2877 mddev->reshape_backwards)
2878 return -EINVAL;
2879 /* Increasing offset is inconsistent with forwards
2880 * reshape. reshape_direction should be set to
2881 * 'backwards' first.
2882 */
2883 if (new_offset > rdev->data_offset &&
2884 !mddev->reshape_backwards)
2885 return -EINVAL;
2886
2887 if (mddev->pers && mddev->persistent &&
2888 !super_types[mddev->major_version]
2889 .allow_new_offset(rdev, new_offset))
2890 return -E2BIG;
2891 rdev->new_data_offset = new_offset;
2892 if (new_offset > rdev->data_offset)
2893 mddev->reshape_backwards = 1;
2894 else if (new_offset < rdev->data_offset)
2895 mddev->reshape_backwards = 0;
2896
2897 return len;
2898}
2899static struct rdev_sysfs_entry rdev_new_offset =
2900__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2901
NeilBrown83303b62006-01-06 00:21:06 -08002902static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002903rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002904{
Andre Nolldd8ac332009-03-31 14:33:13 +11002905 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002906}
2907
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2909{
2910 /* check if two start/length pairs overlap */
2911 if (s1+l1 <= s2)
2912 return 0;
2913 if (s2+l2 <= s1)
2914 return 0;
2915 return 1;
2916}
2917
Dan Williamsb522adc2009-03-31 15:00:31 +11002918static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2919{
2920 unsigned long long blocks;
2921 sector_t new;
2922
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002923 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002924 return -EINVAL;
2925
2926 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2927 return -EINVAL; /* sector conversion overflow */
2928
2929 new = blocks * 2;
2930 if (new != blocks * 2)
2931 return -EINVAL; /* unsigned long long to sector_t overflow */
2932
2933 *sectors = new;
2934 return 0;
2935}
2936
NeilBrown83303b62006-01-06 00:21:06 -08002937static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002938rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002939{
NeilBrownfd01b882011-10-11 16:47:53 +11002940 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002941 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002942 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002943
Dan Williamsb522adc2009-03-31 15:00:31 +11002944 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002945 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002946 if (rdev->data_offset != rdev->new_data_offset)
2947 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002948 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002949 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002950 sectors = super_types[my_mddev->major_version].
2951 rdev_size_change(rdev, sectors);
2952 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002953 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002955 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002956 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002957 if (!my_mddev->pers->resize)
2958 /* Cannot change size for RAID0 or Linear etc */
2959 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002960 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002961 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002962 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 rdev->sectors = sectors;
2965 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002966 /* need to check that all other rdevs with the same ->bdev
2967 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002968 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002969 * we have to change it back, we will have the lock again.
2970 */
NeilBrownfd01b882011-10-11 16:47:53 +11002971 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002972 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002973 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002974
NeilBrown27c529b2008-03-04 14:29:33 -08002975 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002976 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002977 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002978
NeilBrown29f097c2013-11-14 17:54:51 +11002979 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002980 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002981 if (rdev->bdev == rdev2->bdev &&
2982 rdev != rdev2 &&
2983 overlaps(rdev->data_offset, rdev->sectors,
2984 rdev2->data_offset,
2985 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 overlap = 1;
2987 break;
2988 }
2989 mddev_unlock(mddev);
2990 if (overlap) {
2991 mddev_put(mddev);
2992 break;
2993 }
2994 }
NeilBrown29f097c2013-11-14 17:54:51 +11002995 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 if (overlap) {
2997 /* Someone else could have slipped in a size
2998 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002999 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000 * safe, and trust userspace not to race with
3001 * itself
3002 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003003 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 return -EBUSY;
3005 }
3006 }
NeilBrown83303b62006-01-06 00:21:06 -08003007 return len;
3008}
3009
3010static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003011__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003012
Dan Williams06e3c812009-12-12 21:17:12 -07003013
NeilBrown3cb03002011-10-11 16:45:26 +11003014static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003015{
3016 unsigned long long recovery_start = rdev->recovery_offset;
3017
3018 if (test_bit(In_sync, &rdev->flags) ||
3019 recovery_start == MaxSector)
3020 return sprintf(page, "none\n");
3021
3022 return sprintf(page, "%llu\n", recovery_start);
3023}
3024
NeilBrown3cb03002011-10-11 16:45:26 +11003025static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003026{
3027 unsigned long long recovery_start;
3028
3029 if (cmd_match(buf, "none"))
3030 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003031 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003032 return -EINVAL;
3033
3034 if (rdev->mddev->pers &&
3035 rdev->raid_disk >= 0)
3036 return -EBUSY;
3037
3038 rdev->recovery_offset = recovery_start;
3039 if (recovery_start == MaxSector)
3040 set_bit(In_sync, &rdev->flags);
3041 else
3042 clear_bit(In_sync, &rdev->flags);
3043 return len;
3044}
3045
3046static struct rdev_sysfs_entry rdev_recovery_start =
3047__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3048
NeilBrown16c791a2011-07-28 11:31:47 +10003049
3050static ssize_t
3051badblocks_show(struct badblocks *bb, char *page, int unack);
3052static ssize_t
3053badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3054
NeilBrown3cb03002011-10-11 16:45:26 +11003055static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003056{
3057 return badblocks_show(&rdev->badblocks, page, 0);
3058}
NeilBrown3cb03002011-10-11 16:45:26 +11003059static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003060{
NeilBrownde393cd2011-07-28 11:31:48 +10003061 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3062 /* Maybe that ack was all we needed */
3063 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3064 wake_up(&rdev->blocked_wait);
3065 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003066}
3067static struct rdev_sysfs_entry rdev_bad_blocks =
3068__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3069
3070
NeilBrown3cb03002011-10-11 16:45:26 +11003071static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003072{
3073 return badblocks_show(&rdev->badblocks, page, 1);
3074}
NeilBrown3cb03002011-10-11 16:45:26 +11003075static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003076{
3077 return badblocks_store(&rdev->badblocks, page, len, 1);
3078}
3079static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3080__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3081
NeilBrown86e6ffd2005-11-08 21:39:24 -08003082static struct attribute *rdev_default_attrs[] = {
3083 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003084 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003085 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003086 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003087 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003088 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003089 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003090 &rdev_bad_blocks.attr,
3091 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092 NULL,
3093};
3094static ssize_t
3095rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3096{
3097 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003098 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003099 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003100 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003101
3102 if (!entry->show)
3103 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003104
3105 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3106 if (!rv) {
3107 if (rdev->mddev == NULL)
3108 rv = -EBUSY;
3109 else
3110 rv = entry->show(rdev, page);
3111 mddev_unlock(mddev);
3112 }
3113 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003114}
3115
3116static ssize_t
3117rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3118 const char *page, size_t length)
3119{
3120 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003121 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003122 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003123 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003124
3125 if (!entry->store)
3126 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003127 if (!capable(CAP_SYS_ADMIN))
3128 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003129 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003130 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003131 if (rdev->mddev == NULL)
3132 rv = -EBUSY;
3133 else
3134 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003135 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003136 }
3137 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003138}
3139
3140static void rdev_free(struct kobject *ko)
3141{
NeilBrown3cb03002011-10-11 16:45:26 +11003142 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003143 kfree(rdev);
3144}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003145static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003146 .show = rdev_attr_show,
3147 .store = rdev_attr_store,
3148};
3149static struct kobj_type rdev_ktype = {
3150 .release = rdev_free,
3151 .sysfs_ops = &rdev_sysfs_ops,
3152 .default_attrs = rdev_default_attrs,
3153};
3154
NeilBrown3cb03002011-10-11 16:45:26 +11003155int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003156{
3157 rdev->desc_nr = -1;
3158 rdev->saved_raid_disk = -1;
3159 rdev->raid_disk = -1;
3160 rdev->flags = 0;
3161 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003162 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003163 rdev->sb_events = 0;
3164 rdev->last_read_error.tv_sec = 0;
3165 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003166 rdev->sb_loaded = 0;
3167 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003168 atomic_set(&rdev->nr_pending, 0);
3169 atomic_set(&rdev->read_errors, 0);
3170 atomic_set(&rdev->corrected_errors, 0);
3171
3172 INIT_LIST_HEAD(&rdev->same_set);
3173 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003174
3175 /* Add space to store bad block list.
3176 * This reserves the space even on arrays where it cannot
3177 * be used - I wonder if that matters
3178 */
3179 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003180 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003181 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3182 seqlock_init(&rdev->badblocks.lock);
3183 if (rdev->badblocks.page == NULL)
3184 return -ENOMEM;
3185
3186 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003187}
3188EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/*
3190 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3191 *
3192 * mark the device faulty if:
3193 *
3194 * - the device is nonexistent (zero size)
3195 * - the device has no valid superblock
3196 *
3197 * a faulty rdev _never_ has rdev->sb set.
3198 */
NeilBrown3cb03002011-10-11 16:45:26 +11003199static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 char b[BDEVNAME_SIZE];
3202 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003203 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 sector_t size;
3205
NeilBrown9ffae0c2006-01-06 00:20:32 -08003206 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (!rdev) {
3208 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3209 return ERR_PTR(-ENOMEM);
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
NeilBrown2230dfe2011-07-28 11:31:46 +10003212 err = md_rdev_init(rdev);
3213 if (err)
3214 goto abort_free;
3215 err = alloc_disk_sb(rdev);
3216 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 goto abort_free;
3218
NeilBrownc5d79ad2008-02-06 01:39:54 -08003219 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (err)
3221 goto abort_free;
3222
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003223 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003224
Mike Snitzer77304d22010-11-08 14:39:12 +01003225 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (!size) {
3227 printk(KERN_WARNING
3228 "md: %s has zero or unknown size, marking faulty!\n",
3229 bdevname(rdev->bdev,b));
3230 err = -EINVAL;
3231 goto abort_free;
3232 }
3233
3234 if (super_format >= 0) {
3235 err = super_types[super_format].
3236 load_super(rdev, NULL, super_minor);
3237 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003238 printk(KERN_WARNING
3239 "md: %s does not have a valid v%d.%d "
3240 "superblock, not importing!\n",
3241 bdevname(rdev->bdev,b),
3242 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 goto abort_free;
3244 }
3245 if (err < 0) {
3246 printk(KERN_WARNING
3247 "md: could not read %s's sb, not importing!\n",
3248 bdevname(rdev->bdev,b));
3249 goto abort_free;
3250 }
3251 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return rdev;
3254
3255abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003256 if (rdev->bdev)
3257 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003258 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 kfree(rdev);
3260 return ERR_PTR(err);
3261}
3262
3263/*
3264 * Check a full RAID array for plausibility
3265 */
3266
3267
NeilBrownfd01b882011-10-11 16:47:53 +11003268static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003271 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 char b[BDEVNAME_SIZE];
3273
3274 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003275 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 switch (super_types[mddev->major_version].
3277 load_super(rdev, freshest, mddev->minor_version)) {
3278 case 1:
3279 freshest = rdev;
3280 break;
3281 case 0:
3282 break;
3283 default:
3284 printk( KERN_ERR \
3285 "md: fatal superblock inconsistency in %s"
3286 " -- removing from array\n",
3287 bdevname(rdev->bdev,b));
3288 kick_rdev_from_array(rdev);
3289 }
3290
3291
3292 super_types[mddev->major_version].
3293 validate_super(mddev, freshest);
3294
3295 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003296 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003297 if (mddev->max_disks &&
3298 (rdev->desc_nr >= mddev->max_disks ||
3299 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003300 printk(KERN_WARNING
3301 "md: %s: %s: only %d devices permitted\n",
3302 mdname(mddev), bdevname(rdev->bdev, b),
3303 mddev->max_disks);
3304 kick_rdev_from_array(rdev);
3305 continue;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (rdev != freshest)
3308 if (super_types[mddev->major_version].
3309 validate_super(mddev, rdev)) {
3310 printk(KERN_WARNING "md: kicking non-fresh %s"
3311 " from array!\n",
3312 bdevname(rdev->bdev,b));
3313 kick_rdev_from_array(rdev);
3314 continue;
3315 }
3316 if (mddev->level == LEVEL_MULTIPATH) {
3317 rdev->desc_nr = i++;
3318 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003319 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003320 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003321 rdev->raid_disk = -1;
3322 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
NeilBrown72e02072009-12-14 12:49:55 +11003327/* Read a fixed-point number.
3328 * Numbers in sysfs attributes should be in "standard" units where
3329 * possible, so time should be in seconds.
3330 * However we internally use a a much smaller unit such as
3331 * milliseconds or jiffies.
3332 * This function takes a decimal number with a possible fractional
3333 * component, and produces an integer which is the result of
3334 * multiplying that number by 10^'scale'.
3335 * all without any floating-point arithmetic.
3336 */
3337int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3338{
3339 unsigned long result = 0;
3340 long decimals = -1;
3341 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3342 if (*cp == '.')
3343 decimals = 0;
3344 else if (decimals < scale) {
3345 unsigned int value;
3346 value = *cp - '0';
3347 result = result * 10 + value;
3348 if (decimals >= 0)
3349 decimals++;
3350 }
3351 cp++;
3352 }
3353 if (*cp == '\n')
3354 cp++;
3355 if (*cp)
3356 return -EINVAL;
3357 if (decimals < 0)
3358 decimals = 0;
3359 while (decimals < scale) {
3360 result *= 10;
3361 decimals ++;
3362 }
3363 *res = result;
3364 return 0;
3365}
3366
3367
NeilBrown19052c02008-08-05 15:54:13 +10003368static void md_safemode_timeout(unsigned long data);
3369
NeilBrowneae17012005-11-08 21:39:23 -08003370static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003371safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003372{
3373 int msec = (mddev->safemode_delay*1000)/HZ;
3374 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3375}
3376static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003377safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003378{
NeilBrown16f17b32006-06-26 00:27:37 -07003379 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003380
NeilBrown72e02072009-12-14 12:49:55 +11003381 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003382 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003383 if (msec == 0)
3384 mddev->safemode_delay = 0;
3385 else {
NeilBrown19052c02008-08-05 15:54:13 +10003386 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003387 mddev->safemode_delay = (msec*HZ)/1000;
3388 if (mddev->safemode_delay == 0)
3389 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003390 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003391 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003392 }
3393 return len;
3394}
3395static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003396__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003397
3398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003399level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003400{
NeilBrown84fc4b52011-10-11 16:49:58 +11003401 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003402 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003403 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003404 else if (mddev->clevel[0])
3405 return sprintf(page, "%s\n", mddev->clevel);
3406 else if (mddev->level != LEVEL_NONE)
3407 return sprintf(page, "%d\n", mddev->level);
3408 else
3409 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003410}
3411
NeilBrownd9d166c2006-01-06 00:20:51 -08003412static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003413level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003414{
Dan Williamsf2859af2010-05-02 10:04:16 -07003415 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003416 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003417 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003418 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003419 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003420 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003421
3422 if (mddev->pers == NULL) {
3423 if (len == 0)
3424 return 0;
3425 if (len >= sizeof(mddev->clevel))
3426 return -ENOSPC;
3427 strncpy(mddev->clevel, buf, len);
3428 if (mddev->clevel[len-1] == '\n')
3429 len--;
3430 mddev->clevel[len] = 0;
3431 mddev->level = LEVEL_NONE;
3432 return rv;
3433 }
3434
3435 /* request to change the personality. Need to ensure:
3436 * - array is not engaged in resync/recovery/reshape
3437 * - old personality can be suspended
3438 * - new personality will access other array.
3439 */
3440
NeilBrownbb4f1e92010-08-08 21:18:03 +10003441 if (mddev->sync_thread ||
3442 mddev->reshape_position != MaxSector ||
3443 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003444 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003445
3446 if (!mddev->pers->quiesce) {
3447 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3448 mdname(mddev), mddev->pers->name);
3449 return -EINVAL;
3450 }
3451
3452 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003453 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003454 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003455 strncpy(clevel, buf, len);
3456 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003457 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003458 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003459 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003460 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003461
Dan Williamsf2859af2010-05-02 10:04:16 -07003462 if (request_module("md-%s", clevel) != 0)
3463 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003464 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003465 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003466 if (!pers || !try_module_get(pers->owner)) {
3467 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003469 return -EINVAL;
3470 }
3471 spin_unlock(&pers_lock);
3472
3473 if (pers == mddev->pers) {
3474 /* Nothing to do! */
3475 module_put(pers->owner);
3476 return rv;
3477 }
3478 if (!pers->takeover) {
3479 module_put(pers->owner);
3480 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003481 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003482 return -EINVAL;
3483 }
3484
NeilBrowndafb20f2012-03-19 12:46:39 +11003485 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003486 rdev->new_raid_disk = rdev->raid_disk;
3487
NeilBrown245f46c2009-03-31 14:39:39 +11003488 /* ->takeover must set new_* and/or delta_disks
3489 * if it succeeds, and may set them when it fails.
3490 */
3491 priv = pers->takeover(mddev);
3492 if (IS_ERR(priv)) {
3493 mddev->new_level = mddev->level;
3494 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003495 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003496 mddev->raid_disks -= mddev->delta_disks;
3497 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003498 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003499 module_put(pers->owner);
3500 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003501 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003502 return PTR_ERR(priv);
3503 }
3504
3505 /* Looks like we have a winner */
3506 mddev_suspend(mddev);
3507 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003508
3509 if (mddev->pers->sync_request == NULL &&
3510 pers->sync_request != NULL) {
3511 /* need to add the md_redundancy_group */
3512 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3513 printk(KERN_WARNING
3514 "md: cannot register extra attributes for %s\n",
3515 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003516 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003517 }
3518 if (mddev->pers->sync_request != NULL &&
3519 pers->sync_request == NULL) {
3520 /* need to remove the md_redundancy_group */
3521 if (mddev->to_remove == NULL)
3522 mddev->to_remove = &md_redundancy_group;
3523 }
3524
Trela Maciej54071b32010-03-08 16:02:42 +11003525 if (mddev->pers->sync_request == NULL &&
3526 mddev->external) {
3527 /* We are converting from a no-redundancy array
3528 * to a redundancy array and metadata is managed
3529 * externally so we need to be sure that writes
3530 * won't block due to a need to transition
3531 * clean->dirty
3532 * until external management is started.
3533 */
3534 mddev->in_sync = 0;
3535 mddev->safemode_delay = 0;
3536 mddev->safemode = 0;
3537 }
3538
NeilBrowndafb20f2012-03-19 12:46:39 +11003539 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003540 if (rdev->raid_disk < 0)
3541 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003542 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003543 rdev->new_raid_disk = -1;
3544 if (rdev->new_raid_disk == rdev->raid_disk)
3545 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003546 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003547 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003548 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003549 if (rdev->raid_disk < 0)
3550 continue;
3551 if (rdev->new_raid_disk == rdev->raid_disk)
3552 continue;
3553 rdev->raid_disk = rdev->new_raid_disk;
3554 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003555 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003556 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003557 if (sysfs_link_rdev(mddev, rdev))
3558 printk(KERN_WARNING "md: cannot register rd%d"
3559 " for %s after level change\n",
3560 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003561 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003562 }
3563
3564 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003565 mddev->pers = pers;
3566 mddev->private = priv;
3567 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3568 mddev->level = mddev->new_level;
3569 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003570 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003571 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003572 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003573 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003574 if (mddev->pers->sync_request == NULL) {
3575 /* this is now an array without redundancy, so
3576 * it must always be in_sync
3577 */
3578 mddev->in_sync = 1;
3579 del_timer_sync(&mddev->safemode_timer);
3580 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003581 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003582 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003583 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003584 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003585 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003586 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003587 return rv;
3588}
3589
3590static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003591__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003592
NeilBrownd4dbd022006-06-26 00:27:59 -07003593
3594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003595layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003596{
3597 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003598 if (mddev->reshape_position != MaxSector &&
3599 mddev->layout != mddev->new_layout)
3600 return sprintf(page, "%d (%d)\n",
3601 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003602 return sprintf(page, "%d\n", mddev->layout);
3603}
3604
3605static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003606layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003607{
3608 char *e;
3609 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003610
3611 if (!*buf || (*e && *e != '\n'))
3612 return -EINVAL;
3613
NeilBrownb3546032009-03-31 14:56:41 +11003614 if (mddev->pers) {
3615 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003616 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003617 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003618 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003619 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003620 if (err) {
3621 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003622 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003623 }
NeilBrownb3546032009-03-31 14:56:41 +11003624 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003625 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003626 if (mddev->reshape_position == MaxSector)
3627 mddev->layout = n;
3628 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003629 return len;
3630}
3631static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003632__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003633
3634
NeilBrowneae17012005-11-08 21:39:23 -08003635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003636raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003637{
NeilBrownbb636542005-11-08 21:39:45 -08003638 if (mddev->raid_disks == 0)
3639 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003640 if (mddev->reshape_position != MaxSector &&
3641 mddev->delta_disks != 0)
3642 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3643 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003644 return sprintf(page, "%d\n", mddev->raid_disks);
3645}
3646
NeilBrownfd01b882011-10-11 16:47:53 +11003647static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003648
3649static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003650raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003651{
NeilBrownda943b992006-01-06 00:20:54 -08003652 char *e;
3653 int rv = 0;
3654 unsigned long n = simple_strtoul(buf, &e, 10);
3655
3656 if (!*buf || (*e && *e != '\n'))
3657 return -EINVAL;
3658
3659 if (mddev->pers)
3660 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003661 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003662 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003663 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003664
3665 rdev_for_each(rdev, mddev) {
3666 if (olddisks < n &&
3667 rdev->data_offset < rdev->new_data_offset)
3668 return -EINVAL;
3669 if (olddisks > n &&
3670 rdev->data_offset > rdev->new_data_offset)
3671 return -EINVAL;
3672 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003673 mddev->delta_disks = n - olddisks;
3674 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003675 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003676 } else
NeilBrownda943b992006-01-06 00:20:54 -08003677 mddev->raid_disks = n;
3678 return rv ? rv : len;
3679}
3680static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003681__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003682
NeilBrown24dd4692005-11-08 21:39:26 -08003683static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003684chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003685{
NeilBrown08a02ec2007-05-09 02:35:38 -07003686 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003687 mddev->chunk_sectors != mddev->new_chunk_sectors)
3688 return sprintf(page, "%d (%d)\n",
3689 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003690 mddev->chunk_sectors << 9);
3691 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003692}
3693
3694static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003695chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003696{
NeilBrown3b343802006-01-06 00:20:47 -08003697 char *e;
3698 unsigned long n = simple_strtoul(buf, &e, 10);
3699
NeilBrown3b343802006-01-06 00:20:47 -08003700 if (!*buf || (*e && *e != '\n'))
3701 return -EINVAL;
3702
NeilBrownb3546032009-03-31 14:56:41 +11003703 if (mddev->pers) {
3704 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003705 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003706 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003707 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003708 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003709 if (err) {
3710 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003711 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003712 }
NeilBrownb3546032009-03-31 14:56:41 +11003713 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003714 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003715 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003716 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003717 }
NeilBrown3b343802006-01-06 00:20:47 -08003718 return len;
3719}
3720static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003721__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003722
NeilBrowna94213b2006-06-26 00:28:00 -07003723static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003724resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003725{
NeilBrownd1a7c502009-03-31 15:24:32 +11003726 if (mddev->recovery_cp == MaxSector)
3727 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003728 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3729}
3730
3731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003732resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003733{
NeilBrowna94213b2006-06-26 00:28:00 -07003734 char *e;
3735 unsigned long long n = simple_strtoull(buf, &e, 10);
3736
NeilBrownb0986362011-05-11 15:52:21 +10003737 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003738 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003739 if (cmd_match(buf, "none"))
3740 n = MaxSector;
3741 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003742 return -EINVAL;
3743
3744 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003745 if (mddev->pers)
3746 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003747 return len;
3748}
3749static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003750__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003751
NeilBrown9e653b62006-06-26 00:27:58 -07003752/*
3753 * The array state can be:
3754 *
3755 * clear
3756 * No devices, no size, no level
3757 * Equivalent to STOP_ARRAY ioctl
3758 * inactive
3759 * May have some settings, but array is not active
3760 * all IO results in error
3761 * When written, doesn't tear down array, but just stops it
3762 * suspended (not supported yet)
3763 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003764 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003765 * readonly
3766 * no resync can happen. no superblocks get written.
3767 * write requests fail
3768 * read-auto
3769 * like readonly, but behaves like 'clean' on a write request.
3770 *
3771 * clean - no pending writes, but otherwise active.
3772 * When written to inactive array, starts without resync
3773 * If a write request arrives then
3774 * if metadata is known, mark 'dirty' and switch to 'active'.
3775 * if not known, block and switch to write-pending
3776 * If written to an active array that has pending writes, then fails.
3777 * active
3778 * fully active: IO and resync can be happening.
3779 * When written to inactive array, starts with resync
3780 *
3781 * write-pending
3782 * clean, but writes are blocked waiting for 'active' to be written.
3783 *
3784 * active-idle
3785 * like active, but no writes have been seen for a while (100msec).
3786 *
3787 */
3788enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3789 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003790static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003791 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3792 "write-pending", "active-idle", NULL };
3793
3794static int match_word(const char *word, char **list)
3795{
3796 int n;
3797 for (n=0; list[n]; n++)
3798 if (cmd_match(word, list[n]))
3799 break;
3800 return n;
3801}
3802
3803static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003804array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003805{
3806 enum array_state st = inactive;
3807
3808 if (mddev->pers)
3809 switch(mddev->ro) {
3810 case 1:
3811 st = readonly;
3812 break;
3813 case 2:
3814 st = read_auto;
3815 break;
3816 case 0:
3817 if (mddev->in_sync)
3818 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003819 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003820 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003821 else if (mddev->safemode)
3822 st = active_idle;
3823 else
3824 st = active;
3825 }
3826 else {
3827 if (list_empty(&mddev->disks) &&
3828 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003829 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003830 st = clear;
3831 else
3832 st = inactive;
3833 }
3834 return sprintf(page, "%s\n", array_states[st]);
3835}
3836
NeilBrowna05b7ea02012-07-19 15:59:18 +10003837static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3838static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003839static int do_md_run(struct mddev * mddev);
3840static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003841
3842static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003843array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003844{
3845 int err = -EINVAL;
3846 enum array_state st = match_word(buf, array_states);
3847 switch(st) {
3848 case bad_word:
3849 break;
3850 case clear:
3851 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003852 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003853 break;
3854 case inactive:
3855 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003856 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003857 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003858 else
NeilBrowne6910632008-02-06 01:39:51 -08003859 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003860 break;
3861 case suspended:
3862 break; /* not supported yet */
3863 case readonly:
3864 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003865 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003866 else {
3867 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003868 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003869 err = do_md_run(mddev);
3870 }
3871 break;
3872 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003873 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003874 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003875 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003876 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003877 err = restart_array(mddev);
3878 if (err == 0) {
3879 mddev->ro = 2;
3880 set_disk_ro(mddev->gendisk, 0);
3881 }
NeilBrown9e653b62006-06-26 00:27:58 -07003882 } else {
3883 mddev->ro = 2;
3884 err = do_md_run(mddev);
3885 }
3886 break;
3887 case clean:
3888 if (mddev->pers) {
3889 restart_array(mddev);
3890 spin_lock_irq(&mddev->write_lock);
3891 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003892 if (mddev->in_sync == 0) {
3893 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003894 if (mddev->safemode == 1)
3895 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003896 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003897 }
3898 err = 0;
3899 } else
3900 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003901 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003902 } else
3903 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003904 break;
3905 case active:
3906 if (mddev->pers) {
3907 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003908 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003909 wake_up(&mddev->sb_wait);
3910 err = 0;
3911 } else {
3912 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003913 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003914 err = do_md_run(mddev);
3915 }
3916 break;
3917 case write_pending:
3918 case active_idle:
3919 /* these cannot be set */
3920 break;
3921 }
3922 if (err)
3923 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003924 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003925 if (mddev->hold_active == UNTIL_IOCTL)
3926 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003927 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003928 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003929 }
NeilBrown9e653b62006-06-26 00:27:58 -07003930}
NeilBrown80ca3a42006-07-10 04:44:18 -07003931static struct md_sysfs_entry md_array_state =
3932__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003933
NeilBrown6d7ff732006-01-06 00:21:16 -08003934static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003935max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003936 return sprintf(page, "%d\n",
3937 atomic_read(&mddev->max_corr_read_errors));
3938}
3939
3940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003941max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003942{
3943 char *e;
3944 unsigned long n = simple_strtoul(buf, &e, 10);
3945
3946 if (*buf && (*e == 0 || *e == '\n')) {
3947 atomic_set(&mddev->max_corr_read_errors, n);
3948 return len;
3949 }
3950 return -EINVAL;
3951}
3952
3953static struct md_sysfs_entry max_corr_read_errors =
3954__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3955 max_corrected_read_errors_store);
3956
3957static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003958null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003959{
3960 return -EINVAL;
3961}
3962
3963static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003964new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003965{
3966 /* buf must be %d:%d\n? giving major and minor numbers */
3967 /* The new device is added to the array.
3968 * If the array has a persistent superblock, we read the
3969 * superblock to initialise info and check validity.
3970 * Otherwise, only checking done is that in bind_rdev_to_array,
3971 * which mainly checks size.
3972 */
3973 char *e;
3974 int major = simple_strtoul(buf, &e, 10);
3975 int minor;
3976 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003977 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003978 int err;
3979
3980 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3981 return -EINVAL;
3982 minor = simple_strtoul(e+1, &e, 10);
3983 if (*e && *e != '\n')
3984 return -EINVAL;
3985 dev = MKDEV(major, minor);
3986 if (major != MAJOR(dev) ||
3987 minor != MINOR(dev))
3988 return -EOVERFLOW;
3989
3990
3991 if (mddev->persistent) {
3992 rdev = md_import_device(dev, mddev->major_version,
3993 mddev->minor_version);
3994 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003995 struct md_rdev *rdev0
3996 = list_entry(mddev->disks.next,
3997 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003998 err = super_types[mddev->major_version]
3999 .load_super(rdev, rdev0, mddev->minor_version);
4000 if (err < 0)
4001 goto out;
4002 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004003 } else if (mddev->external)
4004 rdev = md_import_device(dev, -2, -1);
4005 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004006 rdev = md_import_device(dev, -1, -1);
4007
4008 if (IS_ERR(rdev))
4009 return PTR_ERR(rdev);
4010 err = bind_rdev_to_array(rdev, mddev);
4011 out:
4012 if (err)
4013 export_rdev(rdev);
4014 return err ? err : len;
4015}
4016
4017static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004018__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004019
4020static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004021bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004022{
4023 char *end;
4024 unsigned long chunk, end_chunk;
4025
4026 if (!mddev->bitmap)
4027 goto out;
4028 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4029 while (*buf) {
4030 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4031 if (buf == end) break;
4032 if (*end == '-') { /* range */
4033 buf = end + 1;
4034 end_chunk = simple_strtoul(buf, &end, 0);
4035 if (buf == end) break;
4036 }
4037 if (*end && !isspace(*end)) break;
4038 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004039 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004040 }
4041 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4042out:
4043 return len;
4044}
4045
4046static struct md_sysfs_entry md_bitmap =
4047__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4048
4049static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004050size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004051{
Andre Noll58c0fed2009-03-31 14:33:13 +11004052 return sprintf(page, "%llu\n",
4053 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004054}
4055
NeilBrownfd01b882011-10-11 16:47:53 +11004056static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004057
4058static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004059size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004060{
4061 /* If array is inactive, we can reduce the component size, but
4062 * not increase it (except from 0).
4063 * If array is active, we can try an on-line resize
4064 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004065 sector_t sectors;
4066 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004067
Andre Noll58c0fed2009-03-31 14:33:13 +11004068 if (err < 0)
4069 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004070 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004071 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004072 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004073 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004074 if (mddev->dev_sectors == 0 ||
4075 mddev->dev_sectors > sectors)
4076 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004077 else
4078 err = -ENOSPC;
4079 }
4080 return err ? err : len;
4081}
4082
4083static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004084__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004085
NeilBrown8bb93aa2006-01-06 00:20:50 -08004086
Masanari Iida83f0d772012-10-30 00:18:08 +09004087/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004088 * This is one of
4089 * 'none' for arrays with no metadata (good luck...)
4090 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004091 * or N.M for internally known formats
4092 */
4093static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004094metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004095{
4096 if (mddev->persistent)
4097 return sprintf(page, "%d.%d\n",
4098 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004099 else if (mddev->external)
4100 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004101 else
4102 return sprintf(page, "none\n");
4103}
4104
4105static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004106metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004107{
4108 int major, minor;
4109 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004110 /* Changing the details of 'external' metadata is
4111 * always permitted. Otherwise there must be
4112 * no devices attached to the array.
4113 */
4114 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4115 ;
4116 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004117 return -EBUSY;
4118
4119 if (cmd_match(buf, "none")) {
4120 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004121 mddev->external = 0;
4122 mddev->major_version = 0;
4123 mddev->minor_version = 90;
4124 return len;
4125 }
4126 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004127 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004128 if (namelen >= sizeof(mddev->metadata_type))
4129 namelen = sizeof(mddev->metadata_type)-1;
4130 strncpy(mddev->metadata_type, buf+9, namelen);
4131 mddev->metadata_type[namelen] = 0;
4132 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4133 mddev->metadata_type[--namelen] = 0;
4134 mddev->persistent = 0;
4135 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004136 mddev->major_version = 0;
4137 mddev->minor_version = 90;
4138 return len;
4139 }
4140 major = simple_strtoul(buf, &e, 10);
4141 if (e==buf || *e != '.')
4142 return -EINVAL;
4143 buf = e+1;
4144 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004145 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004147 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148 return -ENOENT;
4149 mddev->major_version = major;
4150 mddev->minor_version = minor;
4151 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004152 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004153 return len;
4154}
4155
4156static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004157__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004158
NeilBrowna35b0d62006-01-06 00:20:49 -08004159static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004160action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004161{
NeilBrown7eec3142005-11-08 21:39:44 -08004162 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004163 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4164 type = "frozen";
4165 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004166 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004167 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4168 type = "reshape";
4169 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004170 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4171 type = "resync";
4172 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4173 type = "check";
4174 else
4175 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004176 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004177 type = "recover";
4178 }
4179 return sprintf(page, "%s\n", type);
4180}
4181
4182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004183action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004184{
NeilBrown7eec3142005-11-08 21:39:44 -08004185 if (!mddev->pers || !mddev->pers->sync_request)
4186 return -EINVAL;
4187
NeilBrownb6a9ce62009-05-26 09:41:17 +10004188 if (cmd_match(page, "frozen"))
4189 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4190 else
4191 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4192
4193 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004194 if (mddev->sync_thread) {
4195 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004196 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004197 }
NeilBrown03c902e2006-01-06 00:20:46 -08004198 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4199 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004200 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004201 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004202 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004203 else if (cmd_match(page, "recover")) {
4204 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4205 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4206 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004207 int err;
4208 if (mddev->pers->start_reshape == NULL)
4209 return -EINVAL;
4210 err = mddev->pers->start_reshape(mddev);
4211 if (err)
4212 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004213 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004214 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004215 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004216 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004217 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004218 return -EINVAL;
4219 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4220 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004221 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004222 if (mddev->ro == 2) {
4223 /* A write to sync_action is enough to justify
4224 * canceling read-auto mode
4225 */
4226 mddev->ro = 0;
4227 md_wakeup_thread(mddev->sync_thread);
4228 }
NeilBrown03c902e2006-01-06 00:20:46 -08004229 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004230 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004231 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004232 return len;
4233}
4234
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004235static struct md_sysfs_entry md_scan_mode =
4236__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4237
4238static ssize_t
4239last_sync_action_show(struct mddev *mddev, char *page)
4240{
4241 return sprintf(page, "%s\n", mddev->last_sync_action);
4242}
4243
4244static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4245
NeilBrown9d888832005-11-08 21:39:26 -08004246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004247mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004248{
4249 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004250 (unsigned long long)
4251 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004252}
4253
NeilBrown80ca3a42006-07-10 04:44:18 -07004254static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004255
NeilBrown88202a02006-01-06 00:21:36 -08004256static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004257sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004258{
4259 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4260 mddev->sync_speed_min ? "local": "system");
4261}
4262
4263static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004264sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004265{
4266 int min;
4267 char *e;
4268 if (strncmp(buf, "system", 6)==0) {
4269 mddev->sync_speed_min = 0;
4270 return len;
4271 }
4272 min = simple_strtoul(buf, &e, 10);
4273 if (buf == e || (*e && *e != '\n') || min <= 0)
4274 return -EINVAL;
4275 mddev->sync_speed_min = min;
4276 return len;
4277}
4278
4279static struct md_sysfs_entry md_sync_min =
4280__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4281
4282static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004283sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004284{
4285 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4286 mddev->sync_speed_max ? "local": "system");
4287}
4288
4289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004290sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004291{
4292 int max;
4293 char *e;
4294 if (strncmp(buf, "system", 6)==0) {
4295 mddev->sync_speed_max = 0;
4296 return len;
4297 }
4298 max = simple_strtoul(buf, &e, 10);
4299 if (buf == e || (*e && *e != '\n') || max <= 0)
4300 return -EINVAL;
4301 mddev->sync_speed_max = max;
4302 return len;
4303}
4304
4305static struct md_sysfs_entry md_sync_max =
4306__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4307
Iustin Popd7f3d292007-10-16 23:30:54 -07004308static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004309degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004310{
4311 return sprintf(page, "%d\n", mddev->degraded);
4312}
4313static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004314
4315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004316sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004317{
4318 return sprintf(page, "%d\n", mddev->parallel_resync);
4319}
4320
4321static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004322sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004323{
4324 long n;
4325
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004326 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004327 return -EINVAL;
4328
4329 if (n != 0 && n != 1)
4330 return -EINVAL;
4331
4332 mddev->parallel_resync = n;
4333
4334 if (mddev->sync_thread)
4335 wake_up(&resync_wait);
4336
4337 return len;
4338}
4339
4340/* force parallel resync, even with shared block devices */
4341static struct md_sysfs_entry md_sync_force_parallel =
4342__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4343 sync_force_parallel_show, sync_force_parallel_store);
4344
4345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004346sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004347{
4348 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004349 if (mddev->curr_resync == 0)
4350 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004351 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4352 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004353 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004354 db = resync - mddev->resync_mark_cnt;
4355 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004356}
4357
NeilBrown80ca3a42006-07-10 04:44:18 -07004358static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004359
4360static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004361sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004362{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004363 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004364
NeilBrownacb180b2009-04-14 16:28:34 +10004365 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4366 return sprintf(page, "none\n");
4367
NeilBrown72f36d52012-10-11 14:25:57 +11004368 if (mddev->curr_resync == 1 ||
4369 mddev->curr_resync == 2)
4370 return sprintf(page, "delayed\n");
4371
NeilBrownc804cde2012-05-21 09:28:33 +10004372 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4373 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004374 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004375 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004376 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004377
NeilBrownacb180b2009-04-14 16:28:34 +10004378 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004379 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004380}
4381
NeilBrown80ca3a42006-07-10 04:44:18 -07004382static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004383
NeilBrowne464eaf2006-03-27 01:18:14 -08004384static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004385min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004386{
4387 return sprintf(page, "%llu\n",
4388 (unsigned long long)mddev->resync_min);
4389}
4390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004392{
4393 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004394 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004395 return -EINVAL;
4396 if (min > mddev->resync_max)
4397 return -EINVAL;
4398 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4399 return -EBUSY;
4400
4401 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004402 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004403 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004404 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004405 return -EINVAL;
4406 }
4407 mddev->resync_min = min;
4408
4409 return len;
4410}
4411
4412static struct md_sysfs_entry md_min_sync =
4413__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4414
4415static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004416max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004417{
4418 if (mddev->resync_max == MaxSector)
4419 return sprintf(page, "max\n");
4420 else
4421 return sprintf(page, "%llu\n",
4422 (unsigned long long)mddev->resync_max);
4423}
4424static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004425max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004426{
4427 if (strncmp(buf, "max", 3) == 0)
4428 mddev->resync_max = MaxSector;
4429 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004430 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004431 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004432 return -EINVAL;
4433 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004434 return -EINVAL;
4435 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004436 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004437 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4438 return -EBUSY;
4439
4440 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004441 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004442 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004443 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004444 return -EINVAL;
4445 }
4446 mddev->resync_max = max;
4447 }
4448 wake_up(&mddev->recovery_wait);
4449 return len;
4450}
4451
4452static struct md_sysfs_entry md_max_sync =
4453__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4454
4455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004456suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004457{
4458 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4459}
4460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004463{
4464 char *e;
4465 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004466 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004467
NeilBrownb8d966e2009-07-01 11:14:04 +10004468 if (mddev->pers == NULL ||
4469 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004470 return -EINVAL;
4471 if (buf == e || (*e && *e != '\n'))
4472 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004473
4474 mddev->suspend_lo = new;
4475 if (new >= old)
4476 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004477 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004478 else {
4479 /* Expanding suspended region - need to wait */
4480 mddev->pers->quiesce(mddev, 1);
4481 mddev->pers->quiesce(mddev, 0);
4482 }
4483 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004484}
4485static struct md_sysfs_entry md_suspend_lo =
4486__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4487
4488
4489static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004490suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004491{
4492 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4493}
4494
4495static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004496suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004497{
4498 char *e;
4499 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004500 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004501
NeilBrownb8d966e2009-07-01 11:14:04 +10004502 if (mddev->pers == NULL ||
4503 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004504 return -EINVAL;
4505 if (buf == e || (*e && *e != '\n'))
4506 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004507
4508 mddev->suspend_hi = new;
4509 if (new <= old)
4510 /* Shrinking suspended region */
4511 mddev->pers->quiesce(mddev, 2);
4512 else {
4513 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004514 mddev->pers->quiesce(mddev, 1);
4515 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004516 }
4517 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004518}
4519static struct md_sysfs_entry md_suspend_hi =
4520__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4521
NeilBrown08a02ec2007-05-09 02:35:38 -07004522static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004523reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004524{
4525 if (mddev->reshape_position != MaxSector)
4526 return sprintf(page, "%llu\n",
4527 (unsigned long long)mddev->reshape_position);
4528 strcpy(page, "none\n");
4529 return 5;
4530}
4531
4532static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004533reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004534{
NeilBrownc6563a82012-05-21 09:27:00 +10004535 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004536 char *e;
4537 unsigned long long new = simple_strtoull(buf, &e, 10);
4538 if (mddev->pers)
4539 return -EBUSY;
4540 if (buf == e || (*e && *e != '\n'))
4541 return -EINVAL;
4542 mddev->reshape_position = new;
4543 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004544 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004545 mddev->new_level = mddev->level;
4546 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004547 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004548 rdev_for_each(rdev, mddev)
4549 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004550 return len;
4551}
4552
4553static struct md_sysfs_entry md_reshape_position =
4554__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4555 reshape_position_store);
4556
Dan Williamsb522adc2009-03-31 15:00:31 +11004557static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004558reshape_direction_show(struct mddev *mddev, char *page)
4559{
4560 return sprintf(page, "%s\n",
4561 mddev->reshape_backwards ? "backwards" : "forwards");
4562}
4563
4564static ssize_t
4565reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4566{
4567 int backwards = 0;
4568 if (cmd_match(buf, "forwards"))
4569 backwards = 0;
4570 else if (cmd_match(buf, "backwards"))
4571 backwards = 1;
4572 else
4573 return -EINVAL;
4574 if (mddev->reshape_backwards == backwards)
4575 return len;
4576
4577 /* check if we are allowed to change */
4578 if (mddev->delta_disks)
4579 return -EBUSY;
4580
4581 if (mddev->persistent &&
4582 mddev->major_version == 0)
4583 return -EINVAL;
4584
4585 mddev->reshape_backwards = backwards;
4586 return len;
4587}
4588
4589static struct md_sysfs_entry md_reshape_direction =
4590__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4591 reshape_direction_store);
4592
4593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004594array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004595{
4596 if (mddev->external_size)
4597 return sprintf(page, "%llu\n",
4598 (unsigned long long)mddev->array_sectors/2);
4599 else
4600 return sprintf(page, "default\n");
4601}
4602
4603static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004604array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004605{
4606 sector_t sectors;
4607
4608 if (strncmp(buf, "default", 7) == 0) {
4609 if (mddev->pers)
4610 sectors = mddev->pers->size(mddev, 0, 0);
4611 else
4612 sectors = mddev->array_sectors;
4613
4614 mddev->external_size = 0;
4615 } else {
4616 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4617 return -EINVAL;
4618 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004619 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004620
4621 mddev->external_size = 1;
4622 }
4623
4624 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004625 if (mddev->pers) {
4626 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004627 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004628 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004629 return len;
4630}
4631
4632static struct md_sysfs_entry md_array_size =
4633__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4634 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004635
NeilBrowneae17012005-11-08 21:39:23 -08004636static struct attribute *md_default_attrs[] = {
4637 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004638 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004639 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004640 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004641 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004642 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004643 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004644 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004645 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004646 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004647 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004648 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004649 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004650 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004651 NULL,
4652};
4653
4654static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004655 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004656 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004657 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004658 &md_sync_min.attr,
4659 &md_sync_max.attr,
4660 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004661 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004662 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004663 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004664 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004665 &md_suspend_lo.attr,
4666 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004667 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004668 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004669 NULL,
4670};
NeilBrown411036f2005-11-08 21:39:40 -08004671static struct attribute_group md_redundancy_group = {
4672 .name = NULL,
4673 .attrs = md_redundancy_attrs,
4674};
4675
NeilBrowneae17012005-11-08 21:39:23 -08004676
4677static ssize_t
4678md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4679{
4680 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004681 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004682 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004683
4684 if (!entry->show)
4685 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004686 spin_lock(&all_mddevs_lock);
4687 if (list_empty(&mddev->all_mddevs)) {
4688 spin_unlock(&all_mddevs_lock);
4689 return -EBUSY;
4690 }
4691 mddev_get(mddev);
4692 spin_unlock(&all_mddevs_lock);
4693
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004694 rv = mddev_lock(mddev);
4695 if (!rv) {
4696 rv = entry->show(mddev, page);
4697 mddev_unlock(mddev);
4698 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004699 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004700 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004701}
4702
4703static ssize_t
4704md_attr_store(struct kobject *kobj, struct attribute *attr,
4705 const char *page, size_t length)
4706{
4707 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004708 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004709 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004710
4711 if (!entry->store)
4712 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004713 if (!capable(CAP_SYS_ADMIN))
4714 return -EACCES;
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);
NeilBrowna7a3f082012-12-11 13:35:54 +11004722 if (entry->store == new_dev_store)
4723 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004724 rv = mddev_lock(mddev);
4725 if (!rv) {
4726 rv = entry->store(mddev, page, length);
4727 mddev_unlock(mddev);
4728 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004729 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004730 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004731}
4732
4733static void md_free(struct kobject *ko)
4734{
NeilBrownfd01b882011-10-11 16:47:53 +11004735 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004736
4737 if (mddev->sysfs_state)
4738 sysfs_put(mddev->sysfs_state);
4739
4740 if (mddev->gendisk) {
4741 del_gendisk(mddev->gendisk);
4742 put_disk(mddev->gendisk);
4743 }
4744 if (mddev->queue)
4745 blk_cleanup_queue(mddev->queue);
4746
NeilBrowneae17012005-11-08 21:39:23 -08004747 kfree(mddev);
4748}
4749
Emese Revfy52cf25d2010-01-19 02:58:23 +01004750static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004751 .show = md_attr_show,
4752 .store = md_attr_store,
4753};
4754static struct kobj_type md_ktype = {
4755 .release = md_free,
4756 .sysfs_ops = &md_sysfs_ops,
4757 .default_attrs = md_default_attrs,
4758};
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760int mdp_major = 0;
4761
Dan Williams5fd3a172009-03-04 00:57:25 -07004762static void mddev_delayed_delete(struct work_struct *ws)
4763{
NeilBrownfd01b882011-10-11 16:47:53 +11004764 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004765
NeilBrown43a70502009-12-14 12:49:55 +11004766 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004767 kobject_del(&mddev->kobj);
4768 kobject_put(&mddev->kobj);
4769}
4770
NeilBrownefeb53c2009-01-09 08:31:10 +11004771static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004773 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004774 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004776 int partitioned;
4777 int shift;
4778 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004779 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004782 return -ENODEV;
4783
4784 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4785 shift = partitioned ? MdpMinorShift : 0;
4786 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Tejun Heoe804ac72010-10-15 15:36:08 +02004788 /* wait for any previous instance of this device to be
4789 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004790 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004791 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004792
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004793 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004794 error = -EEXIST;
4795 if (mddev->gendisk)
4796 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004797
4798 if (name) {
4799 /* Need to ensure that 'name' is not a duplicate.
4800 */
NeilBrownfd01b882011-10-11 16:47:53 +11004801 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004802 spin_lock(&all_mddevs_lock);
4803
4804 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4805 if (mddev2->gendisk &&
4806 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4807 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004808 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004809 }
4810 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
NeilBrown8b765392009-01-09 08:31:08 +11004812
NeilBrown0909dc42009-07-01 12:27:21 +10004813 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004814 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004815 if (!mddev->queue)
4816 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004817 mddev->queue->queuedata = mddev;
4818
NeilBrown409c57f2009-03-31 14:39:39 +11004819 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004820 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 disk = alloc_disk(1 << shift);
4823 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004824 blk_cleanup_queue(mddev->queue);
4825 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004826 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004828 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004830 if (name)
4831 strcpy(disk->disk_name, name);
4832 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004834 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 disk->fops = &md_fops;
4837 disk->private_data = mddev;
4838 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004839 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004840 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004841 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004842 * remove it now.
4843 */
4844 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004846 /* As soon as we call add_disk(), another thread could get
4847 * through to md_open, so make sure it doesn't get too far
4848 */
4849 mutex_lock(&mddev->open_mutex);
4850 add_disk(disk);
4851
Tejun Heoed9e1982008-08-25 19:56:05 +09004852 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4853 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004854 if (error) {
4855 /* This isn't possible, but as kobject_init_and_add is marked
4856 * __must_check, we must do something with the result
4857 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004858 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4859 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004860 error = 0;
4861 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004862 if (mddev->kobj.sd &&
4863 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004864 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004865 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004866 abort:
4867 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004868 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004869 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004870 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004871 }
NeilBrownd3374822009-01-09 08:31:10 +11004872 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004873 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004874}
4875
4876static struct kobject *md_probe(dev_t dev, int *part, void *data)
4877{
4878 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return NULL;
4880}
4881
NeilBrownefeb53c2009-01-09 08:31:10 +11004882static int add_named_array(const char *val, struct kernel_param *kp)
4883{
4884 /* val must be "md_*" where * is not all digits.
4885 * We allocate an array with a large free minor number, and
4886 * set the name to val. val must not already be an active name.
4887 */
4888 int len = strlen(val);
4889 char buf[DISK_NAME_LEN];
4890
4891 while (len && val[len-1] == '\n')
4892 len--;
4893 if (len >= DISK_NAME_LEN)
4894 return -E2BIG;
4895 strlcpy(buf, val, len+1);
4896 if (strncmp(buf, "md_", 3) != 0)
4897 return -EINVAL;
4898 return md_alloc(0, buf);
4899}
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901static void md_safemode_timeout(unsigned long data)
4902{
NeilBrownfd01b882011-10-11 16:47:53 +11004903 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Neil Brown0fd62b82008-06-28 08:31:36 +10004905 if (!atomic_read(&mddev->writes_pending)) {
4906 mddev->safemode = 1;
4907 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004908 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 md_wakeup_thread(mddev->thread);
4911}
4912
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004913static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
NeilBrownfd01b882011-10-11 16:47:53 +11004915int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
NeilBrown2604b702006-01-06 00:20:36 -08004917 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004918 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004919 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
NeilBrowna757e642005-04-16 15:26:42 -07004921 if (list_empty(&mddev->disks))
4922 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 if (mddev->pers)
4926 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004927 /* Cannot run until previous stop completes properly */
4928 if (mddev->sysfs_active)
4929 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 /*
4932 * Analyze all RAID superblock(s)
4933 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004934 if (!mddev->raid_disks) {
4935 if (!mddev->persistent)
4936 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004937 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
NeilBrownd9d166c2006-01-06 00:20:51 -08004940 if (mddev->level != LEVEL_NONE)
4941 request_module("md-level-%d", mddev->level);
4942 else if (mddev->clevel[0])
4943 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 /*
4946 * Drop all container device buffers, from now on
4947 * the only valid external interface is through the md
4948 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004950 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004951 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 continue;
4953 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004954 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004955
4956 /* perform some consistency tests on the device.
4957 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004958 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004959 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004960 if (rdev->meta_bdev) {
4961 /* Nothing to check */;
4962 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004963 if (mddev->dev_sectors &&
4964 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004965 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004966 printk("md: %s: data overlaps metadata\n",
4967 mdname(mddev));
4968 return -EINVAL;
4969 }
4970 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004971 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004972 > rdev->data_offset) {
4973 printk("md: %s: metadata overlaps data\n",
4974 mdname(mddev));
4975 return -EINVAL;
4976 }
4977 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004978 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 }
4980
NeilBrowna167f662010-10-26 18:31:13 +11004981 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004982 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004985 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004986 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004988 if (mddev->level != LEVEL_NONE)
4989 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4990 mddev->level);
4991 else
4992 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4993 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 return -EINVAL;
4995 }
NeilBrown2604b702006-01-06 00:20:36 -08004996 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004998 if (mddev->level != pers->level) {
4999 mddev->level = pers->level;
5000 mddev->new_level = pers->level;
5001 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005002 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
NeilBrownf6705572006-03-27 01:18:11 -08005004 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005005 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005006 /* This personality cannot handle reshaping... */
5007 mddev->pers = NULL;
5008 module_put(pers->owner);
5009 return -EINVAL;
5010 }
5011
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005012 if (pers->sync_request) {
5013 /* Warn if this is a potentially silly
5014 * configuration.
5015 */
5016 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005017 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005018 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005019
NeilBrowndafb20f2012-03-19 12:46:39 +11005020 rdev_for_each(rdev, mddev)
5021 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005022 if (rdev < rdev2 &&
5023 rdev->bdev->bd_contains ==
5024 rdev2->bdev->bd_contains) {
5025 printk(KERN_WARNING
5026 "%s: WARNING: %s appears to be"
5027 " on the same physical disk as"
5028 " %s.\n",
5029 mdname(mddev),
5030 bdevname(rdev->bdev,b),
5031 bdevname(rdev2->bdev,b2));
5032 warned = 1;
5033 }
5034 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005035
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005036 if (warned)
5037 printk(KERN_WARNING
5038 "True protection against single-disk"
5039 " failure might be compromised.\n");
5040 }
5041
NeilBrown657390d2005-08-26 18:34:16 -07005042 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005043 /* may be over-ridden by personality */
5044 mddev->resync_max_sectors = mddev->dev_sectors;
5045
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005046 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
NeilBrown0f9552b2009-12-30 12:08:50 +11005048 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005049 mddev->ro = 2; /* read-only, but switch on first write */
5050
NeilBrownb15c2e52006-01-06 00:20:16 -08005051 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005052 if (err)
5053 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005054 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5055 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5056 " but 'external_size' not in effect?\n", __func__);
5057 printk(KERN_ERR
5058 "md: invalid array_size %llu > default size %llu\n",
5059 (unsigned long long)mddev->array_sectors / 2,
5060 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5061 err = -EINVAL;
5062 mddev->pers->stop(mddev);
5063 }
NeilBrownef99bf42012-05-22 13:55:08 +10005064 if (err == 0 && mddev->pers->sync_request &&
5065 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005066 err = bitmap_create(mddev);
5067 if (err) {
5068 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5069 mdname(mddev), err);
5070 mddev->pers->stop(mddev);
5071 }
5072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 module_put(mddev->pers->owner);
5075 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005076 bitmap_destroy(mddev);
5077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005079 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005080 if (mddev->kobj.sd &&
5081 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005082 printk(KERN_WARNING
5083 "md: cannot register extra attributes for %s\n",
5084 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005085 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005086 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005087 mddev->ro = 0;
5088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005090 atomic_set(&mddev->max_corr_read_errors,
5091 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 mddev->safemode = 0;
5093 mddev->safemode_timer.function = md_safemode_timeout;
5094 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005095 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005097 smp_wmb();
5098 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005099 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005100 if (rdev->raid_disk >= 0)
5101 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005102 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5105
NeilBrown7a0a5352013-08-27 16:28:23 +10005106 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005107 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
NeilBrownd7603b72006-01-06 00:20:30 -08005109 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005110 sysfs_notify_dirent_safe(mddev->sysfs_state);
5111 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005112 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return 0;
5114}
NeilBrown390ee602010-06-01 19:37:27 +10005115EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
NeilBrownfd01b882011-10-11 16:47:53 +11005117static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005118{
5119 int err;
5120
5121 err = md_run(mddev);
5122 if (err)
5123 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005124 err = bitmap_load(mddev);
5125 if (err) {
5126 bitmap_destroy(mddev);
5127 goto out;
5128 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005129
5130 md_wakeup_thread(mddev->thread);
5131 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5132
NeilBrownfe60b012010-03-29 11:10:42 +11005133 set_capacity(mddev->gendisk, mddev->array_sectors);
5134 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005135 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005136 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5137out:
5138 return err;
5139}
5140
NeilBrownfd01b882011-10-11 16:47:53 +11005141static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
5143 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Andre Noll80fab1d2008-07-11 22:02:21 +10005145 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005147 return -ENXIO;
5148 if (!mddev->pers)
5149 return -EINVAL;
5150 if (!mddev->ro)
5151 return -EBUSY;
5152 mddev->safemode = 0;
5153 mddev->ro = 0;
5154 set_disk_ro(disk, 0);
5155 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5156 mdname(mddev));
5157 /* Kick recovery or resync if necessary */
5158 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5159 md_wakeup_thread(mddev->thread);
5160 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005161 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163}
5164
NeilBrownacc55e22006-06-26 00:27:47 -07005165/* similar to deny_write_access, but accounts for our holding a reference
5166 * to the file ourselves */
5167static int deny_bitmap_write_access(struct file * file)
5168{
5169 struct inode *inode = file->f_mapping->host;
5170
5171 spin_lock(&inode->i_lock);
5172 if (atomic_read(&inode->i_writecount) > 1) {
5173 spin_unlock(&inode->i_lock);
5174 return -ETXTBSY;
5175 }
5176 atomic_set(&inode->i_writecount, -1);
5177 spin_unlock(&inode->i_lock);
5178
5179 return 0;
5180}
5181
NeilBrown43a70502009-12-14 12:49:55 +11005182void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005183{
5184 struct inode *inode = file->f_mapping->host;
5185
5186 spin_lock(&inode->i_lock);
5187 atomic_set(&inode->i_writecount, 1);
5188 spin_unlock(&inode->i_lock);
5189}
5190
NeilBrownfd01b882011-10-11 16:47:53 +11005191static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005192{
5193 mddev->array_sectors = 0;
5194 mddev->external_size = 0;
5195 mddev->dev_sectors = 0;
5196 mddev->raid_disks = 0;
5197 mddev->recovery_cp = 0;
5198 mddev->resync_min = 0;
5199 mddev->resync_max = MaxSector;
5200 mddev->reshape_position = MaxSector;
5201 mddev->external = 0;
5202 mddev->persistent = 0;
5203 mddev->level = LEVEL_NONE;
5204 mddev->clevel[0] = 0;
5205 mddev->flags = 0;
5206 mddev->ro = 0;
5207 mddev->metadata_type[0] = 0;
5208 mddev->chunk_sectors = 0;
5209 mddev->ctime = mddev->utime = 0;
5210 mddev->layout = 0;
5211 mddev->max_disks = 0;
5212 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005213 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005214 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005215 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005216 mddev->new_level = LEVEL_NONE;
5217 mddev->new_layout = 0;
5218 mddev->new_chunk_sectors = 0;
5219 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005220 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005221 mddev->suspend_lo = mddev->suspend_hi = 0;
5222 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5223 mddev->recovery = 0;
5224 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005225 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005226 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005227 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005228 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005229 mddev->bitmap_info.offset = 0;
5230 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005231 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005232 mddev->bitmap_info.chunksize = 0;
5233 mddev->bitmap_info.daemon_sleep = 0;
5234 mddev->bitmap_info.max_write_behind = 0;
5235}
5236
NeilBrownfd01b882011-10-11 16:47:53 +11005237static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005238{
NeilBrown6b6204e2013-05-09 09:48:30 +10005239 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005240 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005241 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005242 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005243 }
5244
5245 del_timer_sync(&mddev->safemode_timer);
5246
5247 bitmap_flush(mddev);
5248 md_super_wait(mddev);
5249
NeilBrownb6d428c2013-04-24 11:42:42 +10005250 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005251 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005252 /* mark array as shutdown cleanly */
5253 mddev->in_sync = 1;
5254 md_update_sb(mddev, 1);
5255 }
5256}
NeilBrowndefad612011-01-14 09:14:33 +11005257
NeilBrownfd01b882011-10-11 16:47:53 +11005258void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005259{
NeilBrown29f097c2013-11-14 17:54:51 +11005260 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005261 __md_stop_writes(mddev);
5262 mddev_unlock(mddev);
5263}
NeilBrown390ee602010-06-01 19:37:27 +10005264EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005265
NeilBrown5eff3c42012-11-19 10:47:48 +11005266static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005267{
NeilBrown0ca69882011-01-14 09:14:33 +11005268 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005269 mddev->pers->stop(mddev);
5270 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5271 mddev->to_remove = &md_redundancy_group;
5272 module_put(mddev->pers->owner);
5273 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005274 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005275}
NeilBrown5eff3c42012-11-19 10:47:48 +11005276
5277void md_stop(struct mddev *mddev)
5278{
5279 /* stop the array and free an attached data structures.
5280 * This is called from dm-raid
5281 */
5282 __md_stop(mddev);
5283 bitmap_destroy(mddev);
5284 if (mddev->bio_set)
5285 bioset_free(mddev->bio_set);
5286}
5287
NeilBrown390ee602010-06-01 19:37:27 +10005288EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005289
NeilBrowna05b7ea02012-07-19 15:59:18 +10005290static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005291{
5292 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005293 int did_freeze = 0;
5294
5295 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5296 did_freeze = 1;
5297 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5298 md_wakeup_thread(mddev->thread);
5299 }
5300 if (mddev->sync_thread) {
5301 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5302 /* Thread might be blocked waiting for metadata update
5303 * which will now never happen */
5304 wake_up_process(mddev->sync_thread->tsk);
5305 }
5306 mddev_unlock(mddev);
5307 wait_event(resync_wait, mddev->sync_thread == NULL);
5308 mddev_lock_nointr(mddev);
5309
NeilBrowna4bd82d2010-03-29 13:23:10 +11005310 mutex_lock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005311 if (atomic_read(&mddev->openers) > !!bdev ||
5312 mddev->sync_thread ||
5313 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005314 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005315 if (did_freeze) {
5316 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5317 md_wakeup_thread(mddev->thread);
5318 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005319 err = -EBUSY;
5320 goto out;
5321 }
5322 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005323 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005324
5325 err = -ENXIO;
5326 if (mddev->ro==1)
5327 goto out;
5328 mddev->ro = 1;
5329 set_disk_ro(mddev->gendisk, 1);
5330 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005331 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005332 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005333 }
5334out:
5335 mutex_unlock(&mddev->open_mutex);
5336 return err;
5337}
5338
NeilBrown9e653b62006-06-26 00:27:58 -07005339/* mode:
5340 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005341 * 2 - stop but do not disassemble array
5342 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005343static int do_md_stop(struct mddev * mddev, int mode,
5344 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005347 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005348 int did_freeze = 0;
5349
5350 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5351 did_freeze = 1;
5352 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5353 md_wakeup_thread(mddev->thread);
5354 }
5355 if (mddev->sync_thread) {
5356 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5357 /* Thread might be blocked waiting for metadata update
5358 * which will now never happen */
5359 wake_up_process(mddev->sync_thread->tsk);
5360 }
5361 mddev_unlock(mddev);
5362 wait_event(resync_wait, mddev->sync_thread == NULL);
5363 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
NeilBrownc8c00a62009-08-10 12:50:52 +10005365 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005366 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005367 mddev->sysfs_active ||
5368 mddev->sync_thread ||
5369 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005370 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005371 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005372 if (did_freeze) {
5373 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5374 md_wakeup_thread(mddev->thread);
5375 }
NeilBrown260fa032013-08-27 16:44:13 +10005376 return -EBUSY;
5377 }
NeilBrown6e17b022010-08-07 21:41:19 +10005378 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005379 if (mddev->ro)
5380 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005381
NeilBrowndefad612011-01-14 09:14:33 +11005382 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005383 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005384 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005385 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005386
NeilBrowna4bd82d2010-03-29 13:23:10 +11005387 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005388 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005389
NeilBrowndafb20f2012-03-19 12:46:39 +11005390 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005391 if (rdev->raid_disk >= 0)
5392 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005393
NeilBrowna4bd82d2010-03-29 13:23:10 +11005394 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005395 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005396 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005397 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005398
NeilBrowna4bd82d2010-03-29 13:23:10 +11005399 if (mddev->ro)
5400 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005401 } else
5402 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 /*
5404 * Free resources if final stop
5405 */
NeilBrown9e653b62006-06-26 00:27:58 -07005406 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5408
NeilBrown978f9462006-02-02 14:28:05 -08005409 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005410 if (mddev->bitmap_info.file) {
5411 restore_bitmap_write_access(mddev->bitmap_info.file);
5412 fput(mddev->bitmap_info.file);
5413 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005414 }
NeilBrownc3d97142009-12-14 12:49:52 +11005415 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 export_array(mddev);
5418
NeilBrown6177b472010-03-29 11:37:13 +11005419 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005420 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005421 if (mddev->hold_active == UNTIL_STOP)
5422 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005423 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005424 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005425 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005426 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428}
5429
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005430#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005431static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432{
NeilBrown3cb03002011-10-11 16:45:26 +11005433 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 int err;
5435
NeilBrowna757e642005-04-16 15:26:42 -07005436 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
5439 printk(KERN_INFO "md: running: ");
5440
NeilBrowndafb20f2012-03-19 12:46:39 +11005441 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 char b[BDEVNAME_SIZE];
5443 printk("<%s>", bdevname(rdev->bdev,b));
5444 }
5445 printk("\n");
5446
NeilBrownd710e132008-10-13 11:55:12 +11005447 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 if (err) {
5449 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005450 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452}
5453
5454/*
5455 * lets try to run arrays based on all disks that have arrived
5456 * until now. (those are in pending_raid_disks)
5457 *
5458 * the method: pick the first pending disk, collect all disks with
5459 * the same UUID, remove all from the pending list and put them into
5460 * the 'same_array' list. Then order this list based on superblock
5461 * update time (freshest comes first), kick out 'old' disks and
5462 * compare superblocks. If everything's fine then run it.
5463 *
5464 * If "unit" is allocated, then bump its reference count
5465 */
5466static void autorun_devices(int part)
5467{
NeilBrown3cb03002011-10-11 16:45:26 +11005468 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005469 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 char b[BDEVNAME_SIZE];
5471
5472 printk(KERN_INFO "md: autorun ...\n");
5473 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005474 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005476 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005478 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 printk(KERN_INFO "md: considering %s ...\n",
5481 bdevname(rdev0->bdev,b));
5482 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005483 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 if (super_90_load(rdev, rdev0, 0) >= 0) {
5485 printk(KERN_INFO "md: adding %s ...\n",
5486 bdevname(rdev->bdev,b));
5487 list_move(&rdev->same_set, &candidates);
5488 }
5489 /*
5490 * now we have a set of devices, with all of them having
5491 * mostly sane superblocks. It's time to allocate the
5492 * mddev.
5493 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005494 if (part) {
5495 dev = MKDEV(mdp_major,
5496 rdev0->preferred_minor << MdpMinorShift);
5497 unit = MINOR(dev) >> MdpMinorShift;
5498 } else {
5499 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5500 unit = MINOR(dev);
5501 }
5502 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5504 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5505 break;
5506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
5508 md_probe(dev, NULL, NULL);
5509 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005510 if (!mddev || !mddev->gendisk) {
5511 if (mddev)
5512 mddev_put(mddev);
5513 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 "md: cannot allocate memory for md drive.\n");
5515 break;
5516 }
5517 if (mddev_lock(mddev))
5518 printk(KERN_WARNING "md: %s locked, cannot run\n",
5519 mdname(mddev));
5520 else if (mddev->raid_disks || mddev->major_version
5521 || !list_empty(&mddev->disks)) {
5522 printk(KERN_WARNING
5523 "md: %s already running, cannot run %s\n",
5524 mdname(mddev), bdevname(rdev0->bdev,b));
5525 mddev_unlock(mddev);
5526 } else {
5527 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005528 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005529 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 list_del_init(&rdev->same_set);
5531 if (bind_rdev_to_array(rdev, mddev))
5532 export_rdev(rdev);
5533 }
5534 autorun_array(mddev);
5535 mddev_unlock(mddev);
5536 }
5537 /* on success, candidates will be empty, on error
5538 * it won't...
5539 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005540 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005541 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 mddev_put(mddev);
5545 }
5546 printk(KERN_INFO "md: ... autorun DONE.\n");
5547}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005548#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550static int get_version(void __user * arg)
5551{
5552 mdu_version_t ver;
5553
5554 ver.major = MD_MAJOR_VERSION;
5555 ver.minor = MD_MINOR_VERSION;
5556 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5557
5558 if (copy_to_user(arg, &ver, sizeof(ver)))
5559 return -EFAULT;
5560
5561 return 0;
5562}
5563
NeilBrownfd01b882011-10-11 16:47:53 +11005564static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
5566 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005567 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005568 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
NeilBrown1ca69c42012-10-11 13:37:33 +11005570 nr = working = insync = failed = spare = 0;
5571 rcu_read_lock();
5572 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005574 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 failed++;
5576 else {
5577 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005578 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005579 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 else
5581 spare++;
5582 }
5583 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005584 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 info.major_version = mddev->major_version;
5587 info.minor_version = mddev->minor_version;
5588 info.patch_version = MD_PATCHLEVEL_VERSION;
5589 info.ctime = mddev->ctime;
5590 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005591 info.size = mddev->dev_sectors / 2;
5592 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005593 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 info.nr_disks = nr;
5595 info.raid_disks = mddev->raid_disks;
5596 info.md_minor = mddev->md_minor;
5597 info.not_persistent= !mddev->persistent;
5598
5599 info.utime = mddev->utime;
5600 info.state = 0;
5601 if (mddev->in_sync)
5602 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005603 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005604 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005605 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 info.working_disks = working;
5607 info.failed_disks = failed;
5608 info.spare_disks = spare;
5609
5610 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005611 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
5613 if (copy_to_user(arg, &info, sizeof(info)))
5614 return -EFAULT;
5615
5616 return 0;
5617}
5618
NeilBrownfd01b882011-10-11 16:47:53 +11005619static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005620{
5621 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5622 char *ptr, *buf = NULL;
5623 int err = -ENOMEM;
5624
NeilBrown60559da2013-07-16 16:44:29 +10005625 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005626
NeilBrown32a76272005-06-21 17:17:14 -07005627 if (!file)
5628 goto out;
5629
5630 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005631 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005632 file->pathname[0] = '\0';
5633 goto copy_out;
5634 }
5635
5636 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5637 if (!buf)
5638 goto out;
5639
NeilBrown1ec885c2012-05-22 13:55:10 +10005640 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5641 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005642 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005643 goto out;
5644
5645 strcpy(file->pathname, ptr);
5646
5647copy_out:
5648 err = 0;
5649 if (copy_to_user(arg, file, sizeof(*file)))
5650 err = -EFAULT;
5651out:
5652 kfree(buf);
5653 kfree(file);
5654 return err;
5655}
5656
NeilBrownfd01b882011-10-11 16:47:53 +11005657static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
5659 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005660 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662 if (copy_from_user(&info, arg, sizeof(info)))
5663 return -EFAULT;
5664
NeilBrown1ca69c42012-10-11 13:37:33 +11005665 rcu_read_lock();
5666 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (rdev) {
5668 info.major = MAJOR(rdev->bdev->bd_dev);
5669 info.minor = MINOR(rdev->bdev->bd_dev);
5670 info.raid_disk = rdev->raid_disk;
5671 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005674 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 info.state |= (1<<MD_DISK_ACTIVE);
5676 info.state |= (1<<MD_DISK_SYNC);
5677 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005678 if (test_bit(WriteMostly, &rdev->flags))
5679 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 } else {
5681 info.major = info.minor = 0;
5682 info.raid_disk = -1;
5683 info.state = (1<<MD_DISK_REMOVED);
5684 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005685 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 if (copy_to_user(arg, &info, sizeof(info)))
5688 return -EFAULT;
5689
5690 return 0;
5691}
5692
NeilBrownfd01b882011-10-11 16:47:53 +11005693static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694{
5695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005696 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 dev_t dev = MKDEV(info->major,info->minor);
5698
5699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5700 return -EOVERFLOW;
5701
5702 if (!mddev->raid_disks) {
5703 int err;
5704 /* expecting a device which has a superblock */
5705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5706 if (IS_ERR(rdev)) {
5707 printk(KERN_WARNING
5708 "md: md_import_device returned %ld\n",
5709 PTR_ERR(rdev));
5710 return PTR_ERR(rdev);
5711 }
5712 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005713 struct md_rdev *rdev0
5714 = list_entry(mddev->disks.next,
5715 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005716 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 .load_super(rdev, rdev0, mddev->minor_version);
5718 if (err < 0) {
5719 printk(KERN_WARNING
5720 "md: %s has different UUID to %s\n",
5721 bdevname(rdev->bdev,b),
5722 bdevname(rdev0->bdev,b2));
5723 export_rdev(rdev);
5724 return -EINVAL;
5725 }
5726 }
5727 err = bind_rdev_to_array(rdev, mddev);
5728 if (err)
5729 export_rdev(rdev);
5730 return err;
5731 }
5732
5733 /*
5734 * add_new_disk can be used once the array is assembled
5735 * to add "hot spares". They must already have a superblock
5736 * written
5737 */
5738 if (mddev->pers) {
5739 int err;
5740 if (!mddev->pers->hot_add_disk) {
5741 printk(KERN_WARNING
5742 "%s: personality does not support diskops!\n",
5743 mdname(mddev));
5744 return -EINVAL;
5745 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005746 if (mddev->persistent)
5747 rdev = md_import_device(dev, mddev->major_version,
5748 mddev->minor_version);
5749 else
5750 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (IS_ERR(rdev)) {
5752 printk(KERN_WARNING
5753 "md: md_import_device returned %ld\n",
5754 PTR_ERR(rdev));
5755 return PTR_ERR(rdev);
5756 }
NeilBrown1a855a02010-12-09 16:36:28 +11005757 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005758 if (!mddev->persistent) {
5759 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005760 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005761 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005762 set_bit(In_sync, &rdev->flags);
5763 } else
NeilBrown41158c72005-06-21 17:17:25 -07005764 rdev->raid_disk = -1;
5765 } else
5766 super_types[mddev->major_version].
5767 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005768 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005769 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005770 /* This was a hot-add request, but events doesn't
5771 * match, so reject it.
5772 */
5773 export_rdev(rdev);
5774 return -EINVAL;
5775 }
5776
NeilBrown1a855a02010-12-09 16:36:28 +11005777 if (test_bit(In_sync, &rdev->flags))
5778 rdev->saved_raid_disk = rdev->raid_disk;
5779 else
5780 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005781
NeilBrownb2d444d2005-11-08 21:39:31 -08005782 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005783 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5784 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005785 else
5786 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 rdev->raid_disk = -1;
5789 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005790 if (!err && !mddev->pers->hot_remove_disk) {
5791 /* If there is hot_add_disk but no hot_remove_disk
5792 * then added disks for geometry changes,
5793 * and should be added immediately.
5794 */
5795 super_types[mddev->major_version].
5796 validate_super(mddev, rdev);
5797 err = mddev->pers->hot_add_disk(mddev, rdev);
5798 if (err)
5799 unbind_rdev_from_array(rdev);
5800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 if (err)
5802 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005803 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005804 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005805
NeilBrown7ceb17e2013-04-24 11:42:42 +10005806 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005807 if (mddev->degraded)
5808 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005809 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005810 if (!err)
5811 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005812 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 return err;
5814 }
5815
5816 /* otherwise, add_new_disk is only allowed
5817 * for major_version==0 superblocks
5818 */
5819 if (mddev->major_version != 0) {
5820 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5821 mdname(mddev));
5822 return -EINVAL;
5823 }
5824
5825 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5826 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005827 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (IS_ERR(rdev)) {
5829 printk(KERN_WARNING
5830 "md: error, md_import_device() returned %ld\n",
5831 PTR_ERR(rdev));
5832 return PTR_ERR(rdev);
5833 }
5834 rdev->desc_nr = info->number;
5835 if (info->raid_disk < mddev->raid_disks)
5836 rdev->raid_disk = info->raid_disk;
5837 else
5838 rdev->raid_disk = -1;
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005841 if (info->state & (1<<MD_DISK_SYNC))
5842 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005844 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5845 set_bit(WriteMostly, &rdev->flags);
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 if (!mddev->persistent) {
5848 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005849 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5850 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005851 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005852 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
NeilBrown2bf071b2006-01-06 00:20:55 -08005854 err = bind_rdev_to_array(rdev, mddev);
5855 if (err) {
5856 export_rdev(rdev);
5857 return err;
5858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
5860
5861 return 0;
5862}
5863
NeilBrownfd01b882011-10-11 16:47:53 +11005864static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865{
5866 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005867 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 rdev = find_rdev(mddev, dev);
5870 if (!rdev)
5871 return -ENXIO;
5872
NeilBrown3ea8929d2013-04-24 11:42:41 +10005873 clear_bit(Blocked, &rdev->flags);
5874 remove_and_add_spares(mddev, rdev);
5875
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 if (rdev->raid_disk >= 0)
5877 goto busy;
5878
5879 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005880 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005881 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 return 0;
5884busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005885 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 bdevname(rdev->bdev,b), mdname(mddev));
5887 return -EBUSY;
5888}
5889
NeilBrownfd01b882011-10-11 16:47:53 +11005890static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
5892 char b[BDEVNAME_SIZE];
5893 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005894 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896 if (!mddev->pers)
5897 return -ENODEV;
5898
5899 if (mddev->major_version != 0) {
5900 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5901 " version-0 superblocks.\n",
5902 mdname(mddev));
5903 return -EINVAL;
5904 }
5905 if (!mddev->pers->hot_add_disk) {
5906 printk(KERN_WARNING
5907 "%s: personality does not support diskops!\n",
5908 mdname(mddev));
5909 return -EINVAL;
5910 }
5911
NeilBrownd710e132008-10-13 11:55:12 +11005912 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 if (IS_ERR(rdev)) {
5914 printk(KERN_WARNING
5915 "md: error, md_import_device() returned %ld\n",
5916 PTR_ERR(rdev));
5917 return -EINVAL;
5918 }
5919
5920 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005921 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005923 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
NeilBrown8190e752009-06-18 08:48:58 +10005925 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
NeilBrownb2d444d2005-11-08 21:39:31 -08005927 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 printk(KERN_WARNING
5929 "md: can not hot-add faulty %s disk to %s!\n",
5930 bdevname(rdev->bdev,b), mdname(mddev));
5931 err = -EINVAL;
5932 goto abort_export;
5933 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005934 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005936 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005937 err = bind_rdev_to_array(rdev, mddev);
5938 if (err)
5939 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 /*
5942 * The rest should better be atomic, we can have disk failures
5943 * noticed in interrupt contexts ...
5944 */
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 rdev->raid_disk = -1;
5947
NeilBrown850b2b42006-10-03 01:15:46 -07005948 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /*
5951 * Kick recovery, maybe this spare has to be added to the
5952 * array immediately.
5953 */
5954 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5955 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005956 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return 0;
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959abort_export:
5960 export_rdev(rdev);
5961 return err;
5962}
5963
NeilBrownfd01b882011-10-11 16:47:53 +11005964static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005965{
5966 int err;
5967
NeilBrown36fa3062005-09-09 16:23:45 -07005968 if (mddev->pers) {
5969 if (!mddev->pers->quiesce)
5970 return -EBUSY;
5971 if (mddev->recovery || mddev->sync_thread)
5972 return -EBUSY;
5973 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005974 }
5975
NeilBrown36fa3062005-09-09 16:23:45 -07005976
5977 if (fd >= 0) {
5978 if (mddev->bitmap)
5979 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005980 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005981
NeilBrownc3d97142009-12-14 12:49:52 +11005982 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005983 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5984 mdname(mddev));
5985 return -EBADF;
5986 }
5987
NeilBrownc3d97142009-12-14 12:49:52 +11005988 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005989 if (err) {
5990 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5991 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005992 fput(mddev->bitmap_info.file);
5993 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005994 return err;
5995 }
NeilBrownc3d97142009-12-14 12:49:52 +11005996 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005997 } else if (mddev->bitmap == NULL)
5998 return -ENOENT; /* cannot remove what isn't there */
5999 err = 0;
6000 if (mddev->pers) {
6001 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006002 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006003 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006004 if (!err)
6005 err = bitmap_load(mddev);
6006 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006007 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006008 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006009 fd = -1; /* make sure to put the file */
6010 }
NeilBrown36fa3062005-09-09 16:23:45 -07006011 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006012 }
6013 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006014 if (mddev->bitmap_info.file) {
6015 restore_bitmap_write_access(mddev->bitmap_info.file);
6016 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006017 }
NeilBrownc3d97142009-12-14 12:49:52 +11006018 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006019 }
6020
NeilBrown32a76272005-06-21 17:17:14 -07006021 return err;
6022}
6023
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024/*
6025 * set_array_info is used two different ways
6026 * The original usage is when creating a new array.
6027 * In this usage, raid_disks is > 0 and it together with
6028 * level, size, not_persistent,layout,chunksize determine the
6029 * shape of the array.
6030 * This will always create an array with a type-0.90.0 superblock.
6031 * The newer usage is when assembling an array.
6032 * In this case raid_disks will be 0, and the major_version field is
6033 * use to determine which style super-blocks are to be found on the devices.
6034 * The minor and patch _version numbers are also kept incase the
6035 * super_block handler wishes to interpret them.
6036 */
NeilBrownfd01b882011-10-11 16:47:53 +11006037static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038{
6039
6040 if (info->raid_disks == 0) {
6041 /* just setting version number for superblock loading */
6042 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006043 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 super_types[info->major_version].name == NULL) {
6045 /* maybe try to auto-load a module? */
6046 printk(KERN_INFO
6047 "md: superblock version %d not known\n",
6048 info->major_version);
6049 return -EINVAL;
6050 }
6051 mddev->major_version = info->major_version;
6052 mddev->minor_version = info->minor_version;
6053 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006054 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006055 /* ensure mddev_put doesn't delete this now that there
6056 * is some minimal configuration.
6057 */
6058 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return 0;
6060 }
6061 mddev->major_version = MD_MAJOR_VERSION;
6062 mddev->minor_version = MD_MINOR_VERSION;
6063 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6064 mddev->ctime = get_seconds();
6065
6066 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006067 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006068 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 mddev->raid_disks = info->raid_disks;
6070 /* don't set md_minor, it is determined by which /dev/md* was
6071 * openned
6072 */
6073 if (info->state & (1<<MD_SB_CLEAN))
6074 mddev->recovery_cp = MaxSector;
6075 else
6076 mddev->recovery_cp = 0;
6077 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006078 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006081 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
6083 mddev->max_disks = MD_SB_DISKS;
6084
NeilBrowne6910632008-02-06 01:39:51 -08006085 if (mddev->persistent)
6086 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006087 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
NeilBrownc3d97142009-12-14 12:49:52 +11006089 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006090 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006091 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006092
NeilBrownf6705572006-03-27 01:18:11 -08006093 mddev->reshape_position = MaxSector;
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 /*
6096 * Generate a 128 bit UUID
6097 */
6098 get_random_bytes(mddev->uuid, 16);
6099
NeilBrownf6705572006-03-27 01:18:11 -08006100 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006101 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006102 mddev->new_layout = mddev->layout;
6103 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006104 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006105
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 return 0;
6107}
6108
NeilBrownfd01b882011-10-11 16:47:53 +11006109void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006110{
Dan Williamsb522adc2009-03-31 15:00:31 +11006111 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6112
6113 if (mddev->external_size)
6114 return;
6115
Dan Williams1f403622009-03-31 14:59:03 +11006116 mddev->array_sectors = array_sectors;
6117}
6118EXPORT_SYMBOL(md_set_array_sectors);
6119
NeilBrownfd01b882011-10-11 16:47:53 +11006120static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006121{
NeilBrown3cb03002011-10-11 16:45:26 +11006122 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006123 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006124 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006125
6126 if (mddev->pers->resize == NULL)
6127 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006128 /* The "num_sectors" is the number of sectors of each device that
6129 * is used. This can only make sense for arrays with redundancy.
6130 * linear and raid0 always use whatever space is available. We can only
6131 * consider changing this number if no resync or reconstruction is
6132 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006133 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006134 * of each device. If num_sectors is zero, we find the largest size
6135 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006136 */
6137 if (mddev->sync_thread)
6138 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006139
NeilBrowndafb20f2012-03-19 12:46:39 +11006140 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006141 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006142
Andre Nolld71f9f82008-07-11 22:02:22 +10006143 if (fit && (num_sectors == 0 || num_sectors > avail))
6144 num_sectors = avail;
6145 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006146 return -ENOSPC;
6147 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006148 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006149 if (!rv)
6150 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006151 return rv;
6152}
6153
NeilBrownfd01b882011-10-11 16:47:53 +11006154static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006155{
6156 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006157 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006158 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006159 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006160 return -EINVAL;
6161 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006162 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006163 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006164 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006165 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006166
6167 rdev_for_each(rdev, mddev) {
6168 if (mddev->raid_disks < raid_disks &&
6169 rdev->data_offset < rdev->new_data_offset)
6170 return -EINVAL;
6171 if (mddev->raid_disks > raid_disks &&
6172 rdev->data_offset > rdev->new_data_offset)
6173 return -EINVAL;
6174 }
6175
NeilBrown63c70c42006-03-27 01:18:13 -08006176 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006177 if (mddev->delta_disks < 0)
6178 mddev->reshape_backwards = 1;
6179 else if (mddev->delta_disks > 0)
6180 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006181
6182 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006183 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006184 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006185 mddev->reshape_backwards = 0;
6186 }
NeilBrownda943b992006-01-06 00:20:54 -08006187 return rv;
6188}
6189
6190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191/*
6192 * update_array_info is used to change the configuration of an
6193 * on-line array.
6194 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6195 * fields in the info are checked against the array.
6196 * Any differences that cannot be handled will cause an error.
6197 * Normally, only one change can be managed at a time.
6198 */
NeilBrownfd01b882011-10-11 16:47:53 +11006199static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
6201 int rv = 0;
6202 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006203 int state = 0;
6204
6205 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006206 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006207 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 if (mddev->major_version != info->major_version ||
6210 mddev->minor_version != info->minor_version ||
6211/* mddev->patch_version != info->patch_version || */
6212 mddev->ctime != info->ctime ||
6213 mddev->level != info->level ||
6214/* mddev->layout != info->layout || */
6215 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006216 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006217 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6218 ((state^info->state) & 0xfffffe00)
6219 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 return -EINVAL;
6221 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006222 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6223 cnt++;
6224 if (mddev->raid_disks != info->raid_disks)
6225 cnt++;
6226 if (mddev->layout != info->layout)
6227 cnt++;
6228 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6229 cnt++;
6230 if (cnt == 0)
6231 return 0;
6232 if (cnt > 1)
6233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
6235 if (mddev->layout != info->layout) {
6236 /* Change layout
6237 * we don't need to do anything at the md level, the
6238 * personality will take care of it all.
6239 */
NeilBrown50ac1682009-06-18 08:47:55 +10006240 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006242 else {
6243 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006244 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006245 if (rv)
6246 mddev->new_layout = mddev->layout;
6247 return rv;
6248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006250 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006251 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
NeilBrownda943b992006-01-06 00:20:54 -08006253 if (mddev->raid_disks != info->raid_disks)
6254 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
NeilBrown36fa3062005-09-09 16:23:45 -07006256 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6257 if (mddev->pers->quiesce == NULL)
6258 return -EINVAL;
6259 if (mddev->recovery || mddev->sync_thread)
6260 return -EBUSY;
6261 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6262 /* add the bitmap */
6263 if (mddev->bitmap)
6264 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006265 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006266 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006267 mddev->bitmap_info.offset =
6268 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006269 mddev->bitmap_info.space =
6270 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006271 mddev->pers->quiesce(mddev, 1);
6272 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006273 if (!rv)
6274 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006275 if (rv)
6276 bitmap_destroy(mddev);
6277 mddev->pers->quiesce(mddev, 0);
6278 } else {
6279 /* remove the bitmap */
6280 if (!mddev->bitmap)
6281 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006282 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006283 return -EINVAL;
6284 mddev->pers->quiesce(mddev, 1);
6285 bitmap_destroy(mddev);
6286 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006287 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006288 }
6289 }
NeilBrown850b2b42006-10-03 01:15:46 -07006290 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 return rv;
6292}
6293
NeilBrownfd01b882011-10-11 16:47:53 +11006294static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295{
NeilBrown3cb03002011-10-11 16:45:26 +11006296 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006297 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
6299 if (mddev->pers == NULL)
6300 return -ENODEV;
6301
NeilBrown1ca69c42012-10-11 13:37:33 +11006302 rcu_read_lock();
6303 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006305 err = -ENODEV;
6306 else {
6307 md_error(mddev, rdev);
6308 if (!test_bit(Faulty, &rdev->flags))
6309 err = -EBUSY;
6310 }
6311 rcu_read_unlock();
6312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
Andre Noll2f9618c2008-04-25 18:57:58 +02006315/*
6316 * We have a problem here : there is no easy way to give a CHS
6317 * virtual geometry. We currently pretend that we have a 2 heads
6318 * 4 sectors (with a BIG number of cylinders...). This drives
6319 * dosfs just mad... ;-)
6320 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006321static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6322{
NeilBrownfd01b882011-10-11 16:47:53 +11006323 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006324
6325 geo->heads = 2;
6326 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006327 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006328 return 0;
6329}
6330
Al Viroa39907f2008-03-02 10:31:15 -05006331static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 unsigned int cmd, unsigned long arg)
6333{
6334 int err = 0;
6335 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006336 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006337 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
NeilBrown506c9e42011-12-23 10:17:26 +11006339 switch (cmd) {
6340 case RAID_VERSION:
6341 case GET_ARRAY_INFO:
6342 case GET_DISK_INFO:
6343 break;
6344 default:
6345 if (!capable(CAP_SYS_ADMIN))
6346 return -EACCES;
6347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
6349 /*
6350 * Commands dealing with the RAID driver but not any
6351 * particular array:
6352 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006353 switch (cmd) {
6354 case RAID_VERSION:
6355 err = get_version(argp);
6356 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
NeilBrownc02c0ae2012-12-11 13:39:21 +11006358 case PRINT_RAID_DEBUG:
6359 err = 0;
6360 md_print_devices();
6361 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006364 case RAID_AUTORUN:
6365 err = 0;
6366 autostart_arrays(arg);
6367 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006369 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 }
6371
6372 /*
6373 * Commands creating/starting a new array:
6374 */
6375
Al Viroa39907f2008-03-02 10:31:15 -05006376 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 if (!mddev) {
6379 BUG();
6380 goto abort;
6381 }
6382
NeilBrown1ca69c42012-10-11 13:37:33 +11006383 /* Some actions do not requires the mutex */
6384 switch (cmd) {
6385 case GET_ARRAY_INFO:
6386 if (!mddev->raid_disks && !mddev->external)
6387 err = -ENODEV;
6388 else
6389 err = get_array_info(mddev, argp);
6390 goto abort;
6391
6392 case GET_DISK_INFO:
6393 if (!mddev->raid_disks && !mddev->external)
6394 err = -ENODEV;
6395 else
6396 err = get_disk_info(mddev, argp);
6397 goto abort;
6398
6399 case SET_DISK_FAULTY:
6400 err = set_disk_faulty(mddev, new_decode_dev(arg));
6401 goto abort;
6402 }
6403
NeilBrowna7a3f082012-12-11 13:35:54 +11006404 if (cmd == ADD_NEW_DISK)
6405 /* need to ensure md_delayed_delete() has completed */
6406 flush_workqueue(md_misc_wq);
6407
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006408 if (cmd == HOT_REMOVE_DISK)
6409 /* need to ensure recovery thread has run */
6410 wait_event_interruptible_timeout(mddev->sb_wait,
6411 !test_bit(MD_RECOVERY_NEEDED,
6412 &mddev->flags),
6413 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006414 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6415 /* Need to flush page cache, and ensure no-one else opens
6416 * and writes
6417 */
6418 mutex_lock(&mddev->open_mutex);
6419 if (atomic_read(&mddev->openers) > 1) {
6420 mutex_unlock(&mddev->open_mutex);
6421 err = -EBUSY;
6422 goto abort;
6423 }
6424 set_bit(MD_STILL_CLOSED, &mddev->flags);
6425 mutex_unlock(&mddev->open_mutex);
6426 sync_blockdev(bdev);
6427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 err = mddev_lock(mddev);
6429 if (err) {
6430 printk(KERN_INFO
6431 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6432 err, cmd);
6433 goto abort;
6434 }
6435
NeilBrownc02c0ae2012-12-11 13:39:21 +11006436 if (cmd == SET_ARRAY_INFO) {
6437 mdu_array_info_t info;
6438 if (!arg)
6439 memset(&info, 0, sizeof(info));
6440 else if (copy_from_user(&info, argp, sizeof(info))) {
6441 err = -EFAULT;
6442 goto abort_unlock;
6443 }
6444 if (mddev->pers) {
6445 err = update_array_info(mddev, &info);
6446 if (err) {
6447 printk(KERN_WARNING "md: couldn't update"
6448 " array info. %d\n", err);
6449 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
6451 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006452 }
6453 if (!list_empty(&mddev->disks)) {
6454 printk(KERN_WARNING
6455 "md: array %s already has disks!\n",
6456 mdname(mddev));
6457 err = -EBUSY;
6458 goto abort_unlock;
6459 }
6460 if (mddev->raid_disks) {
6461 printk(KERN_WARNING
6462 "md: array %s already initialised!\n",
6463 mdname(mddev));
6464 err = -EBUSY;
6465 goto abort_unlock;
6466 }
6467 err = set_array_info(mddev, &info);
6468 if (err) {
6469 printk(KERN_WARNING "md: couldn't set"
6470 " array info. %d\n", err);
6471 goto abort_unlock;
6472 }
6473 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
6475
6476 /*
6477 * Commands querying/configuring an existing array:
6478 */
NeilBrown32a76272005-06-21 17:17:14 -07006479 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006480 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006481 if ((!mddev->raid_disks && !mddev->external)
6482 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6483 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6484 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 err = -ENODEV;
6486 goto abort_unlock;
6487 }
6488
6489 /*
6490 * Commands even a read-only array can execute:
6491 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006492 switch (cmd) {
6493 case GET_BITMAP_FILE:
6494 err = get_bitmap_file(mddev, argp);
6495 goto done_unlock;
6496
6497 case RESTART_ARRAY_RW:
6498 err = restart_array(mddev);
6499 goto done_unlock;
6500
6501 case STOP_ARRAY:
6502 err = do_md_stop(mddev, 0, bdev);
6503 goto done_unlock;
6504
6505 case STOP_ARRAY_RO:
6506 err = md_set_readonly(mddev, bdev);
6507 goto done_unlock;
6508
NeilBrown3ea8929d2013-04-24 11:42:41 +10006509 case HOT_REMOVE_DISK:
6510 err = hot_remove_disk(mddev, new_decode_dev(arg));
6511 goto done_unlock;
6512
NeilBrown7ceb17e2013-04-24 11:42:42 +10006513 case ADD_NEW_DISK:
6514 /* We can support ADD_NEW_DISK on read-only arrays
6515 * on if we are re-adding a preexisting device.
6516 * So require mddev->pers and MD_DISK_SYNC.
6517 */
6518 if (mddev->pers) {
6519 mdu_disk_info_t info;
6520 if (copy_from_user(&info, argp, sizeof(info)))
6521 err = -EFAULT;
6522 else if (!(info.state & (1<<MD_DISK_SYNC)))
6523 /* Need to clear read-only for this */
6524 break;
6525 else
6526 err = add_new_disk(mddev, &info);
6527 goto done_unlock;
6528 }
6529 break;
6530
NeilBrownc02c0ae2012-12-11 13:39:21 +11006531 case BLKROSET:
6532 if (get_user(ro, (int __user *)(arg))) {
6533 err = -EFAULT;
6534 goto done_unlock;
6535 }
6536 err = -EINVAL;
6537
6538 /* if the bdev is going readonly the value of mddev->ro
6539 * does not matter, no writes are coming
6540 */
6541 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006542 goto done_unlock;
6543
NeilBrownc02c0ae2012-12-11 13:39:21 +11006544 /* are we are already prepared for writes? */
6545 if (mddev->ro != 1)
6546 goto done_unlock;
6547
6548 /* transitioning to readauto need only happen for
6549 * arrays that call md_write_start
6550 */
6551 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006553 if (err == 0) {
6554 mddev->ro = 2;
6555 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006556 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006557 }
6558 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 }
6560
6561 /*
6562 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006563 * superblock, so we do not allow them on read-only arrays.
6564 * However non-MD ioctls (e.g. get-size) will still come through
6565 * here and hit the 'default' below, so only disallow
6566 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006568 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006569 if (mddev->ro == 2) {
6570 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006571 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006572 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006573 /* mddev_unlock will wake thread */
6574 /* If a device failed while we were read-only, we
6575 * need to make sure the metadata is updated now.
6576 */
6577 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6578 mddev_unlock(mddev);
6579 wait_event(mddev->sb_wait,
6580 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6581 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006582 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006583 }
NeilBrownf91de922005-11-08 21:39:36 -08006584 } else {
6585 err = -EROFS;
6586 goto abort_unlock;
6587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 }
6589
NeilBrownc02c0ae2012-12-11 13:39:21 +11006590 switch (cmd) {
6591 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006593 mdu_disk_info_t info;
6594 if (copy_from_user(&info, argp, sizeof(info)))
6595 err = -EFAULT;
6596 else
6597 err = add_new_disk(mddev, &info);
6598 goto done_unlock;
6599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
NeilBrownc02c0ae2012-12-11 13:39:21 +11006601 case HOT_ADD_DISK:
6602 err = hot_add_disk(mddev, new_decode_dev(arg));
6603 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
NeilBrownc02c0ae2012-12-11 13:39:21 +11006605 case RUN_ARRAY:
6606 err = do_md_run(mddev);
6607 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
NeilBrownc02c0ae2012-12-11 13:39:21 +11006609 case SET_BITMAP_FILE:
6610 err = set_bitmap_file(mddev, (int)arg);
6611 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006612
NeilBrownc02c0ae2012-12-11 13:39:21 +11006613 default:
6614 err = -EINVAL;
6615 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 }
6617
6618done_unlock:
6619abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006620 if (mddev->hold_active == UNTIL_IOCTL &&
6621 err != -EINVAL)
6622 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 mddev_unlock(mddev);
6624
6625 return err;
6626done:
6627 if (err)
6628 MD_BUG();
6629abort:
6630 return err;
6631}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006632#ifdef CONFIG_COMPAT
6633static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6634 unsigned int cmd, unsigned long arg)
6635{
6636 switch (cmd) {
6637 case HOT_REMOVE_DISK:
6638 case HOT_ADD_DISK:
6639 case SET_DISK_FAULTY:
6640 case SET_BITMAP_FILE:
6641 /* These take in integer arg, do not convert */
6642 break;
6643 default:
6644 arg = (unsigned long)compat_ptr(arg);
6645 break;
6646 }
6647
6648 return md_ioctl(bdev, mode, cmd, arg);
6649}
6650#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
Al Viroa39907f2008-03-02 10:31:15 -05006652static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653{
6654 /*
6655 * Succeed if we can lock the mddev, which confirms that
6656 * it isn't being stopped right now.
6657 */
NeilBrownfd01b882011-10-11 16:47:53 +11006658 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 int err;
6660
Yuanhan Liu0c098222012-05-22 13:55:32 +10006661 if (!mddev)
6662 return -ENODEV;
6663
NeilBrownd3374822009-01-09 08:31:10 +11006664 if (mddev->gendisk != bdev->bd_disk) {
6665 /* we are racing with mddev_put which is discarding this
6666 * bd_disk.
6667 */
6668 mddev_put(mddev);
6669 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006670 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006671 /* Then retry the open from the top */
6672 return -ERESTARTSYS;
6673 }
6674 BUG_ON(mddev != bdev->bd_disk->private_data);
6675
NeilBrownc8c00a62009-08-10 12:50:52 +10006676 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 goto out;
6678
6679 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006680 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006681 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006682 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006684 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 out:
6686 return err;
6687}
6688
Al Virodb2a1442013-05-05 21:52:57 -04006689static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690{
NeilBrownfd01b882011-10-11 16:47:53 +11006691 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006693 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006694 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006697
6698static int md_media_changed(struct gendisk *disk)
6699{
NeilBrownfd01b882011-10-11 16:47:53 +11006700 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006701
6702 return mddev->changed;
6703}
6704
6705static int md_revalidate(struct gendisk *disk)
6706{
NeilBrownfd01b882011-10-11 16:47:53 +11006707 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006708
6709 mddev->changed = 0;
6710 return 0;
6711}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006712static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713{
6714 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006715 .open = md_open,
6716 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006717 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006718#ifdef CONFIG_COMPAT
6719 .compat_ioctl = md_compat_ioctl,
6720#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006721 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006722 .media_changed = md_media_changed,
6723 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724};
6725
Adrian Bunk75c96f82005-05-05 16:16:09 -07006726static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727{
NeilBrown2b8bf342011-10-11 16:48:23 +11006728 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 /*
6731 * md_thread is a 'system-thread', it's priority should be very
6732 * high. We avoid resource deadlocks individually in each
6733 * raid personality. (RAID5 does preallocation) We also use RR and
6734 * the very same RT priority as kswapd, thus we will never get
6735 * into a priority inversion deadlock.
6736 *
6737 * we definitely have to have equal or higher priority than
6738 * bdflush, otherwise bdflush will deadlock if there are too
6739 * many dirty RAID5 blocks.
6740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
NeilBrown6985c432005-10-19 21:23:47 -07006742 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006743 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
NeilBrown93588e22005-11-15 00:09:12 -08006745 /* We need to wait INTERRUPTIBLE so that
6746 * we don't add to the load-average.
6747 * That means we need to be sure no signals are
6748 * pending
6749 */
6750 if (signal_pending(current))
6751 flush_signals(current);
6752
6753 wait_event_interruptible_timeout
6754 (thread->wqueue,
6755 test_bit(THREAD_WAKEUP, &thread->flags)
6756 || kthread_should_stop(),
6757 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
NeilBrown6c987912011-01-14 09:13:53 +11006759 clear_bit(THREAD_WAKEUP, &thread->flags);
6760 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006761 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006763
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 return 0;
6765}
6766
NeilBrown2b8bf342011-10-11 16:48:23 +11006767void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768{
6769 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006770 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 set_bit(THREAD_WAKEUP, &thread->flags);
6772 wake_up(&thread->wqueue);
6773 }
6774}
6775
Shaohua Li4ed87312012-10-11 13:34:00 +11006776struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6777 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778{
NeilBrown2b8bf342011-10-11 16:48:23 +11006779 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
NeilBrown2b8bf342011-10-11 16:48:23 +11006781 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 if (!thread)
6783 return NULL;
6784
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 init_waitqueue_head(&thread->wqueue);
6786
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 thread->run = run;
6788 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006789 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006790 thread->tsk = kthread_run(md_thread, thread,
6791 "%s_%s",
6792 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006793 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006794 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 kfree(thread);
6796 return NULL;
6797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 return thread;
6799}
6800
NeilBrown2b8bf342011-10-11 16:48:23 +11006801void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802{
NeilBrown2b8bf342011-10-11 16:48:23 +11006803 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006804 if (!thread)
6805 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006806 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006807 /* Locking ensures that mddev_unlock does not wake_up a
6808 * non-existent thread
6809 */
6810 spin_lock(&pers_lock);
6811 *threadp = NULL;
6812 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006813
6814 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 kfree(thread);
6816}
6817
NeilBrownfd01b882011-10-11 16:47:53 +11006818void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819{
6820 if (!mddev) {
6821 MD_BUG();
6822 return;
6823 }
6824
NeilBrownb2d444d2005-11-08 21:39:31 -08006825 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006827
NeilBrownde393cd2011-07-28 11:31:48 +10006828 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 return;
6830 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006831 if (mddev->degraded)
6832 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006833 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6835 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6836 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006837 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006838 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006839 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840}
6841
6842/* seq_file implementation /proc/mdstat */
6843
6844static void status_unused(struct seq_file *seq)
6845{
6846 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006847 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
6849 seq_printf(seq, "unused devices: ");
6850
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006851 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 char b[BDEVNAME_SIZE];
6853 i++;
6854 seq_printf(seq, "%s ",
6855 bdevname(rdev->bdev,b));
6856 }
6857 if (!i)
6858 seq_printf(seq, "<none>");
6859
6860 seq_printf(seq, "\n");
6861}
6862
6863
NeilBrownfd01b882011-10-11 16:47:53 +11006864static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865{
NeilBrowndd71cf62009-05-07 12:49:35 +10006866 sector_t max_sectors, resync, res;
6867 unsigned long dt, db;
6868 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006869 int scale;
6870 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
NeilBrown72f36d52012-10-11 14:25:57 +11006872 if (mddev->curr_resync <= 3)
6873 resync = 0;
6874 else
6875 resync = mddev->curr_resync
6876 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
NeilBrownc804cde2012-05-21 09:28:33 +10006878 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6879 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006880 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006882 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883
6884 /*
6885 * Should not happen.
6886 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006887 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 MD_BUG();
6889 return;
6890 }
NeilBrown4588b422006-03-27 01:18:04 -08006891 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006892 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006893 * u32, as those are the requirements for sector_div.
6894 * Thus 'scale' must be at least 10
6895 */
6896 scale = 10;
6897 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006898 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006899 scale++;
6900 }
6901 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006902 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006903
6904 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 {
NeilBrown4588b422006-03-27 01:18:04 -08006906 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 seq_printf(seq, "[");
6908 for (i = 0; i < x; i++)
6909 seq_printf(seq, "=");
6910 seq_printf(seq, ">");
6911 for (i = 0; i < y; i++)
6912 seq_printf(seq, ".");
6913 seq_printf(seq, "] ");
6914 }
NeilBrown4588b422006-03-27 01:18:04 -08006915 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006916 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6917 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006918 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6919 "check" :
6920 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6921 "resync" : "recovery"))),
6922 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006923 (unsigned long long) resync/2,
6924 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
6926 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 * dt: time from mark until now
6928 * db: blocks written from mark until now
6929 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006930 *
6931 * rt is a sector_t, so could be 32bit or 64bit.
6932 * So we divide before multiply in case it is 32bit and close
6933 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006934 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006935 * near the end of resync when the number of remaining sectors
6936 * is close to 'db'.
6937 * We then divide rt by 32 after multiplying by db to compensate.
6938 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 */
6940 dt = ((jiffies - mddev->resync_mark) / HZ);
6941 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006942 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6943 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
NeilBrowndd71cf62009-05-07 12:49:35 +10006945 rt = max_sectors - resync; /* number of remaining sectors */
6946 sector_div(rt, db/32+1);
6947 rt *= dt;
6948 rt >>= 5;
6949
6950 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6951 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
NeilBrownff4e8d92006-07-10 04:44:16 -07006953 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954}
6955
6956static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6957{
6958 struct list_head *tmp;
6959 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006960 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
6962 if (l >= 0x10000)
6963 return NULL;
6964 if (!l--)
6965 /* header */
6966 return (void*)1;
6967
6968 spin_lock(&all_mddevs_lock);
6969 list_for_each(tmp,&all_mddevs)
6970 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006971 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 mddev_get(mddev);
6973 spin_unlock(&all_mddevs_lock);
6974 return mddev;
6975 }
6976 spin_unlock(&all_mddevs_lock);
6977 if (!l--)
6978 return (void*)2;/* tail */
6979 return NULL;
6980}
6981
6982static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6983{
6984 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006985 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
6987 ++*pos;
6988 if (v == (void*)2)
6989 return NULL;
6990
6991 spin_lock(&all_mddevs_lock);
6992 if (v == (void*)1)
6993 tmp = all_mddevs.next;
6994 else
6995 tmp = mddev->all_mddevs.next;
6996 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006997 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 else {
6999 next_mddev = (void*)2;
7000 *pos = 0x10000;
7001 }
7002 spin_unlock(&all_mddevs_lock);
7003
7004 if (v != (void*)1)
7005 mddev_put(mddev);
7006 return next_mddev;
7007
7008}
7009
7010static void md_seq_stop(struct seq_file *seq, void *v)
7011{
NeilBrownfd01b882011-10-11 16:47:53 +11007012 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014 if (mddev && v != (void*)1 && v != (void*)2)
7015 mddev_put(mddev);
7016}
7017
7018static int md_seq_show(struct seq_file *seq, void *v)
7019{
NeilBrownfd01b882011-10-11 16:47:53 +11007020 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007021 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007022 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007025 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 seq_printf(seq, "Personalities : ");
7027 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007028 list_for_each_entry(pers, &pers_list, list)
7029 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 spin_unlock(&pers_lock);
7032 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007033 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 return 0;
7035 }
7036 if (v == (void*)2) {
7037 status_unused(seq);
7038 return 0;
7039 }
7040
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007041 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007043
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7045 seq_printf(seq, "%s : %sactive", mdname(mddev),
7046 mddev->pers ? "" : "in");
7047 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007048 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007050 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007051 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 seq_printf(seq, " %s", mddev->pers->name);
7053 }
7054
Andre Nolldd8ac332009-03-31 14:33:13 +11007055 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007056 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 char b[BDEVNAME_SIZE];
7058 seq_printf(seq, " %s[%d]",
7059 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007060 if (test_bit(WriteMostly, &rdev->flags))
7061 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007062 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 seq_printf(seq, "(F)");
7064 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007065 }
7066 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007067 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007068 if (test_bit(Replacement, &rdev->flags))
7069 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007070 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 }
7072
7073 if (!list_empty(&mddev->disks)) {
7074 if (mddev->pers)
7075 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007076 (unsigned long long)
7077 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 else
7079 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007080 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007082 if (mddev->persistent) {
7083 if (mddev->major_version != 0 ||
7084 mddev->minor_version != 90) {
7085 seq_printf(seq," super %d.%d",
7086 mddev->major_version,
7087 mddev->minor_version);
7088 }
NeilBrowne6910632008-02-06 01:39:51 -08007089 } else if (mddev->external)
7090 seq_printf(seq, " super external:%s",
7091 mddev->metadata_type);
7092 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007093 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094
7095 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007096 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007098 if (mddev->pers->sync_request) {
7099 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007100 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007101 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007102 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007103 seq_printf(seq, "\tresync=DELAYED\n ");
7104 else if (mddev->recovery_cp < MaxSector)
7105 seq_printf(seq, "\tresync=PENDING\n ");
7106 }
NeilBrown32a76272005-06-21 17:17:14 -07007107 } else
7108 seq_printf(seq, "\n ");
7109
NeilBrown57148962012-03-19 12:46:40 +11007110 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
7112 seq_printf(seq, "\n");
7113 }
7114 mddev_unlock(mddev);
7115
7116 return 0;
7117}
7118
Jan Engelhardt110518b2009-05-07 12:49:37 +10007119static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 .start = md_seq_start,
7121 .next = md_seq_next,
7122 .stop = md_seq_stop,
7123 .show = md_seq_show,
7124};
7125
7126static int md_seq_open(struct inode *inode, struct file *file)
7127{
Kay Sieversf1514632011-07-12 20:48:39 +02007128 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 int error;
7130
7131 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007132 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007133 return error;
7134
7135 seq = file->private_data;
7136 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 return error;
7138}
7139
NeilBrownd7603b72006-01-06 00:20:30 -08007140static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7141{
Kay Sieversf1514632011-07-12 20:48:39 +02007142 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007143 int mask;
7144
7145 poll_wait(filp, &md_event_waiters, wait);
7146
7147 /* always allow read */
7148 mask = POLLIN | POLLRDNORM;
7149
Kay Sieversf1514632011-07-12 20:48:39 +02007150 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007151 mask |= POLLERR | POLLPRI;
7152 return mask;
7153}
7154
Arjan van de Venfa027c22007-02-12 00:55:33 -08007155static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007156 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 .open = md_seq_open,
7158 .read = seq_read,
7159 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007160 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007161 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162};
7163
NeilBrown84fc4b52011-10-11 16:49:58 +11007164int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007167 list_add_tail(&p->list, &pers_list);
7168 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 spin_unlock(&pers_lock);
7170 return 0;
7171}
7172
NeilBrown84fc4b52011-10-11 16:49:58 +11007173int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174{
NeilBrown2604b702006-01-06 00:20:36 -08007175 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007177 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 spin_unlock(&pers_lock);
7179 return 0;
7180}
7181
NeilBrownfd01b882011-10-11 16:47:53 +11007182static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183{
NeilBrown3cb03002011-10-11 16:45:26 +11007184 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007186 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
7188 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007189 rcu_read_lock();
7190 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007192 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7193 (int)part_stat_read(&disk->part0, sectors[1]) -
7194 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007195 /* sync IO will cause sync_io to increase before the disk_stats
7196 * as sync_io is counted when a request starts, and
7197 * disk_stats is counted when it completes.
7198 * So resync activity will cause curr_events to be smaller than
7199 * when there was no such activity.
7200 * non-sync IO will cause disk_stat to increase without
7201 * increasing sync_io so curr_events will (eventually)
7202 * be larger than it was before. Once it becomes
7203 * substantially larger, the test below will cause
7204 * the array to appear non-idle, and resync will slow
7205 * down.
7206 * If there is a lot of outstanding resync activity when
7207 * we set last_event to curr_events, then all that activity
7208 * completing might cause the array to appear non-idle
7209 * and resync will be slowed down even though there might
7210 * not have been non-resync activity. This will only
7211 * happen once though. 'last_events' will soon reflect
7212 * the state where there is little or no outstanding
7213 * resync requests, and further resync activity will
7214 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007217 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 rdev->last_events = curr_events;
7219 idle = 0;
7220 }
7221 }
NeilBrown4b809912008-07-21 17:05:25 +10007222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 return idle;
7224}
7225
NeilBrownfd01b882011-10-11 16:47:53 +11007226void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227{
7228 /* another "blocks" (512byte) blocks have been synced */
7229 atomic_sub(blocks, &mddev->recovery_active);
7230 wake_up(&mddev->recovery_wait);
7231 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007232 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007233 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 md_wakeup_thread(mddev->thread);
7235 // stop recovery, signal do_sync ....
7236 }
7237}
7238
7239
NeilBrown06d91a52005-06-21 17:17:12 -07007240/* md_write_start(mddev, bi)
7241 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007242 * in superblock) before writing, schedule a superblock update
7243 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007244 */
NeilBrownfd01b882011-10-11 16:47:53 +11007245void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246{
Neil Brown0fd62b82008-06-28 08:31:36 +10007247 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007248 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007249 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007250
NeilBrownf91de922005-11-08 21:39:36 -08007251 BUG_ON(mddev->ro == 1);
7252 if (mddev->ro == 2) {
7253 /* need to switch to read/write */
7254 mddev->ro = 0;
7255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7256 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007257 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007258 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007259 }
NeilBrown06d91a52005-06-21 17:17:12 -07007260 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007261 if (mddev->safemode == 1)
7262 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007263 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007264 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007265 if (mddev->in_sync) {
7266 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007267 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007268 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007269 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007270 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007271 }
NeilBrowna9701a32005-11-08 21:39:34 -08007272 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007273 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007274 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007275 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007276 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007277 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278}
7279
NeilBrownfd01b882011-10-11 16:47:53 +11007280void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281{
7282 if (atomic_dec_and_test(&mddev->writes_pending)) {
7283 if (mddev->safemode == 2)
7284 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007285 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7287 }
7288}
7289
NeilBrown2a2275d2007-01-26 00:57:11 -08007290/* md_allow_write(mddev)
7291 * Calling this ensures that the array is marked 'active' so that writes
7292 * may proceed without blocking. It is important to call this before
7293 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7294 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007295 *
7296 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7297 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007298 */
NeilBrownfd01b882011-10-11 16:47:53 +11007299int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007300{
7301 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007302 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007303 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007304 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007305 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007306 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007307
7308 spin_lock_irq(&mddev->write_lock);
7309 if (mddev->in_sync) {
7310 mddev->in_sync = 0;
7311 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007312 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007313 if (mddev->safemode_delay &&
7314 mddev->safemode == 0)
7315 mddev->safemode = 1;
7316 spin_unlock_irq(&mddev->write_lock);
7317 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007318 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007319 } else
7320 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007321
NeilBrown070dc6d2010-08-30 17:33:34 +10007322 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007323 return -EAGAIN;
7324 else
7325 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007326}
7327EXPORT_SYMBOL_GPL(md_allow_write);
7328
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329#define SYNC_MARKS 10
7330#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007331#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007332void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333{
Shaohua Li4ed87312012-10-11 13:34:00 +11007334 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007335 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 unsigned int currspeed = 0,
7337 window;
NeilBrown57afd892005-06-21 17:17:13 -07007338 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007340 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 sector_t mark_cnt[SYNC_MARKS];
7342 int last_mark,m;
7343 struct list_head *tmp;
7344 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007345 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007346 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007347 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007348 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349
7350 /* just incase thread restarts... */
7351 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7352 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007353 if (mddev->ro) /* never try to sync a read-only array */
7354 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
NeilBrown61df9d92006-10-03 01:15:57 -07007356 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007357 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007358 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007359 action = "check";
7360 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007361 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007362 action = "repair";
7363 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007364 desc = "resync";
7365 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7366 desc = "reshape";
7367 else
7368 desc = "recovery";
7369
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007370 mddev->last_sync_action = action ?: desc;
7371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 /* we overload curr_resync somewhat here.
7373 * 0 == not engaged in resync at all
7374 * 2 == checking that there is no conflict with another sync
7375 * 1 == like 2, but have yielded to allow conflicting resync to
7376 * commense
7377 * other == active in resync - this many blocks
7378 *
7379 * Before starting a resync we must have set curr_resync to
7380 * 2, and then checked that every "conflicting" array has curr_resync
7381 * less than ours. When we find one that is the same or higher
7382 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7383 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7384 * This will mean we have to start checking from the beginning again.
7385 *
7386 */
7387
7388 do {
7389 mddev->curr_resync = 2;
7390
7391 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007392 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007394 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 if (mddev2 == mddev)
7396 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007397 if (!mddev->parallel_resync
7398 && mddev2->curr_resync
7399 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 DEFINE_WAIT(wq);
7401 if (mddev < mddev2 && mddev->curr_resync == 2) {
7402 /* arbitrarily yield */
7403 mddev->curr_resync = 1;
7404 wake_up(&resync_wait);
7405 }
7406 if (mddev > mddev2 && mddev->curr_resync == 1)
7407 /* no need to wait here, we can wait the next
7408 * time 'round when curr_resync == 2
7409 */
7410 continue;
NeilBrown97441972008-09-19 11:49:54 +10007411 /* We need to wait 'interruptible' so as not to
7412 * contribute to the load average, and not to
7413 * be caught by 'softlockup'
7414 */
7415 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007416 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007417 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007418 printk(KERN_INFO "md: delaying %s of %s"
7419 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007421 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007423 if (signal_pending(current))
7424 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 schedule();
7426 finish_wait(&resync_wait, &wq);
7427 goto try_again;
7428 }
7429 finish_wait(&resync_wait, &wq);
7430 }
7431 }
7432 } while (mddev->curr_resync < 2);
7433
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007434 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007435 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007437 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 */
7439 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007440 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007441 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007442 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7443 j = mddev->resync_min;
7444 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007445 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007446
NeilBrownccfcc3c2006-03-27 01:18:09 -08007447 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007448 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007449 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007451 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007452 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007453 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007454 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007455 if (rdev->raid_disk >= 0 &&
7456 !test_bit(Faulty, &rdev->flags) &&
7457 !test_bit(In_sync, &rdev->flags) &&
7458 rdev->recovery_offset < j)
7459 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007460 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
NeilBrown61df9d92006-10-03 01:15:57 -07007463 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7464 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7465 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007466 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007467 "(but not more than %d KB/sec) for %s.\n",
7468 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
NeilBrowneea1bf32009-03-31 14:27:02 +11007470 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007471
NeilBrown57afd892005-06-21 17:17:13 -07007472 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 for (m = 0; m < SYNC_MARKS; m++) {
7474 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007475 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
7477 last_mark = 0;
7478 mddev->resync_mark = mark[last_mark];
7479 mddev->resync_mark_cnt = mark_cnt[last_mark];
7480
7481 /*
7482 * Tune reconstruction:
7483 */
7484 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007485 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7486 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
7488 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 last_check = 0;
7490
7491 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007492 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007493 "md: resuming %s of %s from checkpoint.\n",
7494 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007496 } else
7497 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007498 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007499 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7500 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007501 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
majianpeng7c2c57c2012-07-03 12:12:26 +10007503 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007505 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
NeilBrown57afd892005-06-21 17:17:13 -07007507 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007508
NeilBrown7a91ee12009-05-26 12:57:21 +10007509 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7510 ((mddev->curr_resync > mddev->curr_resync_completed &&
7511 (mddev->curr_resync - mddev->curr_resync_completed)
7512 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007513 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007514 (j - mddev->curr_resync_completed)*2
7515 >= mddev->resync_max - mddev->curr_resync_completed
7516 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007517 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007518 wait_event(mddev->recovery_wait,
7519 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007520 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007521 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7522 j > mddev->recovery_cp)
7523 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007524 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007525 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007526 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007527 }
NeilBrownacb180b2009-04-14 16:28:34 +10007528
NeilBrownc91abf52013-11-19 12:02:01 +11007529 while (j >= mddev->resync_max &&
7530 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007531 /* As this condition is controlled by user-space,
7532 * we can block indefinitely, so use '_interruptible'
7533 * to avoid triggering warnings.
7534 */
7535 flush_signals(current); /* just in case */
7536 wait_event_interruptible(mddev->recovery_wait,
7537 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007538 || test_bit(MD_RECOVERY_INTR,
7539 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007540 }
NeilBrownacb180b2009-04-14 16:28:34 +10007541
NeilBrownc91abf52013-11-19 12:02:01 +11007542 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7543 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007544
NeilBrown57afd892005-06-21 17:17:13 -07007545 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007546 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007547 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007548 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 }
NeilBrown57afd892005-06-21 17:17:13 -07007551
7552 if (!skipped) { /* actual IO requested */
7553 io_sectors += sectors;
7554 atomic_add(sectors, &mddev->recovery_active);
7555 }
7556
NeilBrowne875ece2011-07-28 11:39:24 +10007557 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7558 break;
7559
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007561 if (j > 2)
7562 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007563 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007564 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007565 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007566 * visible in /proc/mdstat
7567 */
7568 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007569
7570 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 continue;
7572
NeilBrown57afd892005-06-21 17:17:13 -07007573 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 repeat:
7575 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7576 /* step marks */
7577 int next = (last_mark+1) % SYNC_MARKS;
7578
7579 mddev->resync_mark = mark[next];
7580 mddev->resync_mark_cnt = mark_cnt[next];
7581 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007582 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 last_mark = next;
7584 }
7585
NeilBrownc91abf52013-11-19 12:02:01 +11007586 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
7589 /*
7590 * this loop exits only if either when we are slower than
7591 * the 'hard' speed limit, or the system was IO-idle for
7592 * a jiffy.
7593 * the system might be non-idle CPU-wise, but we only care
7594 * about not overloading the IO subsystem. (things like an
7595 * e2fsck being done on the RAID array should execute fast)
7596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 cond_resched();
7598
NeilBrown57afd892005-06-21 17:17:13 -07007599 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7600 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007601
NeilBrown88202a02006-01-06 00:21:36 -08007602 if (currspeed > speed_min(mddev)) {
7603 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007604 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007605 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 goto repeat;
7607 }
7608 }
7609 }
NeilBrownc91abf52013-11-19 12:02:01 +11007610 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7611 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7612 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 /*
7614 * this also signals 'finished resyncing' to md_stop
7615 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007616 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7618
7619 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007620 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621
NeilBrowndfc70642008-05-23 13:04:39 -07007622 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007623 mddev->curr_resync > 2) {
7624 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7625 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7626 if (mddev->curr_resync >= mddev->recovery_cp) {
7627 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007628 "md: checkpointing %s of %s.\n",
7629 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007630 if (test_bit(MD_RECOVERY_ERROR,
7631 &mddev->recovery))
7632 mddev->recovery_cp =
7633 mddev->curr_resync_completed;
7634 else
7635 mddev->recovery_cp =
7636 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007637 }
7638 } else
7639 mddev->recovery_cp = MaxSector;
7640 } else {
7641 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7642 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007643 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007644 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007645 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007646 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007647 !test_bit(Faulty, &rdev->flags) &&
7648 !test_bit(In_sync, &rdev->flags) &&
7649 rdev->recovery_offset < mddev->curr_resync)
7650 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007651 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007654 skip:
NeilBrown17571282006-12-10 02:20:52 -08007655 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
NeilBrownc07b70a2009-12-14 12:49:48 +11007657 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7658 /* We completed so min/max setting can be forgotten if used. */
7659 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7660 mddev->resync_min = 0;
7661 mddev->resync_max = MaxSector;
7662 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7663 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664 mddev->curr_resync = 0;
7665 wake_up(&resync_wait);
7666 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7667 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007668 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669}
NeilBrown29269552006-03-27 01:18:10 -08007670EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671
NeilBrown746d3202013-04-24 11:42:41 +10007672static int remove_and_add_spares(struct mddev *mddev,
7673 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007674{
NeilBrown3cb03002011-10-11 16:45:26 +11007675 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007676 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007677 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007678
NeilBrowndafb20f2012-03-19 12:46:39 +11007679 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007680 if ((this == NULL || rdev == this) &&
7681 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007682 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007683 (test_bit(Faulty, &rdev->flags) ||
7684 ! test_bit(In_sync, &rdev->flags)) &&
7685 atomic_read(&rdev->nr_pending)==0) {
7686 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007687 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007688 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007689 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007690 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007691 }
7692 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007693 if (removed && mddev->kobj.sd)
7694 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007695
NeilBrown746d3202013-04-24 11:42:41 +10007696 if (this)
7697 goto no_add;
7698
NeilBrowndafb20f2012-03-19 12:46:39 +11007699 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007700 if (rdev->raid_disk >= 0 &&
7701 !test_bit(In_sync, &rdev->flags) &&
7702 !test_bit(Faulty, &rdev->flags))
7703 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007704 if (rdev->raid_disk >= 0)
7705 continue;
7706 if (test_bit(Faulty, &rdev->flags))
7707 continue;
7708 if (mddev->ro &&
7709 rdev->saved_raid_disk < 0)
7710 continue;
7711
7712 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007713 if (mddev->pers->
7714 hot_add_disk(mddev, rdev) == 0) {
7715 if (sysfs_link_rdev(mddev, rdev))
7716 /* failure here is OK */;
7717 spares++;
7718 md_new_event(mddev);
7719 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007720 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007721 }
NeilBrown746d3202013-04-24 11:42:41 +10007722no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007723 if (removed)
7724 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007725 return spares;
7726}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007727
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728/*
7729 * This routine is regularly called by all per-raid-array threads to
7730 * deal with generic issues like resync and super-block update.
7731 * Raid personalities that don't have a thread (linear/raid0) do not
7732 * need this as they never do any recovery or update the superblock.
7733 *
7734 * It does not do any resync itself, but rather "forks" off other threads
7735 * to do that as needed.
7736 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7737 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007738 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 * and wakeups up this thread which will reap the thread and finish up.
7740 * This thread also removes any faulty devices (with nr_pending == 0).
7741 *
7742 * The overall approach is:
7743 * 1/ if the superblock needs updating, update it.
7744 * 2/ If a recovery thread is running, don't do anything else.
7745 * 3/ If recovery has finished, clean up, possibly marking spares active.
7746 * 4/ If there are any faulty devices, remove them.
7747 * 5/ If array is degraded, try to add spares devices
7748 * 6/ If array has spares or is not in-sync, start a resync thread.
7749 */
NeilBrownfd01b882011-10-11 16:47:53 +11007750void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007752 if (mddev->suspended)
7753 return;
7754
NeilBrown5f404022005-06-21 17:17:16 -07007755 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007756 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
NeilBrownfca4d842005-06-21 17:17:11 -07007758 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007759 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007760 printk(KERN_INFO "md: %s in immediate safe mode\n",
7761 mdname(mddev));
7762 mddev->safemode = 2;
7763 }
7764 flush_signals(current);
7765 }
7766
NeilBrownc89a8ee2008-08-05 15:54:13 +10007767 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7768 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007770 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007772 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007773 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007774 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7775 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 ))
7777 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007778
NeilBrowndf5b89b2006-03-27 01:18:20 -08007779 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007780 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007781
NeilBrownc89a8ee2008-08-05 15:54:13 +10007782 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007783 /* On a read-only array we can:
7784 * - remove failed devices
7785 * - add already-in_sync devices if the array itself
7786 * is in-sync.
7787 * As we only add devices that are already in-sync,
7788 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007789 */
NeilBrownc89a8ee2008-08-05 15:54:13 +10007790 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown7ceb17e2013-04-24 11:42:42 +10007791 remove_and_add_spares(mddev, NULL);
7792 mddev->pers->spare_active(mddev);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007793 goto unlock;
7794 }
7795
NeilBrown31a59e32008-04-30 00:52:30 -07007796 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007797 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007798 spin_lock_irq(&mddev->write_lock);
7799 if (mddev->safemode &&
7800 !atomic_read(&mddev->writes_pending) &&
7801 !mddev->in_sync &&
7802 mddev->recovery_cp == MaxSector) {
7803 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007804 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007805 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007806 }
7807 if (mddev->safemode == 1)
7808 mddev->safemode = 0;
7809 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007810 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007811 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007812 }
NeilBrownfca4d842005-06-21 17:17:11 -07007813
NeilBrown7a0a5352013-08-27 16:28:23 +10007814 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007815 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7818 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7819 /* resync/recovery still happening */
7820 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7821 goto unlock;
7822 }
7823 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007824 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 goto unlock;
7826 }
Neil Brown72a23c22008-06-28 08:31:41 +10007827 /* Set RUNNING before clearing NEEDED to avoid
7828 * any transients in the value of "sync_action".
7829 */
NeilBrown72f36d52012-10-11 14:25:57 +11007830 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007831 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007832 /* Clear some bits that don't mean anything, but
7833 * might be left set
7834 */
NeilBrown24dd4692005-11-08 21:39:26 -08007835 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7836 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837
NeilBrowned209582012-04-24 10:23:14 +10007838 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7839 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007840 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 /* no recovery is running.
7842 * remove any failed drives, then
7843 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007844 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 * the personality to fail the re-add.
7846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007848 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007849 if (mddev->pers->check_reshape == NULL ||
7850 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007851 /* Cannot proceed */
7852 goto unlock;
7853 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007854 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007855 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007856 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7857 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007858 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007859 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007860 } else if (mddev->recovery_cp < MaxSector) {
7861 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007862 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007863 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7864 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007866
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007868 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007869 /* We are adding a device or devices to an array
7870 * which has the bitmap stored on all devices.
7871 * So make sure all bitmap pages get written
7872 */
7873 bitmap_write_all(mddev->bitmap);
7874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 mddev->sync_thread = md_register_thread(md_do_sync,
7876 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007877 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 if (!mddev->sync_thread) {
7879 printk(KERN_ERR "%s: could not start resync"
7880 " thread...\n",
7881 mdname(mddev));
7882 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007883 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7884 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7886 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7887 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007890 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007891 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 }
7893 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007894 wake_up(&mddev->sb_wait);
7895
Neil Brown72a23c22008-06-28 08:31:41 +10007896 if (!mddev->sync_thread) {
7897 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7898 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7899 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007900 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007901 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 mddev_unlock(mddev);
7904 }
7905}
7906
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007907void md_reap_sync_thread(struct mddev *mddev)
7908{
7909 struct md_rdev *rdev;
7910
7911 /* resync has finished, collect result */
7912 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007913 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007914 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7915 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7916 /* success...*/
7917 /* activate any spares */
7918 if (mddev->pers->spare_active(mddev)) {
7919 sysfs_notify(&mddev->kobj, NULL,
7920 "degraded");
7921 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7922 }
7923 }
7924 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7925 mddev->pers->finish_reshape)
7926 mddev->pers->finish_reshape(mddev);
7927
7928 /* If array is no-longer degraded, then any saved_raid_disk
7929 * information must be scrapped. Also if any device is now
7930 * In_sync we must scrape the saved_raid_disk for that device
7931 * do the superblock for an incrementally recovered device
7932 * written out.
7933 */
7934 rdev_for_each(rdev, mddev)
7935 if (!mddev->degraded ||
7936 test_bit(In_sync, &rdev->flags))
7937 rdev->saved_raid_disk = -1;
7938
7939 md_update_sb(mddev, 1);
7940 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7941 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7942 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7943 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7944 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7945 /* flag recovery needed just to double check */
7946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7947 sysfs_notify_dirent_safe(mddev->sysfs_action);
7948 md_new_event(mddev);
7949 if (mddev->event_work.func)
7950 queue_work(md_misc_wq, &mddev->event_work);
7951}
7952
NeilBrownfd01b882011-10-11 16:47:53 +11007953void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007954{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007955 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007956 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007957 !test_bit(Blocked, &rdev->flags) &&
7958 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007959 msecs_to_jiffies(5000));
7960 rdev_dec_pending(rdev, mddev);
7961}
7962EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7963
NeilBrownc6563a82012-05-21 09:27:00 +10007964void md_finish_reshape(struct mddev *mddev)
7965{
7966 /* called be personality module when reshape completes. */
7967 struct md_rdev *rdev;
7968
7969 rdev_for_each(rdev, mddev) {
7970 if (rdev->data_offset > rdev->new_data_offset)
7971 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7972 else
7973 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7974 rdev->data_offset = rdev->new_data_offset;
7975 }
7976}
7977EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007978
7979/* Bad block management.
7980 * We can record which blocks on each device are 'bad' and so just
7981 * fail those blocks, or that stripe, rather than the whole device.
7982 * Entries in the bad-block table are 64bits wide. This comprises:
7983 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7984 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7985 * A 'shift' can be set so that larger blocks are tracked and
7986 * consequently larger devices can be covered.
7987 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7988 *
7989 * Locking of the bad-block table uses a seqlock so md_is_badblock
7990 * might need to retry if it is very unlucky.
7991 * We will sometimes want to check for bad blocks in a bi_end_io function,
7992 * so we use the write_seqlock_irq variant.
7993 *
7994 * When looking for a bad block we specify a range and want to
7995 * know if any block in the range is bad. So we binary-search
7996 * to the last range that starts at-or-before the given endpoint,
7997 * (or "before the sector after the target range")
7998 * then see if it ends after the given start.
7999 * We return
8000 * 0 if there are no known bad blocks in the range
8001 * 1 if there are known bad block which are all acknowledged
8002 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8003 * plus the start/length of the first bad section we overlap.
8004 */
8005int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8006 sector_t *first_bad, int *bad_sectors)
8007{
8008 int hi;
majianpengab056132012-11-06 17:13:44 +08008009 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008010 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008011 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008012 sector_t target = s + sectors;
8013 unsigned seq;
8014
8015 if (bb->shift > 0) {
8016 /* round the start down, and the end up */
8017 s >>= bb->shift;
8018 target += (1<<bb->shift) - 1;
8019 target >>= bb->shift;
8020 sectors = target - s;
8021 }
8022 /* 'target' is now the first block after the bad range */
8023
8024retry:
8025 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008026 lo = 0;
8027 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008028 hi = bb->count;
8029
8030 /* Binary search between lo and hi for 'target'
8031 * i.e. for the last range that starts before 'target'
8032 */
8033 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8034 * are known not to be the last range before target.
8035 * VARIANT: hi-lo is the number of possible
8036 * ranges, and decreases until it reaches 1
8037 */
8038 while (hi - lo > 1) {
8039 int mid = (lo + hi) / 2;
8040 sector_t a = BB_OFFSET(p[mid]);
8041 if (a < target)
8042 /* This could still be the one, earlier ranges
8043 * could not. */
8044 lo = mid;
8045 else
8046 /* This and later ranges are definitely out. */
8047 hi = mid;
8048 }
8049 /* 'lo' might be the last that started before target, but 'hi' isn't */
8050 if (hi > lo) {
8051 /* need to check all range that end after 's' to see if
8052 * any are unacknowledged.
8053 */
8054 while (lo >= 0 &&
8055 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8056 if (BB_OFFSET(p[lo]) < target) {
8057 /* starts before the end, and finishes after
8058 * the start, so they must overlap
8059 */
8060 if (rv != -1 && BB_ACK(p[lo]))
8061 rv = 1;
8062 else
8063 rv = -1;
8064 *first_bad = BB_OFFSET(p[lo]);
8065 *bad_sectors = BB_LEN(p[lo]);
8066 }
8067 lo--;
8068 }
8069 }
8070
8071 if (read_seqretry(&bb->lock, seq))
8072 goto retry;
8073
8074 return rv;
8075}
8076EXPORT_SYMBOL_GPL(md_is_badblock);
8077
8078/*
8079 * Add a range of bad blocks to the table.
8080 * This might extend the table, or might contract it
8081 * if two adjacent ranges can be merged.
8082 * We binary-search to find the 'insertion' point, then
8083 * decide how best to handle it.
8084 */
8085static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8086 int acknowledged)
8087{
8088 u64 *p;
8089 int lo, hi;
8090 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008091 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008092
8093 if (bb->shift < 0)
8094 /* badblocks are disabled */
8095 return 0;
8096
8097 if (bb->shift) {
8098 /* round the start down, and the end up */
8099 sector_t next = s + sectors;
8100 s >>= bb->shift;
8101 next += (1<<bb->shift) - 1;
8102 next >>= bb->shift;
8103 sectors = next - s;
8104 }
8105
Bian Yu905b0292013-10-12 01:10:03 -04008106 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008107
8108 p = bb->page;
8109 lo = 0;
8110 hi = bb->count;
8111 /* Find the last range that starts at-or-before 's' */
8112 while (hi - lo > 1) {
8113 int mid = (lo + hi) / 2;
8114 sector_t a = BB_OFFSET(p[mid]);
8115 if (a <= s)
8116 lo = mid;
8117 else
8118 hi = mid;
8119 }
8120 if (hi > lo && BB_OFFSET(p[lo]) > s)
8121 hi = lo;
8122
8123 if (hi > lo) {
8124 /* we found a range that might merge with the start
8125 * of our new range
8126 */
8127 sector_t a = BB_OFFSET(p[lo]);
8128 sector_t e = a + BB_LEN(p[lo]);
8129 int ack = BB_ACK(p[lo]);
8130 if (e >= s) {
8131 /* Yes, we can merge with a previous range */
8132 if (s == a && s + sectors >= e)
8133 /* new range covers old */
8134 ack = acknowledged;
8135 else
8136 ack = ack && acknowledged;
8137
8138 if (e < s + sectors)
8139 e = s + sectors;
8140 if (e - a <= BB_MAX_LEN) {
8141 p[lo] = BB_MAKE(a, e-a, ack);
8142 s = e;
8143 } else {
8144 /* does not all fit in one range,
8145 * make p[lo] maximal
8146 */
8147 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8148 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8149 s = a + BB_MAX_LEN;
8150 }
8151 sectors = e - s;
8152 }
8153 }
8154 if (sectors && hi < bb->count) {
8155 /* 'hi' points to the first range that starts after 's'.
8156 * Maybe we can merge with the start of that range */
8157 sector_t a = BB_OFFSET(p[hi]);
8158 sector_t e = a + BB_LEN(p[hi]);
8159 int ack = BB_ACK(p[hi]);
8160 if (a <= s + sectors) {
8161 /* merging is possible */
8162 if (e <= s + sectors) {
8163 /* full overlap */
8164 e = s + sectors;
8165 ack = acknowledged;
8166 } else
8167 ack = ack && acknowledged;
8168
8169 a = s;
8170 if (e - a <= BB_MAX_LEN) {
8171 p[hi] = BB_MAKE(a, e-a, ack);
8172 s = e;
8173 } else {
8174 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8175 s = a + BB_MAX_LEN;
8176 }
8177 sectors = e - s;
8178 lo = hi;
8179 hi++;
8180 }
8181 }
8182 if (sectors == 0 && hi < bb->count) {
8183 /* we might be able to combine lo and hi */
8184 /* Note: 's' is at the end of 'lo' */
8185 sector_t a = BB_OFFSET(p[hi]);
8186 int lolen = BB_LEN(p[lo]);
8187 int hilen = BB_LEN(p[hi]);
8188 int newlen = lolen + hilen - (s - a);
8189 if (s >= a && newlen < BB_MAX_LEN) {
8190 /* yes, we can combine them */
8191 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8192 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8193 memmove(p + hi, p + hi + 1,
8194 (bb->count - hi - 1) * 8);
8195 bb->count--;
8196 }
8197 }
8198 while (sectors) {
8199 /* didn't merge (it all).
8200 * Need to add a range just before 'hi' */
8201 if (bb->count >= MD_MAX_BADBLOCKS) {
8202 /* No room for more */
8203 rv = 0;
8204 break;
8205 } else {
8206 int this_sectors = sectors;
8207 memmove(p + hi + 1, p + hi,
8208 (bb->count - hi) * 8);
8209 bb->count++;
8210
8211 if (this_sectors > BB_MAX_LEN)
8212 this_sectors = BB_MAX_LEN;
8213 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8214 sectors -= this_sectors;
8215 s += this_sectors;
8216 }
8217 }
8218
8219 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008220 if (!acknowledged)
8221 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008222 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008223
8224 return rv;
8225}
8226
NeilBrown3cb03002011-10-11 16:45:26 +11008227int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008228 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008229{
NeilBrownc6563a82012-05-21 09:27:00 +10008230 int rv;
8231 if (is_new)
8232 s += rdev->new_data_offset;
8233 else
8234 s += rdev->data_offset;
8235 rv = md_set_badblocks(&rdev->badblocks,
8236 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008237 if (rv) {
8238 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008239 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008240 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8241 md_wakeup_thread(rdev->mddev->thread);
8242 }
8243 return rv;
8244}
8245EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8246
8247/*
8248 * Remove a range of bad blocks from the table.
8249 * This may involve extending the table if we spilt a region,
8250 * but it must not fail. So if the table becomes full, we just
8251 * drop the remove request.
8252 */
8253static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8254{
8255 u64 *p;
8256 int lo, hi;
8257 sector_t target = s + sectors;
8258 int rv = 0;
8259
8260 if (bb->shift > 0) {
8261 /* When clearing we round the start up and the end down.
8262 * This should not matter as the shift should align with
8263 * the block size and no rounding should ever be needed.
8264 * However it is better the think a block is bad when it
8265 * isn't than to think a block is not bad when it is.
8266 */
8267 s += (1<<bb->shift) - 1;
8268 s >>= bb->shift;
8269 target >>= bb->shift;
8270 sectors = target - s;
8271 }
8272
8273 write_seqlock_irq(&bb->lock);
8274
8275 p = bb->page;
8276 lo = 0;
8277 hi = bb->count;
8278 /* Find the last range that starts before 'target' */
8279 while (hi - lo > 1) {
8280 int mid = (lo + hi) / 2;
8281 sector_t a = BB_OFFSET(p[mid]);
8282 if (a < target)
8283 lo = mid;
8284 else
8285 hi = mid;
8286 }
8287 if (hi > lo) {
8288 /* p[lo] is the last range that could overlap the
8289 * current range. Earlier ranges could also overlap,
8290 * but only this one can overlap the end of the range.
8291 */
8292 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8293 /* Partial overlap, leave the tail of this range */
8294 int ack = BB_ACK(p[lo]);
8295 sector_t a = BB_OFFSET(p[lo]);
8296 sector_t end = a + BB_LEN(p[lo]);
8297
8298 if (a < s) {
8299 /* we need to split this range */
8300 if (bb->count >= MD_MAX_BADBLOCKS) {
8301 rv = 0;
8302 goto out;
8303 }
8304 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8305 bb->count++;
8306 p[lo] = BB_MAKE(a, s-a, ack);
8307 lo++;
8308 }
8309 p[lo] = BB_MAKE(target, end - target, ack);
8310 /* there is no longer an overlap */
8311 hi = lo;
8312 lo--;
8313 }
8314 while (lo >= 0 &&
8315 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8316 /* This range does overlap */
8317 if (BB_OFFSET(p[lo]) < s) {
8318 /* Keep the early parts of this range. */
8319 int ack = BB_ACK(p[lo]);
8320 sector_t start = BB_OFFSET(p[lo]);
8321 p[lo] = BB_MAKE(start, s - start, ack);
8322 /* now low doesn't overlap, so.. */
8323 break;
8324 }
8325 lo--;
8326 }
8327 /* 'lo' is strictly before, 'hi' is strictly after,
8328 * anything between needs to be discarded
8329 */
8330 if (hi - lo > 1) {
8331 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8332 bb->count -= (hi - lo - 1);
8333 }
8334 }
8335
8336 bb->changed = 1;
8337out:
8338 write_sequnlock_irq(&bb->lock);
8339 return rv;
8340}
8341
NeilBrownc6563a82012-05-21 09:27:00 +10008342int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8343 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008344{
NeilBrownc6563a82012-05-21 09:27:00 +10008345 if (is_new)
8346 s += rdev->new_data_offset;
8347 else
8348 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008349 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008350 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008351}
8352EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8353
8354/*
8355 * Acknowledge all bad blocks in a list.
8356 * This only succeeds if ->changed is clear. It is used by
8357 * in-kernel metadata updates
8358 */
8359void md_ack_all_badblocks(struct badblocks *bb)
8360{
8361 if (bb->page == NULL || bb->changed)
8362 /* no point even trying */
8363 return;
8364 write_seqlock_irq(&bb->lock);
8365
majianpengecb178b2012-03-19 12:46:42 +11008366 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008367 u64 *p = bb->page;
8368 int i;
8369 for (i = 0; i < bb->count ; i++) {
8370 if (!BB_ACK(p[i])) {
8371 sector_t start = BB_OFFSET(p[i]);
8372 int len = BB_LEN(p[i]);
8373 p[i] = BB_MAKE(start, len, 1);
8374 }
8375 }
NeilBrownde393cd2011-07-28 11:31:48 +10008376 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008377 }
8378 write_sequnlock_irq(&bb->lock);
8379}
8380EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8381
NeilBrown16c791a2011-07-28 11:31:47 +10008382/* sysfs access to bad-blocks list.
8383 * We present two files.
8384 * 'bad-blocks' lists sector numbers and lengths of ranges that
8385 * are recorded as bad. The list is truncated to fit within
8386 * the one-page limit of sysfs.
8387 * Writing "sector length" to this file adds an acknowledged
8388 * bad block list.
8389 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8390 * been acknowledged. Writing to this file adds bad blocks
8391 * without acknowledging them. This is largely for testing.
8392 */
8393
8394static ssize_t
8395badblocks_show(struct badblocks *bb, char *page, int unack)
8396{
8397 size_t len;
8398 int i;
8399 u64 *p = bb->page;
8400 unsigned seq;
8401
8402 if (bb->shift < 0)
8403 return 0;
8404
8405retry:
8406 seq = read_seqbegin(&bb->lock);
8407
8408 len = 0;
8409 i = 0;
8410
8411 while (len < PAGE_SIZE && i < bb->count) {
8412 sector_t s = BB_OFFSET(p[i]);
8413 unsigned int length = BB_LEN(p[i]);
8414 int ack = BB_ACK(p[i]);
8415 i++;
8416
8417 if (unack && ack)
8418 continue;
8419
8420 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8421 (unsigned long long)s << bb->shift,
8422 length << bb->shift);
8423 }
NeilBrownde393cd2011-07-28 11:31:48 +10008424 if (unack && len == 0)
8425 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008426
8427 if (read_seqretry(&bb->lock, seq))
8428 goto retry;
8429
8430 return len;
8431}
8432
8433#define DO_DEBUG 1
8434
8435static ssize_t
8436badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8437{
8438 unsigned long long sector;
8439 int length;
8440 char newline;
8441#ifdef DO_DEBUG
8442 /* Allow clearing via sysfs *only* for testing/debugging.
8443 * Normally only a successful write may clear a badblock
8444 */
8445 int clear = 0;
8446 if (page[0] == '-') {
8447 clear = 1;
8448 page++;
8449 }
8450#endif /* DO_DEBUG */
8451
8452 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8453 case 3:
8454 if (newline != '\n')
8455 return -EINVAL;
8456 case 2:
8457 if (length <= 0)
8458 return -EINVAL;
8459 break;
8460 default:
8461 return -EINVAL;
8462 }
8463
8464#ifdef DO_DEBUG
8465 if (clear) {
8466 md_clear_badblocks(bb, sector, length);
8467 return len;
8468 }
8469#endif /* DO_DEBUG */
8470 if (md_set_badblocks(bb, sector, length, !unack))
8471 return len;
8472 else
8473 return -ENOSPC;
8474}
8475
Adrian Bunk75c96f82005-05-05 16:16:09 -07008476static int md_notify_reboot(struct notifier_block *this,
8477 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478{
8479 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008480 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008481 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482
NeilBrownc744a652012-03-19 12:46:37 +11008483 for_each_mddev(mddev, tmp) {
8484 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008485 if (mddev->pers)
8486 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008487 mddev->safemode = 2;
8488 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008489 }
NeilBrownc744a652012-03-19 12:46:37 +11008490 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 }
NeilBrownc744a652012-03-19 12:46:37 +11008492 /*
8493 * certain more exotic SCSI devices are known to be
8494 * volatile wrt too early system reboots. While the
8495 * right place to handle this issue is the given
8496 * driver, we do want to have a safe RAID driver ...
8497 */
8498 if (need_delay)
8499 mdelay(1000*1);
8500
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 return NOTIFY_DONE;
8502}
8503
Adrian Bunk75c96f82005-05-05 16:16:09 -07008504static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 .notifier_call = md_notify_reboot,
8506 .next = NULL,
8507 .priority = INT_MAX, /* before any real devices */
8508};
8509
8510static void md_geninit(void)
8511{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008512 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008514 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515}
8516
Adrian Bunk75c96f82005-05-05 16:16:09 -07008517static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518{
Tejun Heoe804ac72010-10-15 15:36:08 +02008519 int ret = -ENOMEM;
8520
Tejun Heoada609e2011-01-25 14:35:54 +01008521 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008522 if (!md_wq)
8523 goto err_wq;
8524
8525 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8526 if (!md_misc_wq)
8527 goto err_misc_wq;
8528
8529 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8530 goto err_md;
8531
8532 if ((ret = register_blkdev(0, "mdp")) < 0)
8533 goto err_mdp;
8534 mdp_major = ret;
8535
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008536 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008537 md_probe, NULL, NULL);
8538 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 md_probe, NULL, NULL);
8540
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008542 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543
8544 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546
Tejun Heoe804ac72010-10-15 15:36:08 +02008547err_mdp:
8548 unregister_blkdev(MD_MAJOR, "md");
8549err_md:
8550 destroy_workqueue(md_misc_wq);
8551err_misc_wq:
8552 destroy_workqueue(md_wq);
8553err_wq:
8554 return ret;
8555}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
8557#ifndef MODULE
8558
8559/*
8560 * Searches all registered partitions for autorun RAID arrays
8561 * at boot time.
8562 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008563
8564static LIST_HEAD(all_detected_devices);
8565struct detected_devices_node {
8566 struct list_head list;
8567 dev_t dev;
8568};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569
8570void md_autodetect_dev(dev_t dev)
8571{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008572 struct detected_devices_node *node_detected_dev;
8573
8574 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8575 if (node_detected_dev) {
8576 node_detected_dev->dev = dev;
8577 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8578 } else {
8579 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8580 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582}
8583
8584
8585static void autostart_arrays(int part)
8586{
NeilBrown3cb03002011-10-11 16:45:26 +11008587 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008588 struct detected_devices_node *node_detected_dev;
8589 dev_t dev;
8590 int i_scanned, i_passed;
8591
8592 i_scanned = 0;
8593 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594
8595 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8596
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008597 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8598 i_scanned++;
8599 node_detected_dev = list_entry(all_detected_devices.next,
8600 struct detected_devices_node, list);
8601 list_del(&node_detected_dev->list);
8602 dev = node_detected_dev->dev;
8603 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008604 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605 if (IS_ERR(rdev))
8606 continue;
8607
NeilBrownb2d444d2005-11-08 21:39:31 -08008608 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 MD_BUG();
8610 continue;
8611 }
NeilBrownd0fae182008-03-04 14:29:31 -08008612 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008614 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008616
8617 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8618 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619
8620 autorun_devices(part);
8621}
8622
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008623#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624
8625static __exit void md_exit(void)
8626{
NeilBrownfd01b882011-10-11 16:47:53 +11008627 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008629
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008630 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008631 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008633 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634 unregister_blkdev(mdp_major, "mdp");
8635 unregister_reboot_notifier(&md_notifier);
8636 unregister_sysctl_table(raid_table_header);
8637 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008638 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008640 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008642 destroy_workqueue(md_misc_wq);
8643 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644}
8645
Dan Williams685784a2007-07-09 11:56:42 -07008646subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647module_exit(md_exit)
8648
NeilBrownf91de922005-11-08 21:39:36 -08008649static int get_ro(char *buffer, struct kernel_param *kp)
8650{
8651 return sprintf(buffer, "%d", start_readonly);
8652}
8653static int set_ro(const char *val, struct kernel_param *kp)
8654{
8655 char *e;
8656 int num = simple_strtoul(val, &e, 10);
8657 if (*val && (*e == '\0' || *e == '\n')) {
8658 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008659 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008660 }
8661 return -EINVAL;
8662}
8663
NeilBrown80ca3a42006-07-10 04:44:18 -07008664module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8665module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008666
NeilBrownefeb53c2009-01-09 08:31:10 +11008667module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008668
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669EXPORT_SYMBOL(register_md_personality);
8670EXPORT_SYMBOL(unregister_md_personality);
8671EXPORT_SYMBOL(md_error);
8672EXPORT_SYMBOL(md_done_sync);
8673EXPORT_SYMBOL(md_write_start);
8674EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675EXPORT_SYMBOL(md_register_thread);
8676EXPORT_SYMBOL(md_unregister_thread);
8677EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008679EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008681MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008682MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008683MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);