blob: 9c33f052c848a6c7f108b84de2f1ba7481d2f09f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
NeilBrownf72ffdd2014-09-30 14:23:59 +10003 Copyright (C) 1998, 1999, 2000 Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Robert Becker1e509152009-12-14 12:49:58 +110079 * Default number of read corrections we'll attempt on an rdev
80 * before ejecting it from the array. We divide the read error
81 * count by 2 for every hour elapsed between read errors.
82 */
83#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
86 * is 1000 KB/sec, so the extra system load does not show up that much.
87 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070088 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * subsystem is idle. There is also an 'absolute maximum' reconstruction
90 * speed limit - in case reconstruction slows down your system despite
91 * idle IO detection.
92 *
93 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080094 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96
97static int sysctl_speed_limit_min = 1000;
98static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110099static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800100{
101 return mddev->sync_speed_min ?
102 mddev->sync_speed_min : sysctl_speed_limit_min;
103}
104
NeilBrownfd01b882011-10-11 16:47:53 +1100105static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800106{
107 return mddev->sync_speed_max ?
108 mddev->sync_speed_max : sysctl_speed_limit_max;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static struct ctl_table_header *raid_table_header;
112
Joe Perches82592c32013-11-14 15:16:18 +1100113static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .procname = "speed_limit_min",
116 .data = &sysctl_speed_limit_min,
117 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700118 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800119 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 },
121 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .procname = "speed_limit_max",
123 .data = &sysctl_speed_limit_max,
124 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700125 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800126 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800128 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Joe Perches82592c32013-11-14 15:16:18 +1100131static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .procname = "raid",
134 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700135 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .child = raid_table,
137 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800138 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Joe Perches82592c32013-11-14 15:16:18 +1100141static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .procname = "dev",
144 .maxlen = 0,
145 .mode = 0555,
146 .child = raid_dir_table,
147 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800148 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700151static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
NeilBrownf91de922005-11-08 21:39:36 -0800153static int start_readonly;
154
NeilBrowna167f662010-10-26 18:31:13 +1100155/* bio_clone_mddev
156 * like bio_clone, but with a local bio set
157 */
158
NeilBrowna167f662010-10-26 18:31:13 +1100159struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100161{
162 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100163
164 if (!mddev || !mddev->bio_set)
165 return bio_alloc(gfp_mask, nr_iovecs);
166
Kent Overstreet395c72a2012-09-06 15:34:55 -0700167 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100168 if (!b)
169 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100170 return b;
171}
172EXPORT_SYMBOL_GPL(bio_alloc_mddev);
173
174struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100175 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100176{
NeilBrowna167f662010-10-26 18:31:13 +1100177 if (!mddev || !mddev->bio_set)
178 return bio_clone(bio, gfp_mask);
179
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700180 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100181}
182EXPORT_SYMBOL_GPL(bio_clone_mddev);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
NeilBrownd7603b72006-01-06 00:20:30 -0800185 * We have a system wide 'event count' that is incremented
186 * on any 'interesting' event, and readers of /proc/mdstat
187 * can use 'poll' or 'select' to find out when the event
188 * count increases.
189 *
190 * Events are:
191 * start array, stop array, error, add device, remove device,
192 * start build, activate spare
193 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800194static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800195static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100196void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800197{
198 atomic_inc(&md_event_count);
199 wake_up(&md_event_waiters);
200}
NeilBrown29269552006-03-27 01:18:10 -0800201EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800202
NeilBrownc331eb02006-05-30 21:27:13 -0700203/* Alternate version that can be called from interrupts
204 * when calling sysfs_notify isn't needed.
205 */
NeilBrownfd01b882011-10-11 16:47:53 +1100206static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700207{
208 atomic_inc(&md_event_count);
209 wake_up(&md_event_waiters);
210}
211
NeilBrownd7603b72006-01-06 00:20:30 -0800212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * Enables to iterate over all existing md arrays
214 * all_mddevs_lock protects this list.
215 */
216static LIST_HEAD(all_mddevs);
217static DEFINE_SPINLOCK(all_mddevs_lock);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * iterates through all used mddevs in the system.
221 * We take care to grab the all_mddevs_lock whenever navigating
222 * the list, and to always hold a refcount when unlocked.
223 * Any code which breaks out of this loop while own
224 * a reference to the current mddev and must mddev_put it.
225 */
NeilBrownfd01b882011-10-11 16:47:53 +1100226#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000228 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100229 _tmp = all_mddevs.next; \
230 _mddev = NULL;}); \
231 ({ if (_tmp != &all_mddevs) \
232 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100234 if (_mddev) mddev_put(_mddev); \
235 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
236 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100238 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 )
240
NeilBrown409c57f2009-03-31 14:39:39 +1100241/* Rather than calling directly into the personality make_request function,
242 * IO requests come here first so that we can check if the device is
243 * being suspended pending a reconfiguration.
244 * We hold a refcount over the call to ->make_request. By the time that
245 * call has finished, the bio has been linked into some internal structure
246 * and so is visible to ->quiesce(), so we don't need the refcount any more.
247 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200248static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
NeilBrown49077322010-03-25 16:20:56 +1100250 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100251 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100252 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500253 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100254
NeilBrown0ca69882011-01-14 09:14:33 +1100255 if (mddev == NULL || mddev->pers == NULL
256 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100257 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200258 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100259 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100260 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
261 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
262 return;
263 }
NeilBrown0ca69882011-01-14 09:14:33 +1100264 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100265 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200266 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100267 DEFINE_WAIT(__wait);
268 for (;;) {
269 prepare_to_wait(&mddev->sb_wait, &__wait,
270 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200271 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100272 break;
273 rcu_read_unlock();
274 schedule();
275 rcu_read_lock();
276 }
277 finish_wait(&mddev->sb_wait, &__wait);
278 }
279 atomic_inc(&mddev->active_io);
280 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100281
Chris Masone91ece52011-02-07 19:21:48 -0500282 /*
283 * save the sectors now since our bio can
284 * go away inside make_request
285 */
286 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200287 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100288
289 cpu = part_stat_lock();
290 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500291 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100292 part_stat_unlock();
293
NeilBrown409c57f2009-03-31 14:39:39 +1100294 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
295 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
NeilBrown9e35b992010-04-06 14:23:02 +1000298/* mddev_suspend makes sure no new requests are submitted
299 * to the device, and that any requests that have been submitted
300 * are completely handled.
301 * Once ->stop is called and completes, the module will be completely
302 * unused.
303 */
NeilBrownfd01b882011-10-11 16:47:53 +1100304void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100305{
306 BUG_ON(mddev->suspended);
307 mddev->suspended = 1;
308 synchronize_rcu();
309 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
310 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500311
312 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100313}
NeilBrown390ee602010-06-01 19:37:27 +1000314EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100315
NeilBrownfd01b882011-10-11 16:47:53 +1100316void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100317{
318 mddev->suspended = 0;
319 wake_up(&mddev->sb_wait);
320 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500321
Jonathan Brassow47525e52012-05-22 13:55:29 +1000322 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500323 md_wakeup_thread(mddev->thread);
324 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100325}
NeilBrown390ee602010-06-01 19:37:27 +1000326EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100327
NeilBrownfd01b882011-10-11 16:47:53 +1100328int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000329{
330 return mddev->suspended;
331}
332EXPORT_SYMBOL(mddev_congested);
333
NeilBrowna2826aa2009-12-14 12:49:49 +1100334/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200335 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100336 */
337
Tejun Heoe9c74692010-09-03 11:56:18 +0200338static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100339{
NeilBrown3cb03002011-10-11 16:45:26 +1100340 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100341 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100342
343 rdev_dec_pending(rdev, mddev);
344
345 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200346 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200347 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100348 }
349 bio_put(bio);
350}
351
NeilBrowna7a07e62010-12-09 16:04:25 +1100352static void md_submit_flush_data(struct work_struct *ws);
353
NeilBrowna035fc32010-12-09 16:17:51 +1100354static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100355{
NeilBrownfd01b882011-10-11 16:47:53 +1100356 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100357 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100358
NeilBrowna7a07e62010-12-09 16:04:25 +1100359 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
360 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100361 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100362 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100363 if (rdev->raid_disk >= 0 &&
364 !test_bit(Faulty, &rdev->flags)) {
365 /* Take two references, one is dropped
366 * when request finishes, one after
367 * we reclaim rcu_read_lock
368 */
369 struct bio *bi;
370 atomic_inc(&rdev->nr_pending);
371 atomic_inc(&rdev->nr_pending);
372 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000373 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200374 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100375 bi->bi_private = rdev;
376 bi->bi_bdev = rdev->bdev;
377 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 rcu_read_lock();
380 rdev_dec_pending(rdev, mddev);
381 }
382 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100383 if (atomic_dec_and_test(&mddev->flush_pending))
384 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100385}
386
Tejun Heoe9c74692010-09-03 11:56:18 +0200387static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100388{
NeilBrownfd01b882011-10-11 16:47:53 +1100389 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200390 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100391
Kent Overstreet4f024f32013-10-11 15:44:27 -0700392 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 /* an empty barrier - all done */
394 bio_endio(bio, 0);
395 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200397 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100398 }
NeilBrown2b74e122010-12-09 15:59:01 +1100399
400 mddev->flush_bio = NULL;
401 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402}
403
NeilBrownfd01b882011-10-11 16:47:53 +1100404void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100405{
406 spin_lock_irq(&mddev->write_lock);
407 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200408 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100409 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 spin_unlock_irq(&mddev->write_lock);
412
NeilBrowna035fc32010-12-09 16:17:51 +1100413 INIT_WORK(&mddev->flush_work, submit_flushes);
414 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100415}
Tejun Heoe9c74692010-09-03 11:56:18 +0200416EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100417
NeilBrown74018dc2012-07-31 09:08:15 +0200418void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000419{
NeilBrown9cbb1752012-07-31 09:08:14 +0200420 struct mddev *mddev = cb->data;
421 md_wakeup_thread(mddev->thread);
422 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000423}
NeilBrown9cbb1752012-07-31 09:08:14 +0200424EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000425
NeilBrownfd01b882011-10-11 16:47:53 +1100426static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 atomic_inc(&mddev->active);
429 return mddev;
430}
431
Dan Williams5fd3a172009-03-04 00:57:25 -0700432static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100433
NeilBrownfd01b882011-10-11 16:47:53 +1100434static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
NeilBrowna167f662010-10-26 18:31:13 +1100436 struct bio_set *bs = NULL;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
439 return;
NeilBrownd3374822009-01-09 08:31:10 +1100440 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100441 mddev->ctime == 0 && !mddev->hold_active) {
442 /* Array is not configured at all, and not held active,
443 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100444 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100445 bs = mddev->bio_set;
446 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100447 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200448 /* We did a probe so need to clean up. Call
449 * queue_work inside the spinlock so that
450 * flush_workqueue() after mddev_find will
451 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100452 */
453 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200454 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100455 } else
456 kfree(mddev);
457 }
458 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100459 if (bs)
460 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
NeilBrownfd01b882011-10-11 16:47:53 +1100463void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100464{
465 mutex_init(&mddev->open_mutex);
466 mutex_init(&mddev->reconfig_mutex);
467 mutex_init(&mddev->bitmap_info.mutex);
468 INIT_LIST_HEAD(&mddev->disks);
469 INIT_LIST_HEAD(&mddev->all_mddevs);
470 init_timer(&mddev->safemode_timer);
471 atomic_set(&mddev->active, 1);
472 atomic_set(&mddev->openers, 0);
473 atomic_set(&mddev->active_io, 0);
474 spin_lock_init(&mddev->write_lock);
475 atomic_set(&mddev->flush_pending, 0);
476 init_waitqueue_head(&mddev->sb_wait);
477 init_waitqueue_head(&mddev->recovery_wait);
478 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000479 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500480 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100481 mddev->resync_min = 0;
482 mddev->resync_max = MaxSector;
483 mddev->level = LEVEL_NONE;
484}
NeilBrown390ee602010-06-01 19:37:27 +1000485EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100486
NeilBrownf72ffdd2014-09-30 14:23:59 +1000487static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
NeilBrownfd01b882011-10-11 16:47:53 +1100489 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
NeilBrown8f5f02c2011-02-16 13:58:51 +1100491 if (unit && MAJOR(unit) != MD_MAJOR)
492 unit &= ~((1<<MdpMinorShift)-1);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 retry:
495 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
NeilBrownefeb53c2009-01-09 08:31:10 +1100497 if (unit) {
498 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
499 if (mddev->unit == unit) {
500 mddev_get(mddev);
501 spin_unlock(&all_mddevs_lock);
502 kfree(new);
503 return mddev;
504 }
505
506 if (new) {
507 list_add(&new->all_mddevs, &all_mddevs);
508 spin_unlock(&all_mddevs_lock);
509 new->hold_active = UNTIL_IOCTL;
510 return new;
511 }
512 } else if (new) {
513 /* find an unused unit number */
514 static int next_minor = 512;
515 int start = next_minor;
516 int is_free = 0;
517 int dev = 0;
518 while (!is_free) {
519 dev = MKDEV(MD_MAJOR, next_minor);
520 next_minor++;
521 if (next_minor > MINORMASK)
522 next_minor = 0;
523 if (next_minor == start) {
524 /* Oh dear, all in use. */
525 spin_unlock(&all_mddevs_lock);
526 kfree(new);
527 return NULL;
528 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000529
NeilBrownefeb53c2009-01-09 08:31:10 +1100530 is_free = 1;
531 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
532 if (mddev->unit == dev) {
533 is_free = 0;
534 break;
535 }
536 }
537 new->unit = dev;
538 new->md_minor = MINOR(dev);
539 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 list_add(&new->all_mddevs, &all_mddevs);
541 spin_unlock(&all_mddevs_lock);
542 return new;
543 }
544 spin_unlock(&all_mddevs_lock);
545
NeilBrown9ffae0c2006-01-06 00:20:32 -0800546 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!new)
548 return NULL;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 new->unit = unit;
551 if (MAJOR(unit) == MD_MAJOR)
552 new->md_minor = MINOR(unit);
553 else
554 new->md_minor = MINOR(unit) >> MdpMinorShift;
555
NeilBrownfafd7fb2010-04-01 15:55:30 +1100556 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto retry;
559}
560
NeilBrownf72ffdd2014-09-30 14:23:59 +1000561static inline int __must_check mddev_lock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800563 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
NeilBrown29f097c2013-11-14 17:54:51 +1100566/* Sometimes we need to take the lock in a situation where
567 * failure due to interrupts is not acceptable.
568 */
NeilBrownf72ffdd2014-09-30 14:23:59 +1000569static inline void mddev_lock_nointr(struct mddev *mddev)
NeilBrown29f097c2013-11-14 17:54:51 +1100570{
571 mutex_lock(&mddev->reconfig_mutex);
572}
573
NeilBrownfd01b882011-10-11 16:47:53 +1100574static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100575{
576 return mutex_is_locked(&mddev->reconfig_mutex);
577}
578
NeilBrownf72ffdd2014-09-30 14:23:59 +1000579static inline int mddev_trylock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800581 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
NeilBrownb6eb1272010-04-15 10:13:47 +1000584static struct attribute_group md_redundancy_group;
585
NeilBrownf72ffdd2014-09-30 14:23:59 +1000586static void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
NeilBrowna64c8762010-04-14 17:15:37 +1000588 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000589 /* These cannot be removed under reconfig_mutex as
590 * an access to the files will try to take reconfig_mutex
591 * while holding the file unremovable, which leads to
592 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000593 * So hold set sysfs_active while the remove in happeing,
594 * and anything else which might set ->to_remove or my
595 * otherwise change the sysfs namespace will fail with
596 * -EBUSY if sysfs_active is still set.
597 * We set sysfs_active under reconfig_mutex and elsewhere
598 * test it under the same mutex to ensure its correct value
599 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000600 */
NeilBrowna64c8762010-04-14 17:15:37 +1000601 struct attribute_group *to_remove = mddev->to_remove;
602 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000603 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 mutex_unlock(&mddev->reconfig_mutex);
605
NeilBrown00bcb4a2010-06-01 19:37:23 +1000606 if (mddev->kobj.sd) {
607 if (to_remove != &md_redundancy_group)
608 sysfs_remove_group(&mddev->kobj, to_remove);
609 if (mddev->pers == NULL ||
610 mddev->pers->sync_request == NULL) {
611 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
612 if (mddev->sysfs_action)
613 sysfs_put(mddev->sysfs_action);
614 mddev->sysfs_action = NULL;
615 }
NeilBrowna64c8762010-04-14 17:15:37 +1000616 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000617 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000618 } else
619 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Chris Dunlop751e67c2011-10-19 16:48:26 +1100621 /* As we've dropped the mutex we need a spinlock to
622 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000623 */
624 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700625 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000626 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
NeilBrown1ca69c42012-10-11 13:37:33 +1100629static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
630{
631 struct md_rdev *rdev;
632
633 rdev_for_each_rcu(rdev, mddev)
634 if (rdev->desc_nr == nr)
635 return rdev;
636
637 return NULL;
638}
639
640static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
NeilBrown3cb03002011-10-11 16:45:26 +1100642 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
NeilBrowndafb20f2012-03-19 12:46:39 +1100644 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (rdev->bdev->bd_dev == dev)
646 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return NULL;
649}
650
NeilBrown1ca69c42012-10-11 13:37:33 +1100651static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
652{
653 struct md_rdev *rdev;
654
655 rdev_for_each_rcu(rdev, mddev)
656 if (rdev->bdev->bd_dev == dev)
657 return rdev;
658
659 return NULL;
660}
661
NeilBrown84fc4b52011-10-11 16:49:58 +1100662static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800663{
NeilBrown84fc4b52011-10-11 16:49:58 +1100664 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800665 list_for_each_entry(pers, &pers_list, list) {
666 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800667 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800668 if (strcmp(pers->name, clevel)==0)
669 return pers;
670 }
NeilBrown2604b702006-01-06 00:20:36 -0800671 return NULL;
672}
673
Andre Nollb73df2d2008-07-11 22:02:23 +1000674/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100675static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100677 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000678 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
NeilBrownf72ffdd2014-09-30 14:23:59 +1000681static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 rdev->sb_page = alloc_page(GFP_KERNEL);
684 if (!rdev->sb_page) {
685 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 return 0;
690}
691
NeilBrown545c8792012-05-22 13:54:30 +1000692void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800695 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rdev->sb_loaded = 0;
697 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000698 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100699 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
NeilBrown2699b672011-07-28 11:31:47 +1000701 if (rdev->bb_page) {
702 put_page(rdev->bb_page);
703 rdev->bb_page = NULL;
704 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000705 kfree(rdev->badblocks.page);
706 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
NeilBrown545c8792012-05-22 13:54:30 +1000708EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
NeilBrown6712ecf2007-09-27 12:47:43 +0200710static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700711{
NeilBrown3cb03002011-10-11 16:45:26 +1100712 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100713 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700714
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700715 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
716 printk("md: super_written gets error=%d, uptodate=%d\n",
717 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
718 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800719 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700720 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
NeilBrowna9701a32005-11-08 21:39:34 -0800722 if (atomic_dec_and_test(&mddev->pending_writes))
723 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700724 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700725}
726
NeilBrownfd01b882011-10-11 16:47:53 +1100727void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728 sector_t sector, int size, struct page *page)
729{
730 /* write first size bytes of page to sector of rdev
731 * Increment mddev->pending_writes before returning
732 * and decrement it on completion, waking up sb_wait
733 * if zero is reached.
734 * If an error occurred, call md_error
735 */
NeilBrowna167f662010-10-26 18:31:13 +1100736 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700737
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100738 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700739 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740 bio_add_page(bio, page, size, 0);
741 bio->bi_private = rdev;
742 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800743
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000745 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800746}
747
NeilBrownfd01b882011-10-11 16:47:53 +1100748void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800749{
Tejun Heoe9c74692010-09-03 11:56:18 +0200750 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000751 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700752}
753
NeilBrown3cb03002011-10-11 16:45:26 +1100754int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100755 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
NeilBrowna167f662010-10-26 18:31:13 +1100757 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int ret;
759
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100760 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
761 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100762 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700763 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000764 else if (rdev->mddev->reshape_position != MaxSector &&
765 (rdev->mddev->reshape_backwards ==
766 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700767 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100768 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700769 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700771 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
774 bio_put(bio);
775 return ret;
776}
NeilBrowna8745db2006-01-06 00:20:34 -0800777EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
NeilBrownf72ffdd2014-09-30 14:23:59 +1000779static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (rdev->sb_loaded)
784 return 0;
785
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100786 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto fail;
788 rdev->sb_loaded = 1;
789 return 0;
790
791fail:
792 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
793 bdevname(rdev->bdev,b));
794 return -EINVAL;
795}
796
797static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
798{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000799 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000800 sb1->set_uuid1 == sb2->set_uuid1 &&
801 sb1->set_uuid2 == sb2->set_uuid2 &&
802 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
806{
807 int ret;
808 mdp_super_t *tmp1, *tmp2;
809
810 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
811 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
812
813 if (!tmp1 || !tmp2) {
814 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100815 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto abort;
817 }
818
819 *tmp1 = *sb1;
820 *tmp2 = *sb2;
821
822 /*
823 * nr_disks is not constant
824 */
825 tmp1->nr_disks = 0;
826 tmp2->nr_disks = 0;
827
Andre Nollce0c8e02008-07-11 22:02:20 +1000828 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700830 kfree(tmp1);
831 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return ret;
833}
834
NeilBrown4d167f02007-05-09 02:35:37 -0700835static u32 md_csum_fold(u32 csum)
836{
837 csum = (csum & 0xffff) + (csum >> 16);
838 return (csum & 0xffff) + (csum >> 16);
839}
840
NeilBrownf72ffdd2014-09-30 14:23:59 +1000841static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
NeilBrown4d167f02007-05-09 02:35:37 -0700843 u64 newcsum = 0;
844 u32 *sb32 = (u32*)sb;
845 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int disk_csum, csum;
847
848 disk_csum = sb->sb_csum;
849 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700850
851 for (i = 0; i < MD_SB_BYTES/4 ; i++)
852 newcsum += sb32[i];
853 csum = (newcsum & 0xffffffff) + (newcsum>>32);
854
NeilBrown4d167f02007-05-09 02:35:37 -0700855#ifdef CONFIG_ALPHA
856 /* This used to use csum_partial, which was wrong for several
857 * reasons including that different results are returned on
858 * different architectures. It isn't critical that we get exactly
859 * the same return value as before (we always csum_fold before
860 * testing, and that removes any differences). However as we
861 * know that csum_partial always returned a 16bit value on
862 * alphas, do a fold to maximise conformity to previous behaviour.
863 */
864 sb->sb_csum = md_csum_fold(disk_csum);
865#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return csum;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * Handle superblock details.
873 * We want to be able to handle multiple superblock formats
874 * so we have a common interface to them all, and an array of
875 * different handlers.
876 * We rely on user-space to write the initial superblock, and support
877 * reading and updating of superblocks.
878 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100879 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * loads and validates a superblock on dev.
881 * if refdev != NULL, compare superblocks on both devices
882 * Return:
883 * 0 - dev has a superblock that is compatible with refdev
884 * 1 - dev has a superblock that is compatible and newer than refdev
885 * so dev should be used as the refdev in future
886 * -EINVAL superblock incompatible or invalid
887 * -othererror e.g. -EIO
888 *
NeilBrownfd01b882011-10-11 16:47:53 +1100889 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * Verify that dev is acceptable into mddev.
891 * The first time, mddev->raid_disks will be 0, and data from
892 * dev should be merged in. Subsequent calls check that dev
893 * is new enough. Return 0 or -EINVAL
894 *
NeilBrownfd01b882011-10-11 16:47:53 +1100895 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * Update the superblock for rdev with data in mddev
897 * This does not write to disc.
898 *
899 */
900
901struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000902 char *name;
903 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000904 int (*load_super)(struct md_rdev *rdev,
905 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000906 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000907 int (*validate_super)(struct mddev *mddev,
908 struct md_rdev *rdev);
909 void (*sync_super)(struct mddev *mddev,
910 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100911 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000912 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000913 int (*allow_new_offset)(struct md_rdev *rdev,
914 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915};
916
917/*
Andre Noll0894cc32009-06-18 08:49:23 +1000918 * Check that the given mddev has no bitmap.
919 *
920 * This function is called from the run method of all personalities that do not
921 * support bitmaps. It prints an error message and returns non-zero if mddev
922 * has a bitmap. Otherwise, it returns 0.
923 *
924 */
NeilBrownfd01b882011-10-11 16:47:53 +1100925int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000926{
NeilBrownc3d97142009-12-14 12:49:52 +1100927 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000928 return 0;
929 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
930 mdname(mddev), mddev->pers->name);
931 return 1;
932}
933EXPORT_SYMBOL(md_check_no_bitmap);
934
935/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000936 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
NeilBrown3cb03002011-10-11 16:45:26 +1100938static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
941 mdp_super_t *sb;
942 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
Andre Noll0f420352008-07-11 22:02:23 +1000945 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 * it's at the end of the disk.
947 *
948 * It also happens to be a multiple of 4Kb.
949 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100950 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
NeilBrown0002b272005-09-09 16:23:53 -0700952 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (ret) return ret;
954
955 ret = -EINVAL;
956
957 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000958 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (sb->md_magic != MD_SB_MAGIC) {
961 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
962 b);
963 goto abort;
964 }
965
966 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800967 sb->minor_version < 90 ||
968 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
970 sb->major_version, sb->minor_version,
971 b);
972 goto abort;
973 }
974
975 if (sb->raid_disks <= 0)
976 goto abort;
977
NeilBrown4d167f02007-05-09 02:35:37 -0700978 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
980 b);
981 goto abort;
982 }
983
984 rdev->preferred_minor = sb->md_minor;
985 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000986 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700987 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000988 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (sb->level == LEVEL_MULTIPATH)
991 rdev->desc_nr = -1;
992 else
993 rdev->desc_nr = sb->this_disk.number;
994
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700995 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +1000999 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!uuid_equal(refsb, sb)) {
1001 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1002 b, bdevname(refdev->bdev,b2));
1003 goto abort;
1004 }
1005 if (!sb_equal(refsb, sb)) {
1006 printk(KERN_WARNING "md: %s has same UUID"
1007 " but different superblock to %s\n",
1008 b, bdevname(refdev->bdev, b2));
1009 goto abort;
1010 }
1011 ev1 = md_event(sb);
1012 ev2 = md_event(refsb);
1013 if (ev1 > ev2)
1014 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 ret = 0;
1017 }
NeilBrown8190e752009-06-18 08:48:58 +10001018 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001019 /* Limit to 4TB as metadata cannot record more than that.
1020 * (not needed for Linear and RAID0 as metadata doesn't
1021 * record this size)
1022 */
1023 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001024 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
NeilBrown27a7b262011-09-10 17:21:28 +10001026 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001027 /* "this cannot possibly happen" ... */
1028 ret = -EINVAL;
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 abort:
1031 return ret;
1032}
1033
1034/*
1035 * validate_super for 0.90.0
1036 */
NeilBrownfd01b882011-10-11 16:47:53 +11001037static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001040 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001041 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
NeilBrown41158c72005-06-21 17:17:25 -07001043 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001044 clear_bit(Faulty, &rdev->flags);
1045 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001046 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001047 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (mddev->raid_disks == 0) {
1050 mddev->major_version = 0;
1051 mddev->minor_version = sb->minor_version;
1052 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001053 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001054 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mddev->ctime = sb->ctime;
1056 mddev->utime = sb->utime;
1057 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001058 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 mddev->layout = sb->layout;
1060 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001061 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001062 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001063 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001064 mddev->bitmap_info.space = 0;
1065 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001066 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001067 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001068 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
NeilBrownf6705572006-03-27 01:18:11 -08001070 if (mddev->minor_version >= 91) {
1071 mddev->reshape_position = sb->reshape_position;
1072 mddev->delta_disks = sb->delta_disks;
1073 mddev->new_level = sb->new_level;
1074 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001075 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001076 if (mddev->delta_disks < 0)
1077 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001078 } else {
1079 mddev->reshape_position = MaxSector;
1080 mddev->delta_disks = 0;
1081 mddev->new_level = mddev->level;
1082 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001083 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (sb->state & (1<<MD_SB_CLEAN))
1087 mddev->recovery_cp = MaxSector;
1088 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001089 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 sb->events_lo == sb->cp_events_lo) {
1091 mddev->recovery_cp = sb->recovery_cp;
1092 } else
1093 mddev->recovery_cp = 0;
1094 }
1095
1096 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1097 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1098 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1099 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1100
1101 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001102
1103 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001104 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001105 mddev->bitmap_info.offset =
1106 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001107 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001108 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001109 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001110
NeilBrown41158c72005-06-21 17:17:25 -07001111 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001112 /* Insist on good event counter while assembling, except
1113 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001115 if (sb->disks[rdev->desc_nr].state & (
1116 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001117 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001118 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001119 } else if (mddev->bitmap) {
1120 /* if adding to array with a bitmap, then we can accept an
1121 * older device ... but not too old.
1122 */
NeilBrown41158c72005-06-21 17:17:25 -07001123 if (ev1 < mddev->bitmap->events_cleared)
1124 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001125 if (ev1 < mddev->events)
1126 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001127 } else {
1128 if (ev1 < mddev->events)
1129 /* just a hot-add of a new device, leave raid_disk at -1 */
1130 return 0;
1131 }
NeilBrown41158c72005-06-21 17:17:25 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 desc = sb->disks + rdev->desc_nr;
1135
1136 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001137 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001138 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1139 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001140 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001142 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001143 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1144 /* active but not in sync implies recovery up to
1145 * reshape position. We don't know exactly where
1146 * that is, so set to zero for now */
1147 if (mddev->minor_version >= 91) {
1148 rdev->recovery_offset = 0;
1149 rdev->raid_disk = desc->raid_disk;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001152 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1153 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001154 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001155 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157}
1158
1159/*
1160 * sync_super for 0.90.0
1161 */
NeilBrownfd01b882011-10-11 16:47:53 +11001162static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001165 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* make rdev->sb match mddev data..
1169 *
1170 * 1/ zero out disks
1171 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1172 * 3/ any empty disks < next_spare become removed
1173 *
1174 * disks[0] gets initialised to REMOVED because
1175 * we cannot be sure from other fields if it has
1176 * been initialised or not.
1177 */
1178 int i;
1179 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1180
NeilBrown61181562005-09-09 16:24:02 -07001181 rdev->sb_size = MD_SB_BYTES;
1182
Namhyung Kim65a06f062011-07-27 11:00:36 +10001183 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 memset(sb, 0, sizeof(*sb));
1186
1187 sb->md_magic = MD_SB_MAGIC;
1188 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 sb->patch_version = mddev->patch_version;
1190 sb->gvalid_words = 0; /* ignored */
1191 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1192 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1193 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1194 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1195
1196 sb->ctime = mddev->ctime;
1197 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001198 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 sb->raid_disks = mddev->raid_disks;
1200 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001201 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 sb->utime = mddev->utime;
1203 sb->state = 0;
1204 sb->events_hi = (mddev->events>>32);
1205 sb->events_lo = (u32)mddev->events;
1206
NeilBrownf6705572006-03-27 01:18:11 -08001207 if (mddev->reshape_position == MaxSector)
1208 sb->minor_version = 90;
1209 else {
1210 sb->minor_version = 91;
1211 sb->reshape_position = mddev->reshape_position;
1212 sb->new_level = mddev->new_level;
1213 sb->delta_disks = mddev->delta_disks;
1214 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001215 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001216 }
1217 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (mddev->in_sync)
1219 {
1220 sb->recovery_cp = mddev->recovery_cp;
1221 sb->cp_events_hi = (mddev->events>>32);
1222 sb->cp_events_lo = (u32)mddev->events;
1223 if (mddev->recovery_cp == MaxSector)
1224 sb->state = (1<< MD_SB_CLEAN);
1225 } else
1226 sb->recovery_cp = 0;
1227
1228 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001229 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
NeilBrownc3d97142009-12-14 12:49:52 +11001231 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001232 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001235 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001237 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001238 int is_active = test_bit(In_sync, &rdev2->flags);
1239
1240 if (rdev2->raid_disk >= 0 &&
1241 sb->minor_version >= 91)
1242 /* we have nowhere to store the recovery_offset,
1243 * but if it is not below the reshape_position,
1244 * we can piggy-back on that.
1245 */
1246 is_active = 1;
1247 if (rdev2->raid_disk < 0 ||
1248 test_bit(Faulty, &rdev2->flags))
1249 is_active = 0;
1250 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001251 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001253 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001254 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 d = &sb->disks[rdev2->desc_nr];
1256 nr_disks++;
1257 d->number = rdev2->desc_nr;
1258 d->major = MAJOR(rdev2->bdev->bd_dev);
1259 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001260 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 d->raid_disk = rdev2->raid_disk;
1262 else
1263 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001264 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001266 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001268 if (test_bit(In_sync, &rdev2->flags))
1269 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 active++;
1271 working++;
1272 } else {
1273 d->state = 0;
1274 spare++;
1275 working++;
1276 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001277 if (test_bit(WriteMostly, &rdev2->flags))
1278 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* now set the "removed" and "faulty" bits on any missing devices */
1281 for (i=0 ; i < mddev->raid_disks ; i++) {
1282 mdp_disk_t *d = &sb->disks[i];
1283 if (d->state == 0 && d->number == 0) {
1284 d->number = i;
1285 d->raid_disk = i;
1286 d->state = (1<<MD_DISK_REMOVED);
1287 d->state |= (1<<MD_DISK_FAULTY);
1288 failed++;
1289 }
1290 }
1291 sb->nr_disks = nr_disks;
1292 sb->active_disks = active;
1293 sb->working_disks = working;
1294 sb->failed_disks = failed;
1295 sb->spare_disks = spare;
1296
1297 sb->this_disk = sb->disks[rdev->desc_nr];
1298 sb->sb_csum = calc_sb_csum(sb);
1299}
1300
1301/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001302 * rdev_size_change for 0.90.0
1303 */
1304static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001305super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001306{
Andre Noll58c0fed2009-03-31 14:33:13 +11001307 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001308 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001309 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001310 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001311 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001312 if (!num_sectors || num_sectors > rdev->sb_start)
1313 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001314 /* Limit to 4TB as metadata cannot record more than that.
1315 * 4TB == 2^32 KB, or 2*2^32 sectors.
1316 */
NeilBrown667a5312012-08-16 16:46:12 +10001317 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001318 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001319 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001320 rdev->sb_page);
1321 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001322 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323}
1324
NeilBrownc6563a82012-05-21 09:27:00 +10001325static int
1326super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1327{
1328 /* non-zero offset changes not possible with v0.90 */
1329 return new_offset == 0;
1330}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331
1332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 * version 1 superblock
1334 */
1335
NeilBrownf72ffdd2014-09-30 14:23:59 +10001336static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001338 __le32 disk_csum;
1339 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 unsigned long long newcsum;
1341 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001342 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 disk_csum = sb->sb_csum;
1345 sb->sb_csum = 0;
1346 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001347 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 newcsum += le32_to_cpu(*isuper++);
1349
1350 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001351 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1354 sb->sb_csum = disk_csum;
1355 return cpu_to_le32(csum);
1356}
1357
NeilBrown2699b672011-07-28 11:31:47 +10001358static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1359 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001360static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct mdp_superblock_1 *sb;
1363 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001364 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001365 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001367 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /*
Andre Noll0f420352008-07-11 22:02:23 +10001370 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * It is always aligned to a 4K boundary and
1372 * depeding on minor_version, it can be:
1373 * 0: At least 8K, but less than 12K, from end of device
1374 * 1: At start of device
1375 * 2: 4K from start of device.
1376 */
1377 switch(minor_version) {
1378 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001379 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001380 sb_start -= 8*2;
1381 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001384 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 break;
1386 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001387 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 default:
1390 return -EINVAL;
1391 }
Andre Noll0f420352008-07-11 22:02:23 +10001392 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
NeilBrown0002b272005-09-09 16:23:53 -07001394 /* superblock is rarely larger than 1K, but it can be larger,
1395 * and it is safe to read 4k, so we do that
1396 */
1397 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (ret) return ret;
1399
Namhyung Kim65a06f062011-07-27 11:00:36 +10001400 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1403 sb->major_version != cpu_to_le32(1) ||
1404 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001405 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001406 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EINVAL;
1408
1409 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1410 printk("md: invalid superblock checksum on %s\n",
1411 bdevname(rdev->bdev,b));
1412 return -EINVAL;
1413 }
1414 if (le64_to_cpu(sb->data_size) < 10) {
1415 printk("md: data_size too small on %s\n",
1416 bdevname(rdev->bdev,b));
1417 return -EINVAL;
1418 }
NeilBrownc6563a82012-05-21 09:27:00 +10001419 if (sb->pad0 ||
1420 sb->pad3[0] ||
1421 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1422 /* Some padding is non-zero, might be a new feature */
1423 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 rdev->preferred_minor = 0xffff;
1426 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001427 rdev->new_data_offset = rdev->data_offset;
1428 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1429 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1430 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001431 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
NeilBrown0002b272005-09-09 16:23:53 -07001433 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001434 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001435 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001436 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1437
1438 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001439 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001440 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001441 if (minor_version
1442 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1443 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001444
NeilBrown31b65a02006-07-10 04:44:14 -07001445 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1446 rdev->desc_nr = -1;
1447 else
1448 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1449
NeilBrown2699b672011-07-28 11:31:47 +10001450 if (!rdev->bb_page) {
1451 rdev->bb_page = alloc_page(GFP_KERNEL);
1452 if (!rdev->bb_page)
1453 return -ENOMEM;
1454 }
1455 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1456 rdev->badblocks.count == 0) {
1457 /* need to load the bad block list.
1458 * Currently we limit it to one page.
1459 */
1460 s32 offset;
1461 sector_t bb_sector;
1462 u64 *bbp;
1463 int i;
1464 int sectors = le16_to_cpu(sb->bblog_size);
1465 if (sectors > (PAGE_SIZE / 512))
1466 return -EINVAL;
1467 offset = le32_to_cpu(sb->bblog_offset);
1468 if (offset == 0)
1469 return -EINVAL;
1470 bb_sector = (long long)offset;
1471 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1472 rdev->bb_page, READ, true))
1473 return -EIO;
1474 bbp = (u64 *)page_address(rdev->bb_page);
1475 rdev->badblocks.shift = sb->bblog_shift;
1476 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1477 u64 bb = le64_to_cpu(*bbp);
1478 int count = bb & (0x3ff);
1479 u64 sector = bb >> 10;
1480 sector <<= sb->bblog_shift;
1481 count <<= sb->bblog_shift;
1482 if (bb + 1 == 0)
1483 break;
1484 if (md_set_badblocks(&rdev->badblocks,
1485 sector, count, 1) == 0)
1486 return -EINVAL;
1487 }
NeilBrown486adf72013-04-24 11:42:44 +10001488 } else if (sb->bblog_offset != 0)
1489 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001490
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001491 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001492 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001493 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001495 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1498 sb->level != refsb->level ||
1499 sb->layout != refsb->layout ||
1500 sb->chunksize != refsb->chunksize) {
1501 printk(KERN_WARNING "md: %s has strangely different"
1502 " superblock to %s\n",
1503 bdevname(rdev->bdev,b),
1504 bdevname(refdev->bdev,b2));
1505 return -EINVAL;
1506 }
1507 ev1 = le64_to_cpu(sb->events);
1508 ev2 = le64_to_cpu(refsb->events);
1509
1510 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001511 ret = 1;
1512 else
1513 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
NeilBrownc6563a82012-05-21 09:27:00 +10001515 if (minor_version) {
1516 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1517 sectors -= rdev->data_offset;
1518 } else
1519 sectors = rdev->sb_start;
1520 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001522 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
NeilBrownfd01b882011-10-11 16:47:53 +11001526static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001528 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001529 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
NeilBrown41158c72005-06-21 17:17:25 -07001531 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001532 clear_bit(Faulty, &rdev->flags);
1533 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001534 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001535 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (mddev->raid_disks == 0) {
1538 mddev->major_version = 1;
1539 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001540 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001541 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1543 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1544 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001545 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 mddev->layout = le32_to_cpu(sb->layout);
1547 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001548 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001549 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001550 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001551 mddev->bitmap_info.space = 0;
1552 /* Default location for bitmap is 1K after superblock
1553 * using 3K - total of 4K
1554 */
NeilBrownc3d97142009-12-14 12:49:52 +11001555 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001556 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001557 mddev->reshape_backwards = 0;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1560 memcpy(mddev->uuid, sb->set_uuid, 16);
1561
1562 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001563
NeilBrown71c08052005-09-09 16:23:51 -07001564 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001565 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001566 mddev->bitmap_info.offset =
1567 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001568 /* Metadata doesn't record how much space is available.
1569 * For 1.0, we assume we can use up to the superblock
1570 * if before, else to 4K beyond superblock.
1571 * For others, assume no change is possible.
1572 */
1573 if (mddev->minor_version > 0)
1574 mddev->bitmap_info.space = 0;
1575 else if (mddev->bitmap_info.offset > 0)
1576 mddev->bitmap_info.space =
1577 8 - mddev->bitmap_info.offset;
1578 else
1579 mddev->bitmap_info.space =
1580 -mddev->bitmap_info.offset;
1581 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001582
NeilBrownf6705572006-03-27 01:18:11 -08001583 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1584 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1585 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1586 mddev->new_level = le32_to_cpu(sb->new_level);
1587 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001588 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001589 if (mddev->delta_disks < 0 ||
1590 (mddev->delta_disks == 0 &&
1591 (le32_to_cpu(sb->feature_map)
1592 & MD_FEATURE_RESHAPE_BACKWARDS)))
1593 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001594 } else {
1595 mddev->reshape_position = MaxSector;
1596 mddev->delta_disks = 0;
1597 mddev->new_level = mddev->level;
1598 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001599 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001600 }
1601
NeilBrown41158c72005-06-21 17:17:25 -07001602 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001603 /* Insist of good event counter while assembling, except for
1604 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001606 if (rdev->desc_nr >= 0 &&
1607 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1608 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1609 if (ev1 < mddev->events)
1610 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001611 } else if (mddev->bitmap) {
1612 /* If adding to array with a bitmap, then we can accept an
1613 * older device, but not too old.
1614 */
NeilBrown41158c72005-06-21 17:17:25 -07001615 if (ev1 < mddev->bitmap->events_cleared)
1616 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001617 if (ev1 < mddev->events)
1618 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001619 } else {
1620 if (ev1 < mddev->events)
1621 /* just a hot-add of a new device, leave raid_disk at -1 */
1622 return 0;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (mddev->level != LEVEL_MULTIPATH) {
1625 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001626 if (rdev->desc_nr < 0 ||
1627 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1628 role = 0xffff;
1629 rdev->desc_nr = -1;
1630 } else
1631 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 switch(role) {
1633 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
1635 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001636 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 default:
NeilBrownf4667222013-12-09 12:04:56 +11001639 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001640 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001641 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001642 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001643 if (!(le32_to_cpu(sb->feature_map) &
1644 MD_FEATURE_RECOVERY_BITMAP))
1645 rdev->saved_raid_disk = -1;
1646 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001647 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 rdev->raid_disk = role;
1649 break;
1650 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001651 if (sb->devflags & WriteMostly1)
1652 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001653 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1654 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001655 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001656 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
1660
NeilBrownfd01b882011-10-11 16:47:53 +11001661static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001664 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int max_dev, i;
1666 /* make rdev->sb match mddev and rdev data. */
1667
Namhyung Kim65a06f062011-07-27 11:00:36 +10001668 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 sb->feature_map = 0;
1671 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001672 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 memset(sb->pad3, 0, sizeof(sb->pad3));
1674
1675 sb->utime = cpu_to_le64((__u64)mddev->utime);
1676 sb->events = cpu_to_le64(mddev->events);
1677 if (mddev->in_sync)
1678 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1679 else
1680 sb->resync_offset = cpu_to_le64(0);
1681
NeilBrown1c05b4b2006-10-21 10:24:08 -07001682 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001683
NeilBrownf0ca3402006-02-02 14:28:04 -08001684 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001685 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001686 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001687 sb->level = cpu_to_le32(mddev->level);
1688 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001689
NeilBrownaeb9b2112011-08-25 14:43:08 +10001690 if (test_bit(WriteMostly, &rdev->flags))
1691 sb->devflags |= WriteMostly1;
1692 else
1693 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001694 sb->data_offset = cpu_to_le64(rdev->data_offset);
1695 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001696
NeilBrownc3d97142009-12-14 12:49:52 +11001697 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1698 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001699 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001700 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001701
1702 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001703 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001704 sb->feature_map |=
1705 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1706 sb->recovery_offset =
1707 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001708 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1709 sb->feature_map |=
1710 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001711 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001712 if (test_bit(Replacement, &rdev->flags))
1713 sb->feature_map |=
1714 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001715
NeilBrownf6705572006-03-27 01:18:11 -08001716 if (mddev->reshape_position != MaxSector) {
1717 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1718 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1719 sb->new_layout = cpu_to_le32(mddev->new_layout);
1720 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1721 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001722 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001723 if (mddev->delta_disks == 0 &&
1724 mddev->reshape_backwards)
1725 sb->feature_map
1726 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001727 if (rdev->new_data_offset != rdev->data_offset) {
1728 sb->feature_map
1729 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1730 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1731 - rdev->data_offset));
1732 }
NeilBrownf6705572006-03-27 01:18:11 -08001733 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001734
NeilBrown2699b672011-07-28 11:31:47 +10001735 if (rdev->badblocks.count == 0)
1736 /* Nothing to do for bad blocks*/ ;
1737 else if (sb->bblog_offset == 0)
1738 /* Cannot record bad blocks on this device */
1739 md_error(mddev, rdev);
1740 else {
1741 struct badblocks *bb = &rdev->badblocks;
1742 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1743 u64 *p = bb->page;
1744 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1745 if (bb->changed) {
1746 unsigned seq;
1747
1748retry:
1749 seq = read_seqbegin(&bb->lock);
1750
1751 memset(bbp, 0xff, PAGE_SIZE);
1752
1753 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001754 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001755 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1756 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001757 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001758 }
NeilBrownd0962932012-03-19 12:46:41 +11001759 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001760 if (read_seqretry(&bb->lock, seq))
1761 goto retry;
1762
1763 bb->sector = (rdev->sb_start +
1764 (int)le32_to_cpu(sb->bblog_offset));
1765 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001766 }
1767 }
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001770 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (rdev2->desc_nr+1 > max_dev)
1772 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001773
NeilBrown70471da2009-08-03 10:59:57 +10001774 if (max_dev > le32_to_cpu(sb->max_dev)) {
1775 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001776 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001777 rdev->sb_size = max_dev * 2 + 256;
1778 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1779 if (rdev->sb_size & bmask)
1780 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001781 } else
1782 max_dev = le32_to_cpu(sb->max_dev);
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 for (i=0; i<max_dev;i++)
1785 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001786
NeilBrowndafb20f2012-03-19 12:46:39 +11001787 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001789 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001791 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001793 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001794 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 else
1796 sb->dev_roles[i] = cpu_to_le16(0xffff);
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 sb->sb_csum = calc_sb_1_csum(sb);
1800}
1801
Chris Webb0cd17fe2008-06-28 08:31:46 +10001802static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001803super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001804{
1805 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001806 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001807 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001808 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001809 if (rdev->data_offset != rdev->new_data_offset)
1810 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001811 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001812 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001813 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001814 max_sectors -= rdev->data_offset;
1815 if (!num_sectors || num_sectors > max_sectors)
1816 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001817 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818 /* minor version 0 with bitmap we can't move */
1819 return 0;
1820 } else {
1821 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001822 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001823 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001824 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001825 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001826 if (!num_sectors || num_sectors > max_sectors)
1827 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001828 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001830 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001831 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001832 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001833 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001834 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835 rdev->sb_page);
1836 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001837 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001838
1839}
1840
1841static int
1842super_1_allow_new_offset(struct md_rdev *rdev,
1843 unsigned long long new_offset)
1844{
1845 /* All necessary checks on new >= old have been done */
1846 struct bitmap *bitmap;
1847 if (new_offset >= rdev->data_offset)
1848 return 1;
1849
1850 /* with 1.0 metadata, there is no metadata to tread on
1851 * so we can always move back */
1852 if (rdev->mddev->minor_version == 0)
1853 return 1;
1854
1855 /* otherwise we must be sure not to step on
1856 * any metadata, so stay:
1857 * 36K beyond start of superblock
1858 * beyond end of badblocks
1859 * beyond write-intent bitmap
1860 */
1861 if (rdev->sb_start + (32+4)*2 > new_offset)
1862 return 0;
1863 bitmap = rdev->mddev->bitmap;
1864 if (bitmap && !rdev->mddev->bitmap_info.file &&
1865 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001866 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001867 return 0;
1868 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1869 return 0;
1870
1871 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Adrian Bunk75c96f82005-05-05 16:16:09 -07001874static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 [0] = {
1876 .name = "0.90.0",
1877 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 .load_super = super_90_load,
1879 .validate_super = super_90_validate,
1880 .sync_super = super_90_sync,
1881 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001882 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 },
1884 [1] = {
1885 .name = "md-1",
1886 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887 .load_super = super_1_load,
1888 .validate_super = super_1_validate,
1889 .sync_super = super_1_sync,
1890 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001891 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 },
1893};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
NeilBrownfd01b882011-10-11 16:47:53 +11001895static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001896{
1897 if (mddev->sync_super) {
1898 mddev->sync_super(mddev, rdev);
1899 return;
1900 }
1901
1902 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1903
1904 super_types[mddev->major_version].sync_super(mddev, rdev);
1905}
1906
NeilBrownfd01b882011-10-11 16:47:53 +11001907static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
NeilBrown3cb03002011-10-11 16:45:26 +11001909 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
NeilBrown4b809912008-07-21 17:05:25 +10001911 rcu_read_lock();
1912 rdev_for_each_rcu(rdev, mddev1)
1913 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001914 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001915 rdev2->bdev->bd_contains) {
1916 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001917 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001918 }
1919 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return 0;
1921}
1922
1923static LIST_HEAD(pending_raid_disks);
1924
Andre Nollac5e7112009-08-03 10:59:47 +10001925/*
1926 * Try to register data integrity profile for an mddev
1927 *
1928 * This is called when an array is started and after a disk has been kicked
1929 * from the array. It only succeeds if all working and active component devices
1930 * are integrity capable with matching profiles.
1931 */
NeilBrownfd01b882011-10-11 16:47:53 +11001932int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001933{
NeilBrown3cb03002011-10-11 16:45:26 +11001934 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001935
Andre Nollac5e7112009-08-03 10:59:47 +10001936 if (list_empty(&mddev->disks))
1937 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001938 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1939 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001940 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001941 /* skip spares and non-functional disks */
1942 if (test_bit(Faulty, &rdev->flags))
1943 continue;
1944 if (rdev->raid_disk < 0)
1945 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001946 if (!reference) {
1947 /* Use the first rdev as the reference */
1948 reference = rdev;
1949 continue;
1950 }
1951 /* does this rdev's profile match the reference profile? */
1952 if (blk_integrity_compare(reference->bdev->bd_disk,
1953 rdev->bdev->bd_disk) < 0)
1954 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001955 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001956 if (!reference || !bdev_get_integrity(reference->bdev))
1957 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001958 /*
1959 * All component devices are integrity capable and have matching
1960 * profiles, register the common profile for the md device.
1961 */
1962 if (blk_integrity_register(mddev->gendisk,
1963 bdev_get_integrity(reference->bdev)) != 0) {
1964 printk(KERN_ERR "md: failed to register integrity for %s\n",
1965 mdname(mddev));
1966 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001967 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001968 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1969 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1970 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1971 mdname(mddev));
1972 return -EINVAL;
1973 }
Andre Nollac5e7112009-08-03 10:59:47 +10001974 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001975}
Andre Nollac5e7112009-08-03 10:59:47 +10001976EXPORT_SYMBOL(md_integrity_register);
1977
1978/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001979void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001980{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001981 struct blk_integrity *bi_rdev;
1982 struct blk_integrity *bi_mddev;
1983
1984 if (!mddev->gendisk)
1985 return;
1986
1987 bi_rdev = bdev_get_integrity(rdev->bdev);
1988 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10001989
1990 if (!bi_mddev) /* nothing to do */
1991 return;
1992 if (rdev->raid_disk < 0) /* skip spares */
1993 return;
1994 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1995 rdev->bdev->bd_disk) >= 0)
1996 return;
1997 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1998 blk_integrity_unregister(mddev->gendisk);
1999}
2000EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002001
NeilBrownf72ffdd2014-09-30 14:23:59 +10002002static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002004 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002005 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002006 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002007 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Dan Williams11e2ede2008-04-30 00:52:32 -07002009 /* prevent duplicates */
2010 if (find_rdev(mddev, rdev->bdev->bd_dev))
2011 return -EEXIST;
2012
Andre Nolldd8ac332009-03-31 14:33:13 +11002013 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2014 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2015 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002016 if (mddev->pers) {
2017 /* Cannot change size, so fail
2018 * If mddev->level <= 0, then we don't care
2019 * about aligning sizes (e.g. linear)
2020 */
2021 if (mddev->level > 0)
2022 return -ENOSPC;
2023 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002024 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /* Verify rdev->desc_nr is unique.
2028 * If it is -1, assign a free number, else
2029 * check number is not in use
2030 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002031 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (rdev->desc_nr < 0) {
2033 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002034 if (mddev->pers)
2035 choice = mddev->raid_disks;
2036 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 choice++;
2038 rdev->desc_nr = choice;
2039 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002040 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2041 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002045 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002046 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2047 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2048 mdname(mddev), mddev->max_disks);
2049 return -EBUSY;
2050 }
NeilBrown19133a42005-11-08 21:39:35 -08002051 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002052 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002053 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002056 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002057
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002058 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002059 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002060
Tejun Heo0762b8b2008-08-25 19:56:12 +09002061 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002062 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2063 /* failure here is OK */;
2064 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002065
NeilBrown4b809912008-07-21 17:05:25 +10002066 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002067 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002068
2069 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002070 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002073
2074 fail:
2075 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2076 b, mdname(mddev));
2077 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
NeilBrown177a99b2008-02-06 01:39:56 -08002080static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002081{
NeilBrown3cb03002011-10-11 16:45:26 +11002082 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002083 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002084 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002085}
2086
NeilBrownf72ffdd2014-09-30 14:23:59 +10002087static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002090
Tejun Heo49731ba2011-01-14 18:43:57 +01002091 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002092 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2094 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002095 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002096 sysfs_put(rdev->sysfs_state);
2097 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002098 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002099 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002100 * writing to 'remove' to "dev/state". We also need
2101 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002102 */
NeilBrown4b809912008-07-21 17:05:25 +10002103 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002104 INIT_WORK(&rdev->del_work, md_delayed_delete);
2105 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002106 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109/*
2110 * prevent the device from being mounted, repartitioned or
2111 * otherwise reused by a RAID array (or any other kernel
2112 * subsystem), by bd_claiming the device.
2113 */
NeilBrown3cb03002011-10-11 16:45:26 +11002114static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 int err = 0;
2117 struct block_device *bdev;
2118 char b[BDEVNAME_SIZE];
2119
Tejun Heod4d77622010-11-13 11:55:18 +01002120 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002121 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (IS_ERR(bdev)) {
2123 printk(KERN_ERR "md: could not open %s.\n",
2124 __bdevname(dev, b));
2125 return PTR_ERR(bdev);
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 rdev->bdev = bdev;
2128 return err;
2129}
2130
NeilBrown3cb03002011-10-11 16:45:26 +11002131static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 struct block_device *bdev = rdev->bdev;
2134 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002135 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
2138void md_autodetect_dev(dev_t dev);
2139
NeilBrownf72ffdd2014-09-30 14:23:59 +10002140static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 printk(KERN_INFO "md: export_rdev(%s)\n",
2145 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002146 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002148 if (test_bit(AutoDetected, &rdev->flags))
2149 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#endif
2151 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002152 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
NeilBrownf72ffdd2014-09-30 14:23:59 +10002155static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 unbind_rdev_from_array(rdev);
2158 export_rdev(rdev);
2159}
2160
NeilBrownfd01b882011-10-11 16:47:53 +11002161static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
NeilBrown0638bb02014-09-25 17:43:47 +10002163 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
NeilBrown0638bb02014-09-25 17:43:47 +10002165 while (!list_empty(&mddev->disks)) {
2166 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2167 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 kick_rdev_from_array(rdev);
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 mddev->raid_disks = 0;
2171 mddev->major_version = 0;
2172}
2173
2174static void print_desc(mdp_disk_t *desc)
2175{
2176 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2177 desc->major,desc->minor,desc->raid_disk,desc->state);
2178}
2179
Cheng Renquancd2ac932009-01-09 08:31:08 +11002180static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 int i;
2183
NeilBrownf72ffdd2014-09-30 14:23:59 +10002184 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2186 sb->major_version, sb->minor_version, sb->patch_version,
2187 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2188 sb->ctime);
2189 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2190 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2191 sb->md_minor, sb->layout, sb->chunk_size);
2192 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2193 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2194 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2195 sb->failed_disks, sb->spare_disks,
2196 sb->sb_csum, (unsigned long)sb->events_lo);
2197
2198 printk(KERN_INFO);
2199 for (i = 0; i < MD_SB_DISKS; i++) {
2200 mdp_disk_t *desc;
2201
2202 desc = sb->disks + i;
2203 if (desc->number || desc->major || desc->minor ||
2204 desc->raid_disk || (desc->state && (desc->state != 4))) {
2205 printk(" D %2d: ", i);
2206 print_desc(desc);
2207 }
2208 }
2209 printk(KERN_INFO "md: THIS: ");
2210 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Cheng Renquancd2ac932009-01-09 08:31:08 +11002213static void print_sb_1(struct mdp_superblock_1 *sb)
2214{
2215 __u8 *uuid;
2216
2217 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002218 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002219 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002220 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002221 le32_to_cpu(sb->major_version),
2222 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002223 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002224 sb->set_name,
2225 (unsigned long long)le64_to_cpu(sb->ctime)
2226 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2227
2228 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002229 printk(KERN_INFO
2230 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002231 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002232 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002233 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2234 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002235 le32_to_cpu(sb->level),
2236 (unsigned long long)le64_to_cpu(sb->size),
2237 le32_to_cpu(sb->raid_disks),
2238 le32_to_cpu(sb->layout),
2239 le32_to_cpu(sb->chunksize),
2240 (unsigned long long)le64_to_cpu(sb->data_offset),
2241 (unsigned long long)le64_to_cpu(sb->data_size),
2242 (unsigned long long)le64_to_cpu(sb->super_offset),
2243 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2244 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002245 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002246 sb->devflags,
2247 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2248 (unsigned long long)le64_to_cpu(sb->events),
2249 (unsigned long long)le64_to_cpu(sb->resync_offset),
2250 le32_to_cpu(sb->sb_csum),
2251 le32_to_cpu(sb->max_dev)
2252 );
2253}
2254
NeilBrown3cb03002011-10-11 16:45:26 +11002255static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002258 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10002259 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2260 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2261 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002263 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2264 switch (major_version) {
2265 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002266 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002267 break;
2268 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002269 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002270 break;
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 } else
2273 printk(KERN_INFO "md: no rdev superblock!\n");
2274}
2275
Adrian Bunk5e563412006-06-26 00:27:42 -07002276static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002278 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002279 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002280 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 char b[BDEVNAME_SIZE];
2282
2283 printk("\n");
2284 printk("md: **********************************\n");
2285 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2286 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002287 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
NeilBrown32a76272005-06-21 17:17:14 -07002289 if (mddev->bitmap)
2290 bitmap_print_sb(mddev->bitmap);
2291 else
2292 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002293 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 printk("<%s>", bdevname(rdev->bdev,b));
2295 printk("\n");
2296
NeilBrowndafb20f2012-03-19 12:46:39 +11002297 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002298 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 printk("md: **********************************\n");
2301 printk("\n");
2302}
2303
NeilBrownf72ffdd2014-09-30 14:23:59 +10002304static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
NeilBrown42543762006-06-26 00:27:57 -07002306 /* Update each superblock (in-memory image), but
2307 * if we are allowed to, skip spares which already
2308 * have the right event counter, or have one earlier
2309 * (which would mean they aren't being marked as dirty
2310 * with the rest of the array)
2311 */
NeilBrown3cb03002011-10-11 16:45:26 +11002312 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002313 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002314 if (rdev->sb_events == mddev->events ||
2315 (nospares &&
2316 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002317 rdev->sb_events+1 == mddev->events)) {
2318 /* Don't update this superblock */
2319 rdev->sb_loaded = 2;
2320 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002321 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002322 rdev->sb_loaded = 1;
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325}
2326
NeilBrownf72ffdd2014-09-30 14:23:59 +10002327static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
NeilBrown3cb03002011-10-11 16:45:26 +11002329 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002330 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002331 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002332 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
NeilBrownd87f0642013-04-24 11:42:40 +10002334 if (mddev->ro) {
2335 if (force_change)
2336 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2337 return;
2338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002340 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002341 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002342 if (rdev->raid_disk >= 0 &&
2343 mddev->delta_disks >= 0 &&
2344 !test_bit(In_sync, &rdev->flags) &&
2345 mddev->curr_resync_completed > rdev->recovery_offset)
2346 rdev->recovery_offset = mddev->curr_resync_completed;
2347
NeilBrownf72ffdd2014-09-30 14:23:59 +10002348 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002349 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002350 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002351 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002352 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002353 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002354 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002355 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002356 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002357 md_ack_all_badblocks(&rdev->badblocks);
2358 md_error(mddev, rdev);
2359 }
2360 clear_bit(Blocked, &rdev->flags);
2361 clear_bit(BlockedBadBlocks, &rdev->flags);
2362 wake_up(&rdev->blocked_wait);
2363 }
2364 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002365 wake_up(&mddev->sb_wait);
2366 return;
2367 }
2368
NeilBrowna9701a32005-11-08 21:39:34 -08002369 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002370
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002371 mddev->utime = get_seconds();
2372
NeilBrown850b2b422006-10-03 01:15:46 -07002373 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2374 force_change = 1;
2375 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2376 /* just a clean<-> dirty transition, possibly leave spares alone,
2377 * though if events isn't the right even/odd, we will have to do
2378 * spares after all
2379 */
2380 nospares = 1;
2381 if (force_change)
2382 nospares = 0;
2383 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002384 /* If the array is degraded, then skipping spares is both
2385 * dangerous and fairly pointless.
2386 * Dangerous because a device that was removed from the array
2387 * might have a event_count that still looks up-to-date,
2388 * so it can be re-added without a resync.
2389 * Pointless because if there are any spares to skip,
2390 * then a recovery will happen and soon that array won't
2391 * be degraded any more and the spare can go back to sleep then.
2392 */
NeilBrown850b2b422006-10-03 01:15:46 -07002393 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002394
NeilBrown06d91a52005-06-21 17:17:12 -07002395 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002396
2397 /* If this is just a dirty<->clean transition, and the array is clean
2398 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002399 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002400 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002401 && mddev->can_decrease_events
2402 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002403 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002404 mddev->can_decrease_events = 0;
2405 } else {
NeilBrown42543762006-06-26 00:27:57 -07002406 /* otherwise we have to go forward and ... */
2407 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002408 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
NeilBrown403df472014-09-30 15:52:29 +10002411 /*
2412 * This 64-bit counter should never wrap.
2413 * Either we are in around ~1 trillion A.C., assuming
2414 * 1 reboot per second, or we have a bug...
2415 */
2416 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002417
NeilBrowndafb20f2012-03-19 12:46:39 +11002418 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002419 if (rdev->badblocks.changed)
2420 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002421 if (test_bit(Faulty, &rdev->flags))
2422 set_bit(FaultRecorded, &rdev->flags);
2423 }
NeilBrown2699b672011-07-28 11:31:47 +10002424
NeilBrowne6910632008-02-06 01:39:51 -08002425 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002426 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
NeilBrown36a4e1f2011-10-07 14:23:17 +11002428 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2429 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
NeilBrown4ad13662007-07-17 04:06:13 -07002431 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002432 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002434
NeilBrown42543762006-06-26 00:27:57 -07002435 if (rdev->sb_loaded != 1)
2436 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
NeilBrownf4667222013-12-09 12:04:56 +11002438 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002439 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002440 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002441 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002442 pr_debug("md: (write) %s's sb offset: %llu\n",
2443 bdevname(rdev->bdev, b),
2444 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002445 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002446 if (rdev->badblocks.size) {
2447 md_super_write(mddev, rdev,
2448 rdev->badblocks.sector,
2449 rdev->badblocks.size << 9,
2450 rdev->bb_page);
2451 rdev->badblocks.size = 0;
2452 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002453
NeilBrownf4667222013-12-09 12:04:56 +11002454 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002455 pr_debug("md: %s (skipping faulty)\n",
2456 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002457
NeilBrown7bfa19f2005-06-21 17:17:28 -07002458 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* only need to write one superblock... */
2460 break;
2461 }
NeilBrowna9701a32005-11-08 21:39:34 -08002462 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002463 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002464
NeilBrowna9701a32005-11-08 21:39:34 -08002465 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002466 if (mddev->in_sync != sync_req ||
2467 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002468 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002469 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002470 goto repeat;
2471 }
NeilBrown850b2b422006-10-03 01:15:46 -07002472 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002473 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002474 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002475 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2476 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002477
NeilBrowndafb20f2012-03-19 12:46:39 +11002478 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002479 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2480 clear_bit(Blocked, &rdev->flags);
2481
2482 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002483 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002484 clear_bit(BlockedBadBlocks, &rdev->flags);
2485 wake_up(&rdev->blocked_wait);
2486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Andre Noll7f6ce762008-03-23 18:34:54 +01002489/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002490 * We want to accept with case. For this we use cmd_match.
2491 */
2492static int cmd_match(const char *cmd, const char *str)
2493{
2494 /* See if cmd, written into a sysfs file, matches
2495 * str. They must either be the same, or cmd can
2496 * have a trailing newline
2497 */
2498 while (*cmd && *str && *cmd == *str) {
2499 cmd++;
2500 str++;
2501 }
2502 if (*cmd == '\n')
2503 cmd++;
2504 if (*str || *cmd)
2505 return 0;
2506 return 1;
2507}
2508
NeilBrown86e6ffd2005-11-08 21:39:24 -08002509struct rdev_sysfs_entry {
2510 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002511 ssize_t (*show)(struct md_rdev *, char *);
2512 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002513};
2514
2515static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002516state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002517{
2518 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002519 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002520
NeilBrownde393cd2011-07-28 11:31:48 +10002521 if (test_bit(Faulty, &rdev->flags) ||
2522 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523 len+= sprintf(page+len, "%sfaulty",sep);
2524 sep = ",";
2525 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002526 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002527 len += sprintf(page+len, "%sin_sync",sep);
2528 sep = ",";
2529 }
NeilBrownf6556752006-06-26 00:28:01 -07002530 if (test_bit(WriteMostly, &rdev->flags)) {
2531 len += sprintf(page+len, "%swrite_mostly",sep);
2532 sep = ",";
2533 }
NeilBrownde393cd2011-07-28 11:31:48 +10002534 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002535 (rdev->badblocks.unacked_exist
2536 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002537 len += sprintf(page+len, "%sblocked", sep);
2538 sep = ",";
2539 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002540 if (!test_bit(Faulty, &rdev->flags) &&
2541 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002542 len += sprintf(page+len, "%sspare", sep);
2543 sep = ",";
2544 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002545 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2546 len += sprintf(page+len, "%swrite_error", sep);
2547 sep = ",";
2548 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002549 if (test_bit(WantReplacement, &rdev->flags)) {
2550 len += sprintf(page+len, "%swant_replacement", sep);
2551 sep = ",";
2552 }
2553 if (test_bit(Replacement, &rdev->flags)) {
2554 len += sprintf(page+len, "%sreplacement", sep);
2555 sep = ",";
2556 }
2557
NeilBrown86e6ffd2005-11-08 21:39:24 -08002558 return len+sprintf(page+len, "\n");
2559}
2560
NeilBrown45dc2de2006-06-26 00:27:58 -07002561static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002562state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002563{
2564 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002565 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002566 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002567 * writemostly - sets write_mostly
2568 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002569 * blocked - sets the Blocked flags
2570 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002571 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002572 * -insync - clear Insync for a device with a slot assigned,
2573 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002574 * write_error - sets WriteErrorSeen
2575 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002576 */
2577 int err = -EINVAL;
2578 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2579 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002580 if (test_bit(Faulty, &rdev->flags))
2581 err = 0;
2582 else
2583 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002584 } else if (cmd_match(buf, "remove")) {
2585 if (rdev->raid_disk >= 0)
2586 err = -EBUSY;
2587 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002588 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002589 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002590 if (mddev->pers)
2591 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002592 md_new_event(mddev);
2593 err = 0;
2594 }
NeilBrownf6556752006-06-26 00:28:01 -07002595 } else if (cmd_match(buf, "writemostly")) {
2596 set_bit(WriteMostly, &rdev->flags);
2597 err = 0;
2598 } else if (cmd_match(buf, "-writemostly")) {
2599 clear_bit(WriteMostly, &rdev->flags);
2600 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002601 } else if (cmd_match(buf, "blocked")) {
2602 set_bit(Blocked, &rdev->flags);
2603 err = 0;
2604 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002605 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002606 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002607 /* metadata handler doesn't understand badblocks,
2608 * so we need to fail the device
2609 */
2610 md_error(rdev->mddev, rdev);
2611 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002612 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002613 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002614 wake_up(&rdev->blocked_wait);
2615 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2616 md_wakeup_thread(rdev->mddev->thread);
2617
2618 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002619 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2620 set_bit(In_sync, &rdev->flags);
2621 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002622 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002623 if (rdev->mddev->pers == NULL) {
2624 clear_bit(In_sync, &rdev->flags);
2625 rdev->saved_raid_disk = rdev->raid_disk;
2626 rdev->raid_disk = -1;
2627 err = 0;
2628 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002629 } else if (cmd_match(buf, "write_error")) {
2630 set_bit(WriteErrorSeen, &rdev->flags);
2631 err = 0;
2632 } else if (cmd_match(buf, "-write_error")) {
2633 clear_bit(WriteErrorSeen, &rdev->flags);
2634 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002635 } else if (cmd_match(buf, "want_replacement")) {
2636 /* Any non-spare device that is not a replacement can
2637 * become want_replacement at any time, but we then need to
2638 * check if recovery is needed.
2639 */
2640 if (rdev->raid_disk >= 0 &&
2641 !test_bit(Replacement, &rdev->flags))
2642 set_bit(WantReplacement, &rdev->flags);
2643 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2644 md_wakeup_thread(rdev->mddev->thread);
2645 err = 0;
2646 } else if (cmd_match(buf, "-want_replacement")) {
2647 /* Clearing 'want_replacement' is always allowed.
2648 * Once replacements starts it is too late though.
2649 */
2650 err = 0;
2651 clear_bit(WantReplacement, &rdev->flags);
2652 } else if (cmd_match(buf, "replacement")) {
2653 /* Can only set a device as a replacement when array has not
2654 * yet been started. Once running, replacement is automatic
2655 * from spares, or by assigning 'slot'.
2656 */
2657 if (rdev->mddev->pers)
2658 err = -EBUSY;
2659 else {
2660 set_bit(Replacement, &rdev->flags);
2661 err = 0;
2662 }
2663 } else if (cmd_match(buf, "-replacement")) {
2664 /* Similarly, can only clear Replacement before start */
2665 if (rdev->mddev->pers)
2666 err = -EBUSY;
2667 else {
2668 clear_bit(Replacement, &rdev->flags);
2669 err = 0;
2670 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002671 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002672 if (!err)
2673 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002674 return err ? err : len;
2675}
NeilBrown80ca3a42006-07-10 04:44:18 -07002676static struct rdev_sysfs_entry rdev_state =
2677__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002678
2679static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002680errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002681{
2682 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2683}
2684
2685static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002686errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002687{
2688 char *e;
2689 unsigned long n = simple_strtoul(buf, &e, 10);
2690 if (*buf && (*e == 0 || *e == '\n')) {
2691 atomic_set(&rdev->corrected_errors, n);
2692 return len;
2693 }
2694 return -EINVAL;
2695}
2696static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002697__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002698
NeilBrown014236d2006-01-06 00:20:55 -08002699static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002700slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002701{
2702 if (rdev->raid_disk < 0)
2703 return sprintf(page, "none\n");
2704 else
2705 return sprintf(page, "%d\n", rdev->raid_disk);
2706}
2707
2708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002710{
2711 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002712 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002713 int slot = simple_strtoul(buf, &e, 10);
2714 if (strncmp(buf, "none", 4)==0)
2715 slot = -1;
2716 else if (e==buf || (*e && *e!= '\n'))
2717 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002718 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002719 /* Setting 'slot' on an active array requires also
2720 * updating the 'rd%d' link, and communicating
2721 * with the personality with ->hot_*_disk.
2722 * For now we only support removing
2723 * failed/spare devices. This normally happens automatically,
2724 * but not when the metadata is externally managed.
2725 */
NeilBrownc303da62008-02-06 01:39:51 -08002726 if (rdev->raid_disk == -1)
2727 return -EEXIST;
2728 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002729 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002730 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002731 clear_bit(Blocked, &rdev->flags);
2732 remove_and_add_spares(rdev->mddev, rdev);
2733 if (rdev->raid_disk >= 0)
2734 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002735 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2736 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002737 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002738 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002739 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002740 */
2741
2742 if (rdev->raid_disk != -1)
2743 return -EBUSY;
2744
NeilBrownc6751b22011-02-02 11:57:13 +11002745 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2746 return -EBUSY;
2747
Neil Brown6c2fce22008-06-28 08:31:31 +10002748 if (rdev->mddev->pers->hot_add_disk == NULL)
2749 return -EINVAL;
2750
NeilBrownba1b41b2011-01-14 09:14:34 +11002751 if (slot >= rdev->mddev->raid_disks &&
2752 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2753 return -ENOSPC;
2754
Neil Brown6c2fce22008-06-28 08:31:31 +10002755 rdev->raid_disk = slot;
2756 if (test_bit(In_sync, &rdev->flags))
2757 rdev->saved_raid_disk = slot;
2758 else
2759 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002760 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002761 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002762 err = rdev->mddev->pers->
2763 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002764 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002765 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 return err;
Neil Brown52664732008-06-28 08:31:44 +10002767 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002768 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002769 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002770 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002771 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002772 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002773 if (slot >= rdev->mddev->raid_disks &&
2774 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002775 return -ENOSPC;
2776 rdev->raid_disk = slot;
2777 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002778 clear_bit(Faulty, &rdev->flags);
2779 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002780 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002781 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002782 }
NeilBrown014236d2006-01-06 00:20:55 -08002783 return len;
2784}
2785
NeilBrown014236d2006-01-06 00:20:55 -08002786static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002787__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002788
NeilBrown93c8cad2006-01-06 00:20:56 -08002789static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002790offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002791{
Andrew Morton6961ece2006-01-06 00:20:59 -08002792 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002793}
2794
2795static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002796offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002797{
NeilBrownc6563a82012-05-21 09:27:00 +10002798 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002799 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002800 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002801 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002802 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002803 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002804 /* Must set offset before size, so overlap checks
2805 * can be sane */
2806 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002807 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002808 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002809 return len;
2810}
2811
2812static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002813__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002814
NeilBrownc6563a82012-05-21 09:27:00 +10002815static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2816{
2817 return sprintf(page, "%llu\n",
2818 (unsigned long long)rdev->new_data_offset);
2819}
2820
2821static ssize_t new_offset_store(struct md_rdev *rdev,
2822 const char *buf, size_t len)
2823{
2824 unsigned long long new_offset;
2825 struct mddev *mddev = rdev->mddev;
2826
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002827 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002828 return -EINVAL;
2829
2830 if (mddev->sync_thread)
2831 return -EBUSY;
2832 if (new_offset == rdev->data_offset)
2833 /* reset is always permitted */
2834 ;
2835 else if (new_offset > rdev->data_offset) {
2836 /* must not push array size beyond rdev_sectors */
2837 if (new_offset - rdev->data_offset
2838 + mddev->dev_sectors > rdev->sectors)
2839 return -E2BIG;
2840 }
2841 /* Metadata worries about other space details. */
2842
2843 /* decreasing the offset is inconsistent with a backwards
2844 * reshape.
2845 */
2846 if (new_offset < rdev->data_offset &&
2847 mddev->reshape_backwards)
2848 return -EINVAL;
2849 /* Increasing offset is inconsistent with forwards
2850 * reshape. reshape_direction should be set to
2851 * 'backwards' first.
2852 */
2853 if (new_offset > rdev->data_offset &&
2854 !mddev->reshape_backwards)
2855 return -EINVAL;
2856
2857 if (mddev->pers && mddev->persistent &&
2858 !super_types[mddev->major_version]
2859 .allow_new_offset(rdev, new_offset))
2860 return -E2BIG;
2861 rdev->new_data_offset = new_offset;
2862 if (new_offset > rdev->data_offset)
2863 mddev->reshape_backwards = 1;
2864 else if (new_offset < rdev->data_offset)
2865 mddev->reshape_backwards = 0;
2866
2867 return len;
2868}
2869static struct rdev_sysfs_entry rdev_new_offset =
2870__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2871
NeilBrown83303b62006-01-06 00:21:06 -08002872static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002873rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002874{
Andre Nolldd8ac332009-03-31 14:33:13 +11002875 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002876}
2877
NeilBrownc5d79ad2008-02-06 01:39:54 -08002878static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2879{
2880 /* check if two start/length pairs overlap */
2881 if (s1+l1 <= s2)
2882 return 0;
2883 if (s2+l2 <= s1)
2884 return 0;
2885 return 1;
2886}
2887
Dan Williamsb522adc2009-03-31 15:00:31 +11002888static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2889{
2890 unsigned long long blocks;
2891 sector_t new;
2892
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002893 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002894 return -EINVAL;
2895
2896 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2897 return -EINVAL; /* sector conversion overflow */
2898
2899 new = blocks * 2;
2900 if (new != blocks * 2)
2901 return -EINVAL; /* unsigned long long to sector_t overflow */
2902
2903 *sectors = new;
2904 return 0;
2905}
2906
NeilBrown83303b62006-01-06 00:21:06 -08002907static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002908rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002909{
NeilBrownfd01b882011-10-11 16:47:53 +11002910 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002911 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002912 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002913
Dan Williamsb522adc2009-03-31 15:00:31 +11002914 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002915 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002916 if (rdev->data_offset != rdev->new_data_offset)
2917 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002918 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002919 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002920 sectors = super_types[my_mddev->major_version].
2921 rdev_size_change(rdev, sectors);
2922 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002923 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002924 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002925 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002926 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002927 if (!my_mddev->pers->resize)
2928 /* Cannot change size for RAID0 or Linear etc */
2929 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002930 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002931 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002932 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002933
Andre Nolldd8ac332009-03-31 14:33:13 +11002934 rdev->sectors = sectors;
2935 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002936 /* Need to check that all other rdevs with the same
2937 * ->bdev do not overlap. 'rcu' is sufficient to walk
2938 * the rdev lists safely.
2939 * This check does not provide a hard guarantee, it
2940 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002941 */
NeilBrownfd01b882011-10-11 16:47:53 +11002942 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002943 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002944 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002945
NeilBrown8b1afc32014-09-29 15:33:20 +10002946 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002947 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002948 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002949
NeilBrowndafb20f2012-03-19 12:46:39 +11002950 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002951 if (rdev->bdev == rdev2->bdev &&
2952 rdev != rdev2 &&
2953 overlaps(rdev->data_offset, rdev->sectors,
2954 rdev2->data_offset,
2955 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002956 overlap = 1;
2957 break;
2958 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002959 if (overlap) {
2960 mddev_put(mddev);
2961 break;
2962 }
2963 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002964 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002965 if (overlap) {
2966 /* Someone else could have slipped in a size
2967 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002968 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002969 * safe, and trust userspace not to race with
2970 * itself
2971 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002972 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002973 return -EBUSY;
2974 }
2975 }
NeilBrown83303b62006-01-06 00:21:06 -08002976 return len;
2977}
2978
2979static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002980__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002981
NeilBrown3cb03002011-10-11 16:45:26 +11002982static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002983{
2984 unsigned long long recovery_start = rdev->recovery_offset;
2985
2986 if (test_bit(In_sync, &rdev->flags) ||
2987 recovery_start == MaxSector)
2988 return sprintf(page, "none\n");
2989
2990 return sprintf(page, "%llu\n", recovery_start);
2991}
2992
NeilBrown3cb03002011-10-11 16:45:26 +11002993static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002994{
2995 unsigned long long recovery_start;
2996
2997 if (cmd_match(buf, "none"))
2998 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002999 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003000 return -EINVAL;
3001
3002 if (rdev->mddev->pers &&
3003 rdev->raid_disk >= 0)
3004 return -EBUSY;
3005
3006 rdev->recovery_offset = recovery_start;
3007 if (recovery_start == MaxSector)
3008 set_bit(In_sync, &rdev->flags);
3009 else
3010 clear_bit(In_sync, &rdev->flags);
3011 return len;
3012}
3013
3014static struct rdev_sysfs_entry rdev_recovery_start =
3015__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3016
NeilBrown16c791a2011-07-28 11:31:47 +10003017static ssize_t
3018badblocks_show(struct badblocks *bb, char *page, int unack);
3019static ssize_t
3020badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3021
NeilBrown3cb03002011-10-11 16:45:26 +11003022static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003023{
3024 return badblocks_show(&rdev->badblocks, page, 0);
3025}
NeilBrown3cb03002011-10-11 16:45:26 +11003026static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003027{
NeilBrownde393cd2011-07-28 11:31:48 +10003028 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3029 /* Maybe that ack was all we needed */
3030 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3031 wake_up(&rdev->blocked_wait);
3032 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003033}
3034static struct rdev_sysfs_entry rdev_bad_blocks =
3035__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3036
NeilBrown3cb03002011-10-11 16:45:26 +11003037static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003038{
3039 return badblocks_show(&rdev->badblocks, page, 1);
3040}
NeilBrown3cb03002011-10-11 16:45:26 +11003041static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003042{
3043 return badblocks_store(&rdev->badblocks, page, len, 1);
3044}
3045static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3046__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3047
NeilBrown86e6ffd2005-11-08 21:39:24 -08003048static struct attribute *rdev_default_attrs[] = {
3049 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003050 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003051 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003052 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003053 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003054 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003055 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003056 &rdev_bad_blocks.attr,
3057 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003058 NULL,
3059};
3060static ssize_t
3061rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3062{
3063 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003064 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003065 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003066 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003067
3068 if (!entry->show)
3069 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003070
3071 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3072 if (!rv) {
3073 if (rdev->mddev == NULL)
3074 rv = -EBUSY;
3075 else
3076 rv = entry->show(rdev, page);
3077 mddev_unlock(mddev);
3078 }
3079 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003080}
3081
3082static ssize_t
3083rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3084 const char *page, size_t length)
3085{
3086 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003087 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003088 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003089 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003090
3091 if (!entry->store)
3092 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003093 if (!capable(CAP_SYS_ADMIN))
3094 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003095 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003096 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003097 if (rdev->mddev == NULL)
3098 rv = -EBUSY;
3099 else
3100 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003101 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003102 }
3103 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003104}
3105
3106static void rdev_free(struct kobject *ko)
3107{
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003109 kfree(rdev);
3110}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003111static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003112 .show = rdev_attr_show,
3113 .store = rdev_attr_store,
3114};
3115static struct kobj_type rdev_ktype = {
3116 .release = rdev_free,
3117 .sysfs_ops = &rdev_sysfs_ops,
3118 .default_attrs = rdev_default_attrs,
3119};
3120
NeilBrown3cb03002011-10-11 16:45:26 +11003121int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003122{
3123 rdev->desc_nr = -1;
3124 rdev->saved_raid_disk = -1;
3125 rdev->raid_disk = -1;
3126 rdev->flags = 0;
3127 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003128 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003129 rdev->sb_events = 0;
3130 rdev->last_read_error.tv_sec = 0;
3131 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003132 rdev->sb_loaded = 0;
3133 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003134 atomic_set(&rdev->nr_pending, 0);
3135 atomic_set(&rdev->read_errors, 0);
3136 atomic_set(&rdev->corrected_errors, 0);
3137
3138 INIT_LIST_HEAD(&rdev->same_set);
3139 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003140
3141 /* Add space to store bad block list.
3142 * This reserves the space even on arrays where it cannot
3143 * be used - I wonder if that matters
3144 */
3145 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003146 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003147 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3148 seqlock_init(&rdev->badblocks.lock);
3149 if (rdev->badblocks.page == NULL)
3150 return -ENOMEM;
3151
3152 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003153}
3154EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155/*
3156 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3157 *
3158 * mark the device faulty if:
3159 *
3160 * - the device is nonexistent (zero size)
3161 * - the device has no valid superblock
3162 *
3163 * a faulty rdev _never_ has rdev->sb set.
3164 */
NeilBrown3cb03002011-10-11 16:45:26 +11003165static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 char b[BDEVNAME_SIZE];
3168 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003169 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 sector_t size;
3171
NeilBrown9ffae0c2006-01-06 00:20:32 -08003172 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (!rdev) {
3174 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3175 return ERR_PTR(-ENOMEM);
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
NeilBrown2230dfe2011-07-28 11:31:46 +10003178 err = md_rdev_init(rdev);
3179 if (err)
3180 goto abort_free;
3181 err = alloc_disk_sb(rdev);
3182 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 goto abort_free;
3184
NeilBrownc5d79ad2008-02-06 01:39:54 -08003185 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (err)
3187 goto abort_free;
3188
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003189 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003190
Mike Snitzer77304d22010-11-08 14:39:12 +01003191 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003193 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 "md: %s has zero or unknown size, marking faulty!\n",
3195 bdevname(rdev->bdev,b));
3196 err = -EINVAL;
3197 goto abort_free;
3198 }
3199
3200 if (super_format >= 0) {
3201 err = super_types[super_format].
3202 load_super(rdev, NULL, super_minor);
3203 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003204 printk(KERN_WARNING
3205 "md: %s does not have a valid v%d.%d "
3206 "superblock, not importing!\n",
3207 bdevname(rdev->bdev,b),
3208 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 goto abort_free;
3210 }
3211 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003212 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 "md: could not read %s's sb, not importing!\n",
3214 bdevname(rdev->bdev,b));
3215 goto abort_free;
3216 }
3217 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return rdev;
3220
3221abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003222 if (rdev->bdev)
3223 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003224 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 kfree(rdev);
3226 return ERR_PTR(err);
3227}
3228
3229/*
3230 * Check a full RAID array for plausibility
3231 */
3232
NeilBrownf72ffdd2014-09-30 14:23:59 +10003233static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003236 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 char b[BDEVNAME_SIZE];
3238
3239 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003240 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 switch (super_types[mddev->major_version].
3242 load_super(rdev, freshest, mddev->minor_version)) {
3243 case 1:
3244 freshest = rdev;
3245 break;
3246 case 0:
3247 break;
3248 default:
3249 printk( KERN_ERR \
3250 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003251 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 bdevname(rdev->bdev,b));
3253 kick_rdev_from_array(rdev);
3254 }
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 super_types[mddev->major_version].
3257 validate_super(mddev, freshest);
3258
3259 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003260 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003261 if (mddev->max_disks &&
3262 (rdev->desc_nr >= mddev->max_disks ||
3263 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003264 printk(KERN_WARNING
3265 "md: %s: %s: only %d devices permitted\n",
3266 mdname(mddev), bdevname(rdev->bdev, b),
3267 mddev->max_disks);
3268 kick_rdev_from_array(rdev);
3269 continue;
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (rdev != freshest)
3272 if (super_types[mddev->major_version].
3273 validate_super(mddev, rdev)) {
3274 printk(KERN_WARNING "md: kicking non-fresh %s"
3275 " from array!\n",
3276 bdevname(rdev->bdev,b));
3277 kick_rdev_from_array(rdev);
3278 continue;
3279 }
3280 if (mddev->level == LEVEL_MULTIPATH) {
3281 rdev->desc_nr = i++;
3282 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003283 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003284 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003285 rdev->raid_disk = -1;
3286 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
NeilBrown72e02072009-12-14 12:49:55 +11003291/* Read a fixed-point number.
3292 * Numbers in sysfs attributes should be in "standard" units where
3293 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003294 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003295 * milliseconds or jiffies.
3296 * This function takes a decimal number with a possible fractional
3297 * component, and produces an integer which is the result of
3298 * multiplying that number by 10^'scale'.
3299 * all without any floating-point arithmetic.
3300 */
3301int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3302{
3303 unsigned long result = 0;
3304 long decimals = -1;
3305 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3306 if (*cp == '.')
3307 decimals = 0;
3308 else if (decimals < scale) {
3309 unsigned int value;
3310 value = *cp - '0';
3311 result = result * 10 + value;
3312 if (decimals >= 0)
3313 decimals++;
3314 }
3315 cp++;
3316 }
3317 if (*cp == '\n')
3318 cp++;
3319 if (*cp)
3320 return -EINVAL;
3321 if (decimals < 0)
3322 decimals = 0;
3323 while (decimals < scale) {
3324 result *= 10;
3325 decimals ++;
3326 }
3327 *res = result;
3328 return 0;
3329}
3330
NeilBrown19052c02008-08-05 15:54:13 +10003331static void md_safemode_timeout(unsigned long data);
3332
NeilBrowneae17012005-11-08 21:39:23 -08003333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003334safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003335{
3336 int msec = (mddev->safemode_delay*1000)/HZ;
3337 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3338}
3339static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003340safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003341{
NeilBrown16f17b32006-06-26 00:27:37 -07003342 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003343
NeilBrown72e02072009-12-14 12:49:55 +11003344 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003345 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003346 if (msec == 0)
3347 mddev->safemode_delay = 0;
3348 else {
NeilBrown19052c02008-08-05 15:54:13 +10003349 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003350 mddev->safemode_delay = (msec*HZ)/1000;
3351 if (mddev->safemode_delay == 0)
3352 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003353 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003354 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003355 }
3356 return len;
3357}
3358static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003359__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003360
3361static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003362level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003363{
NeilBrown84fc4b52011-10-11 16:49:58 +11003364 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003365 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003366 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003367 else if (mddev->clevel[0])
3368 return sprintf(page, "%s\n", mddev->clevel);
3369 else if (mddev->level != LEVEL_NONE)
3370 return sprintf(page, "%d\n", mddev->level);
3371 else
3372 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003373}
3374
NeilBrownd9d166c2006-01-06 00:20:51 -08003375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003376level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003377{
Dan Williamsf2859af2010-05-02 10:04:16 -07003378 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003379 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003380 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003381 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003382 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003383 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003384
3385 if (mddev->pers == NULL) {
3386 if (len == 0)
3387 return 0;
3388 if (len >= sizeof(mddev->clevel))
3389 return -ENOSPC;
3390 strncpy(mddev->clevel, buf, len);
3391 if (mddev->clevel[len-1] == '\n')
3392 len--;
3393 mddev->clevel[len] = 0;
3394 mddev->level = LEVEL_NONE;
3395 return rv;
3396 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003397 if (mddev->ro)
3398 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003399
3400 /* request to change the personality. Need to ensure:
3401 * - array is not engaged in resync/recovery/reshape
3402 * - old personality can be suspended
3403 * - new personality will access other array.
3404 */
3405
NeilBrownbb4f1e92010-08-08 21:18:03 +10003406 if (mddev->sync_thread ||
3407 mddev->reshape_position != MaxSector ||
3408 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003409 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003410
3411 if (!mddev->pers->quiesce) {
3412 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3413 mdname(mddev), mddev->pers->name);
3414 return -EINVAL;
3415 }
3416
3417 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003418 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003419 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003420 strncpy(clevel, buf, len);
3421 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003422 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003423 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003424 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003425 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003426
Dan Williamsf2859af2010-05-02 10:04:16 -07003427 if (request_module("md-%s", clevel) != 0)
3428 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003429 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003430 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003431 if (!pers || !try_module_get(pers->owner)) {
3432 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003433 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003434 return -EINVAL;
3435 }
3436 spin_unlock(&pers_lock);
3437
3438 if (pers == mddev->pers) {
3439 /* Nothing to do! */
3440 module_put(pers->owner);
3441 return rv;
3442 }
3443 if (!pers->takeover) {
3444 module_put(pers->owner);
3445 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003446 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003447 return -EINVAL;
3448 }
3449
NeilBrowndafb20f2012-03-19 12:46:39 +11003450 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003451 rdev->new_raid_disk = rdev->raid_disk;
3452
NeilBrown245f46c2009-03-31 14:39:39 +11003453 /* ->takeover must set new_* and/or delta_disks
3454 * if it succeeds, and may set them when it fails.
3455 */
3456 priv = pers->takeover(mddev);
3457 if (IS_ERR(priv)) {
3458 mddev->new_level = mddev->level;
3459 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003460 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003461 mddev->raid_disks -= mddev->delta_disks;
3462 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003463 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003464 module_put(pers->owner);
3465 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003466 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003467 return PTR_ERR(priv);
3468 }
3469
3470 /* Looks like we have a winner */
3471 mddev_suspend(mddev);
3472 mddev->pers->stop(mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003473
NeilBrowna64c8762010-04-14 17:15:37 +10003474 if (mddev->pers->sync_request == NULL &&
3475 pers->sync_request != NULL) {
3476 /* need to add the md_redundancy_group */
3477 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3478 printk(KERN_WARNING
3479 "md: cannot register extra attributes for %s\n",
3480 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003481 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrownf72ffdd2014-09-30 14:23:59 +10003482 }
NeilBrowna64c8762010-04-14 17:15:37 +10003483 if (mddev->pers->sync_request != NULL &&
3484 pers->sync_request == NULL) {
3485 /* need to remove the md_redundancy_group */
3486 if (mddev->to_remove == NULL)
3487 mddev->to_remove = &md_redundancy_group;
3488 }
3489
Trela Maciej54071b32010-03-08 16:02:42 +11003490 if (mddev->pers->sync_request == NULL &&
3491 mddev->external) {
3492 /* We are converting from a no-redundancy array
3493 * to a redundancy array and metadata is managed
3494 * externally so we need to be sure that writes
3495 * won't block due to a need to transition
3496 * clean->dirty
3497 * until external management is started.
3498 */
3499 mddev->in_sync = 0;
3500 mddev->safemode_delay = 0;
3501 mddev->safemode = 0;
3502 }
3503
NeilBrowndafb20f2012-03-19 12:46:39 +11003504 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003505 if (rdev->raid_disk < 0)
3506 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003507 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003508 rdev->new_raid_disk = -1;
3509 if (rdev->new_raid_disk == rdev->raid_disk)
3510 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003511 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003512 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003513 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003514 if (rdev->raid_disk < 0)
3515 continue;
3516 if (rdev->new_raid_disk == rdev->raid_disk)
3517 continue;
3518 rdev->raid_disk = rdev->new_raid_disk;
3519 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003520 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003521 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003522 if (sysfs_link_rdev(mddev, rdev))
3523 printk(KERN_WARNING "md: cannot register rd%d"
3524 " for %s after level change\n",
3525 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003526 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003527 }
3528
3529 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003530 mddev->pers = pers;
3531 mddev->private = priv;
3532 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3533 mddev->level = mddev->new_level;
3534 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003535 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003536 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003537 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003538 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003539 if (mddev->pers->sync_request == NULL) {
3540 /* this is now an array without redundancy, so
3541 * it must always be in_sync
3542 */
3543 mddev->in_sync = 1;
3544 del_timer_sync(&mddev->safemode_timer);
3545 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003546 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003547 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003548 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003549 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003550 if (!mddev->thread)
3551 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003552 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003553 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003554 return rv;
3555}
3556
3557static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003558__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003559
NeilBrownd4dbd022006-06-26 00:27:59 -07003560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003561layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003562{
3563 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003564 if (mddev->reshape_position != MaxSector &&
3565 mddev->layout != mddev->new_layout)
3566 return sprintf(page, "%d (%d)\n",
3567 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003568 return sprintf(page, "%d\n", mddev->layout);
3569}
3570
3571static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003572layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003573{
3574 char *e;
3575 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003576
3577 if (!*buf || (*e && *e != '\n'))
3578 return -EINVAL;
3579
NeilBrownb3546032009-03-31 14:56:41 +11003580 if (mddev->pers) {
3581 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003582 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003583 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003584 if (mddev->ro)
3585 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003586 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003587 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003588 if (err) {
3589 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003590 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003591 }
NeilBrownb3546032009-03-31 14:56:41 +11003592 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003593 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003594 if (mddev->reshape_position == MaxSector)
3595 mddev->layout = n;
3596 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003597 return len;
3598}
3599static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003600__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003601
NeilBrowneae17012005-11-08 21:39:23 -08003602static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003603raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003604{
NeilBrownbb636542005-11-08 21:39:45 -08003605 if (mddev->raid_disks == 0)
3606 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003607 if (mddev->reshape_position != MaxSector &&
3608 mddev->delta_disks != 0)
3609 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3610 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003611 return sprintf(page, "%d\n", mddev->raid_disks);
3612}
3613
NeilBrownfd01b882011-10-11 16:47:53 +11003614static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003615
3616static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003617raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003618{
NeilBrownda943b992006-01-06 00:20:54 -08003619 char *e;
3620 int rv = 0;
3621 unsigned long n = simple_strtoul(buf, &e, 10);
3622
3623 if (!*buf || (*e && *e != '\n'))
3624 return -EINVAL;
3625
3626 if (mddev->pers)
3627 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003628 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003629 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003630 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003631
3632 rdev_for_each(rdev, mddev) {
3633 if (olddisks < n &&
3634 rdev->data_offset < rdev->new_data_offset)
3635 return -EINVAL;
3636 if (olddisks > n &&
3637 rdev->data_offset > rdev->new_data_offset)
3638 return -EINVAL;
3639 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003640 mddev->delta_disks = n - olddisks;
3641 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003642 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003643 } else
NeilBrownda943b992006-01-06 00:20:54 -08003644 mddev->raid_disks = n;
3645 return rv ? rv : len;
3646}
3647static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003648__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003649
NeilBrown24dd4692005-11-08 21:39:26 -08003650static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003651chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003652{
NeilBrown08a02ec2007-05-09 02:35:38 -07003653 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003654 mddev->chunk_sectors != mddev->new_chunk_sectors)
3655 return sprintf(page, "%d (%d)\n",
3656 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003657 mddev->chunk_sectors << 9);
3658 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003659}
3660
3661static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003662chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003663{
NeilBrown3b343802006-01-06 00:20:47 -08003664 char *e;
3665 unsigned long n = simple_strtoul(buf, &e, 10);
3666
NeilBrown3b343802006-01-06 00:20:47 -08003667 if (!*buf || (*e && *e != '\n'))
3668 return -EINVAL;
3669
NeilBrownb3546032009-03-31 14:56:41 +11003670 if (mddev->pers) {
3671 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003672 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003673 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003674 if (mddev->ro)
3675 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003676 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003677 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003678 if (err) {
3679 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003680 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003681 }
NeilBrownb3546032009-03-31 14:56:41 +11003682 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003683 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003684 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003685 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003686 }
NeilBrown3b343802006-01-06 00:20:47 -08003687 return len;
3688}
3689static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003690__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003691
NeilBrowna94213b2006-06-26 00:28:00 -07003692static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003693resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003694{
NeilBrownd1a7c502009-03-31 15:24:32 +11003695 if (mddev->recovery_cp == MaxSector)
3696 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003697 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3698}
3699
3700static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003701resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003702{
NeilBrowna94213b2006-06-26 00:28:00 -07003703 char *e;
3704 unsigned long long n = simple_strtoull(buf, &e, 10);
3705
NeilBrownb0986362011-05-11 15:52:21 +10003706 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003707 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003708 if (cmd_match(buf, "none"))
3709 n = MaxSector;
3710 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003711 return -EINVAL;
3712
3713 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003714 if (mddev->pers)
3715 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003716 return len;
3717}
3718static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003719__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003720
NeilBrown9e653b62006-06-26 00:27:58 -07003721/*
3722 * The array state can be:
3723 *
3724 * clear
3725 * No devices, no size, no level
3726 * Equivalent to STOP_ARRAY ioctl
3727 * inactive
3728 * May have some settings, but array is not active
3729 * all IO results in error
3730 * When written, doesn't tear down array, but just stops it
3731 * suspended (not supported yet)
3732 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003733 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003734 * readonly
3735 * no resync can happen. no superblocks get written.
3736 * write requests fail
3737 * read-auto
3738 * like readonly, but behaves like 'clean' on a write request.
3739 *
3740 * clean - no pending writes, but otherwise active.
3741 * When written to inactive array, starts without resync
3742 * If a write request arrives then
3743 * if metadata is known, mark 'dirty' and switch to 'active'.
3744 * if not known, block and switch to write-pending
3745 * If written to an active array that has pending writes, then fails.
3746 * active
3747 * fully active: IO and resync can be happening.
3748 * When written to inactive array, starts with resync
3749 *
3750 * write-pending
3751 * clean, but writes are blocked waiting for 'active' to be written.
3752 *
3753 * active-idle
3754 * like active, but no writes have been seen for a while (100msec).
3755 *
3756 */
3757enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3758 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003759static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003760 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3761 "write-pending", "active-idle", NULL };
3762
3763static int match_word(const char *word, char **list)
3764{
3765 int n;
3766 for (n=0; list[n]; n++)
3767 if (cmd_match(word, list[n]))
3768 break;
3769 return n;
3770}
3771
3772static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003773array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003774{
3775 enum array_state st = inactive;
3776
3777 if (mddev->pers)
3778 switch(mddev->ro) {
3779 case 1:
3780 st = readonly;
3781 break;
3782 case 2:
3783 st = read_auto;
3784 break;
3785 case 0:
3786 if (mddev->in_sync)
3787 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003788 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003789 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003790 else if (mddev->safemode)
3791 st = active_idle;
3792 else
3793 st = active;
3794 }
3795 else {
3796 if (list_empty(&mddev->disks) &&
3797 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003798 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003799 st = clear;
3800 else
3801 st = inactive;
3802 }
3803 return sprintf(page, "%s\n", array_states[st]);
3804}
3805
NeilBrownf72ffdd2014-09-30 14:23:59 +10003806static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3807static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3808static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003809static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003810
3811static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003812array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003813{
3814 int err = -EINVAL;
3815 enum array_state st = match_word(buf, array_states);
3816 switch(st) {
3817 case bad_word:
3818 break;
3819 case clear:
3820 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003821 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003822 break;
3823 case inactive:
3824 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003825 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003826 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003827 else
NeilBrowne6910632008-02-06 01:39:51 -08003828 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003829 break;
3830 case suspended:
3831 break; /* not supported yet */
3832 case readonly:
3833 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003834 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003835 else {
3836 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003837 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003838 err = do_md_run(mddev);
3839 }
3840 break;
3841 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003842 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003843 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003844 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003845 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003846 err = restart_array(mddev);
3847 if (err == 0) {
3848 mddev->ro = 2;
3849 set_disk_ro(mddev->gendisk, 0);
3850 }
NeilBrown9e653b62006-06-26 00:27:58 -07003851 } else {
3852 mddev->ro = 2;
3853 err = do_md_run(mddev);
3854 }
3855 break;
3856 case clean:
3857 if (mddev->pers) {
3858 restart_array(mddev);
3859 spin_lock_irq(&mddev->write_lock);
3860 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003861 if (mddev->in_sync == 0) {
3862 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003863 if (mddev->safemode == 1)
3864 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003865 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003866 }
3867 err = 0;
3868 } else
3869 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003870 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003871 } else
3872 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003873 break;
3874 case active:
3875 if (mddev->pers) {
3876 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003877 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003878 wake_up(&mddev->sb_wait);
3879 err = 0;
3880 } else {
3881 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003882 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003883 err = do_md_run(mddev);
3884 }
3885 break;
3886 case write_pending:
3887 case active_idle:
3888 /* these cannot be set */
3889 break;
3890 }
3891 if (err)
3892 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003893 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003894 if (mddev->hold_active == UNTIL_IOCTL)
3895 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003896 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003897 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003898 }
NeilBrown9e653b62006-06-26 00:27:58 -07003899}
NeilBrown80ca3a42006-07-10 04:44:18 -07003900static struct md_sysfs_entry md_array_state =
3901__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003902
NeilBrown6d7ff7382006-01-06 00:21:16 -08003903static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003904max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003905 return sprintf(page, "%d\n",
3906 atomic_read(&mddev->max_corr_read_errors));
3907}
3908
3909static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003910max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003911{
3912 char *e;
3913 unsigned long n = simple_strtoul(buf, &e, 10);
3914
3915 if (*buf && (*e == 0 || *e == '\n')) {
3916 atomic_set(&mddev->max_corr_read_errors, n);
3917 return len;
3918 }
3919 return -EINVAL;
3920}
3921
3922static struct md_sysfs_entry max_corr_read_errors =
3923__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3924 max_corrected_read_errors_store);
3925
3926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003927null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003928{
3929 return -EINVAL;
3930}
3931
3932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003933new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003934{
3935 /* buf must be %d:%d\n? giving major and minor numbers */
3936 /* The new device is added to the array.
3937 * If the array has a persistent superblock, we read the
3938 * superblock to initialise info and check validity.
3939 * Otherwise, only checking done is that in bind_rdev_to_array,
3940 * which mainly checks size.
3941 */
3942 char *e;
3943 int major = simple_strtoul(buf, &e, 10);
3944 int minor;
3945 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003946 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003947 int err;
3948
3949 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3950 return -EINVAL;
3951 minor = simple_strtoul(e+1, &e, 10);
3952 if (*e && *e != '\n')
3953 return -EINVAL;
3954 dev = MKDEV(major, minor);
3955 if (major != MAJOR(dev) ||
3956 minor != MINOR(dev))
3957 return -EOVERFLOW;
3958
NeilBrown6d7ff7382006-01-06 00:21:16 -08003959 if (mddev->persistent) {
3960 rdev = md_import_device(dev, mddev->major_version,
3961 mddev->minor_version);
3962 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003963 struct md_rdev *rdev0
3964 = list_entry(mddev->disks.next,
3965 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003966 err = super_types[mddev->major_version]
3967 .load_super(rdev, rdev0, mddev->minor_version);
3968 if (err < 0)
3969 goto out;
3970 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003971 } else if (mddev->external)
3972 rdev = md_import_device(dev, -2, -1);
3973 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003974 rdev = md_import_device(dev, -1, -1);
3975
3976 if (IS_ERR(rdev))
3977 return PTR_ERR(rdev);
3978 err = bind_rdev_to_array(rdev, mddev);
3979 out:
3980 if (err)
3981 export_rdev(rdev);
3982 return err ? err : len;
3983}
3984
3985static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003986__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003987
3988static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003989bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003990{
3991 char *end;
3992 unsigned long chunk, end_chunk;
3993
3994 if (!mddev->bitmap)
3995 goto out;
3996 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3997 while (*buf) {
3998 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3999 if (buf == end) break;
4000 if (*end == '-') { /* range */
4001 buf = end + 1;
4002 end_chunk = simple_strtoul(buf, &end, 0);
4003 if (buf == end) break;
4004 }
4005 if (*end && !isspace(*end)) break;
4006 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004007 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004008 }
4009 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4010out:
4011 return len;
4012}
4013
4014static struct md_sysfs_entry md_bitmap =
4015__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4016
4017static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004018size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004019{
Andre Noll58c0fed2009-03-31 14:33:13 +11004020 return sprintf(page, "%llu\n",
4021 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004022}
4023
NeilBrownfd01b882011-10-11 16:47:53 +11004024static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004025
4026static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004027size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004028{
4029 /* If array is inactive, we can reduce the component size, but
4030 * not increase it (except from 0).
4031 * If array is active, we can try an on-line resize
4032 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004033 sector_t sectors;
4034 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004035
Andre Noll58c0fed2009-03-31 14:33:13 +11004036 if (err < 0)
4037 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004038 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004039 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004040 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004041 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004042 if (mddev->dev_sectors == 0 ||
4043 mddev->dev_sectors > sectors)
4044 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004045 else
4046 err = -ENOSPC;
4047 }
4048 return err ? err : len;
4049}
4050
4051static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004052__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004053
Masanari Iida83f0d772012-10-30 00:18:08 +09004054/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004055 * This is one of
4056 * 'none' for arrays with no metadata (good luck...)
4057 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004058 * or N.M for internally known formats
4059 */
4060static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004061metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004062{
4063 if (mddev->persistent)
4064 return sprintf(page, "%d.%d\n",
4065 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004066 else if (mddev->external)
4067 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004068 else
4069 return sprintf(page, "none\n");
4070}
4071
4072static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004073metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004074{
4075 int major, minor;
4076 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004077 /* Changing the details of 'external' metadata is
4078 * always permitted. Otherwise there must be
4079 * no devices attached to the array.
4080 */
4081 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4082 ;
4083 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004084 return -EBUSY;
4085
4086 if (cmd_match(buf, "none")) {
4087 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004088 mddev->external = 0;
4089 mddev->major_version = 0;
4090 mddev->minor_version = 90;
4091 return len;
4092 }
4093 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004094 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004095 if (namelen >= sizeof(mddev->metadata_type))
4096 namelen = sizeof(mddev->metadata_type)-1;
4097 strncpy(mddev->metadata_type, buf+9, namelen);
4098 mddev->metadata_type[namelen] = 0;
4099 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4100 mddev->metadata_type[--namelen] = 0;
4101 mddev->persistent = 0;
4102 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004103 mddev->major_version = 0;
4104 mddev->minor_version = 90;
4105 return len;
4106 }
4107 major = simple_strtoul(buf, &e, 10);
4108 if (e==buf || *e != '.')
4109 return -EINVAL;
4110 buf = e+1;
4111 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004112 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004113 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004114 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115 return -ENOENT;
4116 mddev->major_version = major;
4117 mddev->minor_version = minor;
4118 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004119 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004120 return len;
4121}
4122
4123static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004124__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004125
NeilBrowna35b0d62006-01-06 00:20:49 -08004126static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004127action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004128{
NeilBrown7eec3142005-11-08 21:39:44 -08004129 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004130 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4131 type = "frozen";
4132 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004133 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004134 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4135 type = "reshape";
4136 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004137 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4138 type = "resync";
4139 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4140 type = "check";
4141 else
4142 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004143 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004144 type = "recover";
4145 }
4146 return sprintf(page, "%s\n", type);
4147}
4148
4149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004150action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004151{
NeilBrown7eec3142005-11-08 21:39:44 -08004152 if (!mddev->pers || !mddev->pers->sync_request)
4153 return -EINVAL;
4154
NeilBrownb6a9ce62009-05-26 09:41:17 +10004155 if (cmd_match(page, "frozen"))
4156 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4157 else
4158 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4159
4160 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004161 if (mddev->sync_thread) {
4162 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004163 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004164 }
NeilBrown03c902e2006-01-06 00:20:46 -08004165 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4166 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004167 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004168 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004169 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004170 else if (cmd_match(page, "recover")) {
4171 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4173 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004174 int err;
4175 if (mddev->pers->start_reshape == NULL)
4176 return -EINVAL;
4177 err = mddev->pers->start_reshape(mddev);
4178 if (err)
4179 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004180 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004181 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004182 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004183 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004184 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004185 return -EINVAL;
4186 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4187 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004188 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004189 if (mddev->ro == 2) {
4190 /* A write to sync_action is enough to justify
4191 * canceling read-auto mode
4192 */
4193 mddev->ro = 0;
4194 md_wakeup_thread(mddev->sync_thread);
4195 }
NeilBrown03c902e2006-01-06 00:20:46 -08004196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004197 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004198 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004199 return len;
4200}
4201
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004202static struct md_sysfs_entry md_scan_mode =
4203__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4204
4205static ssize_t
4206last_sync_action_show(struct mddev *mddev, char *page)
4207{
4208 return sprintf(page, "%s\n", mddev->last_sync_action);
4209}
4210
4211static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4212
NeilBrown9d888832005-11-08 21:39:26 -08004213static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004214mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004215{
4216 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004217 (unsigned long long)
4218 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004219}
4220
NeilBrown80ca3a42006-07-10 04:44:18 -07004221static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004222
NeilBrown88202a02006-01-06 00:21:36 -08004223static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004224sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004225{
4226 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4227 mddev->sync_speed_min ? "local": "system");
4228}
4229
4230static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004231sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004232{
4233 int min;
4234 char *e;
4235 if (strncmp(buf, "system", 6)==0) {
4236 mddev->sync_speed_min = 0;
4237 return len;
4238 }
4239 min = simple_strtoul(buf, &e, 10);
4240 if (buf == e || (*e && *e != '\n') || min <= 0)
4241 return -EINVAL;
4242 mddev->sync_speed_min = min;
4243 return len;
4244}
4245
4246static struct md_sysfs_entry md_sync_min =
4247__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4248
4249static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004250sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004251{
4252 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4253 mddev->sync_speed_max ? "local": "system");
4254}
4255
4256static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004257sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004258{
4259 int max;
4260 char *e;
4261 if (strncmp(buf, "system", 6)==0) {
4262 mddev->sync_speed_max = 0;
4263 return len;
4264 }
4265 max = simple_strtoul(buf, &e, 10);
4266 if (buf == e || (*e && *e != '\n') || max <= 0)
4267 return -EINVAL;
4268 mddev->sync_speed_max = max;
4269 return len;
4270}
4271
4272static struct md_sysfs_entry md_sync_max =
4273__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4274
Iustin Popd7f3d292007-10-16 23:30:54 -07004275static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004276degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004277{
4278 return sprintf(page, "%d\n", mddev->degraded);
4279}
4280static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004281
4282static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004283sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004284{
4285 return sprintf(page, "%d\n", mddev->parallel_resync);
4286}
4287
4288static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004289sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004290{
4291 long n;
4292
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004293 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004294 return -EINVAL;
4295
4296 if (n != 0 && n != 1)
4297 return -EINVAL;
4298
4299 mddev->parallel_resync = n;
4300
4301 if (mddev->sync_thread)
4302 wake_up(&resync_wait);
4303
4304 return len;
4305}
4306
4307/* force parallel resync, even with shared block devices */
4308static struct md_sysfs_entry md_sync_force_parallel =
4309__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4310 sync_force_parallel_show, sync_force_parallel_store);
4311
4312static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004313sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004314{
4315 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004316 if (mddev->curr_resync == 0)
4317 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004318 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4319 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004320 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004321 db = resync - mddev->resync_mark_cnt;
4322 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004323}
4324
NeilBrown80ca3a42006-07-10 04:44:18 -07004325static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004326
4327static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004328sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004329{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004330 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004331
NeilBrownacb180b2009-04-14 16:28:34 +10004332 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4333 return sprintf(page, "none\n");
4334
NeilBrown72f36d52012-10-11 14:25:57 +11004335 if (mddev->curr_resync == 1 ||
4336 mddev->curr_resync == 2)
4337 return sprintf(page, "delayed\n");
4338
NeilBrownc804cde2012-05-21 09:28:33 +10004339 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4340 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004341 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004342 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004343 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004344
NeilBrownacb180b2009-04-14 16:28:34 +10004345 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004346 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004347}
4348
NeilBrown80ca3a42006-07-10 04:44:18 -07004349static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004350
NeilBrowne464eaf2006-03-27 01:18:14 -08004351static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004352min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004353{
4354 return sprintf(page, "%llu\n",
4355 (unsigned long long)mddev->resync_min);
4356}
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004359{
4360 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004361 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004362 return -EINVAL;
4363 if (min > mddev->resync_max)
4364 return -EINVAL;
4365 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4366 return -EBUSY;
4367
4368 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004369 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004370 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004371 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004372 return -EINVAL;
4373 }
4374 mddev->resync_min = min;
4375
4376 return len;
4377}
4378
4379static struct md_sysfs_entry md_min_sync =
4380__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4381
4382static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004383max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004384{
4385 if (mddev->resync_max == MaxSector)
4386 return sprintf(page, "max\n");
4387 else
4388 return sprintf(page, "%llu\n",
4389 (unsigned long long)mddev->resync_max);
4390}
4391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004393{
4394 if (strncmp(buf, "max", 3) == 0)
4395 mddev->resync_max = MaxSector;
4396 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004397 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004398 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004399 return -EINVAL;
4400 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004401 return -EINVAL;
4402 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004403 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004404 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4405 return -EBUSY;
4406
4407 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004408 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004409 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004410 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004411 return -EINVAL;
4412 }
4413 mddev->resync_max = max;
4414 }
4415 wake_up(&mddev->recovery_wait);
4416 return len;
4417}
4418
4419static struct md_sysfs_entry md_max_sync =
4420__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4421
4422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004423suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004424{
4425 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4426}
4427
4428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004429suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004430{
4431 char *e;
4432 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004433 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004434
NeilBrownf72ffdd2014-09-30 14:23:59 +10004435 if (mddev->pers == NULL ||
NeilBrownb8d966e2009-07-01 11:14:04 +10004436 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004437 return -EINVAL;
4438 if (buf == e || (*e && *e != '\n'))
4439 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004440
4441 mddev->suspend_lo = new;
4442 if (new >= old)
4443 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004444 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004445 else {
4446 /* Expanding suspended region - need to wait */
4447 mddev->pers->quiesce(mddev, 1);
4448 mddev->pers->quiesce(mddev, 0);
4449 }
4450 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004451}
4452static struct md_sysfs_entry md_suspend_lo =
4453__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4454
NeilBrowne464eaf2006-03-27 01:18:14 -08004455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004456suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004457{
4458 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4459}
4460
4461static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004462suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004463{
4464 char *e;
4465 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004466 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004467
NeilBrownb8d966e2009-07-01 11:14:04 +10004468 if (mddev->pers == NULL ||
4469 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004470 return -EINVAL;
4471 if (buf == e || (*e && *e != '\n'))
4472 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004473
4474 mddev->suspend_hi = new;
4475 if (new <= old)
4476 /* Shrinking suspended region */
4477 mddev->pers->quiesce(mddev, 2);
4478 else {
4479 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004480 mddev->pers->quiesce(mddev, 1);
4481 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004482 }
4483 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004484}
4485static struct md_sysfs_entry md_suspend_hi =
4486__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4487
NeilBrown08a02ec2007-05-09 02:35:38 -07004488static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004489reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004490{
4491 if (mddev->reshape_position != MaxSector)
4492 return sprintf(page, "%llu\n",
4493 (unsigned long long)mddev->reshape_position);
4494 strcpy(page, "none\n");
4495 return 5;
4496}
4497
4498static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004499reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004500{
NeilBrownc6563a82012-05-21 09:27:00 +10004501 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004502 char *e;
4503 unsigned long long new = simple_strtoull(buf, &e, 10);
4504 if (mddev->pers)
4505 return -EBUSY;
4506 if (buf == e || (*e && *e != '\n'))
4507 return -EINVAL;
4508 mddev->reshape_position = new;
4509 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004510 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004511 mddev->new_level = mddev->level;
4512 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004513 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004514 rdev_for_each(rdev, mddev)
4515 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004516 return len;
4517}
4518
4519static struct md_sysfs_entry md_reshape_position =
4520__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4521 reshape_position_store);
4522
Dan Williamsb522adc2009-03-31 15:00:31 +11004523static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004524reshape_direction_show(struct mddev *mddev, char *page)
4525{
4526 return sprintf(page, "%s\n",
4527 mddev->reshape_backwards ? "backwards" : "forwards");
4528}
4529
4530static ssize_t
4531reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4532{
4533 int backwards = 0;
4534 if (cmd_match(buf, "forwards"))
4535 backwards = 0;
4536 else if (cmd_match(buf, "backwards"))
4537 backwards = 1;
4538 else
4539 return -EINVAL;
4540 if (mddev->reshape_backwards == backwards)
4541 return len;
4542
4543 /* check if we are allowed to change */
4544 if (mddev->delta_disks)
4545 return -EBUSY;
4546
4547 if (mddev->persistent &&
4548 mddev->major_version == 0)
4549 return -EINVAL;
4550
4551 mddev->reshape_backwards = backwards;
4552 return len;
4553}
4554
4555static struct md_sysfs_entry md_reshape_direction =
4556__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4557 reshape_direction_store);
4558
4559static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004560array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004561{
4562 if (mddev->external_size)
4563 return sprintf(page, "%llu\n",
4564 (unsigned long long)mddev->array_sectors/2);
4565 else
4566 return sprintf(page, "default\n");
4567}
4568
4569static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004570array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004571{
4572 sector_t sectors;
4573
4574 if (strncmp(buf, "default", 7) == 0) {
4575 if (mddev->pers)
4576 sectors = mddev->pers->size(mddev, 0, 0);
4577 else
4578 sectors = mddev->array_sectors;
4579
4580 mddev->external_size = 0;
4581 } else {
4582 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4583 return -EINVAL;
4584 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004585 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004586
4587 mddev->external_size = 1;
4588 }
4589
4590 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004591 if (mddev->pers) {
4592 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004593 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004594 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004595 return len;
4596}
4597
4598static struct md_sysfs_entry md_array_size =
4599__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4600 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004601
NeilBrowneae17012005-11-08 21:39:23 -08004602static struct attribute *md_default_attrs[] = {
4603 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004604 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004605 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004606 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004607 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004608 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004609 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004610 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004611 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004612 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004613 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004614 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004615 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004616 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004617 NULL,
4618};
4619
4620static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004621 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004622 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004623 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004624 &md_sync_min.attr,
4625 &md_sync_max.attr,
4626 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004627 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004628 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004629 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004630 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004631 &md_suspend_lo.attr,
4632 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004633 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004634 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004635 NULL,
4636};
NeilBrown411036f2005-11-08 21:39:40 -08004637static struct attribute_group md_redundancy_group = {
4638 .name = NULL,
4639 .attrs = md_redundancy_attrs,
4640};
4641
NeilBrowneae17012005-11-08 21:39:23 -08004642static ssize_t
4643md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4644{
4645 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004646 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004647 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004648
4649 if (!entry->show)
4650 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004651 spin_lock(&all_mddevs_lock);
4652 if (list_empty(&mddev->all_mddevs)) {
4653 spin_unlock(&all_mddevs_lock);
4654 return -EBUSY;
4655 }
4656 mddev_get(mddev);
4657 spin_unlock(&all_mddevs_lock);
4658
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004659 rv = mddev_lock(mddev);
4660 if (!rv) {
4661 rv = entry->show(mddev, page);
4662 mddev_unlock(mddev);
4663 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004664 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004665 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004666}
4667
4668static ssize_t
4669md_attr_store(struct kobject *kobj, struct attribute *attr,
4670 const char *page, size_t length)
4671{
4672 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004673 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004674 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004675
4676 if (!entry->store)
4677 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004678 if (!capable(CAP_SYS_ADMIN))
4679 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004680 spin_lock(&all_mddevs_lock);
4681 if (list_empty(&mddev->all_mddevs)) {
4682 spin_unlock(&all_mddevs_lock);
4683 return -EBUSY;
4684 }
4685 mddev_get(mddev);
4686 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004687 if (entry->store == new_dev_store)
4688 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004689 rv = mddev_lock(mddev);
4690 if (!rv) {
4691 rv = entry->store(mddev, page, length);
4692 mddev_unlock(mddev);
4693 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004694 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004695 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004696}
4697
4698static void md_free(struct kobject *ko)
4699{
NeilBrownfd01b882011-10-11 16:47:53 +11004700 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004701
4702 if (mddev->sysfs_state)
4703 sysfs_put(mddev->sysfs_state);
4704
4705 if (mddev->gendisk) {
4706 del_gendisk(mddev->gendisk);
4707 put_disk(mddev->gendisk);
4708 }
4709 if (mddev->queue)
4710 blk_cleanup_queue(mddev->queue);
4711
NeilBrowneae17012005-11-08 21:39:23 -08004712 kfree(mddev);
4713}
4714
Emese Revfy52cf25d2010-01-19 02:58:23 +01004715static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004716 .show = md_attr_show,
4717 .store = md_attr_store,
4718};
4719static struct kobj_type md_ktype = {
4720 .release = md_free,
4721 .sysfs_ops = &md_sysfs_ops,
4722 .default_attrs = md_default_attrs,
4723};
4724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725int mdp_major = 0;
4726
Dan Williams5fd3a172009-03-04 00:57:25 -07004727static void mddev_delayed_delete(struct work_struct *ws)
4728{
NeilBrownfd01b882011-10-11 16:47:53 +11004729 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004730
NeilBrown43a70502009-12-14 12:49:55 +11004731 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004732 kobject_del(&mddev->kobj);
4733 kobject_put(&mddev->kobj);
4734}
4735
NeilBrownefeb53c2009-01-09 08:31:10 +11004736static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004738 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004739 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004741 int partitioned;
4742 int shift;
4743 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004744 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004747 return -ENODEV;
4748
4749 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4750 shift = partitioned ? MdpMinorShift : 0;
4751 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Tejun Heoe804ac72010-10-15 15:36:08 +02004753 /* wait for any previous instance of this device to be
4754 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004755 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004756 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004757
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004758 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004759 error = -EEXIST;
4760 if (mddev->gendisk)
4761 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004762
4763 if (name) {
4764 /* Need to ensure that 'name' is not a duplicate.
4765 */
NeilBrownfd01b882011-10-11 16:47:53 +11004766 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004767 spin_lock(&all_mddevs_lock);
4768
4769 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4770 if (mddev2->gendisk &&
4771 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4772 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004773 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004774 }
4775 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 }
NeilBrown8b765392009-01-09 08:31:08 +11004777
NeilBrown0909dc42009-07-01 12:27:21 +10004778 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004779 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004780 if (!mddev->queue)
4781 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004782 mddev->queue->queuedata = mddev;
4783
NeilBrown409c57f2009-03-31 14:39:39 +11004784 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004785 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 disk = alloc_disk(1 << shift);
4788 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004789 blk_cleanup_queue(mddev->queue);
4790 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004791 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004793 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004795 if (name)
4796 strcpy(disk->disk_name, name);
4797 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004799 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 disk->fops = &md_fops;
4802 disk->private_data = mddev;
4803 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004804 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004805 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004806 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004807 * remove it now.
4808 */
4809 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004811 /* As soon as we call add_disk(), another thread could get
4812 * through to md_open, so make sure it doesn't get too far
4813 */
4814 mutex_lock(&mddev->open_mutex);
4815 add_disk(disk);
4816
Tejun Heoed9e1982008-08-25 19:56:05 +09004817 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4818 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004819 if (error) {
4820 /* This isn't possible, but as kobject_init_and_add is marked
4821 * __must_check, we must do something with the result
4822 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004823 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4824 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004825 error = 0;
4826 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004827 if (mddev->kobj.sd &&
4828 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004829 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004830 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004831 abort:
4832 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004833 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004834 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004835 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004836 }
NeilBrownd3374822009-01-09 08:31:10 +11004837 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004838 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004839}
4840
4841static struct kobject *md_probe(dev_t dev, int *part, void *data)
4842{
4843 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return NULL;
4845}
4846
NeilBrownefeb53c2009-01-09 08:31:10 +11004847static int add_named_array(const char *val, struct kernel_param *kp)
4848{
4849 /* val must be "md_*" where * is not all digits.
4850 * We allocate an array with a large free minor number, and
4851 * set the name to val. val must not already be an active name.
4852 */
4853 int len = strlen(val);
4854 char buf[DISK_NAME_LEN];
4855
4856 while (len && val[len-1] == '\n')
4857 len--;
4858 if (len >= DISK_NAME_LEN)
4859 return -E2BIG;
4860 strlcpy(buf, val, len+1);
4861 if (strncmp(buf, "md_", 3) != 0)
4862 return -EINVAL;
4863 return md_alloc(0, buf);
4864}
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866static void md_safemode_timeout(unsigned long data)
4867{
NeilBrownfd01b882011-10-11 16:47:53 +11004868 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Neil Brown0fd62b82008-06-28 08:31:36 +10004870 if (!atomic_read(&mddev->writes_pending)) {
4871 mddev->safemode = 1;
4872 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004873 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 md_wakeup_thread(mddev->thread);
4876}
4877
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004878static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
NeilBrownfd01b882011-10-11 16:47:53 +11004880int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881{
NeilBrown2604b702006-01-06 00:20:36 -08004882 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004883 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004884 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
NeilBrowna757e642005-04-16 15:26:42 -07004886 if (list_empty(&mddev->disks))
4887 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
4890 if (mddev->pers)
4891 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004892 /* Cannot run until previous stop completes properly */
4893 if (mddev->sysfs_active)
4894 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 /*
4897 * Analyze all RAID superblock(s)
4898 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004899 if (!mddev->raid_disks) {
4900 if (!mddev->persistent)
4901 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004902 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
NeilBrownd9d166c2006-01-06 00:20:51 -08004905 if (mddev->level != LEVEL_NONE)
4906 request_module("md-level-%d", mddev->level);
4907 else if (mddev->clevel[0])
4908 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 /*
4911 * Drop all container device buffers, from now on
4912 * the only valid external interface is through the md
4913 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004915 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004916 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 continue;
4918 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004919 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004920
4921 /* perform some consistency tests on the device.
4922 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004923 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004924 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004925 if (rdev->meta_bdev) {
4926 /* Nothing to check */;
4927 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004928 if (mddev->dev_sectors &&
4929 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004930 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004931 printk("md: %s: data overlaps metadata\n",
4932 mdname(mddev));
4933 return -EINVAL;
4934 }
4935 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004936 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004937 > rdev->data_offset) {
4938 printk("md: %s: metadata overlaps data\n",
4939 mdname(mddev));
4940 return -EINVAL;
4941 }
4942 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004943 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
4945
NeilBrowna167f662010-10-26 18:31:13 +11004946 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004947 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004950 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004951 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004953 if (mddev->level != LEVEL_NONE)
4954 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4955 mddev->level);
4956 else
4957 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4958 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return -EINVAL;
4960 }
NeilBrown2604b702006-01-06 00:20:36 -08004961 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004963 if (mddev->level != pers->level) {
4964 mddev->level = pers->level;
4965 mddev->new_level = pers->level;
4966 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004967 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
NeilBrownf6705572006-03-27 01:18:11 -08004969 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004970 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004971 /* This personality cannot handle reshaping... */
4972 mddev->pers = NULL;
4973 module_put(pers->owner);
4974 return -EINVAL;
4975 }
4976
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004977 if (pers->sync_request) {
4978 /* Warn if this is a potentially silly
4979 * configuration.
4980 */
4981 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004982 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004983 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004984
NeilBrowndafb20f2012-03-19 12:46:39 +11004985 rdev_for_each(rdev, mddev)
4986 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004987 if (rdev < rdev2 &&
4988 rdev->bdev->bd_contains ==
4989 rdev2->bdev->bd_contains) {
4990 printk(KERN_WARNING
4991 "%s: WARNING: %s appears to be"
4992 " on the same physical disk as"
4993 " %s.\n",
4994 mdname(mddev),
4995 bdevname(rdev->bdev,b),
4996 bdevname(rdev2->bdev,b2));
4997 warned = 1;
4998 }
4999 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005000
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005001 if (warned)
5002 printk(KERN_WARNING
5003 "True protection against single-disk"
5004 " failure might be compromised.\n");
5005 }
5006
NeilBrown657390d2005-08-26 18:34:16 -07005007 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005008 /* may be over-ridden by personality */
5009 mddev->resync_max_sectors = mddev->dev_sectors;
5010
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005011 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
NeilBrown0f9552b52009-12-30 12:08:50 +11005013 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005014 mddev->ro = 2; /* read-only, but switch on first write */
5015
NeilBrownb15c2e52006-01-06 00:20:16 -08005016 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005017 if (err)
5018 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005019 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5020 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5021 " but 'external_size' not in effect?\n", __func__);
5022 printk(KERN_ERR
5023 "md: invalid array_size %llu > default size %llu\n",
5024 (unsigned long long)mddev->array_sectors / 2,
5025 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5026 err = -EINVAL;
5027 mddev->pers->stop(mddev);
5028 }
NeilBrownef99bf42012-05-22 13:55:08 +10005029 if (err == 0 && mddev->pers->sync_request &&
5030 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005031 err = bitmap_create(mddev);
5032 if (err) {
5033 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5034 mdname(mddev), err);
5035 mddev->pers->stop(mddev);
5036 }
5037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 module_put(mddev->pers->owner);
5040 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005041 bitmap_destroy(mddev);
5042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005044 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005045 if (mddev->kobj.sd &&
5046 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005047 printk(KERN_WARNING
5048 "md: cannot register extra attributes for %s\n",
5049 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005050 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005051 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005052 mddev->ro = 0;
5053
NeilBrownf72ffdd2014-09-30 14:23:59 +10005054 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005055 atomic_set(&mddev->max_corr_read_errors,
5056 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 mddev->safemode = 0;
5058 mddev->safemode_timer.function = md_safemode_timeout;
5059 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005060 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005062 smp_wmb();
5063 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005064 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005065 if (rdev->raid_disk >= 0)
5066 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005067 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10005068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005070
NeilBrown7a0a5352013-08-27 16:28:23 +10005071 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005072 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
NeilBrownd7603b72006-01-06 00:20:30 -08005074 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005075 sysfs_notify_dirent_safe(mddev->sysfs_state);
5076 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005077 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return 0;
5079}
NeilBrown390ee602010-06-01 19:37:27 +10005080EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
NeilBrownfd01b882011-10-11 16:47:53 +11005082static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005083{
5084 int err;
5085
5086 err = md_run(mddev);
5087 if (err)
5088 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005089 err = bitmap_load(mddev);
5090 if (err) {
5091 bitmap_destroy(mddev);
5092 goto out;
5093 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005094
5095 md_wakeup_thread(mddev->thread);
5096 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5097
NeilBrownfe60b012010-03-29 11:10:42 +11005098 set_capacity(mddev->gendisk, mddev->array_sectors);
5099 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005100 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005101 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5102out:
5103 return err;
5104}
5105
NeilBrownfd01b882011-10-11 16:47:53 +11005106static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107{
5108 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Andre Noll80fab1d2008-07-11 22:02:21 +10005110 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005112 return -ENXIO;
5113 if (!mddev->pers)
5114 return -EINVAL;
5115 if (!mddev->ro)
5116 return -EBUSY;
5117 mddev->safemode = 0;
5118 mddev->ro = 0;
5119 set_disk_ro(disk, 0);
5120 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5121 mdname(mddev));
5122 /* Kick recovery or resync if necessary */
5123 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5124 md_wakeup_thread(mddev->thread);
5125 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005126 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128}
5129
NeilBrownfd01b882011-10-11 16:47:53 +11005130static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005131{
5132 mddev->array_sectors = 0;
5133 mddev->external_size = 0;
5134 mddev->dev_sectors = 0;
5135 mddev->raid_disks = 0;
5136 mddev->recovery_cp = 0;
5137 mddev->resync_min = 0;
5138 mddev->resync_max = MaxSector;
5139 mddev->reshape_position = MaxSector;
5140 mddev->external = 0;
5141 mddev->persistent = 0;
5142 mddev->level = LEVEL_NONE;
5143 mddev->clevel[0] = 0;
5144 mddev->flags = 0;
5145 mddev->ro = 0;
5146 mddev->metadata_type[0] = 0;
5147 mddev->chunk_sectors = 0;
5148 mddev->ctime = mddev->utime = 0;
5149 mddev->layout = 0;
5150 mddev->max_disks = 0;
5151 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005152 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005153 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005154 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005155 mddev->new_level = LEVEL_NONE;
5156 mddev->new_layout = 0;
5157 mddev->new_chunk_sectors = 0;
5158 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005159 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005160 mddev->suspend_lo = mddev->suspend_hi = 0;
5161 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5162 mddev->recovery = 0;
5163 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005164 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005165 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005166 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005167 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005168 mddev->bitmap_info.offset = 0;
5169 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005170 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005171 mddev->bitmap_info.chunksize = 0;
5172 mddev->bitmap_info.daemon_sleep = 0;
5173 mddev->bitmap_info.max_write_behind = 0;
5174}
5175
NeilBrownfd01b882011-10-11 16:47:53 +11005176static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005177{
NeilBrown6b6204e2013-05-09 09:48:30 +10005178 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005179 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005180 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005181 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005182 }
5183
5184 del_timer_sync(&mddev->safemode_timer);
5185
5186 bitmap_flush(mddev);
5187 md_super_wait(mddev);
5188
NeilBrownb6d428c2013-04-24 11:42:42 +10005189 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005190 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005191 /* mark array as shutdown cleanly */
5192 mddev->in_sync = 1;
5193 md_update_sb(mddev, 1);
5194 }
5195}
NeilBrowndefad612011-01-14 09:14:33 +11005196
NeilBrownfd01b882011-10-11 16:47:53 +11005197void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005198{
NeilBrown29f097c2013-11-14 17:54:51 +11005199 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005200 __md_stop_writes(mddev);
5201 mddev_unlock(mddev);
5202}
NeilBrown390ee602010-06-01 19:37:27 +10005203EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005204
NeilBrown5eff3c42012-11-19 10:47:48 +11005205static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005206{
NeilBrown0ca69882011-01-14 09:14:33 +11005207 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005208 mddev->pers->stop(mddev);
5209 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5210 mddev->to_remove = &md_redundancy_group;
5211 module_put(mddev->pers->owner);
5212 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005213 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005214}
NeilBrown5eff3c42012-11-19 10:47:48 +11005215
5216void md_stop(struct mddev *mddev)
5217{
5218 /* stop the array and free an attached data structures.
5219 * This is called from dm-raid
5220 */
5221 __md_stop(mddev);
5222 bitmap_destroy(mddev);
5223 if (mddev->bio_set)
5224 bioset_free(mddev->bio_set);
5225}
5226
NeilBrown390ee602010-06-01 19:37:27 +10005227EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005228
NeilBrowna05b7ea2012-07-19 15:59:18 +10005229static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005230{
5231 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005232 int did_freeze = 0;
5233
5234 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5235 did_freeze = 1;
5236 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5237 md_wakeup_thread(mddev->thread);
5238 }
5239 if (mddev->sync_thread) {
5240 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5241 /* Thread might be blocked waiting for metadata update
5242 * which will now never happen */
5243 wake_up_process(mddev->sync_thread->tsk);
5244 }
5245 mddev_unlock(mddev);
5246 wait_event(resync_wait, mddev->sync_thread == NULL);
5247 mddev_lock_nointr(mddev);
5248
NeilBrowna4bd82d2010-03-29 13:23:10 +11005249 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005250 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005251 mddev->sync_thread ||
5252 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005253 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005254 if (did_freeze) {
5255 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5256 md_wakeup_thread(mddev->thread);
5257 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005258 err = -EBUSY;
5259 goto out;
5260 }
5261 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005262 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005263
5264 err = -ENXIO;
5265 if (mddev->ro==1)
5266 goto out;
5267 mddev->ro = 1;
5268 set_disk_ro(mddev->gendisk, 1);
5269 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005270 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005271 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005272 }
5273out:
5274 mutex_unlock(&mddev->open_mutex);
5275 return err;
5276}
5277
NeilBrown9e653b62006-06-26 00:27:58 -07005278/* mode:
5279 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005280 * 2 - stop but do not disassemble array
5281 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005282static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005283 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005286 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005287 int did_freeze = 0;
5288
5289 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5290 did_freeze = 1;
5291 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5292 md_wakeup_thread(mddev->thread);
5293 }
5294 if (mddev->sync_thread) {
5295 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5296 /* Thread might be blocked waiting for metadata update
5297 * which will now never happen */
5298 wake_up_process(mddev->sync_thread->tsk);
5299 }
5300 mddev_unlock(mddev);
5301 wait_event(resync_wait, mddev->sync_thread == NULL);
5302 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
NeilBrownc8c00a62009-08-10 12:50:52 +10005304 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005305 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005306 mddev->sysfs_active ||
5307 mddev->sync_thread ||
5308 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005309 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005310 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005311 if (did_freeze) {
5312 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5313 md_wakeup_thread(mddev->thread);
5314 }
NeilBrown260fa032013-08-27 16:44:13 +10005315 return -EBUSY;
5316 }
NeilBrown6e17b022010-08-07 21:41:19 +10005317 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005318 if (mddev->ro)
5319 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005320
NeilBrowndefad612011-01-14 09:14:33 +11005321 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005322 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005323 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005324 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005325
NeilBrowna4bd82d2010-03-29 13:23:10 +11005326 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005327 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005328
NeilBrowndafb20f2012-03-19 12:46:39 +11005329 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005330 if (rdev->raid_disk >= 0)
5331 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005332
NeilBrowna4bd82d2010-03-29 13:23:10 +11005333 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005334 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005335 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005336 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005337
NeilBrowna4bd82d2010-03-29 13:23:10 +11005338 if (mddev->ro)
5339 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005340 } else
5341 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 /*
5343 * Free resources if final stop
5344 */
NeilBrown9e653b62006-06-26 00:27:58 -07005345 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5347
NeilBrown978f9462006-02-02 14:28:05 -08005348 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005349 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005350 fput(mddev->bitmap_info.file);
5351 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005352 }
NeilBrownc3d97142009-12-14 12:49:52 +11005353 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005354
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 export_array(mddev);
5356
NeilBrown6177b472010-03-29 11:37:13 +11005357 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005358 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005359 if (mddev->hold_active == UNTIL_STOP)
5360 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005361 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005362 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005363 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005364 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366}
5367
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005368#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005369static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370{
NeilBrown3cb03002011-10-11 16:45:26 +11005371 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 int err;
5373
NeilBrowna757e642005-04-16 15:26:42 -07005374 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 printk(KERN_INFO "md: running: ");
5378
NeilBrowndafb20f2012-03-19 12:46:39 +11005379 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 char b[BDEVNAME_SIZE];
5381 printk("<%s>", bdevname(rdev->bdev,b));
5382 }
5383 printk("\n");
5384
NeilBrownd710e132008-10-13 11:55:12 +11005385 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (err) {
5387 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005388 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 }
5390}
5391
5392/*
5393 * lets try to run arrays based on all disks that have arrived
5394 * until now. (those are in pending_raid_disks)
5395 *
5396 * the method: pick the first pending disk, collect all disks with
5397 * the same UUID, remove all from the pending list and put them into
5398 * the 'same_array' list. Then order this list based on superblock
5399 * update time (freshest comes first), kick out 'old' disks and
5400 * compare superblocks. If everything's fine then run it.
5401 *
5402 * If "unit" is allocated, then bump its reference count
5403 */
5404static void autorun_devices(int part)
5405{
NeilBrown3cb03002011-10-11 16:45:26 +11005406 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005407 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 char b[BDEVNAME_SIZE];
5409
5410 printk(KERN_INFO "md: autorun ...\n");
5411 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005412 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005414 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005416 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 printk(KERN_INFO "md: considering %s ...\n",
5419 bdevname(rdev0->bdev,b));
5420 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005421 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 if (super_90_load(rdev, rdev0, 0) >= 0) {
5423 printk(KERN_INFO "md: adding %s ...\n",
5424 bdevname(rdev->bdev,b));
5425 list_move(&rdev->same_set, &candidates);
5426 }
5427 /*
5428 * now we have a set of devices, with all of them having
5429 * mostly sane superblocks. It's time to allocate the
5430 * mddev.
5431 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005432 if (part) {
5433 dev = MKDEV(mdp_major,
5434 rdev0->preferred_minor << MdpMinorShift);
5435 unit = MINOR(dev) >> MdpMinorShift;
5436 } else {
5437 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5438 unit = MINOR(dev);
5439 }
5440 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5442 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5443 break;
5444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446 md_probe(dev, NULL, NULL);
5447 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005448 if (!mddev || !mddev->gendisk) {
5449 if (mddev)
5450 mddev_put(mddev);
5451 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 "md: cannot allocate memory for md drive.\n");
5453 break;
5454 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005455 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 printk(KERN_WARNING "md: %s locked, cannot run\n",
5457 mdname(mddev));
5458 else if (mddev->raid_disks || mddev->major_version
5459 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005460 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 "md: %s already running, cannot run %s\n",
5462 mdname(mddev), bdevname(rdev0->bdev,b));
5463 mddev_unlock(mddev);
5464 } else {
5465 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005466 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005467 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 list_del_init(&rdev->same_set);
5469 if (bind_rdev_to_array(rdev, mddev))
5470 export_rdev(rdev);
5471 }
5472 autorun_array(mddev);
5473 mddev_unlock(mddev);
5474 }
5475 /* on success, candidates will be empty, on error
5476 * it won't...
5477 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005478 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005479 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 mddev_put(mddev);
5483 }
5484 printk(KERN_INFO "md: ... autorun DONE.\n");
5485}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005486#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
NeilBrownf72ffdd2014-09-30 14:23:59 +10005488static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489{
5490 mdu_version_t ver;
5491
5492 ver.major = MD_MAJOR_VERSION;
5493 ver.minor = MD_MINOR_VERSION;
5494 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5495
5496 if (copy_to_user(arg, &ver, sizeof(ver)))
5497 return -EFAULT;
5498
5499 return 0;
5500}
5501
NeilBrownf72ffdd2014-09-30 14:23:59 +10005502static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
5504 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005505 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005506 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
NeilBrown1ca69c42012-10-11 13:37:33 +11005508 nr = working = insync = failed = spare = 0;
5509 rcu_read_lock();
5510 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005512 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 failed++;
5514 else {
5515 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005516 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005517 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 else
5519 spare++;
5520 }
5521 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005522 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
5524 info.major_version = mddev->major_version;
5525 info.minor_version = mddev->minor_version;
5526 info.patch_version = MD_PATCHLEVEL_VERSION;
5527 info.ctime = mddev->ctime;
5528 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005529 info.size = mddev->dev_sectors / 2;
5530 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005531 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 info.nr_disks = nr;
5533 info.raid_disks = mddev->raid_disks;
5534 info.md_minor = mddev->md_minor;
5535 info.not_persistent= !mddev->persistent;
5536
5537 info.utime = mddev->utime;
5538 info.state = 0;
5539 if (mddev->in_sync)
5540 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005541 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005542 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005543 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 info.working_disks = working;
5545 info.failed_disks = failed;
5546 info.spare_disks = spare;
5547
5548 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005549 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
5551 if (copy_to_user(arg, &info, sizeof(info)))
5552 return -EFAULT;
5553
5554 return 0;
5555}
5556
NeilBrownf72ffdd2014-09-30 14:23:59 +10005557static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005558{
5559 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5560 char *ptr, *buf = NULL;
5561 int err = -ENOMEM;
5562
NeilBrown60559da2013-07-16 16:44:29 +10005563 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005564
NeilBrown32a76272005-06-21 17:17:14 -07005565 if (!file)
5566 goto out;
5567
5568 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005569 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005570 file->pathname[0] = '\0';
5571 goto copy_out;
5572 }
5573
5574 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5575 if (!buf)
5576 goto out;
5577
NeilBrown1ec885c2012-05-22 13:55:10 +10005578 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5579 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005580 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005581 goto out;
5582
5583 strcpy(file->pathname, ptr);
5584
5585copy_out:
5586 err = 0;
5587 if (copy_to_user(arg, file, sizeof(*file)))
5588 err = -EFAULT;
5589out:
5590 kfree(buf);
5591 kfree(file);
5592 return err;
5593}
5594
NeilBrownf72ffdd2014-09-30 14:23:59 +10005595static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
5597 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005598 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
5600 if (copy_from_user(&info, arg, sizeof(info)))
5601 return -EFAULT;
5602
NeilBrown1ca69c42012-10-11 13:37:33 +11005603 rcu_read_lock();
5604 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 if (rdev) {
5606 info.major = MAJOR(rdev->bdev->bd_dev);
5607 info.minor = MINOR(rdev->bdev->bd_dev);
5608 info.raid_disk = rdev->raid_disk;
5609 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005610 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005612 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 info.state |= (1<<MD_DISK_ACTIVE);
5614 info.state |= (1<<MD_DISK_SYNC);
5615 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005616 if (test_bit(WriteMostly, &rdev->flags))
5617 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 } else {
5619 info.major = info.minor = 0;
5620 info.raid_disk = -1;
5621 info.state = (1<<MD_DISK_REMOVED);
5622 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005623 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 if (copy_to_user(arg, &info, sizeof(info)))
5626 return -EFAULT;
5627
5628 return 0;
5629}
5630
NeilBrownf72ffdd2014-09-30 14:23:59 +10005631static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632{
5633 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005634 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 dev_t dev = MKDEV(info->major,info->minor);
5636
5637 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5638 return -EOVERFLOW;
5639
5640 if (!mddev->raid_disks) {
5641 int err;
5642 /* expecting a device which has a superblock */
5643 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5644 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005645 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 "md: md_import_device returned %ld\n",
5647 PTR_ERR(rdev));
5648 return PTR_ERR(rdev);
5649 }
5650 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005651 struct md_rdev *rdev0
5652 = list_entry(mddev->disks.next,
5653 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005654 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 .load_super(rdev, rdev0, mddev->minor_version);
5656 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005657 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005659 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 bdevname(rdev0->bdev,b2));
5661 export_rdev(rdev);
5662 return -EINVAL;
5663 }
5664 }
5665 err = bind_rdev_to_array(rdev, mddev);
5666 if (err)
5667 export_rdev(rdev);
5668 return err;
5669 }
5670
5671 /*
5672 * add_new_disk can be used once the array is assembled
5673 * to add "hot spares". They must already have a superblock
5674 * written
5675 */
5676 if (mddev->pers) {
5677 int err;
5678 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005679 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 "%s: personality does not support diskops!\n",
5681 mdname(mddev));
5682 return -EINVAL;
5683 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005684 if (mddev->persistent)
5685 rdev = md_import_device(dev, mddev->major_version,
5686 mddev->minor_version);
5687 else
5688 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005690 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 "md: md_import_device returned %ld\n",
5692 PTR_ERR(rdev));
5693 return PTR_ERR(rdev);
5694 }
NeilBrown1a855a02010-12-09 16:36:28 +11005695 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005696 if (!mddev->persistent) {
5697 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005698 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005699 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005700 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005701 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005702 } else
NeilBrown41158c72005-06-21 17:17:25 -07005703 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005704 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005705 } else
5706 super_types[mddev->major_version].
5707 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005708 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005709 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005710 /* This was a hot-add request, but events doesn't
5711 * match, so reject it.
5712 */
5713 export_rdev(rdev);
5714 return -EINVAL;
5715 }
5716
NeilBrownb2d444d2005-11-08 21:39:31 -08005717 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005718 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5719 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005720 else
5721 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 rdev->raid_disk = -1;
5724 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005725 if (!err && !mddev->pers->hot_remove_disk) {
5726 /* If there is hot_add_disk but no hot_remove_disk
5727 * then added disks for geometry changes,
5728 * and should be added immediately.
5729 */
5730 super_types[mddev->major_version].
5731 validate_super(mddev, rdev);
5732 err = mddev->pers->hot_add_disk(mddev, rdev);
5733 if (err)
5734 unbind_rdev_from_array(rdev);
5735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (err)
5737 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005738 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005739 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005740
NeilBrown7ceb17e2013-04-24 11:42:42 +10005741 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005742 if (mddev->degraded)
5743 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005744 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005745 if (!err)
5746 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005747 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 return err;
5749 }
5750
5751 /* otherwise, add_new_disk is only allowed
5752 * for major_version==0 superblocks
5753 */
5754 if (mddev->major_version != 0) {
5755 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5756 mdname(mddev));
5757 return -EINVAL;
5758 }
5759
5760 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5761 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005762 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005764 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 "md: error, md_import_device() returned %ld\n",
5766 PTR_ERR(rdev));
5767 return PTR_ERR(rdev);
5768 }
5769 rdev->desc_nr = info->number;
5770 if (info->raid_disk < mddev->raid_disks)
5771 rdev->raid_disk = info->raid_disk;
5772 else
5773 rdev->raid_disk = -1;
5774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005776 if (info->state & (1<<MD_DISK_SYNC))
5777 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005779 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5780 set_bit(WriteMostly, &rdev->flags);
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 if (!mddev->persistent) {
5783 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005784 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5785 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005786 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005787 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
NeilBrown2bf071b2006-01-06 00:20:55 -08005789 err = bind_rdev_to_array(rdev, mddev);
5790 if (err) {
5791 export_rdev(rdev);
5792 return err;
5793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 }
5795
5796 return 0;
5797}
5798
NeilBrownf72ffdd2014-09-30 14:23:59 +10005799static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800{
5801 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005802 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 rdev = find_rdev(mddev, dev);
5805 if (!rdev)
5806 return -ENXIO;
5807
NeilBrown3ea8929d2013-04-24 11:42:41 +10005808 clear_bit(Blocked, &rdev->flags);
5809 remove_and_add_spares(mddev, rdev);
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 if (rdev->raid_disk >= 0)
5812 goto busy;
5813
5814 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005815 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005816 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 return 0;
5819busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005820 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 bdevname(rdev->bdev,b), mdname(mddev));
5822 return -EBUSY;
5823}
5824
NeilBrownf72ffdd2014-09-30 14:23:59 +10005825static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826{
5827 char b[BDEVNAME_SIZE];
5828 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005829 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
5831 if (!mddev->pers)
5832 return -ENODEV;
5833
5834 if (mddev->major_version != 0) {
5835 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5836 " version-0 superblocks.\n",
5837 mdname(mddev));
5838 return -EINVAL;
5839 }
5840 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005841 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 "%s: personality does not support diskops!\n",
5843 mdname(mddev));
5844 return -EINVAL;
5845 }
5846
NeilBrownd710e132008-10-13 11:55:12 +11005847 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005849 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 "md: error, md_import_device() returned %ld\n",
5851 PTR_ERR(rdev));
5852 return -EINVAL;
5853 }
5854
5855 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005856 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005858 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
NeilBrown8190e752009-06-18 08:48:58 +10005860 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
NeilBrownb2d444d2005-11-08 21:39:31 -08005862 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005863 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 "md: can not hot-add faulty %s disk to %s!\n",
5865 bdevname(rdev->bdev,b), mdname(mddev));
5866 err = -EINVAL;
5867 goto abort_export;
5868 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005869 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005871 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005872 err = bind_rdev_to_array(rdev, mddev);
5873 if (err)
5874 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
5876 /*
5877 * The rest should better be atomic, we can have disk failures
5878 * noticed in interrupt contexts ...
5879 */
5880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 rdev->raid_disk = -1;
5882
NeilBrown850b2b422006-10-03 01:15:46 -07005883 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 /*
5886 * Kick recovery, maybe this spare has to be added to the
5887 * array immediately.
5888 */
5889 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5890 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005891 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 return 0;
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894abort_export:
5895 export_rdev(rdev);
5896 return err;
5897}
5898
NeilBrownfd01b882011-10-11 16:47:53 +11005899static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005900{
NeilBrown035328c2014-04-09 12:25:40 +10005901 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005902
NeilBrown36fa3062005-09-09 16:23:45 -07005903 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005904 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005905 return -EBUSY;
5906 if (mddev->recovery || mddev->sync_thread)
5907 return -EBUSY;
5908 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005909 }
5910
NeilBrown36fa3062005-09-09 16:23:45 -07005911 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005912 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005913 if (mddev->bitmap)
5914 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005915 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005916
NeilBrownc3d97142009-12-14 12:49:52 +11005917 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005918 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5919 mdname(mddev));
5920 return -EBADF;
5921 }
5922
NeilBrown035328c2014-04-09 12:25:40 +10005923 inode = mddev->bitmap_info.file->f_mapping->host;
5924 if (!S_ISREG(inode->i_mode)) {
5925 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5926 mdname(mddev));
5927 err = -EBADF;
5928 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5929 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5930 mdname(mddev));
5931 err = -EBADF;
5932 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005933 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5934 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005935 err = -EBUSY;
5936 }
5937 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005938 fput(mddev->bitmap_info.file);
5939 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005940 return err;
5941 }
NeilBrownc3d97142009-12-14 12:49:52 +11005942 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005943 } else if (mddev->bitmap == NULL)
5944 return -ENOENT; /* cannot remove what isn't there */
5945 err = 0;
5946 if (mddev->pers) {
5947 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005948 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005949 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005950 if (!err)
5951 err = bitmap_load(mddev);
5952 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005953 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005954 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005955 fd = -1; /* make sure to put the file */
5956 }
NeilBrown36fa3062005-09-09 16:23:45 -07005957 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005958 }
5959 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005960 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11005961 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11005962 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005963 }
5964
NeilBrown32a76272005-06-21 17:17:14 -07005965 return err;
5966}
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968/*
5969 * set_array_info is used two different ways
5970 * The original usage is when creating a new array.
5971 * In this usage, raid_disks is > 0 and it together with
5972 * level, size, not_persistent,layout,chunksize determine the
5973 * shape of the array.
5974 * This will always create an array with a type-0.90.0 superblock.
5975 * The newer usage is when assembling an array.
5976 * In this case raid_disks will be 0, and the major_version field is
5977 * use to determine which style super-blocks are to be found on the devices.
5978 * The minor and patch _version numbers are also kept incase the
5979 * super_block handler wishes to interpret them.
5980 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005981static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982{
5983
5984 if (info->raid_disks == 0) {
5985 /* just setting version number for superblock loading */
5986 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005987 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 super_types[info->major_version].name == NULL) {
5989 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005990 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 "md: superblock version %d not known\n",
5992 info->major_version);
5993 return -EINVAL;
5994 }
5995 mddev->major_version = info->major_version;
5996 mddev->minor_version = info->minor_version;
5997 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005998 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005999 /* ensure mddev_put doesn't delete this now that there
6000 * is some minimal configuration.
6001 */
6002 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 return 0;
6004 }
6005 mddev->major_version = MD_MAJOR_VERSION;
6006 mddev->minor_version = MD_MINOR_VERSION;
6007 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6008 mddev->ctime = get_seconds();
6009
6010 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006011 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006012 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 mddev->raid_disks = info->raid_disks;
6014 /* don't set md_minor, it is determined by which /dev/md* was
6015 * openned
6016 */
6017 if (info->state & (1<<MD_SB_CLEAN))
6018 mddev->recovery_cp = MaxSector;
6019 else
6020 mddev->recovery_cp = 0;
6021 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006022 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
6024 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006025 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
6027 mddev->max_disks = MD_SB_DISKS;
6028
NeilBrowne6910632008-02-06 01:39:51 -08006029 if (mddev->persistent)
6030 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006031 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
NeilBrownc3d97142009-12-14 12:49:52 +11006033 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006034 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006035 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006036
NeilBrownf6705572006-03-27 01:18:11 -08006037 mddev->reshape_position = MaxSector;
6038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 /*
6040 * Generate a 128 bit UUID
6041 */
6042 get_random_bytes(mddev->uuid, 16);
6043
NeilBrownf6705572006-03-27 01:18:11 -08006044 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006045 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006046 mddev->new_layout = mddev->layout;
6047 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006048 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return 0;
6051}
6052
NeilBrownfd01b882011-10-11 16:47:53 +11006053void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006054{
Dan Williamsb522adc2009-03-31 15:00:31 +11006055 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6056
6057 if (mddev->external_size)
6058 return;
6059
Dan Williams1f403622009-03-31 14:59:03 +11006060 mddev->array_sectors = array_sectors;
6061}
6062EXPORT_SYMBOL(md_set_array_sectors);
6063
NeilBrownfd01b882011-10-11 16:47:53 +11006064static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006065{
NeilBrown3cb03002011-10-11 16:45:26 +11006066 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006067 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006068 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006069
6070 if (mddev->pers->resize == NULL)
6071 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006072 /* The "num_sectors" is the number of sectors of each device that
6073 * is used. This can only make sense for arrays with redundancy.
6074 * linear and raid0 always use whatever space is available. We can only
6075 * consider changing this number if no resync or reconstruction is
6076 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006077 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006078 * of each device. If num_sectors is zero, we find the largest size
6079 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006080 */
6081 if (mddev->sync_thread)
6082 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006083 if (mddev->ro)
6084 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006085
NeilBrowndafb20f2012-03-19 12:46:39 +11006086 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006087 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006088
Andre Nolld71f9f82008-07-11 22:02:22 +10006089 if (fit && (num_sectors == 0 || num_sectors > avail))
6090 num_sectors = avail;
6091 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006092 return -ENOSPC;
6093 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006094 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006095 if (!rv)
6096 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006097 return rv;
6098}
6099
NeilBrownfd01b882011-10-11 16:47:53 +11006100static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006101{
6102 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006103 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006104 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006105 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006106 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006107 if (mddev->ro)
6108 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006109 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006110 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006111 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006112 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006113 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006114
6115 rdev_for_each(rdev, mddev) {
6116 if (mddev->raid_disks < raid_disks &&
6117 rdev->data_offset < rdev->new_data_offset)
6118 return -EINVAL;
6119 if (mddev->raid_disks > raid_disks &&
6120 rdev->data_offset > rdev->new_data_offset)
6121 return -EINVAL;
6122 }
6123
NeilBrown63c70c42006-03-27 01:18:13 -08006124 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006125 if (mddev->delta_disks < 0)
6126 mddev->reshape_backwards = 1;
6127 else if (mddev->delta_disks > 0)
6128 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006129
6130 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006131 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006132 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006133 mddev->reshape_backwards = 0;
6134 }
NeilBrownda943b992006-01-06 00:20:54 -08006135 return rv;
6136}
6137
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138/*
6139 * update_array_info is used to change the configuration of an
6140 * on-line array.
6141 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6142 * fields in the info are checked against the array.
6143 * Any differences that cannot be handled will cause an error.
6144 * Normally, only one change can be managed at a time.
6145 */
NeilBrownfd01b882011-10-11 16:47:53 +11006146static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147{
6148 int rv = 0;
6149 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006150 int state = 0;
6151
6152 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006153 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006154 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155
6156 if (mddev->major_version != info->major_version ||
6157 mddev->minor_version != info->minor_version ||
6158/* mddev->patch_version != info->patch_version || */
6159 mddev->ctime != info->ctime ||
6160 mddev->level != info->level ||
6161/* mddev->layout != info->layout || */
6162 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006163 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006164 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6165 ((state^info->state) & 0xfffffe00)
6166 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 return -EINVAL;
6168 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006169 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6170 cnt++;
6171 if (mddev->raid_disks != info->raid_disks)
6172 cnt++;
6173 if (mddev->layout != info->layout)
6174 cnt++;
6175 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6176 cnt++;
6177 if (cnt == 0)
6178 return 0;
6179 if (cnt > 1)
6180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
6182 if (mddev->layout != info->layout) {
6183 /* Change layout
6184 * we don't need to do anything at the md level, the
6185 * personality will take care of it all.
6186 */
NeilBrown50ac1682009-06-18 08:47:55 +10006187 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006189 else {
6190 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006191 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006192 if (rv)
6193 mddev->new_layout = mddev->layout;
6194 return rv;
6195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006197 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006198 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
NeilBrownda943b992006-01-06 00:20:54 -08006200 if (mddev->raid_disks != info->raid_disks)
6201 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202
NeilBrown36fa3062005-09-09 16:23:45 -07006203 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006204 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006205 return -EINVAL;
6206 if (mddev->recovery || mddev->sync_thread)
6207 return -EBUSY;
6208 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6209 /* add the bitmap */
6210 if (mddev->bitmap)
6211 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006212 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006213 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006214 mddev->bitmap_info.offset =
6215 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006216 mddev->bitmap_info.space =
6217 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006218 mddev->pers->quiesce(mddev, 1);
6219 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006220 if (!rv)
6221 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006222 if (rv)
6223 bitmap_destroy(mddev);
6224 mddev->pers->quiesce(mddev, 0);
6225 } else {
6226 /* remove the bitmap */
6227 if (!mddev->bitmap)
6228 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006229 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006230 return -EINVAL;
6231 mddev->pers->quiesce(mddev, 1);
6232 bitmap_destroy(mddev);
6233 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006234 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006235 }
6236 }
NeilBrown850b2b422006-10-03 01:15:46 -07006237 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 return rv;
6239}
6240
NeilBrownfd01b882011-10-11 16:47:53 +11006241static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242{
NeilBrown3cb03002011-10-11 16:45:26 +11006243 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006244 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 if (mddev->pers == NULL)
6247 return -ENODEV;
6248
NeilBrown1ca69c42012-10-11 13:37:33 +11006249 rcu_read_lock();
6250 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006252 err = -ENODEV;
6253 else {
6254 md_error(mddev, rdev);
6255 if (!test_bit(Faulty, &rdev->flags))
6256 err = -EBUSY;
6257 }
6258 rcu_read_unlock();
6259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
Andre Noll2f9618c2008-04-25 18:57:58 +02006262/*
6263 * We have a problem here : there is no easy way to give a CHS
6264 * virtual geometry. We currently pretend that we have a 2 heads
6265 * 4 sectors (with a BIG number of cylinders...). This drives
6266 * dosfs just mad... ;-)
6267 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006268static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6269{
NeilBrownfd01b882011-10-11 16:47:53 +11006270 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006271
6272 geo->heads = 2;
6273 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006274 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006275 return 0;
6276}
6277
Nicolas Schichancb335f82014-01-15 16:58:52 +01006278static inline bool md_ioctl_valid(unsigned int cmd)
6279{
6280 switch (cmd) {
6281 case ADD_NEW_DISK:
6282 case BLKROSET:
6283 case GET_ARRAY_INFO:
6284 case GET_BITMAP_FILE:
6285 case GET_DISK_INFO:
6286 case HOT_ADD_DISK:
6287 case HOT_REMOVE_DISK:
6288 case PRINT_RAID_DEBUG:
6289 case RAID_AUTORUN:
6290 case RAID_VERSION:
6291 case RESTART_ARRAY_RW:
6292 case RUN_ARRAY:
6293 case SET_ARRAY_INFO:
6294 case SET_BITMAP_FILE:
6295 case SET_DISK_FAULTY:
6296 case STOP_ARRAY:
6297 case STOP_ARRAY_RO:
6298 return true;
6299 default:
6300 return false;
6301 }
6302}
6303
Al Viroa39907f2008-03-02 10:31:15 -05006304static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 unsigned int cmd, unsigned long arg)
6306{
6307 int err = 0;
6308 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006309 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006310 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
Nicolas Schichancb335f82014-01-15 16:58:52 +01006312 if (!md_ioctl_valid(cmd))
6313 return -ENOTTY;
6314
NeilBrown506c9e42011-12-23 10:17:26 +11006315 switch (cmd) {
6316 case RAID_VERSION:
6317 case GET_ARRAY_INFO:
6318 case GET_DISK_INFO:
6319 break;
6320 default:
6321 if (!capable(CAP_SYS_ADMIN))
6322 return -EACCES;
6323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
6325 /*
6326 * Commands dealing with the RAID driver but not any
6327 * particular array:
6328 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006329 switch (cmd) {
6330 case RAID_VERSION:
6331 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrownc02c0ae2012-12-11 13:39:21 +11006334 case PRINT_RAID_DEBUG:
6335 err = 0;
6336 md_print_devices();
NeilBrown3adc28d2014-09-30 15:46:41 +10006337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006340 case RAID_AUTORUN:
6341 err = 0;
6342 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006345 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 }
6347
6348 /*
6349 * Commands creating/starting a new array:
6350 */
6351
Al Viroa39907f2008-03-02 10:31:15 -05006352 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
6354 if (!mddev) {
6355 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006356 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 }
6358
NeilBrown1ca69c42012-10-11 13:37:33 +11006359 /* Some actions do not requires the mutex */
6360 switch (cmd) {
6361 case GET_ARRAY_INFO:
6362 if (!mddev->raid_disks && !mddev->external)
6363 err = -ENODEV;
6364 else
6365 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006366 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006367
6368 case GET_DISK_INFO:
6369 if (!mddev->raid_disks && !mddev->external)
6370 err = -ENODEV;
6371 else
6372 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006373 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006374
6375 case SET_DISK_FAULTY:
6376 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006377 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006378 }
6379
NeilBrowna7a3f082012-12-11 13:35:54 +11006380 if (cmd == ADD_NEW_DISK)
6381 /* need to ensure md_delayed_delete() has completed */
6382 flush_workqueue(md_misc_wq);
6383
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006384 if (cmd == HOT_REMOVE_DISK)
6385 /* need to ensure recovery thread has run */
6386 wait_event_interruptible_timeout(mddev->sb_wait,
6387 !test_bit(MD_RECOVERY_NEEDED,
6388 &mddev->flags),
6389 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006390 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6391 /* Need to flush page cache, and ensure no-one else opens
6392 * and writes
6393 */
6394 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006395 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006396 mutex_unlock(&mddev->open_mutex);
6397 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006398 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006399 }
6400 set_bit(MD_STILL_CLOSED, &mddev->flags);
6401 mutex_unlock(&mddev->open_mutex);
6402 sync_blockdev(bdev);
6403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 err = mddev_lock(mddev);
6405 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006406 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6408 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 }
6411
NeilBrownc02c0ae2012-12-11 13:39:21 +11006412 if (cmd == SET_ARRAY_INFO) {
6413 mdu_array_info_t info;
6414 if (!arg)
6415 memset(&info, 0, sizeof(info));
6416 else if (copy_from_user(&info, argp, sizeof(info))) {
6417 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006418 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006419 }
6420 if (mddev->pers) {
6421 err = update_array_info(mddev, &info);
6422 if (err) {
6423 printk(KERN_WARNING "md: couldn't update"
6424 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006425 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006427 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006428 }
6429 if (!list_empty(&mddev->disks)) {
6430 printk(KERN_WARNING
6431 "md: array %s already has disks!\n",
6432 mdname(mddev));
6433 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006434 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006435 }
6436 if (mddev->raid_disks) {
6437 printk(KERN_WARNING
6438 "md: array %s already initialised!\n",
6439 mdname(mddev));
6440 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006441 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006442 }
6443 err = set_array_info(mddev, &info);
6444 if (err) {
6445 printk(KERN_WARNING "md: couldn't set"
6446 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006447 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006448 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006449 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
6451
6452 /*
6453 * Commands querying/configuring an existing array:
6454 */
NeilBrown32a76272005-06-21 17:17:14 -07006455 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006456 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006457 if ((!mddev->raid_disks && !mddev->external)
6458 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6459 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6460 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006462 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 }
6464
6465 /*
6466 * Commands even a read-only array can execute:
6467 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006468 switch (cmd) {
6469 case GET_BITMAP_FILE:
6470 err = get_bitmap_file(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006471 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006472
6473 case RESTART_ARRAY_RW:
6474 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006475 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006476
6477 case STOP_ARRAY:
6478 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006479 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006480
6481 case STOP_ARRAY_RO:
6482 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006483 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006484
NeilBrown3ea8929d2013-04-24 11:42:41 +10006485 case HOT_REMOVE_DISK:
6486 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006487 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006488
NeilBrown7ceb17e2013-04-24 11:42:42 +10006489 case ADD_NEW_DISK:
6490 /* We can support ADD_NEW_DISK on read-only arrays
6491 * on if we are re-adding a preexisting device.
6492 * So require mddev->pers and MD_DISK_SYNC.
6493 */
6494 if (mddev->pers) {
6495 mdu_disk_info_t info;
6496 if (copy_from_user(&info, argp, sizeof(info)))
6497 err = -EFAULT;
6498 else if (!(info.state & (1<<MD_DISK_SYNC)))
6499 /* Need to clear read-only for this */
6500 break;
6501 else
6502 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006503 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006504 }
6505 break;
6506
NeilBrownc02c0ae2012-12-11 13:39:21 +11006507 case BLKROSET:
6508 if (get_user(ro, (int __user *)(arg))) {
6509 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006510 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006511 }
6512 err = -EINVAL;
6513
6514 /* if the bdev is going readonly the value of mddev->ro
6515 * does not matter, no writes are coming
6516 */
6517 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006518 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006519
NeilBrownc02c0ae2012-12-11 13:39:21 +11006520 /* are we are already prepared for writes? */
6521 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006522 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006523
6524 /* transitioning to readauto need only happen for
6525 * arrays that call md_write_start
6526 */
6527 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006529 if (err == 0) {
6530 mddev->ro = 2;
6531 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006532 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006533 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006534 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 }
6536
6537 /*
6538 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006539 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 */
NeilBrown326eb172014-09-30 15:36:28 +10006541 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006542 if (mddev->ro == 2) {
6543 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006544 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006545 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006546 /* mddev_unlock will wake thread */
6547 /* If a device failed while we were read-only, we
6548 * need to make sure the metadata is updated now.
6549 */
6550 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6551 mddev_unlock(mddev);
6552 wait_event(mddev->sb_wait,
6553 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6554 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006555 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006556 }
NeilBrownf91de922005-11-08 21:39:36 -08006557 } else {
6558 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006559 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 }
6562
NeilBrownc02c0ae2012-12-11 13:39:21 +11006563 switch (cmd) {
6564 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006566 mdu_disk_info_t info;
6567 if (copy_from_user(&info, argp, sizeof(info)))
6568 err = -EFAULT;
6569 else
6570 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006571 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
NeilBrownc02c0ae2012-12-11 13:39:21 +11006574 case HOT_ADD_DISK:
6575 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006576 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
NeilBrownc02c0ae2012-12-11 13:39:21 +11006578 case RUN_ARRAY:
6579 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006580 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
NeilBrownc02c0ae2012-12-11 13:39:21 +11006582 case SET_BITMAP_FILE:
6583 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006584 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006585
NeilBrownc02c0ae2012-12-11 13:39:21 +11006586 default:
6587 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006588 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 }
6590
NeilBrown3adc28d2014-09-30 15:46:41 +10006591unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006592 if (mddev->hold_active == UNTIL_IOCTL &&
6593 err != -EINVAL)
6594 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006596out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 return err;
6598}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006599#ifdef CONFIG_COMPAT
6600static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6601 unsigned int cmd, unsigned long arg)
6602{
6603 switch (cmd) {
6604 case HOT_REMOVE_DISK:
6605 case HOT_ADD_DISK:
6606 case SET_DISK_FAULTY:
6607 case SET_BITMAP_FILE:
6608 /* These take in integer arg, do not convert */
6609 break;
6610 default:
6611 arg = (unsigned long)compat_ptr(arg);
6612 break;
6613 }
6614
6615 return md_ioctl(bdev, mode, cmd, arg);
6616}
6617#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618
Al Viroa39907f2008-03-02 10:31:15 -05006619static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620{
6621 /*
6622 * Succeed if we can lock the mddev, which confirms that
6623 * it isn't being stopped right now.
6624 */
NeilBrownfd01b882011-10-11 16:47:53 +11006625 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 int err;
6627
Yuanhan Liu0c098222012-05-22 13:55:32 +10006628 if (!mddev)
6629 return -ENODEV;
6630
NeilBrownd3374822009-01-09 08:31:10 +11006631 if (mddev->gendisk != bdev->bd_disk) {
6632 /* we are racing with mddev_put which is discarding this
6633 * bd_disk.
6634 */
6635 mddev_put(mddev);
6636 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006637 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006638 /* Then retry the open from the top */
6639 return -ERESTARTSYS;
6640 }
6641 BUG_ON(mddev != bdev->bd_disk->private_data);
6642
NeilBrownc8c00a62009-08-10 12:50:52 +10006643 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 goto out;
6645
6646 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006647 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006648 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006649 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006651 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 out:
6653 return err;
6654}
6655
Al Virodb2a1442013-05-05 21:52:57 -04006656static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006658 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006660 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006661 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006664
6665static int md_media_changed(struct gendisk *disk)
6666{
NeilBrownfd01b882011-10-11 16:47:53 +11006667 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006668
6669 return mddev->changed;
6670}
6671
6672static int md_revalidate(struct gendisk *disk)
6673{
NeilBrownfd01b882011-10-11 16:47:53 +11006674 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006675
6676 mddev->changed = 0;
6677 return 0;
6678}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006679static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680{
6681 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006682 .open = md_open,
6683 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006684 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006685#ifdef CONFIG_COMPAT
6686 .compat_ioctl = md_compat_ioctl,
6687#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006688 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006689 .media_changed = md_media_changed,
6690 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691};
6692
NeilBrownf72ffdd2014-09-30 14:23:59 +10006693static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694{
NeilBrown2b8bf342011-10-11 16:48:23 +11006695 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 /*
6698 * md_thread is a 'system-thread', it's priority should be very
6699 * high. We avoid resource deadlocks individually in each
6700 * raid personality. (RAID5 does preallocation) We also use RR and
6701 * the very same RT priority as kswapd, thus we will never get
6702 * into a priority inversion deadlock.
6703 *
6704 * we definitely have to have equal or higher priority than
6705 * bdflush, otherwise bdflush will deadlock if there are too
6706 * many dirty RAID5 blocks.
6707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
NeilBrown6985c432005-10-19 21:23:47 -07006709 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006710 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
NeilBrown93588e22005-11-15 00:09:12 -08006712 /* We need to wait INTERRUPTIBLE so that
6713 * we don't add to the load-average.
6714 * That means we need to be sure no signals are
6715 * pending
6716 */
6717 if (signal_pending(current))
6718 flush_signals(current);
6719
6720 wait_event_interruptible_timeout
6721 (thread->wqueue,
6722 test_bit(THREAD_WAKEUP, &thread->flags)
6723 || kthread_should_stop(),
6724 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
NeilBrown6c987912011-01-14 09:13:53 +11006726 clear_bit(THREAD_WAKEUP, &thread->flags);
6727 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006728 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006730
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 return 0;
6732}
6733
NeilBrown2b8bf342011-10-11 16:48:23 +11006734void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735{
6736 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006737 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 set_bit(THREAD_WAKEUP, &thread->flags);
6739 wake_up(&thread->wqueue);
6740 }
6741}
6742
Shaohua Li4ed87312012-10-11 13:34:00 +11006743struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6744 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745{
NeilBrown2b8bf342011-10-11 16:48:23 +11006746 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747
NeilBrown2b8bf342011-10-11 16:48:23 +11006748 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 if (!thread)
6750 return NULL;
6751
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 init_waitqueue_head(&thread->wqueue);
6753
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 thread->run = run;
6755 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006756 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006757 thread->tsk = kthread_run(md_thread, thread,
6758 "%s_%s",
6759 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006760 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006761 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 kfree(thread);
6763 return NULL;
6764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 return thread;
6766}
6767
NeilBrown2b8bf342011-10-11 16:48:23 +11006768void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769{
NeilBrown2b8bf342011-10-11 16:48:23 +11006770 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006771 if (!thread)
6772 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006773 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006774 /* Locking ensures that mddev_unlock does not wake_up a
6775 * non-existent thread
6776 */
6777 spin_lock(&pers_lock);
6778 *threadp = NULL;
6779 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006780
6781 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 kfree(thread);
6783}
6784
NeilBrownfd01b882011-10-11 16:47:53 +11006785void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786{
NeilBrownb2d444d2005-11-08 21:39:31 -08006787 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006789
NeilBrownde393cd2011-07-28 11:31:48 +10006790 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 return;
6792 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006793 if (mddev->degraded)
6794 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006795 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6798 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006799 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006800 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006801 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802}
6803
6804/* seq_file implementation /proc/mdstat */
6805
6806static void status_unused(struct seq_file *seq)
6807{
6808 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006809 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
6811 seq_printf(seq, "unused devices: ");
6812
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006813 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 char b[BDEVNAME_SIZE];
6815 i++;
6816 seq_printf(seq, "%s ",
6817 bdevname(rdev->bdev,b));
6818 }
6819 if (!i)
6820 seq_printf(seq, "<none>");
6821
6822 seq_printf(seq, "\n");
6823}
6824
NeilBrownf72ffdd2014-09-30 14:23:59 +10006825static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826{
NeilBrowndd71cf62009-05-07 12:49:35 +10006827 sector_t max_sectors, resync, res;
6828 unsigned long dt, db;
6829 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006830 int scale;
6831 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
NeilBrown72f36d52012-10-11 14:25:57 +11006833 if (mddev->curr_resync <= 3)
6834 resync = 0;
6835 else
6836 resync = mddev->curr_resync
6837 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
NeilBrownc804cde2012-05-21 09:28:33 +10006839 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6840 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006841 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006843 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844
NeilBrown403df472014-09-30 15:52:29 +10006845 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08006846 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006847 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006848 * u32, as those are the requirements for sector_div.
6849 * Thus 'scale' must be at least 10
6850 */
6851 scale = 10;
6852 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006853 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006854 scale++;
6855 }
6856 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006857 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006858
6859 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 {
NeilBrown4588b422006-03-27 01:18:04 -08006861 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 seq_printf(seq, "[");
6863 for (i = 0; i < x; i++)
6864 seq_printf(seq, "=");
6865 seq_printf(seq, ">");
6866 for (i = 0; i < y; i++)
6867 seq_printf(seq, ".");
6868 seq_printf(seq, "] ");
6869 }
NeilBrown4588b422006-03-27 01:18:04 -08006870 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006871 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6872 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006873 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6874 "check" :
6875 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6876 "resync" : "recovery"))),
6877 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006878 (unsigned long long) resync/2,
6879 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
6881 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 * dt: time from mark until now
6883 * db: blocks written from mark until now
6884 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006885 *
6886 * rt is a sector_t, so could be 32bit or 64bit.
6887 * So we divide before multiply in case it is 32bit and close
6888 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006889 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006890 * near the end of resync when the number of remaining sectors
6891 * is close to 'db'.
6892 * We then divide rt by 32 after multiplying by db to compensate.
6893 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 */
6895 dt = ((jiffies - mddev->resync_mark) / HZ);
6896 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006897 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6898 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
NeilBrowndd71cf62009-05-07 12:49:35 +10006900 rt = max_sectors - resync; /* number of remaining sectors */
6901 sector_div(rt, db/32+1);
6902 rt *= dt;
6903 rt >>= 5;
6904
6905 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6906 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907
NeilBrownff4e8d92006-07-10 04:44:16 -07006908 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909}
6910
6911static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6912{
6913 struct list_head *tmp;
6914 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006915 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
6917 if (l >= 0x10000)
6918 return NULL;
6919 if (!l--)
6920 /* header */
6921 return (void*)1;
6922
6923 spin_lock(&all_mddevs_lock);
6924 list_for_each(tmp,&all_mddevs)
6925 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006926 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 mddev_get(mddev);
6928 spin_unlock(&all_mddevs_lock);
6929 return mddev;
6930 }
6931 spin_unlock(&all_mddevs_lock);
6932 if (!l--)
6933 return (void*)2;/* tail */
6934 return NULL;
6935}
6936
6937static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6938{
6939 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006940 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006941
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 ++*pos;
6943 if (v == (void*)2)
6944 return NULL;
6945
6946 spin_lock(&all_mddevs_lock);
6947 if (v == (void*)1)
6948 tmp = all_mddevs.next;
6949 else
6950 tmp = mddev->all_mddevs.next;
6951 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006952 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 else {
6954 next_mddev = (void*)2;
6955 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 spin_unlock(&all_mddevs_lock);
6958
6959 if (v != (void*)1)
6960 mddev_put(mddev);
6961 return next_mddev;
6962
6963}
6964
6965static void md_seq_stop(struct seq_file *seq, void *v)
6966{
NeilBrownfd01b882011-10-11 16:47:53 +11006967 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
6969 if (mddev && v != (void*)1 && v != (void*)2)
6970 mddev_put(mddev);
6971}
6972
6973static int md_seq_show(struct seq_file *seq, void *v)
6974{
NeilBrownfd01b882011-10-11 16:47:53 +11006975 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006976 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006977 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
6979 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006980 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 seq_printf(seq, "Personalities : ");
6982 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006983 list_for_each_entry(pers, &pers_list, list)
6984 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
6986 spin_unlock(&pers_lock);
6987 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006988 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 return 0;
6990 }
6991 if (v == (void*)2) {
6992 status_unused(seq);
6993 return 0;
6994 }
6995
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006996 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006998
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7000 seq_printf(seq, "%s : %sactive", mdname(mddev),
7001 mddev->pers ? "" : "in");
7002 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007003 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007005 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007006 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 seq_printf(seq, " %s", mddev->pers->name);
7008 }
7009
Andre Nolldd8ac332009-03-31 14:33:13 +11007010 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007011 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 char b[BDEVNAME_SIZE];
7013 seq_printf(seq, " %s[%d]",
7014 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007015 if (test_bit(WriteMostly, &rdev->flags))
7016 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007017 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 seq_printf(seq, "(F)");
7019 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007020 }
7021 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007022 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007023 if (test_bit(Replacement, &rdev->flags))
7024 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007025 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 }
7027
7028 if (!list_empty(&mddev->disks)) {
7029 if (mddev->pers)
7030 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007031 (unsigned long long)
7032 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 else
7034 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007035 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007037 if (mddev->persistent) {
7038 if (mddev->major_version != 0 ||
7039 mddev->minor_version != 90) {
7040 seq_printf(seq," super %d.%d",
7041 mddev->major_version,
7042 mddev->minor_version);
7043 }
NeilBrowne6910632008-02-06 01:39:51 -08007044 } else if (mddev->external)
7045 seq_printf(seq, " super external:%s",
7046 mddev->metadata_type);
7047 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007048 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
7050 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007051 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007052 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007053 if (mddev->pers->sync_request) {
7054 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007055 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007056 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007057 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007058 seq_printf(seq, "\tresync=DELAYED\n ");
7059 else if (mddev->recovery_cp < MaxSector)
7060 seq_printf(seq, "\tresync=PENDING\n ");
7061 }
NeilBrown32a76272005-06-21 17:17:14 -07007062 } else
7063 seq_printf(seq, "\n ");
7064
NeilBrown57148962012-03-19 12:46:40 +11007065 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
7067 seq_printf(seq, "\n");
7068 }
7069 mddev_unlock(mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10007070
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 return 0;
7072}
7073
Jan Engelhardt110518b2009-05-07 12:49:37 +10007074static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 .start = md_seq_start,
7076 .next = md_seq_next,
7077 .stop = md_seq_stop,
7078 .show = md_seq_show,
7079};
7080
7081static int md_seq_open(struct inode *inode, struct file *file)
7082{
Kay Sieversf1514632011-07-12 20:48:39 +02007083 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 int error;
7085
7086 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007087 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007088 return error;
7089
7090 seq = file->private_data;
7091 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 return error;
7093}
7094
NeilBrowne2f23b62014-04-09 14:33:51 +10007095static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007096static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7097{
Kay Sieversf1514632011-07-12 20:48:39 +02007098 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007099 int mask;
7100
NeilBrowne2f23b62014-04-09 14:33:51 +10007101 if (md_unloading)
7102 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;;
NeilBrownd7603b72006-01-06 00:20:30 -08007103 poll_wait(filp, &md_event_waiters, wait);
7104
7105 /* always allow read */
7106 mask = POLLIN | POLLRDNORM;
7107
Kay Sieversf1514632011-07-12 20:48:39 +02007108 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007109 mask |= POLLERR | POLLPRI;
7110 return mask;
7111}
7112
Arjan van de Venfa027c22007-02-12 00:55:33 -08007113static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007114 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 .open = md_seq_open,
7116 .read = seq_read,
7117 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007118 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007119 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120};
7121
NeilBrown84fc4b52011-10-11 16:49:58 +11007122int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
Chao Yu50bd3772014-09-25 15:28:34 +08007124 printk(KERN_INFO "md: %s personality registered for level %d\n",
7125 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007127 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 spin_unlock(&pers_lock);
7129 return 0;
7130}
7131
NeilBrown84fc4b52011-10-11 16:49:58 +11007132int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133{
NeilBrown2604b702006-01-06 00:20:36 -08007134 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007136 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 spin_unlock(&pers_lock);
7138 return 0;
7139}
7140
NeilBrownfd01b882011-10-11 16:47:53 +11007141static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007143 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007145 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007148 rcu_read_lock();
7149 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007151 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7152 (int)part_stat_read(&disk->part0, sectors[1]) -
7153 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007154 /* sync IO will cause sync_io to increase before the disk_stats
7155 * as sync_io is counted when a request starts, and
7156 * disk_stats is counted when it completes.
7157 * So resync activity will cause curr_events to be smaller than
7158 * when there was no such activity.
7159 * non-sync IO will cause disk_stat to increase without
7160 * increasing sync_io so curr_events will (eventually)
7161 * be larger than it was before. Once it becomes
7162 * substantially larger, the test below will cause
7163 * the array to appear non-idle, and resync will slow
7164 * down.
7165 * If there is a lot of outstanding resync activity when
7166 * we set last_event to curr_events, then all that activity
7167 * completing might cause the array to appear non-idle
7168 * and resync will be slowed down even though there might
7169 * not have been non-resync activity. This will only
7170 * happen once though. 'last_events' will soon reflect
7171 * the state where there is little or no outstanding
7172 * resync requests, and further resync activity will
7173 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007176 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 rdev->last_events = curr_events;
7178 idle = 0;
7179 }
7180 }
NeilBrown4b809912008-07-21 17:05:25 +10007181 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 return idle;
7183}
7184
NeilBrownfd01b882011-10-11 16:47:53 +11007185void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186{
7187 /* another "blocks" (512byte) blocks have been synced */
7188 atomic_sub(blocks, &mddev->recovery_active);
7189 wake_up(&mddev->recovery_wait);
7190 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007191 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007192 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 md_wakeup_thread(mddev->thread);
7194 // stop recovery, signal do_sync ....
7195 }
7196}
7197
NeilBrown06d91a52005-06-21 17:17:12 -07007198/* md_write_start(mddev, bi)
7199 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007200 * in superblock) before writing, schedule a superblock update
7201 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007202 */
NeilBrownfd01b882011-10-11 16:47:53 +11007203void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204{
Neil Brown0fd62b82008-06-28 08:31:36 +10007205 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007206 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007207 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007208
NeilBrownf91de922005-11-08 21:39:36 -08007209 BUG_ON(mddev->ro == 1);
7210 if (mddev->ro == 2) {
7211 /* need to switch to read/write */
7212 mddev->ro = 0;
7213 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7214 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007215 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007216 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007217 }
NeilBrown06d91a52005-06-21 17:17:12 -07007218 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007219 if (mddev->safemode == 1)
7220 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007221 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007222 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007223 if (mddev->in_sync) {
7224 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007225 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007226 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007227 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007228 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007229 }
NeilBrowna9701a32005-11-08 21:39:34 -08007230 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007231 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007232 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007233 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007234 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007235 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236}
7237
NeilBrownfd01b882011-10-11 16:47:53 +11007238void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239{
7240 if (atomic_dec_and_test(&mddev->writes_pending)) {
7241 if (mddev->safemode == 2)
7242 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007243 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7245 }
7246}
7247
NeilBrown2a2275d2007-01-26 00:57:11 -08007248/* md_allow_write(mddev)
7249 * Calling this ensures that the array is marked 'active' so that writes
7250 * may proceed without blocking. It is important to call this before
7251 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7252 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007253 *
7254 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7255 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007256 */
NeilBrownfd01b882011-10-11 16:47:53 +11007257int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007258{
7259 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007260 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007261 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007262 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007263 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007264 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007265
7266 spin_lock_irq(&mddev->write_lock);
7267 if (mddev->in_sync) {
7268 mddev->in_sync = 0;
7269 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007270 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007271 if (mddev->safemode_delay &&
7272 mddev->safemode == 0)
7273 mddev->safemode = 1;
7274 spin_unlock_irq(&mddev->write_lock);
7275 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007276 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007277 } else
7278 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007279
NeilBrown070dc6d2010-08-30 17:33:34 +10007280 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007281 return -EAGAIN;
7282 else
7283 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007284}
7285EXPORT_SYMBOL_GPL(md_allow_write);
7286
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287#define SYNC_MARKS 10
7288#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007289#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007290void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291{
Shaohua Li4ed87312012-10-11 13:34:00 +11007292 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007293 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 unsigned int currspeed = 0,
7295 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007296 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007298 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 sector_t mark_cnt[SYNC_MARKS];
7300 int last_mark,m;
7301 struct list_head *tmp;
7302 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007303 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007304 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007305 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007306 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307
7308 /* just incase thread restarts... */
7309 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7310 return;
NeilBrown3991b312014-05-28 13:39:23 +10007311 if (mddev->ro) {/* never try to sync a read-only array */
7312 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007313 return;
NeilBrown3991b312014-05-28 13:39:23 +10007314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315
NeilBrown61df9d92006-10-03 01:15:57 -07007316 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007317 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007318 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007319 action = "check";
7320 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007321 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007322 action = "repair";
7323 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007324 desc = "resync";
7325 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7326 desc = "reshape";
7327 else
7328 desc = "recovery";
7329
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007330 mddev->last_sync_action = action ?: desc;
7331
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 /* we overload curr_resync somewhat here.
7333 * 0 == not engaged in resync at all
7334 * 2 == checking that there is no conflict with another sync
7335 * 1 == like 2, but have yielded to allow conflicting resync to
7336 * commense
7337 * other == active in resync - this many blocks
7338 *
7339 * Before starting a resync we must have set curr_resync to
7340 * 2, and then checked that every "conflicting" array has curr_resync
7341 * less than ours. When we find one that is the same or higher
7342 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7343 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7344 * This will mean we have to start checking from the beginning again.
7345 *
7346 */
7347
7348 do {
7349 mddev->curr_resync = 2;
7350
7351 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007352 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007354 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 if (mddev2 == mddev)
7356 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007357 if (!mddev->parallel_resync
7358 && mddev2->curr_resync
7359 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 DEFINE_WAIT(wq);
7361 if (mddev < mddev2 && mddev->curr_resync == 2) {
7362 /* arbitrarily yield */
7363 mddev->curr_resync = 1;
7364 wake_up(&resync_wait);
7365 }
7366 if (mddev > mddev2 && mddev->curr_resync == 1)
7367 /* no need to wait here, we can wait the next
7368 * time 'round when curr_resync == 2
7369 */
7370 continue;
NeilBrown97441972008-09-19 11:49:54 +10007371 /* We need to wait 'interruptible' so as not to
7372 * contribute to the load average, and not to
7373 * be caught by 'softlockup'
7374 */
7375 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007376 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007377 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007378 printk(KERN_INFO "md: delaying %s of %s"
7379 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007381 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007383 if (signal_pending(current))
7384 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 schedule();
7386 finish_wait(&resync_wait, &wq);
7387 goto try_again;
7388 }
7389 finish_wait(&resync_wait, &wq);
7390 }
7391 }
7392 } while (mddev->curr_resync < 2);
7393
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007394 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007395 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007397 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 */
7399 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007400 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007401 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007402 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7403 j = mddev->resync_min;
7404 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007405 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007406
NeilBrownccfcc3c2006-03-27 01:18:09 -08007407 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007408 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007409 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007411 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007412 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007413 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007414 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007415 if (rdev->raid_disk >= 0 &&
7416 !test_bit(Faulty, &rdev->flags) &&
7417 !test_bit(In_sync, &rdev->flags) &&
7418 rdev->recovery_offset < j)
7419 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007420 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007421
7422 /* If there is a bitmap, we need to make sure all
7423 * writes that started before we added a spare
7424 * complete before we start doing a recovery.
7425 * Otherwise the write might complete and (via
7426 * bitmap_endwrite) set a bit in the bitmap after the
7427 * recovery has checked that bit and skipped that
7428 * region.
7429 */
7430 if (mddev->bitmap) {
7431 mddev->pers->quiesce(mddev, 1);
7432 mddev->pers->quiesce(mddev, 0);
7433 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
NeilBrown61df9d92006-10-03 01:15:57 -07007436 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7437 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7438 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007439 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007440 "(but not more than %d KB/sec) for %s.\n",
7441 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442
NeilBrowneea1bf32009-03-31 14:27:02 +11007443 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007444
NeilBrown57afd892005-06-21 17:17:13 -07007445 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 for (m = 0; m < SYNC_MARKS; m++) {
7447 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007448 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 }
7450 last_mark = 0;
7451 mddev->resync_mark = mark[last_mark];
7452 mddev->resync_mark_cnt = mark_cnt[last_mark];
7453
7454 /*
7455 * Tune reconstruction:
7456 */
7457 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007458 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7459 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
7461 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 last_check = 0;
7463
7464 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007465 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007466 "md: resuming %s of %s from checkpoint.\n",
7467 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007469 } else
7470 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007471 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007472 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7473 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007474 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
majianpeng7c2c57c2012-07-03 12:12:26 +10007476 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007478 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479
NeilBrown57afd892005-06-21 17:17:13 -07007480 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007481
NeilBrown7a91ee12009-05-26 12:57:21 +10007482 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7483 ((mddev->curr_resync > mddev->curr_resync_completed &&
7484 (mddev->curr_resync - mddev->curr_resync_completed)
7485 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007486 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007487 (j - mddev->curr_resync_completed)*2
7488 >= mddev->resync_max - mddev->curr_resync_completed
7489 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007490 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007491 wait_event(mddev->recovery_wait,
7492 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007493 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007494 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7495 j > mddev->recovery_cp)
7496 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007497 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007498 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007499 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007500 }
NeilBrownacb180b2009-04-14 16:28:34 +10007501
NeilBrownc91abf52013-11-19 12:02:01 +11007502 while (j >= mddev->resync_max &&
7503 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007504 /* As this condition is controlled by user-space,
7505 * we can block indefinitely, so use '_interruptible'
7506 * to avoid triggering warnings.
7507 */
7508 flush_signals(current); /* just in case */
7509 wait_event_interruptible(mddev->recovery_wait,
7510 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007511 || test_bit(MD_RECOVERY_INTR,
7512 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007513 }
NeilBrownacb180b2009-04-14 16:28:34 +10007514
NeilBrownc91abf52013-11-19 12:02:01 +11007515 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7516 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007517
NeilBrown57afd892005-06-21 17:17:13 -07007518 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007519 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007520 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007521 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 }
NeilBrown57afd892005-06-21 17:17:13 -07007524
7525 if (!skipped) { /* actual IO requested */
7526 io_sectors += sectors;
7527 atomic_add(sectors, &mddev->recovery_active);
7528 }
7529
NeilBrowne875ece2011-07-28 11:39:24 +10007530 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7531 break;
7532
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007534 if (j > 2)
7535 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007536 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007537 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007538 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007539 * visible in /proc/mdstat
7540 */
7541 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007542
7543 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 continue;
7545
NeilBrown57afd892005-06-21 17:17:13 -07007546 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 repeat:
7548 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7549 /* step marks */
7550 int next = (last_mark+1) % SYNC_MARKS;
7551
7552 mddev->resync_mark = mark[next];
7553 mddev->resync_mark_cnt = mark_cnt[next];
7554 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007555 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 last_mark = next;
7557 }
7558
NeilBrownc91abf52013-11-19 12:02:01 +11007559 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
7562 /*
7563 * this loop exits only if either when we are slower than
7564 * the 'hard' speed limit, or the system was IO-idle for
7565 * a jiffy.
7566 * the system might be non-idle CPU-wise, but we only care
7567 * about not overloading the IO subsystem. (things like an
7568 * e2fsck being done on the RAID array should execute fast)
7569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 cond_resched();
7571
Xiao Niac7e50a2014-08-07 09:37:41 -04007572 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7573 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007574 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
NeilBrown88202a02006-01-06 00:21:36 -08007576 if (currspeed > speed_min(mddev)) {
7577 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007578 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007579 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 goto repeat;
7581 }
7582 }
7583 }
NeilBrownc91abf52013-11-19 12:02:01 +11007584 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7585 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7586 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 /*
7588 * this also signals 'finished resyncing' to md_stop
7589 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007590 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7592
7593 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007594 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
NeilBrowndfc70642008-05-23 13:04:39 -07007596 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007597 mddev->curr_resync > 2) {
7598 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7599 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7600 if (mddev->curr_resync >= mddev->recovery_cp) {
7601 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007602 "md: checkpointing %s of %s.\n",
7603 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007604 if (test_bit(MD_RECOVERY_ERROR,
7605 &mddev->recovery))
7606 mddev->recovery_cp =
7607 mddev->curr_resync_completed;
7608 else
7609 mddev->recovery_cp =
7610 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007611 }
7612 } else
7613 mddev->recovery_cp = MaxSector;
7614 } else {
7615 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7616 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007617 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007618 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007619 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007620 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007621 !test_bit(Faulty, &rdev->flags) &&
7622 !test_bit(In_sync, &rdev->flags) &&
7623 rdev->recovery_offset < mddev->curr_resync)
7624 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007625 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007628 skip:
NeilBrown17571282006-12-10 02:20:52 -08007629 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630
NeilBrownc07b70a2009-12-14 12:49:48 +11007631 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7632 /* We completed so min/max setting can be forgotten if used. */
7633 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7634 mddev->resync_min = 0;
7635 mddev->resync_max = MaxSector;
7636 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7637 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 mddev->curr_resync = 0;
7639 wake_up(&resync_wait);
7640 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7641 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007642 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643}
NeilBrown29269552006-03-27 01:18:10 -08007644EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645
NeilBrown746d3202013-04-24 11:42:41 +10007646static int remove_and_add_spares(struct mddev *mddev,
7647 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007648{
NeilBrown3cb03002011-10-11 16:45:26 +11007649 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007650 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007651 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007652
NeilBrowndafb20f2012-03-19 12:46:39 +11007653 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007654 if ((this == NULL || rdev == this) &&
7655 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007656 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007657 (test_bit(Faulty, &rdev->flags) ||
7658 ! test_bit(In_sync, &rdev->flags)) &&
7659 atomic_read(&rdev->nr_pending)==0) {
7660 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007661 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007662 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007663 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007664 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007665 }
7666 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007667 if (removed && mddev->kobj.sd)
7668 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007669
NeilBrown746d3202013-04-24 11:42:41 +10007670 if (this)
7671 goto no_add;
7672
NeilBrowndafb20f2012-03-19 12:46:39 +11007673 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007674 if (rdev->raid_disk >= 0 &&
7675 !test_bit(In_sync, &rdev->flags) &&
7676 !test_bit(Faulty, &rdev->flags))
7677 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007678 if (rdev->raid_disk >= 0)
7679 continue;
7680 if (test_bit(Faulty, &rdev->flags))
7681 continue;
7682 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007683 ! (rdev->saved_raid_disk >= 0 &&
7684 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007685 continue;
7686
NeilBrown7eb41882014-01-14 15:55:14 +11007687 if (rdev->saved_raid_disk < 0)
7688 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007689 if (mddev->pers->
7690 hot_add_disk(mddev, rdev) == 0) {
7691 if (sysfs_link_rdev(mddev, rdev))
7692 /* failure here is OK */;
7693 spares++;
7694 md_new_event(mddev);
7695 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007696 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007697 }
NeilBrown746d3202013-04-24 11:42:41 +10007698no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007699 if (removed)
7700 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007701 return spares;
7702}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007703
NeilBrownac05f252014-09-30 08:10:42 +10007704static void md_start_sync(struct work_struct *ws)
7705{
7706 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7707
7708 mddev->sync_thread = md_register_thread(md_do_sync,
7709 mddev,
7710 "resync");
7711 if (!mddev->sync_thread) {
7712 printk(KERN_ERR "%s: could not start resync"
7713 " thread...\n",
7714 mdname(mddev));
7715 /* leave the spares where they are, it shouldn't hurt */
7716 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7717 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7718 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7719 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7720 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7721 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7722 &mddev->recovery))
7723 if (mddev->sysfs_action)
7724 sysfs_notify_dirent_safe(mddev->sysfs_action);
7725 } else
7726 md_wakeup_thread(mddev->sync_thread);
7727 sysfs_notify_dirent_safe(mddev->sysfs_action);
7728 md_new_event(mddev);
7729}
7730
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731/*
7732 * This routine is regularly called by all per-raid-array threads to
7733 * deal with generic issues like resync and super-block update.
7734 * Raid personalities that don't have a thread (linear/raid0) do not
7735 * need this as they never do any recovery or update the superblock.
7736 *
7737 * It does not do any resync itself, but rather "forks" off other threads
7738 * to do that as needed.
7739 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7740 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007741 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 * and wakeups up this thread which will reap the thread and finish up.
7743 * This thread also removes any faulty devices (with nr_pending == 0).
7744 *
7745 * The overall approach is:
7746 * 1/ if the superblock needs updating, update it.
7747 * 2/ If a recovery thread is running, don't do anything else.
7748 * 3/ If recovery has finished, clean up, possibly marking spares active.
7749 * 4/ If there are any faulty devices, remove them.
7750 * 5/ If array is degraded, try to add spares devices
7751 * 6/ If array has spares or is not in-sync, start a resync thread.
7752 */
NeilBrownfd01b882011-10-11 16:47:53 +11007753void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007755 if (mddev->suspended)
7756 return;
7757
NeilBrown5f404022005-06-21 17:17:16 -07007758 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007759 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760
NeilBrownfca4d842005-06-21 17:17:11 -07007761 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007762 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007763 printk(KERN_INFO "md: %s in immediate safe mode\n",
7764 mdname(mddev));
7765 mddev->safemode = 2;
7766 }
7767 flush_signals(current);
7768 }
7769
NeilBrownc89a8ee2008-08-05 15:54:13 +10007770 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7771 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007773 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007775 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007776 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007777 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7778 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 ))
7780 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007781
NeilBrowndf5b89b2006-03-27 01:18:20 -08007782 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007783 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007784
NeilBrownc89a8ee2008-08-05 15:54:13 +10007785 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007786 /* On a read-only array we can:
7787 * - remove failed devices
7788 * - add already-in_sync devices if the array itself
7789 * is in-sync.
7790 * As we only add devices that are already in-sync,
7791 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007792 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007793 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007794 /* There is no thread, but we need to call
7795 * ->spare_active and clear saved_raid_disk
7796 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007797 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007798 md_reap_sync_thread(mddev);
7799 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007800 goto unlock;
7801 }
7802
NeilBrown31a59e32008-04-30 00:52:30 -07007803 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007804 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007805 spin_lock_irq(&mddev->write_lock);
7806 if (mddev->safemode &&
7807 !atomic_read(&mddev->writes_pending) &&
7808 !mddev->in_sync &&
7809 mddev->recovery_cp == MaxSector) {
7810 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007811 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007812 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007813 }
7814 if (mddev->safemode == 1)
7815 mddev->safemode = 0;
7816 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007817 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007818 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007819 }
NeilBrownfca4d842005-06-21 17:17:11 -07007820
NeilBrown7a0a5352013-08-27 16:28:23 +10007821 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07007822 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007823
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7825 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7826 /* resync/recovery still happening */
7827 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7828 goto unlock;
7829 }
7830 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007831 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 goto unlock;
7833 }
Neil Brown72a23c22008-06-28 08:31:41 +10007834 /* Set RUNNING before clearing NEEDED to avoid
7835 * any transients in the value of "sync_action".
7836 */
NeilBrown72f36d52012-10-11 14:25:57 +11007837 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007838 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007839 /* Clear some bits that don't mean anything, but
7840 * might be left set
7841 */
NeilBrown24dd4692005-11-08 21:39:26 -08007842 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7843 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844
NeilBrowned209582012-04-24 10:23:14 +10007845 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7846 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10007847 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 /* no recovery is running.
7849 * remove any failed drives, then
7850 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007851 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 * the personality to fail the re-add.
7853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007855 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007856 if (mddev->pers->check_reshape == NULL ||
7857 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007858 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10007859 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007860 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007861 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007862 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007863 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7864 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007865 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007866 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007867 } else if (mddev->recovery_cp < MaxSector) {
7868 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007869 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007870 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7871 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10007872 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08007873
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007875 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007876 /* We are adding a device or devices to an array
7877 * which has the bitmap stored on all devices.
7878 * So make sure all bitmap pages get written
7879 */
7880 bitmap_write_all(mddev->bitmap);
7881 }
NeilBrownac05f252014-09-30 08:10:42 +10007882 INIT_WORK(&mddev->del_work, md_start_sync);
7883 queue_work(md_misc_wq, &mddev->del_work);
7884 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 }
NeilBrownac05f252014-09-30 08:10:42 +10007886 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10007887 if (!mddev->sync_thread) {
7888 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7889 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7890 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007891 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007892 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007893 }
NeilBrownac05f252014-09-30 08:10:42 +10007894 unlock:
7895 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 mddev_unlock(mddev);
7897 }
7898}
7899
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007900void md_reap_sync_thread(struct mddev *mddev)
7901{
7902 struct md_rdev *rdev;
7903
7904 /* resync has finished, collect result */
7905 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007906 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007907 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7908 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7909 /* success...*/
7910 /* activate any spares */
7911 if (mddev->pers->spare_active(mddev)) {
7912 sysfs_notify(&mddev->kobj, NULL,
7913 "degraded");
7914 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7915 }
7916 }
7917 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7918 mddev->pers->finish_reshape)
7919 mddev->pers->finish_reshape(mddev);
7920
7921 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007922 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007923 */
NeilBrownf4667222013-12-09 12:04:56 +11007924 if (!mddev->degraded)
7925 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007926 rdev->saved_raid_disk = -1;
7927
7928 md_update_sb(mddev, 1);
7929 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7930 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7931 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7932 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7933 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7934 /* flag recovery needed just to double check */
7935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7936 sysfs_notify_dirent_safe(mddev->sysfs_action);
7937 md_new_event(mddev);
7938 if (mddev->event_work.func)
7939 queue_work(md_misc_wq, &mddev->event_work);
7940}
7941
NeilBrownfd01b882011-10-11 16:47:53 +11007942void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007943{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007944 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007945 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007946 !test_bit(Blocked, &rdev->flags) &&
7947 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007948 msecs_to_jiffies(5000));
7949 rdev_dec_pending(rdev, mddev);
7950}
7951EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7952
NeilBrownc6563a82012-05-21 09:27:00 +10007953void md_finish_reshape(struct mddev *mddev)
7954{
7955 /* called be personality module when reshape completes. */
7956 struct md_rdev *rdev;
7957
7958 rdev_for_each(rdev, mddev) {
7959 if (rdev->data_offset > rdev->new_data_offset)
7960 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7961 else
7962 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7963 rdev->data_offset = rdev->new_data_offset;
7964 }
7965}
7966EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007967
7968/* Bad block management.
7969 * We can record which blocks on each device are 'bad' and so just
7970 * fail those blocks, or that stripe, rather than the whole device.
7971 * Entries in the bad-block table are 64bits wide. This comprises:
7972 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7973 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7974 * A 'shift' can be set so that larger blocks are tracked and
7975 * consequently larger devices can be covered.
7976 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7977 *
7978 * Locking of the bad-block table uses a seqlock so md_is_badblock
7979 * might need to retry if it is very unlucky.
7980 * We will sometimes want to check for bad blocks in a bi_end_io function,
7981 * so we use the write_seqlock_irq variant.
7982 *
7983 * When looking for a bad block we specify a range and want to
7984 * know if any block in the range is bad. So we binary-search
7985 * to the last range that starts at-or-before the given endpoint,
7986 * (or "before the sector after the target range")
7987 * then see if it ends after the given start.
7988 * We return
7989 * 0 if there are no known bad blocks in the range
7990 * 1 if there are known bad block which are all acknowledged
7991 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7992 * plus the start/length of the first bad section we overlap.
7993 */
7994int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7995 sector_t *first_bad, int *bad_sectors)
7996{
7997 int hi;
majianpengab056132012-11-06 17:13:44 +08007998 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007999 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008000 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008001 sector_t target = s + sectors;
8002 unsigned seq;
8003
8004 if (bb->shift > 0) {
8005 /* round the start down, and the end up */
8006 s >>= bb->shift;
8007 target += (1<<bb->shift) - 1;
8008 target >>= bb->shift;
8009 sectors = target - s;
8010 }
8011 /* 'target' is now the first block after the bad range */
8012
8013retry:
8014 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008015 lo = 0;
8016 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008017 hi = bb->count;
8018
8019 /* Binary search between lo and hi for 'target'
8020 * i.e. for the last range that starts before 'target'
8021 */
8022 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8023 * are known not to be the last range before target.
8024 * VARIANT: hi-lo is the number of possible
8025 * ranges, and decreases until it reaches 1
8026 */
8027 while (hi - lo > 1) {
8028 int mid = (lo + hi) / 2;
8029 sector_t a = BB_OFFSET(p[mid]);
8030 if (a < target)
8031 /* This could still be the one, earlier ranges
8032 * could not. */
8033 lo = mid;
8034 else
8035 /* This and later ranges are definitely out. */
8036 hi = mid;
8037 }
8038 /* 'lo' might be the last that started before target, but 'hi' isn't */
8039 if (hi > lo) {
8040 /* need to check all range that end after 's' to see if
8041 * any are unacknowledged.
8042 */
8043 while (lo >= 0 &&
8044 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8045 if (BB_OFFSET(p[lo]) < target) {
8046 /* starts before the end, and finishes after
8047 * the start, so they must overlap
8048 */
8049 if (rv != -1 && BB_ACK(p[lo]))
8050 rv = 1;
8051 else
8052 rv = -1;
8053 *first_bad = BB_OFFSET(p[lo]);
8054 *bad_sectors = BB_LEN(p[lo]);
8055 }
8056 lo--;
8057 }
8058 }
8059
8060 if (read_seqretry(&bb->lock, seq))
8061 goto retry;
8062
8063 return rv;
8064}
8065EXPORT_SYMBOL_GPL(md_is_badblock);
8066
8067/*
8068 * Add a range of bad blocks to the table.
8069 * This might extend the table, or might contract it
8070 * if two adjacent ranges can be merged.
8071 * We binary-search to find the 'insertion' point, then
8072 * decide how best to handle it.
8073 */
8074static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8075 int acknowledged)
8076{
8077 u64 *p;
8078 int lo, hi;
8079 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008080 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008081
8082 if (bb->shift < 0)
8083 /* badblocks are disabled */
8084 return 0;
8085
8086 if (bb->shift) {
8087 /* round the start down, and the end up */
8088 sector_t next = s + sectors;
8089 s >>= bb->shift;
8090 next += (1<<bb->shift) - 1;
8091 next >>= bb->shift;
8092 sectors = next - s;
8093 }
8094
Bian Yu905b0292013-10-12 01:10:03 -04008095 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008096
8097 p = bb->page;
8098 lo = 0;
8099 hi = bb->count;
8100 /* Find the last range that starts at-or-before 's' */
8101 while (hi - lo > 1) {
8102 int mid = (lo + hi) / 2;
8103 sector_t a = BB_OFFSET(p[mid]);
8104 if (a <= s)
8105 lo = mid;
8106 else
8107 hi = mid;
8108 }
8109 if (hi > lo && BB_OFFSET(p[lo]) > s)
8110 hi = lo;
8111
8112 if (hi > lo) {
8113 /* we found a range that might merge with the start
8114 * of our new range
8115 */
8116 sector_t a = BB_OFFSET(p[lo]);
8117 sector_t e = a + BB_LEN(p[lo]);
8118 int ack = BB_ACK(p[lo]);
8119 if (e >= s) {
8120 /* Yes, we can merge with a previous range */
8121 if (s == a && s + sectors >= e)
8122 /* new range covers old */
8123 ack = acknowledged;
8124 else
8125 ack = ack && acknowledged;
8126
8127 if (e < s + sectors)
8128 e = s + sectors;
8129 if (e - a <= BB_MAX_LEN) {
8130 p[lo] = BB_MAKE(a, e-a, ack);
8131 s = e;
8132 } else {
8133 /* does not all fit in one range,
8134 * make p[lo] maximal
8135 */
8136 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8137 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8138 s = a + BB_MAX_LEN;
8139 }
8140 sectors = e - s;
8141 }
8142 }
8143 if (sectors && hi < bb->count) {
8144 /* 'hi' points to the first range that starts after 's'.
8145 * Maybe we can merge with the start of that range */
8146 sector_t a = BB_OFFSET(p[hi]);
8147 sector_t e = a + BB_LEN(p[hi]);
8148 int ack = BB_ACK(p[hi]);
8149 if (a <= s + sectors) {
8150 /* merging is possible */
8151 if (e <= s + sectors) {
8152 /* full overlap */
8153 e = s + sectors;
8154 ack = acknowledged;
8155 } else
8156 ack = ack && acknowledged;
8157
8158 a = s;
8159 if (e - a <= BB_MAX_LEN) {
8160 p[hi] = BB_MAKE(a, e-a, ack);
8161 s = e;
8162 } else {
8163 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8164 s = a + BB_MAX_LEN;
8165 }
8166 sectors = e - s;
8167 lo = hi;
8168 hi++;
8169 }
8170 }
8171 if (sectors == 0 && hi < bb->count) {
8172 /* we might be able to combine lo and hi */
8173 /* Note: 's' is at the end of 'lo' */
8174 sector_t a = BB_OFFSET(p[hi]);
8175 int lolen = BB_LEN(p[lo]);
8176 int hilen = BB_LEN(p[hi]);
8177 int newlen = lolen + hilen - (s - a);
8178 if (s >= a && newlen < BB_MAX_LEN) {
8179 /* yes, we can combine them */
8180 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8181 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8182 memmove(p + hi, p + hi + 1,
8183 (bb->count - hi - 1) * 8);
8184 bb->count--;
8185 }
8186 }
8187 while (sectors) {
8188 /* didn't merge (it all).
8189 * Need to add a range just before 'hi' */
8190 if (bb->count >= MD_MAX_BADBLOCKS) {
8191 /* No room for more */
8192 rv = 0;
8193 break;
8194 } else {
8195 int this_sectors = sectors;
8196 memmove(p + hi + 1, p + hi,
8197 (bb->count - hi) * 8);
8198 bb->count++;
8199
8200 if (this_sectors > BB_MAX_LEN)
8201 this_sectors = BB_MAX_LEN;
8202 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8203 sectors -= this_sectors;
8204 s += this_sectors;
8205 }
8206 }
8207
8208 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008209 if (!acknowledged)
8210 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008211 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008212
8213 return rv;
8214}
8215
NeilBrown3cb03002011-10-11 16:45:26 +11008216int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008217 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008218{
NeilBrownc6563a82012-05-21 09:27:00 +10008219 int rv;
8220 if (is_new)
8221 s += rdev->new_data_offset;
8222 else
8223 s += rdev->data_offset;
8224 rv = md_set_badblocks(&rdev->badblocks,
8225 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008226 if (rv) {
8227 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008228 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008229 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8230 md_wakeup_thread(rdev->mddev->thread);
8231 }
8232 return rv;
8233}
8234EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8235
8236/*
8237 * Remove a range of bad blocks from the table.
8238 * This may involve extending the table if we spilt a region,
8239 * but it must not fail. So if the table becomes full, we just
8240 * drop the remove request.
8241 */
8242static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8243{
8244 u64 *p;
8245 int lo, hi;
8246 sector_t target = s + sectors;
8247 int rv = 0;
8248
8249 if (bb->shift > 0) {
8250 /* When clearing we round the start up and the end down.
8251 * This should not matter as the shift should align with
8252 * the block size and no rounding should ever be needed.
8253 * However it is better the think a block is bad when it
8254 * isn't than to think a block is not bad when it is.
8255 */
8256 s += (1<<bb->shift) - 1;
8257 s >>= bb->shift;
8258 target >>= bb->shift;
8259 sectors = target - s;
8260 }
8261
8262 write_seqlock_irq(&bb->lock);
8263
8264 p = bb->page;
8265 lo = 0;
8266 hi = bb->count;
8267 /* Find the last range that starts before 'target' */
8268 while (hi - lo > 1) {
8269 int mid = (lo + hi) / 2;
8270 sector_t a = BB_OFFSET(p[mid]);
8271 if (a < target)
8272 lo = mid;
8273 else
8274 hi = mid;
8275 }
8276 if (hi > lo) {
8277 /* p[lo] is the last range that could overlap the
8278 * current range. Earlier ranges could also overlap,
8279 * but only this one can overlap the end of the range.
8280 */
8281 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8282 /* Partial overlap, leave the tail of this range */
8283 int ack = BB_ACK(p[lo]);
8284 sector_t a = BB_OFFSET(p[lo]);
8285 sector_t end = a + BB_LEN(p[lo]);
8286
8287 if (a < s) {
8288 /* we need to split this range */
8289 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008290 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008291 goto out;
8292 }
8293 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8294 bb->count++;
8295 p[lo] = BB_MAKE(a, s-a, ack);
8296 lo++;
8297 }
8298 p[lo] = BB_MAKE(target, end - target, ack);
8299 /* there is no longer an overlap */
8300 hi = lo;
8301 lo--;
8302 }
8303 while (lo >= 0 &&
8304 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8305 /* This range does overlap */
8306 if (BB_OFFSET(p[lo]) < s) {
8307 /* Keep the early parts of this range. */
8308 int ack = BB_ACK(p[lo]);
8309 sector_t start = BB_OFFSET(p[lo]);
8310 p[lo] = BB_MAKE(start, s - start, ack);
8311 /* now low doesn't overlap, so.. */
8312 break;
8313 }
8314 lo--;
8315 }
8316 /* 'lo' is strictly before, 'hi' is strictly after,
8317 * anything between needs to be discarded
8318 */
8319 if (hi - lo > 1) {
8320 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8321 bb->count -= (hi - lo - 1);
8322 }
8323 }
8324
8325 bb->changed = 1;
8326out:
8327 write_sequnlock_irq(&bb->lock);
8328 return rv;
8329}
8330
NeilBrownc6563a82012-05-21 09:27:00 +10008331int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8332 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008333{
NeilBrownc6563a82012-05-21 09:27:00 +10008334 if (is_new)
8335 s += rdev->new_data_offset;
8336 else
8337 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008338 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008339 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008340}
8341EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8342
8343/*
8344 * Acknowledge all bad blocks in a list.
8345 * This only succeeds if ->changed is clear. It is used by
8346 * in-kernel metadata updates
8347 */
8348void md_ack_all_badblocks(struct badblocks *bb)
8349{
8350 if (bb->page == NULL || bb->changed)
8351 /* no point even trying */
8352 return;
8353 write_seqlock_irq(&bb->lock);
8354
majianpengecb178b2012-03-19 12:46:42 +11008355 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008356 u64 *p = bb->page;
8357 int i;
8358 for (i = 0; i < bb->count ; i++) {
8359 if (!BB_ACK(p[i])) {
8360 sector_t start = BB_OFFSET(p[i]);
8361 int len = BB_LEN(p[i]);
8362 p[i] = BB_MAKE(start, len, 1);
8363 }
8364 }
NeilBrownde393cd2011-07-28 11:31:48 +10008365 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008366 }
8367 write_sequnlock_irq(&bb->lock);
8368}
8369EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8370
NeilBrown16c791a2011-07-28 11:31:47 +10008371/* sysfs access to bad-blocks list.
8372 * We present two files.
8373 * 'bad-blocks' lists sector numbers and lengths of ranges that
8374 * are recorded as bad. The list is truncated to fit within
8375 * the one-page limit of sysfs.
8376 * Writing "sector length" to this file adds an acknowledged
8377 * bad block list.
8378 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8379 * been acknowledged. Writing to this file adds bad blocks
8380 * without acknowledging them. This is largely for testing.
8381 */
8382
8383static ssize_t
8384badblocks_show(struct badblocks *bb, char *page, int unack)
8385{
8386 size_t len;
8387 int i;
8388 u64 *p = bb->page;
8389 unsigned seq;
8390
8391 if (bb->shift < 0)
8392 return 0;
8393
8394retry:
8395 seq = read_seqbegin(&bb->lock);
8396
8397 len = 0;
8398 i = 0;
8399
8400 while (len < PAGE_SIZE && i < bb->count) {
8401 sector_t s = BB_OFFSET(p[i]);
8402 unsigned int length = BB_LEN(p[i]);
8403 int ack = BB_ACK(p[i]);
8404 i++;
8405
8406 if (unack && ack)
8407 continue;
8408
8409 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8410 (unsigned long long)s << bb->shift,
8411 length << bb->shift);
8412 }
NeilBrownde393cd2011-07-28 11:31:48 +10008413 if (unack && len == 0)
8414 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008415
8416 if (read_seqretry(&bb->lock, seq))
8417 goto retry;
8418
8419 return len;
8420}
8421
8422#define DO_DEBUG 1
8423
8424static ssize_t
8425badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8426{
8427 unsigned long long sector;
8428 int length;
8429 char newline;
8430#ifdef DO_DEBUG
8431 /* Allow clearing via sysfs *only* for testing/debugging.
8432 * Normally only a successful write may clear a badblock
8433 */
8434 int clear = 0;
8435 if (page[0] == '-') {
8436 clear = 1;
8437 page++;
8438 }
8439#endif /* DO_DEBUG */
8440
8441 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8442 case 3:
8443 if (newline != '\n')
8444 return -EINVAL;
8445 case 2:
8446 if (length <= 0)
8447 return -EINVAL;
8448 break;
8449 default:
8450 return -EINVAL;
8451 }
8452
8453#ifdef DO_DEBUG
8454 if (clear) {
8455 md_clear_badblocks(bb, sector, length);
8456 return len;
8457 }
8458#endif /* DO_DEBUG */
8459 if (md_set_badblocks(bb, sector, length, !unack))
8460 return len;
8461 else
8462 return -ENOSPC;
8463}
8464
Adrian Bunk75c96f82005-05-05 16:16:09 -07008465static int md_notify_reboot(struct notifier_block *this,
8466 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467{
8468 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008469 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008470 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471
NeilBrownc744a652012-03-19 12:46:37 +11008472 for_each_mddev(mddev, tmp) {
8473 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008474 if (mddev->pers)
8475 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008476 if (mddev->persistent)
8477 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008478 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008479 }
NeilBrownc744a652012-03-19 12:46:37 +11008480 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 }
NeilBrownc744a652012-03-19 12:46:37 +11008482 /*
8483 * certain more exotic SCSI devices are known to be
8484 * volatile wrt too early system reboots. While the
8485 * right place to handle this issue is the given
8486 * driver, we do want to have a safe RAID driver ...
8487 */
8488 if (need_delay)
8489 mdelay(1000*1);
8490
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 return NOTIFY_DONE;
8492}
8493
Adrian Bunk75c96f82005-05-05 16:16:09 -07008494static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 .notifier_call = md_notify_reboot,
8496 .next = NULL,
8497 .priority = INT_MAX, /* before any real devices */
8498};
8499
8500static void md_geninit(void)
8501{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008502 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008504 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505}
8506
Adrian Bunk75c96f82005-05-05 16:16:09 -07008507static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508{
Tejun Heoe804ac72010-10-15 15:36:08 +02008509 int ret = -ENOMEM;
8510
Tejun Heoada609e2011-01-25 14:35:54 +01008511 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008512 if (!md_wq)
8513 goto err_wq;
8514
8515 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8516 if (!md_misc_wq)
8517 goto err_misc_wq;
8518
8519 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8520 goto err_md;
8521
8522 if ((ret = register_blkdev(0, "mdp")) < 0)
8523 goto err_mdp;
8524 mdp_major = ret;
8525
NeilBrownaf5628f2014-07-31 13:54:54 +10008526 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008527 md_probe, NULL, NULL);
8528 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 md_probe, NULL, NULL);
8530
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008532 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
8534 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536
Tejun Heoe804ac72010-10-15 15:36:08 +02008537err_mdp:
8538 unregister_blkdev(MD_MAJOR, "md");
8539err_md:
8540 destroy_workqueue(md_misc_wq);
8541err_misc_wq:
8542 destroy_workqueue(md_wq);
8543err_wq:
8544 return ret;
8545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546
8547#ifndef MODULE
8548
8549/*
8550 * Searches all registered partitions for autorun RAID arrays
8551 * at boot time.
8552 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008553
8554static LIST_HEAD(all_detected_devices);
8555struct detected_devices_node {
8556 struct list_head list;
8557 dev_t dev;
8558};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559
8560void md_autodetect_dev(dev_t dev)
8561{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008562 struct detected_devices_node *node_detected_dev;
8563
8564 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8565 if (node_detected_dev) {
8566 node_detected_dev->dev = dev;
8567 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8568 } else {
8569 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8570 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572}
8573
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574static void autostart_arrays(int part)
8575{
NeilBrown3cb03002011-10-11 16:45:26 +11008576 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008577 struct detected_devices_node *node_detected_dev;
8578 dev_t dev;
8579 int i_scanned, i_passed;
8580
8581 i_scanned = 0;
8582 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
8584 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8585
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008586 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8587 i_scanned++;
8588 node_detected_dev = list_entry(all_detected_devices.next,
8589 struct detected_devices_node, list);
8590 list_del(&node_detected_dev->list);
8591 dev = node_detected_dev->dev;
8592 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008593 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 if (IS_ERR(rdev))
8595 continue;
8596
NeilBrown403df472014-09-30 15:52:29 +10008597 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598 continue;
NeilBrown403df472014-09-30 15:52:29 +10008599
NeilBrownd0fae182008-03-04 14:29:31 -08008600 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008602 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008604
8605 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8606 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607
8608 autorun_devices(part);
8609}
8610
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008611#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
8613static __exit void md_exit(void)
8614{
NeilBrownfd01b882011-10-11 16:47:53 +11008615 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008617 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008618
NeilBrownaf5628f2014-07-31 13:54:54 +10008619 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008620 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008622 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 unregister_blkdev(mdp_major, "mdp");
8624 unregister_reboot_notifier(&md_notifier);
8625 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008626
8627 /* We cannot unload the modules while some process is
8628 * waiting for us in select() or poll() - wake them up
8629 */
8630 md_unloading = 1;
8631 while (waitqueue_active(&md_event_waiters)) {
8632 /* not safe to leave yet */
8633 wake_up(&md_event_waiters);
8634 msleep(delay);
8635 delay += delay;
8636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008638
NeilBrown29ac4aa2008-02-06 01:39:58 -08008639 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008641 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008643 destroy_workqueue(md_misc_wq);
8644 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645}
8646
Dan Williams685784a2007-07-09 11:56:42 -07008647subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648module_exit(md_exit)
8649
NeilBrownf91de922005-11-08 21:39:36 -08008650static int get_ro(char *buffer, struct kernel_param *kp)
8651{
8652 return sprintf(buffer, "%d", start_readonly);
8653}
8654static int set_ro(const char *val, struct kernel_param *kp)
8655{
8656 char *e;
8657 int num = simple_strtoul(val, &e, 10);
8658 if (*val && (*e == '\0' || *e == '\n')) {
8659 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008660 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008661 }
8662 return -EINVAL;
8663}
8664
NeilBrown80ca3a42006-07-10 04:44:18 -07008665module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8666module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008667
NeilBrownefeb53c2009-01-09 08:31:10 +11008668module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008669
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670EXPORT_SYMBOL(register_md_personality);
8671EXPORT_SYMBOL(unregister_md_personality);
8672EXPORT_SYMBOL(md_error);
8673EXPORT_SYMBOL(md_done_sync);
8674EXPORT_SYMBOL(md_write_start);
8675EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676EXPORT_SYMBOL(md_register_thread);
8677EXPORT_SYMBOL(md_unregister_thread);
8678EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008680EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008682MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008683MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008684MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);