blob: 34846856dbc6106c95b81d948a4e41ad6e4d816d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Kent Overstreet4f024f32013-10-11 15:44:27 -0700396 if (bio->bi_iter.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;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700757 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758 bio_add_page(bio, page, size, 0);
759 bio->bi_private = rdev;
760 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800761
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000763 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800764}
765
NeilBrownfd01b882011-10-11 16:47:53 +1100766void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800767{
Tejun Heoe9c74692010-09-03 11:56:18 +0200768 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800769 DEFINE_WAIT(wq);
770 for(;;) {
771 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
772 if (atomic_read(&mddev->pending_writes)==0)
773 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800774 schedule();
775 }
776 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777}
778
NeilBrown3cb03002011-10-11 16:45:26 +1100779int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
NeilBrowna167f662010-10-26 18:31:13 +1100782 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int ret;
784
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100785 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
786 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100787 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700788 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000789 else if (rdev->mddev->reshape_position != MaxSector &&
790 (rdev->mddev->reshape_backwards ==
791 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700792 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100793 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700794 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700796 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
799 bio_put(bio);
800 return ret;
801}
NeilBrowna8745db2006-01-06 00:20:34 -0800802EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
NeilBrown3cb03002011-10-11 16:45:26 +1100804static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 char b[BDEVNAME_SIZE];
807 if (!rdev->sb_page) {
808 MD_BUG();
809 return -EINVAL;
810 }
811 if (rdev->sb_loaded)
812 return 0;
813
814
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100815 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto fail;
817 rdev->sb_loaded = 1;
818 return 0;
819
820fail:
821 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
822 bdevname(rdev->bdev,b));
823 return -EINVAL;
824}
825
826static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
827{
Andre Noll05710462008-07-11 22:02:20 +1000828 return sb1->set_uuid0 == sb2->set_uuid0 &&
829 sb1->set_uuid1 == sb2->set_uuid1 &&
830 sb1->set_uuid2 == sb2->set_uuid2 &&
831 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
835{
836 int ret;
837 mdp_super_t *tmp1, *tmp2;
838
839 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
840 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
841
842 if (!tmp1 || !tmp2) {
843 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100844 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto abort;
846 }
847
848 *tmp1 = *sb1;
849 *tmp2 = *sb2;
850
851 /*
852 * nr_disks is not constant
853 */
854 tmp1->nr_disks = 0;
855 tmp2->nr_disks = 0;
856
Andre Nollce0c8e02008-07-11 22:02:20 +1000857 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700859 kfree(tmp1);
860 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return ret;
862}
863
NeilBrown4d167f02007-05-09 02:35:37 -0700864
865static u32 md_csum_fold(u32 csum)
866{
867 csum = (csum & 0xffff) + (csum >> 16);
868 return (csum & 0xffff) + (csum >> 16);
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static unsigned int calc_sb_csum(mdp_super_t * sb)
872{
NeilBrown4d167f02007-05-09 02:35:37 -0700873 u64 newcsum = 0;
874 u32 *sb32 = (u32*)sb;
875 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int disk_csum, csum;
877
878 disk_csum = sb->sb_csum;
879 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700880
881 for (i = 0; i < MD_SB_BYTES/4 ; i++)
882 newcsum += sb32[i];
883 csum = (newcsum & 0xffffffff) + (newcsum>>32);
884
885
886#ifdef CONFIG_ALPHA
887 /* This used to use csum_partial, which was wrong for several
888 * reasons including that different results are returned on
889 * different architectures. It isn't critical that we get exactly
890 * the same return value as before (we always csum_fold before
891 * testing, and that removes any differences). However as we
892 * know that csum_partial always returned a 16bit value on
893 * alphas, do a fold to maximise conformity to previous behaviour.
894 */
895 sb->sb_csum = md_csum_fold(disk_csum);
896#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return csum;
900}
901
902
903/*
904 * Handle superblock details.
905 * We want to be able to handle multiple superblock formats
906 * so we have a common interface to them all, and an array of
907 * different handlers.
908 * We rely on user-space to write the initial superblock, and support
909 * reading and updating of superblocks.
910 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100911 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * loads and validates a superblock on dev.
913 * if refdev != NULL, compare superblocks on both devices
914 * Return:
915 * 0 - dev has a superblock that is compatible with refdev
916 * 1 - dev has a superblock that is compatible and newer than refdev
917 * so dev should be used as the refdev in future
918 * -EINVAL superblock incompatible or invalid
919 * -othererror e.g. -EIO
920 *
NeilBrownfd01b882011-10-11 16:47:53 +1100921 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 * Verify that dev is acceptable into mddev.
923 * The first time, mddev->raid_disks will be 0, and data from
924 * dev should be merged in. Subsequent calls check that dev
925 * is new enough. Return 0 or -EINVAL
926 *
NeilBrownfd01b882011-10-11 16:47:53 +1100927 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * Update the superblock for rdev with data in mddev
929 * This does not write to disc.
930 *
931 */
932
933struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000934 char *name;
935 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000936 int (*load_super)(struct md_rdev *rdev,
937 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000938 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000939 int (*validate_super)(struct mddev *mddev,
940 struct md_rdev *rdev);
941 void (*sync_super)(struct mddev *mddev,
942 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100943 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000944 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000945 int (*allow_new_offset)(struct md_rdev *rdev,
946 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947};
948
949/*
Andre Noll0894cc32009-06-18 08:49:23 +1000950 * Check that the given mddev has no bitmap.
951 *
952 * This function is called from the run method of all personalities that do not
953 * support bitmaps. It prints an error message and returns non-zero if mddev
954 * has a bitmap. Otherwise, it returns 0.
955 *
956 */
NeilBrownfd01b882011-10-11 16:47:53 +1100957int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000958{
NeilBrownc3d97142009-12-14 12:49:52 +1100959 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000960 return 0;
961 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
962 mdname(mddev), mddev->pers->name);
963 return 1;
964}
965EXPORT_SYMBOL(md_check_no_bitmap);
966
967/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * load_super for 0.90.0
969 */
NeilBrown3cb03002011-10-11 16:45:26 +1100970static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
973 mdp_super_t *sb;
974 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /*
Andre Noll0f420352008-07-11 22:02:23 +1000977 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * it's at the end of the disk.
979 *
980 * It also happens to be a multiple of 4Kb.
981 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100982 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrown0002b272005-09-09 16:23:53 -0700984 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ret) return ret;
986
987 ret = -EINVAL;
988
989 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000990 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (sb->md_magic != MD_SB_MAGIC) {
993 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
994 b);
995 goto abort;
996 }
997
998 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800999 sb->minor_version < 90 ||
1000 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1002 sb->major_version, sb->minor_version,
1003 b);
1004 goto abort;
1005 }
1006
1007 if (sb->raid_disks <= 0)
1008 goto abort;
1009
NeilBrown4d167f02007-05-09 02:35:37 -07001010 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1012 b);
1013 goto abort;
1014 }
1015
1016 rdev->preferred_minor = sb->md_minor;
1017 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001018 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001019 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001020 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (sb->level == LEVEL_MULTIPATH)
1023 rdev->desc_nr = -1;
1024 else
1025 rdev->desc_nr = sb->this_disk.number;
1026
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001027 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001031 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!uuid_equal(refsb, sb)) {
1033 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1034 b, bdevname(refdev->bdev,b2));
1035 goto abort;
1036 }
1037 if (!sb_equal(refsb, sb)) {
1038 printk(KERN_WARNING "md: %s has same UUID"
1039 " but different superblock to %s\n",
1040 b, bdevname(refdev->bdev, b2));
1041 goto abort;
1042 }
1043 ev1 = md_event(sb);
1044 ev2 = md_event(refsb);
1045 if (ev1 > ev2)
1046 ret = 1;
1047 else
1048 ret = 0;
1049 }
NeilBrown8190e752009-06-18 08:48:58 +10001050 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001051 /* Limit to 4TB as metadata cannot record more than that.
1052 * (not needed for Linear and RAID0 as metadata doesn't
1053 * record this size)
1054 */
1055 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001056 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
NeilBrown27a7b262011-09-10 17:21:28 +10001058 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001059 /* "this cannot possibly happen" ... */
1060 ret = -EINVAL;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 abort:
1063 return ret;
1064}
1065
1066/*
1067 * validate_super for 0.90.0
1068 */
NeilBrownfd01b882011-10-11 16:47:53 +11001069static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001072 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001073 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
NeilBrown41158c72005-06-21 17:17:25 -07001075 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001076 clear_bit(Faulty, &rdev->flags);
1077 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001078 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001079 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (mddev->raid_disks == 0) {
1082 mddev->major_version = 0;
1083 mddev->minor_version = sb->minor_version;
1084 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001085 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001086 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mddev->ctime = sb->ctime;
1088 mddev->utime = sb->utime;
1089 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001090 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 mddev->layout = sb->layout;
1092 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001093 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001094 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001095 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001096 mddev->bitmap_info.space = 0;
1097 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001098 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001099 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001100 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
NeilBrownf6705572006-03-27 01:18:11 -08001102 if (mddev->minor_version >= 91) {
1103 mddev->reshape_position = sb->reshape_position;
1104 mddev->delta_disks = sb->delta_disks;
1105 mddev->new_level = sb->new_level;
1106 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001107 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001108 if (mddev->delta_disks < 0)
1109 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001110 } else {
1111 mddev->reshape_position = MaxSector;
1112 mddev->delta_disks = 0;
1113 mddev->new_level = mddev->level;
1114 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001115 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (sb->state & (1<<MD_SB_CLEAN))
1119 mddev->recovery_cp = MaxSector;
1120 else {
1121 if (sb->events_hi == sb->cp_events_hi &&
1122 sb->events_lo == sb->cp_events_lo) {
1123 mddev->recovery_cp = sb->recovery_cp;
1124 } else
1125 mddev->recovery_cp = 0;
1126 }
1127
1128 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1129 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1130 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1131 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1132
1133 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001134
1135 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001136 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001137 mddev->bitmap_info.offset =
1138 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001139 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001140 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001141 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001142
NeilBrown41158c72005-06-21 17:17:25 -07001143 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001144 /* Insist on good event counter while assembling, except
1145 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001147 if (sb->disks[rdev->desc_nr].state & (
1148 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1149 if (ev1 < mddev->events)
1150 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001151 } else if (mddev->bitmap) {
1152 /* if adding to array with a bitmap, then we can accept an
1153 * older device ... but not too old.
1154 */
NeilBrown41158c72005-06-21 17:17:25 -07001155 if (ev1 < mddev->bitmap->events_cleared)
1156 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001157 if (ev1 < mddev->events)
1158 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001159 } else {
1160 if (ev1 < mddev->events)
1161 /* just a hot-add of a new device, leave raid_disk at -1 */
1162 return 0;
1163 }
NeilBrown41158c72005-06-21 17:17:25 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 desc = sb->disks + rdev->desc_nr;
1167
1168 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001170 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1171 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001174 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001175 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1176 /* active but not in sync implies recovery up to
1177 * reshape position. We don't know exactly where
1178 * that is, so set to zero for now */
1179 if (mddev->minor_version >= 91) {
1180 rdev->recovery_offset = 0;
1181 rdev->raid_disk = desc->raid_disk;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001184 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1185 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001186 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189}
1190
1191/*
1192 * sync_super for 0.90.0
1193 */
NeilBrownfd01b882011-10-11 16:47:53 +11001194static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001197 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* make rdev->sb match mddev data..
1202 *
1203 * 1/ zero out disks
1204 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1205 * 3/ any empty disks < next_spare become removed
1206 *
1207 * disks[0] gets initialised to REMOVED because
1208 * we cannot be sure from other fields if it has
1209 * been initialised or not.
1210 */
1211 int i;
1212 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1213
NeilBrown61181562005-09-09 16:24:02 -07001214 rdev->sb_size = MD_SB_BYTES;
1215
Namhyung Kim65a06f062011-07-27 11:00:36 +10001216 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 memset(sb, 0, sizeof(*sb));
1219
1220 sb->md_magic = MD_SB_MAGIC;
1221 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sb->patch_version = mddev->patch_version;
1223 sb->gvalid_words = 0; /* ignored */
1224 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1225 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1226 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1227 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1228
1229 sb->ctime = mddev->ctime;
1230 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001231 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sb->raid_disks = mddev->raid_disks;
1233 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001234 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 sb->utime = mddev->utime;
1236 sb->state = 0;
1237 sb->events_hi = (mddev->events>>32);
1238 sb->events_lo = (u32)mddev->events;
1239
NeilBrownf6705572006-03-27 01:18:11 -08001240 if (mddev->reshape_position == MaxSector)
1241 sb->minor_version = 90;
1242 else {
1243 sb->minor_version = 91;
1244 sb->reshape_position = mddev->reshape_position;
1245 sb->new_level = mddev->new_level;
1246 sb->delta_disks = mddev->delta_disks;
1247 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001248 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001249 }
1250 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (mddev->in_sync)
1252 {
1253 sb->recovery_cp = mddev->recovery_cp;
1254 sb->cp_events_hi = (mddev->events>>32);
1255 sb->cp_events_lo = (u32)mddev->events;
1256 if (mddev->recovery_cp == MaxSector)
1257 sb->state = (1<< MD_SB_CLEAN);
1258 } else
1259 sb->recovery_cp = 0;
1260
1261 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001262 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
NeilBrownc3d97142009-12-14 12:49:52 +11001264 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001265 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001268 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001270 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001271 int is_active = test_bit(In_sync, &rdev2->flags);
1272
1273 if (rdev2->raid_disk >= 0 &&
1274 sb->minor_version >= 91)
1275 /* we have nowhere to store the recovery_offset,
1276 * but if it is not below the reshape_position,
1277 * we can piggy-back on that.
1278 */
1279 is_active = 1;
1280 if (rdev2->raid_disk < 0 ||
1281 test_bit(Faulty, &rdev2->flags))
1282 is_active = 0;
1283 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001284 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001286 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001287 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d = &sb->disks[rdev2->desc_nr];
1289 nr_disks++;
1290 d->number = rdev2->desc_nr;
1291 d->major = MAJOR(rdev2->bdev->bd_dev);
1292 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001293 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 d->raid_disk = rdev2->raid_disk;
1295 else
1296 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001297 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001299 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001301 if (test_bit(In_sync, &rdev2->flags))
1302 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 active++;
1304 working++;
1305 } else {
1306 d->state = 0;
1307 spare++;
1308 working++;
1309 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001310 if (test_bit(WriteMostly, &rdev2->flags))
1311 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* now set the "removed" and "faulty" bits on any missing devices */
1314 for (i=0 ; i < mddev->raid_disks ; i++) {
1315 mdp_disk_t *d = &sb->disks[i];
1316 if (d->state == 0 && d->number == 0) {
1317 d->number = i;
1318 d->raid_disk = i;
1319 d->state = (1<<MD_DISK_REMOVED);
1320 d->state |= (1<<MD_DISK_FAULTY);
1321 failed++;
1322 }
1323 }
1324 sb->nr_disks = nr_disks;
1325 sb->active_disks = active;
1326 sb->working_disks = working;
1327 sb->failed_disks = failed;
1328 sb->spare_disks = spare;
1329
1330 sb->this_disk = sb->disks[rdev->desc_nr];
1331 sb->sb_csum = calc_sb_csum(sb);
1332}
1333
1334/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335 * rdev_size_change for 0.90.0
1336 */
1337static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001338super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339{
Andre Noll58c0fed2009-03-31 14:33:13 +11001340 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001342 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001343 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001344 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001345 if (!num_sectors || num_sectors > rdev->sb_start)
1346 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001347 /* Limit to 4TB as metadata cannot record more than that.
1348 * 4TB == 2^32 KB, or 2*2^32 sectors.
1349 */
NeilBrown667a5312012-08-16 16:46:12 +10001350 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001351 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001352 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001353 rdev->sb_page);
1354 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001355 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001356}
1357
NeilBrownc6563a82012-05-21 09:27:00 +10001358static int
1359super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1360{
1361 /* non-zero offset changes not possible with v0.90 */
1362 return new_offset == 0;
1363}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001364
1365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * version 1 superblock
1367 */
1368
NeilBrown1c05b4b2006-10-21 10:24:08 -07001369static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001371 __le32 disk_csum;
1372 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 unsigned long long newcsum;
1374 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001375 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 disk_csum = sb->sb_csum;
1378 sb->sb_csum = 0;
1379 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001380 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 newcsum += le32_to_cpu(*isuper++);
1382
1383 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001384 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1387 sb->sb_csum = disk_csum;
1388 return cpu_to_le32(csum);
1389}
1390
NeilBrown2699b672011-07-28 11:31:47 +10001391static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1392 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001393static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct mdp_superblock_1 *sb;
1396 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001397 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001398 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001400 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /*
Andre Noll0f420352008-07-11 22:02:23 +10001403 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * It is always aligned to a 4K boundary and
1405 * depeding on minor_version, it can be:
1406 * 0: At least 8K, but less than 12K, from end of device
1407 * 1: At start of device
1408 * 2: 4K from start of device.
1409 */
1410 switch(minor_version) {
1411 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001412 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001413 sb_start -= 8*2;
1414 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001417 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001420 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
1422 default:
1423 return -EINVAL;
1424 }
Andre Noll0f420352008-07-11 22:02:23 +10001425 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
NeilBrown0002b272005-09-09 16:23:53 -07001427 /* superblock is rarely larger than 1K, but it can be larger,
1428 * and it is safe to read 4k, so we do that
1429 */
1430 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (ret) return ret;
1432
1433
Namhyung Kim65a06f062011-07-27 11:00:36 +10001434 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1437 sb->major_version != cpu_to_le32(1) ||
1438 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001439 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001440 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EINVAL;
1442
1443 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1444 printk("md: invalid superblock checksum on %s\n",
1445 bdevname(rdev->bdev,b));
1446 return -EINVAL;
1447 }
1448 if (le64_to_cpu(sb->data_size) < 10) {
1449 printk("md: data_size too small on %s\n",
1450 bdevname(rdev->bdev,b));
1451 return -EINVAL;
1452 }
NeilBrownc6563a82012-05-21 09:27:00 +10001453 if (sb->pad0 ||
1454 sb->pad3[0] ||
1455 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1456 /* Some padding is non-zero, might be a new feature */
1457 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 rdev->preferred_minor = 0xffff;
1460 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001461 rdev->new_data_offset = rdev->data_offset;
1462 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1463 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1464 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001465 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
NeilBrown0002b272005-09-09 16:23:53 -07001467 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001468 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001469 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001470 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1471
1472 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001473 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001474 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001475 if (minor_version
1476 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1477 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001478
NeilBrown31b65a02006-07-10 04:44:14 -07001479 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1480 rdev->desc_nr = -1;
1481 else
1482 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1483
NeilBrown2699b672011-07-28 11:31:47 +10001484 if (!rdev->bb_page) {
1485 rdev->bb_page = alloc_page(GFP_KERNEL);
1486 if (!rdev->bb_page)
1487 return -ENOMEM;
1488 }
1489 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1490 rdev->badblocks.count == 0) {
1491 /* need to load the bad block list.
1492 * Currently we limit it to one page.
1493 */
1494 s32 offset;
1495 sector_t bb_sector;
1496 u64 *bbp;
1497 int i;
1498 int sectors = le16_to_cpu(sb->bblog_size);
1499 if (sectors > (PAGE_SIZE / 512))
1500 return -EINVAL;
1501 offset = le32_to_cpu(sb->bblog_offset);
1502 if (offset == 0)
1503 return -EINVAL;
1504 bb_sector = (long long)offset;
1505 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1506 rdev->bb_page, READ, true))
1507 return -EIO;
1508 bbp = (u64 *)page_address(rdev->bb_page);
1509 rdev->badblocks.shift = sb->bblog_shift;
1510 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1511 u64 bb = le64_to_cpu(*bbp);
1512 int count = bb & (0x3ff);
1513 u64 sector = bb >> 10;
1514 sector <<= sb->bblog_shift;
1515 count <<= sb->bblog_shift;
1516 if (bb + 1 == 0)
1517 break;
1518 if (md_set_badblocks(&rdev->badblocks,
1519 sector, count, 1) == 0)
1520 return -EINVAL;
1521 }
NeilBrown486adf72013-04-24 11:42:44 +10001522 } else if (sb->bblog_offset != 0)
1523 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001524
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001525 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001526 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001529 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1532 sb->level != refsb->level ||
1533 sb->layout != refsb->layout ||
1534 sb->chunksize != refsb->chunksize) {
1535 printk(KERN_WARNING "md: %s has strangely different"
1536 " superblock to %s\n",
1537 bdevname(rdev->bdev,b),
1538 bdevname(refdev->bdev,b2));
1539 return -EINVAL;
1540 }
1541 ev1 = le64_to_cpu(sb->events);
1542 ev2 = le64_to_cpu(refsb->events);
1543
1544 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001545 ret = 1;
1546 else
1547 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
NeilBrownc6563a82012-05-21 09:27:00 +10001549 if (minor_version) {
1550 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1551 sectors -= rdev->data_offset;
1552 } else
1553 sectors = rdev->sb_start;
1554 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001556 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
NeilBrownfd01b882011-10-11 16:47:53 +11001560static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001562 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001563 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
NeilBrown41158c72005-06-21 17:17:25 -07001565 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001566 clear_bit(Faulty, &rdev->flags);
1567 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001568 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001569 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (mddev->raid_disks == 0) {
1572 mddev->major_version = 1;
1573 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001574 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001575 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1577 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1578 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001579 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->layout = le32_to_cpu(sb->layout);
1581 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001582 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001583 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001584 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001585 mddev->bitmap_info.space = 0;
1586 /* Default location for bitmap is 1K after superblock
1587 * using 3K - total of 4K
1588 */
NeilBrownc3d97142009-12-14 12:49:52 +11001589 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001590 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001591 mddev->reshape_backwards = 0;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1594 memcpy(mddev->uuid, sb->set_uuid, 16);
1595
1596 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001597
NeilBrown71c08052005-09-09 16:23:51 -07001598 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001599 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001600 mddev->bitmap_info.offset =
1601 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001602 /* Metadata doesn't record how much space is available.
1603 * For 1.0, we assume we can use up to the superblock
1604 * if before, else to 4K beyond superblock.
1605 * For others, assume no change is possible.
1606 */
1607 if (mddev->minor_version > 0)
1608 mddev->bitmap_info.space = 0;
1609 else if (mddev->bitmap_info.offset > 0)
1610 mddev->bitmap_info.space =
1611 8 - mddev->bitmap_info.offset;
1612 else
1613 mddev->bitmap_info.space =
1614 -mddev->bitmap_info.offset;
1615 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001616
NeilBrownf6705572006-03-27 01:18:11 -08001617 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1618 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1619 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1620 mddev->new_level = le32_to_cpu(sb->new_level);
1621 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001622 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001623 if (mddev->delta_disks < 0 ||
1624 (mddev->delta_disks == 0 &&
1625 (le32_to_cpu(sb->feature_map)
1626 & MD_FEATURE_RESHAPE_BACKWARDS)))
1627 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001628 } else {
1629 mddev->reshape_position = MaxSector;
1630 mddev->delta_disks = 0;
1631 mddev->new_level = mddev->level;
1632 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001633 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001634 }
1635
NeilBrown41158c72005-06-21 17:17:25 -07001636 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001637 /* Insist of good event counter while assembling, except for
1638 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001640 if (rdev->desc_nr >= 0 &&
1641 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1642 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1643 if (ev1 < mddev->events)
1644 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001645 } else if (mddev->bitmap) {
1646 /* If adding to array with a bitmap, then we can accept an
1647 * older device, but not too old.
1648 */
NeilBrown41158c72005-06-21 17:17:25 -07001649 if (ev1 < mddev->bitmap->events_cleared)
1650 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001651 if (ev1 < mddev->events)
1652 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001653 } else {
1654 if (ev1 < mddev->events)
1655 /* just a hot-add of a new device, leave raid_disk at -1 */
1656 return 0;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (mddev->level != LEVEL_MULTIPATH) {
1659 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001660 if (rdev->desc_nr < 0 ||
1661 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1662 role = 0xffff;
1663 rdev->desc_nr = -1;
1664 } else
1665 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 switch(role) {
1667 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001670 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672 default:
NeilBrownf4667222013-12-09 12:04:56 +11001673 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001674 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001675 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001676 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001677 if (!(le32_to_cpu(sb->feature_map) &
1678 MD_FEATURE_RECOVERY_BITMAP))
1679 rdev->saved_raid_disk = -1;
1680 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001681 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 rdev->raid_disk = role;
1683 break;
1684 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001685 if (sb->devflags & WriteMostly1)
1686 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001687 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1688 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001689 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001690 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 0;
1693}
1694
NeilBrownfd01b882011-10-11 16:47:53 +11001695static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001698 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int max_dev, i;
1700 /* make rdev->sb match mddev and rdev data. */
1701
Namhyung Kim65a06f062011-07-27 11:00:36 +10001702 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 sb->feature_map = 0;
1705 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001706 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 memset(sb->pad3, 0, sizeof(sb->pad3));
1708
1709 sb->utime = cpu_to_le64((__u64)mddev->utime);
1710 sb->events = cpu_to_le64(mddev->events);
1711 if (mddev->in_sync)
1712 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1713 else
1714 sb->resync_offset = cpu_to_le64(0);
1715
NeilBrown1c05b4b2006-10-21 10:24:08 -07001716 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001717
NeilBrownf0ca3402006-02-02 14:28:04 -08001718 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001719 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001720 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001721 sb->level = cpu_to_le32(mddev->level);
1722 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001723
NeilBrownaeb9b212011-08-25 14:43:08 +10001724 if (test_bit(WriteMostly, &rdev->flags))
1725 sb->devflags |= WriteMostly1;
1726 else
1727 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001728 sb->data_offset = cpu_to_le64(rdev->data_offset);
1729 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001730
NeilBrownc3d97142009-12-14 12:49:52 +11001731 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1732 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001733 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001734 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001735
1736 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001737 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001738 sb->feature_map |=
1739 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1740 sb->recovery_offset =
1741 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001742 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1743 sb->feature_map |=
1744 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001745 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001746 if (test_bit(Replacement, &rdev->flags))
1747 sb->feature_map |=
1748 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001749
NeilBrownf6705572006-03-27 01:18:11 -08001750 if (mddev->reshape_position != MaxSector) {
1751 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1752 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1753 sb->new_layout = cpu_to_le32(mddev->new_layout);
1754 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1755 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001756 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001757 if (mddev->delta_disks == 0 &&
1758 mddev->reshape_backwards)
1759 sb->feature_map
1760 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001761 if (rdev->new_data_offset != rdev->data_offset) {
1762 sb->feature_map
1763 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1764 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1765 - rdev->data_offset));
1766 }
NeilBrownf6705572006-03-27 01:18:11 -08001767 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001768
NeilBrown2699b672011-07-28 11:31:47 +10001769 if (rdev->badblocks.count == 0)
1770 /* Nothing to do for bad blocks*/ ;
1771 else if (sb->bblog_offset == 0)
1772 /* Cannot record bad blocks on this device */
1773 md_error(mddev, rdev);
1774 else {
1775 struct badblocks *bb = &rdev->badblocks;
1776 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1777 u64 *p = bb->page;
1778 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1779 if (bb->changed) {
1780 unsigned seq;
1781
1782retry:
1783 seq = read_seqbegin(&bb->lock);
1784
1785 memset(bbp, 0xff, PAGE_SIZE);
1786
1787 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001788 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001789 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1790 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001791 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001792 }
NeilBrownd0962932012-03-19 12:46:41 +11001793 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001794 if (read_seqretry(&bb->lock, seq))
1795 goto retry;
1796
1797 bb->sector = (rdev->sb_start +
1798 (int)le32_to_cpu(sb->bblog_offset));
1799 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001800 }
1801 }
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001804 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (rdev2->desc_nr+1 > max_dev)
1806 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001807
NeilBrown70471da2009-08-03 10:59:57 +10001808 if (max_dev > le32_to_cpu(sb->max_dev)) {
1809 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001810 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001811 rdev->sb_size = max_dev * 2 + 256;
1812 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1813 if (rdev->sb_size & bmask)
1814 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001815 } else
1816 max_dev = le32_to_cpu(sb->max_dev);
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 for (i=0; i<max_dev;i++)
1819 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1820
NeilBrowndafb20f2012-03-19 12:46:39 +11001821 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001825 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001827 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001828 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 else
1830 sb->dev_roles[i] = cpu_to_le16(0xffff);
1831 }
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 sb->sb_csum = calc_sb_1_csum(sb);
1834}
1835
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001837super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001838{
1839 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001840 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001841 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001842 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001843 if (rdev->data_offset != rdev->new_data_offset)
1844 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001845 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001846 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001847 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001848 max_sectors -= rdev->data_offset;
1849 if (!num_sectors || num_sectors > max_sectors)
1850 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001851 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852 /* minor version 0 with bitmap we can't move */
1853 return 0;
1854 } else {
1855 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001856 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001857 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001858 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001859 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 if (!num_sectors || num_sectors > max_sectors)
1861 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001862 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001863 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001864 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001865 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001866 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001868 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001869 rdev->sb_page);
1870 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001871 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001872
1873}
1874
1875static int
1876super_1_allow_new_offset(struct md_rdev *rdev,
1877 unsigned long long new_offset)
1878{
1879 /* All necessary checks on new >= old have been done */
1880 struct bitmap *bitmap;
1881 if (new_offset >= rdev->data_offset)
1882 return 1;
1883
1884 /* with 1.0 metadata, there is no metadata to tread on
1885 * so we can always move back */
1886 if (rdev->mddev->minor_version == 0)
1887 return 1;
1888
1889 /* otherwise we must be sure not to step on
1890 * any metadata, so stay:
1891 * 36K beyond start of superblock
1892 * beyond end of badblocks
1893 * beyond write-intent bitmap
1894 */
1895 if (rdev->sb_start + (32+4)*2 > new_offset)
1896 return 0;
1897 bitmap = rdev->mddev->bitmap;
1898 if (bitmap && !rdev->mddev->bitmap_info.file &&
1899 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001900 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001901 return 0;
1902 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1903 return 0;
1904
1905 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Adrian Bunk75c96f82005-05-05 16:16:09 -07001908static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 [0] = {
1910 .name = "0.90.0",
1911 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001912 .load_super = super_90_load,
1913 .validate_super = super_90_validate,
1914 .sync_super = super_90_sync,
1915 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001916 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 },
1918 [1] = {
1919 .name = "md-1",
1920 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001921 .load_super = super_1_load,
1922 .validate_super = super_1_validate,
1923 .sync_super = super_1_sync,
1924 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001925 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 },
1927};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
NeilBrownfd01b882011-10-11 16:47:53 +11001929static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001930{
1931 if (mddev->sync_super) {
1932 mddev->sync_super(mddev, rdev);
1933 return;
1934 }
1935
1936 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1937
1938 super_types[mddev->major_version].sync_super(mddev, rdev);
1939}
1940
NeilBrownfd01b882011-10-11 16:47:53 +11001941static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
NeilBrown3cb03002011-10-11 16:45:26 +11001943 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
NeilBrown4b809912008-07-21 17:05:25 +10001945 rcu_read_lock();
1946 rdev_for_each_rcu(rdev, mddev1)
1947 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001948 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001949 rdev2->bdev->bd_contains) {
1950 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001951 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001952 }
1953 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 0;
1955}
1956
1957static LIST_HEAD(pending_raid_disks);
1958
Andre Nollac5e7112009-08-03 10:59:47 +10001959/*
1960 * Try to register data integrity profile for an mddev
1961 *
1962 * This is called when an array is started and after a disk has been kicked
1963 * from the array. It only succeeds if all working and active component devices
1964 * are integrity capable with matching profiles.
1965 */
NeilBrownfd01b882011-10-11 16:47:53 +11001966int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001967{
NeilBrown3cb03002011-10-11 16:45:26 +11001968 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001969
Andre Nollac5e7112009-08-03 10:59:47 +10001970 if (list_empty(&mddev->disks))
1971 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001972 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1973 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001974 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001975 /* skip spares and non-functional disks */
1976 if (test_bit(Faulty, &rdev->flags))
1977 continue;
1978 if (rdev->raid_disk < 0)
1979 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001980 if (!reference) {
1981 /* Use the first rdev as the reference */
1982 reference = rdev;
1983 continue;
1984 }
1985 /* does this rdev's profile match the reference profile? */
1986 if (blk_integrity_compare(reference->bdev->bd_disk,
1987 rdev->bdev->bd_disk) < 0)
1988 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001989 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001990 if (!reference || !bdev_get_integrity(reference->bdev))
1991 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001992 /*
1993 * All component devices are integrity capable and have matching
1994 * profiles, register the common profile for the md device.
1995 */
1996 if (blk_integrity_register(mddev->gendisk,
1997 bdev_get_integrity(reference->bdev)) != 0) {
1998 printk(KERN_ERR "md: failed to register integrity for %s\n",
1999 mdname(mddev));
2000 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002001 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002002 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2003 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2004 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2005 mdname(mddev));
2006 return -EINVAL;
2007 }
Andre Nollac5e7112009-08-03 10:59:47 +10002008 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002009}
Andre Nollac5e7112009-08-03 10:59:47 +10002010EXPORT_SYMBOL(md_integrity_register);
2011
2012/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002013void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002014{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002015 struct blk_integrity *bi_rdev;
2016 struct blk_integrity *bi_mddev;
2017
2018 if (!mddev->gendisk)
2019 return;
2020
2021 bi_rdev = bdev_get_integrity(rdev->bdev);
2022 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002023
2024 if (!bi_mddev) /* nothing to do */
2025 return;
2026 if (rdev->raid_disk < 0) /* skip spares */
2027 return;
2028 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2029 rdev->bdev->bd_disk) >= 0)
2030 return;
2031 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2032 blk_integrity_unregister(mddev->gendisk);
2033}
2034EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002035
NeilBrownfd01b882011-10-11 16:47:53 +11002036static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002038 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002039 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002040 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002041 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (rdev->mddev) {
2044 MD_BUG();
2045 return -EINVAL;
2046 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002047
2048 /* prevent duplicates */
2049 if (find_rdev(mddev, rdev->bdev->bd_dev))
2050 return -EEXIST;
2051
Andre Nolldd8ac332009-03-31 14:33:13 +11002052 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2053 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2054 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002055 if (mddev->pers) {
2056 /* Cannot change size, so fail
2057 * If mddev->level <= 0, then we don't care
2058 * about aligning sizes (e.g. linear)
2059 */
2060 if (mddev->level > 0)
2061 return -ENOSPC;
2062 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002063 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 /* Verify rdev->desc_nr is unique.
2067 * If it is -1, assign a free number, else
2068 * check number is not in use
2069 */
2070 if (rdev->desc_nr < 0) {
2071 int choice = 0;
2072 if (mddev->pers) choice = mddev->raid_disks;
2073 while (find_rdev_nr(mddev, choice))
2074 choice++;
2075 rdev->desc_nr = choice;
2076 } else {
2077 if (find_rdev_nr(mddev, rdev->desc_nr))
2078 return -EBUSY;
2079 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002080 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2081 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2082 mdname(mddev), mddev->max_disks);
2083 return -EBUSY;
2084 }
NeilBrown19133a42005-11-08 21:39:35 -08002085 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002086 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002087 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002090 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002091
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002092 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002093 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002094
Tejun Heo0762b8b2008-08-25 19:56:12 +09002095 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002096 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2097 /* failure here is OK */;
2098 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002099
NeilBrown4b809912008-07-21 17:05:25 +10002100 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002101 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002102
2103 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002104 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002107
2108 fail:
2109 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2110 b, mdname(mddev));
2111 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
NeilBrown177a99b2008-02-06 01:39:56 -08002114static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002115{
NeilBrown3cb03002011-10-11 16:45:26 +11002116 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002117 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002118 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002119}
2120
NeilBrown3cb03002011-10-11 16:45:26 +11002121static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 char b[BDEVNAME_SIZE];
2124 if (!rdev->mddev) {
2125 MD_BUG();
2126 return;
2127 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002128 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002129 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2131 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002132 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002133 sysfs_put(rdev->sysfs_state);
2134 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002135 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002136 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002137 * writing to 'remove' to "dev/state". We also need
2138 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002139 */
NeilBrown4b809912008-07-21 17:05:25 +10002140 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002141 INIT_WORK(&rdev->del_work, md_delayed_delete);
2142 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002143 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/*
2147 * prevent the device from being mounted, repartitioned or
2148 * otherwise reused by a RAID array (or any other kernel
2149 * subsystem), by bd_claiming the device.
2150 */
NeilBrown3cb03002011-10-11 16:45:26 +11002151static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 int err = 0;
2154 struct block_device *bdev;
2155 char b[BDEVNAME_SIZE];
2156
Tejun Heod4d77622010-11-13 11:55:18 +01002157 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002158 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (IS_ERR(bdev)) {
2160 printk(KERN_ERR "md: could not open %s.\n",
2161 __bdevname(dev, b));
2162 return PTR_ERR(bdev);
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 rdev->bdev = bdev;
2165 return err;
2166}
2167
NeilBrown3cb03002011-10-11 16:45:26 +11002168static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct block_device *bdev = rdev->bdev;
2171 rdev->bdev = NULL;
2172 if (!bdev)
2173 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002174 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177void md_autodetect_dev(dev_t dev);
2178
NeilBrown3cb03002011-10-11 16:45:26 +11002179static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 char b[BDEVNAME_SIZE];
2182 printk(KERN_INFO "md: export_rdev(%s)\n",
2183 bdevname(rdev->bdev,b));
2184 if (rdev->mddev)
2185 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002186 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002188 if (test_bit(AutoDetected, &rdev->flags))
2189 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#endif
2191 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002192 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
NeilBrown3cb03002011-10-11 16:45:26 +11002195static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 unbind_rdev_from_array(rdev);
2198 export_rdev(rdev);
2199}
2200
NeilBrownfd01b882011-10-11 16:47:53 +11002201static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
NeilBrown3cb03002011-10-11 16:45:26 +11002203 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
NeilBrowndafb20f2012-03-19 12:46:39 +11002205 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (!rdev->mddev) {
2207 MD_BUG();
2208 continue;
2209 }
2210 kick_rdev_from_array(rdev);
2211 }
2212 if (!list_empty(&mddev->disks))
2213 MD_BUG();
2214 mddev->raid_disks = 0;
2215 mddev->major_version = 0;
2216}
2217
2218static void print_desc(mdp_disk_t *desc)
2219{
2220 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2221 desc->major,desc->minor,desc->raid_disk,desc->state);
2222}
2223
Cheng Renquancd2ac932009-01-09 08:31:08 +11002224static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 int i;
2227
2228 printk(KERN_INFO
2229 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2230 sb->major_version, sb->minor_version, sb->patch_version,
2231 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2232 sb->ctime);
2233 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2234 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2235 sb->md_minor, sb->layout, sb->chunk_size);
2236 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2237 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2238 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2239 sb->failed_disks, sb->spare_disks,
2240 sb->sb_csum, (unsigned long)sb->events_lo);
2241
2242 printk(KERN_INFO);
2243 for (i = 0; i < MD_SB_DISKS; i++) {
2244 mdp_disk_t *desc;
2245
2246 desc = sb->disks + i;
2247 if (desc->number || desc->major || desc->minor ||
2248 desc->raid_disk || (desc->state && (desc->state != 4))) {
2249 printk(" D %2d: ", i);
2250 print_desc(desc);
2251 }
2252 }
2253 printk(KERN_INFO "md: THIS: ");
2254 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Cheng Renquancd2ac932009-01-09 08:31:08 +11002257static void print_sb_1(struct mdp_superblock_1 *sb)
2258{
2259 __u8 *uuid;
2260
2261 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002262 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002263 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002264 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002265 le32_to_cpu(sb->major_version),
2266 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002267 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002268 sb->set_name,
2269 (unsigned long long)le64_to_cpu(sb->ctime)
2270 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2271
2272 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002273 printk(KERN_INFO
2274 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002275 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002276 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002277 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2278 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002279 le32_to_cpu(sb->level),
2280 (unsigned long long)le64_to_cpu(sb->size),
2281 le32_to_cpu(sb->raid_disks),
2282 le32_to_cpu(sb->layout),
2283 le32_to_cpu(sb->chunksize),
2284 (unsigned long long)le64_to_cpu(sb->data_offset),
2285 (unsigned long long)le64_to_cpu(sb->data_size),
2286 (unsigned long long)le64_to_cpu(sb->super_offset),
2287 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2288 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002289 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002290 sb->devflags,
2291 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2292 (unsigned long long)le64_to_cpu(sb->events),
2293 (unsigned long long)le64_to_cpu(sb->resync_offset),
2294 le32_to_cpu(sb->sb_csum),
2295 le32_to_cpu(sb->max_dev)
2296 );
2297}
2298
NeilBrown3cb03002011-10-11 16:45:26 +11002299static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002302 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2303 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002304 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2305 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002307 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2308 switch (major_version) {
2309 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002310 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002311 break;
2312 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002313 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002314 break;
2315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 } else
2317 printk(KERN_INFO "md: no rdev superblock!\n");
2318}
2319
Adrian Bunk5e563412006-06-26 00:27:42 -07002320static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002322 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002323 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002324 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 char b[BDEVNAME_SIZE];
2326
2327 printk("\n");
2328 printk("md: **********************************\n");
2329 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2330 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002331 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
NeilBrown32a76272005-06-21 17:17:14 -07002333 if (mddev->bitmap)
2334 bitmap_print_sb(mddev->bitmap);
2335 else
2336 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002337 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 printk("<%s>", bdevname(rdev->bdev,b));
2339 printk("\n");
2340
NeilBrowndafb20f2012-03-19 12:46:39 +11002341 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002342 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344 printk("md: **********************************\n");
2345 printk("\n");
2346}
2347
2348
NeilBrownfd01b882011-10-11 16:47:53 +11002349static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
NeilBrown42543762006-06-26 00:27:57 -07002351 /* Update each superblock (in-memory image), but
2352 * if we are allowed to, skip spares which already
2353 * have the right event counter, or have one earlier
2354 * (which would mean they aren't being marked as dirty
2355 * with the rest of the array)
2356 */
NeilBrown3cb03002011-10-11 16:45:26 +11002357 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002358 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002359 if (rdev->sb_events == mddev->events ||
2360 (nospares &&
2361 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002362 rdev->sb_events+1 == mddev->events)) {
2363 /* Don't update this superblock */
2364 rdev->sb_loaded = 2;
2365 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002366 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002367 rdev->sb_loaded = 1;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370}
2371
NeilBrownfd01b882011-10-11 16:47:53 +11002372static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
NeilBrown3cb03002011-10-11 16:45:26 +11002374 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002375 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002376 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002377 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
NeilBrownd87f0642013-04-24 11:42:40 +10002379 if (mddev->ro) {
2380 if (force_change)
2381 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2382 return;
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002385 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002386 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002387 if (rdev->raid_disk >= 0 &&
2388 mddev->delta_disks >= 0 &&
2389 !test_bit(In_sync, &rdev->flags) &&
2390 mddev->curr_resync_completed > rdev->recovery_offset)
2391 rdev->recovery_offset = mddev->curr_resync_completed;
2392
2393 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002394 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002395 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002396 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002397 if (!mddev->external) {
NeilBrownd97a41dc2010-10-28 17:30:20 +11002398 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002399 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002400 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002401 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002402 md_ack_all_badblocks(&rdev->badblocks);
2403 md_error(mddev, rdev);
2404 }
2405 clear_bit(Blocked, &rdev->flags);
2406 clear_bit(BlockedBadBlocks, &rdev->flags);
2407 wake_up(&rdev->blocked_wait);
2408 }
2409 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002410 wake_up(&mddev->sb_wait);
2411 return;
2412 }
2413
NeilBrowna9701a32005-11-08 21:39:34 -08002414 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002415
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002416 mddev->utime = get_seconds();
2417
NeilBrown850b2b42006-10-03 01:15:46 -07002418 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2419 force_change = 1;
2420 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2421 /* just a clean<-> dirty transition, possibly leave spares alone,
2422 * though if events isn't the right even/odd, we will have to do
2423 * spares after all
2424 */
2425 nospares = 1;
2426 if (force_change)
2427 nospares = 0;
2428 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002429 /* If the array is degraded, then skipping spares is both
2430 * dangerous and fairly pointless.
2431 * Dangerous because a device that was removed from the array
2432 * might have a event_count that still looks up-to-date,
2433 * so it can be re-added without a resync.
2434 * Pointless because if there are any spares to skip,
2435 * then a recovery will happen and soon that array won't
2436 * be degraded any more and the spare can go back to sleep then.
2437 */
NeilBrown850b2b42006-10-03 01:15:46 -07002438 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002439
NeilBrown06d91a52005-06-21 17:17:12 -07002440 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002441
2442 /* If this is just a dirty<->clean transition, and the array is clean
2443 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002444 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002445 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002446 && mddev->can_decrease_events
2447 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002448 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002449 mddev->can_decrease_events = 0;
2450 } else {
NeilBrown42543762006-06-26 00:27:57 -07002451 /* otherwise we have to go forward and ... */
2452 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002453 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 if (!mddev->events) {
2457 /*
2458 * oops, this 64-bit counter should never wrap.
2459 * Either we are in around ~1 trillion A.C., assuming
2460 * 1 reboot per second, or we have a bug:
2461 */
2462 MD_BUG();
2463 mddev->events --;
2464 }
NeilBrown2699b672011-07-28 11:31:47 +10002465
NeilBrowndafb20f2012-03-19 12:46:39 +11002466 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002467 if (rdev->badblocks.changed)
2468 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002469 if (test_bit(Faulty, &rdev->flags))
2470 set_bit(FaultRecorded, &rdev->flags);
2471 }
NeilBrown2699b672011-07-28 11:31:47 +10002472
NeilBrowne6910632008-02-06 01:39:51 -08002473 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002474 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
NeilBrown36a4e1f2011-10-07 14:23:17 +11002476 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2477 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
NeilBrown4ad13662007-07-17 04:06:13 -07002479 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002480 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002482
NeilBrown42543762006-06-26 00:27:57 -07002483 if (rdev->sb_loaded != 1)
2484 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
NeilBrownf4667222013-12-09 12:04:56 +11002486 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002487 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002488 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002489 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002490 pr_debug("md: (write) %s's sb offset: %llu\n",
2491 bdevname(rdev->bdev, b),
2492 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002493 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002494 if (rdev->badblocks.size) {
2495 md_super_write(mddev, rdev,
2496 rdev->badblocks.sector,
2497 rdev->badblocks.size << 9,
2498 rdev->bb_page);
2499 rdev->badblocks.size = 0;
2500 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002501
NeilBrownf4667222013-12-09 12:04:56 +11002502 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002503 pr_debug("md: %s (skipping faulty)\n",
2504 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002505
NeilBrown7bfa19f2005-06-21 17:17:28 -07002506 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 /* only need to write one superblock... */
2508 break;
2509 }
NeilBrowna9701a32005-11-08 21:39:34 -08002510 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002511 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002512
NeilBrowna9701a32005-11-08 21:39:34 -08002513 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002514 if (mddev->in_sync != sync_req ||
2515 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002516 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002517 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002518 goto repeat;
2519 }
NeilBrown850b2b42006-10-03 01:15:46 -07002520 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002521 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002522 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002523 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2524 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002525
NeilBrowndafb20f2012-03-19 12:46:39 +11002526 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002527 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2528 clear_bit(Blocked, &rdev->flags);
2529
2530 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002531 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002532 clear_bit(BlockedBadBlocks, &rdev->flags);
2533 wake_up(&rdev->blocked_wait);
2534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Andre Noll7f6ce762008-03-23 18:34:54 +01002537/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002538 * We want to accept with case. For this we use cmd_match.
2539 */
2540static int cmd_match(const char *cmd, const char *str)
2541{
2542 /* See if cmd, written into a sysfs file, matches
2543 * str. They must either be the same, or cmd can
2544 * have a trailing newline
2545 */
2546 while (*cmd && *str && *cmd == *str) {
2547 cmd++;
2548 str++;
2549 }
2550 if (*cmd == '\n')
2551 cmd++;
2552 if (*str || *cmd)
2553 return 0;
2554 return 1;
2555}
2556
NeilBrown86e6ffd2005-11-08 21:39:24 -08002557struct rdev_sysfs_entry {
2558 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002559 ssize_t (*show)(struct md_rdev *, char *);
2560 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561};
2562
2563static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002564state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002565{
2566 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002567 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002568
NeilBrownde393cd2011-07-28 11:31:48 +10002569 if (test_bit(Faulty, &rdev->flags) ||
2570 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002571 len+= sprintf(page+len, "%sfaulty",sep);
2572 sep = ",";
2573 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002574 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002575 len += sprintf(page+len, "%sin_sync",sep);
2576 sep = ",";
2577 }
NeilBrownf6556752006-06-26 00:28:01 -07002578 if (test_bit(WriteMostly, &rdev->flags)) {
2579 len += sprintf(page+len, "%swrite_mostly",sep);
2580 sep = ",";
2581 }
NeilBrownde393cd2011-07-28 11:31:48 +10002582 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002583 (rdev->badblocks.unacked_exist
2584 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002585 len += sprintf(page+len, "%sblocked", sep);
2586 sep = ",";
2587 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002588 if (!test_bit(Faulty, &rdev->flags) &&
2589 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002590 len += sprintf(page+len, "%sspare", sep);
2591 sep = ",";
2592 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002593 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2594 len += sprintf(page+len, "%swrite_error", sep);
2595 sep = ",";
2596 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002597 if (test_bit(WantReplacement, &rdev->flags)) {
2598 len += sprintf(page+len, "%swant_replacement", sep);
2599 sep = ",";
2600 }
2601 if (test_bit(Replacement, &rdev->flags)) {
2602 len += sprintf(page+len, "%sreplacement", sep);
2603 sep = ",";
2604 }
2605
NeilBrown86e6ffd2005-11-08 21:39:24 -08002606 return len+sprintf(page+len, "\n");
2607}
2608
NeilBrown45dc2de2006-06-26 00:27:58 -07002609static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002610state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002611{
2612 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002613 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002614 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002615 * writemostly - sets write_mostly
2616 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002617 * blocked - sets the Blocked flags
2618 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002619 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002620 * -insync - clear Insync for a device with a slot assigned,
2621 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002622 * write_error - sets WriteErrorSeen
2623 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002624 */
2625 int err = -EINVAL;
2626 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2627 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002628 if (test_bit(Faulty, &rdev->flags))
2629 err = 0;
2630 else
2631 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002632 } else if (cmd_match(buf, "remove")) {
2633 if (rdev->raid_disk >= 0)
2634 err = -EBUSY;
2635 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002636 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002637 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002638 if (mddev->pers)
2639 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002640 md_new_event(mddev);
2641 err = 0;
2642 }
NeilBrownf6556752006-06-26 00:28:01 -07002643 } else if (cmd_match(buf, "writemostly")) {
2644 set_bit(WriteMostly, &rdev->flags);
2645 err = 0;
2646 } else if (cmd_match(buf, "-writemostly")) {
2647 clear_bit(WriteMostly, &rdev->flags);
2648 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002649 } else if (cmd_match(buf, "blocked")) {
2650 set_bit(Blocked, &rdev->flags);
2651 err = 0;
2652 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002653 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002654 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002655 /* metadata handler doesn't understand badblocks,
2656 * so we need to fail the device
2657 */
2658 md_error(rdev->mddev, rdev);
2659 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002660 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002661 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002662 wake_up(&rdev->blocked_wait);
2663 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2664 md_wakeup_thread(rdev->mddev->thread);
2665
2666 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002667 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2668 set_bit(In_sync, &rdev->flags);
2669 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002670 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2671 clear_bit(In_sync, &rdev->flags);
2672 rdev->saved_raid_disk = rdev->raid_disk;
2673 rdev->raid_disk = -1;
2674 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002675 } else if (cmd_match(buf, "write_error")) {
2676 set_bit(WriteErrorSeen, &rdev->flags);
2677 err = 0;
2678 } else if (cmd_match(buf, "-write_error")) {
2679 clear_bit(WriteErrorSeen, &rdev->flags);
2680 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002681 } else if (cmd_match(buf, "want_replacement")) {
2682 /* Any non-spare device that is not a replacement can
2683 * become want_replacement at any time, but we then need to
2684 * check if recovery is needed.
2685 */
2686 if (rdev->raid_disk >= 0 &&
2687 !test_bit(Replacement, &rdev->flags))
2688 set_bit(WantReplacement, &rdev->flags);
2689 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2690 md_wakeup_thread(rdev->mddev->thread);
2691 err = 0;
2692 } else if (cmd_match(buf, "-want_replacement")) {
2693 /* Clearing 'want_replacement' is always allowed.
2694 * Once replacements starts it is too late though.
2695 */
2696 err = 0;
2697 clear_bit(WantReplacement, &rdev->flags);
2698 } else if (cmd_match(buf, "replacement")) {
2699 /* Can only set a device as a replacement when array has not
2700 * yet been started. Once running, replacement is automatic
2701 * from spares, or by assigning 'slot'.
2702 */
2703 if (rdev->mddev->pers)
2704 err = -EBUSY;
2705 else {
2706 set_bit(Replacement, &rdev->flags);
2707 err = 0;
2708 }
2709 } else if (cmd_match(buf, "-replacement")) {
2710 /* Similarly, can only clear Replacement before start */
2711 if (rdev->mddev->pers)
2712 err = -EBUSY;
2713 else {
2714 clear_bit(Replacement, &rdev->flags);
2715 err = 0;
2716 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002717 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002718 if (!err)
2719 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002720 return err ? err : len;
2721}
NeilBrown80ca3a42006-07-10 04:44:18 -07002722static struct rdev_sysfs_entry rdev_state =
2723__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002724
2725static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002726errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002727{
2728 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2729}
2730
2731static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002732errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002733{
2734 char *e;
2735 unsigned long n = simple_strtoul(buf, &e, 10);
2736 if (*buf && (*e == 0 || *e == '\n')) {
2737 atomic_set(&rdev->corrected_errors, n);
2738 return len;
2739 }
2740 return -EINVAL;
2741}
2742static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002743__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002744
NeilBrown014236d2006-01-06 00:20:55 -08002745static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002746slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002747{
2748 if (rdev->raid_disk < 0)
2749 return sprintf(page, "none\n");
2750 else
2751 return sprintf(page, "%d\n", rdev->raid_disk);
2752}
2753
2754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002756{
2757 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002758 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002759 int slot = simple_strtoul(buf, &e, 10);
2760 if (strncmp(buf, "none", 4)==0)
2761 slot = -1;
2762 else if (e==buf || (*e && *e!= '\n'))
2763 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002764 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002765 /* Setting 'slot' on an active array requires also
2766 * updating the 'rd%d' link, and communicating
2767 * with the personality with ->hot_*_disk.
2768 * For now we only support removing
2769 * failed/spare devices. This normally happens automatically,
2770 * but not when the metadata is externally managed.
2771 */
NeilBrownc303da62008-02-06 01:39:51 -08002772 if (rdev->raid_disk == -1)
2773 return -EEXIST;
2774 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002775 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002776 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002777 clear_bit(Blocked, &rdev->flags);
2778 remove_and_add_spares(rdev->mddev, rdev);
2779 if (rdev->raid_disk >= 0)
2780 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002781 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2782 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002784 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002785 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002786 */
2787
2788 if (rdev->raid_disk != -1)
2789 return -EBUSY;
2790
NeilBrownc6751b22011-02-02 11:57:13 +11002791 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2792 return -EBUSY;
2793
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 if (rdev->mddev->pers->hot_add_disk == NULL)
2795 return -EINVAL;
2796
NeilBrownba1b41b2011-01-14 09:14:34 +11002797 if (slot >= rdev->mddev->raid_disks &&
2798 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2799 return -ENOSPC;
2800
Neil Brown6c2fce22008-06-28 08:31:31 +10002801 rdev->raid_disk = slot;
2802 if (test_bit(In_sync, &rdev->flags))
2803 rdev->saved_raid_disk = slot;
2804 else
2805 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002806 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002807 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002808 err = rdev->mddev->pers->
2809 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002810 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002811 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002812 return err;
Neil Brown52664732008-06-28 08:31:44 +10002813 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002814 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002815 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002816 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002818 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002819 if (slot >= rdev->mddev->raid_disks &&
2820 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002821 return -ENOSPC;
2822 rdev->raid_disk = slot;
2823 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002824 clear_bit(Faulty, &rdev->flags);
2825 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002826 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002827 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002828 }
NeilBrown014236d2006-01-06 00:20:55 -08002829 return len;
2830}
2831
2832
2833static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002834__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002835
NeilBrown93c8cad2006-01-06 00:20:56 -08002836static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002837offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002838{
Andrew Morton6961ece2006-01-06 00:20:59 -08002839 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002840}
2841
2842static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002843offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002844{
NeilBrownc6563a82012-05-21 09:27:00 +10002845 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002846 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002847 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002848 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002849 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002850 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002851 /* Must set offset before size, so overlap checks
2852 * can be sane */
2853 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002854 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002855 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002856 return len;
2857}
2858
2859static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002860__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002861
NeilBrownc6563a82012-05-21 09:27:00 +10002862static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2863{
2864 return sprintf(page, "%llu\n",
2865 (unsigned long long)rdev->new_data_offset);
2866}
2867
2868static ssize_t new_offset_store(struct md_rdev *rdev,
2869 const char *buf, size_t len)
2870{
2871 unsigned long long new_offset;
2872 struct mddev *mddev = rdev->mddev;
2873
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002874 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002875 return -EINVAL;
2876
2877 if (mddev->sync_thread)
2878 return -EBUSY;
2879 if (new_offset == rdev->data_offset)
2880 /* reset is always permitted */
2881 ;
2882 else if (new_offset > rdev->data_offset) {
2883 /* must not push array size beyond rdev_sectors */
2884 if (new_offset - rdev->data_offset
2885 + mddev->dev_sectors > rdev->sectors)
2886 return -E2BIG;
2887 }
2888 /* Metadata worries about other space details. */
2889
2890 /* decreasing the offset is inconsistent with a backwards
2891 * reshape.
2892 */
2893 if (new_offset < rdev->data_offset &&
2894 mddev->reshape_backwards)
2895 return -EINVAL;
2896 /* Increasing offset is inconsistent with forwards
2897 * reshape. reshape_direction should be set to
2898 * 'backwards' first.
2899 */
2900 if (new_offset > rdev->data_offset &&
2901 !mddev->reshape_backwards)
2902 return -EINVAL;
2903
2904 if (mddev->pers && mddev->persistent &&
2905 !super_types[mddev->major_version]
2906 .allow_new_offset(rdev, new_offset))
2907 return -E2BIG;
2908 rdev->new_data_offset = new_offset;
2909 if (new_offset > rdev->data_offset)
2910 mddev->reshape_backwards = 1;
2911 else if (new_offset < rdev->data_offset)
2912 mddev->reshape_backwards = 0;
2913
2914 return len;
2915}
2916static struct rdev_sysfs_entry rdev_new_offset =
2917__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2918
NeilBrown83303b62006-01-06 00:21:06 -08002919static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002920rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002921{
Andre Nolldd8ac332009-03-31 14:33:13 +11002922 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002923}
2924
NeilBrownc5d79ad2008-02-06 01:39:54 -08002925static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2926{
2927 /* check if two start/length pairs overlap */
2928 if (s1+l1 <= s2)
2929 return 0;
2930 if (s2+l2 <= s1)
2931 return 0;
2932 return 1;
2933}
2934
Dan Williamsb522adc2009-03-31 15:00:31 +11002935static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2936{
2937 unsigned long long blocks;
2938 sector_t new;
2939
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002940 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002941 return -EINVAL;
2942
2943 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2944 return -EINVAL; /* sector conversion overflow */
2945
2946 new = blocks * 2;
2947 if (new != blocks * 2)
2948 return -EINVAL; /* unsigned long long to sector_t overflow */
2949
2950 *sectors = new;
2951 return 0;
2952}
2953
NeilBrown83303b62006-01-06 00:21:06 -08002954static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002955rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002956{
NeilBrownfd01b882011-10-11 16:47:53 +11002957 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002958 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002959 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002960
Dan Williamsb522adc2009-03-31 15:00:31 +11002961 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002962 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002963 if (rdev->data_offset != rdev->new_data_offset)
2964 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002965 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002966 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002967 sectors = super_types[my_mddev->major_version].
2968 rdev_size_change(rdev, sectors);
2969 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002972 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002973 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002974 if (!my_mddev->pers->resize)
2975 /* Cannot change size for RAID0 or Linear etc */
2976 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002977 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002979 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002980
Andre Nolldd8ac332009-03-31 14:33:13 +11002981 rdev->sectors = sectors;
2982 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002983 /* need to check that all other rdevs with the same ->bdev
2984 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002985 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 * we have to change it back, we will have the lock again.
2987 */
NeilBrownfd01b882011-10-11 16:47:53 +11002988 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002989 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002990 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002991
NeilBrown27c529b2008-03-04 14:29:33 -08002992 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002993 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002994 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002995
NeilBrown29f097c2013-11-14 17:54:51 +11002996 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002997 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002998 if (rdev->bdev == rdev2->bdev &&
2999 rdev != rdev2 &&
3000 overlaps(rdev->data_offset, rdev->sectors,
3001 rdev2->data_offset,
3002 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003003 overlap = 1;
3004 break;
3005 }
3006 mddev_unlock(mddev);
3007 if (overlap) {
3008 mddev_put(mddev);
3009 break;
3010 }
3011 }
NeilBrown29f097c2013-11-14 17:54:51 +11003012 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 if (overlap) {
3014 /* Someone else could have slipped in a size
3015 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003016 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003017 * safe, and trust userspace not to race with
3018 * itself
3019 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003020 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003021 return -EBUSY;
3022 }
3023 }
NeilBrown83303b62006-01-06 00:21:06 -08003024 return len;
3025}
3026
3027static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003028__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003029
Dan Williams06e3c812009-12-12 21:17:12 -07003030
NeilBrown3cb03002011-10-11 16:45:26 +11003031static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003032{
3033 unsigned long long recovery_start = rdev->recovery_offset;
3034
3035 if (test_bit(In_sync, &rdev->flags) ||
3036 recovery_start == MaxSector)
3037 return sprintf(page, "none\n");
3038
3039 return sprintf(page, "%llu\n", recovery_start);
3040}
3041
NeilBrown3cb03002011-10-11 16:45:26 +11003042static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003043{
3044 unsigned long long recovery_start;
3045
3046 if (cmd_match(buf, "none"))
3047 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003048 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003049 return -EINVAL;
3050
3051 if (rdev->mddev->pers &&
3052 rdev->raid_disk >= 0)
3053 return -EBUSY;
3054
3055 rdev->recovery_offset = recovery_start;
3056 if (recovery_start == MaxSector)
3057 set_bit(In_sync, &rdev->flags);
3058 else
3059 clear_bit(In_sync, &rdev->flags);
3060 return len;
3061}
3062
3063static struct rdev_sysfs_entry rdev_recovery_start =
3064__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3065
NeilBrown16c791a2011-07-28 11:31:47 +10003066
3067static ssize_t
3068badblocks_show(struct badblocks *bb, char *page, int unack);
3069static ssize_t
3070badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3071
NeilBrown3cb03002011-10-11 16:45:26 +11003072static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003073{
3074 return badblocks_show(&rdev->badblocks, page, 0);
3075}
NeilBrown3cb03002011-10-11 16:45:26 +11003076static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003077{
NeilBrownde393cd2011-07-28 11:31:48 +10003078 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3079 /* Maybe that ack was all we needed */
3080 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3081 wake_up(&rdev->blocked_wait);
3082 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003083}
3084static struct rdev_sysfs_entry rdev_bad_blocks =
3085__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3086
3087
NeilBrown3cb03002011-10-11 16:45:26 +11003088static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003089{
3090 return badblocks_show(&rdev->badblocks, page, 1);
3091}
NeilBrown3cb03002011-10-11 16:45:26 +11003092static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003093{
3094 return badblocks_store(&rdev->badblocks, page, len, 1);
3095}
3096static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3097__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3098
NeilBrown86e6ffd2005-11-08 21:39:24 -08003099static struct attribute *rdev_default_attrs[] = {
3100 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003101 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003102 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003103 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003104 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003105 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003106 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003107 &rdev_bad_blocks.attr,
3108 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109 NULL,
3110};
3111static ssize_t
3112rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3113{
3114 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003115 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003116 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003117 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003118
3119 if (!entry->show)
3120 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003121
3122 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3123 if (!rv) {
3124 if (rdev->mddev == NULL)
3125 rv = -EBUSY;
3126 else
3127 rv = entry->show(rdev, page);
3128 mddev_unlock(mddev);
3129 }
3130 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003131}
3132
3133static ssize_t
3134rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3135 const char *page, size_t length)
3136{
3137 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003138 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003139 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003140 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003141
3142 if (!entry->store)
3143 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003144 if (!capable(CAP_SYS_ADMIN))
3145 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003146 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003147 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003148 if (rdev->mddev == NULL)
3149 rv = -EBUSY;
3150 else
3151 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003152 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003153 }
3154 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003155}
3156
3157static void rdev_free(struct kobject *ko)
3158{
NeilBrown3cb03002011-10-11 16:45:26 +11003159 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003160 kfree(rdev);
3161}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003162static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003163 .show = rdev_attr_show,
3164 .store = rdev_attr_store,
3165};
3166static struct kobj_type rdev_ktype = {
3167 .release = rdev_free,
3168 .sysfs_ops = &rdev_sysfs_ops,
3169 .default_attrs = rdev_default_attrs,
3170};
3171
NeilBrown3cb03002011-10-11 16:45:26 +11003172int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003173{
3174 rdev->desc_nr = -1;
3175 rdev->saved_raid_disk = -1;
3176 rdev->raid_disk = -1;
3177 rdev->flags = 0;
3178 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003179 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003180 rdev->sb_events = 0;
3181 rdev->last_read_error.tv_sec = 0;
3182 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003183 rdev->sb_loaded = 0;
3184 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003185 atomic_set(&rdev->nr_pending, 0);
3186 atomic_set(&rdev->read_errors, 0);
3187 atomic_set(&rdev->corrected_errors, 0);
3188
3189 INIT_LIST_HEAD(&rdev->same_set);
3190 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003191
3192 /* Add space to store bad block list.
3193 * This reserves the space even on arrays where it cannot
3194 * be used - I wonder if that matters
3195 */
3196 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003197 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003198 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3199 seqlock_init(&rdev->badblocks.lock);
3200 if (rdev->badblocks.page == NULL)
3201 return -ENOMEM;
3202
3203 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003204}
3205EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206/*
3207 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3208 *
3209 * mark the device faulty if:
3210 *
3211 * - the device is nonexistent (zero size)
3212 * - the device has no valid superblock
3213 *
3214 * a faulty rdev _never_ has rdev->sb set.
3215 */
NeilBrown3cb03002011-10-11 16:45:26 +11003216static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 char b[BDEVNAME_SIZE];
3219 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003220 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 sector_t size;
3222
NeilBrown9ffae0c2006-01-06 00:20:32 -08003223 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (!rdev) {
3225 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3226 return ERR_PTR(-ENOMEM);
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
NeilBrown2230dfe2011-07-28 11:31:46 +10003229 err = md_rdev_init(rdev);
3230 if (err)
3231 goto abort_free;
3232 err = alloc_disk_sb(rdev);
3233 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto abort_free;
3235
NeilBrownc5d79ad2008-02-06 01:39:54 -08003236 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (err)
3238 goto abort_free;
3239
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003240 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003241
Mike Snitzer77304d22010-11-08 14:39:12 +01003242 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (!size) {
3244 printk(KERN_WARNING
3245 "md: %s has zero or unknown size, marking faulty!\n",
3246 bdevname(rdev->bdev,b));
3247 err = -EINVAL;
3248 goto abort_free;
3249 }
3250
3251 if (super_format >= 0) {
3252 err = super_types[super_format].
3253 load_super(rdev, NULL, super_minor);
3254 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003255 printk(KERN_WARNING
3256 "md: %s does not have a valid v%d.%d "
3257 "superblock, not importing!\n",
3258 bdevname(rdev->bdev,b),
3259 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 goto abort_free;
3261 }
3262 if (err < 0) {
3263 printk(KERN_WARNING
3264 "md: could not read %s's sb, not importing!\n",
3265 bdevname(rdev->bdev,b));
3266 goto abort_free;
3267 }
3268 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return rdev;
3271
3272abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003273 if (rdev->bdev)
3274 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003275 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 kfree(rdev);
3277 return ERR_PTR(err);
3278}
3279
3280/*
3281 * Check a full RAID array for plausibility
3282 */
3283
3284
NeilBrownfd01b882011-10-11 16:47:53 +11003285static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
3287 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003288 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 char b[BDEVNAME_SIZE];
3290
3291 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003292 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 switch (super_types[mddev->major_version].
3294 load_super(rdev, freshest, mddev->minor_version)) {
3295 case 1:
3296 freshest = rdev;
3297 break;
3298 case 0:
3299 break;
3300 default:
3301 printk( KERN_ERR \
3302 "md: fatal superblock inconsistency in %s"
3303 " -- removing from array\n",
3304 bdevname(rdev->bdev,b));
3305 kick_rdev_from_array(rdev);
3306 }
3307
3308
3309 super_types[mddev->major_version].
3310 validate_super(mddev, freshest);
3311
3312 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003313 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003314 if (mddev->max_disks &&
3315 (rdev->desc_nr >= mddev->max_disks ||
3316 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003317 printk(KERN_WARNING
3318 "md: %s: %s: only %d devices permitted\n",
3319 mdname(mddev), bdevname(rdev->bdev, b),
3320 mddev->max_disks);
3321 kick_rdev_from_array(rdev);
3322 continue;
3323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (rdev != freshest)
3325 if (super_types[mddev->major_version].
3326 validate_super(mddev, rdev)) {
3327 printk(KERN_WARNING "md: kicking non-fresh %s"
3328 " from array!\n",
3329 bdevname(rdev->bdev,b));
3330 kick_rdev_from_array(rdev);
3331 continue;
3332 }
3333 if (mddev->level == LEVEL_MULTIPATH) {
3334 rdev->desc_nr = i++;
3335 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003336 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003337 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003338 rdev->raid_disk = -1;
3339 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
NeilBrown72e02072009-12-14 12:49:55 +11003344/* Read a fixed-point number.
3345 * Numbers in sysfs attributes should be in "standard" units where
3346 * possible, so time should be in seconds.
3347 * However we internally use a a much smaller unit such as
3348 * milliseconds or jiffies.
3349 * This function takes a decimal number with a possible fractional
3350 * component, and produces an integer which is the result of
3351 * multiplying that number by 10^'scale'.
3352 * all without any floating-point arithmetic.
3353 */
3354int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3355{
3356 unsigned long result = 0;
3357 long decimals = -1;
3358 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3359 if (*cp == '.')
3360 decimals = 0;
3361 else if (decimals < scale) {
3362 unsigned int value;
3363 value = *cp - '0';
3364 result = result * 10 + value;
3365 if (decimals >= 0)
3366 decimals++;
3367 }
3368 cp++;
3369 }
3370 if (*cp == '\n')
3371 cp++;
3372 if (*cp)
3373 return -EINVAL;
3374 if (decimals < 0)
3375 decimals = 0;
3376 while (decimals < scale) {
3377 result *= 10;
3378 decimals ++;
3379 }
3380 *res = result;
3381 return 0;
3382}
3383
3384
NeilBrown19052c02008-08-05 15:54:13 +10003385static void md_safemode_timeout(unsigned long data);
3386
NeilBrowneae17012005-11-08 21:39:23 -08003387static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003388safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003389{
3390 int msec = (mddev->safemode_delay*1000)/HZ;
3391 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3392}
3393static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003394safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003395{
NeilBrown16f17b32006-06-26 00:27:37 -07003396 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003397
NeilBrown72e02072009-12-14 12:49:55 +11003398 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003399 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003400 if (msec == 0)
3401 mddev->safemode_delay = 0;
3402 else {
NeilBrown19052c02008-08-05 15:54:13 +10003403 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003404 mddev->safemode_delay = (msec*HZ)/1000;
3405 if (mddev->safemode_delay == 0)
3406 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003407 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003408 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003409 }
3410 return len;
3411}
3412static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003413__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003414
3415static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003416level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003417{
NeilBrown84fc4b52011-10-11 16:49:58 +11003418 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003419 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003420 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003421 else if (mddev->clevel[0])
3422 return sprintf(page, "%s\n", mddev->clevel);
3423 else if (mddev->level != LEVEL_NONE)
3424 return sprintf(page, "%d\n", mddev->level);
3425 else
3426 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003427}
3428
NeilBrownd9d166c2006-01-06 00:20:51 -08003429static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003430level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003431{
Dan Williamsf2859af2010-05-02 10:04:16 -07003432 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003433 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003434 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003435 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003436 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003437 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003438
3439 if (mddev->pers == NULL) {
3440 if (len == 0)
3441 return 0;
3442 if (len >= sizeof(mddev->clevel))
3443 return -ENOSPC;
3444 strncpy(mddev->clevel, buf, len);
3445 if (mddev->clevel[len-1] == '\n')
3446 len--;
3447 mddev->clevel[len] = 0;
3448 mddev->level = LEVEL_NONE;
3449 return rv;
3450 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003451 if (mddev->ro)
3452 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003453
3454 /* request to change the personality. Need to ensure:
3455 * - array is not engaged in resync/recovery/reshape
3456 * - old personality can be suspended
3457 * - new personality will access other array.
3458 */
3459
NeilBrownbb4f1e92010-08-08 21:18:03 +10003460 if (mddev->sync_thread ||
3461 mddev->reshape_position != MaxSector ||
3462 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003463 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003464
3465 if (!mddev->pers->quiesce) {
3466 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3467 mdname(mddev), mddev->pers->name);
3468 return -EINVAL;
3469 }
3470
3471 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003472 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003473 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003474 strncpy(clevel, buf, len);
3475 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003476 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003477 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003478 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003479 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003480
Dan Williamsf2859af2010-05-02 10:04:16 -07003481 if (request_module("md-%s", clevel) != 0)
3482 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003483 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003484 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003485 if (!pers || !try_module_get(pers->owner)) {
3486 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003487 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003488 return -EINVAL;
3489 }
3490 spin_unlock(&pers_lock);
3491
3492 if (pers == mddev->pers) {
3493 /* Nothing to do! */
3494 module_put(pers->owner);
3495 return rv;
3496 }
3497 if (!pers->takeover) {
3498 module_put(pers->owner);
3499 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003500 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003501 return -EINVAL;
3502 }
3503
NeilBrowndafb20f2012-03-19 12:46:39 +11003504 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003505 rdev->new_raid_disk = rdev->raid_disk;
3506
NeilBrown245f46c2009-03-31 14:39:39 +11003507 /* ->takeover must set new_* and/or delta_disks
3508 * if it succeeds, and may set them when it fails.
3509 */
3510 priv = pers->takeover(mddev);
3511 if (IS_ERR(priv)) {
3512 mddev->new_level = mddev->level;
3513 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003514 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003515 mddev->raid_disks -= mddev->delta_disks;
3516 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003517 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003518 module_put(pers->owner);
3519 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003520 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003521 return PTR_ERR(priv);
3522 }
3523
3524 /* Looks like we have a winner */
3525 mddev_suspend(mddev);
3526 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003527
3528 if (mddev->pers->sync_request == NULL &&
3529 pers->sync_request != NULL) {
3530 /* need to add the md_redundancy_group */
3531 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3532 printk(KERN_WARNING
3533 "md: cannot register extra attributes for %s\n",
3534 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003535 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003536 }
3537 if (mddev->pers->sync_request != NULL &&
3538 pers->sync_request == NULL) {
3539 /* need to remove the md_redundancy_group */
3540 if (mddev->to_remove == NULL)
3541 mddev->to_remove = &md_redundancy_group;
3542 }
3543
Trela Maciej54071b32010-03-08 16:02:42 +11003544 if (mddev->pers->sync_request == NULL &&
3545 mddev->external) {
3546 /* We are converting from a no-redundancy array
3547 * to a redundancy array and metadata is managed
3548 * externally so we need to be sure that writes
3549 * won't block due to a need to transition
3550 * clean->dirty
3551 * until external management is started.
3552 */
3553 mddev->in_sync = 0;
3554 mddev->safemode_delay = 0;
3555 mddev->safemode = 0;
3556 }
3557
NeilBrowndafb20f2012-03-19 12:46:39 +11003558 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003559 if (rdev->raid_disk < 0)
3560 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003561 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003562 rdev->new_raid_disk = -1;
3563 if (rdev->new_raid_disk == rdev->raid_disk)
3564 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003565 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003566 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003567 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003568 if (rdev->raid_disk < 0)
3569 continue;
3570 if (rdev->new_raid_disk == rdev->raid_disk)
3571 continue;
3572 rdev->raid_disk = rdev->new_raid_disk;
3573 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003574 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003575 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003576 if (sysfs_link_rdev(mddev, rdev))
3577 printk(KERN_WARNING "md: cannot register rd%d"
3578 " for %s after level change\n",
3579 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003580 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003581 }
3582
3583 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003584 mddev->pers = pers;
3585 mddev->private = priv;
3586 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3587 mddev->level = mddev->new_level;
3588 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003589 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003590 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003591 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003592 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003593 if (mddev->pers->sync_request == NULL) {
3594 /* this is now an array without redundancy, so
3595 * it must always be in_sync
3596 */
3597 mddev->in_sync = 1;
3598 del_timer_sync(&mddev->safemode_timer);
3599 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003600 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003601 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003602 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003603 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003604 if (!mddev->thread)
3605 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003606 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003607 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003608 return rv;
3609}
3610
3611static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003612__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003613
NeilBrownd4dbd022006-06-26 00:27:59 -07003614
3615static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003616layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003617{
3618 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003619 if (mddev->reshape_position != MaxSector &&
3620 mddev->layout != mddev->new_layout)
3621 return sprintf(page, "%d (%d)\n",
3622 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003623 return sprintf(page, "%d\n", mddev->layout);
3624}
3625
3626static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003627layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003628{
3629 char *e;
3630 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003631
3632 if (!*buf || (*e && *e != '\n'))
3633 return -EINVAL;
3634
NeilBrownb3546032009-03-31 14:56:41 +11003635 if (mddev->pers) {
3636 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003637 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003638 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003639 if (mddev->ro)
3640 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003641 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003642 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003643 if (err) {
3644 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003645 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003646 }
NeilBrownb3546032009-03-31 14:56:41 +11003647 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003648 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003649 if (mddev->reshape_position == MaxSector)
3650 mddev->layout = n;
3651 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003652 return len;
3653}
3654static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003655__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003656
3657
NeilBrowneae17012005-11-08 21:39:23 -08003658static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003659raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003660{
NeilBrownbb636542005-11-08 21:39:45 -08003661 if (mddev->raid_disks == 0)
3662 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003663 if (mddev->reshape_position != MaxSector &&
3664 mddev->delta_disks != 0)
3665 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3666 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003667 return sprintf(page, "%d\n", mddev->raid_disks);
3668}
3669
NeilBrownfd01b882011-10-11 16:47:53 +11003670static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003671
3672static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003673raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003674{
NeilBrownda943b992006-01-06 00:20:54 -08003675 char *e;
3676 int rv = 0;
3677 unsigned long n = simple_strtoul(buf, &e, 10);
3678
3679 if (!*buf || (*e && *e != '\n'))
3680 return -EINVAL;
3681
3682 if (mddev->pers)
3683 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003684 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003685 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003686 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003687
3688 rdev_for_each(rdev, mddev) {
3689 if (olddisks < n &&
3690 rdev->data_offset < rdev->new_data_offset)
3691 return -EINVAL;
3692 if (olddisks > n &&
3693 rdev->data_offset > rdev->new_data_offset)
3694 return -EINVAL;
3695 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003696 mddev->delta_disks = n - olddisks;
3697 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003698 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003699 } else
NeilBrownda943b992006-01-06 00:20:54 -08003700 mddev->raid_disks = n;
3701 return rv ? rv : len;
3702}
3703static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003704__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003705
NeilBrown24dd4692005-11-08 21:39:26 -08003706static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003707chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003708{
NeilBrown08a02ec2007-05-09 02:35:38 -07003709 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003710 mddev->chunk_sectors != mddev->new_chunk_sectors)
3711 return sprintf(page, "%d (%d)\n",
3712 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003713 mddev->chunk_sectors << 9);
3714 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003715}
3716
3717static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003718chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003719{
NeilBrown3b343802006-01-06 00:20:47 -08003720 char *e;
3721 unsigned long n = simple_strtoul(buf, &e, 10);
3722
NeilBrown3b343802006-01-06 00:20:47 -08003723 if (!*buf || (*e && *e != '\n'))
3724 return -EINVAL;
3725
NeilBrownb3546032009-03-31 14:56:41 +11003726 if (mddev->pers) {
3727 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003728 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003729 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003730 if (mddev->ro)
3731 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003732 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003733 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003734 if (err) {
3735 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003736 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003737 }
NeilBrownb3546032009-03-31 14:56:41 +11003738 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003739 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003740 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003741 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003742 }
NeilBrown3b343802006-01-06 00:20:47 -08003743 return len;
3744}
3745static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003746__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003747
NeilBrowna94213b2006-06-26 00:28:00 -07003748static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003749resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003750{
NeilBrownd1a7c502009-03-31 15:24:32 +11003751 if (mddev->recovery_cp == MaxSector)
3752 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003753 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3754}
3755
3756static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003757resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003758{
NeilBrowna94213b2006-06-26 00:28:00 -07003759 char *e;
3760 unsigned long long n = simple_strtoull(buf, &e, 10);
3761
NeilBrownb0986362011-05-11 15:52:21 +10003762 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003763 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003764 if (cmd_match(buf, "none"))
3765 n = MaxSector;
3766 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003767 return -EINVAL;
3768
3769 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003770 if (mddev->pers)
3771 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003772 return len;
3773}
3774static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003775__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003776
NeilBrown9e653b62006-06-26 00:27:58 -07003777/*
3778 * The array state can be:
3779 *
3780 * clear
3781 * No devices, no size, no level
3782 * Equivalent to STOP_ARRAY ioctl
3783 * inactive
3784 * May have some settings, but array is not active
3785 * all IO results in error
3786 * When written, doesn't tear down array, but just stops it
3787 * suspended (not supported yet)
3788 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003789 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003790 * readonly
3791 * no resync can happen. no superblocks get written.
3792 * write requests fail
3793 * read-auto
3794 * like readonly, but behaves like 'clean' on a write request.
3795 *
3796 * clean - no pending writes, but otherwise active.
3797 * When written to inactive array, starts without resync
3798 * If a write request arrives then
3799 * if metadata is known, mark 'dirty' and switch to 'active'.
3800 * if not known, block and switch to write-pending
3801 * If written to an active array that has pending writes, then fails.
3802 * active
3803 * fully active: IO and resync can be happening.
3804 * When written to inactive array, starts with resync
3805 *
3806 * write-pending
3807 * clean, but writes are blocked waiting for 'active' to be written.
3808 *
3809 * active-idle
3810 * like active, but no writes have been seen for a while (100msec).
3811 *
3812 */
3813enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3814 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003815static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003816 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3817 "write-pending", "active-idle", NULL };
3818
3819static int match_word(const char *word, char **list)
3820{
3821 int n;
3822 for (n=0; list[n]; n++)
3823 if (cmd_match(word, list[n]))
3824 break;
3825 return n;
3826}
3827
3828static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003829array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003830{
3831 enum array_state st = inactive;
3832
3833 if (mddev->pers)
3834 switch(mddev->ro) {
3835 case 1:
3836 st = readonly;
3837 break;
3838 case 2:
3839 st = read_auto;
3840 break;
3841 case 0:
3842 if (mddev->in_sync)
3843 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003844 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003845 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003846 else if (mddev->safemode)
3847 st = active_idle;
3848 else
3849 st = active;
3850 }
3851 else {
3852 if (list_empty(&mddev->disks) &&
3853 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003854 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003855 st = clear;
3856 else
3857 st = inactive;
3858 }
3859 return sprintf(page, "%s\n", array_states[st]);
3860}
3861
NeilBrowna05b7ea2012-07-19 15:59:18 +10003862static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3863static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003864static int do_md_run(struct mddev * mddev);
3865static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003866
3867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003868array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003869{
3870 int err = -EINVAL;
3871 enum array_state st = match_word(buf, array_states);
3872 switch(st) {
3873 case bad_word:
3874 break;
3875 case clear:
3876 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003877 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003878 break;
3879 case inactive:
3880 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003881 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003882 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003883 else
NeilBrowne6910632008-02-06 01:39:51 -08003884 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003885 break;
3886 case suspended:
3887 break; /* not supported yet */
3888 case readonly:
3889 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003890 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003891 else {
3892 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003893 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003894 err = do_md_run(mddev);
3895 }
3896 break;
3897 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003898 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003899 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003900 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003901 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003902 err = restart_array(mddev);
3903 if (err == 0) {
3904 mddev->ro = 2;
3905 set_disk_ro(mddev->gendisk, 0);
3906 }
NeilBrown9e653b62006-06-26 00:27:58 -07003907 } else {
3908 mddev->ro = 2;
3909 err = do_md_run(mddev);
3910 }
3911 break;
3912 case clean:
3913 if (mddev->pers) {
3914 restart_array(mddev);
3915 spin_lock_irq(&mddev->write_lock);
3916 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003917 if (mddev->in_sync == 0) {
3918 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003919 if (mddev->safemode == 1)
3920 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003921 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003922 }
3923 err = 0;
3924 } else
3925 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003926 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003927 } else
3928 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003929 break;
3930 case active:
3931 if (mddev->pers) {
3932 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003933 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003934 wake_up(&mddev->sb_wait);
3935 err = 0;
3936 } else {
3937 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003938 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003939 err = do_md_run(mddev);
3940 }
3941 break;
3942 case write_pending:
3943 case active_idle:
3944 /* these cannot be set */
3945 break;
3946 }
3947 if (err)
3948 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003949 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003950 if (mddev->hold_active == UNTIL_IOCTL)
3951 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003952 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003953 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003954 }
NeilBrown9e653b62006-06-26 00:27:58 -07003955}
NeilBrown80ca3a42006-07-10 04:44:18 -07003956static struct md_sysfs_entry md_array_state =
3957__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003958
NeilBrown6d7ff732006-01-06 00:21:16 -08003959static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003960max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003961 return sprintf(page, "%d\n",
3962 atomic_read(&mddev->max_corr_read_errors));
3963}
3964
3965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003966max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003967{
3968 char *e;
3969 unsigned long n = simple_strtoul(buf, &e, 10);
3970
3971 if (*buf && (*e == 0 || *e == '\n')) {
3972 atomic_set(&mddev->max_corr_read_errors, n);
3973 return len;
3974 }
3975 return -EINVAL;
3976}
3977
3978static struct md_sysfs_entry max_corr_read_errors =
3979__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3980 max_corrected_read_errors_store);
3981
3982static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003983null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003984{
3985 return -EINVAL;
3986}
3987
3988static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003989new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003990{
3991 /* buf must be %d:%d\n? giving major and minor numbers */
3992 /* The new device is added to the array.
3993 * If the array has a persistent superblock, we read the
3994 * superblock to initialise info and check validity.
3995 * Otherwise, only checking done is that in bind_rdev_to_array,
3996 * which mainly checks size.
3997 */
3998 char *e;
3999 int major = simple_strtoul(buf, &e, 10);
4000 int minor;
4001 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004002 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004003 int err;
4004
4005 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4006 return -EINVAL;
4007 minor = simple_strtoul(e+1, &e, 10);
4008 if (*e && *e != '\n')
4009 return -EINVAL;
4010 dev = MKDEV(major, minor);
4011 if (major != MAJOR(dev) ||
4012 minor != MINOR(dev))
4013 return -EOVERFLOW;
4014
4015
4016 if (mddev->persistent) {
4017 rdev = md_import_device(dev, mddev->major_version,
4018 mddev->minor_version);
4019 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004020 struct md_rdev *rdev0
4021 = list_entry(mddev->disks.next,
4022 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004023 err = super_types[mddev->major_version]
4024 .load_super(rdev, rdev0, mddev->minor_version);
4025 if (err < 0)
4026 goto out;
4027 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004028 } else if (mddev->external)
4029 rdev = md_import_device(dev, -2, -1);
4030 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004031 rdev = md_import_device(dev, -1, -1);
4032
4033 if (IS_ERR(rdev))
4034 return PTR_ERR(rdev);
4035 err = bind_rdev_to_array(rdev, mddev);
4036 out:
4037 if (err)
4038 export_rdev(rdev);
4039 return err ? err : len;
4040}
4041
4042static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004043__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004044
4045static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004046bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004047{
4048 char *end;
4049 unsigned long chunk, end_chunk;
4050
4051 if (!mddev->bitmap)
4052 goto out;
4053 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4054 while (*buf) {
4055 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4056 if (buf == end) break;
4057 if (*end == '-') { /* range */
4058 buf = end + 1;
4059 end_chunk = simple_strtoul(buf, &end, 0);
4060 if (buf == end) break;
4061 }
4062 if (*end && !isspace(*end)) break;
4063 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004064 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004065 }
4066 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4067out:
4068 return len;
4069}
4070
4071static struct md_sysfs_entry md_bitmap =
4072__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4073
4074static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004075size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004076{
Andre Noll58c0fed2009-03-31 14:33:13 +11004077 return sprintf(page, "%llu\n",
4078 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004079}
4080
NeilBrownfd01b882011-10-11 16:47:53 +11004081static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004082
4083static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004084size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004085{
4086 /* If array is inactive, we can reduce the component size, but
4087 * not increase it (except from 0).
4088 * If array is active, we can try an on-line resize
4089 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004090 sector_t sectors;
4091 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004092
Andre Noll58c0fed2009-03-31 14:33:13 +11004093 if (err < 0)
4094 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004095 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004096 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004097 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004098 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004099 if (mddev->dev_sectors == 0 ||
4100 mddev->dev_sectors > sectors)
4101 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004102 else
4103 err = -ENOSPC;
4104 }
4105 return err ? err : len;
4106}
4107
4108static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004109__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004110
NeilBrown8bb93aa2006-01-06 00:20:50 -08004111
Masanari Iida83f0d772012-10-30 00:18:08 +09004112/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004113 * This is one of
4114 * 'none' for arrays with no metadata (good luck...)
4115 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004116 * or N.M for internally known formats
4117 */
4118static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004119metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004120{
4121 if (mddev->persistent)
4122 return sprintf(page, "%d.%d\n",
4123 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004124 else if (mddev->external)
4125 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004126 else
4127 return sprintf(page, "none\n");
4128}
4129
4130static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004131metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004132{
4133 int major, minor;
4134 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004135 /* Changing the details of 'external' metadata is
4136 * always permitted. Otherwise there must be
4137 * no devices attached to the array.
4138 */
4139 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4140 ;
4141 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004142 return -EBUSY;
4143
4144 if (cmd_match(buf, "none")) {
4145 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004146 mddev->external = 0;
4147 mddev->major_version = 0;
4148 mddev->minor_version = 90;
4149 return len;
4150 }
4151 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004152 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004153 if (namelen >= sizeof(mddev->metadata_type))
4154 namelen = sizeof(mddev->metadata_type)-1;
4155 strncpy(mddev->metadata_type, buf+9, namelen);
4156 mddev->metadata_type[namelen] = 0;
4157 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4158 mddev->metadata_type[--namelen] = 0;
4159 mddev->persistent = 0;
4160 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004161 mddev->major_version = 0;
4162 mddev->minor_version = 90;
4163 return len;
4164 }
4165 major = simple_strtoul(buf, &e, 10);
4166 if (e==buf || *e != '.')
4167 return -EINVAL;
4168 buf = e+1;
4169 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004170 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004171 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004172 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004173 return -ENOENT;
4174 mddev->major_version = major;
4175 mddev->minor_version = minor;
4176 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004177 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004178 return len;
4179}
4180
4181static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004182__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004183
NeilBrowna35b0d62006-01-06 00:20:49 -08004184static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004185action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004186{
NeilBrown7eec3142005-11-08 21:39:44 -08004187 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004188 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4189 type = "frozen";
4190 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004191 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004192 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4193 type = "reshape";
4194 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004195 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4196 type = "resync";
4197 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4198 type = "check";
4199 else
4200 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004201 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004202 type = "recover";
4203 }
4204 return sprintf(page, "%s\n", type);
4205}
4206
4207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004209{
NeilBrown7eec3142005-11-08 21:39:44 -08004210 if (!mddev->pers || !mddev->pers->sync_request)
4211 return -EINVAL;
4212
NeilBrownb6a9ce62009-05-26 09:41:17 +10004213 if (cmd_match(page, "frozen"))
4214 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4215 else
4216 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4217
4218 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004219 if (mddev->sync_thread) {
4220 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004221 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004222 }
NeilBrown03c902e2006-01-06 00:20:46 -08004223 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4224 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004225 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004226 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004227 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004228 else if (cmd_match(page, "recover")) {
4229 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4230 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4231 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004232 int err;
4233 if (mddev->pers->start_reshape == NULL)
4234 return -EINVAL;
4235 err = mddev->pers->start_reshape(mddev);
4236 if (err)
4237 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004238 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004239 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004240 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004241 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004242 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004243 return -EINVAL;
4244 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4245 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004246 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004247 if (mddev->ro == 2) {
4248 /* A write to sync_action is enough to justify
4249 * canceling read-auto mode
4250 */
4251 mddev->ro = 0;
4252 md_wakeup_thread(mddev->sync_thread);
4253 }
NeilBrown03c902e2006-01-06 00:20:46 -08004254 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004255 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004256 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004257 return len;
4258}
4259
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004260static struct md_sysfs_entry md_scan_mode =
4261__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4262
4263static ssize_t
4264last_sync_action_show(struct mddev *mddev, char *page)
4265{
4266 return sprintf(page, "%s\n", mddev->last_sync_action);
4267}
4268
4269static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4270
NeilBrown9d888832005-11-08 21:39:26 -08004271static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004272mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004273{
4274 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004275 (unsigned long long)
4276 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004277}
4278
NeilBrown80ca3a42006-07-10 04:44:18 -07004279static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004280
NeilBrown88202a02006-01-06 00:21:36 -08004281static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004282sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004283{
4284 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4285 mddev->sync_speed_min ? "local": "system");
4286}
4287
4288static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004289sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004290{
4291 int min;
4292 char *e;
4293 if (strncmp(buf, "system", 6)==0) {
4294 mddev->sync_speed_min = 0;
4295 return len;
4296 }
4297 min = simple_strtoul(buf, &e, 10);
4298 if (buf == e || (*e && *e != '\n') || min <= 0)
4299 return -EINVAL;
4300 mddev->sync_speed_min = min;
4301 return len;
4302}
4303
4304static struct md_sysfs_entry md_sync_min =
4305__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4306
4307static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004308sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004309{
4310 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4311 mddev->sync_speed_max ? "local": "system");
4312}
4313
4314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004315sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004316{
4317 int max;
4318 char *e;
4319 if (strncmp(buf, "system", 6)==0) {
4320 mddev->sync_speed_max = 0;
4321 return len;
4322 }
4323 max = simple_strtoul(buf, &e, 10);
4324 if (buf == e || (*e && *e != '\n') || max <= 0)
4325 return -EINVAL;
4326 mddev->sync_speed_max = max;
4327 return len;
4328}
4329
4330static struct md_sysfs_entry md_sync_max =
4331__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4332
Iustin Popd7f3d292007-10-16 23:30:54 -07004333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004334degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004335{
4336 return sprintf(page, "%d\n", mddev->degraded);
4337}
4338static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004339
4340static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004341sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004342{
4343 return sprintf(page, "%d\n", mddev->parallel_resync);
4344}
4345
4346static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004347sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004348{
4349 long n;
4350
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004351 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004352 return -EINVAL;
4353
4354 if (n != 0 && n != 1)
4355 return -EINVAL;
4356
4357 mddev->parallel_resync = n;
4358
4359 if (mddev->sync_thread)
4360 wake_up(&resync_wait);
4361
4362 return len;
4363}
4364
4365/* force parallel resync, even with shared block devices */
4366static struct md_sysfs_entry md_sync_force_parallel =
4367__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4368 sync_force_parallel_show, sync_force_parallel_store);
4369
4370static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004371sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004372{
4373 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004374 if (mddev->curr_resync == 0)
4375 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004376 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4377 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004378 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004379 db = resync - mddev->resync_mark_cnt;
4380 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004381}
4382
NeilBrown80ca3a42006-07-10 04:44:18 -07004383static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004384
4385static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004386sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004387{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004388 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004389
NeilBrownacb180b2009-04-14 16:28:34 +10004390 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4391 return sprintf(page, "none\n");
4392
NeilBrown72f36d52012-10-11 14:25:57 +11004393 if (mddev->curr_resync == 1 ||
4394 mddev->curr_resync == 2)
4395 return sprintf(page, "delayed\n");
4396
NeilBrownc804cde2012-05-21 09:28:33 +10004397 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4398 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004399 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004400 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004401 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004402
NeilBrownacb180b2009-04-14 16:28:34 +10004403 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004404 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004405}
4406
NeilBrown80ca3a42006-07-10 04:44:18 -07004407static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004408
NeilBrowne464eaf2006-03-27 01:18:14 -08004409static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004410min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004411{
4412 return sprintf(page, "%llu\n",
4413 (unsigned long long)mddev->resync_min);
4414}
4415static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004416min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004417{
4418 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004419 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004420 return -EINVAL;
4421 if (min > mddev->resync_max)
4422 return -EINVAL;
4423 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4424 return -EBUSY;
4425
4426 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004427 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004428 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004429 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004430 return -EINVAL;
4431 }
4432 mddev->resync_min = min;
4433
4434 return len;
4435}
4436
4437static struct md_sysfs_entry md_min_sync =
4438__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4439
4440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004441max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004442{
4443 if (mddev->resync_max == MaxSector)
4444 return sprintf(page, "max\n");
4445 else
4446 return sprintf(page, "%llu\n",
4447 (unsigned long long)mddev->resync_max);
4448}
4449static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004450max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004451{
4452 if (strncmp(buf, "max", 3) == 0)
4453 mddev->resync_max = MaxSector;
4454 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004455 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004456 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004457 return -EINVAL;
4458 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004459 return -EINVAL;
4460 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004461 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004462 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4463 return -EBUSY;
4464
4465 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004466 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004467 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004468 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004469 return -EINVAL;
4470 }
4471 mddev->resync_max = max;
4472 }
4473 wake_up(&mddev->recovery_wait);
4474 return len;
4475}
4476
4477static struct md_sysfs_entry md_max_sync =
4478__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4479
4480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004481suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004482{
4483 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4484}
4485
4486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004488{
4489 char *e;
4490 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004491 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004492
NeilBrownb8d966e2009-07-01 11:14:04 +10004493 if (mddev->pers == NULL ||
4494 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004495 return -EINVAL;
4496 if (buf == e || (*e && *e != '\n'))
4497 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004498
4499 mddev->suspend_lo = new;
4500 if (new >= old)
4501 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004502 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004503 else {
4504 /* Expanding suspended region - need to wait */
4505 mddev->pers->quiesce(mddev, 1);
4506 mddev->pers->quiesce(mddev, 0);
4507 }
4508 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004509}
4510static struct md_sysfs_entry md_suspend_lo =
4511__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4512
4513
4514static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004515suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004516{
4517 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4518}
4519
4520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004521suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004522{
4523 char *e;
4524 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004525 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004526
NeilBrownb8d966e2009-07-01 11:14:04 +10004527 if (mddev->pers == NULL ||
4528 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004529 return -EINVAL;
4530 if (buf == e || (*e && *e != '\n'))
4531 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004532
4533 mddev->suspend_hi = new;
4534 if (new <= old)
4535 /* Shrinking suspended region */
4536 mddev->pers->quiesce(mddev, 2);
4537 else {
4538 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004539 mddev->pers->quiesce(mddev, 1);
4540 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004541 }
4542 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004543}
4544static struct md_sysfs_entry md_suspend_hi =
4545__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4546
NeilBrown08a02ec2007-05-09 02:35:38 -07004547static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004548reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004549{
4550 if (mddev->reshape_position != MaxSector)
4551 return sprintf(page, "%llu\n",
4552 (unsigned long long)mddev->reshape_position);
4553 strcpy(page, "none\n");
4554 return 5;
4555}
4556
4557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004558reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004559{
NeilBrownc6563a82012-05-21 09:27:00 +10004560 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004561 char *e;
4562 unsigned long long new = simple_strtoull(buf, &e, 10);
4563 if (mddev->pers)
4564 return -EBUSY;
4565 if (buf == e || (*e && *e != '\n'))
4566 return -EINVAL;
4567 mddev->reshape_position = new;
4568 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004569 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004570 mddev->new_level = mddev->level;
4571 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004572 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004573 rdev_for_each(rdev, mddev)
4574 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004575 return len;
4576}
4577
4578static struct md_sysfs_entry md_reshape_position =
4579__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4580 reshape_position_store);
4581
Dan Williamsb522adc2009-03-31 15:00:31 +11004582static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004583reshape_direction_show(struct mddev *mddev, char *page)
4584{
4585 return sprintf(page, "%s\n",
4586 mddev->reshape_backwards ? "backwards" : "forwards");
4587}
4588
4589static ssize_t
4590reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4591{
4592 int backwards = 0;
4593 if (cmd_match(buf, "forwards"))
4594 backwards = 0;
4595 else if (cmd_match(buf, "backwards"))
4596 backwards = 1;
4597 else
4598 return -EINVAL;
4599 if (mddev->reshape_backwards == backwards)
4600 return len;
4601
4602 /* check if we are allowed to change */
4603 if (mddev->delta_disks)
4604 return -EBUSY;
4605
4606 if (mddev->persistent &&
4607 mddev->major_version == 0)
4608 return -EINVAL;
4609
4610 mddev->reshape_backwards = backwards;
4611 return len;
4612}
4613
4614static struct md_sysfs_entry md_reshape_direction =
4615__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4616 reshape_direction_store);
4617
4618static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004619array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004620{
4621 if (mddev->external_size)
4622 return sprintf(page, "%llu\n",
4623 (unsigned long long)mddev->array_sectors/2);
4624 else
4625 return sprintf(page, "default\n");
4626}
4627
4628static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004629array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004630{
4631 sector_t sectors;
4632
4633 if (strncmp(buf, "default", 7) == 0) {
4634 if (mddev->pers)
4635 sectors = mddev->pers->size(mddev, 0, 0);
4636 else
4637 sectors = mddev->array_sectors;
4638
4639 mddev->external_size = 0;
4640 } else {
4641 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4642 return -EINVAL;
4643 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004644 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004645
4646 mddev->external_size = 1;
4647 }
4648
4649 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004650 if (mddev->pers) {
4651 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004652 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004653 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004654 return len;
4655}
4656
4657static struct md_sysfs_entry md_array_size =
4658__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4659 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004660
NeilBrowneae17012005-11-08 21:39:23 -08004661static struct attribute *md_default_attrs[] = {
4662 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004663 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004664 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004665 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004666 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004667 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004668 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004669 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004670 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004671 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004672 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004673 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004674 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004675 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004676 NULL,
4677};
4678
4679static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004680 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004681 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004682 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004683 &md_sync_min.attr,
4684 &md_sync_max.attr,
4685 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004686 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004687 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004688 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004689 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004690 &md_suspend_lo.attr,
4691 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004692 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004693 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004694 NULL,
4695};
NeilBrown411036f2005-11-08 21:39:40 -08004696static struct attribute_group md_redundancy_group = {
4697 .name = NULL,
4698 .attrs = md_redundancy_attrs,
4699};
4700
NeilBrowneae17012005-11-08 21:39:23 -08004701
4702static ssize_t
4703md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4704{
4705 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004706 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004707 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004708
4709 if (!entry->show)
4710 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004711 spin_lock(&all_mddevs_lock);
4712 if (list_empty(&mddev->all_mddevs)) {
4713 spin_unlock(&all_mddevs_lock);
4714 return -EBUSY;
4715 }
4716 mddev_get(mddev);
4717 spin_unlock(&all_mddevs_lock);
4718
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004719 rv = mddev_lock(mddev);
4720 if (!rv) {
4721 rv = entry->show(mddev, page);
4722 mddev_unlock(mddev);
4723 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004724 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004725 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004726}
4727
4728static ssize_t
4729md_attr_store(struct kobject *kobj, struct attribute *attr,
4730 const char *page, size_t length)
4731{
4732 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004733 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004734 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004735
4736 if (!entry->store)
4737 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004738 if (!capable(CAP_SYS_ADMIN))
4739 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004740 spin_lock(&all_mddevs_lock);
4741 if (list_empty(&mddev->all_mddevs)) {
4742 spin_unlock(&all_mddevs_lock);
4743 return -EBUSY;
4744 }
4745 mddev_get(mddev);
4746 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004747 if (entry->store == new_dev_store)
4748 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004749 rv = mddev_lock(mddev);
4750 if (!rv) {
4751 rv = entry->store(mddev, page, length);
4752 mddev_unlock(mddev);
4753 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004754 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004755 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004756}
4757
4758static void md_free(struct kobject *ko)
4759{
NeilBrownfd01b882011-10-11 16:47:53 +11004760 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004761
4762 if (mddev->sysfs_state)
4763 sysfs_put(mddev->sysfs_state);
4764
4765 if (mddev->gendisk) {
4766 del_gendisk(mddev->gendisk);
4767 put_disk(mddev->gendisk);
4768 }
4769 if (mddev->queue)
4770 blk_cleanup_queue(mddev->queue);
4771
NeilBrowneae17012005-11-08 21:39:23 -08004772 kfree(mddev);
4773}
4774
Emese Revfy52cf25d2010-01-19 02:58:23 +01004775static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004776 .show = md_attr_show,
4777 .store = md_attr_store,
4778};
4779static struct kobj_type md_ktype = {
4780 .release = md_free,
4781 .sysfs_ops = &md_sysfs_ops,
4782 .default_attrs = md_default_attrs,
4783};
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785int mdp_major = 0;
4786
Dan Williams5fd3a172009-03-04 00:57:25 -07004787static void mddev_delayed_delete(struct work_struct *ws)
4788{
NeilBrownfd01b882011-10-11 16:47:53 +11004789 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004790
NeilBrown43a70502009-12-14 12:49:55 +11004791 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004792 kobject_del(&mddev->kobj);
4793 kobject_put(&mddev->kobj);
4794}
4795
NeilBrownefeb53c2009-01-09 08:31:10 +11004796static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004798 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004799 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004801 int partitioned;
4802 int shift;
4803 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004804 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004807 return -ENODEV;
4808
4809 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4810 shift = partitioned ? MdpMinorShift : 0;
4811 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Tejun Heoe804ac72010-10-15 15:36:08 +02004813 /* wait for any previous instance of this device to be
4814 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004815 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004816 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004817
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004818 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004819 error = -EEXIST;
4820 if (mddev->gendisk)
4821 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004822
4823 if (name) {
4824 /* Need to ensure that 'name' is not a duplicate.
4825 */
NeilBrownfd01b882011-10-11 16:47:53 +11004826 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004827 spin_lock(&all_mddevs_lock);
4828
4829 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4830 if (mddev2->gendisk &&
4831 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4832 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004833 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004834 }
4835 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
NeilBrown8b765392009-01-09 08:31:08 +11004837
NeilBrown0909dc42009-07-01 12:27:21 +10004838 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004839 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004840 if (!mddev->queue)
4841 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004842 mddev->queue->queuedata = mddev;
4843
NeilBrown409c57f2009-03-31 14:39:39 +11004844 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004845 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 disk = alloc_disk(1 << shift);
4848 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004849 blk_cleanup_queue(mddev->queue);
4850 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004851 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004853 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004855 if (name)
4856 strcpy(disk->disk_name, name);
4857 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004859 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 disk->fops = &md_fops;
4862 disk->private_data = mddev;
4863 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004864 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004865 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004866 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004867 * remove it now.
4868 */
4869 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004871 /* As soon as we call add_disk(), another thread could get
4872 * through to md_open, so make sure it doesn't get too far
4873 */
4874 mutex_lock(&mddev->open_mutex);
4875 add_disk(disk);
4876
Tejun Heoed9e1982008-08-25 19:56:05 +09004877 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4878 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004879 if (error) {
4880 /* This isn't possible, but as kobject_init_and_add is marked
4881 * __must_check, we must do something with the result
4882 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004883 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4884 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004885 error = 0;
4886 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004887 if (mddev->kobj.sd &&
4888 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004889 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004890 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004891 abort:
4892 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004893 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004894 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004895 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004896 }
NeilBrownd3374822009-01-09 08:31:10 +11004897 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004898 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004899}
4900
4901static struct kobject *md_probe(dev_t dev, int *part, void *data)
4902{
4903 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return NULL;
4905}
4906
NeilBrownefeb53c2009-01-09 08:31:10 +11004907static int add_named_array(const char *val, struct kernel_param *kp)
4908{
4909 /* val must be "md_*" where * is not all digits.
4910 * We allocate an array with a large free minor number, and
4911 * set the name to val. val must not already be an active name.
4912 */
4913 int len = strlen(val);
4914 char buf[DISK_NAME_LEN];
4915
4916 while (len && val[len-1] == '\n')
4917 len--;
4918 if (len >= DISK_NAME_LEN)
4919 return -E2BIG;
4920 strlcpy(buf, val, len+1);
4921 if (strncmp(buf, "md_", 3) != 0)
4922 return -EINVAL;
4923 return md_alloc(0, buf);
4924}
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926static void md_safemode_timeout(unsigned long data)
4927{
NeilBrownfd01b882011-10-11 16:47:53 +11004928 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Neil Brown0fd62b82008-06-28 08:31:36 +10004930 if (!atomic_read(&mddev->writes_pending)) {
4931 mddev->safemode = 1;
4932 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004933 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 md_wakeup_thread(mddev->thread);
4936}
4937
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004938static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
NeilBrownfd01b882011-10-11 16:47:53 +11004940int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
NeilBrown2604b702006-01-06 00:20:36 -08004942 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004943 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004944 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
NeilBrowna757e642005-04-16 15:26:42 -07004946 if (list_empty(&mddev->disks))
4947 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 if (mddev->pers)
4951 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004952 /* Cannot run until previous stop completes properly */
4953 if (mddev->sysfs_active)
4954 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 /*
4957 * Analyze all RAID superblock(s)
4958 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004959 if (!mddev->raid_disks) {
4960 if (!mddev->persistent)
4961 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004962 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
NeilBrownd9d166c2006-01-06 00:20:51 -08004965 if (mddev->level != LEVEL_NONE)
4966 request_module("md-level-%d", mddev->level);
4967 else if (mddev->clevel[0])
4968 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 /*
4971 * Drop all container device buffers, from now on
4972 * the only valid external interface is through the md
4973 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004975 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004976 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 continue;
4978 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004979 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004980
4981 /* perform some consistency tests on the device.
4982 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004983 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004984 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004985 if (rdev->meta_bdev) {
4986 /* Nothing to check */;
4987 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004988 if (mddev->dev_sectors &&
4989 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004990 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004991 printk("md: %s: data overlaps metadata\n",
4992 mdname(mddev));
4993 return -EINVAL;
4994 }
4995 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004996 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004997 > rdev->data_offset) {
4998 printk("md: %s: metadata overlaps data\n",
4999 mdname(mddev));
5000 return -EINVAL;
5001 }
5002 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005003 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 }
5005
NeilBrowna167f662010-10-26 18:31:13 +11005006 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005007 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005010 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005011 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005013 if (mddev->level != LEVEL_NONE)
5014 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5015 mddev->level);
5016 else
5017 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5018 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return -EINVAL;
5020 }
NeilBrown2604b702006-01-06 00:20:36 -08005021 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005023 if (mddev->level != pers->level) {
5024 mddev->level = pers->level;
5025 mddev->new_level = pers->level;
5026 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005027 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
NeilBrownf6705572006-03-27 01:18:11 -08005029 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005030 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005031 /* This personality cannot handle reshaping... */
5032 mddev->pers = NULL;
5033 module_put(pers->owner);
5034 return -EINVAL;
5035 }
5036
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005037 if (pers->sync_request) {
5038 /* Warn if this is a potentially silly
5039 * configuration.
5040 */
5041 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005042 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005043 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005044
NeilBrowndafb20f2012-03-19 12:46:39 +11005045 rdev_for_each(rdev, mddev)
5046 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005047 if (rdev < rdev2 &&
5048 rdev->bdev->bd_contains ==
5049 rdev2->bdev->bd_contains) {
5050 printk(KERN_WARNING
5051 "%s: WARNING: %s appears to be"
5052 " on the same physical disk as"
5053 " %s.\n",
5054 mdname(mddev),
5055 bdevname(rdev->bdev,b),
5056 bdevname(rdev2->bdev,b2));
5057 warned = 1;
5058 }
5059 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005060
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005061 if (warned)
5062 printk(KERN_WARNING
5063 "True protection against single-disk"
5064 " failure might be compromised.\n");
5065 }
5066
NeilBrown657390d2005-08-26 18:34:16 -07005067 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005068 /* may be over-ridden by personality */
5069 mddev->resync_max_sectors = mddev->dev_sectors;
5070
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005071 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
NeilBrown0f9552b2009-12-30 12:08:50 +11005073 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005074 mddev->ro = 2; /* read-only, but switch on first write */
5075
NeilBrownb15c2e52006-01-06 00:20:16 -08005076 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005077 if (err)
5078 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005079 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5080 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5081 " but 'external_size' not in effect?\n", __func__);
5082 printk(KERN_ERR
5083 "md: invalid array_size %llu > default size %llu\n",
5084 (unsigned long long)mddev->array_sectors / 2,
5085 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5086 err = -EINVAL;
5087 mddev->pers->stop(mddev);
5088 }
NeilBrownef99bf42012-05-22 13:55:08 +10005089 if (err == 0 && mddev->pers->sync_request &&
5090 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005091 err = bitmap_create(mddev);
5092 if (err) {
5093 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5094 mdname(mddev), err);
5095 mddev->pers->stop(mddev);
5096 }
5097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 module_put(mddev->pers->owner);
5100 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005101 bitmap_destroy(mddev);
5102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005104 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005105 if (mddev->kobj.sd &&
5106 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005107 printk(KERN_WARNING
5108 "md: cannot register extra attributes for %s\n",
5109 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005110 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005111 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005112 mddev->ro = 0;
5113
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005115 atomic_set(&mddev->max_corr_read_errors,
5116 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 mddev->safemode = 0;
5118 mddev->safemode_timer.function = md_safemode_timeout;
5119 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005120 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005122 smp_wmb();
5123 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005124 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005125 if (rdev->raid_disk >= 0)
5126 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005127 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5130
NeilBrown7a0a5352013-08-27 16:28:23 +10005131 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005132 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
NeilBrownd7603b72006-01-06 00:20:30 -08005134 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005135 sysfs_notify_dirent_safe(mddev->sysfs_state);
5136 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005137 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 return 0;
5139}
NeilBrown390ee602010-06-01 19:37:27 +10005140EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
NeilBrownfd01b882011-10-11 16:47:53 +11005142static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005143{
5144 int err;
5145
5146 err = md_run(mddev);
5147 if (err)
5148 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005149 err = bitmap_load(mddev);
5150 if (err) {
5151 bitmap_destroy(mddev);
5152 goto out;
5153 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005154
5155 md_wakeup_thread(mddev->thread);
5156 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5157
NeilBrownfe60b012010-03-29 11:10:42 +11005158 set_capacity(mddev->gendisk, mddev->array_sectors);
5159 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005160 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005161 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5162out:
5163 return err;
5164}
5165
NeilBrownfd01b882011-10-11 16:47:53 +11005166static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
5168 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
Andre Noll80fab1d2008-07-11 22:02:21 +10005170 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005172 return -ENXIO;
5173 if (!mddev->pers)
5174 return -EINVAL;
5175 if (!mddev->ro)
5176 return -EBUSY;
5177 mddev->safemode = 0;
5178 mddev->ro = 0;
5179 set_disk_ro(disk, 0);
5180 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5181 mdname(mddev));
5182 /* Kick recovery or resync if necessary */
5183 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5184 md_wakeup_thread(mddev->thread);
5185 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005186 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
NeilBrownfd01b882011-10-11 16:47:53 +11005190static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005191{
5192 mddev->array_sectors = 0;
5193 mddev->external_size = 0;
5194 mddev->dev_sectors = 0;
5195 mddev->raid_disks = 0;
5196 mddev->recovery_cp = 0;
5197 mddev->resync_min = 0;
5198 mddev->resync_max = MaxSector;
5199 mddev->reshape_position = MaxSector;
5200 mddev->external = 0;
5201 mddev->persistent = 0;
5202 mddev->level = LEVEL_NONE;
5203 mddev->clevel[0] = 0;
5204 mddev->flags = 0;
5205 mddev->ro = 0;
5206 mddev->metadata_type[0] = 0;
5207 mddev->chunk_sectors = 0;
5208 mddev->ctime = mddev->utime = 0;
5209 mddev->layout = 0;
5210 mddev->max_disks = 0;
5211 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005212 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005213 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005214 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005215 mddev->new_level = LEVEL_NONE;
5216 mddev->new_layout = 0;
5217 mddev->new_chunk_sectors = 0;
5218 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005219 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005220 mddev->suspend_lo = mddev->suspend_hi = 0;
5221 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5222 mddev->recovery = 0;
5223 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005224 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005225 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005226 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005227 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005228 mddev->bitmap_info.offset = 0;
5229 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005230 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005231 mddev->bitmap_info.chunksize = 0;
5232 mddev->bitmap_info.daemon_sleep = 0;
5233 mddev->bitmap_info.max_write_behind = 0;
5234}
5235
NeilBrownfd01b882011-10-11 16:47:53 +11005236static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005237{
NeilBrown6b6204e2013-05-09 09:48:30 +10005238 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005239 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005240 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005241 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005242 }
5243
5244 del_timer_sync(&mddev->safemode_timer);
5245
5246 bitmap_flush(mddev);
5247 md_super_wait(mddev);
5248
NeilBrownb6d428c2013-04-24 11:42:42 +10005249 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005250 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005251 /* mark array as shutdown cleanly */
5252 mddev->in_sync = 1;
5253 md_update_sb(mddev, 1);
5254 }
5255}
NeilBrowndefad612011-01-14 09:14:33 +11005256
NeilBrownfd01b882011-10-11 16:47:53 +11005257void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005258{
NeilBrown29f097c2013-11-14 17:54:51 +11005259 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005260 __md_stop_writes(mddev);
5261 mddev_unlock(mddev);
5262}
NeilBrown390ee602010-06-01 19:37:27 +10005263EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005264
NeilBrown5eff3c42012-11-19 10:47:48 +11005265static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005266{
NeilBrown0ca69882011-01-14 09:14:33 +11005267 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005268 mddev->pers->stop(mddev);
5269 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5270 mddev->to_remove = &md_redundancy_group;
5271 module_put(mddev->pers->owner);
5272 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005273 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005274}
NeilBrown5eff3c42012-11-19 10:47:48 +11005275
5276void md_stop(struct mddev *mddev)
5277{
5278 /* stop the array and free an attached data structures.
5279 * This is called from dm-raid
5280 */
5281 __md_stop(mddev);
5282 bitmap_destroy(mddev);
5283 if (mddev->bio_set)
5284 bioset_free(mddev->bio_set);
5285}
5286
NeilBrown390ee602010-06-01 19:37:27 +10005287EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005288
NeilBrowna05b7ea2012-07-19 15:59:18 +10005289static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005290{
5291 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005292 int did_freeze = 0;
5293
5294 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5295 did_freeze = 1;
5296 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5297 md_wakeup_thread(mddev->thread);
5298 }
5299 if (mddev->sync_thread) {
5300 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5301 /* Thread might be blocked waiting for metadata update
5302 * which will now never happen */
5303 wake_up_process(mddev->sync_thread->tsk);
5304 }
5305 mddev_unlock(mddev);
5306 wait_event(resync_wait, mddev->sync_thread == NULL);
5307 mddev_lock_nointr(mddev);
5308
NeilBrowna4bd82d2010-03-29 13:23:10 +11005309 mutex_lock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005310 if (atomic_read(&mddev->openers) > !!bdev ||
5311 mddev->sync_thread ||
5312 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005313 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005314 if (did_freeze) {
5315 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5316 md_wakeup_thread(mddev->thread);
5317 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005318 err = -EBUSY;
5319 goto out;
5320 }
5321 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005322 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005323
5324 err = -ENXIO;
5325 if (mddev->ro==1)
5326 goto out;
5327 mddev->ro = 1;
5328 set_disk_ro(mddev->gendisk, 1);
5329 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005330 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005331 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005332 }
5333out:
5334 mutex_unlock(&mddev->open_mutex);
5335 return err;
5336}
5337
NeilBrown9e653b62006-06-26 00:27:58 -07005338/* mode:
5339 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005340 * 2 - stop but do not disassemble array
5341 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005342static int do_md_stop(struct mddev * mddev, int mode,
5343 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005346 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005347 int did_freeze = 0;
5348
5349 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5350 did_freeze = 1;
5351 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5352 md_wakeup_thread(mddev->thread);
5353 }
5354 if (mddev->sync_thread) {
5355 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5356 /* Thread might be blocked waiting for metadata update
5357 * which will now never happen */
5358 wake_up_process(mddev->sync_thread->tsk);
5359 }
5360 mddev_unlock(mddev);
5361 wait_event(resync_wait, mddev->sync_thread == NULL);
5362 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
NeilBrownc8c00a62009-08-10 12:50:52 +10005364 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005365 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005366 mddev->sysfs_active ||
5367 mddev->sync_thread ||
5368 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005369 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005370 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005371 if (did_freeze) {
5372 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5373 md_wakeup_thread(mddev->thread);
5374 }
NeilBrown260fa032013-08-27 16:44:13 +10005375 return -EBUSY;
5376 }
NeilBrown6e17b022010-08-07 21:41:19 +10005377 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005378 if (mddev->ro)
5379 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005380
NeilBrowndefad612011-01-14 09:14:33 +11005381 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005382 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005383 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005384 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005385
NeilBrowna4bd82d2010-03-29 13:23:10 +11005386 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005387 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005388
NeilBrowndafb20f2012-03-19 12:46:39 +11005389 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005390 if (rdev->raid_disk >= 0)
5391 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005392
NeilBrowna4bd82d2010-03-29 13:23:10 +11005393 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005394 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005395 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005396 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005397
NeilBrowna4bd82d2010-03-29 13:23:10 +11005398 if (mddev->ro)
5399 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005400 } else
5401 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 /*
5403 * Free resources if final stop
5404 */
NeilBrown9e653b62006-06-26 00:27:58 -07005405 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5407
NeilBrown978f9462006-02-02 14:28:05 -08005408 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005409 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005410 fput(mddev->bitmap_info.file);
5411 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005412 }
NeilBrownc3d97142009-12-14 12:49:52 +11005413 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 export_array(mddev);
5416
NeilBrown6177b472010-03-29 11:37:13 +11005417 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005418 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005419 if (mddev->hold_active == UNTIL_STOP)
5420 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005421 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005422 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005423 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005424 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426}
5427
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005428#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005429static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430{
NeilBrown3cb03002011-10-11 16:45:26 +11005431 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 int err;
5433
NeilBrowna757e642005-04-16 15:26:42 -07005434 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 printk(KERN_INFO "md: running: ");
5438
NeilBrowndafb20f2012-03-19 12:46:39 +11005439 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 char b[BDEVNAME_SIZE];
5441 printk("<%s>", bdevname(rdev->bdev,b));
5442 }
5443 printk("\n");
5444
NeilBrownd710e132008-10-13 11:55:12 +11005445 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 if (err) {
5447 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005448 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450}
5451
5452/*
5453 * lets try to run arrays based on all disks that have arrived
5454 * until now. (those are in pending_raid_disks)
5455 *
5456 * the method: pick the first pending disk, collect all disks with
5457 * the same UUID, remove all from the pending list and put them into
5458 * the 'same_array' list. Then order this list based on superblock
5459 * update time (freshest comes first), kick out 'old' disks and
5460 * compare superblocks. If everything's fine then run it.
5461 *
5462 * If "unit" is allocated, then bump its reference count
5463 */
5464static void autorun_devices(int part)
5465{
NeilBrown3cb03002011-10-11 16:45:26 +11005466 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005467 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 char b[BDEVNAME_SIZE];
5469
5470 printk(KERN_INFO "md: autorun ...\n");
5471 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005472 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005474 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005476 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 printk(KERN_INFO "md: considering %s ...\n",
5479 bdevname(rdev0->bdev,b));
5480 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005481 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (super_90_load(rdev, rdev0, 0) >= 0) {
5483 printk(KERN_INFO "md: adding %s ...\n",
5484 bdevname(rdev->bdev,b));
5485 list_move(&rdev->same_set, &candidates);
5486 }
5487 /*
5488 * now we have a set of devices, with all of them having
5489 * mostly sane superblocks. It's time to allocate the
5490 * mddev.
5491 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005492 if (part) {
5493 dev = MKDEV(mdp_major,
5494 rdev0->preferred_minor << MdpMinorShift);
5495 unit = MINOR(dev) >> MdpMinorShift;
5496 } else {
5497 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5498 unit = MINOR(dev);
5499 }
5500 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5502 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5503 break;
5504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 md_probe(dev, NULL, NULL);
5507 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005508 if (!mddev || !mddev->gendisk) {
5509 if (mddev)
5510 mddev_put(mddev);
5511 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 "md: cannot allocate memory for md drive.\n");
5513 break;
5514 }
5515 if (mddev_lock(mddev))
5516 printk(KERN_WARNING "md: %s locked, cannot run\n",
5517 mdname(mddev));
5518 else if (mddev->raid_disks || mddev->major_version
5519 || !list_empty(&mddev->disks)) {
5520 printk(KERN_WARNING
5521 "md: %s already running, cannot run %s\n",
5522 mdname(mddev), bdevname(rdev0->bdev,b));
5523 mddev_unlock(mddev);
5524 } else {
5525 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005526 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005527 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 list_del_init(&rdev->same_set);
5529 if (bind_rdev_to_array(rdev, mddev))
5530 export_rdev(rdev);
5531 }
5532 autorun_array(mddev);
5533 mddev_unlock(mddev);
5534 }
5535 /* on success, candidates will be empty, on error
5536 * it won't...
5537 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005538 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005539 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 mddev_put(mddev);
5543 }
5544 printk(KERN_INFO "md: ... autorun DONE.\n");
5545}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005546#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548static int get_version(void __user * arg)
5549{
5550 mdu_version_t ver;
5551
5552 ver.major = MD_MAJOR_VERSION;
5553 ver.minor = MD_MINOR_VERSION;
5554 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5555
5556 if (copy_to_user(arg, &ver, sizeof(ver)))
5557 return -EFAULT;
5558
5559 return 0;
5560}
5561
NeilBrownfd01b882011-10-11 16:47:53 +11005562static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005565 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005566 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
NeilBrown1ca69c42012-10-11 13:37:33 +11005568 nr = working = insync = failed = spare = 0;
5569 rcu_read_lock();
5570 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005572 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 failed++;
5574 else {
5575 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005576 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005577 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 else
5579 spare++;
5580 }
5581 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005582 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
5584 info.major_version = mddev->major_version;
5585 info.minor_version = mddev->minor_version;
5586 info.patch_version = MD_PATCHLEVEL_VERSION;
5587 info.ctime = mddev->ctime;
5588 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005589 info.size = mddev->dev_sectors / 2;
5590 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005591 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 info.nr_disks = nr;
5593 info.raid_disks = mddev->raid_disks;
5594 info.md_minor = mddev->md_minor;
5595 info.not_persistent= !mddev->persistent;
5596
5597 info.utime = mddev->utime;
5598 info.state = 0;
5599 if (mddev->in_sync)
5600 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005601 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005602 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005603 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 info.working_disks = working;
5605 info.failed_disks = failed;
5606 info.spare_disks = spare;
5607
5608 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005609 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 if (copy_to_user(arg, &info, sizeof(info)))
5612 return -EFAULT;
5613
5614 return 0;
5615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005618{
5619 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5620 char *ptr, *buf = NULL;
5621 int err = -ENOMEM;
5622
NeilBrown60559da2013-07-16 16:44:29 +10005623 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005624
NeilBrown32a76272005-06-21 17:17:14 -07005625 if (!file)
5626 goto out;
5627
5628 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005629 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005630 file->pathname[0] = '\0';
5631 goto copy_out;
5632 }
5633
5634 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5635 if (!buf)
5636 goto out;
5637
NeilBrown1ec885c2012-05-22 13:55:10 +10005638 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5639 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005640 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005641 goto out;
5642
5643 strcpy(file->pathname, ptr);
5644
5645copy_out:
5646 err = 0;
5647 if (copy_to_user(arg, file, sizeof(*file)))
5648 err = -EFAULT;
5649out:
5650 kfree(buf);
5651 kfree(file);
5652 return err;
5653}
5654
NeilBrownfd01b882011-10-11 16:47:53 +11005655static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
5657 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 if (copy_from_user(&info, arg, sizeof(info)))
5661 return -EFAULT;
5662
NeilBrown1ca69c42012-10-11 13:37:33 +11005663 rcu_read_lock();
5664 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (rdev) {
5666 info.major = MAJOR(rdev->bdev->bd_dev);
5667 info.minor = MINOR(rdev->bdev->bd_dev);
5668 info.raid_disk = rdev->raid_disk;
5669 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005670 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005672 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 info.state |= (1<<MD_DISK_ACTIVE);
5674 info.state |= (1<<MD_DISK_SYNC);
5675 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005676 if (test_bit(WriteMostly, &rdev->flags))
5677 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 } else {
5679 info.major = info.minor = 0;
5680 info.raid_disk = -1;
5681 info.state = (1<<MD_DISK_REMOVED);
5682 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005683 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
5685 if (copy_to_user(arg, &info, sizeof(info)))
5686 return -EFAULT;
5687
5688 return 0;
5689}
5690
NeilBrownfd01b882011-10-11 16:47:53 +11005691static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
5693 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005694 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 dev_t dev = MKDEV(info->major,info->minor);
5696
5697 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5698 return -EOVERFLOW;
5699
5700 if (!mddev->raid_disks) {
5701 int err;
5702 /* expecting a device which has a superblock */
5703 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5704 if (IS_ERR(rdev)) {
5705 printk(KERN_WARNING
5706 "md: md_import_device returned %ld\n",
5707 PTR_ERR(rdev));
5708 return PTR_ERR(rdev);
5709 }
5710 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005711 struct md_rdev *rdev0
5712 = list_entry(mddev->disks.next,
5713 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005714 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 .load_super(rdev, rdev0, mddev->minor_version);
5716 if (err < 0) {
5717 printk(KERN_WARNING
5718 "md: %s has different UUID to %s\n",
5719 bdevname(rdev->bdev,b),
5720 bdevname(rdev0->bdev,b2));
5721 export_rdev(rdev);
5722 return -EINVAL;
5723 }
5724 }
5725 err = bind_rdev_to_array(rdev, mddev);
5726 if (err)
5727 export_rdev(rdev);
5728 return err;
5729 }
5730
5731 /*
5732 * add_new_disk can be used once the array is assembled
5733 * to add "hot spares". They must already have a superblock
5734 * written
5735 */
5736 if (mddev->pers) {
5737 int err;
5738 if (!mddev->pers->hot_add_disk) {
5739 printk(KERN_WARNING
5740 "%s: personality does not support diskops!\n",
5741 mdname(mddev));
5742 return -EINVAL;
5743 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005744 if (mddev->persistent)
5745 rdev = md_import_device(dev, mddev->major_version,
5746 mddev->minor_version);
5747 else
5748 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 if (IS_ERR(rdev)) {
5750 printk(KERN_WARNING
5751 "md: md_import_device returned %ld\n",
5752 PTR_ERR(rdev));
5753 return PTR_ERR(rdev);
5754 }
NeilBrown1a855a02010-12-09 16:36:28 +11005755 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005756 if (!mddev->persistent) {
5757 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005758 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005759 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005760 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005761 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005762 } else
NeilBrown41158c72005-06-21 17:17:25 -07005763 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005764 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005765 } 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
NeilBrownb2d444d2005-11-08 21:39:31 -08005777 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005778 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5779 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005780 else
5781 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005782
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 rdev->raid_disk = -1;
5784 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005785 if (!err && !mddev->pers->hot_remove_disk) {
5786 /* If there is hot_add_disk but no hot_remove_disk
5787 * then added disks for geometry changes,
5788 * and should be added immediately.
5789 */
5790 super_types[mddev->major_version].
5791 validate_super(mddev, rdev);
5792 err = mddev->pers->hot_add_disk(mddev, rdev);
5793 if (err)
5794 unbind_rdev_from_array(rdev);
5795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 if (err)
5797 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005798 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005799 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005800
NeilBrown7ceb17e2013-04-24 11:42:42 +10005801 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005802 if (mddev->degraded)
5803 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005804 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005805 if (!err)
5806 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005807 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 return err;
5809 }
5810
5811 /* otherwise, add_new_disk is only allowed
5812 * for major_version==0 superblocks
5813 */
5814 if (mddev->major_version != 0) {
5815 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5816 mdname(mddev));
5817 return -EINVAL;
5818 }
5819
5820 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5821 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005822 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 if (IS_ERR(rdev)) {
5824 printk(KERN_WARNING
5825 "md: error, md_import_device() returned %ld\n",
5826 PTR_ERR(rdev));
5827 return PTR_ERR(rdev);
5828 }
5829 rdev->desc_nr = info->number;
5830 if (info->raid_disk < mddev->raid_disks)
5831 rdev->raid_disk = info->raid_disk;
5832 else
5833 rdev->raid_disk = -1;
5834
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005836 if (info->state & (1<<MD_DISK_SYNC))
5837 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005839 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5840 set_bit(WriteMostly, &rdev->flags);
5841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 if (!mddev->persistent) {
5843 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005844 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5845 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005846 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005847 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
NeilBrown2bf071b2006-01-06 00:20:55 -08005849 err = bind_rdev_to_array(rdev, mddev);
5850 if (err) {
5851 export_rdev(rdev);
5852 return err;
5853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
5855
5856 return 0;
5857}
5858
NeilBrownfd01b882011-10-11 16:47:53 +11005859static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
5861 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005862 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 rdev = find_rdev(mddev, dev);
5865 if (!rdev)
5866 return -ENXIO;
5867
NeilBrown3ea8929d2013-04-24 11:42:41 +10005868 clear_bit(Blocked, &rdev->flags);
5869 remove_and_add_spares(mddev, rdev);
5870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 if (rdev->raid_disk >= 0)
5872 goto busy;
5873
5874 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005875 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005876 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
5878 return 0;
5879busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005880 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 bdevname(rdev->bdev,b), mdname(mddev));
5882 return -EBUSY;
5883}
5884
NeilBrownfd01b882011-10-11 16:47:53 +11005885static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886{
5887 char b[BDEVNAME_SIZE];
5888 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005889 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 if (!mddev->pers)
5892 return -ENODEV;
5893
5894 if (mddev->major_version != 0) {
5895 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5896 " version-0 superblocks.\n",
5897 mdname(mddev));
5898 return -EINVAL;
5899 }
5900 if (!mddev->pers->hot_add_disk) {
5901 printk(KERN_WARNING
5902 "%s: personality does not support diskops!\n",
5903 mdname(mddev));
5904 return -EINVAL;
5905 }
5906
NeilBrownd710e132008-10-13 11:55:12 +11005907 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 if (IS_ERR(rdev)) {
5909 printk(KERN_WARNING
5910 "md: error, md_import_device() returned %ld\n",
5911 PTR_ERR(rdev));
5912 return -EINVAL;
5913 }
5914
5915 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005916 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005918 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
NeilBrown8190e752009-06-18 08:48:58 +10005920 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
NeilBrownb2d444d2005-11-08 21:39:31 -08005922 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 printk(KERN_WARNING
5924 "md: can not hot-add faulty %s disk to %s!\n",
5925 bdevname(rdev->bdev,b), mdname(mddev));
5926 err = -EINVAL;
5927 goto abort_export;
5928 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005929 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005931 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005932 err = bind_rdev_to_array(rdev, mddev);
5933 if (err)
5934 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
5936 /*
5937 * The rest should better be atomic, we can have disk failures
5938 * noticed in interrupt contexts ...
5939 */
5940
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 rdev->raid_disk = -1;
5942
NeilBrown850b2b42006-10-03 01:15:46 -07005943 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
5945 /*
5946 * Kick recovery, maybe this spare has to be added to the
5947 * array immediately.
5948 */
5949 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5950 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005951 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 return 0;
5953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954abort_export:
5955 export_rdev(rdev);
5956 return err;
5957}
5958
NeilBrownfd01b882011-10-11 16:47:53 +11005959static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005960{
NeilBrown035328c2014-04-09 12:25:40 +10005961 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005962
NeilBrown36fa3062005-09-09 16:23:45 -07005963 if (mddev->pers) {
5964 if (!mddev->pers->quiesce)
5965 return -EBUSY;
5966 if (mddev->recovery || mddev->sync_thread)
5967 return -EBUSY;
5968 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005969 }
5970
NeilBrown36fa3062005-09-09 16:23:45 -07005971
5972 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005973 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005974 if (mddev->bitmap)
5975 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005976 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005977
NeilBrownc3d97142009-12-14 12:49:52 +11005978 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005979 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5980 mdname(mddev));
5981 return -EBADF;
5982 }
5983
NeilBrown035328c2014-04-09 12:25:40 +10005984 inode = mddev->bitmap_info.file->f_mapping->host;
5985 if (!S_ISREG(inode->i_mode)) {
5986 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5987 mdname(mddev));
5988 err = -EBADF;
5989 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5990 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5991 mdname(mddev));
5992 err = -EBADF;
5993 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005994 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5995 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005996 err = -EBUSY;
5997 }
5998 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005999 fput(mddev->bitmap_info.file);
6000 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006001 return err;
6002 }
NeilBrownc3d97142009-12-14 12:49:52 +11006003 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006004 } else if (mddev->bitmap == NULL)
6005 return -ENOENT; /* cannot remove what isn't there */
6006 err = 0;
6007 if (mddev->pers) {
6008 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006009 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006010 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006011 if (!err)
6012 err = bitmap_load(mddev);
6013 }
NeilBrownd7375ab322006-06-26 00:27:43 -07006014 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006015 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07006016 fd = -1; /* make sure to put the file */
6017 }
NeilBrown36fa3062005-09-09 16:23:45 -07006018 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07006019 }
6020 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006021 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11006022 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11006023 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006024 }
6025
NeilBrown32a76272005-06-21 17:17:14 -07006026 return err;
6027}
6028
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029/*
6030 * set_array_info is used two different ways
6031 * The original usage is when creating a new array.
6032 * In this usage, raid_disks is > 0 and it together with
6033 * level, size, not_persistent,layout,chunksize determine the
6034 * shape of the array.
6035 * This will always create an array with a type-0.90.0 superblock.
6036 * The newer usage is when assembling an array.
6037 * In this case raid_disks will be 0, and the major_version field is
6038 * use to determine which style super-blocks are to be found on the devices.
6039 * The minor and patch _version numbers are also kept incase the
6040 * super_block handler wishes to interpret them.
6041 */
NeilBrownfd01b882011-10-11 16:47:53 +11006042static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043{
6044
6045 if (info->raid_disks == 0) {
6046 /* just setting version number for superblock loading */
6047 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006048 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 super_types[info->major_version].name == NULL) {
6050 /* maybe try to auto-load a module? */
6051 printk(KERN_INFO
6052 "md: superblock version %d not known\n",
6053 info->major_version);
6054 return -EINVAL;
6055 }
6056 mddev->major_version = info->major_version;
6057 mddev->minor_version = info->minor_version;
6058 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006059 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006060 /* ensure mddev_put doesn't delete this now that there
6061 * is some minimal configuration.
6062 */
6063 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 return 0;
6065 }
6066 mddev->major_version = MD_MAJOR_VERSION;
6067 mddev->minor_version = MD_MINOR_VERSION;
6068 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6069 mddev->ctime = get_seconds();
6070
6071 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006072 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006073 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 mddev->raid_disks = info->raid_disks;
6075 /* don't set md_minor, it is determined by which /dev/md* was
6076 * openned
6077 */
6078 if (info->state & (1<<MD_SB_CLEAN))
6079 mddev->recovery_cp = MaxSector;
6080 else
6081 mddev->recovery_cp = 0;
6082 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006083 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
6085 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006086 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
6088 mddev->max_disks = MD_SB_DISKS;
6089
NeilBrowne6910632008-02-06 01:39:51 -08006090 if (mddev->persistent)
6091 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006092 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
NeilBrownc3d97142009-12-14 12:49:52 +11006094 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006095 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006096 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006097
NeilBrownf6705572006-03-27 01:18:11 -08006098 mddev->reshape_position = MaxSector;
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 /*
6101 * Generate a 128 bit UUID
6102 */
6103 get_random_bytes(mddev->uuid, 16);
6104
NeilBrownf6705572006-03-27 01:18:11 -08006105 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006106 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006107 mddev->new_layout = mddev->layout;
6108 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006109 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return 0;
6112}
6113
NeilBrownfd01b882011-10-11 16:47:53 +11006114void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006115{
Dan Williamsb522adc2009-03-31 15:00:31 +11006116 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6117
6118 if (mddev->external_size)
6119 return;
6120
Dan Williams1f403622009-03-31 14:59:03 +11006121 mddev->array_sectors = array_sectors;
6122}
6123EXPORT_SYMBOL(md_set_array_sectors);
6124
NeilBrownfd01b882011-10-11 16:47:53 +11006125static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006126{
NeilBrown3cb03002011-10-11 16:45:26 +11006127 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006128 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006129 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006130
6131 if (mddev->pers->resize == NULL)
6132 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006133 /* The "num_sectors" is the number of sectors of each device that
6134 * is used. This can only make sense for arrays with redundancy.
6135 * linear and raid0 always use whatever space is available. We can only
6136 * consider changing this number if no resync or reconstruction is
6137 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006138 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006139 * of each device. If num_sectors is zero, we find the largest size
6140 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006141 */
6142 if (mddev->sync_thread)
6143 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006144 if (mddev->ro)
6145 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006146
NeilBrowndafb20f2012-03-19 12:46:39 +11006147 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006148 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006149
Andre Nolld71f9f82008-07-11 22:02:22 +10006150 if (fit && (num_sectors == 0 || num_sectors > avail))
6151 num_sectors = avail;
6152 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006153 return -ENOSPC;
6154 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006155 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006156 if (!rv)
6157 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006158 return rv;
6159}
6160
NeilBrownfd01b882011-10-11 16:47:53 +11006161static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006162{
6163 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006164 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006165 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006166 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006167 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006168 if (mddev->ro)
6169 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006170 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006171 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006172 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006173 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006174 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006175
6176 rdev_for_each(rdev, mddev) {
6177 if (mddev->raid_disks < raid_disks &&
6178 rdev->data_offset < rdev->new_data_offset)
6179 return -EINVAL;
6180 if (mddev->raid_disks > raid_disks &&
6181 rdev->data_offset > rdev->new_data_offset)
6182 return -EINVAL;
6183 }
6184
NeilBrown63c70c42006-03-27 01:18:13 -08006185 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006186 if (mddev->delta_disks < 0)
6187 mddev->reshape_backwards = 1;
6188 else if (mddev->delta_disks > 0)
6189 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006190
6191 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006192 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006193 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006194 mddev->reshape_backwards = 0;
6195 }
NeilBrownda943b992006-01-06 00:20:54 -08006196 return rv;
6197}
6198
6199
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200/*
6201 * update_array_info is used to change the configuration of an
6202 * on-line array.
6203 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6204 * fields in the info are checked against the array.
6205 * Any differences that cannot be handled will cause an error.
6206 * Normally, only one change can be managed at a time.
6207 */
NeilBrownfd01b882011-10-11 16:47:53 +11006208static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
6210 int rv = 0;
6211 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006212 int state = 0;
6213
6214 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006215 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006216 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
6218 if (mddev->major_version != info->major_version ||
6219 mddev->minor_version != info->minor_version ||
6220/* mddev->patch_version != info->patch_version || */
6221 mddev->ctime != info->ctime ||
6222 mddev->level != info->level ||
6223/* mddev->layout != info->layout || */
6224 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006225 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006226 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6227 ((state^info->state) & 0xfffffe00)
6228 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return -EINVAL;
6230 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006231 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6232 cnt++;
6233 if (mddev->raid_disks != info->raid_disks)
6234 cnt++;
6235 if (mddev->layout != info->layout)
6236 cnt++;
6237 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6238 cnt++;
6239 if (cnt == 0)
6240 return 0;
6241 if (cnt > 1)
6242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 if (mddev->layout != info->layout) {
6245 /* Change layout
6246 * we don't need to do anything at the md level, the
6247 * personality will take care of it all.
6248 */
NeilBrown50ac1682009-06-18 08:47:55 +10006249 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006251 else {
6252 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006253 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006254 if (rv)
6255 mddev->new_layout = mddev->layout;
6256 return rv;
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006259 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006260 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
NeilBrownda943b992006-01-06 00:20:54 -08006262 if (mddev->raid_disks != info->raid_disks)
6263 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
NeilBrown36fa3062005-09-09 16:23:45 -07006265 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6266 if (mddev->pers->quiesce == NULL)
6267 return -EINVAL;
6268 if (mddev->recovery || mddev->sync_thread)
6269 return -EBUSY;
6270 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6271 /* add the bitmap */
6272 if (mddev->bitmap)
6273 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006274 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006275 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006276 mddev->bitmap_info.offset =
6277 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006278 mddev->bitmap_info.space =
6279 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006280 mddev->pers->quiesce(mddev, 1);
6281 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006282 if (!rv)
6283 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006284 if (rv)
6285 bitmap_destroy(mddev);
6286 mddev->pers->quiesce(mddev, 0);
6287 } else {
6288 /* remove the bitmap */
6289 if (!mddev->bitmap)
6290 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006291 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006292 return -EINVAL;
6293 mddev->pers->quiesce(mddev, 1);
6294 bitmap_destroy(mddev);
6295 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006296 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006297 }
6298 }
NeilBrown850b2b42006-10-03 01:15:46 -07006299 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 return rv;
6301}
6302
NeilBrownfd01b882011-10-11 16:47:53 +11006303static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304{
NeilBrown3cb03002011-10-11 16:45:26 +11006305 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006306 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
6308 if (mddev->pers == NULL)
6309 return -ENODEV;
6310
NeilBrown1ca69c42012-10-11 13:37:33 +11006311 rcu_read_lock();
6312 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006314 err = -ENODEV;
6315 else {
6316 md_error(mddev, rdev);
6317 if (!test_bit(Faulty, &rdev->flags))
6318 err = -EBUSY;
6319 }
6320 rcu_read_unlock();
6321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322}
6323
Andre Noll2f9618c2008-04-25 18:57:58 +02006324/*
6325 * We have a problem here : there is no easy way to give a CHS
6326 * virtual geometry. We currently pretend that we have a 2 heads
6327 * 4 sectors (with a BIG number of cylinders...). This drives
6328 * dosfs just mad... ;-)
6329 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006330static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6331{
NeilBrownfd01b882011-10-11 16:47:53 +11006332 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006333
6334 geo->heads = 2;
6335 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006336 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006337 return 0;
6338}
6339
Nicolas Schichancb335f82014-01-15 16:58:52 +01006340static inline bool md_ioctl_valid(unsigned int cmd)
6341{
6342 switch (cmd) {
6343 case ADD_NEW_DISK:
6344 case BLKROSET:
6345 case GET_ARRAY_INFO:
6346 case GET_BITMAP_FILE:
6347 case GET_DISK_INFO:
6348 case HOT_ADD_DISK:
6349 case HOT_REMOVE_DISK:
6350 case PRINT_RAID_DEBUG:
6351 case RAID_AUTORUN:
6352 case RAID_VERSION:
6353 case RESTART_ARRAY_RW:
6354 case RUN_ARRAY:
6355 case SET_ARRAY_INFO:
6356 case SET_BITMAP_FILE:
6357 case SET_DISK_FAULTY:
6358 case STOP_ARRAY:
6359 case STOP_ARRAY_RO:
6360 return true;
6361 default:
6362 return false;
6363 }
6364}
6365
Al Viroa39907f2008-03-02 10:31:15 -05006366static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 unsigned int cmd, unsigned long arg)
6368{
6369 int err = 0;
6370 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006371 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006372 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Nicolas Schichancb335f82014-01-15 16:58:52 +01006374 if (!md_ioctl_valid(cmd))
6375 return -ENOTTY;
6376
NeilBrown506c9e42011-12-23 10:17:26 +11006377 switch (cmd) {
6378 case RAID_VERSION:
6379 case GET_ARRAY_INFO:
6380 case GET_DISK_INFO:
6381 break;
6382 default:
6383 if (!capable(CAP_SYS_ADMIN))
6384 return -EACCES;
6385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387 /*
6388 * Commands dealing with the RAID driver but not any
6389 * particular array:
6390 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006391 switch (cmd) {
6392 case RAID_VERSION:
6393 err = get_version(argp);
6394 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
NeilBrownc02c0ae2012-12-11 13:39:21 +11006396 case PRINT_RAID_DEBUG:
6397 err = 0;
6398 md_print_devices();
6399 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
6401#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006402 case RAID_AUTORUN:
6403 err = 0;
6404 autostart_arrays(arg);
6405 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006407 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 }
6409
6410 /*
6411 * Commands creating/starting a new array:
6412 */
6413
Al Viroa39907f2008-03-02 10:31:15 -05006414 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
6416 if (!mddev) {
6417 BUG();
6418 goto abort;
6419 }
6420
NeilBrown1ca69c42012-10-11 13:37:33 +11006421 /* Some actions do not requires the mutex */
6422 switch (cmd) {
6423 case GET_ARRAY_INFO:
6424 if (!mddev->raid_disks && !mddev->external)
6425 err = -ENODEV;
6426 else
6427 err = get_array_info(mddev, argp);
6428 goto abort;
6429
6430 case GET_DISK_INFO:
6431 if (!mddev->raid_disks && !mddev->external)
6432 err = -ENODEV;
6433 else
6434 err = get_disk_info(mddev, argp);
6435 goto abort;
6436
6437 case SET_DISK_FAULTY:
6438 err = set_disk_faulty(mddev, new_decode_dev(arg));
6439 goto abort;
6440 }
6441
NeilBrowna7a3f082012-12-11 13:35:54 +11006442 if (cmd == ADD_NEW_DISK)
6443 /* need to ensure md_delayed_delete() has completed */
6444 flush_workqueue(md_misc_wq);
6445
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006446 if (cmd == HOT_REMOVE_DISK)
6447 /* need to ensure recovery thread has run */
6448 wait_event_interruptible_timeout(mddev->sb_wait,
6449 !test_bit(MD_RECOVERY_NEEDED,
6450 &mddev->flags),
6451 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006452 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6453 /* Need to flush page cache, and ensure no-one else opens
6454 * and writes
6455 */
6456 mutex_lock(&mddev->open_mutex);
6457 if (atomic_read(&mddev->openers) > 1) {
6458 mutex_unlock(&mddev->open_mutex);
6459 err = -EBUSY;
6460 goto abort;
6461 }
6462 set_bit(MD_STILL_CLOSED, &mddev->flags);
6463 mutex_unlock(&mddev->open_mutex);
6464 sync_blockdev(bdev);
6465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 err = mddev_lock(mddev);
6467 if (err) {
6468 printk(KERN_INFO
6469 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6470 err, cmd);
6471 goto abort;
6472 }
6473
NeilBrownc02c0ae2012-12-11 13:39:21 +11006474 if (cmd == SET_ARRAY_INFO) {
6475 mdu_array_info_t info;
6476 if (!arg)
6477 memset(&info, 0, sizeof(info));
6478 else if (copy_from_user(&info, argp, sizeof(info))) {
6479 err = -EFAULT;
6480 goto abort_unlock;
6481 }
6482 if (mddev->pers) {
6483 err = update_array_info(mddev, &info);
6484 if (err) {
6485 printk(KERN_WARNING "md: couldn't update"
6486 " array info. %d\n", err);
6487 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 }
6489 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006490 }
6491 if (!list_empty(&mddev->disks)) {
6492 printk(KERN_WARNING
6493 "md: array %s already has disks!\n",
6494 mdname(mddev));
6495 err = -EBUSY;
6496 goto abort_unlock;
6497 }
6498 if (mddev->raid_disks) {
6499 printk(KERN_WARNING
6500 "md: array %s already initialised!\n",
6501 mdname(mddev));
6502 err = -EBUSY;
6503 goto abort_unlock;
6504 }
6505 err = set_array_info(mddev, &info);
6506 if (err) {
6507 printk(KERN_WARNING "md: couldn't set"
6508 " array info. %d\n", err);
6509 goto abort_unlock;
6510 }
6511 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 }
6513
6514 /*
6515 * Commands querying/configuring an existing array:
6516 */
NeilBrown32a76272005-06-21 17:17:14 -07006517 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006518 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006519 if ((!mddev->raid_disks && !mddev->external)
6520 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6521 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6522 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 err = -ENODEV;
6524 goto abort_unlock;
6525 }
6526
6527 /*
6528 * Commands even a read-only array can execute:
6529 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006530 switch (cmd) {
6531 case GET_BITMAP_FILE:
6532 err = get_bitmap_file(mddev, argp);
6533 goto done_unlock;
6534
6535 case RESTART_ARRAY_RW:
6536 err = restart_array(mddev);
6537 goto done_unlock;
6538
6539 case STOP_ARRAY:
6540 err = do_md_stop(mddev, 0, bdev);
6541 goto done_unlock;
6542
6543 case STOP_ARRAY_RO:
6544 err = md_set_readonly(mddev, bdev);
6545 goto done_unlock;
6546
NeilBrown3ea8929d2013-04-24 11:42:41 +10006547 case HOT_REMOVE_DISK:
6548 err = hot_remove_disk(mddev, new_decode_dev(arg));
6549 goto done_unlock;
6550
NeilBrown7ceb17e2013-04-24 11:42:42 +10006551 case ADD_NEW_DISK:
6552 /* We can support ADD_NEW_DISK on read-only arrays
6553 * on if we are re-adding a preexisting device.
6554 * So require mddev->pers and MD_DISK_SYNC.
6555 */
6556 if (mddev->pers) {
6557 mdu_disk_info_t info;
6558 if (copy_from_user(&info, argp, sizeof(info)))
6559 err = -EFAULT;
6560 else if (!(info.state & (1<<MD_DISK_SYNC)))
6561 /* Need to clear read-only for this */
6562 break;
6563 else
6564 err = add_new_disk(mddev, &info);
6565 goto done_unlock;
6566 }
6567 break;
6568
NeilBrownc02c0ae2012-12-11 13:39:21 +11006569 case BLKROSET:
6570 if (get_user(ro, (int __user *)(arg))) {
6571 err = -EFAULT;
6572 goto done_unlock;
6573 }
6574 err = -EINVAL;
6575
6576 /* if the bdev is going readonly the value of mddev->ro
6577 * does not matter, no writes are coming
6578 */
6579 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006580 goto done_unlock;
6581
NeilBrownc02c0ae2012-12-11 13:39:21 +11006582 /* are we are already prepared for writes? */
6583 if (mddev->ro != 1)
6584 goto done_unlock;
6585
6586 /* transitioning to readauto need only happen for
6587 * arrays that call md_write_start
6588 */
6589 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006591 if (err == 0) {
6592 mddev->ro = 2;
6593 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006594 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006595 }
6596 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 }
6598
6599 /*
6600 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006601 * superblock, so we do not allow them on read-only arrays.
6602 * However non-MD ioctls (e.g. get-size) will still come through
6603 * here and hit the 'default' below, so only disallow
6604 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006606 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006607 if (mddev->ro == 2) {
6608 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006609 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006610 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006611 /* mddev_unlock will wake thread */
6612 /* If a device failed while we were read-only, we
6613 * need to make sure the metadata is updated now.
6614 */
6615 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6616 mddev_unlock(mddev);
6617 wait_event(mddev->sb_wait,
6618 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6619 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006620 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006621 }
NeilBrownf91de922005-11-08 21:39:36 -08006622 } else {
6623 err = -EROFS;
6624 goto abort_unlock;
6625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 }
6627
NeilBrownc02c0ae2012-12-11 13:39:21 +11006628 switch (cmd) {
6629 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006631 mdu_disk_info_t info;
6632 if (copy_from_user(&info, argp, sizeof(info)))
6633 err = -EFAULT;
6634 else
6635 err = add_new_disk(mddev, &info);
6636 goto done_unlock;
6637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
NeilBrownc02c0ae2012-12-11 13:39:21 +11006639 case HOT_ADD_DISK:
6640 err = hot_add_disk(mddev, new_decode_dev(arg));
6641 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
NeilBrownc02c0ae2012-12-11 13:39:21 +11006643 case RUN_ARRAY:
6644 err = do_md_run(mddev);
6645 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
NeilBrownc02c0ae2012-12-11 13:39:21 +11006647 case SET_BITMAP_FILE:
6648 err = set_bitmap_file(mddev, (int)arg);
6649 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006650
NeilBrownc02c0ae2012-12-11 13:39:21 +11006651 default:
6652 err = -EINVAL;
6653 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 }
6655
6656done_unlock:
6657abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006658 if (mddev->hold_active == UNTIL_IOCTL &&
6659 err != -EINVAL)
6660 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 mddev_unlock(mddev);
6662
6663 return err;
6664done:
6665 if (err)
6666 MD_BUG();
6667abort:
6668 return err;
6669}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006670#ifdef CONFIG_COMPAT
6671static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6672 unsigned int cmd, unsigned long arg)
6673{
6674 switch (cmd) {
6675 case HOT_REMOVE_DISK:
6676 case HOT_ADD_DISK:
6677 case SET_DISK_FAULTY:
6678 case SET_BITMAP_FILE:
6679 /* These take in integer arg, do not convert */
6680 break;
6681 default:
6682 arg = (unsigned long)compat_ptr(arg);
6683 break;
6684 }
6685
6686 return md_ioctl(bdev, mode, cmd, arg);
6687}
6688#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689
Al Viroa39907f2008-03-02 10:31:15 -05006690static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691{
6692 /*
6693 * Succeed if we can lock the mddev, which confirms that
6694 * it isn't being stopped right now.
6695 */
NeilBrownfd01b882011-10-11 16:47:53 +11006696 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 int err;
6698
Yuanhan Liu0c098222012-05-22 13:55:32 +10006699 if (!mddev)
6700 return -ENODEV;
6701
NeilBrownd3374822009-01-09 08:31:10 +11006702 if (mddev->gendisk != bdev->bd_disk) {
6703 /* we are racing with mddev_put which is discarding this
6704 * bd_disk.
6705 */
6706 mddev_put(mddev);
6707 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006708 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006709 /* Then retry the open from the top */
6710 return -ERESTARTSYS;
6711 }
6712 BUG_ON(mddev != bdev->bd_disk->private_data);
6713
NeilBrownc8c00a62009-08-10 12:50:52 +10006714 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 goto out;
6716
6717 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006718 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006719 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006720 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006722 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 out:
6724 return err;
6725}
6726
Al Virodb2a1442013-05-05 21:52:57 -04006727static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728{
NeilBrownfd01b882011-10-11 16:47:53 +11006729 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006731 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006732 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006735
6736static int md_media_changed(struct gendisk *disk)
6737{
NeilBrownfd01b882011-10-11 16:47:53 +11006738 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006739
6740 return mddev->changed;
6741}
6742
6743static int md_revalidate(struct gendisk *disk)
6744{
NeilBrownfd01b882011-10-11 16:47:53 +11006745 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006746
6747 mddev->changed = 0;
6748 return 0;
6749}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006750static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751{
6752 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006753 .open = md_open,
6754 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006755 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006756#ifdef CONFIG_COMPAT
6757 .compat_ioctl = md_compat_ioctl,
6758#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006759 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006760 .media_changed = md_media_changed,
6761 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762};
6763
Adrian Bunk75c96f82005-05-05 16:16:09 -07006764static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765{
NeilBrown2b8bf342011-10-11 16:48:23 +11006766 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 /*
6769 * md_thread is a 'system-thread', it's priority should be very
6770 * high. We avoid resource deadlocks individually in each
6771 * raid personality. (RAID5 does preallocation) We also use RR and
6772 * the very same RT priority as kswapd, thus we will never get
6773 * into a priority inversion deadlock.
6774 *
6775 * we definitely have to have equal or higher priority than
6776 * bdflush, otherwise bdflush will deadlock if there are too
6777 * many dirty RAID5 blocks.
6778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
NeilBrown6985c432005-10-19 21:23:47 -07006780 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006781 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
NeilBrown93588e22005-11-15 00:09:12 -08006783 /* We need to wait INTERRUPTIBLE so that
6784 * we don't add to the load-average.
6785 * That means we need to be sure no signals are
6786 * pending
6787 */
6788 if (signal_pending(current))
6789 flush_signals(current);
6790
6791 wait_event_interruptible_timeout
6792 (thread->wqueue,
6793 test_bit(THREAD_WAKEUP, &thread->flags)
6794 || kthread_should_stop(),
6795 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
NeilBrown6c987912011-01-14 09:13:53 +11006797 clear_bit(THREAD_WAKEUP, &thread->flags);
6798 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006799 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006801
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 return 0;
6803}
6804
NeilBrown2b8bf342011-10-11 16:48:23 +11006805void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806{
6807 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006808 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 set_bit(THREAD_WAKEUP, &thread->flags);
6810 wake_up(&thread->wqueue);
6811 }
6812}
6813
Shaohua Li4ed87312012-10-11 13:34:00 +11006814struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6815 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816{
NeilBrown2b8bf342011-10-11 16:48:23 +11006817 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
NeilBrown2b8bf342011-10-11 16:48:23 +11006819 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 if (!thread)
6821 return NULL;
6822
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 init_waitqueue_head(&thread->wqueue);
6824
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 thread->run = run;
6826 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006827 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006828 thread->tsk = kthread_run(md_thread, thread,
6829 "%s_%s",
6830 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006831 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006832 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 kfree(thread);
6834 return NULL;
6835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 return thread;
6837}
6838
NeilBrown2b8bf342011-10-11 16:48:23 +11006839void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840{
NeilBrown2b8bf342011-10-11 16:48:23 +11006841 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006842 if (!thread)
6843 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006844 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006845 /* Locking ensures that mddev_unlock does not wake_up a
6846 * non-existent thread
6847 */
6848 spin_lock(&pers_lock);
6849 *threadp = NULL;
6850 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006851
6852 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 kfree(thread);
6854}
6855
NeilBrownfd01b882011-10-11 16:47:53 +11006856void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857{
6858 if (!mddev) {
6859 MD_BUG();
6860 return;
6861 }
6862
NeilBrownb2d444d2005-11-08 21:39:31 -08006863 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006865
NeilBrownde393cd2011-07-28 11:31:48 +10006866 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 return;
6868 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006869 if (mddev->degraded)
6870 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006871 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6873 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6874 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006875 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006876 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006877 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878}
6879
6880/* seq_file implementation /proc/mdstat */
6881
6882static void status_unused(struct seq_file *seq)
6883{
6884 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006885 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 seq_printf(seq, "unused devices: ");
6888
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006889 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 char b[BDEVNAME_SIZE];
6891 i++;
6892 seq_printf(seq, "%s ",
6893 bdevname(rdev->bdev,b));
6894 }
6895 if (!i)
6896 seq_printf(seq, "<none>");
6897
6898 seq_printf(seq, "\n");
6899}
6900
6901
NeilBrownfd01b882011-10-11 16:47:53 +11006902static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903{
NeilBrowndd71cf62009-05-07 12:49:35 +10006904 sector_t max_sectors, resync, res;
6905 unsigned long dt, db;
6906 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006907 int scale;
6908 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
NeilBrown72f36d52012-10-11 14:25:57 +11006910 if (mddev->curr_resync <= 3)
6911 resync = 0;
6912 else
6913 resync = mddev->curr_resync
6914 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
NeilBrownc804cde2012-05-21 09:28:33 +10006916 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6917 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006918 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006920 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922 /*
6923 * Should not happen.
6924 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006925 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 MD_BUG();
6927 return;
6928 }
NeilBrown4588b422006-03-27 01:18:04 -08006929 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006930 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006931 * u32, as those are the requirements for sector_div.
6932 * Thus 'scale' must be at least 10
6933 */
6934 scale = 10;
6935 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006936 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006937 scale++;
6938 }
6939 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006940 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006941
6942 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 {
NeilBrown4588b422006-03-27 01:18:04 -08006944 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 seq_printf(seq, "[");
6946 for (i = 0; i < x; i++)
6947 seq_printf(seq, "=");
6948 seq_printf(seq, ">");
6949 for (i = 0; i < y; i++)
6950 seq_printf(seq, ".");
6951 seq_printf(seq, "] ");
6952 }
NeilBrown4588b422006-03-27 01:18:04 -08006953 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006954 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6955 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006956 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6957 "check" :
6958 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6959 "resync" : "recovery"))),
6960 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006961 (unsigned long long) resync/2,
6962 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
6964 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 * dt: time from mark until now
6966 * db: blocks written from mark until now
6967 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006968 *
6969 * rt is a sector_t, so could be 32bit or 64bit.
6970 * So we divide before multiply in case it is 32bit and close
6971 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006972 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006973 * near the end of resync when the number of remaining sectors
6974 * is close to 'db'.
6975 * We then divide rt by 32 after multiplying by db to compensate.
6976 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 */
6978 dt = ((jiffies - mddev->resync_mark) / HZ);
6979 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006980 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6981 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
NeilBrowndd71cf62009-05-07 12:49:35 +10006983 rt = max_sectors - resync; /* number of remaining sectors */
6984 sector_div(rt, db/32+1);
6985 rt *= dt;
6986 rt >>= 5;
6987
6988 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6989 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
NeilBrownff4e8d92006-07-10 04:44:16 -07006991 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992}
6993
6994static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6995{
6996 struct list_head *tmp;
6997 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006998 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
7000 if (l >= 0x10000)
7001 return NULL;
7002 if (!l--)
7003 /* header */
7004 return (void*)1;
7005
7006 spin_lock(&all_mddevs_lock);
7007 list_for_each(tmp,&all_mddevs)
7008 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007009 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 mddev_get(mddev);
7011 spin_unlock(&all_mddevs_lock);
7012 return mddev;
7013 }
7014 spin_unlock(&all_mddevs_lock);
7015 if (!l--)
7016 return (void*)2;/* tail */
7017 return NULL;
7018}
7019
7020static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7021{
7022 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007023 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
7025 ++*pos;
7026 if (v == (void*)2)
7027 return NULL;
7028
7029 spin_lock(&all_mddevs_lock);
7030 if (v == (void*)1)
7031 tmp = all_mddevs.next;
7032 else
7033 tmp = mddev->all_mddevs.next;
7034 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007035 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 else {
7037 next_mddev = (void*)2;
7038 *pos = 0x10000;
7039 }
7040 spin_unlock(&all_mddevs_lock);
7041
7042 if (v != (void*)1)
7043 mddev_put(mddev);
7044 return next_mddev;
7045
7046}
7047
7048static void md_seq_stop(struct seq_file *seq, void *v)
7049{
NeilBrownfd01b882011-10-11 16:47:53 +11007050 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
7052 if (mddev && v != (void*)1 && v != (void*)2)
7053 mddev_put(mddev);
7054}
7055
7056static int md_seq_show(struct seq_file *seq, void *v)
7057{
NeilBrownfd01b882011-10-11 16:47:53 +11007058 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007059 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007060 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007063 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 seq_printf(seq, "Personalities : ");
7065 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007066 list_for_each_entry(pers, &pers_list, list)
7067 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
7069 spin_unlock(&pers_lock);
7070 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007071 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 return 0;
7073 }
7074 if (v == (void*)2) {
7075 status_unused(seq);
7076 return 0;
7077 }
7078
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007079 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7083 seq_printf(seq, "%s : %sactive", mdname(mddev),
7084 mddev->pers ? "" : "in");
7085 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007086 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007088 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007089 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 seq_printf(seq, " %s", mddev->pers->name);
7091 }
7092
Andre Nolldd8ac332009-03-31 14:33:13 +11007093 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007094 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 char b[BDEVNAME_SIZE];
7096 seq_printf(seq, " %s[%d]",
7097 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007098 if (test_bit(WriteMostly, &rdev->flags))
7099 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007100 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 seq_printf(seq, "(F)");
7102 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007103 }
7104 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007105 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007106 if (test_bit(Replacement, &rdev->flags))
7107 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007108 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 }
7110
7111 if (!list_empty(&mddev->disks)) {
7112 if (mddev->pers)
7113 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007114 (unsigned long long)
7115 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 else
7117 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007118 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007120 if (mddev->persistent) {
7121 if (mddev->major_version != 0 ||
7122 mddev->minor_version != 90) {
7123 seq_printf(seq," super %d.%d",
7124 mddev->major_version,
7125 mddev->minor_version);
7126 }
NeilBrowne6910632008-02-06 01:39:51 -08007127 } else if (mddev->external)
7128 seq_printf(seq, " super external:%s",
7129 mddev->metadata_type);
7130 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007131 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007134 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007136 if (mddev->pers->sync_request) {
7137 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007138 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007139 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007140 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007141 seq_printf(seq, "\tresync=DELAYED\n ");
7142 else if (mddev->recovery_cp < MaxSector)
7143 seq_printf(seq, "\tresync=PENDING\n ");
7144 }
NeilBrown32a76272005-06-21 17:17:14 -07007145 } else
7146 seq_printf(seq, "\n ");
7147
NeilBrown57148962012-03-19 12:46:40 +11007148 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
7150 seq_printf(seq, "\n");
7151 }
7152 mddev_unlock(mddev);
7153
7154 return 0;
7155}
7156
Jan Engelhardt110518b2009-05-07 12:49:37 +10007157static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 .start = md_seq_start,
7159 .next = md_seq_next,
7160 .stop = md_seq_stop,
7161 .show = md_seq_show,
7162};
7163
7164static int md_seq_open(struct inode *inode, struct file *file)
7165{
Kay Sieversf1514632011-07-12 20:48:39 +02007166 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 int error;
7168
7169 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007170 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007171 return error;
7172
7173 seq = file->private_data;
7174 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 return error;
7176}
7177
NeilBrowne2f23b62014-04-09 14:33:51 +10007178static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007179static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7180{
Kay Sieversf1514632011-07-12 20:48:39 +02007181 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007182 int mask;
7183
NeilBrowne2f23b62014-04-09 14:33:51 +10007184 if (md_unloading)
7185 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;;
NeilBrownd7603b72006-01-06 00:20:30 -08007186 poll_wait(filp, &md_event_waiters, wait);
7187
7188 /* always allow read */
7189 mask = POLLIN | POLLRDNORM;
7190
Kay Sieversf1514632011-07-12 20:48:39 +02007191 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007192 mask |= POLLERR | POLLPRI;
7193 return mask;
7194}
7195
Arjan van de Venfa027c22007-02-12 00:55:33 -08007196static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007197 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 .open = md_seq_open,
7199 .read = seq_read,
7200 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007201 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007202 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203};
7204
NeilBrown84fc4b52011-10-11 16:49:58 +11007205int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007208 list_add_tail(&p->list, &pers_list);
7209 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 spin_unlock(&pers_lock);
7211 return 0;
7212}
7213
NeilBrown84fc4b52011-10-11 16:49:58 +11007214int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215{
NeilBrown2604b702006-01-06 00:20:36 -08007216 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007218 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 spin_unlock(&pers_lock);
7220 return 0;
7221}
7222
NeilBrownfd01b882011-10-11 16:47:53 +11007223static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224{
NeilBrown3cb03002011-10-11 16:45:26 +11007225 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007227 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
7229 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007230 rcu_read_lock();
7231 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007233 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7234 (int)part_stat_read(&disk->part0, sectors[1]) -
7235 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007236 /* sync IO will cause sync_io to increase before the disk_stats
7237 * as sync_io is counted when a request starts, and
7238 * disk_stats is counted when it completes.
7239 * So resync activity will cause curr_events to be smaller than
7240 * when there was no such activity.
7241 * non-sync IO will cause disk_stat to increase without
7242 * increasing sync_io so curr_events will (eventually)
7243 * be larger than it was before. Once it becomes
7244 * substantially larger, the test below will cause
7245 * the array to appear non-idle, and resync will slow
7246 * down.
7247 * If there is a lot of outstanding resync activity when
7248 * we set last_event to curr_events, then all that activity
7249 * completing might cause the array to appear non-idle
7250 * and resync will be slowed down even though there might
7251 * not have been non-resync activity. This will only
7252 * happen once though. 'last_events' will soon reflect
7253 * the state where there is little or no outstanding
7254 * resync requests, and further resync activity will
7255 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007256 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007258 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 rdev->last_events = curr_events;
7260 idle = 0;
7261 }
7262 }
NeilBrown4b809912008-07-21 17:05:25 +10007263 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 return idle;
7265}
7266
NeilBrownfd01b882011-10-11 16:47:53 +11007267void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268{
7269 /* another "blocks" (512byte) blocks have been synced */
7270 atomic_sub(blocks, &mddev->recovery_active);
7271 wake_up(&mddev->recovery_wait);
7272 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007273 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007274 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 md_wakeup_thread(mddev->thread);
7276 // stop recovery, signal do_sync ....
7277 }
7278}
7279
7280
NeilBrown06d91a52005-06-21 17:17:12 -07007281/* md_write_start(mddev, bi)
7282 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007283 * in superblock) before writing, schedule a superblock update
7284 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007285 */
NeilBrownfd01b882011-10-11 16:47:53 +11007286void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287{
Neil Brown0fd62b82008-06-28 08:31:36 +10007288 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007289 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007290 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007291
NeilBrownf91de922005-11-08 21:39:36 -08007292 BUG_ON(mddev->ro == 1);
7293 if (mddev->ro == 2) {
7294 /* need to switch to read/write */
7295 mddev->ro = 0;
7296 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7297 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007298 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007299 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007300 }
NeilBrown06d91a52005-06-21 17:17:12 -07007301 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007302 if (mddev->safemode == 1)
7303 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007304 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007305 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007306 if (mddev->in_sync) {
7307 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007308 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007309 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007310 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007311 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007312 }
NeilBrowna9701a32005-11-08 21:39:34 -08007313 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007314 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007315 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007316 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007317 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007318 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319}
7320
NeilBrownfd01b882011-10-11 16:47:53 +11007321void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322{
7323 if (atomic_dec_and_test(&mddev->writes_pending)) {
7324 if (mddev->safemode == 2)
7325 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007326 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7328 }
7329}
7330
NeilBrown2a2275d2007-01-26 00:57:11 -08007331/* md_allow_write(mddev)
7332 * Calling this ensures that the array is marked 'active' so that writes
7333 * may proceed without blocking. It is important to call this before
7334 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7335 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007336 *
7337 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7338 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007339 */
NeilBrownfd01b882011-10-11 16:47:53 +11007340int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007341{
7342 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007343 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007344 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007345 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007346 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007347 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007348
7349 spin_lock_irq(&mddev->write_lock);
7350 if (mddev->in_sync) {
7351 mddev->in_sync = 0;
7352 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007353 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007354 if (mddev->safemode_delay &&
7355 mddev->safemode == 0)
7356 mddev->safemode = 1;
7357 spin_unlock_irq(&mddev->write_lock);
7358 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007359 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007360 } else
7361 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007362
NeilBrown070dc6d2010-08-30 17:33:34 +10007363 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007364 return -EAGAIN;
7365 else
7366 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007367}
7368EXPORT_SYMBOL_GPL(md_allow_write);
7369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370#define SYNC_MARKS 10
7371#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007372#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007373void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374{
Shaohua Li4ed87312012-10-11 13:34:00 +11007375 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007376 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 unsigned int currspeed = 0,
7378 window;
NeilBrown57afd892005-06-21 17:17:13 -07007379 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007381 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 sector_t mark_cnt[SYNC_MARKS];
7383 int last_mark,m;
7384 struct list_head *tmp;
7385 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007386 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007387 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007388 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007389 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
7391 /* just incase thread restarts... */
7392 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7393 return;
NeilBrown3991b312014-05-28 13:39:23 +10007394 if (mddev->ro) {/* never try to sync a read-only array */
7395 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007396 return;
NeilBrown3991b312014-05-28 13:39:23 +10007397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
NeilBrown61df9d92006-10-03 01:15:57 -07007399 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007400 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007401 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007402 action = "check";
7403 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007404 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007405 action = "repair";
7406 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007407 desc = "resync";
7408 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7409 desc = "reshape";
7410 else
7411 desc = "recovery";
7412
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007413 mddev->last_sync_action = action ?: desc;
7414
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415 /* we overload curr_resync somewhat here.
7416 * 0 == not engaged in resync at all
7417 * 2 == checking that there is no conflict with another sync
7418 * 1 == like 2, but have yielded to allow conflicting resync to
7419 * commense
7420 * other == active in resync - this many blocks
7421 *
7422 * Before starting a resync we must have set curr_resync to
7423 * 2, and then checked that every "conflicting" array has curr_resync
7424 * less than ours. When we find one that is the same or higher
7425 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7426 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7427 * This will mean we have to start checking from the beginning again.
7428 *
7429 */
7430
7431 do {
7432 mddev->curr_resync = 2;
7433
7434 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007435 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007437 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 if (mddev2 == mddev)
7439 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007440 if (!mddev->parallel_resync
7441 && mddev2->curr_resync
7442 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 DEFINE_WAIT(wq);
7444 if (mddev < mddev2 && mddev->curr_resync == 2) {
7445 /* arbitrarily yield */
7446 mddev->curr_resync = 1;
7447 wake_up(&resync_wait);
7448 }
7449 if (mddev > mddev2 && mddev->curr_resync == 1)
7450 /* no need to wait here, we can wait the next
7451 * time 'round when curr_resync == 2
7452 */
7453 continue;
NeilBrown97441972008-09-19 11:49:54 +10007454 /* We need to wait 'interruptible' so as not to
7455 * contribute to the load average, and not to
7456 * be caught by 'softlockup'
7457 */
7458 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007459 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007460 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007461 printk(KERN_INFO "md: delaying %s of %s"
7462 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007464 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007466 if (signal_pending(current))
7467 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 schedule();
7469 finish_wait(&resync_wait, &wq);
7470 goto try_again;
7471 }
7472 finish_wait(&resync_wait, &wq);
7473 }
7474 }
7475 } while (mddev->curr_resync < 2);
7476
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007477 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007478 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007480 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 */
7482 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007483 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007484 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007485 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7486 j = mddev->resync_min;
7487 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007488 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007489
NeilBrownccfcc3c2006-03-27 01:18:09 -08007490 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007491 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007492 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007494 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007495 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007496 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007497 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007498 if (rdev->raid_disk >= 0 &&
7499 !test_bit(Faulty, &rdev->flags) &&
7500 !test_bit(In_sync, &rdev->flags) &&
7501 rdev->recovery_offset < j)
7502 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007503 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505
NeilBrown61df9d92006-10-03 01:15:57 -07007506 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7507 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7508 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007509 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007510 "(but not more than %d KB/sec) for %s.\n",
7511 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
NeilBrowneea1bf32009-03-31 14:27:02 +11007513 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007514
NeilBrown57afd892005-06-21 17:17:13 -07007515 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 for (m = 0; m < SYNC_MARKS; m++) {
7517 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007518 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 }
7520 last_mark = 0;
7521 mddev->resync_mark = mark[last_mark];
7522 mddev->resync_mark_cnt = mark_cnt[last_mark];
7523
7524 /*
7525 * Tune reconstruction:
7526 */
7527 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007528 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7529 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
7531 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 last_check = 0;
7533
7534 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007535 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007536 "md: resuming %s of %s from checkpoint.\n",
7537 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007539 } else
7540 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007541 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007542 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7543 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007544 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
majianpeng7c2c57c2012-07-03 12:12:26 +10007546 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007548 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549
NeilBrown57afd892005-06-21 17:17:13 -07007550 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007551
NeilBrown7a91ee12009-05-26 12:57:21 +10007552 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7553 ((mddev->curr_resync > mddev->curr_resync_completed &&
7554 (mddev->curr_resync - mddev->curr_resync_completed)
7555 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007556 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007557 (j - mddev->curr_resync_completed)*2
7558 >= mddev->resync_max - mddev->curr_resync_completed
7559 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007560 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007561 wait_event(mddev->recovery_wait,
7562 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007563 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007564 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7565 j > mddev->recovery_cp)
7566 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007567 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007568 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007569 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007570 }
NeilBrownacb180b2009-04-14 16:28:34 +10007571
NeilBrownc91abf52013-11-19 12:02:01 +11007572 while (j >= mddev->resync_max &&
7573 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007574 /* As this condition is controlled by user-space,
7575 * we can block indefinitely, so use '_interruptible'
7576 * to avoid triggering warnings.
7577 */
7578 flush_signals(current); /* just in case */
7579 wait_event_interruptible(mddev->recovery_wait,
7580 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007581 || test_bit(MD_RECOVERY_INTR,
7582 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007583 }
NeilBrownacb180b2009-04-14 16:28:34 +10007584
NeilBrownc91abf52013-11-19 12:02:01 +11007585 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7586 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007587
NeilBrown57afd892005-06-21 17:17:13 -07007588 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007589 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007590 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007591 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 }
NeilBrown57afd892005-06-21 17:17:13 -07007594
7595 if (!skipped) { /* actual IO requested */
7596 io_sectors += sectors;
7597 atomic_add(sectors, &mddev->recovery_active);
7598 }
7599
NeilBrowne875ece2011-07-28 11:39:24 +10007600 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7601 break;
7602
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007604 if (j > 2)
7605 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007606 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007607 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007608 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007609 * visible in /proc/mdstat
7610 */
7611 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007612
7613 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 continue;
7615
NeilBrown57afd892005-06-21 17:17:13 -07007616 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 repeat:
7618 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7619 /* step marks */
7620 int next = (last_mark+1) % SYNC_MARKS;
7621
7622 mddev->resync_mark = mark[next];
7623 mddev->resync_mark_cnt = mark_cnt[next];
7624 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007625 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 last_mark = next;
7627 }
7628
NeilBrownc91abf52013-11-19 12:02:01 +11007629 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
7632 /*
7633 * this loop exits only if either when we are slower than
7634 * the 'hard' speed limit, or the system was IO-idle for
7635 * a jiffy.
7636 * the system might be non-idle CPU-wise, but we only care
7637 * about not overloading the IO subsystem. (things like an
7638 * e2fsck being done on the RAID array should execute fast)
7639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 cond_resched();
7641
NeilBrown57afd892005-06-21 17:17:13 -07007642 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7643 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
NeilBrown88202a02006-01-06 00:21:36 -08007645 if (currspeed > speed_min(mddev)) {
7646 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007647 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007648 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 goto repeat;
7650 }
7651 }
7652 }
NeilBrownc91abf52013-11-19 12:02:01 +11007653 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7654 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7655 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 /*
7657 * this also signals 'finished resyncing' to md_stop
7658 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007659 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7661
7662 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007663 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
NeilBrowndfc70642008-05-23 13:04:39 -07007665 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007666 mddev->curr_resync > 2) {
7667 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7668 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7669 if (mddev->curr_resync >= mddev->recovery_cp) {
7670 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007671 "md: checkpointing %s of %s.\n",
7672 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007673 if (test_bit(MD_RECOVERY_ERROR,
7674 &mddev->recovery))
7675 mddev->recovery_cp =
7676 mddev->curr_resync_completed;
7677 else
7678 mddev->recovery_cp =
7679 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007680 }
7681 } else
7682 mddev->recovery_cp = MaxSector;
7683 } else {
7684 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7685 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007686 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007687 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007688 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007689 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007690 !test_bit(Faulty, &rdev->flags) &&
7691 !test_bit(In_sync, &rdev->flags) &&
7692 rdev->recovery_offset < mddev->curr_resync)
7693 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007694 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007697 skip:
NeilBrown17571282006-12-10 02:20:52 -08007698 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
NeilBrownc07b70a2009-12-14 12:49:48 +11007700 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7701 /* We completed so min/max setting can be forgotten if used. */
7702 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7703 mddev->resync_min = 0;
7704 mddev->resync_max = MaxSector;
7705 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7706 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 mddev->curr_resync = 0;
7708 wake_up(&resync_wait);
7709 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7710 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712}
NeilBrown29269552006-03-27 01:18:10 -08007713EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714
NeilBrown746d3202013-04-24 11:42:41 +10007715static int remove_and_add_spares(struct mddev *mddev,
7716 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007717{
NeilBrown3cb03002011-10-11 16:45:26 +11007718 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007719 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007720 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007721
NeilBrowndafb20f2012-03-19 12:46:39 +11007722 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007723 if ((this == NULL || rdev == this) &&
7724 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007725 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007726 (test_bit(Faulty, &rdev->flags) ||
7727 ! test_bit(In_sync, &rdev->flags)) &&
7728 atomic_read(&rdev->nr_pending)==0) {
7729 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007730 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007731 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007732 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007733 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007734 }
7735 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007736 if (removed && mddev->kobj.sd)
7737 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007738
NeilBrown746d3202013-04-24 11:42:41 +10007739 if (this)
7740 goto no_add;
7741
NeilBrowndafb20f2012-03-19 12:46:39 +11007742 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007743 if (rdev->raid_disk >= 0 &&
7744 !test_bit(In_sync, &rdev->flags) &&
7745 !test_bit(Faulty, &rdev->flags))
7746 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007747 if (rdev->raid_disk >= 0)
7748 continue;
7749 if (test_bit(Faulty, &rdev->flags))
7750 continue;
7751 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007752 ! (rdev->saved_raid_disk >= 0 &&
7753 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007754 continue;
7755
NeilBrown7eb41882014-01-14 15:55:14 +11007756 if (rdev->saved_raid_disk < 0)
7757 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007758 if (mddev->pers->
7759 hot_add_disk(mddev, rdev) == 0) {
7760 if (sysfs_link_rdev(mddev, rdev))
7761 /* failure here is OK */;
7762 spares++;
7763 md_new_event(mddev);
7764 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007765 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007766 }
NeilBrown746d3202013-04-24 11:42:41 +10007767no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007768 if (removed)
7769 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007770 return spares;
7771}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007772
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773/*
7774 * This routine is regularly called by all per-raid-array threads to
7775 * deal with generic issues like resync and super-block update.
7776 * Raid personalities that don't have a thread (linear/raid0) do not
7777 * need this as they never do any recovery or update the superblock.
7778 *
7779 * It does not do any resync itself, but rather "forks" off other threads
7780 * to do that as needed.
7781 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7782 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007783 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 * and wakeups up this thread which will reap the thread and finish up.
7785 * This thread also removes any faulty devices (with nr_pending == 0).
7786 *
7787 * The overall approach is:
7788 * 1/ if the superblock needs updating, update it.
7789 * 2/ If a recovery thread is running, don't do anything else.
7790 * 3/ If recovery has finished, clean up, possibly marking spares active.
7791 * 4/ If there are any faulty devices, remove them.
7792 * 5/ If array is degraded, try to add spares devices
7793 * 6/ If array has spares or is not in-sync, start a resync thread.
7794 */
NeilBrownfd01b882011-10-11 16:47:53 +11007795void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007797 if (mddev->suspended)
7798 return;
7799
NeilBrown5f404022005-06-21 17:17:16 -07007800 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007801 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802
NeilBrownfca4d842005-06-21 17:17:11 -07007803 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007804 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007805 printk(KERN_INFO "md: %s in immediate safe mode\n",
7806 mdname(mddev));
7807 mddev->safemode = 2;
7808 }
7809 flush_signals(current);
7810 }
7811
NeilBrownc89a8ee2008-08-05 15:54:13 +10007812 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7813 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007815 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007817 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007818 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007819 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7820 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 ))
7822 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007823
NeilBrowndf5b89b2006-03-27 01:18:20 -08007824 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007825 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007826
NeilBrownc89a8ee2008-08-05 15:54:13 +10007827 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007828 /* On a read-only array we can:
7829 * - remove failed devices
7830 * - add already-in_sync devices if the array itself
7831 * is in-sync.
7832 * As we only add devices that are already in-sync,
7833 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007834 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007835 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007836 /* There is no thread, but we need to call
7837 * ->spare_active and clear saved_raid_disk
7838 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007839 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007840 md_reap_sync_thread(mddev);
7841 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007842 goto unlock;
7843 }
7844
NeilBrown31a59e32008-04-30 00:52:30 -07007845 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007846 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007847 spin_lock_irq(&mddev->write_lock);
7848 if (mddev->safemode &&
7849 !atomic_read(&mddev->writes_pending) &&
7850 !mddev->in_sync &&
7851 mddev->recovery_cp == MaxSector) {
7852 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007853 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007854 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007855 }
7856 if (mddev->safemode == 1)
7857 mddev->safemode = 0;
7858 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007859 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007860 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007861 }
NeilBrownfca4d842005-06-21 17:17:11 -07007862
NeilBrown7a0a5352013-08-27 16:28:23 +10007863 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007864 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007865
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7867 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7868 /* resync/recovery still happening */
7869 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7870 goto unlock;
7871 }
7872 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007873 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 goto unlock;
7875 }
Neil Brown72a23c22008-06-28 08:31:41 +10007876 /* Set RUNNING before clearing NEEDED to avoid
7877 * any transients in the value of "sync_action".
7878 */
NeilBrown72f36d52012-10-11 14:25:57 +11007879 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007880 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007881 /* Clear some bits that don't mean anything, but
7882 * might be left set
7883 */
NeilBrown24dd4692005-11-08 21:39:26 -08007884 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7885 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886
NeilBrowned209582012-04-24 10:23:14 +10007887 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7888 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007889 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007890 /* no recovery is running.
7891 * remove any failed drives, then
7892 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007893 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 * the personality to fail the re-add.
7895 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007897 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007898 if (mddev->pers->check_reshape == NULL ||
7899 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007900 /* Cannot proceed */
7901 goto unlock;
7902 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007903 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007904 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007905 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7906 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007907 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007908 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007909 } else if (mddev->recovery_cp < MaxSector) {
7910 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007911 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007912 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7913 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007915
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007917 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007918 /* We are adding a device or devices to an array
7919 * which has the bitmap stored on all devices.
7920 * So make sure all bitmap pages get written
7921 */
7922 bitmap_write_all(mddev->bitmap);
7923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 mddev->sync_thread = md_register_thread(md_do_sync,
7925 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007926 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 if (!mddev->sync_thread) {
7928 printk(KERN_ERR "%s: could not start resync"
7929 " thread...\n",
7930 mdname(mddev));
7931 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007932 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7933 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7934 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7935 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7936 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007937 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007939 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007940 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 }
7942 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007943 wake_up(&mddev->sb_wait);
7944
Neil Brown72a23c22008-06-28 08:31:41 +10007945 if (!mddev->sync_thread) {
7946 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7947 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7948 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007949 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007950 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952 mddev_unlock(mddev);
7953 }
7954}
7955
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007956void md_reap_sync_thread(struct mddev *mddev)
7957{
7958 struct md_rdev *rdev;
7959
7960 /* resync has finished, collect result */
7961 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007962 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007963 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7964 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7965 /* success...*/
7966 /* activate any spares */
7967 if (mddev->pers->spare_active(mddev)) {
7968 sysfs_notify(&mddev->kobj, NULL,
7969 "degraded");
7970 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7971 }
7972 }
7973 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7974 mddev->pers->finish_reshape)
7975 mddev->pers->finish_reshape(mddev);
7976
7977 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007978 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007979 */
NeilBrownf4667222013-12-09 12:04:56 +11007980 if (!mddev->degraded)
7981 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007982 rdev->saved_raid_disk = -1;
7983
7984 md_update_sb(mddev, 1);
7985 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7986 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7987 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7988 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7989 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7990 /* flag recovery needed just to double check */
7991 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7992 sysfs_notify_dirent_safe(mddev->sysfs_action);
7993 md_new_event(mddev);
7994 if (mddev->event_work.func)
7995 queue_work(md_misc_wq, &mddev->event_work);
7996}
7997
NeilBrownfd01b882011-10-11 16:47:53 +11007998void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007999{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008000 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008001 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008002 !test_bit(Blocked, &rdev->flags) &&
8003 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008004 msecs_to_jiffies(5000));
8005 rdev_dec_pending(rdev, mddev);
8006}
8007EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8008
NeilBrownc6563a82012-05-21 09:27:00 +10008009void md_finish_reshape(struct mddev *mddev)
8010{
8011 /* called be personality module when reshape completes. */
8012 struct md_rdev *rdev;
8013
8014 rdev_for_each(rdev, mddev) {
8015 if (rdev->data_offset > rdev->new_data_offset)
8016 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8017 else
8018 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8019 rdev->data_offset = rdev->new_data_offset;
8020 }
8021}
8022EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008023
8024/* Bad block management.
8025 * We can record which blocks on each device are 'bad' and so just
8026 * fail those blocks, or that stripe, rather than the whole device.
8027 * Entries in the bad-block table are 64bits wide. This comprises:
8028 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8029 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8030 * A 'shift' can be set so that larger blocks are tracked and
8031 * consequently larger devices can be covered.
8032 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8033 *
8034 * Locking of the bad-block table uses a seqlock so md_is_badblock
8035 * might need to retry if it is very unlucky.
8036 * We will sometimes want to check for bad blocks in a bi_end_io function,
8037 * so we use the write_seqlock_irq variant.
8038 *
8039 * When looking for a bad block we specify a range and want to
8040 * know if any block in the range is bad. So we binary-search
8041 * to the last range that starts at-or-before the given endpoint,
8042 * (or "before the sector after the target range")
8043 * then see if it ends after the given start.
8044 * We return
8045 * 0 if there are no known bad blocks in the range
8046 * 1 if there are known bad block which are all acknowledged
8047 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8048 * plus the start/length of the first bad section we overlap.
8049 */
8050int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8051 sector_t *first_bad, int *bad_sectors)
8052{
8053 int hi;
majianpengab056132012-11-06 17:13:44 +08008054 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008055 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008056 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008057 sector_t target = s + sectors;
8058 unsigned seq;
8059
8060 if (bb->shift > 0) {
8061 /* round the start down, and the end up */
8062 s >>= bb->shift;
8063 target += (1<<bb->shift) - 1;
8064 target >>= bb->shift;
8065 sectors = target - s;
8066 }
8067 /* 'target' is now the first block after the bad range */
8068
8069retry:
8070 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008071 lo = 0;
8072 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008073 hi = bb->count;
8074
8075 /* Binary search between lo and hi for 'target'
8076 * i.e. for the last range that starts before 'target'
8077 */
8078 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8079 * are known not to be the last range before target.
8080 * VARIANT: hi-lo is the number of possible
8081 * ranges, and decreases until it reaches 1
8082 */
8083 while (hi - lo > 1) {
8084 int mid = (lo + hi) / 2;
8085 sector_t a = BB_OFFSET(p[mid]);
8086 if (a < target)
8087 /* This could still be the one, earlier ranges
8088 * could not. */
8089 lo = mid;
8090 else
8091 /* This and later ranges are definitely out. */
8092 hi = mid;
8093 }
8094 /* 'lo' might be the last that started before target, but 'hi' isn't */
8095 if (hi > lo) {
8096 /* need to check all range that end after 's' to see if
8097 * any are unacknowledged.
8098 */
8099 while (lo >= 0 &&
8100 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8101 if (BB_OFFSET(p[lo]) < target) {
8102 /* starts before the end, and finishes after
8103 * the start, so they must overlap
8104 */
8105 if (rv != -1 && BB_ACK(p[lo]))
8106 rv = 1;
8107 else
8108 rv = -1;
8109 *first_bad = BB_OFFSET(p[lo]);
8110 *bad_sectors = BB_LEN(p[lo]);
8111 }
8112 lo--;
8113 }
8114 }
8115
8116 if (read_seqretry(&bb->lock, seq))
8117 goto retry;
8118
8119 return rv;
8120}
8121EXPORT_SYMBOL_GPL(md_is_badblock);
8122
8123/*
8124 * Add a range of bad blocks to the table.
8125 * This might extend the table, or might contract it
8126 * if two adjacent ranges can be merged.
8127 * We binary-search to find the 'insertion' point, then
8128 * decide how best to handle it.
8129 */
8130static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8131 int acknowledged)
8132{
8133 u64 *p;
8134 int lo, hi;
8135 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008136 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008137
8138 if (bb->shift < 0)
8139 /* badblocks are disabled */
8140 return 0;
8141
8142 if (bb->shift) {
8143 /* round the start down, and the end up */
8144 sector_t next = s + sectors;
8145 s >>= bb->shift;
8146 next += (1<<bb->shift) - 1;
8147 next >>= bb->shift;
8148 sectors = next - s;
8149 }
8150
Bian Yu905b0292013-10-12 01:10:03 -04008151 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008152
8153 p = bb->page;
8154 lo = 0;
8155 hi = bb->count;
8156 /* Find the last range that starts at-or-before 's' */
8157 while (hi - lo > 1) {
8158 int mid = (lo + hi) / 2;
8159 sector_t a = BB_OFFSET(p[mid]);
8160 if (a <= s)
8161 lo = mid;
8162 else
8163 hi = mid;
8164 }
8165 if (hi > lo && BB_OFFSET(p[lo]) > s)
8166 hi = lo;
8167
8168 if (hi > lo) {
8169 /* we found a range that might merge with the start
8170 * of our new range
8171 */
8172 sector_t a = BB_OFFSET(p[lo]);
8173 sector_t e = a + BB_LEN(p[lo]);
8174 int ack = BB_ACK(p[lo]);
8175 if (e >= s) {
8176 /* Yes, we can merge with a previous range */
8177 if (s == a && s + sectors >= e)
8178 /* new range covers old */
8179 ack = acknowledged;
8180 else
8181 ack = ack && acknowledged;
8182
8183 if (e < s + sectors)
8184 e = s + sectors;
8185 if (e - a <= BB_MAX_LEN) {
8186 p[lo] = BB_MAKE(a, e-a, ack);
8187 s = e;
8188 } else {
8189 /* does not all fit in one range,
8190 * make p[lo] maximal
8191 */
8192 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8193 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8194 s = a + BB_MAX_LEN;
8195 }
8196 sectors = e - s;
8197 }
8198 }
8199 if (sectors && hi < bb->count) {
8200 /* 'hi' points to the first range that starts after 's'.
8201 * Maybe we can merge with the start of that range */
8202 sector_t a = BB_OFFSET(p[hi]);
8203 sector_t e = a + BB_LEN(p[hi]);
8204 int ack = BB_ACK(p[hi]);
8205 if (a <= s + sectors) {
8206 /* merging is possible */
8207 if (e <= s + sectors) {
8208 /* full overlap */
8209 e = s + sectors;
8210 ack = acknowledged;
8211 } else
8212 ack = ack && acknowledged;
8213
8214 a = s;
8215 if (e - a <= BB_MAX_LEN) {
8216 p[hi] = BB_MAKE(a, e-a, ack);
8217 s = e;
8218 } else {
8219 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8220 s = a + BB_MAX_LEN;
8221 }
8222 sectors = e - s;
8223 lo = hi;
8224 hi++;
8225 }
8226 }
8227 if (sectors == 0 && hi < bb->count) {
8228 /* we might be able to combine lo and hi */
8229 /* Note: 's' is at the end of 'lo' */
8230 sector_t a = BB_OFFSET(p[hi]);
8231 int lolen = BB_LEN(p[lo]);
8232 int hilen = BB_LEN(p[hi]);
8233 int newlen = lolen + hilen - (s - a);
8234 if (s >= a && newlen < BB_MAX_LEN) {
8235 /* yes, we can combine them */
8236 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8237 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8238 memmove(p + hi, p + hi + 1,
8239 (bb->count - hi - 1) * 8);
8240 bb->count--;
8241 }
8242 }
8243 while (sectors) {
8244 /* didn't merge (it all).
8245 * Need to add a range just before 'hi' */
8246 if (bb->count >= MD_MAX_BADBLOCKS) {
8247 /* No room for more */
8248 rv = 0;
8249 break;
8250 } else {
8251 int this_sectors = sectors;
8252 memmove(p + hi + 1, p + hi,
8253 (bb->count - hi) * 8);
8254 bb->count++;
8255
8256 if (this_sectors > BB_MAX_LEN)
8257 this_sectors = BB_MAX_LEN;
8258 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8259 sectors -= this_sectors;
8260 s += this_sectors;
8261 }
8262 }
8263
8264 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008265 if (!acknowledged)
8266 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008267 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008268
8269 return rv;
8270}
8271
NeilBrown3cb03002011-10-11 16:45:26 +11008272int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008273 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008274{
NeilBrownc6563a82012-05-21 09:27:00 +10008275 int rv;
8276 if (is_new)
8277 s += rdev->new_data_offset;
8278 else
8279 s += rdev->data_offset;
8280 rv = md_set_badblocks(&rdev->badblocks,
8281 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008282 if (rv) {
8283 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008284 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008285 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8286 md_wakeup_thread(rdev->mddev->thread);
8287 }
8288 return rv;
8289}
8290EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8291
8292/*
8293 * Remove a range of bad blocks from the table.
8294 * This may involve extending the table if we spilt a region,
8295 * but it must not fail. So if the table becomes full, we just
8296 * drop the remove request.
8297 */
8298static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8299{
8300 u64 *p;
8301 int lo, hi;
8302 sector_t target = s + sectors;
8303 int rv = 0;
8304
8305 if (bb->shift > 0) {
8306 /* When clearing we round the start up and the end down.
8307 * This should not matter as the shift should align with
8308 * the block size and no rounding should ever be needed.
8309 * However it is better the think a block is bad when it
8310 * isn't than to think a block is not bad when it is.
8311 */
8312 s += (1<<bb->shift) - 1;
8313 s >>= bb->shift;
8314 target >>= bb->shift;
8315 sectors = target - s;
8316 }
8317
8318 write_seqlock_irq(&bb->lock);
8319
8320 p = bb->page;
8321 lo = 0;
8322 hi = bb->count;
8323 /* Find the last range that starts before 'target' */
8324 while (hi - lo > 1) {
8325 int mid = (lo + hi) / 2;
8326 sector_t a = BB_OFFSET(p[mid]);
8327 if (a < target)
8328 lo = mid;
8329 else
8330 hi = mid;
8331 }
8332 if (hi > lo) {
8333 /* p[lo] is the last range that could overlap the
8334 * current range. Earlier ranges could also overlap,
8335 * but only this one can overlap the end of the range.
8336 */
8337 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8338 /* Partial overlap, leave the tail of this range */
8339 int ack = BB_ACK(p[lo]);
8340 sector_t a = BB_OFFSET(p[lo]);
8341 sector_t end = a + BB_LEN(p[lo]);
8342
8343 if (a < s) {
8344 /* we need to split this range */
8345 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008346 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008347 goto out;
8348 }
8349 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8350 bb->count++;
8351 p[lo] = BB_MAKE(a, s-a, ack);
8352 lo++;
8353 }
8354 p[lo] = BB_MAKE(target, end - target, ack);
8355 /* there is no longer an overlap */
8356 hi = lo;
8357 lo--;
8358 }
8359 while (lo >= 0 &&
8360 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8361 /* This range does overlap */
8362 if (BB_OFFSET(p[lo]) < s) {
8363 /* Keep the early parts of this range. */
8364 int ack = BB_ACK(p[lo]);
8365 sector_t start = BB_OFFSET(p[lo]);
8366 p[lo] = BB_MAKE(start, s - start, ack);
8367 /* now low doesn't overlap, so.. */
8368 break;
8369 }
8370 lo--;
8371 }
8372 /* 'lo' is strictly before, 'hi' is strictly after,
8373 * anything between needs to be discarded
8374 */
8375 if (hi - lo > 1) {
8376 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8377 bb->count -= (hi - lo - 1);
8378 }
8379 }
8380
8381 bb->changed = 1;
8382out:
8383 write_sequnlock_irq(&bb->lock);
8384 return rv;
8385}
8386
NeilBrownc6563a82012-05-21 09:27:00 +10008387int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8388 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008389{
NeilBrownc6563a82012-05-21 09:27:00 +10008390 if (is_new)
8391 s += rdev->new_data_offset;
8392 else
8393 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008394 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008395 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008396}
8397EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8398
8399/*
8400 * Acknowledge all bad blocks in a list.
8401 * This only succeeds if ->changed is clear. It is used by
8402 * in-kernel metadata updates
8403 */
8404void md_ack_all_badblocks(struct badblocks *bb)
8405{
8406 if (bb->page == NULL || bb->changed)
8407 /* no point even trying */
8408 return;
8409 write_seqlock_irq(&bb->lock);
8410
majianpengecb178b2012-03-19 12:46:42 +11008411 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008412 u64 *p = bb->page;
8413 int i;
8414 for (i = 0; i < bb->count ; i++) {
8415 if (!BB_ACK(p[i])) {
8416 sector_t start = BB_OFFSET(p[i]);
8417 int len = BB_LEN(p[i]);
8418 p[i] = BB_MAKE(start, len, 1);
8419 }
8420 }
NeilBrownde393cd2011-07-28 11:31:48 +10008421 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008422 }
8423 write_sequnlock_irq(&bb->lock);
8424}
8425EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8426
NeilBrown16c791a2011-07-28 11:31:47 +10008427/* sysfs access to bad-blocks list.
8428 * We present two files.
8429 * 'bad-blocks' lists sector numbers and lengths of ranges that
8430 * are recorded as bad. The list is truncated to fit within
8431 * the one-page limit of sysfs.
8432 * Writing "sector length" to this file adds an acknowledged
8433 * bad block list.
8434 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8435 * been acknowledged. Writing to this file adds bad blocks
8436 * without acknowledging them. This is largely for testing.
8437 */
8438
8439static ssize_t
8440badblocks_show(struct badblocks *bb, char *page, int unack)
8441{
8442 size_t len;
8443 int i;
8444 u64 *p = bb->page;
8445 unsigned seq;
8446
8447 if (bb->shift < 0)
8448 return 0;
8449
8450retry:
8451 seq = read_seqbegin(&bb->lock);
8452
8453 len = 0;
8454 i = 0;
8455
8456 while (len < PAGE_SIZE && i < bb->count) {
8457 sector_t s = BB_OFFSET(p[i]);
8458 unsigned int length = BB_LEN(p[i]);
8459 int ack = BB_ACK(p[i]);
8460 i++;
8461
8462 if (unack && ack)
8463 continue;
8464
8465 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8466 (unsigned long long)s << bb->shift,
8467 length << bb->shift);
8468 }
NeilBrownde393cd2011-07-28 11:31:48 +10008469 if (unack && len == 0)
8470 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008471
8472 if (read_seqretry(&bb->lock, seq))
8473 goto retry;
8474
8475 return len;
8476}
8477
8478#define DO_DEBUG 1
8479
8480static ssize_t
8481badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8482{
8483 unsigned long long sector;
8484 int length;
8485 char newline;
8486#ifdef DO_DEBUG
8487 /* Allow clearing via sysfs *only* for testing/debugging.
8488 * Normally only a successful write may clear a badblock
8489 */
8490 int clear = 0;
8491 if (page[0] == '-') {
8492 clear = 1;
8493 page++;
8494 }
8495#endif /* DO_DEBUG */
8496
8497 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8498 case 3:
8499 if (newline != '\n')
8500 return -EINVAL;
8501 case 2:
8502 if (length <= 0)
8503 return -EINVAL;
8504 break;
8505 default:
8506 return -EINVAL;
8507 }
8508
8509#ifdef DO_DEBUG
8510 if (clear) {
8511 md_clear_badblocks(bb, sector, length);
8512 return len;
8513 }
8514#endif /* DO_DEBUG */
8515 if (md_set_badblocks(bb, sector, length, !unack))
8516 return len;
8517 else
8518 return -ENOSPC;
8519}
8520
Adrian Bunk75c96f82005-05-05 16:16:09 -07008521static int md_notify_reboot(struct notifier_block *this,
8522 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523{
8524 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008525 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008526 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527
NeilBrownc744a652012-03-19 12:46:37 +11008528 for_each_mddev(mddev, tmp) {
8529 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008530 if (mddev->pers)
8531 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008532 if (mddev->persistent)
8533 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008534 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008535 }
NeilBrownc744a652012-03-19 12:46:37 +11008536 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 }
NeilBrownc744a652012-03-19 12:46:37 +11008538 /*
8539 * certain more exotic SCSI devices are known to be
8540 * volatile wrt too early system reboots. While the
8541 * right place to handle this issue is the given
8542 * driver, we do want to have a safe RAID driver ...
8543 */
8544 if (need_delay)
8545 mdelay(1000*1);
8546
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 return NOTIFY_DONE;
8548}
8549
Adrian Bunk75c96f82005-05-05 16:16:09 -07008550static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 .notifier_call = md_notify_reboot,
8552 .next = NULL,
8553 .priority = INT_MAX, /* before any real devices */
8554};
8555
8556static void md_geninit(void)
8557{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008558 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008560 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561}
8562
Adrian Bunk75c96f82005-05-05 16:16:09 -07008563static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008564{
Tejun Heoe804ac72010-10-15 15:36:08 +02008565 int ret = -ENOMEM;
8566
Tejun Heoada609e2011-01-25 14:35:54 +01008567 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008568 if (!md_wq)
8569 goto err_wq;
8570
8571 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8572 if (!md_misc_wq)
8573 goto err_misc_wq;
8574
8575 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8576 goto err_md;
8577
8578 if ((ret = register_blkdev(0, "mdp")) < 0)
8579 goto err_mdp;
8580 mdp_major = ret;
8581
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008582 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008583 md_probe, NULL, NULL);
8584 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008585 md_probe, NULL, NULL);
8586
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008588 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
8590 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Tejun Heoe804ac72010-10-15 15:36:08 +02008593err_mdp:
8594 unregister_blkdev(MD_MAJOR, "md");
8595err_md:
8596 destroy_workqueue(md_misc_wq);
8597err_misc_wq:
8598 destroy_workqueue(md_wq);
8599err_wq:
8600 return ret;
8601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602
8603#ifndef MODULE
8604
8605/*
8606 * Searches all registered partitions for autorun RAID arrays
8607 * at boot time.
8608 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008609
8610static LIST_HEAD(all_detected_devices);
8611struct detected_devices_node {
8612 struct list_head list;
8613 dev_t dev;
8614};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615
8616void md_autodetect_dev(dev_t dev)
8617{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008618 struct detected_devices_node *node_detected_dev;
8619
8620 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8621 if (node_detected_dev) {
8622 node_detected_dev->dev = dev;
8623 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8624 } else {
8625 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8626 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628}
8629
8630
8631static void autostart_arrays(int part)
8632{
NeilBrown3cb03002011-10-11 16:45:26 +11008633 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008634 struct detected_devices_node *node_detected_dev;
8635 dev_t dev;
8636 int i_scanned, i_passed;
8637
8638 i_scanned = 0;
8639 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640
8641 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8642
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008643 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8644 i_scanned++;
8645 node_detected_dev = list_entry(all_detected_devices.next,
8646 struct detected_devices_node, list);
8647 list_del(&node_detected_dev->list);
8648 dev = node_detected_dev->dev;
8649 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008650 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 if (IS_ERR(rdev))
8652 continue;
8653
NeilBrownb2d444d2005-11-08 21:39:31 -08008654 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 MD_BUG();
8656 continue;
8657 }
NeilBrownd0fae182008-03-04 14:29:31 -08008658 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008660 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008661 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008662
8663 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8664 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
8666 autorun_devices(part);
8667}
8668
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008669#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670
8671static __exit void md_exit(void)
8672{
NeilBrownfd01b882011-10-11 16:47:53 +11008673 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008675 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008676
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008677 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008678 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008680 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 unregister_blkdev(mdp_major, "mdp");
8682 unregister_reboot_notifier(&md_notifier);
8683 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008684
8685 /* We cannot unload the modules while some process is
8686 * waiting for us in select() or poll() - wake them up
8687 */
8688 md_unloading = 1;
8689 while (waitqueue_active(&md_event_waiters)) {
8690 /* not safe to leave yet */
8691 wake_up(&md_event_waiters);
8692 msleep(delay);
8693 delay += delay;
8694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008696
NeilBrown29ac4aa2008-02-06 01:39:58 -08008697 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008699 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008701 destroy_workqueue(md_misc_wq);
8702 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703}
8704
Dan Williams685784a2007-07-09 11:56:42 -07008705subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706module_exit(md_exit)
8707
NeilBrownf91de922005-11-08 21:39:36 -08008708static int get_ro(char *buffer, struct kernel_param *kp)
8709{
8710 return sprintf(buffer, "%d", start_readonly);
8711}
8712static int set_ro(const char *val, struct kernel_param *kp)
8713{
8714 char *e;
8715 int num = simple_strtoul(val, &e, 10);
8716 if (*val && (*e == '\0' || *e == '\n')) {
8717 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008718 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008719 }
8720 return -EINVAL;
8721}
8722
NeilBrown80ca3a42006-07-10 04:44:18 -07008723module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8724module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008725
NeilBrownefeb53c2009-01-09 08:31:10 +11008726module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008727
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728EXPORT_SYMBOL(register_md_personality);
8729EXPORT_SYMBOL(unregister_md_personality);
8730EXPORT_SYMBOL(md_error);
8731EXPORT_SYMBOL(md_done_sync);
8732EXPORT_SYMBOL(md_write_start);
8733EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734EXPORT_SYMBOL(md_register_thread);
8735EXPORT_SYMBOL(md_unregister_thread);
8736EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008738EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008740MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008741MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008742MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);