blob: 709755fb6d7b62823d2291c99397ca0111adcbaf [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
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
NeilBrown746d3202013-04-24 11:42:41 +100073static int remove_and_add_spares(struct mddev *mddev,
74 struct md_rdev *this);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
Robert Becker1e509152009-12-14 12:49:58 +110077 * Default number of read corrections we'll attempt on an rdev
78 * before ejecting it from the array. We divide the read error
79 * count by 2 for every hour elapsed between read errors.
80 */
81#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
82/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
84 * is 1000 KB/sec, so the extra system load does not show up that much.
85 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070086 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * subsystem is idle. There is also an 'absolute maximum' reconstruction
88 * speed limit - in case reconstruction slows down your system despite
89 * idle IO detection.
90 *
91 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080092 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94
95static int sysctl_speed_limit_min = 1000;
96static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110097static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080098{
99 return mddev->sync_speed_min ?
100 mddev->sync_speed_min : sysctl_speed_limit_min;
101}
102
NeilBrownfd01b882011-10-11 16:47:53 +1100103static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800104{
105 return mddev->sync_speed_max ?
106 mddev->sync_speed_max : sysctl_speed_limit_max;
107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static struct ctl_table_header *raid_table_header;
110
Joe Perches82592c32013-11-14 15:16:18 +1100111static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700116 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800117 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 },
119 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .procname = "speed_limit_max",
121 .data = &sysctl_speed_limit_max,
122 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800126 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Joe Perches82592c32013-11-14 15:16:18 +1100129static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .procname = "raid",
132 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700133 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .child = raid_table,
135 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800136 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Joe Perches82592c32013-11-14 15:16:18 +1100139static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .procname = "dev",
142 .maxlen = 0,
143 .mode = 0555,
144 .child = raid_dir_table,
145 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800146 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700149static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
NeilBrownf91de922005-11-08 21:39:36 -0800151static int start_readonly;
152
NeilBrowna167f662010-10-26 18:31:13 +1100153/* bio_clone_mddev
154 * like bio_clone, but with a local bio set
155 */
156
NeilBrowna167f662010-10-26 18:31:13 +1100157struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100158 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100159{
160 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 if (!mddev || !mddev->bio_set)
163 return bio_alloc(gfp_mask, nr_iovecs);
164
Kent Overstreet395c72a2012-09-06 15:34:55 -0700165 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100166 if (!b)
167 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100168 return b;
169}
170EXPORT_SYMBOL_GPL(bio_alloc_mddev);
171
172struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100173 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100174{
NeilBrowna167f662010-10-26 18:31:13 +1100175 if (!mddev || !mddev->bio_set)
176 return bio_clone(bio, gfp_mask);
177
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700178 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100179}
180EXPORT_SYMBOL_GPL(bio_clone_mddev);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
NeilBrownd7603b72006-01-06 00:20:30 -0800183 * We have a system wide 'event count' that is incremented
184 * on any 'interesting' event, and readers of /proc/mdstat
185 * can use 'poll' or 'select' to find out when the event
186 * count increases.
187 *
188 * Events are:
189 * start array, stop array, error, add device, remove device,
190 * start build, activate spare
191 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800192static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800193static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100194void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800195{
196 atomic_inc(&md_event_count);
197 wake_up(&md_event_waiters);
198}
NeilBrown29269552006-03-27 01:18:10 -0800199EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800200
NeilBrownc331eb02006-05-30 21:27:13 -0700201/* Alternate version that can be called from interrupts
202 * when calling sysfs_notify isn't needed.
203 */
NeilBrownfd01b882011-10-11 16:47:53 +1100204static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700205{
206 atomic_inc(&md_event_count);
207 wake_up(&md_event_waiters);
208}
209
NeilBrownd7603b72006-01-06 00:20:30 -0800210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * Enables to iterate over all existing md arrays
212 * all_mddevs_lock protects this list.
213 */
214static LIST_HEAD(all_mddevs);
215static DEFINE_SPINLOCK(all_mddevs_lock);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
218 * iterates through all used mddevs in the system.
219 * We take care to grab the all_mddevs_lock whenever navigating
220 * the list, and to always hold a refcount when unlocked.
221 * Any code which breaks out of this loop while own
222 * a reference to the current mddev and must mddev_put it.
223 */
NeilBrownfd01b882011-10-11 16:47:53 +1100224#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000226 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100227 _tmp = all_mddevs.next; \
228 _mddev = NULL;}); \
229 ({ if (_tmp != &all_mddevs) \
230 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 if (_mddev) mddev_put(_mddev); \
233 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
234 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100236 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 )
238
NeilBrown409c57f2009-03-31 14:39:39 +1100239/* Rather than calling directly into the personality make_request function,
240 * IO requests come here first so that we can check if the device is
241 * being suspended pending a reconfiguration.
242 * We hold a refcount over the call to ->make_request. By the time that
243 * call has finished, the bio has been linked into some internal structure
244 * and so is visible to ->quiesce(), so we don't need the refcount any more.
245 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200246static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
NeilBrown49077322010-03-25 16:20:56 +1100248 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100249 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500250 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100251
NeilBrown0ca69882011-01-14 09:14:33 +1100252 if (mddev == NULL || mddev->pers == NULL
253 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100254 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200255 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100256 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100257 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
258 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
259 return;
260 }
NeilBrown0ca69882011-01-14 09:14:33 +1100261 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100262 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200263 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100264 DEFINE_WAIT(__wait);
265 for (;;) {
266 prepare_to_wait(&mddev->sb_wait, &__wait,
267 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200268 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100269 break;
270 rcu_read_unlock();
271 schedule();
272 rcu_read_lock();
273 }
274 finish_wait(&mddev->sb_wait, &__wait);
275 }
276 atomic_inc(&mddev->active_io);
277 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100278
Chris Masone91ece52011-02-07 19:21:48 -0500279 /*
280 * save the sectors now since our bio can
281 * go away inside make_request
282 */
283 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200284 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100285
Gu Zheng18c0b222014-11-24 11:05:26 +0800286 generic_start_io_acct(rw, sectors, &mddev->gendisk->part0);
NeilBrown49077322010-03-25 16:20:56 +1100287
NeilBrown409c57f2009-03-31 14:39:39 +1100288 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
289 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
NeilBrown9e35b992010-04-06 14:23:02 +1000292/* mddev_suspend makes sure no new requests are submitted
293 * to the device, and that any requests that have been submitted
294 * are completely handled.
295 * Once ->stop is called and completes, the module will be completely
296 * unused.
297 */
NeilBrownfd01b882011-10-11 16:47:53 +1100298void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100299{
300 BUG_ON(mddev->suspended);
301 mddev->suspended = 1;
302 synchronize_rcu();
303 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
304 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500305
306 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100307}
NeilBrown390ee602010-06-01 19:37:27 +1000308EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100309
NeilBrownfd01b882011-10-11 16:47:53 +1100310void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100311{
312 mddev->suspended = 0;
313 wake_up(&mddev->sb_wait);
314 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500315
Jonathan Brassow47525e52012-05-22 13:55:29 +1000316 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500317 md_wakeup_thread(mddev->thread);
318 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100319}
NeilBrown390ee602010-06-01 19:37:27 +1000320EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100321
NeilBrownfd01b882011-10-11 16:47:53 +1100322int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000323{
324 return mddev->suspended;
325}
326EXPORT_SYMBOL(mddev_congested);
327
NeilBrowna2826aa2009-12-14 12:49:49 +1100328/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200329 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100330 */
331
Tejun Heoe9c74692010-09-03 11:56:18 +0200332static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100333{
NeilBrown3cb03002011-10-11 16:45:26 +1100334 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100335 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100336
337 rdev_dec_pending(rdev, mddev);
338
339 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200340 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200341 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100342 }
343 bio_put(bio);
344}
345
NeilBrowna7a07e62010-12-09 16:04:25 +1100346static void md_submit_flush_data(struct work_struct *ws);
347
NeilBrowna035fc32010-12-09 16:17:51 +1100348static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100349{
NeilBrownfd01b882011-10-11 16:47:53 +1100350 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100351 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100352
NeilBrowna7a07e62010-12-09 16:04:25 +1100353 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
354 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100355 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100356 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100357 if (rdev->raid_disk >= 0 &&
358 !test_bit(Faulty, &rdev->flags)) {
359 /* Take two references, one is dropped
360 * when request finishes, one after
361 * we reclaim rcu_read_lock
362 */
363 struct bio *bi;
364 atomic_inc(&rdev->nr_pending);
365 atomic_inc(&rdev->nr_pending);
366 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000367 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200368 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100369 bi->bi_private = rdev;
370 bi->bi_bdev = rdev->bdev;
371 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200372 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100373 rcu_read_lock();
374 rdev_dec_pending(rdev, mddev);
375 }
376 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100377 if (atomic_dec_and_test(&mddev->flush_pending))
378 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100379}
380
Tejun Heoe9c74692010-09-03 11:56:18 +0200381static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100382{
NeilBrownfd01b882011-10-11 16:47:53 +1100383 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200384 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100385
Kent Overstreet4f024f32013-10-11 15:44:27 -0700386 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100387 /* an empty barrier - all done */
388 bio_endio(bio, 0);
389 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200390 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200391 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100392 }
NeilBrown2b74e122010-12-09 15:59:01 +1100393
394 mddev->flush_bio = NULL;
395 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100396}
397
NeilBrownfd01b882011-10-11 16:47:53 +1100398void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100399{
400 spin_lock_irq(&mddev->write_lock);
401 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200402 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100403 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200404 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100405 spin_unlock_irq(&mddev->write_lock);
406
NeilBrowna035fc32010-12-09 16:17:51 +1100407 INIT_WORK(&mddev->flush_work, submit_flushes);
408 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100409}
Tejun Heoe9c74692010-09-03 11:56:18 +0200410EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100411
NeilBrown74018dc2012-07-31 09:08:15 +0200412void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000413{
NeilBrown9cbb1752012-07-31 09:08:14 +0200414 struct mddev *mddev = cb->data;
415 md_wakeup_thread(mddev->thread);
416 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000417}
NeilBrown9cbb1752012-07-31 09:08:14 +0200418EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000419
NeilBrownfd01b882011-10-11 16:47:53 +1100420static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 atomic_inc(&mddev->active);
423 return mddev;
424}
425
Dan Williams5fd3a172009-03-04 00:57:25 -0700426static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100427
NeilBrownfd01b882011-10-11 16:47:53 +1100428static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
NeilBrowna167f662010-10-26 18:31:13 +1100430 struct bio_set *bs = NULL;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
433 return;
NeilBrownd3374822009-01-09 08:31:10 +1100434 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100435 mddev->ctime == 0 && !mddev->hold_active) {
436 /* Array is not configured at all, and not held active,
437 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100438 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100439 bs = mddev->bio_set;
440 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100441 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200442 /* We did a probe so need to clean up. Call
443 * queue_work inside the spinlock so that
444 * flush_workqueue() after mddev_find will
445 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100446 */
447 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200448 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100449 } else
450 kfree(mddev);
451 }
452 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100453 if (bs)
454 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
NeilBrownfd01b882011-10-11 16:47:53 +1100457void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100458{
459 mutex_init(&mddev->open_mutex);
460 mutex_init(&mddev->reconfig_mutex);
461 mutex_init(&mddev->bitmap_info.mutex);
462 INIT_LIST_HEAD(&mddev->disks);
463 INIT_LIST_HEAD(&mddev->all_mddevs);
464 init_timer(&mddev->safemode_timer);
465 atomic_set(&mddev->active, 1);
466 atomic_set(&mddev->openers, 0);
467 atomic_set(&mddev->active_io, 0);
468 spin_lock_init(&mddev->write_lock);
469 atomic_set(&mddev->flush_pending, 0);
470 init_waitqueue_head(&mddev->sb_wait);
471 init_waitqueue_head(&mddev->recovery_wait);
472 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000473 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500474 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100475 mddev->resync_min = 0;
476 mddev->resync_max = MaxSector;
477 mddev->level = LEVEL_NONE;
478}
NeilBrown390ee602010-06-01 19:37:27 +1000479EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100480
NeilBrownf72ffdd2014-09-30 14:23:59 +1000481static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
NeilBrownfd01b882011-10-11 16:47:53 +1100483 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
NeilBrown8f5f02c2011-02-16 13:58:51 +1100485 if (unit && MAJOR(unit) != MD_MAJOR)
486 unit &= ~((1<<MdpMinorShift)-1);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 retry:
489 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
NeilBrownefeb53c2009-01-09 08:31:10 +1100491 if (unit) {
492 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
493 if (mddev->unit == unit) {
494 mddev_get(mddev);
495 spin_unlock(&all_mddevs_lock);
496 kfree(new);
497 return mddev;
498 }
499
500 if (new) {
501 list_add(&new->all_mddevs, &all_mddevs);
502 spin_unlock(&all_mddevs_lock);
503 new->hold_active = UNTIL_IOCTL;
504 return new;
505 }
506 } else if (new) {
507 /* find an unused unit number */
508 static int next_minor = 512;
509 int start = next_minor;
510 int is_free = 0;
511 int dev = 0;
512 while (!is_free) {
513 dev = MKDEV(MD_MAJOR, next_minor);
514 next_minor++;
515 if (next_minor > MINORMASK)
516 next_minor = 0;
517 if (next_minor == start) {
518 /* Oh dear, all in use. */
519 spin_unlock(&all_mddevs_lock);
520 kfree(new);
521 return NULL;
522 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000523
NeilBrownefeb53c2009-01-09 08:31:10 +1100524 is_free = 1;
525 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
526 if (mddev->unit == dev) {
527 is_free = 0;
528 break;
529 }
530 }
531 new->unit = dev;
532 new->md_minor = MINOR(dev);
533 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 list_add(&new->all_mddevs, &all_mddevs);
535 spin_unlock(&all_mddevs_lock);
536 return new;
537 }
538 spin_unlock(&all_mddevs_lock);
539
NeilBrown9ffae0c2006-01-06 00:20:32 -0800540 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!new)
542 return NULL;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 new->unit = unit;
545 if (MAJOR(unit) == MD_MAJOR)
546 new->md_minor = MINOR(unit);
547 else
548 new->md_minor = MINOR(unit) >> MdpMinorShift;
549
NeilBrownfafd7fb2010-04-01 15:55:30 +1100550 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto retry;
553}
554
NeilBrownf72ffdd2014-09-30 14:23:59 +1000555static inline int __must_check mddev_lock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800557 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrown29f097c2013-11-14 17:54:51 +1100560/* Sometimes we need to take the lock in a situation where
561 * failure due to interrupts is not acceptable.
562 */
NeilBrownf72ffdd2014-09-30 14:23:59 +1000563static inline void mddev_lock_nointr(struct mddev *mddev)
NeilBrown29f097c2013-11-14 17:54:51 +1100564{
565 mutex_lock(&mddev->reconfig_mutex);
566}
567
NeilBrownfd01b882011-10-11 16:47:53 +1100568static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100569{
570 return mutex_is_locked(&mddev->reconfig_mutex);
571}
572
NeilBrownf72ffdd2014-09-30 14:23:59 +1000573static inline int mddev_trylock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800575 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
NeilBrownb6eb1272010-04-15 10:13:47 +1000578static struct attribute_group md_redundancy_group;
579
NeilBrownf72ffdd2014-09-30 14:23:59 +1000580static void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
NeilBrowna64c8762010-04-14 17:15:37 +1000582 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000583 /* These cannot be removed under reconfig_mutex as
584 * an access to the files will try to take reconfig_mutex
585 * while holding the file unremovable, which leads to
586 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000587 * So hold set sysfs_active while the remove in happeing,
588 * and anything else which might set ->to_remove or my
589 * otherwise change the sysfs namespace will fail with
590 * -EBUSY if sysfs_active is still set.
591 * We set sysfs_active under reconfig_mutex and elsewhere
592 * test it under the same mutex to ensure its correct value
593 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000594 */
NeilBrowna64c8762010-04-14 17:15:37 +1000595 struct attribute_group *to_remove = mddev->to_remove;
596 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000598 mutex_unlock(&mddev->reconfig_mutex);
599
NeilBrown00bcb4a2010-06-01 19:37:23 +1000600 if (mddev->kobj.sd) {
601 if (to_remove != &md_redundancy_group)
602 sysfs_remove_group(&mddev->kobj, to_remove);
603 if (mddev->pers == NULL ||
604 mddev->pers->sync_request == NULL) {
605 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
606 if (mddev->sysfs_action)
607 sysfs_put(mddev->sysfs_action);
608 mddev->sysfs_action = NULL;
609 }
NeilBrowna64c8762010-04-14 17:15:37 +1000610 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000611 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000612 } else
613 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Chris Dunlop751e67c2011-10-19 16:48:26 +1100615 /* As we've dropped the mutex we need a spinlock to
616 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000617 */
618 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700619 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000620 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
NeilBrown1ca69c42012-10-11 13:37:33 +1100623static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
624{
625 struct md_rdev *rdev;
626
627 rdev_for_each_rcu(rdev, mddev)
628 if (rdev->desc_nr == nr)
629 return rdev;
630
631 return NULL;
632}
633
634static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
NeilBrown3cb03002011-10-11 16:45:26 +1100636 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
NeilBrowndafb20f2012-03-19 12:46:39 +1100638 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (rdev->bdev->bd_dev == dev)
640 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return NULL;
643}
644
NeilBrown1ca69c42012-10-11 13:37:33 +1100645static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
646{
647 struct md_rdev *rdev;
648
649 rdev_for_each_rcu(rdev, mddev)
650 if (rdev->bdev->bd_dev == dev)
651 return rdev;
652
653 return NULL;
654}
655
NeilBrown84fc4b52011-10-11 16:49:58 +1100656static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800657{
NeilBrown84fc4b52011-10-11 16:49:58 +1100658 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800659 list_for_each_entry(pers, &pers_list, list) {
660 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800661 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800662 if (strcmp(pers->name, clevel)==0)
663 return pers;
664 }
NeilBrown2604b702006-01-06 00:20:36 -0800665 return NULL;
666}
667
Andre Nollb73df2d2008-07-11 22:02:23 +1000668/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100669static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100671 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000672 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
NeilBrownf72ffdd2014-09-30 14:23:59 +1000675static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 rdev->sb_page = alloc_page(GFP_KERNEL);
678 if (!rdev->sb_page) {
679 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000680 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 return 0;
684}
685
NeilBrown545c8792012-05-22 13:54:30 +1000686void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800689 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 rdev->sb_loaded = 0;
691 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000692 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100693 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
NeilBrown2699b672011-07-28 11:31:47 +1000695 if (rdev->bb_page) {
696 put_page(rdev->bb_page);
697 rdev->bb_page = NULL;
698 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000699 kfree(rdev->badblocks.page);
700 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
NeilBrown545c8792012-05-22 13:54:30 +1000702EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
NeilBrown6712ecf2007-09-27 12:47:43 +0200704static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700705{
NeilBrown3cb03002011-10-11 16:45:26 +1100706 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100707 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700708
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700709 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
710 printk("md: super_written gets error=%d, uptodate=%d\n",
711 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
712 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800713 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700714 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700715
NeilBrowna9701a32005-11-08 21:39:34 -0800716 if (atomic_dec_and_test(&mddev->pending_writes))
717 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700718 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700719}
720
NeilBrownfd01b882011-10-11 16:47:53 +1100721void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700722 sector_t sector, int size, struct page *page)
723{
724 /* write first size bytes of page to sector of rdev
725 * Increment mddev->pending_writes before returning
726 * and decrement it on completion, waking up sb_wait
727 * if zero is reached.
728 * If an error occurred, call md_error
729 */
NeilBrowna167f662010-10-26 18:31:13 +1100730 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700731
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100732 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700733 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700734 bio_add_page(bio, page, size, 0);
735 bio->bi_private = rdev;
736 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800737
NeilBrown7bfa19f2005-06-21 17:17:28 -0700738 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000739 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800740}
741
NeilBrownfd01b882011-10-11 16:47:53 +1100742void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800743{
Tejun Heoe9c74692010-09-03 11:56:18 +0200744 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000745 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746}
747
NeilBrown3cb03002011-10-11 16:45:26 +1100748int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100749 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
NeilBrowna167f662010-10-26 18:31:13 +1100751 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int ret;
753
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100754 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
755 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100756 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700757 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000758 else if (rdev->mddev->reshape_position != MaxSector &&
759 (rdev->mddev->reshape_backwards ==
760 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700761 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100762 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700763 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700765 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
768 bio_put(bio);
769 return ret;
770}
NeilBrowna8745db2006-01-06 00:20:34 -0800771EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
NeilBrownf72ffdd2014-09-30 14:23:59 +1000773static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (rdev->sb_loaded)
778 return 0;
779
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto fail;
782 rdev->sb_loaded = 1;
783 return 0;
784
785fail:
786 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
787 bdevname(rdev->bdev,b));
788 return -EINVAL;
789}
790
791static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
792{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000793 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000794 sb1->set_uuid1 == sb2->set_uuid1 &&
795 sb1->set_uuid2 == sb2->set_uuid2 &&
796 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
800{
801 int ret;
802 mdp_super_t *tmp1, *tmp2;
803
804 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
805 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
806
807 if (!tmp1 || !tmp2) {
808 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100809 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto abort;
811 }
812
813 *tmp1 = *sb1;
814 *tmp2 = *sb2;
815
816 /*
817 * nr_disks is not constant
818 */
819 tmp1->nr_disks = 0;
820 tmp2->nr_disks = 0;
821
Andre Nollce0c8e02008-07-11 22:02:20 +1000822 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700824 kfree(tmp1);
825 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return ret;
827}
828
NeilBrown4d167f02007-05-09 02:35:37 -0700829static u32 md_csum_fold(u32 csum)
830{
831 csum = (csum & 0xffff) + (csum >> 16);
832 return (csum & 0xffff) + (csum >> 16);
833}
834
NeilBrownf72ffdd2014-09-30 14:23:59 +1000835static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
NeilBrown4d167f02007-05-09 02:35:37 -0700837 u64 newcsum = 0;
838 u32 *sb32 = (u32*)sb;
839 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned int disk_csum, csum;
841
842 disk_csum = sb->sb_csum;
843 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700844
845 for (i = 0; i < MD_SB_BYTES/4 ; i++)
846 newcsum += sb32[i];
847 csum = (newcsum & 0xffffffff) + (newcsum>>32);
848
NeilBrown4d167f02007-05-09 02:35:37 -0700849#ifdef CONFIG_ALPHA
850 /* This used to use csum_partial, which was wrong for several
851 * reasons including that different results are returned on
852 * different architectures. It isn't critical that we get exactly
853 * the same return value as before (we always csum_fold before
854 * testing, and that removes any differences). However as we
855 * know that csum_partial always returned a 16bit value on
856 * alphas, do a fold to maximise conformity to previous behaviour.
857 */
858 sb->sb_csum = md_csum_fold(disk_csum);
859#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return csum;
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/*
866 * Handle superblock details.
867 * We want to be able to handle multiple superblock formats
868 * so we have a common interface to them all, and an array of
869 * different handlers.
870 * We rely on user-space to write the initial superblock, and support
871 * reading and updating of superblocks.
872 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100873 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * loads and validates a superblock on dev.
875 * if refdev != NULL, compare superblocks on both devices
876 * Return:
877 * 0 - dev has a superblock that is compatible with refdev
878 * 1 - dev has a superblock that is compatible and newer than refdev
879 * so dev should be used as the refdev in future
880 * -EINVAL superblock incompatible or invalid
881 * -othererror e.g. -EIO
882 *
NeilBrownfd01b882011-10-11 16:47:53 +1100883 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * Verify that dev is acceptable into mddev.
885 * The first time, mddev->raid_disks will be 0, and data from
886 * dev should be merged in. Subsequent calls check that dev
887 * is new enough. Return 0 or -EINVAL
888 *
NeilBrownfd01b882011-10-11 16:47:53 +1100889 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * Update the superblock for rdev with data in mddev
891 * This does not write to disc.
892 *
893 */
894
895struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000896 char *name;
897 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000898 int (*load_super)(struct md_rdev *rdev,
899 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000900 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000901 int (*validate_super)(struct mddev *mddev,
902 struct md_rdev *rdev);
903 void (*sync_super)(struct mddev *mddev,
904 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100905 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000906 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000907 int (*allow_new_offset)(struct md_rdev *rdev,
908 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909};
910
911/*
Andre Noll0894cc32009-06-18 08:49:23 +1000912 * Check that the given mddev has no bitmap.
913 *
914 * This function is called from the run method of all personalities that do not
915 * support bitmaps. It prints an error message and returns non-zero if mddev
916 * has a bitmap. Otherwise, it returns 0.
917 *
918 */
NeilBrownfd01b882011-10-11 16:47:53 +1100919int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000920{
NeilBrownc3d97142009-12-14 12:49:52 +1100921 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000922 return 0;
923 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
924 mdname(mddev), mddev->pers->name);
925 return 1;
926}
927EXPORT_SYMBOL(md_check_no_bitmap);
928
929/*
NeilBrownf72ffdd2014-09-30 14:23:59 +1000930 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
NeilBrown3cb03002011-10-11 16:45:26 +1100932static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
935 mdp_super_t *sb;
936 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /*
Andre Noll0f420352008-07-11 22:02:23 +1000939 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * it's at the end of the disk.
941 *
942 * It also happens to be a multiple of 4Kb.
943 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100944 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
NeilBrown0002b272005-09-09 16:23:53 -0700946 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (ret) return ret;
948
949 ret = -EINVAL;
950
951 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000952 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (sb->md_magic != MD_SB_MAGIC) {
955 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
956 b);
957 goto abort;
958 }
959
960 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800961 sb->minor_version < 90 ||
962 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
964 sb->major_version, sb->minor_version,
965 b);
966 goto abort;
967 }
968
969 if (sb->raid_disks <= 0)
970 goto abort;
971
NeilBrown4d167f02007-05-09 02:35:37 -0700972 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
974 b);
975 goto abort;
976 }
977
978 rdev->preferred_minor = sb->md_minor;
979 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +1000980 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700981 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +1000982 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (sb->level == LEVEL_MULTIPATH)
985 rdev->desc_nr = -1;
986 else
987 rdev->desc_nr = sb->this_disk.number;
988
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700989 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700991 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +1000993 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!uuid_equal(refsb, sb)) {
995 printk(KERN_WARNING "md: %s has different UUID to %s\n",
996 b, bdevname(refdev->bdev,b2));
997 goto abort;
998 }
999 if (!sb_equal(refsb, sb)) {
1000 printk(KERN_WARNING "md: %s has same UUID"
1001 " but different superblock to %s\n",
1002 b, bdevname(refdev->bdev, b2));
1003 goto abort;
1004 }
1005 ev1 = md_event(sb);
1006 ev2 = md_event(refsb);
1007 if (ev1 > ev2)
1008 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001009 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ret = 0;
1011 }
NeilBrown8190e752009-06-18 08:48:58 +10001012 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001013 /* Limit to 4TB as metadata cannot record more than that.
1014 * (not needed for Linear and RAID0 as metadata doesn't
1015 * record this size)
1016 */
1017 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001018 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
NeilBrown27a7b262011-09-10 17:21:28 +10001020 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001021 /* "this cannot possibly happen" ... */
1022 ret = -EINVAL;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 abort:
1025 return ret;
1026}
1027
1028/*
1029 * validate_super for 0.90.0
1030 */
NeilBrownfd01b882011-10-11 16:47:53 +11001031static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001034 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001035 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
NeilBrown41158c72005-06-21 17:17:25 -07001037 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001038 clear_bit(Faulty, &rdev->flags);
1039 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001040 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001041 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (mddev->raid_disks == 0) {
1044 mddev->major_version = 0;
1045 mddev->minor_version = sb->minor_version;
1046 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001047 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001048 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 mddev->ctime = sb->ctime;
1050 mddev->utime = sb->utime;
1051 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001052 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 mddev->layout = sb->layout;
1054 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001055 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001056 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001057 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001058 mddev->bitmap_info.space = 0;
1059 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001060 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001061 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001062 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
NeilBrownf6705572006-03-27 01:18:11 -08001064 if (mddev->minor_version >= 91) {
1065 mddev->reshape_position = sb->reshape_position;
1066 mddev->delta_disks = sb->delta_disks;
1067 mddev->new_level = sb->new_level;
1068 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001069 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001070 if (mddev->delta_disks < 0)
1071 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001072 } else {
1073 mddev->reshape_position = MaxSector;
1074 mddev->delta_disks = 0;
1075 mddev->new_level = mddev->level;
1076 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001077 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001078 }
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (sb->state & (1<<MD_SB_CLEAN))
1081 mddev->recovery_cp = MaxSector;
1082 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001083 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 sb->events_lo == sb->cp_events_lo) {
1085 mddev->recovery_cp = sb->recovery_cp;
1086 } else
1087 mddev->recovery_cp = 0;
1088 }
1089
1090 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1091 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1092 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1093 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1094
1095 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001096
1097 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001098 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001099 mddev->bitmap_info.offset =
1100 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001101 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001102 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001103 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001104
NeilBrown41158c72005-06-21 17:17:25 -07001105 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001106 /* Insist on good event counter while assembling, except
1107 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001109 if (sb->disks[rdev->desc_nr].state & (
1110 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001111 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001112 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001113 } else if (mddev->bitmap) {
1114 /* if adding to array with a bitmap, then we can accept an
1115 * older device ... but not too old.
1116 */
NeilBrown41158c72005-06-21 17:17:25 -07001117 if (ev1 < mddev->bitmap->events_cleared)
1118 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001119 if (ev1 < mddev->events)
1120 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001121 } else {
1122 if (ev1 < mddev->events)
1123 /* just a hot-add of a new device, leave raid_disk at -1 */
1124 return 0;
1125 }
NeilBrown41158c72005-06-21 17:17:25 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 desc = sb->disks + rdev->desc_nr;
1129
1130 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001131 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001132 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1133 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001134 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001136 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001137 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1138 /* active but not in sync implies recovery up to
1139 * reshape position. We don't know exactly where
1140 * that is, so set to zero for now */
1141 if (mddev->minor_version >= 91) {
1142 rdev->recovery_offset = 0;
1143 rdev->raid_disk = desc->raid_disk;
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001146 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1147 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001148 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001149 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
1153/*
1154 * sync_super for 0.90.0
1155 */
NeilBrownfd01b882011-10-11 16:47:53 +11001156static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001159 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* make rdev->sb match mddev data..
1163 *
1164 * 1/ zero out disks
1165 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1166 * 3/ any empty disks < next_spare become removed
1167 *
1168 * disks[0] gets initialised to REMOVED because
1169 * we cannot be sure from other fields if it has
1170 * been initialised or not.
1171 */
1172 int i;
1173 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1174
NeilBrown61181562005-09-09 16:24:02 -07001175 rdev->sb_size = MD_SB_BYTES;
1176
Namhyung Kim65a06f062011-07-27 11:00:36 +10001177 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 memset(sb, 0, sizeof(*sb));
1180
1181 sb->md_magic = MD_SB_MAGIC;
1182 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sb->patch_version = mddev->patch_version;
1184 sb->gvalid_words = 0; /* ignored */
1185 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1186 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1187 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1188 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1189
1190 sb->ctime = mddev->ctime;
1191 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001192 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 sb->raid_disks = mddev->raid_disks;
1194 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001195 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sb->utime = mddev->utime;
1197 sb->state = 0;
1198 sb->events_hi = (mddev->events>>32);
1199 sb->events_lo = (u32)mddev->events;
1200
NeilBrownf6705572006-03-27 01:18:11 -08001201 if (mddev->reshape_position == MaxSector)
1202 sb->minor_version = 90;
1203 else {
1204 sb->minor_version = 91;
1205 sb->reshape_position = mddev->reshape_position;
1206 sb->new_level = mddev->new_level;
1207 sb->delta_disks = mddev->delta_disks;
1208 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001209 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001210 }
1211 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (mddev->in_sync)
1213 {
1214 sb->recovery_cp = mddev->recovery_cp;
1215 sb->cp_events_hi = (mddev->events>>32);
1216 sb->cp_events_lo = (u32)mddev->events;
1217 if (mddev->recovery_cp == MaxSector)
1218 sb->state = (1<< MD_SB_CLEAN);
1219 } else
1220 sb->recovery_cp = 0;
1221
1222 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001223 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
NeilBrownc3d97142009-12-14 12:49:52 +11001225 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001226 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001229 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001231 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001232 int is_active = test_bit(In_sync, &rdev2->flags);
1233
1234 if (rdev2->raid_disk >= 0 &&
1235 sb->minor_version >= 91)
1236 /* we have nowhere to store the recovery_offset,
1237 * but if it is not below the reshape_position,
1238 * we can piggy-back on that.
1239 */
1240 is_active = 1;
1241 if (rdev2->raid_disk < 0 ||
1242 test_bit(Faulty, &rdev2->flags))
1243 is_active = 0;
1244 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001245 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001247 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001248 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 d = &sb->disks[rdev2->desc_nr];
1250 nr_disks++;
1251 d->number = rdev2->desc_nr;
1252 d->major = MAJOR(rdev2->bdev->bd_dev);
1253 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001254 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 d->raid_disk = rdev2->raid_disk;
1256 else
1257 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001258 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001260 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001262 if (test_bit(In_sync, &rdev2->flags))
1263 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 active++;
1265 working++;
1266 } else {
1267 d->state = 0;
1268 spare++;
1269 working++;
1270 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001271 if (test_bit(WriteMostly, &rdev2->flags))
1272 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* now set the "removed" and "faulty" bits on any missing devices */
1275 for (i=0 ; i < mddev->raid_disks ; i++) {
1276 mdp_disk_t *d = &sb->disks[i];
1277 if (d->state == 0 && d->number == 0) {
1278 d->number = i;
1279 d->raid_disk = i;
1280 d->state = (1<<MD_DISK_REMOVED);
1281 d->state |= (1<<MD_DISK_FAULTY);
1282 failed++;
1283 }
1284 }
1285 sb->nr_disks = nr_disks;
1286 sb->active_disks = active;
1287 sb->working_disks = working;
1288 sb->failed_disks = failed;
1289 sb->spare_disks = spare;
1290
1291 sb->this_disk = sb->disks[rdev->desc_nr];
1292 sb->sb_csum = calc_sb_csum(sb);
1293}
1294
1295/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001296 * rdev_size_change for 0.90.0
1297 */
1298static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001299super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001300{
Andre Noll58c0fed2009-03-31 14:33:13 +11001301 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001302 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001303 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001304 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001305 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001306 if (!num_sectors || num_sectors > rdev->sb_start)
1307 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001308 /* Limit to 4TB as metadata cannot record more than that.
1309 * 4TB == 2^32 KB, or 2*2^32 sectors.
1310 */
NeilBrown667a5312012-08-16 16:46:12 +10001311 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001312 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001313 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001314 rdev->sb_page);
1315 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001316 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317}
1318
NeilBrownc6563a82012-05-21 09:27:00 +10001319static int
1320super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1321{
1322 /* non-zero offset changes not possible with v0.90 */
1323 return new_offset == 0;
1324}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325
1326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * version 1 superblock
1328 */
1329
NeilBrownf72ffdd2014-09-30 14:23:59 +10001330static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001332 __le32 disk_csum;
1333 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 unsigned long long newcsum;
1335 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001336 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 disk_csum = sb->sb_csum;
1339 sb->sb_csum = 0;
1340 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001341 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 newcsum += le32_to_cpu(*isuper++);
1343
1344 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001345 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1348 sb->sb_csum = disk_csum;
1349 return cpu_to_le32(csum);
1350}
1351
NeilBrown2699b672011-07-28 11:31:47 +10001352static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1353 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001354static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct mdp_superblock_1 *sb;
1357 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001358 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001359 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001361 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
Andre Noll0f420352008-07-11 22:02:23 +10001364 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * It is always aligned to a 4K boundary and
1366 * depeding on minor_version, it can be:
1367 * 0: At least 8K, but less than 12K, from end of device
1368 * 1: At start of device
1369 * 2: 4K from start of device.
1370 */
1371 switch(minor_version) {
1372 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001373 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001374 sb_start -= 8*2;
1375 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
1377 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001378 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 break;
1380 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001381 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 default:
1384 return -EINVAL;
1385 }
Andre Noll0f420352008-07-11 22:02:23 +10001386 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
NeilBrown0002b272005-09-09 16:23:53 -07001388 /* superblock is rarely larger than 1K, but it can be larger,
1389 * and it is safe to read 4k, so we do that
1390 */
1391 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (ret) return ret;
1393
Namhyung Kim65a06f062011-07-27 11:00:36 +10001394 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1397 sb->major_version != cpu_to_le32(1) ||
1398 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001399 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001400 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return -EINVAL;
1402
1403 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1404 printk("md: invalid superblock checksum on %s\n",
1405 bdevname(rdev->bdev,b));
1406 return -EINVAL;
1407 }
1408 if (le64_to_cpu(sb->data_size) < 10) {
1409 printk("md: data_size too small on %s\n",
1410 bdevname(rdev->bdev,b));
1411 return -EINVAL;
1412 }
NeilBrownc6563a82012-05-21 09:27:00 +10001413 if (sb->pad0 ||
1414 sb->pad3[0] ||
1415 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1416 /* Some padding is non-zero, might be a new feature */
1417 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 rdev->preferred_minor = 0xffff;
1420 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001421 rdev->new_data_offset = rdev->data_offset;
1422 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1423 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1424 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001425 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
NeilBrown0002b272005-09-09 16:23:53 -07001427 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001428 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001429 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001430 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1431
1432 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001433 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001434 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001435 if (minor_version
1436 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1437 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001438
NeilBrown31b65a02006-07-10 04:44:14 -07001439 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1440 rdev->desc_nr = -1;
1441 else
1442 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1443
NeilBrown2699b672011-07-28 11:31:47 +10001444 if (!rdev->bb_page) {
1445 rdev->bb_page = alloc_page(GFP_KERNEL);
1446 if (!rdev->bb_page)
1447 return -ENOMEM;
1448 }
1449 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1450 rdev->badblocks.count == 0) {
1451 /* need to load the bad block list.
1452 * Currently we limit it to one page.
1453 */
1454 s32 offset;
1455 sector_t bb_sector;
1456 u64 *bbp;
1457 int i;
1458 int sectors = le16_to_cpu(sb->bblog_size);
1459 if (sectors > (PAGE_SIZE / 512))
1460 return -EINVAL;
1461 offset = le32_to_cpu(sb->bblog_offset);
1462 if (offset == 0)
1463 return -EINVAL;
1464 bb_sector = (long long)offset;
1465 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1466 rdev->bb_page, READ, true))
1467 return -EIO;
1468 bbp = (u64 *)page_address(rdev->bb_page);
1469 rdev->badblocks.shift = sb->bblog_shift;
1470 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1471 u64 bb = le64_to_cpu(*bbp);
1472 int count = bb & (0x3ff);
1473 u64 sector = bb >> 10;
1474 sector <<= sb->bblog_shift;
1475 count <<= sb->bblog_shift;
1476 if (bb + 1 == 0)
1477 break;
1478 if (md_set_badblocks(&rdev->badblocks,
1479 sector, count, 1) == 0)
1480 return -EINVAL;
1481 }
NeilBrown486adf72013-04-24 11:42:44 +10001482 } else if (sb->bblog_offset != 0)
1483 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001484
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001485 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001486 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001489 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1492 sb->level != refsb->level ||
1493 sb->layout != refsb->layout ||
1494 sb->chunksize != refsb->chunksize) {
1495 printk(KERN_WARNING "md: %s has strangely different"
1496 " superblock to %s\n",
1497 bdevname(rdev->bdev,b),
1498 bdevname(refdev->bdev,b2));
1499 return -EINVAL;
1500 }
1501 ev1 = le64_to_cpu(sb->events);
1502 ev2 = le64_to_cpu(refsb->events);
1503
1504 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001505 ret = 1;
1506 else
1507 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
NeilBrownc6563a82012-05-21 09:27:00 +10001509 if (minor_version) {
1510 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1511 sectors -= rdev->data_offset;
1512 } else
1513 sectors = rdev->sb_start;
1514 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001516 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
NeilBrownfd01b882011-10-11 16:47:53 +11001520static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001522 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001523 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
NeilBrown41158c72005-06-21 17:17:25 -07001525 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001526 clear_bit(Faulty, &rdev->flags);
1527 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001528 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001529 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (mddev->raid_disks == 0) {
1532 mddev->major_version = 1;
1533 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001534 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001535 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1537 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1538 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001539 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 mddev->layout = le32_to_cpu(sb->layout);
1541 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001542 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001543 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001544 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001545 mddev->bitmap_info.space = 0;
1546 /* Default location for bitmap is 1K after superblock
1547 * using 3K - total of 4K
1548 */
NeilBrownc3d97142009-12-14 12:49:52 +11001549 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001550 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001551 mddev->reshape_backwards = 0;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1554 memcpy(mddev->uuid, sb->set_uuid, 16);
1555
1556 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001557
NeilBrown71c08052005-09-09 16:23:51 -07001558 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001559 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001560 mddev->bitmap_info.offset =
1561 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001562 /* Metadata doesn't record how much space is available.
1563 * For 1.0, we assume we can use up to the superblock
1564 * if before, else to 4K beyond superblock.
1565 * For others, assume no change is possible.
1566 */
1567 if (mddev->minor_version > 0)
1568 mddev->bitmap_info.space = 0;
1569 else if (mddev->bitmap_info.offset > 0)
1570 mddev->bitmap_info.space =
1571 8 - mddev->bitmap_info.offset;
1572 else
1573 mddev->bitmap_info.space =
1574 -mddev->bitmap_info.offset;
1575 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001576
NeilBrownf6705572006-03-27 01:18:11 -08001577 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1578 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1579 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1580 mddev->new_level = le32_to_cpu(sb->new_level);
1581 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001582 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001583 if (mddev->delta_disks < 0 ||
1584 (mddev->delta_disks == 0 &&
1585 (le32_to_cpu(sb->feature_map)
1586 & MD_FEATURE_RESHAPE_BACKWARDS)))
1587 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001588 } else {
1589 mddev->reshape_position = MaxSector;
1590 mddev->delta_disks = 0;
1591 mddev->new_level = mddev->level;
1592 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001593 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001594 }
1595
NeilBrown41158c72005-06-21 17:17:25 -07001596 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001597 /* Insist of good event counter while assembling, except for
1598 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001600 if (rdev->desc_nr >= 0 &&
1601 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1602 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1603 if (ev1 < mddev->events)
1604 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001605 } else if (mddev->bitmap) {
1606 /* If adding to array with a bitmap, then we can accept an
1607 * older device, but not too old.
1608 */
NeilBrown41158c72005-06-21 17:17:25 -07001609 if (ev1 < mddev->bitmap->events_cleared)
1610 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001611 if (ev1 < mddev->events)
1612 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001613 } else {
1614 if (ev1 < mddev->events)
1615 /* just a hot-add of a new device, leave raid_disk at -1 */
1616 return 0;
1617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (mddev->level != LEVEL_MULTIPATH) {
1619 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001620 if (rdev->desc_nr < 0 ||
1621 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1622 role = 0xffff;
1623 rdev->desc_nr = -1;
1624 } else
1625 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 switch(role) {
1627 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
1629 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001630 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 default:
NeilBrownf4667222013-12-09 12:04:56 +11001633 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001634 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001635 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001636 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001637 if (!(le32_to_cpu(sb->feature_map) &
1638 MD_FEATURE_RECOVERY_BITMAP))
1639 rdev->saved_raid_disk = -1;
1640 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001641 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 rdev->raid_disk = role;
1643 break;
1644 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001645 if (sb->devflags & WriteMostly1)
1646 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001647 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1648 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001649 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001650 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653}
1654
NeilBrownfd01b882011-10-11 16:47:53 +11001655static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001658 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 int max_dev, i;
1660 /* make rdev->sb match mddev and rdev data. */
1661
Namhyung Kim65a06f062011-07-27 11:00:36 +10001662 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 sb->feature_map = 0;
1665 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001666 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 memset(sb->pad3, 0, sizeof(sb->pad3));
1668
1669 sb->utime = cpu_to_le64((__u64)mddev->utime);
1670 sb->events = cpu_to_le64(mddev->events);
1671 if (mddev->in_sync)
1672 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1673 else
1674 sb->resync_offset = cpu_to_le64(0);
1675
NeilBrown1c05b4b2006-10-21 10:24:08 -07001676 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001677
NeilBrownf0ca3402006-02-02 14:28:04 -08001678 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001679 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001680 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001681 sb->level = cpu_to_le32(mddev->level);
1682 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001683
NeilBrownaeb9b212011-08-25 14:43:08 +10001684 if (test_bit(WriteMostly, &rdev->flags))
1685 sb->devflags |= WriteMostly1;
1686 else
1687 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001688 sb->data_offset = cpu_to_le64(rdev->data_offset);
1689 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001690
NeilBrownc3d97142009-12-14 12:49:52 +11001691 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1692 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001693 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001694 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001695
1696 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001697 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001698 sb->feature_map |=
1699 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1700 sb->recovery_offset =
1701 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001702 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1703 sb->feature_map |=
1704 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001705 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001706 if (test_bit(Replacement, &rdev->flags))
1707 sb->feature_map |=
1708 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001709
NeilBrownf6705572006-03-27 01:18:11 -08001710 if (mddev->reshape_position != MaxSector) {
1711 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1712 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1713 sb->new_layout = cpu_to_le32(mddev->new_layout);
1714 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1715 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001716 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001717 if (mddev->delta_disks == 0 &&
1718 mddev->reshape_backwards)
1719 sb->feature_map
1720 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001721 if (rdev->new_data_offset != rdev->data_offset) {
1722 sb->feature_map
1723 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1724 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1725 - rdev->data_offset));
1726 }
NeilBrownf6705572006-03-27 01:18:11 -08001727 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001728
NeilBrown2699b672011-07-28 11:31:47 +10001729 if (rdev->badblocks.count == 0)
1730 /* Nothing to do for bad blocks*/ ;
1731 else if (sb->bblog_offset == 0)
1732 /* Cannot record bad blocks on this device */
1733 md_error(mddev, rdev);
1734 else {
1735 struct badblocks *bb = &rdev->badblocks;
1736 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1737 u64 *p = bb->page;
1738 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1739 if (bb->changed) {
1740 unsigned seq;
1741
1742retry:
1743 seq = read_seqbegin(&bb->lock);
1744
1745 memset(bbp, 0xff, PAGE_SIZE);
1746
1747 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001748 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001749 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1750 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001751 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001752 }
NeilBrownd0962932012-03-19 12:46:41 +11001753 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001754 if (read_seqretry(&bb->lock, seq))
1755 goto retry;
1756
1757 bb->sector = (rdev->sb_start +
1758 (int)le32_to_cpu(sb->bblog_offset));
1759 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001760 }
1761 }
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001764 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (rdev2->desc_nr+1 > max_dev)
1766 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001767
NeilBrown70471da2009-08-03 10:59:57 +10001768 if (max_dev > le32_to_cpu(sb->max_dev)) {
1769 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001770 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001771 rdev->sb_size = max_dev * 2 + 256;
1772 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1773 if (rdev->sb_size & bmask)
1774 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001775 } else
1776 max_dev = le32_to_cpu(sb->max_dev);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 for (i=0; i<max_dev;i++)
1779 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001780
NeilBrowndafb20f2012-03-19 12:46:39 +11001781 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001783 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001785 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001787 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001788 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 else
1790 sb->dev_roles[i] = cpu_to_le16(0xffff);
1791 }
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 sb->sb_csum = calc_sb_1_csum(sb);
1794}
1795
Chris Webb0cd17fe2008-06-28 08:31:46 +10001796static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001797super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001798{
1799 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001800 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001801 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001802 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001803 if (rdev->data_offset != rdev->new_data_offset)
1804 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001805 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001806 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001807 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001808 max_sectors -= rdev->data_offset;
1809 if (!num_sectors || num_sectors > max_sectors)
1810 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001811 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001812 /* minor version 0 with bitmap we can't move */
1813 return 0;
1814 } else {
1815 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001816 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001817 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001818 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001819 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001820 if (!num_sectors || num_sectors > max_sectors)
1821 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001822 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001823 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001824 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001825 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001826 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001827 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001828 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 rdev->sb_page);
1830 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001831 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001832
1833}
1834
1835static int
1836super_1_allow_new_offset(struct md_rdev *rdev,
1837 unsigned long long new_offset)
1838{
1839 /* All necessary checks on new >= old have been done */
1840 struct bitmap *bitmap;
1841 if (new_offset >= rdev->data_offset)
1842 return 1;
1843
1844 /* with 1.0 metadata, there is no metadata to tread on
1845 * so we can always move back */
1846 if (rdev->mddev->minor_version == 0)
1847 return 1;
1848
1849 /* otherwise we must be sure not to step on
1850 * any metadata, so stay:
1851 * 36K beyond start of superblock
1852 * beyond end of badblocks
1853 * beyond write-intent bitmap
1854 */
1855 if (rdev->sb_start + (32+4)*2 > new_offset)
1856 return 0;
1857 bitmap = rdev->mddev->bitmap;
1858 if (bitmap && !rdev->mddev->bitmap_info.file &&
1859 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001860 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001861 return 0;
1862 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1863 return 0;
1864
1865 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001866}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Adrian Bunk75c96f82005-05-05 16:16:09 -07001868static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 [0] = {
1870 .name = "0.90.0",
1871 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001872 .load_super = super_90_load,
1873 .validate_super = super_90_validate,
1874 .sync_super = super_90_sync,
1875 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001876 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 },
1878 [1] = {
1879 .name = "md-1",
1880 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 .load_super = super_1_load,
1882 .validate_super = super_1_validate,
1883 .sync_super = super_1_sync,
1884 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001885 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 },
1887};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
NeilBrownfd01b882011-10-11 16:47:53 +11001889static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001890{
1891 if (mddev->sync_super) {
1892 mddev->sync_super(mddev, rdev);
1893 return;
1894 }
1895
1896 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1897
1898 super_types[mddev->major_version].sync_super(mddev, rdev);
1899}
1900
NeilBrownfd01b882011-10-11 16:47:53 +11001901static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
NeilBrown3cb03002011-10-11 16:45:26 +11001903 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
NeilBrown4b809912008-07-21 17:05:25 +10001905 rcu_read_lock();
1906 rdev_for_each_rcu(rdev, mddev1)
1907 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001908 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001909 rdev2->bdev->bd_contains) {
1910 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001911 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001912 }
1913 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915}
1916
1917static LIST_HEAD(pending_raid_disks);
1918
Andre Nollac5e7112009-08-03 10:59:47 +10001919/*
1920 * Try to register data integrity profile for an mddev
1921 *
1922 * This is called when an array is started and after a disk has been kicked
1923 * from the array. It only succeeds if all working and active component devices
1924 * are integrity capable with matching profiles.
1925 */
NeilBrownfd01b882011-10-11 16:47:53 +11001926int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001927{
NeilBrown3cb03002011-10-11 16:45:26 +11001928 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001929
Andre Nollac5e7112009-08-03 10:59:47 +10001930 if (list_empty(&mddev->disks))
1931 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001932 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1933 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001934 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001935 /* skip spares and non-functional disks */
1936 if (test_bit(Faulty, &rdev->flags))
1937 continue;
1938 if (rdev->raid_disk < 0)
1939 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001940 if (!reference) {
1941 /* Use the first rdev as the reference */
1942 reference = rdev;
1943 continue;
1944 }
1945 /* does this rdev's profile match the reference profile? */
1946 if (blk_integrity_compare(reference->bdev->bd_disk,
1947 rdev->bdev->bd_disk) < 0)
1948 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001949 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001950 if (!reference || !bdev_get_integrity(reference->bdev))
1951 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001952 /*
1953 * All component devices are integrity capable and have matching
1954 * profiles, register the common profile for the md device.
1955 */
1956 if (blk_integrity_register(mddev->gendisk,
1957 bdev_get_integrity(reference->bdev)) != 0) {
1958 printk(KERN_ERR "md: failed to register integrity for %s\n",
1959 mdname(mddev));
1960 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001961 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001962 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1963 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1964 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1965 mdname(mddev));
1966 return -EINVAL;
1967 }
Andre Nollac5e7112009-08-03 10:59:47 +10001968 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001969}
Andre Nollac5e7112009-08-03 10:59:47 +10001970EXPORT_SYMBOL(md_integrity_register);
1971
1972/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001973void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001974{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001975 struct blk_integrity *bi_rdev;
1976 struct blk_integrity *bi_mddev;
1977
1978 if (!mddev->gendisk)
1979 return;
1980
1981 bi_rdev = bdev_get_integrity(rdev->bdev);
1982 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10001983
1984 if (!bi_mddev) /* nothing to do */
1985 return;
1986 if (rdev->raid_disk < 0) /* skip spares */
1987 return;
1988 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1989 rdev->bdev->bd_disk) >= 0)
1990 return;
1991 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1992 blk_integrity_unregister(mddev->gendisk);
1993}
1994EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001995
NeilBrownf72ffdd2014-09-30 14:23:59 +10001996static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001998 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001999 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002000 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002001 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Dan Williams11e2ede2008-04-30 00:52:32 -07002003 /* prevent duplicates */
2004 if (find_rdev(mddev, rdev->bdev->bd_dev))
2005 return -EEXIST;
2006
Andre Nolldd8ac332009-03-31 14:33:13 +11002007 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2008 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2009 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002010 if (mddev->pers) {
2011 /* Cannot change size, so fail
2012 * If mddev->level <= 0, then we don't care
2013 * about aligning sizes (e.g. linear)
2014 */
2015 if (mddev->level > 0)
2016 return -ENOSPC;
2017 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002018 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 /* Verify rdev->desc_nr is unique.
2022 * If it is -1, assign a free number, else
2023 * check number is not in use
2024 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002025 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (rdev->desc_nr < 0) {
2027 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002028 if (mddev->pers)
2029 choice = mddev->raid_disks;
2030 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 choice++;
2032 rdev->desc_nr = choice;
2033 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002034 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2035 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002039 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002040 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2041 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2042 mdname(mddev), mddev->max_disks);
2043 return -EBUSY;
2044 }
NeilBrown19133a42005-11-08 21:39:35 -08002045 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002046 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002047 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002050 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002051
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002052 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002053 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002054
Tejun Heo0762b8b2008-08-25 19:56:12 +09002055 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002056 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2057 /* failure here is OK */;
2058 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002059
NeilBrown4b809912008-07-21 17:05:25 +10002060 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002061 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002062
2063 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002064 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002067
2068 fail:
2069 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2070 b, mdname(mddev));
2071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
NeilBrown177a99b2008-02-06 01:39:56 -08002074static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002075{
NeilBrown3cb03002011-10-11 16:45:26 +11002076 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002077 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002078 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002079}
2080
NeilBrownf72ffdd2014-09-30 14:23:59 +10002081static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002084
Tejun Heo49731ba2011-01-14 18:43:57 +01002085 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002086 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2088 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002089 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002090 sysfs_put(rdev->sysfs_state);
2091 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002092 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002093 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002094 * writing to 'remove' to "dev/state". We also need
2095 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002096 */
NeilBrown4b809912008-07-21 17:05:25 +10002097 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002098 INIT_WORK(&rdev->del_work, md_delayed_delete);
2099 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002100 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
2103/*
2104 * prevent the device from being mounted, repartitioned or
2105 * otherwise reused by a RAID array (or any other kernel
2106 * subsystem), by bd_claiming the device.
2107 */
NeilBrown3cb03002011-10-11 16:45:26 +11002108static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 int err = 0;
2111 struct block_device *bdev;
2112 char b[BDEVNAME_SIZE];
2113
Tejun Heod4d77622010-11-13 11:55:18 +01002114 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002115 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (IS_ERR(bdev)) {
2117 printk(KERN_ERR "md: could not open %s.\n",
2118 __bdevname(dev, b));
2119 return PTR_ERR(bdev);
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 rdev->bdev = bdev;
2122 return err;
2123}
2124
NeilBrown3cb03002011-10-11 16:45:26 +11002125static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 struct block_device *bdev = rdev->bdev;
2128 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002129 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132void md_autodetect_dev(dev_t dev);
2133
NeilBrownf72ffdd2014-09-30 14:23:59 +10002134static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 printk(KERN_INFO "md: export_rdev(%s)\n",
2139 bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002140 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002142 if (test_bit(AutoDetected, &rdev->flags))
2143 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#endif
2145 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002146 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
NeilBrownf72ffdd2014-09-30 14:23:59 +10002149static void kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 unbind_rdev_from_array(rdev);
2152 export_rdev(rdev);
2153}
2154
NeilBrownfd01b882011-10-11 16:47:53 +11002155static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
NeilBrown0638bb02014-09-25 17:43:47 +10002157 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
NeilBrown0638bb02014-09-25 17:43:47 +10002159 while (!list_empty(&mddev->disks)) {
2160 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2161 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 kick_rdev_from_array(rdev);
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 mddev->raid_disks = 0;
2165 mddev->major_version = 0;
2166}
2167
NeilBrownf72ffdd2014-09-30 14:23:59 +10002168static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
NeilBrown42543762006-06-26 00:27:57 -07002170 /* Update each superblock (in-memory image), but
2171 * if we are allowed to, skip spares which already
2172 * have the right event counter, or have one earlier
2173 * (which would mean they aren't being marked as dirty
2174 * with the rest of the array)
2175 */
NeilBrown3cb03002011-10-11 16:45:26 +11002176 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002177 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002178 if (rdev->sb_events == mddev->events ||
2179 (nospares &&
2180 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002181 rdev->sb_events+1 == mddev->events)) {
2182 /* Don't update this superblock */
2183 rdev->sb_loaded = 2;
2184 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002185 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002186 rdev->sb_loaded = 1;
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189}
2190
NeilBrownf72ffdd2014-09-30 14:23:59 +10002191static void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
NeilBrown3cb03002011-10-11 16:45:26 +11002193 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002194 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002195 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002196 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrownd87f0642013-04-24 11:42:40 +10002198 if (mddev->ro) {
2199 if (force_change)
2200 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2201 return;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002204 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002205 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002206 if (rdev->raid_disk >= 0 &&
2207 mddev->delta_disks >= 0 &&
2208 !test_bit(In_sync, &rdev->flags) &&
2209 mddev->curr_resync_completed > rdev->recovery_offset)
2210 rdev->recovery_offset = mddev->curr_resync_completed;
2211
NeilBrownf72ffdd2014-09-30 14:23:59 +10002212 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002213 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002214 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002215 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002216 if (!mddev->external) {
NeilBrownd97a41dc2010-10-28 17:30:20 +11002217 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002218 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002219 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002220 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002221 md_ack_all_badblocks(&rdev->badblocks);
2222 md_error(mddev, rdev);
2223 }
2224 clear_bit(Blocked, &rdev->flags);
2225 clear_bit(BlockedBadBlocks, &rdev->flags);
2226 wake_up(&rdev->blocked_wait);
2227 }
2228 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002229 wake_up(&mddev->sb_wait);
2230 return;
2231 }
2232
NeilBrowna9701a32005-11-08 21:39:34 -08002233 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002234
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002235 mddev->utime = get_seconds();
2236
NeilBrown850b2b42006-10-03 01:15:46 -07002237 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2238 force_change = 1;
2239 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2240 /* just a clean<-> dirty transition, possibly leave spares alone,
2241 * though if events isn't the right even/odd, we will have to do
2242 * spares after all
2243 */
2244 nospares = 1;
2245 if (force_change)
2246 nospares = 0;
2247 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002248 /* If the array is degraded, then skipping spares is both
2249 * dangerous and fairly pointless.
2250 * Dangerous because a device that was removed from the array
2251 * might have a event_count that still looks up-to-date,
2252 * so it can be re-added without a resync.
2253 * Pointless because if there are any spares to skip,
2254 * then a recovery will happen and soon that array won't
2255 * be degraded any more and the spare can go back to sleep then.
2256 */
NeilBrown850b2b42006-10-03 01:15:46 -07002257 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002258
NeilBrown06d91a52005-06-21 17:17:12 -07002259 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002260
2261 /* If this is just a dirty<->clean transition, and the array is clean
2262 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002263 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002264 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002265 && mddev->can_decrease_events
2266 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002267 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002268 mddev->can_decrease_events = 0;
2269 } else {
NeilBrown42543762006-06-26 00:27:57 -07002270 /* otherwise we have to go forward and ... */
2271 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002272 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
NeilBrown403df472014-09-30 15:52:29 +10002275 /*
2276 * This 64-bit counter should never wrap.
2277 * Either we are in around ~1 trillion A.C., assuming
2278 * 1 reboot per second, or we have a bug...
2279 */
2280 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002281
NeilBrowndafb20f2012-03-19 12:46:39 +11002282 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002283 if (rdev->badblocks.changed)
2284 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002285 if (test_bit(Faulty, &rdev->flags))
2286 set_bit(FaultRecorded, &rdev->flags);
2287 }
NeilBrown2699b672011-07-28 11:31:47 +10002288
NeilBrowne6910632008-02-06 01:39:51 -08002289 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002290 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
NeilBrown36a4e1f2011-10-07 14:23:17 +11002292 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2293 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
NeilBrown4ad13662007-07-17 04:06:13 -07002295 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002296 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002298
NeilBrown42543762006-06-26 00:27:57 -07002299 if (rdev->sb_loaded != 1)
2300 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
NeilBrownf4667222013-12-09 12:04:56 +11002302 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002303 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002304 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002305 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002306 pr_debug("md: (write) %s's sb offset: %llu\n",
2307 bdevname(rdev->bdev, b),
2308 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002309 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002310 if (rdev->badblocks.size) {
2311 md_super_write(mddev, rdev,
2312 rdev->badblocks.sector,
2313 rdev->badblocks.size << 9,
2314 rdev->bb_page);
2315 rdev->badblocks.size = 0;
2316 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002317
NeilBrownf4667222013-12-09 12:04:56 +11002318 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002319 pr_debug("md: %s (skipping faulty)\n",
2320 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002321
NeilBrown7bfa19f2005-06-21 17:17:28 -07002322 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /* only need to write one superblock... */
2324 break;
2325 }
NeilBrowna9701a32005-11-08 21:39:34 -08002326 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002327 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002328
NeilBrowna9701a32005-11-08 21:39:34 -08002329 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002330 if (mddev->in_sync != sync_req ||
2331 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002332 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002333 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002334 goto repeat;
2335 }
NeilBrown850b2b42006-10-03 01:15:46 -07002336 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002337 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002338 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002339 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2340 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002341
NeilBrowndafb20f2012-03-19 12:46:39 +11002342 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002343 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2344 clear_bit(Blocked, &rdev->flags);
2345
2346 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002347 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002348 clear_bit(BlockedBadBlocks, &rdev->flags);
2349 wake_up(&rdev->blocked_wait);
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Andre Noll7f6ce762008-03-23 18:34:54 +01002353/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002354 * We want to accept with case. For this we use cmd_match.
2355 */
2356static int cmd_match(const char *cmd, const char *str)
2357{
2358 /* See if cmd, written into a sysfs file, matches
2359 * str. They must either be the same, or cmd can
2360 * have a trailing newline
2361 */
2362 while (*cmd && *str && *cmd == *str) {
2363 cmd++;
2364 str++;
2365 }
2366 if (*cmd == '\n')
2367 cmd++;
2368 if (*str || *cmd)
2369 return 0;
2370 return 1;
2371}
2372
NeilBrown86e6ffd2005-11-08 21:39:24 -08002373struct rdev_sysfs_entry {
2374 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002375 ssize_t (*show)(struct md_rdev *, char *);
2376 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002377};
2378
2379static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002380state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002381{
2382 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002383 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002384
NeilBrownde393cd2011-07-28 11:31:48 +10002385 if (test_bit(Faulty, &rdev->flags) ||
2386 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002387 len+= sprintf(page+len, "%sfaulty",sep);
2388 sep = ",";
2389 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002390 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002391 len += sprintf(page+len, "%sin_sync",sep);
2392 sep = ",";
2393 }
NeilBrownf6556752006-06-26 00:28:01 -07002394 if (test_bit(WriteMostly, &rdev->flags)) {
2395 len += sprintf(page+len, "%swrite_mostly",sep);
2396 sep = ",";
2397 }
NeilBrownde393cd2011-07-28 11:31:48 +10002398 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002399 (rdev->badblocks.unacked_exist
2400 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002401 len += sprintf(page+len, "%sblocked", sep);
2402 sep = ",";
2403 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002404 if (!test_bit(Faulty, &rdev->flags) &&
2405 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002406 len += sprintf(page+len, "%sspare", sep);
2407 sep = ",";
2408 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002409 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2410 len += sprintf(page+len, "%swrite_error", sep);
2411 sep = ",";
2412 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002413 if (test_bit(WantReplacement, &rdev->flags)) {
2414 len += sprintf(page+len, "%swant_replacement", sep);
2415 sep = ",";
2416 }
2417 if (test_bit(Replacement, &rdev->flags)) {
2418 len += sprintf(page+len, "%sreplacement", sep);
2419 sep = ",";
2420 }
2421
NeilBrown86e6ffd2005-11-08 21:39:24 -08002422 return len+sprintf(page+len, "\n");
2423}
2424
NeilBrown45dc2de2006-06-26 00:27:58 -07002425static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002426state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002427{
2428 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002429 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002430 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002431 * writemostly - sets write_mostly
2432 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002433 * blocked - sets the Blocked flags
2434 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002435 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002436 * -insync - clear Insync for a device with a slot assigned,
2437 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002438 * write_error - sets WriteErrorSeen
2439 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002440 */
2441 int err = -EINVAL;
2442 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2443 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002444 if (test_bit(Faulty, &rdev->flags))
2445 err = 0;
2446 else
2447 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002448 } else if (cmd_match(buf, "remove")) {
2449 if (rdev->raid_disk >= 0)
2450 err = -EBUSY;
2451 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002452 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002453 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002454 if (mddev->pers)
2455 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002456 md_new_event(mddev);
2457 err = 0;
2458 }
NeilBrownf6556752006-06-26 00:28:01 -07002459 } else if (cmd_match(buf, "writemostly")) {
2460 set_bit(WriteMostly, &rdev->flags);
2461 err = 0;
2462 } else if (cmd_match(buf, "-writemostly")) {
2463 clear_bit(WriteMostly, &rdev->flags);
2464 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002465 } else if (cmd_match(buf, "blocked")) {
2466 set_bit(Blocked, &rdev->flags);
2467 err = 0;
2468 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002469 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002470 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002471 /* metadata handler doesn't understand badblocks,
2472 * so we need to fail the device
2473 */
2474 md_error(rdev->mddev, rdev);
2475 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002476 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002477 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002478 wake_up(&rdev->blocked_wait);
2479 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2480 md_wakeup_thread(rdev->mddev->thread);
2481
2482 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002483 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2484 set_bit(In_sync, &rdev->flags);
2485 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002486 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
NeilBrowne1960f82014-09-30 15:24:25 +10002487 if (rdev->mddev->pers == NULL) {
2488 clear_bit(In_sync, &rdev->flags);
2489 rdev->saved_raid_disk = rdev->raid_disk;
2490 rdev->raid_disk = -1;
2491 err = 0;
2492 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002493 } else if (cmd_match(buf, "write_error")) {
2494 set_bit(WriteErrorSeen, &rdev->flags);
2495 err = 0;
2496 } else if (cmd_match(buf, "-write_error")) {
2497 clear_bit(WriteErrorSeen, &rdev->flags);
2498 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002499 } else if (cmd_match(buf, "want_replacement")) {
2500 /* Any non-spare device that is not a replacement can
2501 * become want_replacement at any time, but we then need to
2502 * check if recovery is needed.
2503 */
2504 if (rdev->raid_disk >= 0 &&
2505 !test_bit(Replacement, &rdev->flags))
2506 set_bit(WantReplacement, &rdev->flags);
2507 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2508 md_wakeup_thread(rdev->mddev->thread);
2509 err = 0;
2510 } else if (cmd_match(buf, "-want_replacement")) {
2511 /* Clearing 'want_replacement' is always allowed.
2512 * Once replacements starts it is too late though.
2513 */
2514 err = 0;
2515 clear_bit(WantReplacement, &rdev->flags);
2516 } else if (cmd_match(buf, "replacement")) {
2517 /* Can only set a device as a replacement when array has not
2518 * yet been started. Once running, replacement is automatic
2519 * from spares, or by assigning 'slot'.
2520 */
2521 if (rdev->mddev->pers)
2522 err = -EBUSY;
2523 else {
2524 set_bit(Replacement, &rdev->flags);
2525 err = 0;
2526 }
2527 } else if (cmd_match(buf, "-replacement")) {
2528 /* Similarly, can only clear Replacement before start */
2529 if (rdev->mddev->pers)
2530 err = -EBUSY;
2531 else {
2532 clear_bit(Replacement, &rdev->flags);
2533 err = 0;
2534 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002535 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002536 if (!err)
2537 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002538 return err ? err : len;
2539}
NeilBrown80ca3a42006-07-10 04:44:18 -07002540static struct rdev_sysfs_entry rdev_state =
2541__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002542
2543static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002544errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002545{
2546 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2547}
2548
2549static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002550errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002551{
2552 char *e;
2553 unsigned long n = simple_strtoul(buf, &e, 10);
2554 if (*buf && (*e == 0 || *e == '\n')) {
2555 atomic_set(&rdev->corrected_errors, n);
2556 return len;
2557 }
2558 return -EINVAL;
2559}
2560static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002561__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002562
NeilBrown014236d2006-01-06 00:20:55 -08002563static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002564slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002565{
2566 if (rdev->raid_disk < 0)
2567 return sprintf(page, "none\n");
2568 else
2569 return sprintf(page, "%d\n", rdev->raid_disk);
2570}
2571
2572static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002573slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002574{
2575 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002576 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002577 int slot = simple_strtoul(buf, &e, 10);
2578 if (strncmp(buf, "none", 4)==0)
2579 slot = -1;
2580 else if (e==buf || (*e && *e!= '\n'))
2581 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002582 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002583 /* Setting 'slot' on an active array requires also
2584 * updating the 'rd%d' link, and communicating
2585 * with the personality with ->hot_*_disk.
2586 * For now we only support removing
2587 * failed/spare devices. This normally happens automatically,
2588 * but not when the metadata is externally managed.
2589 */
NeilBrownc303da62008-02-06 01:39:51 -08002590 if (rdev->raid_disk == -1)
2591 return -EEXIST;
2592 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002593 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002594 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002595 clear_bit(Blocked, &rdev->flags);
2596 remove_and_add_spares(rdev->mddev, rdev);
2597 if (rdev->raid_disk >= 0)
2598 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002599 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2600 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002601 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002602 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002603 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002604 */
2605
2606 if (rdev->raid_disk != -1)
2607 return -EBUSY;
2608
NeilBrownc6751b22011-02-02 11:57:13 +11002609 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2610 return -EBUSY;
2611
Neil Brown6c2fce22008-06-28 08:31:31 +10002612 if (rdev->mddev->pers->hot_add_disk == NULL)
2613 return -EINVAL;
2614
NeilBrownba1b41b2011-01-14 09:14:34 +11002615 if (slot >= rdev->mddev->raid_disks &&
2616 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2617 return -ENOSPC;
2618
Neil Brown6c2fce22008-06-28 08:31:31 +10002619 rdev->raid_disk = slot;
2620 if (test_bit(In_sync, &rdev->flags))
2621 rdev->saved_raid_disk = slot;
2622 else
2623 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002624 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002625 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002626 err = rdev->mddev->pers->
2627 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002628 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002629 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002630 return err;
Neil Brown52664732008-06-28 08:31:44 +10002631 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002632 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002633 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002634 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002635 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002636 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002637 if (slot >= rdev->mddev->raid_disks &&
2638 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002639 return -ENOSPC;
2640 rdev->raid_disk = slot;
2641 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002642 clear_bit(Faulty, &rdev->flags);
2643 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002644 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002645 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002646 }
NeilBrown014236d2006-01-06 00:20:55 -08002647 return len;
2648}
2649
NeilBrown014236d2006-01-06 00:20:55 -08002650static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002651__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002652
NeilBrown93c8cad2006-01-06 00:20:56 -08002653static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002654offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002655{
Andrew Morton6961ece2006-01-06 00:20:59 -08002656 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002657}
2658
2659static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002660offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002661{
NeilBrownc6563a82012-05-21 09:27:00 +10002662 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002663 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002664 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002665 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002666 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002667 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002668 /* Must set offset before size, so overlap checks
2669 * can be sane */
2670 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002671 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002672 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002673 return len;
2674}
2675
2676static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002677__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002678
NeilBrownc6563a82012-05-21 09:27:00 +10002679static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2680{
2681 return sprintf(page, "%llu\n",
2682 (unsigned long long)rdev->new_data_offset);
2683}
2684
2685static ssize_t new_offset_store(struct md_rdev *rdev,
2686 const char *buf, size_t len)
2687{
2688 unsigned long long new_offset;
2689 struct mddev *mddev = rdev->mddev;
2690
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002691 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002692 return -EINVAL;
2693
NeilBrownf851b602014-12-11 10:02:10 +11002694 if (mddev->sync_thread ||
2695 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10002696 return -EBUSY;
2697 if (new_offset == rdev->data_offset)
2698 /* reset is always permitted */
2699 ;
2700 else if (new_offset > rdev->data_offset) {
2701 /* must not push array size beyond rdev_sectors */
2702 if (new_offset - rdev->data_offset
2703 + mddev->dev_sectors > rdev->sectors)
2704 return -E2BIG;
2705 }
2706 /* Metadata worries about other space details. */
2707
2708 /* decreasing the offset is inconsistent with a backwards
2709 * reshape.
2710 */
2711 if (new_offset < rdev->data_offset &&
2712 mddev->reshape_backwards)
2713 return -EINVAL;
2714 /* Increasing offset is inconsistent with forwards
2715 * reshape. reshape_direction should be set to
2716 * 'backwards' first.
2717 */
2718 if (new_offset > rdev->data_offset &&
2719 !mddev->reshape_backwards)
2720 return -EINVAL;
2721
2722 if (mddev->pers && mddev->persistent &&
2723 !super_types[mddev->major_version]
2724 .allow_new_offset(rdev, new_offset))
2725 return -E2BIG;
2726 rdev->new_data_offset = new_offset;
2727 if (new_offset > rdev->data_offset)
2728 mddev->reshape_backwards = 1;
2729 else if (new_offset < rdev->data_offset)
2730 mddev->reshape_backwards = 0;
2731
2732 return len;
2733}
2734static struct rdev_sysfs_entry rdev_new_offset =
2735__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2736
NeilBrown83303b62006-01-06 00:21:06 -08002737static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002738rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002739{
Andre Nolldd8ac332009-03-31 14:33:13 +11002740 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002741}
2742
NeilBrownc5d79ad2008-02-06 01:39:54 -08002743static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2744{
2745 /* check if two start/length pairs overlap */
2746 if (s1+l1 <= s2)
2747 return 0;
2748 if (s2+l2 <= s1)
2749 return 0;
2750 return 1;
2751}
2752
Dan Williamsb522adc2009-03-31 15:00:31 +11002753static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2754{
2755 unsigned long long blocks;
2756 sector_t new;
2757
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002758 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002759 return -EINVAL;
2760
2761 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2762 return -EINVAL; /* sector conversion overflow */
2763
2764 new = blocks * 2;
2765 if (new != blocks * 2)
2766 return -EINVAL; /* unsigned long long to sector_t overflow */
2767
2768 *sectors = new;
2769 return 0;
2770}
2771
NeilBrown83303b62006-01-06 00:21:06 -08002772static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002773rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002774{
NeilBrownfd01b882011-10-11 16:47:53 +11002775 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002776 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002777 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002778
Dan Williamsb522adc2009-03-31 15:00:31 +11002779 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002780 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002781 if (rdev->data_offset != rdev->new_data_offset)
2782 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002783 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002784 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002785 sectors = super_types[my_mddev->major_version].
2786 rdev_size_change(rdev, sectors);
2787 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002788 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002789 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002790 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002791 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002792 if (!my_mddev->pers->resize)
2793 /* Cannot change size for RAID0 or Linear etc */
2794 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002795 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002796 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002797 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002798
Andre Nolldd8ac332009-03-31 14:33:13 +11002799 rdev->sectors = sectors;
2800 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10002801 /* Need to check that all other rdevs with the same
2802 * ->bdev do not overlap. 'rcu' is sufficient to walk
2803 * the rdev lists safely.
2804 * This check does not provide a hard guarantee, it
2805 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08002806 */
NeilBrownfd01b882011-10-11 16:47:53 +11002807 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002808 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002809 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002810
NeilBrown8b1afc32014-09-29 15:33:20 +10002811 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08002812 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002813 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002814
NeilBrowndafb20f2012-03-19 12:46:39 +11002815 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002816 if (rdev->bdev == rdev2->bdev &&
2817 rdev != rdev2 &&
2818 overlaps(rdev->data_offset, rdev->sectors,
2819 rdev2->data_offset,
2820 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002821 overlap = 1;
2822 break;
2823 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002824 if (overlap) {
2825 mddev_put(mddev);
2826 break;
2827 }
2828 }
NeilBrown8b1afc32014-09-29 15:33:20 +10002829 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08002830 if (overlap) {
2831 /* Someone else could have slipped in a size
2832 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002834 * safe, and trust userspace not to race with
2835 * itself
2836 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002837 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 return -EBUSY;
2839 }
2840 }
NeilBrown83303b62006-01-06 00:21:06 -08002841 return len;
2842}
2843
2844static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002845__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002846
NeilBrown3cb03002011-10-11 16:45:26 +11002847static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002848{
2849 unsigned long long recovery_start = rdev->recovery_offset;
2850
2851 if (test_bit(In_sync, &rdev->flags) ||
2852 recovery_start == MaxSector)
2853 return sprintf(page, "none\n");
2854
2855 return sprintf(page, "%llu\n", recovery_start);
2856}
2857
NeilBrown3cb03002011-10-11 16:45:26 +11002858static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002859{
2860 unsigned long long recovery_start;
2861
2862 if (cmd_match(buf, "none"))
2863 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002864 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07002865 return -EINVAL;
2866
2867 if (rdev->mddev->pers &&
2868 rdev->raid_disk >= 0)
2869 return -EBUSY;
2870
2871 rdev->recovery_offset = recovery_start;
2872 if (recovery_start == MaxSector)
2873 set_bit(In_sync, &rdev->flags);
2874 else
2875 clear_bit(In_sync, &rdev->flags);
2876 return len;
2877}
2878
2879static struct rdev_sysfs_entry rdev_recovery_start =
2880__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2881
NeilBrown16c791a2011-07-28 11:31:47 +10002882static ssize_t
2883badblocks_show(struct badblocks *bb, char *page, int unack);
2884static ssize_t
2885badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2886
NeilBrown3cb03002011-10-11 16:45:26 +11002887static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002888{
2889 return badblocks_show(&rdev->badblocks, page, 0);
2890}
NeilBrown3cb03002011-10-11 16:45:26 +11002891static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002892{
NeilBrownde393cd2011-07-28 11:31:48 +10002893 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2894 /* Maybe that ack was all we needed */
2895 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2896 wake_up(&rdev->blocked_wait);
2897 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002898}
2899static struct rdev_sysfs_entry rdev_bad_blocks =
2900__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2901
NeilBrown3cb03002011-10-11 16:45:26 +11002902static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002903{
2904 return badblocks_show(&rdev->badblocks, page, 1);
2905}
NeilBrown3cb03002011-10-11 16:45:26 +11002906static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002907{
2908 return badblocks_store(&rdev->badblocks, page, len, 1);
2909}
2910static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2911__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2912
NeilBrown86e6ffd2005-11-08 21:39:24 -08002913static struct attribute *rdev_default_attrs[] = {
2914 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002915 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002916 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002917 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10002918 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002919 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002920 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002921 &rdev_bad_blocks.attr,
2922 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002923 NULL,
2924};
2925static ssize_t
2926rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2927{
2928 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002929 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002930 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002931 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002932
2933 if (!entry->show)
2934 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002935
2936 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2937 if (!rv) {
2938 if (rdev->mddev == NULL)
2939 rv = -EBUSY;
2940 else
2941 rv = entry->show(rdev, page);
2942 mddev_unlock(mddev);
2943 }
2944 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002945}
2946
2947static ssize_t
2948rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2949 const char *page, size_t length)
2950{
2951 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002952 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002953 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002954 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002955
2956 if (!entry->store)
2957 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002958 if (!capable(CAP_SYS_ADMIN))
2959 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002960 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002961 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002962 if (rdev->mddev == NULL)
2963 rv = -EBUSY;
2964 else
2965 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002966 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002967 }
2968 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002969}
2970
2971static void rdev_free(struct kobject *ko)
2972{
NeilBrown3cb03002011-10-11 16:45:26 +11002973 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002974 kfree(rdev);
2975}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002976static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002977 .show = rdev_attr_show,
2978 .store = rdev_attr_store,
2979};
2980static struct kobj_type rdev_ktype = {
2981 .release = rdev_free,
2982 .sysfs_ops = &rdev_sysfs_ops,
2983 .default_attrs = rdev_default_attrs,
2984};
2985
NeilBrown3cb03002011-10-11 16:45:26 +11002986int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10002987{
2988 rdev->desc_nr = -1;
2989 rdev->saved_raid_disk = -1;
2990 rdev->raid_disk = -1;
2991 rdev->flags = 0;
2992 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10002993 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002994 rdev->sb_events = 0;
2995 rdev->last_read_error.tv_sec = 0;
2996 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002997 rdev->sb_loaded = 0;
2998 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002999 atomic_set(&rdev->nr_pending, 0);
3000 atomic_set(&rdev->read_errors, 0);
3001 atomic_set(&rdev->corrected_errors, 0);
3002
3003 INIT_LIST_HEAD(&rdev->same_set);
3004 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003005
3006 /* Add space to store bad block list.
3007 * This reserves the space even on arrays where it cannot
3008 * be used - I wonder if that matters
3009 */
3010 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003011 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003012 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3013 seqlock_init(&rdev->badblocks.lock);
3014 if (rdev->badblocks.page == NULL)
3015 return -ENOMEM;
3016
3017 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003018}
3019EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020/*
3021 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3022 *
3023 * mark the device faulty if:
3024 *
3025 * - the device is nonexistent (zero size)
3026 * - the device has no valid superblock
3027 *
3028 * a faulty rdev _never_ has rdev->sb set.
3029 */
NeilBrown3cb03002011-10-11 16:45:26 +11003030static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 char b[BDEVNAME_SIZE];
3033 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003034 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 sector_t size;
3036
NeilBrown9ffae0c2006-01-06 00:20:32 -08003037 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (!rdev) {
3039 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3040 return ERR_PTR(-ENOMEM);
3041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
NeilBrown2230dfe2011-07-28 11:31:46 +10003043 err = md_rdev_init(rdev);
3044 if (err)
3045 goto abort_free;
3046 err = alloc_disk_sb(rdev);
3047 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto abort_free;
3049
NeilBrownc5d79ad2008-02-06 01:39:54 -08003050 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (err)
3052 goto abort_free;
3053
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003054 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003055
Mike Snitzer77304d22010-11-08 14:39:12 +01003056 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (!size) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003058 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 "md: %s has zero or unknown size, marking faulty!\n",
3060 bdevname(rdev->bdev,b));
3061 err = -EINVAL;
3062 goto abort_free;
3063 }
3064
3065 if (super_format >= 0) {
3066 err = super_types[super_format].
3067 load_super(rdev, NULL, super_minor);
3068 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003069 printk(KERN_WARNING
3070 "md: %s does not have a valid v%d.%d "
3071 "superblock, not importing!\n",
3072 bdevname(rdev->bdev,b),
3073 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 goto abort_free;
3075 }
3076 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10003077 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 "md: could not read %s's sb, not importing!\n",
3079 bdevname(rdev->bdev,b));
3080 goto abort_free;
3081 }
3082 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return rdev;
3085
3086abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003087 if (rdev->bdev)
3088 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003089 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 kfree(rdev);
3091 return ERR_PTR(err);
3092}
3093
3094/*
3095 * Check a full RAID array for plausibility
3096 */
3097
NeilBrownf72ffdd2014-09-30 14:23:59 +10003098static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
3100 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003101 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 char b[BDEVNAME_SIZE];
3103
3104 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003105 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 switch (super_types[mddev->major_version].
3107 load_super(rdev, freshest, mddev->minor_version)) {
3108 case 1:
3109 freshest = rdev;
3110 break;
3111 case 0:
3112 break;
3113 default:
3114 printk( KERN_ERR \
3115 "md: fatal superblock inconsistency in %s"
NeilBrownf72ffdd2014-09-30 14:23:59 +10003116 " -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 bdevname(rdev->bdev,b));
3118 kick_rdev_from_array(rdev);
3119 }
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 super_types[mddev->major_version].
3122 validate_super(mddev, freshest);
3123
3124 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003125 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003126 if (mddev->max_disks &&
3127 (rdev->desc_nr >= mddev->max_disks ||
3128 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003129 printk(KERN_WARNING
3130 "md: %s: %s: only %d devices permitted\n",
3131 mdname(mddev), bdevname(rdev->bdev, b),
3132 mddev->max_disks);
3133 kick_rdev_from_array(rdev);
3134 continue;
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (rdev != freshest)
3137 if (super_types[mddev->major_version].
3138 validate_super(mddev, rdev)) {
3139 printk(KERN_WARNING "md: kicking non-fresh %s"
3140 " from array!\n",
3141 bdevname(rdev->bdev,b));
3142 kick_rdev_from_array(rdev);
3143 continue;
3144 }
3145 if (mddev->level == LEVEL_MULTIPATH) {
3146 rdev->desc_nr = i++;
3147 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003148 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003149 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003150 rdev->raid_disk = -1;
3151 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
NeilBrown72e02072009-12-14 12:49:55 +11003156/* Read a fixed-point number.
3157 * Numbers in sysfs attributes should be in "standard" units where
3158 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003159 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003160 * milliseconds or jiffies.
3161 * This function takes a decimal number with a possible fractional
3162 * component, and produces an integer which is the result of
3163 * multiplying that number by 10^'scale'.
3164 * all without any floating-point arithmetic.
3165 */
3166int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3167{
3168 unsigned long result = 0;
3169 long decimals = -1;
3170 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3171 if (*cp == '.')
3172 decimals = 0;
3173 else if (decimals < scale) {
3174 unsigned int value;
3175 value = *cp - '0';
3176 result = result * 10 + value;
3177 if (decimals >= 0)
3178 decimals++;
3179 }
3180 cp++;
3181 }
3182 if (*cp == '\n')
3183 cp++;
3184 if (*cp)
3185 return -EINVAL;
3186 if (decimals < 0)
3187 decimals = 0;
3188 while (decimals < scale) {
3189 result *= 10;
3190 decimals ++;
3191 }
3192 *res = result;
3193 return 0;
3194}
3195
NeilBrown19052c02008-08-05 15:54:13 +10003196static void md_safemode_timeout(unsigned long data);
3197
NeilBrowneae17012005-11-08 21:39:23 -08003198static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003199safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003200{
3201 int msec = (mddev->safemode_delay*1000)/HZ;
3202 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3203}
3204static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003205safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003206{
NeilBrown16f17b32006-06-26 00:27:37 -07003207 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003208
NeilBrown72e02072009-12-14 12:49:55 +11003209 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003210 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003211 if (msec == 0)
3212 mddev->safemode_delay = 0;
3213 else {
NeilBrown19052c02008-08-05 15:54:13 +10003214 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003215 mddev->safemode_delay = (msec*HZ)/1000;
3216 if (mddev->safemode_delay == 0)
3217 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003218 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003219 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003220 }
3221 return len;
3222}
3223static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003224__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003225
3226static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003227level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003228{
NeilBrown84fc4b52011-10-11 16:49:58 +11003229 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003230 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003231 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003232 else if (mddev->clevel[0])
3233 return sprintf(page, "%s\n", mddev->clevel);
3234 else if (mddev->level != LEVEL_NONE)
3235 return sprintf(page, "%d\n", mddev->level);
3236 else
3237 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003238}
3239
NeilBrownd9d166c2006-01-06 00:20:51 -08003240static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003241level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003242{
Dan Williamsf2859af2010-05-02 10:04:16 -07003243 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003244 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003245 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003246 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003247 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003248 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003249
3250 if (mddev->pers == NULL) {
3251 if (len == 0)
3252 return 0;
3253 if (len >= sizeof(mddev->clevel))
3254 return -ENOSPC;
3255 strncpy(mddev->clevel, buf, len);
3256 if (mddev->clevel[len-1] == '\n')
3257 len--;
3258 mddev->clevel[len] = 0;
3259 mddev->level = LEVEL_NONE;
3260 return rv;
3261 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003262 if (mddev->ro)
3263 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003264
3265 /* request to change the personality. Need to ensure:
3266 * - array is not engaged in resync/recovery/reshape
3267 * - old personality can be suspended
3268 * - new personality will access other array.
3269 */
3270
NeilBrownbb4f1e92010-08-08 21:18:03 +10003271 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003272 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003273 mddev->reshape_position != MaxSector ||
3274 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003275 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003276
3277 if (!mddev->pers->quiesce) {
3278 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3279 mdname(mddev), mddev->pers->name);
3280 return -EINVAL;
3281 }
3282
3283 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003284 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003285 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003286 strncpy(clevel, buf, len);
3287 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003288 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003289 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003290 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003291 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003292
Dan Williamsf2859af2010-05-02 10:04:16 -07003293 if (request_module("md-%s", clevel) != 0)
3294 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003295 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003296 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003297 if (!pers || !try_module_get(pers->owner)) {
3298 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003299 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003300 return -EINVAL;
3301 }
3302 spin_unlock(&pers_lock);
3303
3304 if (pers == mddev->pers) {
3305 /* Nothing to do! */
3306 module_put(pers->owner);
3307 return rv;
3308 }
3309 if (!pers->takeover) {
3310 module_put(pers->owner);
3311 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003312 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003313 return -EINVAL;
3314 }
3315
NeilBrowndafb20f2012-03-19 12:46:39 +11003316 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003317 rdev->new_raid_disk = rdev->raid_disk;
3318
NeilBrown245f46c2009-03-31 14:39:39 +11003319 /* ->takeover must set new_* and/or delta_disks
3320 * if it succeeds, and may set them when it fails.
3321 */
3322 priv = pers->takeover(mddev);
3323 if (IS_ERR(priv)) {
3324 mddev->new_level = mddev->level;
3325 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003326 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003327 mddev->raid_disks -= mddev->delta_disks;
3328 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003329 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003330 module_put(pers->owner);
3331 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003332 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003333 return PTR_ERR(priv);
3334 }
3335
3336 /* Looks like we have a winner */
3337 mddev_suspend(mddev);
3338 mddev->pers->stop(mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003339
NeilBrowna64c8762010-04-14 17:15:37 +10003340 if (mddev->pers->sync_request == NULL &&
3341 pers->sync_request != NULL) {
3342 /* need to add the md_redundancy_group */
3343 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3344 printk(KERN_WARNING
3345 "md: cannot register extra attributes for %s\n",
3346 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003347 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrownf72ffdd2014-09-30 14:23:59 +10003348 }
NeilBrowna64c8762010-04-14 17:15:37 +10003349 if (mddev->pers->sync_request != NULL &&
3350 pers->sync_request == NULL) {
3351 /* need to remove the md_redundancy_group */
3352 if (mddev->to_remove == NULL)
3353 mddev->to_remove = &md_redundancy_group;
3354 }
3355
Trela Maciej54071b32010-03-08 16:02:42 +11003356 if (mddev->pers->sync_request == NULL &&
3357 mddev->external) {
3358 /* We are converting from a no-redundancy array
3359 * to a redundancy array and metadata is managed
3360 * externally so we need to be sure that writes
3361 * won't block due to a need to transition
3362 * clean->dirty
3363 * until external management is started.
3364 */
3365 mddev->in_sync = 0;
3366 mddev->safemode_delay = 0;
3367 mddev->safemode = 0;
3368 }
3369
NeilBrowndafb20f2012-03-19 12:46:39 +11003370 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003371 if (rdev->raid_disk < 0)
3372 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003373 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003374 rdev->new_raid_disk = -1;
3375 if (rdev->new_raid_disk == rdev->raid_disk)
3376 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003377 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003378 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003379 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003380 if (rdev->raid_disk < 0)
3381 continue;
3382 if (rdev->new_raid_disk == rdev->raid_disk)
3383 continue;
3384 rdev->raid_disk = rdev->new_raid_disk;
3385 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003386 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003387 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003388 if (sysfs_link_rdev(mddev, rdev))
3389 printk(KERN_WARNING "md: cannot register rd%d"
3390 " for %s after level change\n",
3391 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003392 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003393 }
3394
3395 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003396 mddev->pers = pers;
3397 mddev->private = priv;
3398 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3399 mddev->level = mddev->new_level;
3400 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003401 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003402 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003403 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003404 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003405 if (mddev->pers->sync_request == NULL) {
3406 /* this is now an array without redundancy, so
3407 * it must always be in_sync
3408 */
3409 mddev->in_sync = 1;
3410 del_timer_sync(&mddev->safemode_timer);
3411 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003412 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003413 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003414 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003415 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003416 if (!mddev->thread)
3417 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003418 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003419 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003420 return rv;
3421}
3422
3423static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003424__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003425
NeilBrownd4dbd022006-06-26 00:27:59 -07003426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003427layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003428{
3429 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003430 if (mddev->reshape_position != MaxSector &&
3431 mddev->layout != mddev->new_layout)
3432 return sprintf(page, "%d (%d)\n",
3433 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003434 return sprintf(page, "%d\n", mddev->layout);
3435}
3436
3437static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003438layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003439{
3440 char *e;
3441 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003442
3443 if (!*buf || (*e && *e != '\n'))
3444 return -EINVAL;
3445
NeilBrownb3546032009-03-31 14:56:41 +11003446 if (mddev->pers) {
3447 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003448 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003449 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003450 if (mddev->ro)
3451 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003452 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003453 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003454 if (err) {
3455 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003456 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003457 }
NeilBrownb3546032009-03-31 14:56:41 +11003458 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003459 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003460 if (mddev->reshape_position == MaxSector)
3461 mddev->layout = n;
3462 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003463 return len;
3464}
3465static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003466__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003467
NeilBrowneae17012005-11-08 21:39:23 -08003468static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003469raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003470{
NeilBrownbb636542005-11-08 21:39:45 -08003471 if (mddev->raid_disks == 0)
3472 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003473 if (mddev->reshape_position != MaxSector &&
3474 mddev->delta_disks != 0)
3475 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3476 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003477 return sprintf(page, "%d\n", mddev->raid_disks);
3478}
3479
NeilBrownfd01b882011-10-11 16:47:53 +11003480static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003481
3482static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003483raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003484{
NeilBrownda943b992006-01-06 00:20:54 -08003485 char *e;
3486 int rv = 0;
3487 unsigned long n = simple_strtoul(buf, &e, 10);
3488
3489 if (!*buf || (*e && *e != '\n'))
3490 return -EINVAL;
3491
3492 if (mddev->pers)
3493 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003494 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003495 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003496 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003497
3498 rdev_for_each(rdev, mddev) {
3499 if (olddisks < n &&
3500 rdev->data_offset < rdev->new_data_offset)
3501 return -EINVAL;
3502 if (olddisks > n &&
3503 rdev->data_offset > rdev->new_data_offset)
3504 return -EINVAL;
3505 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003506 mddev->delta_disks = n - olddisks;
3507 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003508 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003509 } else
NeilBrownda943b992006-01-06 00:20:54 -08003510 mddev->raid_disks = n;
3511 return rv ? rv : len;
3512}
3513static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003514__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003515
NeilBrown24dd4692005-11-08 21:39:26 -08003516static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003517chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003518{
NeilBrown08a02ec2007-05-09 02:35:38 -07003519 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003520 mddev->chunk_sectors != mddev->new_chunk_sectors)
3521 return sprintf(page, "%d (%d)\n",
3522 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003523 mddev->chunk_sectors << 9);
3524 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003525}
3526
3527static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003528chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003529{
NeilBrown3b343802006-01-06 00:20:47 -08003530 char *e;
3531 unsigned long n = simple_strtoul(buf, &e, 10);
3532
NeilBrown3b343802006-01-06 00:20:47 -08003533 if (!*buf || (*e && *e != '\n'))
3534 return -EINVAL;
3535
NeilBrownb3546032009-03-31 14:56:41 +11003536 if (mddev->pers) {
3537 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003538 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003539 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003540 if (mddev->ro)
3541 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003542 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003543 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003544 if (err) {
3545 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003546 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003547 }
NeilBrownb3546032009-03-31 14:56:41 +11003548 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003549 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003550 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003551 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003552 }
NeilBrown3b343802006-01-06 00:20:47 -08003553 return len;
3554}
3555static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003556__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003557
NeilBrowna94213b2006-06-26 00:28:00 -07003558static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003559resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003560{
NeilBrownd1a7c502009-03-31 15:24:32 +11003561 if (mddev->recovery_cp == MaxSector)
3562 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003563 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3564}
3565
3566static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003567resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003568{
NeilBrowna94213b2006-06-26 00:28:00 -07003569 char *e;
3570 unsigned long long n = simple_strtoull(buf, &e, 10);
3571
NeilBrownb0986362011-05-11 15:52:21 +10003572 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003573 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003574 if (cmd_match(buf, "none"))
3575 n = MaxSector;
3576 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003577 return -EINVAL;
3578
3579 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003580 if (mddev->pers)
3581 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003582 return len;
3583}
3584static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003585__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003586
NeilBrown9e653b62006-06-26 00:27:58 -07003587/*
3588 * The array state can be:
3589 *
3590 * clear
3591 * No devices, no size, no level
3592 * Equivalent to STOP_ARRAY ioctl
3593 * inactive
3594 * May have some settings, but array is not active
3595 * all IO results in error
3596 * When written, doesn't tear down array, but just stops it
3597 * suspended (not supported yet)
3598 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003599 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003600 * readonly
3601 * no resync can happen. no superblocks get written.
3602 * write requests fail
3603 * read-auto
3604 * like readonly, but behaves like 'clean' on a write request.
3605 *
3606 * clean - no pending writes, but otherwise active.
3607 * When written to inactive array, starts without resync
3608 * If a write request arrives then
3609 * if metadata is known, mark 'dirty' and switch to 'active'.
3610 * if not known, block and switch to write-pending
3611 * If written to an active array that has pending writes, then fails.
3612 * active
3613 * fully active: IO and resync can be happening.
3614 * When written to inactive array, starts with resync
3615 *
3616 * write-pending
3617 * clean, but writes are blocked waiting for 'active' to be written.
3618 *
3619 * active-idle
3620 * like active, but no writes have been seen for a while (100msec).
3621 *
3622 */
3623enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3624 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003625static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003626 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3627 "write-pending", "active-idle", NULL };
3628
3629static int match_word(const char *word, char **list)
3630{
3631 int n;
3632 for (n=0; list[n]; n++)
3633 if (cmd_match(word, list[n]))
3634 break;
3635 return n;
3636}
3637
3638static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003639array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003640{
3641 enum array_state st = inactive;
3642
3643 if (mddev->pers)
3644 switch(mddev->ro) {
3645 case 1:
3646 st = readonly;
3647 break;
3648 case 2:
3649 st = read_auto;
3650 break;
3651 case 0:
3652 if (mddev->in_sync)
3653 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003654 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003655 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003656 else if (mddev->safemode)
3657 st = active_idle;
3658 else
3659 st = active;
3660 }
3661 else {
3662 if (list_empty(&mddev->disks) &&
3663 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003664 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003665 st = clear;
3666 else
3667 st = inactive;
3668 }
3669 return sprintf(page, "%s\n", array_states[st]);
3670}
3671
NeilBrownf72ffdd2014-09-30 14:23:59 +10003672static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
3673static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
3674static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11003675static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003676
3677static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003678array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003679{
3680 int err = -EINVAL;
3681 enum array_state st = match_word(buf, array_states);
3682 switch(st) {
3683 case bad_word:
3684 break;
3685 case clear:
3686 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003687 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003688 break;
3689 case inactive:
3690 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003691 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003692 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003693 else
NeilBrowne6910632008-02-06 01:39:51 -08003694 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003695 break;
3696 case suspended:
3697 break; /* not supported yet */
3698 case readonly:
3699 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003700 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003701 else {
3702 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003703 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003704 err = do_md_run(mddev);
3705 }
3706 break;
3707 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003708 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003709 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003710 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003711 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003712 err = restart_array(mddev);
3713 if (err == 0) {
3714 mddev->ro = 2;
3715 set_disk_ro(mddev->gendisk, 0);
3716 }
NeilBrown9e653b62006-06-26 00:27:58 -07003717 } else {
3718 mddev->ro = 2;
3719 err = do_md_run(mddev);
3720 }
3721 break;
3722 case clean:
3723 if (mddev->pers) {
3724 restart_array(mddev);
3725 spin_lock_irq(&mddev->write_lock);
3726 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003727 if (mddev->in_sync == 0) {
3728 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003729 if (mddev->safemode == 1)
3730 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003731 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003732 }
3733 err = 0;
3734 } else
3735 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003736 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003737 } else
3738 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003739 break;
3740 case active:
3741 if (mddev->pers) {
3742 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003743 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003744 wake_up(&mddev->sb_wait);
3745 err = 0;
3746 } else {
3747 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003748 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003749 err = do_md_run(mddev);
3750 }
3751 break;
3752 case write_pending:
3753 case active_idle:
3754 /* these cannot be set */
3755 break;
3756 }
3757 if (err)
3758 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003759 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003760 if (mddev->hold_active == UNTIL_IOCTL)
3761 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003762 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003763 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003764 }
NeilBrown9e653b62006-06-26 00:27:58 -07003765}
NeilBrown80ca3a42006-07-10 04:44:18 -07003766static struct md_sysfs_entry md_array_state =
3767__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003768
NeilBrown6d7ff732006-01-06 00:21:16 -08003769static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003770max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003771 return sprintf(page, "%d\n",
3772 atomic_read(&mddev->max_corr_read_errors));
3773}
3774
3775static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003776max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003777{
3778 char *e;
3779 unsigned long n = simple_strtoul(buf, &e, 10);
3780
3781 if (*buf && (*e == 0 || *e == '\n')) {
3782 atomic_set(&mddev->max_corr_read_errors, n);
3783 return len;
3784 }
3785 return -EINVAL;
3786}
3787
3788static struct md_sysfs_entry max_corr_read_errors =
3789__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3790 max_corrected_read_errors_store);
3791
3792static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003793null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003794{
3795 return -EINVAL;
3796}
3797
3798static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003799new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003800{
3801 /* buf must be %d:%d\n? giving major and minor numbers */
3802 /* The new device is added to the array.
3803 * If the array has a persistent superblock, we read the
3804 * superblock to initialise info and check validity.
3805 * Otherwise, only checking done is that in bind_rdev_to_array,
3806 * which mainly checks size.
3807 */
3808 char *e;
3809 int major = simple_strtoul(buf, &e, 10);
3810 int minor;
3811 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003812 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003813 int err;
3814
3815 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3816 return -EINVAL;
3817 minor = simple_strtoul(e+1, &e, 10);
3818 if (*e && *e != '\n')
3819 return -EINVAL;
3820 dev = MKDEV(major, minor);
3821 if (major != MAJOR(dev) ||
3822 minor != MINOR(dev))
3823 return -EOVERFLOW;
3824
NeilBrown6d7ff732006-01-06 00:21:16 -08003825 if (mddev->persistent) {
3826 rdev = md_import_device(dev, mddev->major_version,
3827 mddev->minor_version);
3828 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003829 struct md_rdev *rdev0
3830 = list_entry(mddev->disks.next,
3831 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003832 err = super_types[mddev->major_version]
3833 .load_super(rdev, rdev0, mddev->minor_version);
3834 if (err < 0)
3835 goto out;
3836 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003837 } else if (mddev->external)
3838 rdev = md_import_device(dev, -2, -1);
3839 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003840 rdev = md_import_device(dev, -1, -1);
3841
3842 if (IS_ERR(rdev))
3843 return PTR_ERR(rdev);
3844 err = bind_rdev_to_array(rdev, mddev);
3845 out:
3846 if (err)
3847 export_rdev(rdev);
3848 return err ? err : len;
3849}
3850
3851static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003852__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003853
3854static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003855bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003856{
3857 char *end;
3858 unsigned long chunk, end_chunk;
3859
3860 if (!mddev->bitmap)
3861 goto out;
3862 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3863 while (*buf) {
3864 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3865 if (buf == end) break;
3866 if (*end == '-') { /* range */
3867 buf = end + 1;
3868 end_chunk = simple_strtoul(buf, &end, 0);
3869 if (buf == end) break;
3870 }
3871 if (*end && !isspace(*end)) break;
3872 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003873 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003874 }
3875 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3876out:
3877 return len;
3878}
3879
3880static struct md_sysfs_entry md_bitmap =
3881__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3882
3883static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003884size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003885{
Andre Noll58c0fed2009-03-31 14:33:13 +11003886 return sprintf(page, "%llu\n",
3887 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003888}
3889
NeilBrownfd01b882011-10-11 16:47:53 +11003890static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003891
3892static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003893size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003894{
3895 /* If array is inactive, we can reduce the component size, but
3896 * not increase it (except from 0).
3897 * If array is active, we can try an on-line resize
3898 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003899 sector_t sectors;
3900 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003901
Andre Noll58c0fed2009-03-31 14:33:13 +11003902 if (err < 0)
3903 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003904 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003905 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003906 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003907 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003908 if (mddev->dev_sectors == 0 ||
3909 mddev->dev_sectors > sectors)
3910 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003911 else
3912 err = -ENOSPC;
3913 }
3914 return err ? err : len;
3915}
3916
3917static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003918__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003919
Masanari Iida83f0d772012-10-30 00:18:08 +09003920/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08003921 * This is one of
3922 * 'none' for arrays with no metadata (good luck...)
3923 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003924 * or N.M for internally known formats
3925 */
3926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003927metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003928{
3929 if (mddev->persistent)
3930 return sprintf(page, "%d.%d\n",
3931 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003932 else if (mddev->external)
3933 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003934 else
3935 return sprintf(page, "none\n");
3936}
3937
3938static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003939metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003940{
3941 int major, minor;
3942 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003943 /* Changing the details of 'external' metadata is
3944 * always permitted. Otherwise there must be
3945 * no devices attached to the array.
3946 */
3947 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3948 ;
3949 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003950 return -EBUSY;
3951
3952 if (cmd_match(buf, "none")) {
3953 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003954 mddev->external = 0;
3955 mddev->major_version = 0;
3956 mddev->minor_version = 90;
3957 return len;
3958 }
3959 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003960 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003961 if (namelen >= sizeof(mddev->metadata_type))
3962 namelen = sizeof(mddev->metadata_type)-1;
3963 strncpy(mddev->metadata_type, buf+9, namelen);
3964 mddev->metadata_type[namelen] = 0;
3965 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3966 mddev->metadata_type[--namelen] = 0;
3967 mddev->persistent = 0;
3968 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003969 mddev->major_version = 0;
3970 mddev->minor_version = 90;
3971 return len;
3972 }
3973 major = simple_strtoul(buf, &e, 10);
3974 if (e==buf || *e != '.')
3975 return -EINVAL;
3976 buf = e+1;
3977 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003978 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003979 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003980 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003981 return -ENOENT;
3982 mddev->major_version = major;
3983 mddev->minor_version = minor;
3984 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003985 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003986 return len;
3987}
3988
3989static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003990__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003991
NeilBrowna35b0d62006-01-06 00:20:49 -08003992static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003993action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003994{
NeilBrown7eec3142005-11-08 21:39:44 -08003995 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003996 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3997 type = "frozen";
3998 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003999 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004000 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4001 type = "reshape";
4002 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004003 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4004 type = "resync";
4005 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4006 type = "check";
4007 else
4008 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004009 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004010 type = "recover";
4011 }
4012 return sprintf(page, "%s\n", type);
4013}
4014
4015static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004016action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004017{
NeilBrown7eec3142005-11-08 21:39:44 -08004018 if (!mddev->pers || !mddev->pers->sync_request)
4019 return -EINVAL;
4020
NeilBrownb6a9ce62009-05-26 09:41:17 +10004021 if (cmd_match(page, "frozen"))
4022 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4023 else
4024 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4025
4026 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrownf851b602014-12-11 10:02:10 +11004027 flush_workqueue(md_misc_wq);
NeilBrown7eec3142005-11-08 21:39:44 -08004028 if (mddev->sync_thread) {
4029 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004030 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004031 }
NeilBrown03c902e2006-01-06 00:20:46 -08004032 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4033 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004034 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004035 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004036 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004037 else if (cmd_match(page, "recover")) {
4038 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4039 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4040 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004041 int err;
4042 if (mddev->pers->start_reshape == NULL)
4043 return -EINVAL;
4044 err = mddev->pers->start_reshape(mddev);
4045 if (err)
4046 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004047 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004048 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004049 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004050 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004051 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004052 return -EINVAL;
4053 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4054 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004055 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004056 if (mddev->ro == 2) {
4057 /* A write to sync_action is enough to justify
4058 * canceling read-auto mode
4059 */
4060 mddev->ro = 0;
4061 md_wakeup_thread(mddev->sync_thread);
4062 }
NeilBrown03c902e2006-01-06 00:20:46 -08004063 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004064 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004065 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004066 return len;
4067}
4068
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004069static struct md_sysfs_entry md_scan_mode =
4070__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4071
4072static ssize_t
4073last_sync_action_show(struct mddev *mddev, char *page)
4074{
4075 return sprintf(page, "%s\n", mddev->last_sync_action);
4076}
4077
4078static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4079
NeilBrown9d888832005-11-08 21:39:26 -08004080static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004081mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004082{
4083 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004084 (unsigned long long)
4085 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004086}
4087
NeilBrown80ca3a42006-07-10 04:44:18 -07004088static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004089
NeilBrown88202a02006-01-06 00:21:36 -08004090static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004091sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004092{
4093 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4094 mddev->sync_speed_min ? "local": "system");
4095}
4096
4097static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004098sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004099{
4100 int min;
4101 char *e;
4102 if (strncmp(buf, "system", 6)==0) {
4103 mddev->sync_speed_min = 0;
4104 return len;
4105 }
4106 min = simple_strtoul(buf, &e, 10);
4107 if (buf == e || (*e && *e != '\n') || min <= 0)
4108 return -EINVAL;
4109 mddev->sync_speed_min = min;
4110 return len;
4111}
4112
4113static struct md_sysfs_entry md_sync_min =
4114__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4115
4116static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004117sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004118{
4119 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4120 mddev->sync_speed_max ? "local": "system");
4121}
4122
4123static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004124sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004125{
4126 int max;
4127 char *e;
4128 if (strncmp(buf, "system", 6)==0) {
4129 mddev->sync_speed_max = 0;
4130 return len;
4131 }
4132 max = simple_strtoul(buf, &e, 10);
4133 if (buf == e || (*e && *e != '\n') || max <= 0)
4134 return -EINVAL;
4135 mddev->sync_speed_max = max;
4136 return len;
4137}
4138
4139static struct md_sysfs_entry md_sync_max =
4140__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4141
Iustin Popd7f3d292007-10-16 23:30:54 -07004142static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004143degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004144{
4145 return sprintf(page, "%d\n", mddev->degraded);
4146}
4147static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004148
4149static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004150sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004151{
4152 return sprintf(page, "%d\n", mddev->parallel_resync);
4153}
4154
4155static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004156sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004157{
4158 long n;
4159
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004160 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004161 return -EINVAL;
4162
4163 if (n != 0 && n != 1)
4164 return -EINVAL;
4165
4166 mddev->parallel_resync = n;
4167
4168 if (mddev->sync_thread)
4169 wake_up(&resync_wait);
4170
4171 return len;
4172}
4173
4174/* force parallel resync, even with shared block devices */
4175static struct md_sysfs_entry md_sync_force_parallel =
4176__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4177 sync_force_parallel_show, sync_force_parallel_store);
4178
4179static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004180sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004181{
4182 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004183 if (mddev->curr_resync == 0)
4184 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004185 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4186 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004187 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004188 db = resync - mddev->resync_mark_cnt;
4189 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004190}
4191
NeilBrown80ca3a42006-07-10 04:44:18 -07004192static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004193
4194static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004195sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004196{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004197 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004198
NeilBrownacb180b2009-04-14 16:28:34 +10004199 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4200 return sprintf(page, "none\n");
4201
NeilBrown72f36d52012-10-11 14:25:57 +11004202 if (mddev->curr_resync == 1 ||
4203 mddev->curr_resync == 2)
4204 return sprintf(page, "delayed\n");
4205
NeilBrownc804cde2012-05-21 09:28:33 +10004206 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4207 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004208 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004209 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004210 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004211
NeilBrownacb180b2009-04-14 16:28:34 +10004212 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004213 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004214}
4215
NeilBrown80ca3a42006-07-10 04:44:18 -07004216static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004217
NeilBrowne464eaf2006-03-27 01:18:14 -08004218static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004219min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004220{
4221 return sprintf(page, "%llu\n",
4222 (unsigned long long)mddev->resync_min);
4223}
4224static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004225min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004226{
4227 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004228 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004229 return -EINVAL;
4230 if (min > mddev->resync_max)
4231 return -EINVAL;
4232 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4233 return -EBUSY;
4234
4235 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004236 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004237 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004238 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004239 return -EINVAL;
4240 }
4241 mddev->resync_min = min;
4242
4243 return len;
4244}
4245
4246static struct md_sysfs_entry md_min_sync =
4247__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4248
4249static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004250max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004251{
4252 if (mddev->resync_max == MaxSector)
4253 return sprintf(page, "max\n");
4254 else
4255 return sprintf(page, "%llu\n",
4256 (unsigned long long)mddev->resync_max);
4257}
4258static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004259max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004260{
4261 if (strncmp(buf, "max", 3) == 0)
4262 mddev->resync_max = MaxSector;
4263 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004264 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004265 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004266 return -EINVAL;
4267 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004268 return -EINVAL;
4269 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004270 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004271 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4272 return -EBUSY;
4273
4274 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004275 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004276 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004277 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004278 return -EINVAL;
4279 }
4280 mddev->resync_max = max;
4281 }
4282 wake_up(&mddev->recovery_wait);
4283 return len;
4284}
4285
4286static struct md_sysfs_entry md_max_sync =
4287__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4288
4289static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004290suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004291{
4292 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4293}
4294
4295static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004296suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004297{
4298 char *e;
4299 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004300 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004301
NeilBrownf72ffdd2014-09-30 14:23:59 +10004302 if (mddev->pers == NULL ||
NeilBrownb8d966e2009-07-01 11:14:04 +10004303 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004304 return -EINVAL;
4305 if (buf == e || (*e && *e != '\n'))
4306 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004307
4308 mddev->suspend_lo = new;
4309 if (new >= old)
4310 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004311 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004312 else {
4313 /* Expanding suspended region - need to wait */
4314 mddev->pers->quiesce(mddev, 1);
4315 mddev->pers->quiesce(mddev, 0);
4316 }
4317 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004318}
4319static struct md_sysfs_entry md_suspend_lo =
4320__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4321
NeilBrowne464eaf2006-03-27 01:18:14 -08004322static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004323suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004324{
4325 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4326}
4327
4328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004329suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004330{
4331 char *e;
4332 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004333 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004334
NeilBrownb8d966e2009-07-01 11:14:04 +10004335 if (mddev->pers == NULL ||
4336 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004337 return -EINVAL;
4338 if (buf == e || (*e && *e != '\n'))
4339 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004340
4341 mddev->suspend_hi = new;
4342 if (new <= old)
4343 /* Shrinking suspended region */
4344 mddev->pers->quiesce(mddev, 2);
4345 else {
4346 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004347 mddev->pers->quiesce(mddev, 1);
4348 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004349 }
4350 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004351}
4352static struct md_sysfs_entry md_suspend_hi =
4353__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4354
NeilBrown08a02ec2007-05-09 02:35:38 -07004355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004357{
4358 if (mddev->reshape_position != MaxSector)
4359 return sprintf(page, "%llu\n",
4360 (unsigned long long)mddev->reshape_position);
4361 strcpy(page, "none\n");
4362 return 5;
4363}
4364
4365static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004366reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004367{
NeilBrownc6563a82012-05-21 09:27:00 +10004368 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004369 char *e;
4370 unsigned long long new = simple_strtoull(buf, &e, 10);
4371 if (mddev->pers)
4372 return -EBUSY;
4373 if (buf == e || (*e && *e != '\n'))
4374 return -EINVAL;
4375 mddev->reshape_position = new;
4376 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004377 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004378 mddev->new_level = mddev->level;
4379 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004380 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004381 rdev_for_each(rdev, mddev)
4382 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004383 return len;
4384}
4385
4386static struct md_sysfs_entry md_reshape_position =
4387__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4388 reshape_position_store);
4389
Dan Williamsb522adc2009-03-31 15:00:31 +11004390static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004391reshape_direction_show(struct mddev *mddev, char *page)
4392{
4393 return sprintf(page, "%s\n",
4394 mddev->reshape_backwards ? "backwards" : "forwards");
4395}
4396
4397static ssize_t
4398reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4399{
4400 int backwards = 0;
4401 if (cmd_match(buf, "forwards"))
4402 backwards = 0;
4403 else if (cmd_match(buf, "backwards"))
4404 backwards = 1;
4405 else
4406 return -EINVAL;
4407 if (mddev->reshape_backwards == backwards)
4408 return len;
4409
4410 /* check if we are allowed to change */
4411 if (mddev->delta_disks)
4412 return -EBUSY;
4413
4414 if (mddev->persistent &&
4415 mddev->major_version == 0)
4416 return -EINVAL;
4417
4418 mddev->reshape_backwards = backwards;
4419 return len;
4420}
4421
4422static struct md_sysfs_entry md_reshape_direction =
4423__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4424 reshape_direction_store);
4425
4426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004427array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004428{
4429 if (mddev->external_size)
4430 return sprintf(page, "%llu\n",
4431 (unsigned long long)mddev->array_sectors/2);
4432 else
4433 return sprintf(page, "default\n");
4434}
4435
4436static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004437array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004438{
4439 sector_t sectors;
4440
4441 if (strncmp(buf, "default", 7) == 0) {
4442 if (mddev->pers)
4443 sectors = mddev->pers->size(mddev, 0, 0);
4444 else
4445 sectors = mddev->array_sectors;
4446
4447 mddev->external_size = 0;
4448 } else {
4449 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4450 return -EINVAL;
4451 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004452 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004453
4454 mddev->external_size = 1;
4455 }
4456
4457 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004458 if (mddev->pers) {
4459 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004460 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004461 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004462 return len;
4463}
4464
4465static struct md_sysfs_entry md_array_size =
4466__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4467 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004468
NeilBrowneae17012005-11-08 21:39:23 -08004469static struct attribute *md_default_attrs[] = {
4470 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004471 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004472 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004473 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004474 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004475 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004476 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004477 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004478 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004479 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004480 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004481 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004482 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004483 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004484 NULL,
4485};
4486
4487static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004488 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004489 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004490 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004491 &md_sync_min.attr,
4492 &md_sync_max.attr,
4493 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004494 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004495 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004496 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004497 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004498 &md_suspend_lo.attr,
4499 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004500 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004501 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004502 NULL,
4503};
NeilBrown411036f2005-11-08 21:39:40 -08004504static struct attribute_group md_redundancy_group = {
4505 .name = NULL,
4506 .attrs = md_redundancy_attrs,
4507};
4508
NeilBrowneae17012005-11-08 21:39:23 -08004509static ssize_t
4510md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4511{
4512 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004513 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004514 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004515
4516 if (!entry->show)
4517 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004518 spin_lock(&all_mddevs_lock);
4519 if (list_empty(&mddev->all_mddevs)) {
4520 spin_unlock(&all_mddevs_lock);
4521 return -EBUSY;
4522 }
4523 mddev_get(mddev);
4524 spin_unlock(&all_mddevs_lock);
4525
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004526 rv = mddev_lock(mddev);
4527 if (!rv) {
4528 rv = entry->show(mddev, page);
4529 mddev_unlock(mddev);
4530 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004531 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004532 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004533}
4534
4535static ssize_t
4536md_attr_store(struct kobject *kobj, struct attribute *attr,
4537 const char *page, size_t length)
4538{
4539 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004540 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004541 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004542
4543 if (!entry->store)
4544 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004545 if (!capable(CAP_SYS_ADMIN))
4546 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004547 spin_lock(&all_mddevs_lock);
4548 if (list_empty(&mddev->all_mddevs)) {
4549 spin_unlock(&all_mddevs_lock);
4550 return -EBUSY;
4551 }
4552 mddev_get(mddev);
4553 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004554 if (entry->store == new_dev_store)
4555 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004556 rv = mddev_lock(mddev);
4557 if (!rv) {
4558 rv = entry->store(mddev, page, length);
4559 mddev_unlock(mddev);
4560 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004561 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004562 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004563}
4564
4565static void md_free(struct kobject *ko)
4566{
NeilBrownfd01b882011-10-11 16:47:53 +11004567 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004568
4569 if (mddev->sysfs_state)
4570 sysfs_put(mddev->sysfs_state);
4571
4572 if (mddev->gendisk) {
4573 del_gendisk(mddev->gendisk);
4574 put_disk(mddev->gendisk);
4575 }
4576 if (mddev->queue)
4577 blk_cleanup_queue(mddev->queue);
4578
NeilBrowneae17012005-11-08 21:39:23 -08004579 kfree(mddev);
4580}
4581
Emese Revfy52cf25d2010-01-19 02:58:23 +01004582static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004583 .show = md_attr_show,
4584 .store = md_attr_store,
4585};
4586static struct kobj_type md_ktype = {
4587 .release = md_free,
4588 .sysfs_ops = &md_sysfs_ops,
4589 .default_attrs = md_default_attrs,
4590};
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592int mdp_major = 0;
4593
Dan Williams5fd3a172009-03-04 00:57:25 -07004594static void mddev_delayed_delete(struct work_struct *ws)
4595{
NeilBrownfd01b882011-10-11 16:47:53 +11004596 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004597
NeilBrown43a70502009-12-14 12:49:55 +11004598 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004599 kobject_del(&mddev->kobj);
4600 kobject_put(&mddev->kobj);
4601}
4602
NeilBrownefeb53c2009-01-09 08:31:10 +11004603static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004605 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004606 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004608 int partitioned;
4609 int shift;
4610 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004611 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
4613 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004614 return -ENODEV;
4615
4616 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4617 shift = partitioned ? MdpMinorShift : 0;
4618 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Tejun Heoe804ac72010-10-15 15:36:08 +02004620 /* wait for any previous instance of this device to be
4621 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004622 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004623 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004624
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004625 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004626 error = -EEXIST;
4627 if (mddev->gendisk)
4628 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004629
4630 if (name) {
4631 /* Need to ensure that 'name' is not a duplicate.
4632 */
NeilBrownfd01b882011-10-11 16:47:53 +11004633 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004634 spin_lock(&all_mddevs_lock);
4635
4636 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4637 if (mddev2->gendisk &&
4638 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4639 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004640 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004641 }
4642 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 }
NeilBrown8b765392009-01-09 08:31:08 +11004644
NeilBrown0909dc42009-07-01 12:27:21 +10004645 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004646 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004647 if (!mddev->queue)
4648 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004649 mddev->queue->queuedata = mddev;
4650
NeilBrown409c57f2009-03-31 14:39:39 +11004651 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004652 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 disk = alloc_disk(1 << shift);
4655 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004656 blk_cleanup_queue(mddev->queue);
4657 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004658 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004660 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004662 if (name)
4663 strcpy(disk->disk_name, name);
4664 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004666 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 disk->fops = &md_fops;
4669 disk->private_data = mddev;
4670 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004671 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004672 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004673 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004674 * remove it now.
4675 */
4676 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004678 /* As soon as we call add_disk(), another thread could get
4679 * through to md_open, so make sure it doesn't get too far
4680 */
4681 mutex_lock(&mddev->open_mutex);
4682 add_disk(disk);
4683
Tejun Heoed9e1982008-08-25 19:56:05 +09004684 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4685 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004686 if (error) {
4687 /* This isn't possible, but as kobject_init_and_add is marked
4688 * __must_check, we must do something with the result
4689 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004690 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4691 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004692 error = 0;
4693 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004694 if (mddev->kobj.sd &&
4695 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004696 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004697 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004698 abort:
4699 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004700 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004701 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004702 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004703 }
NeilBrownd3374822009-01-09 08:31:10 +11004704 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004705 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004706}
4707
4708static struct kobject *md_probe(dev_t dev, int *part, void *data)
4709{
4710 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 return NULL;
4712}
4713
NeilBrownefeb53c2009-01-09 08:31:10 +11004714static int add_named_array(const char *val, struct kernel_param *kp)
4715{
4716 /* val must be "md_*" where * is not all digits.
4717 * We allocate an array with a large free minor number, and
4718 * set the name to val. val must not already be an active name.
4719 */
4720 int len = strlen(val);
4721 char buf[DISK_NAME_LEN];
4722
4723 while (len && val[len-1] == '\n')
4724 len--;
4725 if (len >= DISK_NAME_LEN)
4726 return -E2BIG;
4727 strlcpy(buf, val, len+1);
4728 if (strncmp(buf, "md_", 3) != 0)
4729 return -EINVAL;
4730 return md_alloc(0, buf);
4731}
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733static void md_safemode_timeout(unsigned long data)
4734{
NeilBrownfd01b882011-10-11 16:47:53 +11004735 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Neil Brown0fd62b82008-06-28 08:31:36 +10004737 if (!atomic_read(&mddev->writes_pending)) {
4738 mddev->safemode = 1;
4739 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004740 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 md_wakeup_thread(mddev->thread);
4743}
4744
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004745static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
NeilBrownfd01b882011-10-11 16:47:53 +11004747int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
NeilBrown2604b702006-01-06 00:20:36 -08004749 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004750 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004751 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
NeilBrowna757e642005-04-16 15:26:42 -07004753 if (list_empty(&mddev->disks))
4754 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
4757 if (mddev->pers)
4758 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004759 /* Cannot run until previous stop completes properly */
4760 if (mddev->sysfs_active)
4761 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 /*
4764 * Analyze all RAID superblock(s)
4765 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004766 if (!mddev->raid_disks) {
4767 if (!mddev->persistent)
4768 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004769 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
NeilBrownd9d166c2006-01-06 00:20:51 -08004772 if (mddev->level != LEVEL_NONE)
4773 request_module("md-level-%d", mddev->level);
4774 else if (mddev->clevel[0])
4775 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 /*
4778 * Drop all container device buffers, from now on
4779 * the only valid external interface is through the md
4780 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004782 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004783 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 continue;
4785 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004786 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004787
4788 /* perform some consistency tests on the device.
4789 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004790 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004791 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004792 if (rdev->meta_bdev) {
4793 /* Nothing to check */;
4794 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004795 if (mddev->dev_sectors &&
4796 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004797 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004798 printk("md: %s: data overlaps metadata\n",
4799 mdname(mddev));
4800 return -EINVAL;
4801 }
4802 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004803 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004804 > rdev->data_offset) {
4805 printk("md: %s: metadata overlaps data\n",
4806 mdname(mddev));
4807 return -EINVAL;
4808 }
4809 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004810 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
4812
NeilBrowna167f662010-10-26 18:31:13 +11004813 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004814 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004817 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004818 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004820 if (mddev->level != LEVEL_NONE)
4821 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4822 mddev->level);
4823 else
4824 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4825 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 return -EINVAL;
4827 }
NeilBrown2604b702006-01-06 00:20:36 -08004828 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004830 if (mddev->level != pers->level) {
4831 mddev->level = pers->level;
4832 mddev->new_level = pers->level;
4833 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004834 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
NeilBrownf6705572006-03-27 01:18:11 -08004836 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004837 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004838 /* This personality cannot handle reshaping... */
4839 mddev->pers = NULL;
4840 module_put(pers->owner);
4841 return -EINVAL;
4842 }
4843
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004844 if (pers->sync_request) {
4845 /* Warn if this is a potentially silly
4846 * configuration.
4847 */
4848 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004849 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004850 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004851
NeilBrowndafb20f2012-03-19 12:46:39 +11004852 rdev_for_each(rdev, mddev)
4853 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004854 if (rdev < rdev2 &&
4855 rdev->bdev->bd_contains ==
4856 rdev2->bdev->bd_contains) {
4857 printk(KERN_WARNING
4858 "%s: WARNING: %s appears to be"
4859 " on the same physical disk as"
4860 " %s.\n",
4861 mdname(mddev),
4862 bdevname(rdev->bdev,b),
4863 bdevname(rdev2->bdev,b2));
4864 warned = 1;
4865 }
4866 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004867
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004868 if (warned)
4869 printk(KERN_WARNING
4870 "True protection against single-disk"
4871 " failure might be compromised.\n");
4872 }
4873
NeilBrown657390d2005-08-26 18:34:16 -07004874 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004875 /* may be over-ridden by personality */
4876 mddev->resync_max_sectors = mddev->dev_sectors;
4877
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004878 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
NeilBrown0f9552b2009-12-30 12:08:50 +11004880 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004881 mddev->ro = 2; /* read-only, but switch on first write */
4882
NeilBrownb15c2e52006-01-06 00:20:16 -08004883 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004884 if (err)
4885 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004886 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4887 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4888 " but 'external_size' not in effect?\n", __func__);
4889 printk(KERN_ERR
4890 "md: invalid array_size %llu > default size %llu\n",
4891 (unsigned long long)mddev->array_sectors / 2,
4892 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4893 err = -EINVAL;
4894 mddev->pers->stop(mddev);
4895 }
NeilBrownef99bf42012-05-22 13:55:08 +10004896 if (err == 0 && mddev->pers->sync_request &&
4897 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004898 err = bitmap_create(mddev);
4899 if (err) {
4900 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4901 mdname(mddev), err);
4902 mddev->pers->stop(mddev);
4903 }
4904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 module_put(mddev->pers->owner);
4907 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004908 bitmap_destroy(mddev);
4909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004911 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004912 if (mddev->kobj.sd &&
4913 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004914 printk(KERN_WARNING
4915 "md: cannot register extra attributes for %s\n",
4916 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004917 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004918 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004919 mddev->ro = 0;
4920
NeilBrownf72ffdd2014-09-30 14:23:59 +10004921 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004922 atomic_set(&mddev->max_corr_read_errors,
4923 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 mddev->safemode = 0;
4925 mddev->safemode_timer.function = md_safemode_timeout;
4926 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004927 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004929 smp_wmb();
4930 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11004931 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10004932 if (rdev->raid_disk >= 0)
4933 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004934 /* failure here is OK */;
NeilBrownf72ffdd2014-09-30 14:23:59 +10004935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10004937
NeilBrown7a0a5352013-08-27 16:28:23 +10004938 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07004939 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
NeilBrownd7603b72006-01-06 00:20:30 -08004941 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004942 sysfs_notify_dirent_safe(mddev->sysfs_state);
4943 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004944 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return 0;
4946}
NeilBrown390ee602010-06-01 19:37:27 +10004947EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
NeilBrownfd01b882011-10-11 16:47:53 +11004949static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004950{
4951 int err;
4952
4953 err = md_run(mddev);
4954 if (err)
4955 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004956 err = bitmap_load(mddev);
4957 if (err) {
4958 bitmap_destroy(mddev);
4959 goto out;
4960 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004961
4962 md_wakeup_thread(mddev->thread);
4963 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4964
NeilBrownfe60b012010-03-29 11:10:42 +11004965 set_capacity(mddev->gendisk, mddev->array_sectors);
4966 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004967 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004968 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4969out:
4970 return err;
4971}
4972
NeilBrownfd01b882011-10-11 16:47:53 +11004973static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974{
4975 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Andre Noll80fab1d2008-07-11 22:02:21 +10004977 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004979 return -ENXIO;
4980 if (!mddev->pers)
4981 return -EINVAL;
4982 if (!mddev->ro)
4983 return -EBUSY;
4984 mddev->safemode = 0;
4985 mddev->ro = 0;
4986 set_disk_ro(disk, 0);
4987 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4988 mdname(mddev));
4989 /* Kick recovery or resync if necessary */
4990 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4991 md_wakeup_thread(mddev->thread);
4992 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004993 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
4996
NeilBrownfd01b882011-10-11 16:47:53 +11004997static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004998{
4999 mddev->array_sectors = 0;
5000 mddev->external_size = 0;
5001 mddev->dev_sectors = 0;
5002 mddev->raid_disks = 0;
5003 mddev->recovery_cp = 0;
5004 mddev->resync_min = 0;
5005 mddev->resync_max = MaxSector;
5006 mddev->reshape_position = MaxSector;
5007 mddev->external = 0;
5008 mddev->persistent = 0;
5009 mddev->level = LEVEL_NONE;
5010 mddev->clevel[0] = 0;
5011 mddev->flags = 0;
5012 mddev->ro = 0;
5013 mddev->metadata_type[0] = 0;
5014 mddev->chunk_sectors = 0;
5015 mddev->ctime = mddev->utime = 0;
5016 mddev->layout = 0;
5017 mddev->max_disks = 0;
5018 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005019 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005020 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005021 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005022 mddev->new_level = LEVEL_NONE;
5023 mddev->new_layout = 0;
5024 mddev->new_chunk_sectors = 0;
5025 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005026 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005027 mddev->suspend_lo = mddev->suspend_hi = 0;
5028 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5029 mddev->recovery = 0;
5030 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005031 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005032 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005033 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005034 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005035 mddev->bitmap_info.offset = 0;
5036 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005037 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005038 mddev->bitmap_info.chunksize = 0;
5039 mddev->bitmap_info.daemon_sleep = 0;
5040 mddev->bitmap_info.max_write_behind = 0;
5041}
5042
NeilBrownfd01b882011-10-11 16:47:53 +11005043static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005044{
NeilBrown6b6204e2013-05-09 09:48:30 +10005045 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005046 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005047 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005048 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005049 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005050 }
5051
5052 del_timer_sync(&mddev->safemode_timer);
5053
5054 bitmap_flush(mddev);
5055 md_super_wait(mddev);
5056
NeilBrownb6d428c2013-04-24 11:42:42 +10005057 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005058 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005059 /* mark array as shutdown cleanly */
5060 mddev->in_sync = 1;
5061 md_update_sb(mddev, 1);
5062 }
5063}
NeilBrowndefad612011-01-14 09:14:33 +11005064
NeilBrownfd01b882011-10-11 16:47:53 +11005065void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005066{
NeilBrown29f097c2013-11-14 17:54:51 +11005067 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005068 __md_stop_writes(mddev);
5069 mddev_unlock(mddev);
5070}
NeilBrown390ee602010-06-01 19:37:27 +10005071EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005072
NeilBrown5eff3c42012-11-19 10:47:48 +11005073static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005074{
NeilBrown0ca69882011-01-14 09:14:33 +11005075 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005076 mddev->pers->stop(mddev);
5077 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5078 mddev->to_remove = &md_redundancy_group;
5079 module_put(mddev->pers->owner);
5080 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005081 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005082}
NeilBrown5eff3c42012-11-19 10:47:48 +11005083
5084void md_stop(struct mddev *mddev)
5085{
5086 /* stop the array and free an attached data structures.
5087 * This is called from dm-raid
5088 */
5089 __md_stop(mddev);
5090 bitmap_destroy(mddev);
5091 if (mddev->bio_set)
5092 bioset_free(mddev->bio_set);
5093}
5094
NeilBrown390ee602010-06-01 19:37:27 +10005095EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005096
NeilBrowna05b7ea2012-07-19 15:59:18 +10005097static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005098{
5099 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005100 int did_freeze = 0;
5101
5102 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5103 did_freeze = 1;
5104 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5105 md_wakeup_thread(mddev->thread);
5106 }
NeilBrownf851b602014-12-11 10:02:10 +11005107 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005108 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005109 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005110 /* Thread might be blocked waiting for metadata update
5111 * which will now never happen */
5112 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005113
NeilBrown30b8feb2013-11-14 15:16:17 +11005114 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005115 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
5116 &mddev->recovery));
NeilBrown30b8feb2013-11-14 15:16:17 +11005117 mddev_lock_nointr(mddev);
5118
NeilBrowna4bd82d2010-03-29 13:23:10 +11005119 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005120 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005121 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005122 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005123 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005124 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005125 if (did_freeze) {
5126 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005127 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005128 md_wakeup_thread(mddev->thread);
5129 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005130 err = -EBUSY;
5131 goto out;
5132 }
5133 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005134 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005135
5136 err = -ENXIO;
5137 if (mddev->ro==1)
5138 goto out;
5139 mddev->ro = 1;
5140 set_disk_ro(mddev->gendisk, 1);
5141 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005142 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5143 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005144 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005145 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005146 }
5147out:
5148 mutex_unlock(&mddev->open_mutex);
5149 return err;
5150}
5151
NeilBrown9e653b62006-06-26 00:27:58 -07005152/* mode:
5153 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005154 * 2 - stop but do not disassemble array
5155 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005156static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10005157 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005160 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005161 int did_freeze = 0;
5162
5163 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5164 did_freeze = 1;
5165 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5166 md_wakeup_thread(mddev->thread);
5167 }
NeilBrownf851b602014-12-11 10:02:10 +11005168 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11005169 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005170 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11005171 /* Thread might be blocked waiting for metadata update
5172 * which will now never happen */
5173 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11005174
NeilBrown30b8feb2013-11-14 15:16:17 +11005175 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11005176 wait_event(resync_wait, (mddev->sync_thread == NULL &&
5177 !test_bit(MD_RECOVERY_RUNNING,
5178 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11005179 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
NeilBrownc8c00a62009-08-10 12:50:52 +10005181 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005182 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005183 mddev->sysfs_active ||
5184 mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11005185 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005186 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005187 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005188 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005189 if (did_freeze) {
5190 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11005191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11005192 md_wakeup_thread(mddev->thread);
5193 }
NeilBrown260fa032013-08-27 16:44:13 +10005194 return -EBUSY;
5195 }
NeilBrown6e17b022010-08-07 21:41:19 +10005196 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005197 if (mddev->ro)
5198 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005199
NeilBrowndefad612011-01-14 09:14:33 +11005200 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005201 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005202 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005203 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005204
NeilBrowna4bd82d2010-03-29 13:23:10 +11005205 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005206 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005207
NeilBrowndafb20f2012-03-19 12:46:39 +11005208 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005209 if (rdev->raid_disk >= 0)
5210 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005211
NeilBrowna4bd82d2010-03-29 13:23:10 +11005212 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005213 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005214 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005215 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005216
NeilBrowna4bd82d2010-03-29 13:23:10 +11005217 if (mddev->ro)
5218 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005219 } else
5220 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 /*
5222 * Free resources if final stop
5223 */
NeilBrown9e653b62006-06-26 00:27:58 -07005224 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5226
NeilBrown978f9462006-02-02 14:28:05 -08005227 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005228 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005229 fput(mddev->bitmap_info.file);
5230 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005231 }
NeilBrownc3d97142009-12-14 12:49:52 +11005232 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 export_array(mddev);
5235
NeilBrown6177b472010-03-29 11:37:13 +11005236 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005237 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005238 if (mddev->hold_active == UNTIL_STOP)
5239 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005240 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005241 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005242 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005243 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245}
5246
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005247#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005248static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
NeilBrown3cb03002011-10-11 16:45:26 +11005250 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 int err;
5252
NeilBrowna757e642005-04-16 15:26:42 -07005253 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 printk(KERN_INFO "md: running: ");
5257
NeilBrowndafb20f2012-03-19 12:46:39 +11005258 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 char b[BDEVNAME_SIZE];
5260 printk("<%s>", bdevname(rdev->bdev,b));
5261 }
5262 printk("\n");
5263
NeilBrownd710e132008-10-13 11:55:12 +11005264 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (err) {
5266 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005267 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
5269}
5270
5271/*
5272 * lets try to run arrays based on all disks that have arrived
5273 * until now. (those are in pending_raid_disks)
5274 *
5275 * the method: pick the first pending disk, collect all disks with
5276 * the same UUID, remove all from the pending list and put them into
5277 * the 'same_array' list. Then order this list based on superblock
5278 * update time (freshest comes first), kick out 'old' disks and
5279 * compare superblocks. If everything's fine then run it.
5280 *
5281 * If "unit" is allocated, then bump its reference count
5282 */
5283static void autorun_devices(int part)
5284{
NeilBrown3cb03002011-10-11 16:45:26 +11005285 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005286 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 char b[BDEVNAME_SIZE];
5288
5289 printk(KERN_INFO "md: autorun ...\n");
5290 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005291 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005293 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005295 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 printk(KERN_INFO "md: considering %s ...\n",
5298 bdevname(rdev0->bdev,b));
5299 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005300 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 if (super_90_load(rdev, rdev0, 0) >= 0) {
5302 printk(KERN_INFO "md: adding %s ...\n",
5303 bdevname(rdev->bdev,b));
5304 list_move(&rdev->same_set, &candidates);
5305 }
5306 /*
5307 * now we have a set of devices, with all of them having
5308 * mostly sane superblocks. It's time to allocate the
5309 * mddev.
5310 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005311 if (part) {
5312 dev = MKDEV(mdp_major,
5313 rdev0->preferred_minor << MdpMinorShift);
5314 unit = MINOR(dev) >> MdpMinorShift;
5315 } else {
5316 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5317 unit = MINOR(dev);
5318 }
5319 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5321 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5322 break;
5323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 md_probe(dev, NULL, NULL);
5326 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005327 if (!mddev || !mddev->gendisk) {
5328 if (mddev)
5329 mddev_put(mddev);
5330 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 "md: cannot allocate memory for md drive.\n");
5332 break;
5333 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10005334 if (mddev_lock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 printk(KERN_WARNING "md: %s locked, cannot run\n",
5336 mdname(mddev));
5337 else if (mddev->raid_disks || mddev->major_version
5338 || !list_empty(&mddev->disks)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005339 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 "md: %s already running, cannot run %s\n",
5341 mdname(mddev), bdevname(rdev0->bdev,b));
5342 mddev_unlock(mddev);
5343 } else {
5344 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005345 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005346 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 list_del_init(&rdev->same_set);
5348 if (bind_rdev_to_array(rdev, mddev))
5349 export_rdev(rdev);
5350 }
5351 autorun_array(mddev);
5352 mddev_unlock(mddev);
5353 }
5354 /* on success, candidates will be empty, on error
5355 * it won't...
5356 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005357 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005358 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 mddev_put(mddev);
5362 }
5363 printk(KERN_INFO "md: ... autorun DONE.\n");
5364}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005365#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
NeilBrownf72ffdd2014-09-30 14:23:59 +10005367static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
5369 mdu_version_t ver;
5370
5371 ver.major = MD_MAJOR_VERSION;
5372 ver.minor = MD_MINOR_VERSION;
5373 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5374
5375 if (copy_to_user(arg, &ver, sizeof(ver)))
5376 return -EFAULT;
5377
5378 return 0;
5379}
5380
NeilBrownf72ffdd2014-09-30 14:23:59 +10005381static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
5383 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005384 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005385 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
NeilBrown1ca69c42012-10-11 13:37:33 +11005387 nr = working = insync = failed = spare = 0;
5388 rcu_read_lock();
5389 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005391 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 failed++;
5393 else {
5394 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005395 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10005396 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 else
5398 spare++;
5399 }
5400 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005401 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
5403 info.major_version = mddev->major_version;
5404 info.minor_version = mddev->minor_version;
5405 info.patch_version = MD_PATCHLEVEL_VERSION;
5406 info.ctime = mddev->ctime;
5407 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005408 info.size = mddev->dev_sectors / 2;
5409 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005410 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 info.nr_disks = nr;
5412 info.raid_disks = mddev->raid_disks;
5413 info.md_minor = mddev->md_minor;
5414 info.not_persistent= !mddev->persistent;
5415
5416 info.utime = mddev->utime;
5417 info.state = 0;
5418 if (mddev->in_sync)
5419 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005420 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005421 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005422 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 info.working_disks = working;
5424 info.failed_disks = failed;
5425 info.spare_disks = spare;
5426
5427 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005428 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 if (copy_to_user(arg, &info, sizeof(info)))
5431 return -EFAULT;
5432
5433 return 0;
5434}
5435
NeilBrownf72ffdd2014-09-30 14:23:59 +10005436static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005437{
5438 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5439 char *ptr, *buf = NULL;
5440 int err = -ENOMEM;
5441
NeilBrown60559da2013-07-16 16:44:29 +10005442 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005443
NeilBrown32a76272005-06-21 17:17:14 -07005444 if (!file)
5445 goto out;
5446
5447 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005448 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005449 file->pathname[0] = '\0';
5450 goto copy_out;
5451 }
5452
5453 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5454 if (!buf)
5455 goto out;
5456
NeilBrown1ec885c2012-05-22 13:55:10 +10005457 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5458 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005459 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005460 goto out;
5461
5462 strcpy(file->pathname, ptr);
5463
5464copy_out:
5465 err = 0;
5466 if (copy_to_user(arg, file, sizeof(*file)))
5467 err = -EFAULT;
5468out:
5469 kfree(buf);
5470 kfree(file);
5471 return err;
5472}
5473
NeilBrownf72ffdd2014-09-30 14:23:59 +10005474static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475{
5476 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005477 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 if (copy_from_user(&info, arg, sizeof(info)))
5480 return -EFAULT;
5481
NeilBrown1ca69c42012-10-11 13:37:33 +11005482 rcu_read_lock();
5483 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 if (rdev) {
5485 info.major = MAJOR(rdev->bdev->bd_dev);
5486 info.minor = MINOR(rdev->bdev->bd_dev);
5487 info.raid_disk = rdev->raid_disk;
5488 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005489 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005491 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 info.state |= (1<<MD_DISK_ACTIVE);
5493 info.state |= (1<<MD_DISK_SYNC);
5494 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005495 if (test_bit(WriteMostly, &rdev->flags))
5496 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 } else {
5498 info.major = info.minor = 0;
5499 info.raid_disk = -1;
5500 info.state = (1<<MD_DISK_REMOVED);
5501 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 if (copy_to_user(arg, &info, sizeof(info)))
5505 return -EFAULT;
5506
5507 return 0;
5508}
5509
NeilBrownf72ffdd2014-09-30 14:23:59 +10005510static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
5512 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005513 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 dev_t dev = MKDEV(info->major,info->minor);
5515
5516 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5517 return -EOVERFLOW;
5518
5519 if (!mddev->raid_disks) {
5520 int err;
5521 /* expecting a device which has a superblock */
5522 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5523 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005524 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 "md: md_import_device returned %ld\n",
5526 PTR_ERR(rdev));
5527 return PTR_ERR(rdev);
5528 }
5529 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005530 struct md_rdev *rdev0
5531 = list_entry(mddev->disks.next,
5532 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005533 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 .load_super(rdev, rdev0, mddev->minor_version);
5535 if (err < 0) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005536 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 "md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10005538 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 bdevname(rdev0->bdev,b2));
5540 export_rdev(rdev);
5541 return -EINVAL;
5542 }
5543 }
5544 err = bind_rdev_to_array(rdev, mddev);
5545 if (err)
5546 export_rdev(rdev);
5547 return err;
5548 }
5549
5550 /*
5551 * add_new_disk can be used once the array is assembled
5552 * to add "hot spares". They must already have a superblock
5553 * written
5554 */
5555 if (mddev->pers) {
5556 int err;
5557 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005558 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 "%s: personality does not support diskops!\n",
5560 mdname(mddev));
5561 return -EINVAL;
5562 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005563 if (mddev->persistent)
5564 rdev = md_import_device(dev, mddev->major_version,
5565 mddev->minor_version);
5566 else
5567 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005569 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 "md: md_import_device returned %ld\n",
5571 PTR_ERR(rdev));
5572 return PTR_ERR(rdev);
5573 }
NeilBrown1a855a02010-12-09 16:36:28 +11005574 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005575 if (!mddev->persistent) {
5576 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005577 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005578 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005579 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005580 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005581 } else
NeilBrown41158c72005-06-21 17:17:25 -07005582 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005583 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005584 } else
5585 super_types[mddev->major_version].
5586 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005587 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005588 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005589 /* This was a hot-add request, but events doesn't
5590 * match, so reject it.
5591 */
5592 export_rdev(rdev);
5593 return -EINVAL;
5594 }
5595
NeilBrownb2d444d2005-11-08 21:39:31 -08005596 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005597 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5598 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005599 else
5600 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005601
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 rdev->raid_disk = -1;
5603 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005604 if (!err && !mddev->pers->hot_remove_disk) {
5605 /* If there is hot_add_disk but no hot_remove_disk
5606 * then added disks for geometry changes,
5607 * and should be added immediately.
5608 */
5609 super_types[mddev->major_version].
5610 validate_super(mddev, rdev);
5611 err = mddev->pers->hot_add_disk(mddev, rdev);
5612 if (err)
5613 unbind_rdev_from_array(rdev);
5614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 if (err)
5616 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005617 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005618 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005619
NeilBrown7ceb17e2013-04-24 11:42:42 +10005620 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005621 if (mddev->degraded)
5622 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005623 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005624 if (!err)
5625 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005626 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return err;
5628 }
5629
5630 /* otherwise, add_new_disk is only allowed
5631 * for major_version==0 superblocks
5632 */
5633 if (mddev->major_version != 0) {
5634 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5635 mdname(mddev));
5636 return -EINVAL;
5637 }
5638
5639 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5640 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005641 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005643 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 "md: error, md_import_device() returned %ld\n",
5645 PTR_ERR(rdev));
5646 return PTR_ERR(rdev);
5647 }
5648 rdev->desc_nr = info->number;
5649 if (info->raid_disk < mddev->raid_disks)
5650 rdev->raid_disk = info->raid_disk;
5651 else
5652 rdev->raid_disk = -1;
5653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005655 if (info->state & (1<<MD_DISK_SYNC))
5656 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005658 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5659 set_bit(WriteMostly, &rdev->flags);
5660
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 if (!mddev->persistent) {
5662 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005663 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5664 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005665 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005666 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
NeilBrown2bf071b2006-01-06 00:20:55 -08005668 err = bind_rdev_to_array(rdev, mddev);
5669 if (err) {
5670 export_rdev(rdev);
5671 return err;
5672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674
5675 return 0;
5676}
5677
NeilBrownf72ffdd2014-09-30 14:23:59 +10005678static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
5680 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005681 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 rdev = find_rdev(mddev, dev);
5684 if (!rdev)
5685 return -ENXIO;
5686
NeilBrown3ea8929d2013-04-24 11:42:41 +10005687 clear_bit(Blocked, &rdev->flags);
5688 remove_and_add_spares(mddev, rdev);
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 if (rdev->raid_disk >= 0)
5691 goto busy;
5692
5693 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005694 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005695 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 return 0;
5698busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005699 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 bdevname(rdev->bdev,b), mdname(mddev));
5701 return -EBUSY;
5702}
5703
NeilBrownf72ffdd2014-09-30 14:23:59 +10005704static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705{
5706 char b[BDEVNAME_SIZE];
5707 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005708 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 if (!mddev->pers)
5711 return -ENODEV;
5712
5713 if (mddev->major_version != 0) {
5714 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5715 " version-0 superblocks.\n",
5716 mdname(mddev));
5717 return -EINVAL;
5718 }
5719 if (!mddev->pers->hot_add_disk) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005720 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 "%s: personality does not support diskops!\n",
5722 mdname(mddev));
5723 return -EINVAL;
5724 }
5725
NeilBrownd710e132008-10-13 11:55:12 +11005726 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 if (IS_ERR(rdev)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005728 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 "md: error, md_import_device() returned %ld\n",
5730 PTR_ERR(rdev));
5731 return -EINVAL;
5732 }
5733
5734 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005735 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005737 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
NeilBrown8190e752009-06-18 08:48:58 +10005739 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
NeilBrownb2d444d2005-11-08 21:39:31 -08005741 if (test_bit(Faulty, &rdev->flags)) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10005742 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 "md: can not hot-add faulty %s disk to %s!\n",
5744 bdevname(rdev->bdev,b), mdname(mddev));
5745 err = -EINVAL;
5746 goto abort_export;
5747 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005748 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005750 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005751 err = bind_rdev_to_array(rdev, mddev);
5752 if (err)
5753 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755 /*
5756 * The rest should better be atomic, we can have disk failures
5757 * noticed in interrupt contexts ...
5758 */
5759
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 rdev->raid_disk = -1;
5761
NeilBrown850b2b42006-10-03 01:15:46 -07005762 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 /*
5765 * Kick recovery, maybe this spare has to be added to the
5766 * array immediately.
5767 */
5768 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5769 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005770 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 return 0;
5772
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773abort_export:
5774 export_rdev(rdev);
5775 return err;
5776}
5777
NeilBrownfd01b882011-10-11 16:47:53 +11005778static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005779{
NeilBrown035328c2014-04-09 12:25:40 +10005780 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005781
NeilBrown36fa3062005-09-09 16:23:45 -07005782 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005783 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005784 return -EBUSY;
5785 if (mddev->recovery || mddev->sync_thread)
5786 return -EBUSY;
5787 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005788 }
5789
NeilBrown36fa3062005-09-09 16:23:45 -07005790 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005791 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005792 if (mddev->bitmap)
5793 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005794 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005795
NeilBrownc3d97142009-12-14 12:49:52 +11005796 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005797 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5798 mdname(mddev));
5799 return -EBADF;
5800 }
5801
NeilBrown035328c2014-04-09 12:25:40 +10005802 inode = mddev->bitmap_info.file->f_mapping->host;
5803 if (!S_ISREG(inode->i_mode)) {
5804 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5805 mdname(mddev));
5806 err = -EBADF;
5807 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5808 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5809 mdname(mddev));
5810 err = -EBADF;
5811 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005812 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5813 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005814 err = -EBUSY;
5815 }
5816 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005817 fput(mddev->bitmap_info.file);
5818 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005819 return err;
5820 }
NeilBrownc3d97142009-12-14 12:49:52 +11005821 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005822 } else if (mddev->bitmap == NULL)
5823 return -ENOENT; /* cannot remove what isn't there */
5824 err = 0;
5825 if (mddev->pers) {
5826 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005827 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005828 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005829 if (!err)
5830 err = bitmap_load(mddev);
5831 }
NeilBrownd7375ab322006-06-26 00:27:43 -07005832 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005833 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005834 fd = -1; /* make sure to put the file */
5835 }
NeilBrown36fa3062005-09-09 16:23:45 -07005836 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005837 }
5838 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005839 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11005840 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11005841 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005842 }
5843
NeilBrown32a76272005-06-21 17:17:14 -07005844 return err;
5845}
5846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847/*
5848 * set_array_info is used two different ways
5849 * The original usage is when creating a new array.
5850 * In this usage, raid_disks is > 0 and it together with
5851 * level, size, not_persistent,layout,chunksize determine the
5852 * shape of the array.
5853 * This will always create an array with a type-0.90.0 superblock.
5854 * The newer usage is when assembling an array.
5855 * In this case raid_disks will be 0, and the major_version field is
5856 * use to determine which style super-blocks are to be found on the devices.
5857 * The minor and patch _version numbers are also kept incase the
5858 * super_block handler wishes to interpret them.
5859 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005860static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
5862
5863 if (info->raid_disks == 0) {
5864 /* just setting version number for superblock loading */
5865 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005866 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 super_types[info->major_version].name == NULL) {
5868 /* maybe try to auto-load a module? */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005869 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 "md: superblock version %d not known\n",
5871 info->major_version);
5872 return -EINVAL;
5873 }
5874 mddev->major_version = info->major_version;
5875 mddev->minor_version = info->minor_version;
5876 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005877 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005878 /* ensure mddev_put doesn't delete this now that there
5879 * is some minimal configuration.
5880 */
5881 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 return 0;
5883 }
5884 mddev->major_version = MD_MAJOR_VERSION;
5885 mddev->minor_version = MD_MINOR_VERSION;
5886 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5887 mddev->ctime = get_seconds();
5888
5889 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005890 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005891 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 mddev->raid_disks = info->raid_disks;
5893 /* don't set md_minor, it is determined by which /dev/md* was
5894 * openned
5895 */
5896 if (info->state & (1<<MD_SB_CLEAN))
5897 mddev->recovery_cp = MaxSector;
5898 else
5899 mddev->recovery_cp = 0;
5900 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005901 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005904 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
5906 mddev->max_disks = MD_SB_DISKS;
5907
NeilBrowne6910632008-02-06 01:39:51 -08005908 if (mddev->persistent)
5909 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005910 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
NeilBrownc3d97142009-12-14 12:49:52 +11005912 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10005913 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11005914 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005915
NeilBrownf6705572006-03-27 01:18:11 -08005916 mddev->reshape_position = MaxSector;
5917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 /*
5919 * Generate a 128 bit UUID
5920 */
5921 get_random_bytes(mddev->uuid, 16);
5922
NeilBrownf6705572006-03-27 01:18:11 -08005923 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005924 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005925 mddev->new_layout = mddev->layout;
5926 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005927 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08005928
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 return 0;
5930}
5931
NeilBrownfd01b882011-10-11 16:47:53 +11005932void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005933{
Dan Williamsb522adc2009-03-31 15:00:31 +11005934 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5935
5936 if (mddev->external_size)
5937 return;
5938
Dan Williams1f403622009-03-31 14:59:03 +11005939 mddev->array_sectors = array_sectors;
5940}
5941EXPORT_SYMBOL(md_set_array_sectors);
5942
NeilBrownfd01b882011-10-11 16:47:53 +11005943static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005944{
NeilBrown3cb03002011-10-11 16:45:26 +11005945 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005946 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005947 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005948
5949 if (mddev->pers->resize == NULL)
5950 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005951 /* The "num_sectors" is the number of sectors of each device that
5952 * is used. This can only make sense for arrays with redundancy.
5953 * linear and raid0 always use whatever space is available. We can only
5954 * consider changing this number if no resync or reconstruction is
5955 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005956 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005957 * of each device. If num_sectors is zero, we find the largest size
5958 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005959 */
NeilBrownf851b602014-12-11 10:02:10 +11005960 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5961 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08005962 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10005963 if (mddev->ro)
5964 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10005965
NeilBrowndafb20f2012-03-19 12:46:39 +11005966 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005967 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005968
Andre Nolld71f9f82008-07-11 22:02:22 +10005969 if (fit && (num_sectors == 0 || num_sectors > avail))
5970 num_sectors = avail;
5971 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005972 return -ENOSPC;
5973 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005974 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005975 if (!rv)
5976 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005977 return rv;
5978}
5979
NeilBrownfd01b882011-10-11 16:47:53 +11005980static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08005981{
5982 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10005983 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08005984 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005985 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005986 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10005987 if (mddev->ro)
5988 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08005989 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005990 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005991 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11005992 if (mddev->sync_thread ||
5993 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
5994 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005995 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10005996
5997 rdev_for_each(rdev, mddev) {
5998 if (mddev->raid_disks < raid_disks &&
5999 rdev->data_offset < rdev->new_data_offset)
6000 return -EINVAL;
6001 if (mddev->raid_disks > raid_disks &&
6002 rdev->data_offset > rdev->new_data_offset)
6003 return -EINVAL;
6004 }
6005
NeilBrown63c70c42006-03-27 01:18:13 -08006006 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006007 if (mddev->delta_disks < 0)
6008 mddev->reshape_backwards = 1;
6009 else if (mddev->delta_disks > 0)
6010 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006011
6012 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006013 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006014 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006015 mddev->reshape_backwards = 0;
6016 }
NeilBrownda943b992006-01-06 00:20:54 -08006017 return rv;
6018}
6019
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020/*
6021 * update_array_info is used to change the configuration of an
6022 * on-line array.
6023 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6024 * fields in the info are checked against the array.
6025 * Any differences that cannot be handled will cause an error.
6026 * Normally, only one change can be managed at a time.
6027 */
NeilBrownfd01b882011-10-11 16:47:53 +11006028static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
6030 int rv = 0;
6031 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006032 int state = 0;
6033
6034 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006035 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006036 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038 if (mddev->major_version != info->major_version ||
6039 mddev->minor_version != info->minor_version ||
6040/* mddev->patch_version != info->patch_version || */
6041 mddev->ctime != info->ctime ||
6042 mddev->level != info->level ||
6043/* mddev->layout != info->layout || */
6044 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006045 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006046 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6047 ((state^info->state) & 0xfffffe00)
6048 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return -EINVAL;
6050 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006051 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6052 cnt++;
6053 if (mddev->raid_disks != info->raid_disks)
6054 cnt++;
6055 if (mddev->layout != info->layout)
6056 cnt++;
6057 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6058 cnt++;
6059 if (cnt == 0)
6060 return 0;
6061 if (cnt > 1)
6062 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
6064 if (mddev->layout != info->layout) {
6065 /* Change layout
6066 * we don't need to do anything at the md level, the
6067 * personality will take care of it all.
6068 */
NeilBrown50ac1682009-06-18 08:47:55 +10006069 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006071 else {
6072 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006073 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006074 if (rv)
6075 mddev->new_layout = mddev->layout;
6076 return rv;
6077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006079 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006080 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
NeilBrownda943b992006-01-06 00:20:54 -08006082 if (mddev->raid_disks != info->raid_disks)
6083 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
NeilBrown36fa3062005-09-09 16:23:45 -07006085 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006086 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006087 return -EINVAL;
6088 if (mddev->recovery || mddev->sync_thread)
6089 return -EBUSY;
6090 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6091 /* add the bitmap */
6092 if (mddev->bitmap)
6093 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006094 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006095 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006096 mddev->bitmap_info.offset =
6097 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006098 mddev->bitmap_info.space =
6099 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006100 mddev->pers->quiesce(mddev, 1);
6101 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006102 if (!rv)
6103 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006104 if (rv)
6105 bitmap_destroy(mddev);
6106 mddev->pers->quiesce(mddev, 0);
6107 } else {
6108 /* remove the bitmap */
6109 if (!mddev->bitmap)
6110 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006111 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006112 return -EINVAL;
6113 mddev->pers->quiesce(mddev, 1);
6114 bitmap_destroy(mddev);
6115 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006116 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006117 }
6118 }
NeilBrown850b2b42006-10-03 01:15:46 -07006119 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 return rv;
6121}
6122
NeilBrownfd01b882011-10-11 16:47:53 +11006123static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124{
NeilBrown3cb03002011-10-11 16:45:26 +11006125 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006126 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
6128 if (mddev->pers == NULL)
6129 return -ENODEV;
6130
NeilBrown1ca69c42012-10-11 13:37:33 +11006131 rcu_read_lock();
6132 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006134 err = -ENODEV;
6135 else {
6136 md_error(mddev, rdev);
6137 if (!test_bit(Faulty, &rdev->flags))
6138 err = -EBUSY;
6139 }
6140 rcu_read_unlock();
6141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
Andre Noll2f9618c2008-04-25 18:57:58 +02006144/*
6145 * We have a problem here : there is no easy way to give a CHS
6146 * virtual geometry. We currently pretend that we have a 2 heads
6147 * 4 sectors (with a BIG number of cylinders...). This drives
6148 * dosfs just mad... ;-)
6149 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006150static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6151{
NeilBrownfd01b882011-10-11 16:47:53 +11006152 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006153
6154 geo->heads = 2;
6155 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006156 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006157 return 0;
6158}
6159
Nicolas Schichancb335f82014-01-15 16:58:52 +01006160static inline bool md_ioctl_valid(unsigned int cmd)
6161{
6162 switch (cmd) {
6163 case ADD_NEW_DISK:
6164 case BLKROSET:
6165 case GET_ARRAY_INFO:
6166 case GET_BITMAP_FILE:
6167 case GET_DISK_INFO:
6168 case HOT_ADD_DISK:
6169 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01006170 case RAID_AUTORUN:
6171 case RAID_VERSION:
6172 case RESTART_ARRAY_RW:
6173 case RUN_ARRAY:
6174 case SET_ARRAY_INFO:
6175 case SET_BITMAP_FILE:
6176 case SET_DISK_FAULTY:
6177 case STOP_ARRAY:
6178 case STOP_ARRAY_RO:
6179 return true;
6180 default:
6181 return false;
6182 }
6183}
6184
Al Viroa39907f2008-03-02 10:31:15 -05006185static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 unsigned int cmd, unsigned long arg)
6187{
6188 int err = 0;
6189 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006190 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006191 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
Nicolas Schichancb335f82014-01-15 16:58:52 +01006193 if (!md_ioctl_valid(cmd))
6194 return -ENOTTY;
6195
NeilBrown506c9e42011-12-23 10:17:26 +11006196 switch (cmd) {
6197 case RAID_VERSION:
6198 case GET_ARRAY_INFO:
6199 case GET_DISK_INFO:
6200 break;
6201 default:
6202 if (!capable(CAP_SYS_ADMIN))
6203 return -EACCES;
6204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 /*
6207 * Commands dealing with the RAID driver but not any
6208 * particular array:
6209 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006210 switch (cmd) {
6211 case RAID_VERSION:
6212 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006213 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006216 case RAID_AUTORUN:
6217 err = 0;
6218 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006221 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 }
6223
6224 /*
6225 * Commands creating/starting a new array:
6226 */
6227
Al Viroa39907f2008-03-02 10:31:15 -05006228 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
6230 if (!mddev) {
6231 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10006232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 }
6234
NeilBrown1ca69c42012-10-11 13:37:33 +11006235 /* Some actions do not requires the mutex */
6236 switch (cmd) {
6237 case GET_ARRAY_INFO:
6238 if (!mddev->raid_disks && !mddev->external)
6239 err = -ENODEV;
6240 else
6241 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006242 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006243
6244 case GET_DISK_INFO:
6245 if (!mddev->raid_disks && !mddev->external)
6246 err = -ENODEV;
6247 else
6248 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006249 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006250
6251 case SET_DISK_FAULTY:
6252 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006253 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11006254 }
6255
NeilBrowna7a3f082012-12-11 13:35:54 +11006256 if (cmd == ADD_NEW_DISK)
6257 /* need to ensure md_delayed_delete() has completed */
6258 flush_workqueue(md_misc_wq);
6259
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006260 if (cmd == HOT_REMOVE_DISK)
6261 /* need to ensure recovery thread has run */
6262 wait_event_interruptible_timeout(mddev->sb_wait,
6263 !test_bit(MD_RECOVERY_NEEDED,
6264 &mddev->flags),
6265 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006266 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6267 /* Need to flush page cache, and ensure no-one else opens
6268 * and writes
6269 */
6270 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006271 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006272 mutex_unlock(&mddev->open_mutex);
6273 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006274 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10006275 }
6276 set_bit(MD_STILL_CLOSED, &mddev->flags);
6277 mutex_unlock(&mddev->open_mutex);
6278 sync_blockdev(bdev);
6279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 err = mddev_lock(mddev);
6281 if (err) {
NeilBrownf72ffdd2014-09-30 14:23:59 +10006282 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6284 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10006285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 }
6287
NeilBrownc02c0ae2012-12-11 13:39:21 +11006288 if (cmd == SET_ARRAY_INFO) {
6289 mdu_array_info_t info;
6290 if (!arg)
6291 memset(&info, 0, sizeof(info));
6292 else if (copy_from_user(&info, argp, sizeof(info))) {
6293 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006294 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006295 }
6296 if (mddev->pers) {
6297 err = update_array_info(mddev, &info);
6298 if (err) {
6299 printk(KERN_WARNING "md: couldn't update"
6300 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006301 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006303 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006304 }
6305 if (!list_empty(&mddev->disks)) {
6306 printk(KERN_WARNING
6307 "md: array %s already has disks!\n",
6308 mdname(mddev));
6309 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006310 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006311 }
6312 if (mddev->raid_disks) {
6313 printk(KERN_WARNING
6314 "md: array %s already initialised!\n",
6315 mdname(mddev));
6316 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10006317 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006318 }
6319 err = set_array_info(mddev, &info);
6320 if (err) {
6321 printk(KERN_WARNING "md: couldn't set"
6322 " array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10006323 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006324 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006325 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 }
6327
6328 /*
6329 * Commands querying/configuring an existing array:
6330 */
NeilBrown32a76272005-06-21 17:17:14 -07006331 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006332 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006333 if ((!mddev->raid_disks && !mddev->external)
6334 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6335 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6336 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10006338 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 }
6340
6341 /*
6342 * Commands even a read-only array can execute:
6343 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006344 switch (cmd) {
6345 case GET_BITMAP_FILE:
6346 err = get_bitmap_file(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10006347 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006348
6349 case RESTART_ARRAY_RW:
6350 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006351 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006352
6353 case STOP_ARRAY:
6354 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006355 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006356
6357 case STOP_ARRAY_RO:
6358 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006359 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006360
NeilBrown3ea8929d2013-04-24 11:42:41 +10006361 case HOT_REMOVE_DISK:
6362 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006363 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10006364
NeilBrown7ceb17e2013-04-24 11:42:42 +10006365 case ADD_NEW_DISK:
6366 /* We can support ADD_NEW_DISK on read-only arrays
6367 * on if we are re-adding a preexisting device.
6368 * So require mddev->pers and MD_DISK_SYNC.
6369 */
6370 if (mddev->pers) {
6371 mdu_disk_info_t info;
6372 if (copy_from_user(&info, argp, sizeof(info)))
6373 err = -EFAULT;
6374 else if (!(info.state & (1<<MD_DISK_SYNC)))
6375 /* Need to clear read-only for this */
6376 break;
6377 else
6378 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006379 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10006380 }
6381 break;
6382
NeilBrownc02c0ae2012-12-11 13:39:21 +11006383 case BLKROSET:
6384 if (get_user(ro, (int __user *)(arg))) {
6385 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10006386 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006387 }
6388 err = -EINVAL;
6389
6390 /* if the bdev is going readonly the value of mddev->ro
6391 * does not matter, no writes are coming
6392 */
6393 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10006394 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006395
NeilBrownc02c0ae2012-12-11 13:39:21 +11006396 /* are we are already prepared for writes? */
6397 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10006398 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006399
6400 /* transitioning to readauto need only happen for
6401 * arrays that call md_write_start
6402 */
6403 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006405 if (err == 0) {
6406 mddev->ro = 2;
6407 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006408 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006409 }
NeilBrown3adc28d2014-09-30 15:46:41 +10006410 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 }
6412
6413 /*
6414 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006415 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 */
NeilBrown326eb172014-09-30 15:36:28 +10006417 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006418 if (mddev->ro == 2) {
6419 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006420 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006421 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006422 /* mddev_unlock will wake thread */
6423 /* If a device failed while we were read-only, we
6424 * need to make sure the metadata is updated now.
6425 */
6426 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6427 mddev_unlock(mddev);
6428 wait_event(mddev->sb_wait,
6429 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6430 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006431 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006432 }
NeilBrownf91de922005-11-08 21:39:36 -08006433 } else {
6434 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10006435 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08006436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 }
6438
NeilBrownc02c0ae2012-12-11 13:39:21 +11006439 switch (cmd) {
6440 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006442 mdu_disk_info_t info;
6443 if (copy_from_user(&info, argp, sizeof(info)))
6444 err = -EFAULT;
6445 else
6446 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10006447 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
NeilBrownc02c0ae2012-12-11 13:39:21 +11006450 case HOT_ADD_DISK:
6451 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10006452 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
NeilBrownc02c0ae2012-12-11 13:39:21 +11006454 case RUN_ARRAY:
6455 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006456 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
NeilBrownc02c0ae2012-12-11 13:39:21 +11006458 case SET_BITMAP_FILE:
6459 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10006460 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006461
NeilBrownc02c0ae2012-12-11 13:39:21 +11006462 default:
6463 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10006464 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 }
6466
NeilBrown3adc28d2014-09-30 15:46:41 +10006467unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006468 if (mddev->hold_active == UNTIL_IOCTL &&
6469 err != -EINVAL)
6470 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10006472out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 return err;
6474}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006475#ifdef CONFIG_COMPAT
6476static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6477 unsigned int cmd, unsigned long arg)
6478{
6479 switch (cmd) {
6480 case HOT_REMOVE_DISK:
6481 case HOT_ADD_DISK:
6482 case SET_DISK_FAULTY:
6483 case SET_BITMAP_FILE:
6484 /* These take in integer arg, do not convert */
6485 break;
6486 default:
6487 arg = (unsigned long)compat_ptr(arg);
6488 break;
6489 }
6490
6491 return md_ioctl(bdev, mode, cmd, arg);
6492}
6493#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
Al Viroa39907f2008-03-02 10:31:15 -05006495static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496{
6497 /*
6498 * Succeed if we can lock the mddev, which confirms that
6499 * it isn't being stopped right now.
6500 */
NeilBrownfd01b882011-10-11 16:47:53 +11006501 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 int err;
6503
Yuanhan Liu0c098222012-05-22 13:55:32 +10006504 if (!mddev)
6505 return -ENODEV;
6506
NeilBrownd3374822009-01-09 08:31:10 +11006507 if (mddev->gendisk != bdev->bd_disk) {
6508 /* we are racing with mddev_put which is discarding this
6509 * bd_disk.
6510 */
6511 mddev_put(mddev);
6512 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006513 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006514 /* Then retry the open from the top */
6515 return -ERESTARTSYS;
6516 }
6517 BUG_ON(mddev != bdev->bd_disk->private_data);
6518
NeilBrownc8c00a62009-08-10 12:50:52 +10006519 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 goto out;
6521
6522 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006523 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006524 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006525 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006527 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 out:
6529 return err;
6530}
6531
Al Virodb2a1442013-05-05 21:52:57 -04006532static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533{
NeilBrownf72ffdd2014-09-30 14:23:59 +10006534 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006536 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006537 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006540
6541static int md_media_changed(struct gendisk *disk)
6542{
NeilBrownfd01b882011-10-11 16:47:53 +11006543 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006544
6545 return mddev->changed;
6546}
6547
6548static int md_revalidate(struct gendisk *disk)
6549{
NeilBrownfd01b882011-10-11 16:47:53 +11006550 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006551
6552 mddev->changed = 0;
6553 return 0;
6554}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006555static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556{
6557 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006558 .open = md_open,
6559 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006560 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006561#ifdef CONFIG_COMPAT
6562 .compat_ioctl = md_compat_ioctl,
6563#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006564 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006565 .media_changed = md_media_changed,
6566 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567};
6568
NeilBrownf72ffdd2014-09-30 14:23:59 +10006569static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570{
NeilBrown2b8bf342011-10-11 16:48:23 +11006571 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 /*
6574 * md_thread is a 'system-thread', it's priority should be very
6575 * high. We avoid resource deadlocks individually in each
6576 * raid personality. (RAID5 does preallocation) We also use RR and
6577 * the very same RT priority as kswapd, thus we will never get
6578 * into a priority inversion deadlock.
6579 *
6580 * we definitely have to have equal or higher priority than
6581 * bdflush, otherwise bdflush will deadlock if there are too
6582 * many dirty RAID5 blocks.
6583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrown6985c432005-10-19 21:23:47 -07006585 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006586 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
NeilBrown93588e22005-11-15 00:09:12 -08006588 /* We need to wait INTERRUPTIBLE so that
6589 * we don't add to the load-average.
6590 * That means we need to be sure no signals are
6591 * pending
6592 */
6593 if (signal_pending(current))
6594 flush_signals(current);
6595
6596 wait_event_interruptible_timeout
6597 (thread->wqueue,
6598 test_bit(THREAD_WAKEUP, &thread->flags)
6599 || kthread_should_stop(),
6600 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
NeilBrown6c987912011-01-14 09:13:53 +11006602 clear_bit(THREAD_WAKEUP, &thread->flags);
6603 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006604 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 return 0;
6608}
6609
NeilBrown2b8bf342011-10-11 16:48:23 +11006610void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611{
6612 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006613 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 set_bit(THREAD_WAKEUP, &thread->flags);
6615 wake_up(&thread->wqueue);
6616 }
6617}
NeilBrown6c144d32014-09-30 16:15:38 +10006618EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
Shaohua Li4ed87312012-10-11 13:34:00 +11006620struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6621 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622{
NeilBrown2b8bf342011-10-11 16:48:23 +11006623 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
NeilBrown2b8bf342011-10-11 16:48:23 +11006625 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 if (!thread)
6627 return NULL;
6628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 init_waitqueue_head(&thread->wqueue);
6630
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 thread->run = run;
6632 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006633 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006634 thread->tsk = kthread_run(md_thread, thread,
6635 "%s_%s",
6636 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006637 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006638 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 kfree(thread);
6640 return NULL;
6641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 return thread;
6643}
NeilBrown6c144d32014-09-30 16:15:38 +10006644EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
NeilBrown2b8bf342011-10-11 16:48:23 +11006646void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647{
NeilBrown2b8bf342011-10-11 16:48:23 +11006648 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006649 if (!thread)
6650 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006651 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006652 /* Locking ensures that mddev_unlock does not wake_up a
6653 * non-existent thread
6654 */
6655 spin_lock(&pers_lock);
6656 *threadp = NULL;
6657 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006658
6659 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 kfree(thread);
6661}
NeilBrown6c144d32014-09-30 16:15:38 +10006662EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
NeilBrownfd01b882011-10-11 16:47:53 +11006664void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665{
NeilBrownb2d444d2005-11-08 21:39:31 -08006666 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006668
NeilBrownde393cd2011-07-28 11:31:48 +10006669 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 return;
6671 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006672 if (mddev->degraded)
6673 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006674 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6676 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6677 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006678 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006679 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006680 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681}
NeilBrown6c144d32014-09-30 16:15:38 +10006682EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
6684/* seq_file implementation /proc/mdstat */
6685
6686static void status_unused(struct seq_file *seq)
6687{
6688 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006689 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
6691 seq_printf(seq, "unused devices: ");
6692
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006693 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 char b[BDEVNAME_SIZE];
6695 i++;
6696 seq_printf(seq, "%s ",
6697 bdevname(rdev->bdev,b));
6698 }
6699 if (!i)
6700 seq_printf(seq, "<none>");
6701
6702 seq_printf(seq, "\n");
6703}
6704
NeilBrownf72ffdd2014-09-30 14:23:59 +10006705static void status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706{
NeilBrowndd71cf62009-05-07 12:49:35 +10006707 sector_t max_sectors, resync, res;
6708 unsigned long dt, db;
6709 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006710 int scale;
6711 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
NeilBrown72f36d52012-10-11 14:25:57 +11006713 if (mddev->curr_resync <= 3)
6714 resync = 0;
6715 else
6716 resync = mddev->curr_resync
6717 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
NeilBrownc804cde2012-05-21 09:28:33 +10006719 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6720 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006721 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006723 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
NeilBrown403df472014-09-30 15:52:29 +10006725 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08006726 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006727 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006728 * u32, as those are the requirements for sector_div.
6729 * Thus 'scale' must be at least 10
6730 */
6731 scale = 10;
6732 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006733 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006734 scale++;
6735 }
6736 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006737 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006738
6739 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 {
NeilBrown4588b422006-03-27 01:18:04 -08006741 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 seq_printf(seq, "[");
6743 for (i = 0; i < x; i++)
6744 seq_printf(seq, "=");
6745 seq_printf(seq, ">");
6746 for (i = 0; i < y; i++)
6747 seq_printf(seq, ".");
6748 seq_printf(seq, "] ");
6749 }
NeilBrown4588b422006-03-27 01:18:04 -08006750 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006751 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6752 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006753 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6754 "check" :
6755 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6756 "resync" : "recovery"))),
6757 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006758 (unsigned long long) resync/2,
6759 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
6761 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 * dt: time from mark until now
6763 * db: blocks written from mark until now
6764 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006765 *
6766 * rt is a sector_t, so could be 32bit or 64bit.
6767 * So we divide before multiply in case it is 32bit and close
6768 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006769 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006770 * near the end of resync when the number of remaining sectors
6771 * is close to 'db'.
6772 * We then divide rt by 32 after multiplying by db to compensate.
6773 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 */
6775 dt = ((jiffies - mddev->resync_mark) / HZ);
6776 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006777 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6778 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
NeilBrowndd71cf62009-05-07 12:49:35 +10006780 rt = max_sectors - resync; /* number of remaining sectors */
6781 sector_div(rt, db/32+1);
6782 rt *= dt;
6783 rt >>= 5;
6784
6785 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6786 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
NeilBrownff4e8d92006-07-10 04:44:16 -07006788 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789}
6790
6791static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6792{
6793 struct list_head *tmp;
6794 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006795 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
6797 if (l >= 0x10000)
6798 return NULL;
6799 if (!l--)
6800 /* header */
6801 return (void*)1;
6802
6803 spin_lock(&all_mddevs_lock);
6804 list_for_each(tmp,&all_mddevs)
6805 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006806 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 mddev_get(mddev);
6808 spin_unlock(&all_mddevs_lock);
6809 return mddev;
6810 }
6811 spin_unlock(&all_mddevs_lock);
6812 if (!l--)
6813 return (void*)2;/* tail */
6814 return NULL;
6815}
6816
6817static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6818{
6819 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006820 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006821
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 ++*pos;
6823 if (v == (void*)2)
6824 return NULL;
6825
6826 spin_lock(&all_mddevs_lock);
6827 if (v == (void*)1)
6828 tmp = all_mddevs.next;
6829 else
6830 tmp = mddev->all_mddevs.next;
6831 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006832 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 else {
6834 next_mddev = (void*)2;
6835 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10006836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 spin_unlock(&all_mddevs_lock);
6838
6839 if (v != (void*)1)
6840 mddev_put(mddev);
6841 return next_mddev;
6842
6843}
6844
6845static void md_seq_stop(struct seq_file *seq, void *v)
6846{
NeilBrownfd01b882011-10-11 16:47:53 +11006847 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
6849 if (mddev && v != (void*)1 && v != (void*)2)
6850 mddev_put(mddev);
6851}
6852
6853static int md_seq_show(struct seq_file *seq, void *v)
6854{
NeilBrownfd01b882011-10-11 16:47:53 +11006855 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006856 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006857 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
6859 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006860 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 seq_printf(seq, "Personalities : ");
6862 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006863 list_for_each_entry(pers, &pers_list, list)
6864 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
6866 spin_unlock(&pers_lock);
6867 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006868 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 return 0;
6870 }
6871 if (v == (void*)2) {
6872 status_unused(seq);
6873 return 0;
6874 }
6875
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006876 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006878
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6880 seq_printf(seq, "%s : %sactive", mdname(mddev),
6881 mddev->pers ? "" : "in");
6882 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006883 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006885 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006886 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 seq_printf(seq, " %s", mddev->pers->name);
6888 }
6889
Andre Nolldd8ac332009-03-31 14:33:13 +11006890 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006891 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 char b[BDEVNAME_SIZE];
6893 seq_printf(seq, " %s[%d]",
6894 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006895 if (test_bit(WriteMostly, &rdev->flags))
6896 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006897 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 seq_printf(seq, "(F)");
6899 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006900 }
6901 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006902 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006903 if (test_bit(Replacement, &rdev->flags))
6904 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006905 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 }
6907
6908 if (!list_empty(&mddev->disks)) {
6909 if (mddev->pers)
6910 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006911 (unsigned long long)
6912 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 else
6914 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006915 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006917 if (mddev->persistent) {
6918 if (mddev->major_version != 0 ||
6919 mddev->minor_version != 90) {
6920 seq_printf(seq," super %d.%d",
6921 mddev->major_version,
6922 mddev->minor_version);
6923 }
NeilBrowne6910632008-02-06 01:39:51 -08006924 } else if (mddev->external)
6925 seq_printf(seq, " super external:%s",
6926 mddev->metadata_type);
6927 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006928 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
6930 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006931 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006932 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006933 if (mddev->pers->sync_request) {
6934 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006935 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006936 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11006937 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08006938 seq_printf(seq, "\tresync=DELAYED\n ");
6939 else if (mddev->recovery_cp < MaxSector)
6940 seq_printf(seq, "\tresync=PENDING\n ");
6941 }
NeilBrown32a76272005-06-21 17:17:14 -07006942 } else
6943 seq_printf(seq, "\n ");
6944
NeilBrown57148962012-03-19 12:46:40 +11006945 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
6947 seq_printf(seq, "\n");
6948 }
6949 mddev_unlock(mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10006950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 return 0;
6952}
6953
Jan Engelhardt110518b2009-05-07 12:49:37 +10006954static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 .start = md_seq_start,
6956 .next = md_seq_next,
6957 .stop = md_seq_stop,
6958 .show = md_seq_show,
6959};
6960
6961static int md_seq_open(struct inode *inode, struct file *file)
6962{
Kay Sieversf1514632011-07-12 20:48:39 +02006963 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 int error;
6965
6966 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006967 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006968 return error;
6969
6970 seq = file->private_data;
6971 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 return error;
6973}
6974
NeilBrowne2f23b62014-04-09 14:33:51 +10006975static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08006976static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6977{
Kay Sieversf1514632011-07-12 20:48:39 +02006978 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006979 int mask;
6980
NeilBrowne2f23b62014-04-09 14:33:51 +10006981 if (md_unloading)
kbuild test robot7d7e64f2014-12-03 16:07:59 +11006982 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08006983 poll_wait(filp, &md_event_waiters, wait);
6984
6985 /* always allow read */
6986 mask = POLLIN | POLLRDNORM;
6987
Kay Sieversf1514632011-07-12 20:48:39 +02006988 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006989 mask |= POLLERR | POLLPRI;
6990 return mask;
6991}
6992
Arjan van de Venfa027c22007-02-12 00:55:33 -08006993static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006994 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 .open = md_seq_open,
6996 .read = seq_read,
6997 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006998 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006999 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000};
7001
NeilBrown84fc4b52011-10-11 16:49:58 +11007002int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003{
Chao Yu50bd3772014-09-25 15:28:34 +08007004 printk(KERN_INFO "md: %s personality registered for level %d\n",
7005 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007007 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 spin_unlock(&pers_lock);
7009 return 0;
7010}
NeilBrown6c144d32014-09-30 16:15:38 +10007011EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
NeilBrown84fc4b52011-10-11 16:49:58 +11007013int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014{
NeilBrown2604b702006-01-06 00:20:36 -08007015 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007017 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 spin_unlock(&pers_lock);
7019 return 0;
7020}
NeilBrown6c144d32014-09-30 16:15:38 +10007021EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
NeilBrownfd01b882011-10-11 16:47:53 +11007023static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007025 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007027 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
7029 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007030 rcu_read_lock();
7031 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007033 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7034 (int)part_stat_read(&disk->part0, sectors[1]) -
7035 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007036 /* sync IO will cause sync_io to increase before the disk_stats
7037 * as sync_io is counted when a request starts, and
7038 * disk_stats is counted when it completes.
7039 * So resync activity will cause curr_events to be smaller than
7040 * when there was no such activity.
7041 * non-sync IO will cause disk_stat to increase without
7042 * increasing sync_io so curr_events will (eventually)
7043 * be larger than it was before. Once it becomes
7044 * substantially larger, the test below will cause
7045 * the array to appear non-idle, and resync will slow
7046 * down.
7047 * If there is a lot of outstanding resync activity when
7048 * we set last_event to curr_events, then all that activity
7049 * completing might cause the array to appear non-idle
7050 * and resync will be slowed down even though there might
7051 * not have been non-resync activity. This will only
7052 * happen once though. 'last_events' will soon reflect
7053 * the state where there is little or no outstanding
7054 * resync requests, and further resync activity will
7055 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007056 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007058 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 rdev->last_events = curr_events;
7060 idle = 0;
7061 }
7062 }
NeilBrown4b809912008-07-21 17:05:25 +10007063 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 return idle;
7065}
7066
NeilBrownfd01b882011-10-11 16:47:53 +11007067void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068{
7069 /* another "blocks" (512byte) blocks have been synced */
7070 atomic_sub(blocks, &mddev->recovery_active);
7071 wake_up(&mddev->recovery_wait);
7072 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007073 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007074 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 md_wakeup_thread(mddev->thread);
7076 // stop recovery, signal do_sync ....
7077 }
7078}
NeilBrown6c144d32014-09-30 16:15:38 +10007079EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
NeilBrown06d91a52005-06-21 17:17:12 -07007081/* md_write_start(mddev, bi)
7082 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007083 * in superblock) before writing, schedule a superblock update
7084 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007085 */
NeilBrownfd01b882011-10-11 16:47:53 +11007086void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
Neil Brown0fd62b82008-06-28 08:31:36 +10007088 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007089 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007090 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007091
NeilBrownf91de922005-11-08 21:39:36 -08007092 BUG_ON(mddev->ro == 1);
7093 if (mddev->ro == 2) {
7094 /* need to switch to read/write */
7095 mddev->ro = 0;
7096 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7097 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007098 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007099 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007100 }
NeilBrown06d91a52005-06-21 17:17:12 -07007101 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007102 if (mddev->safemode == 1)
7103 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007104 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007105 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007106 if (mddev->in_sync) {
7107 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007108 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007109 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007110 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007111 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007112 }
NeilBrowna9701a32005-11-08 21:39:34 -08007113 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007114 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007115 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007116 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007117 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007118 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119}
NeilBrown6c144d32014-09-30 16:15:38 +10007120EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
NeilBrownfd01b882011-10-11 16:47:53 +11007122void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123{
7124 if (atomic_dec_and_test(&mddev->writes_pending)) {
7125 if (mddev->safemode == 2)
7126 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007127 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7129 }
7130}
NeilBrown6c144d32014-09-30 16:15:38 +10007131EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
NeilBrown2a2275d2007-01-26 00:57:11 -08007133/* md_allow_write(mddev)
7134 * Calling this ensures that the array is marked 'active' so that writes
7135 * may proceed without blocking. It is important to call this before
7136 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7137 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007138 *
7139 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7140 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007141 */
NeilBrownfd01b882011-10-11 16:47:53 +11007142int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007143{
7144 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007145 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007146 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007147 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007148 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007149 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007150
7151 spin_lock_irq(&mddev->write_lock);
7152 if (mddev->in_sync) {
7153 mddev->in_sync = 0;
7154 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007155 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007156 if (mddev->safemode_delay &&
7157 mddev->safemode == 0)
7158 mddev->safemode = 1;
7159 spin_unlock_irq(&mddev->write_lock);
7160 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007161 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007162 } else
7163 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007164
NeilBrown070dc6d2010-08-30 17:33:34 +10007165 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007166 return -EAGAIN;
7167 else
7168 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007169}
7170EXPORT_SYMBOL_GPL(md_allow_write);
7171
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172#define SYNC_MARKS 10
7173#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007174#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007175void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176{
Shaohua Li4ed87312012-10-11 13:34:00 +11007177 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007178 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 unsigned int currspeed = 0,
7180 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007181 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007183 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 sector_t mark_cnt[SYNC_MARKS];
7185 int last_mark,m;
7186 struct list_head *tmp;
7187 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007188 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007189 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007190 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007191 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
7193 /* just incase thread restarts... */
7194 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7195 return;
NeilBrown3991b312014-05-28 13:39:23 +10007196 if (mddev->ro) {/* never try to sync a read-only array */
7197 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007198 return;
NeilBrown3991b312014-05-28 13:39:23 +10007199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
NeilBrown61df9d92006-10-03 01:15:57 -07007201 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007202 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007203 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007204 action = "check";
7205 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007206 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007207 action = "repair";
7208 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007209 desc = "resync";
7210 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7211 desc = "reshape";
7212 else
7213 desc = "recovery";
7214
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007215 mddev->last_sync_action = action ?: desc;
7216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 /* we overload curr_resync somewhat here.
7218 * 0 == not engaged in resync at all
7219 * 2 == checking that there is no conflict with another sync
7220 * 1 == like 2, but have yielded to allow conflicting resync to
7221 * commense
7222 * other == active in resync - this many blocks
7223 *
7224 * Before starting a resync we must have set curr_resync to
7225 * 2, and then checked that every "conflicting" array has curr_resync
7226 * less than ours. When we find one that is the same or higher
7227 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7228 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7229 * This will mean we have to start checking from the beginning again.
7230 *
7231 */
7232
7233 do {
7234 mddev->curr_resync = 2;
7235
7236 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007237 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007239 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 if (mddev2 == mddev)
7241 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007242 if (!mddev->parallel_resync
7243 && mddev2->curr_resync
7244 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 DEFINE_WAIT(wq);
7246 if (mddev < mddev2 && mddev->curr_resync == 2) {
7247 /* arbitrarily yield */
7248 mddev->curr_resync = 1;
7249 wake_up(&resync_wait);
7250 }
7251 if (mddev > mddev2 && mddev->curr_resync == 1)
7252 /* no need to wait here, we can wait the next
7253 * time 'round when curr_resync == 2
7254 */
7255 continue;
NeilBrown97441972008-09-19 11:49:54 +10007256 /* We need to wait 'interruptible' so as not to
7257 * contribute to the load average, and not to
7258 * be caught by 'softlockup'
7259 */
7260 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007261 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007262 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007263 printk(KERN_INFO "md: delaying %s of %s"
7264 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007266 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007268 if (signal_pending(current))
7269 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 schedule();
7271 finish_wait(&resync_wait, &wq);
7272 goto try_again;
7273 }
7274 finish_wait(&resync_wait, &wq);
7275 }
7276 }
7277 } while (mddev->curr_resync < 2);
7278
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007279 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007280 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007282 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 */
7284 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007285 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007286 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007287 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7288 j = mddev->resync_min;
7289 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007290 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007291
NeilBrownccfcc3c2006-03-27 01:18:09 -08007292 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007293 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007294 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007296 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007297 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007298 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007299 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007300 if (rdev->raid_disk >= 0 &&
7301 !test_bit(Faulty, &rdev->flags) &&
7302 !test_bit(In_sync, &rdev->flags) &&
7303 rdev->recovery_offset < j)
7304 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007305 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007306
7307 /* If there is a bitmap, we need to make sure all
7308 * writes that started before we added a spare
7309 * complete before we start doing a recovery.
7310 * Otherwise the write might complete and (via
7311 * bitmap_endwrite) set a bit in the bitmap after the
7312 * recovery has checked that bit and skipped that
7313 * region.
7314 */
7315 if (mddev->bitmap) {
7316 mddev->pers->quiesce(mddev, 1);
7317 mddev->pers->quiesce(mddev, 0);
7318 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320
NeilBrown61df9d92006-10-03 01:15:57 -07007321 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7322 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7323 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007324 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007325 "(but not more than %d KB/sec) for %s.\n",
7326 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
NeilBrowneea1bf32009-03-31 14:27:02 +11007328 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007329
NeilBrown57afd892005-06-21 17:17:13 -07007330 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 for (m = 0; m < SYNC_MARKS; m++) {
7332 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007333 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 }
7335 last_mark = 0;
7336 mddev->resync_mark = mark[last_mark];
7337 mddev->resync_mark_cnt = mark_cnt[last_mark];
7338
7339 /*
7340 * Tune reconstruction:
7341 */
7342 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007343 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7344 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
7346 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 last_check = 0;
7348
7349 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007350 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007351 "md: resuming %s of %s from checkpoint.\n",
7352 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007354 } else
7355 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007356 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007357 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7358 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007359 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
majianpeng7c2c57c2012-07-03 12:12:26 +10007361 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007363 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
NeilBrown57afd892005-06-21 17:17:13 -07007365 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007366
NeilBrown7a91ee12009-05-26 12:57:21 +10007367 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7368 ((mddev->curr_resync > mddev->curr_resync_completed &&
7369 (mddev->curr_resync - mddev->curr_resync_completed)
7370 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007371 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007372 (j - mddev->curr_resync_completed)*2
7373 >= mddev->resync_max - mddev->curr_resync_completed
7374 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007375 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007376 wait_event(mddev->recovery_wait,
7377 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007378 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007379 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7380 j > mddev->recovery_cp)
7381 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007382 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007383 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007384 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007385 }
NeilBrownacb180b2009-04-14 16:28:34 +10007386
NeilBrownc91abf52013-11-19 12:02:01 +11007387 while (j >= mddev->resync_max &&
7388 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007389 /* As this condition is controlled by user-space,
7390 * we can block indefinitely, so use '_interruptible'
7391 * to avoid triggering warnings.
7392 */
7393 flush_signals(current); /* just in case */
7394 wait_event_interruptible(mddev->recovery_wait,
7395 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007396 || test_bit(MD_RECOVERY_INTR,
7397 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007398 }
NeilBrownacb180b2009-04-14 16:28:34 +10007399
NeilBrownc91abf52013-11-19 12:02:01 +11007400 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7401 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007402
NeilBrown57afd892005-06-21 17:17:13 -07007403 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007404 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007405 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007406 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 }
NeilBrown57afd892005-06-21 17:17:13 -07007409
7410 if (!skipped) { /* actual IO requested */
7411 io_sectors += sectors;
7412 atomic_add(sectors, &mddev->recovery_active);
7413 }
7414
NeilBrowne875ece2011-07-28 11:39:24 +10007415 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7416 break;
7417
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007419 if (j > 2)
7420 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007421 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007422 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007423 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007424 * visible in /proc/mdstat
7425 */
7426 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007427
7428 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 continue;
7430
NeilBrown57afd892005-06-21 17:17:13 -07007431 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 repeat:
7433 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7434 /* step marks */
7435 int next = (last_mark+1) % SYNC_MARKS;
7436
7437 mddev->resync_mark = mark[next];
7438 mddev->resync_mark_cnt = mark_cnt[next];
7439 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007440 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 last_mark = next;
7442 }
7443
NeilBrownc91abf52013-11-19 12:02:01 +11007444 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
7447 /*
7448 * this loop exits only if either when we are slower than
7449 * the 'hard' speed limit, or the system was IO-idle for
7450 * a jiffy.
7451 * the system might be non-idle CPU-wise, but we only care
7452 * about not overloading the IO subsystem. (things like an
7453 * e2fsck being done on the RAID array should execute fast)
7454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 cond_resched();
7456
Xiao Niac7e50a2014-08-07 09:37:41 -04007457 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7458 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007459 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460
NeilBrown88202a02006-01-06 00:21:36 -08007461 if (currspeed > speed_min(mddev)) {
7462 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007463 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007464 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 goto repeat;
7466 }
7467 }
7468 }
NeilBrownc91abf52013-11-19 12:02:01 +11007469 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7470 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7471 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 /*
7473 * this also signals 'finished resyncing' to md_stop
7474 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007475 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7477
7478 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007479 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
NeilBrowndfc70642008-05-23 13:04:39 -07007481 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007482 mddev->curr_resync > 2) {
7483 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7484 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7485 if (mddev->curr_resync >= mddev->recovery_cp) {
7486 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007487 "md: checkpointing %s of %s.\n",
7488 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007489 if (test_bit(MD_RECOVERY_ERROR,
7490 &mddev->recovery))
7491 mddev->recovery_cp =
7492 mddev->curr_resync_completed;
7493 else
7494 mddev->recovery_cp =
7495 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007496 }
7497 } else
7498 mddev->recovery_cp = MaxSector;
7499 } else {
7500 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7501 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007502 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007503 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007504 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007505 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007506 !test_bit(Faulty, &rdev->flags) &&
7507 !test_bit(In_sync, &rdev->flags) &&
7508 rdev->recovery_offset < mddev->curr_resync)
7509 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007510 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007513 skip:
NeilBrown17571282006-12-10 02:20:52 -08007514 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515
NeilBrownc07b70a2009-12-14 12:49:48 +11007516 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7517 /* We completed so min/max setting can be forgotten if used. */
7518 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7519 mddev->resync_min = 0;
7520 mddev->resync_max = MaxSector;
7521 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7522 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 mddev->curr_resync = 0;
7524 wake_up(&resync_wait);
7525 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7526 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528}
NeilBrown29269552006-03-27 01:18:10 -08007529EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
NeilBrown746d3202013-04-24 11:42:41 +10007531static int remove_and_add_spares(struct mddev *mddev,
7532 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007533{
NeilBrown3cb03002011-10-11 16:45:26 +11007534 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007535 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007536 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007537
NeilBrowndafb20f2012-03-19 12:46:39 +11007538 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007539 if ((this == NULL || rdev == this) &&
7540 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007541 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007542 (test_bit(Faulty, &rdev->flags) ||
7543 ! test_bit(In_sync, &rdev->flags)) &&
7544 atomic_read(&rdev->nr_pending)==0) {
7545 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007546 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007547 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007548 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007549 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007550 }
7551 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007552 if (removed && mddev->kobj.sd)
7553 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007554
NeilBrown746d3202013-04-24 11:42:41 +10007555 if (this)
7556 goto no_add;
7557
NeilBrowndafb20f2012-03-19 12:46:39 +11007558 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007559 if (rdev->raid_disk >= 0 &&
7560 !test_bit(In_sync, &rdev->flags) &&
7561 !test_bit(Faulty, &rdev->flags))
7562 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007563 if (rdev->raid_disk >= 0)
7564 continue;
7565 if (test_bit(Faulty, &rdev->flags))
7566 continue;
7567 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007568 ! (rdev->saved_raid_disk >= 0 &&
7569 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007570 continue;
7571
NeilBrown7eb41882014-01-14 15:55:14 +11007572 if (rdev->saved_raid_disk < 0)
7573 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007574 if (mddev->pers->
7575 hot_add_disk(mddev, rdev) == 0) {
7576 if (sysfs_link_rdev(mddev, rdev))
7577 /* failure here is OK */;
7578 spares++;
7579 md_new_event(mddev);
7580 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007581 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007582 }
NeilBrown746d3202013-04-24 11:42:41 +10007583no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007584 if (removed)
7585 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007586 return spares;
7587}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007588
NeilBrownac05f252014-09-30 08:10:42 +10007589static void md_start_sync(struct work_struct *ws)
7590{
7591 struct mddev *mddev = container_of(ws, struct mddev, del_work);
7592
7593 mddev->sync_thread = md_register_thread(md_do_sync,
7594 mddev,
7595 "resync");
7596 if (!mddev->sync_thread) {
7597 printk(KERN_ERR "%s: could not start resync"
7598 " thread...\n",
7599 mdname(mddev));
7600 /* leave the spares where they are, it shouldn't hurt */
7601 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7602 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7603 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7604 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7605 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007606 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10007607 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7608 &mddev->recovery))
7609 if (mddev->sysfs_action)
7610 sysfs_notify_dirent_safe(mddev->sysfs_action);
7611 } else
7612 md_wakeup_thread(mddev->sync_thread);
7613 sysfs_notify_dirent_safe(mddev->sysfs_action);
7614 md_new_event(mddev);
7615}
7616
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617/*
7618 * This routine is regularly called by all per-raid-array threads to
7619 * deal with generic issues like resync and super-block update.
7620 * Raid personalities that don't have a thread (linear/raid0) do not
7621 * need this as they never do any recovery or update the superblock.
7622 *
7623 * It does not do any resync itself, but rather "forks" off other threads
7624 * to do that as needed.
7625 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7626 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007627 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 * and wakeups up this thread which will reap the thread and finish up.
7629 * This thread also removes any faulty devices (with nr_pending == 0).
7630 *
7631 * The overall approach is:
7632 * 1/ if the superblock needs updating, update it.
7633 * 2/ If a recovery thread is running, don't do anything else.
7634 * 3/ If recovery has finished, clean up, possibly marking spares active.
7635 * 4/ If there are any faulty devices, remove them.
7636 * 5/ If array is degraded, try to add spares devices
7637 * 6/ If array has spares or is not in-sync, start a resync thread.
7638 */
NeilBrownfd01b882011-10-11 16:47:53 +11007639void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007641 if (mddev->suspended)
7642 return;
7643
NeilBrown5f404022005-06-21 17:17:16 -07007644 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007645 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646
NeilBrownfca4d842005-06-21 17:17:11 -07007647 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007648 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007649 printk(KERN_INFO "md: %s in immediate safe mode\n",
7650 mdname(mddev));
7651 mddev->safemode = 2;
7652 }
7653 flush_signals(current);
7654 }
7655
NeilBrownc89a8ee2008-08-05 15:54:13 +10007656 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7657 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007659 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007661 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007662 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007663 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7664 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 ))
7666 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007667
NeilBrowndf5b89b2006-03-27 01:18:20 -08007668 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007669 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007670
NeilBrownc89a8ee2008-08-05 15:54:13 +10007671 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007672 /* On a read-only array we can:
7673 * - remove failed devices
7674 * - add already-in_sync devices if the array itself
7675 * is in-sync.
7676 * As we only add devices that are already in-sync,
7677 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007678 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007679 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007680 /* There is no thread, but we need to call
7681 * ->spare_active and clear saved_raid_disk
7682 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007683 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007684 md_reap_sync_thread(mddev);
7685 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007686 goto unlock;
7687 }
7688
NeilBrown31a59e32008-04-30 00:52:30 -07007689 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007690 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007691 spin_lock_irq(&mddev->write_lock);
7692 if (mddev->safemode &&
7693 !atomic_read(&mddev->writes_pending) &&
7694 !mddev->in_sync &&
7695 mddev->recovery_cp == MaxSector) {
7696 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007697 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007698 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007699 }
7700 if (mddev->safemode == 1)
7701 mddev->safemode = 0;
7702 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007703 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007704 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007705 }
NeilBrownfca4d842005-06-21 17:17:11 -07007706
NeilBrown7a0a5352013-08-27 16:28:23 +10007707 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007708 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007709
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7711 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7712 /* resync/recovery still happening */
7713 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7714 goto unlock;
7715 }
7716 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007717 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 goto unlock;
7719 }
Neil Brown72a23c22008-06-28 08:31:41 +10007720 /* Set RUNNING before clearing NEEDED to avoid
7721 * any transients in the value of "sync_action".
7722 */
NeilBrown72f36d52012-10-11 14:25:57 +11007723 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007724 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007725 /* Clear some bits that don't mean anything, but
7726 * might be left set
7727 */
NeilBrown24dd4692005-11-08 21:39:26 -08007728 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7729 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730
NeilBrowned209582012-04-24 10:23:14 +10007731 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7732 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10007733 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 /* no recovery is running.
7735 * remove any failed drives, then
7736 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007737 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 * the personality to fail the re-add.
7739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007741 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007742 if (mddev->pers->check_reshape == NULL ||
7743 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007744 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10007745 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007746 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007747 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007748 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007749 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7750 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007751 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007752 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007753 } else if (mddev->recovery_cp < MaxSector) {
7754 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007755 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007756 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7757 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10007758 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08007759
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007761 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007762 /* We are adding a device or devices to an array
7763 * which has the bitmap stored on all devices.
7764 * So make sure all bitmap pages get written
7765 */
7766 bitmap_write_all(mddev->bitmap);
7767 }
NeilBrownac05f252014-09-30 08:10:42 +10007768 INIT_WORK(&mddev->del_work, md_start_sync);
7769 queue_work(md_misc_wq, &mddev->del_work);
7770 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771 }
NeilBrownac05f252014-09-30 08:10:42 +10007772 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10007773 if (!mddev->sync_thread) {
7774 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007775 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10007776 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7777 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007778 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007779 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007780 }
NeilBrownac05f252014-09-30 08:10:42 +10007781 unlock:
7782 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 mddev_unlock(mddev);
7784 }
7785}
NeilBrown6c144d32014-09-30 16:15:38 +10007786EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007788void md_reap_sync_thread(struct mddev *mddev)
7789{
7790 struct md_rdev *rdev;
7791
7792 /* resync has finished, collect result */
7793 md_unregister_thread(&mddev->sync_thread);
7794 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7795 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7796 /* success...*/
7797 /* activate any spares */
7798 if (mddev->pers->spare_active(mddev)) {
7799 sysfs_notify(&mddev->kobj, NULL,
7800 "degraded");
7801 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7802 }
7803 }
7804 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7805 mddev->pers->finish_reshape)
7806 mddev->pers->finish_reshape(mddev);
7807
7808 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007809 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007810 */
NeilBrownf4667222013-12-09 12:04:56 +11007811 if (!mddev->degraded)
7812 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007813 rdev->saved_raid_disk = -1;
7814
7815 md_update_sb(mddev, 1);
7816 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7817 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7818 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7819 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7820 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11007821 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007822 /* flag recovery needed just to double check */
7823 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7824 sysfs_notify_dirent_safe(mddev->sysfs_action);
7825 md_new_event(mddev);
7826 if (mddev->event_work.func)
7827 queue_work(md_misc_wq, &mddev->event_work);
7828}
NeilBrown6c144d32014-09-30 16:15:38 +10007829EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007830
NeilBrownfd01b882011-10-11 16:47:53 +11007831void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007832{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007833 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007834 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007835 !test_bit(Blocked, &rdev->flags) &&
7836 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007837 msecs_to_jiffies(5000));
7838 rdev_dec_pending(rdev, mddev);
7839}
7840EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7841
NeilBrownc6563a82012-05-21 09:27:00 +10007842void md_finish_reshape(struct mddev *mddev)
7843{
7844 /* called be personality module when reshape completes. */
7845 struct md_rdev *rdev;
7846
7847 rdev_for_each(rdev, mddev) {
7848 if (rdev->data_offset > rdev->new_data_offset)
7849 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7850 else
7851 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7852 rdev->data_offset = rdev->new_data_offset;
7853 }
7854}
7855EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007856
7857/* Bad block management.
7858 * We can record which blocks on each device are 'bad' and so just
7859 * fail those blocks, or that stripe, rather than the whole device.
7860 * Entries in the bad-block table are 64bits wide. This comprises:
7861 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7862 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7863 * A 'shift' can be set so that larger blocks are tracked and
7864 * consequently larger devices can be covered.
7865 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7866 *
7867 * Locking of the bad-block table uses a seqlock so md_is_badblock
7868 * might need to retry if it is very unlucky.
7869 * We will sometimes want to check for bad blocks in a bi_end_io function,
7870 * so we use the write_seqlock_irq variant.
7871 *
7872 * When looking for a bad block we specify a range and want to
7873 * know if any block in the range is bad. So we binary-search
7874 * to the last range that starts at-or-before the given endpoint,
7875 * (or "before the sector after the target range")
7876 * then see if it ends after the given start.
7877 * We return
7878 * 0 if there are no known bad blocks in the range
7879 * 1 if there are known bad block which are all acknowledged
7880 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7881 * plus the start/length of the first bad section we overlap.
7882 */
7883int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7884 sector_t *first_bad, int *bad_sectors)
7885{
7886 int hi;
majianpengab056132012-11-06 17:13:44 +08007887 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10007888 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08007889 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10007890 sector_t target = s + sectors;
7891 unsigned seq;
7892
7893 if (bb->shift > 0) {
7894 /* round the start down, and the end up */
7895 s >>= bb->shift;
7896 target += (1<<bb->shift) - 1;
7897 target >>= bb->shift;
7898 sectors = target - s;
7899 }
7900 /* 'target' is now the first block after the bad range */
7901
7902retry:
7903 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08007904 lo = 0;
7905 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007906 hi = bb->count;
7907
7908 /* Binary search between lo and hi for 'target'
7909 * i.e. for the last range that starts before 'target'
7910 */
7911 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7912 * are known not to be the last range before target.
7913 * VARIANT: hi-lo is the number of possible
7914 * ranges, and decreases until it reaches 1
7915 */
7916 while (hi - lo > 1) {
7917 int mid = (lo + hi) / 2;
7918 sector_t a = BB_OFFSET(p[mid]);
7919 if (a < target)
7920 /* This could still be the one, earlier ranges
7921 * could not. */
7922 lo = mid;
7923 else
7924 /* This and later ranges are definitely out. */
7925 hi = mid;
7926 }
7927 /* 'lo' might be the last that started before target, but 'hi' isn't */
7928 if (hi > lo) {
7929 /* need to check all range that end after 's' to see if
7930 * any are unacknowledged.
7931 */
7932 while (lo >= 0 &&
7933 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7934 if (BB_OFFSET(p[lo]) < target) {
7935 /* starts before the end, and finishes after
7936 * the start, so they must overlap
7937 */
7938 if (rv != -1 && BB_ACK(p[lo]))
7939 rv = 1;
7940 else
7941 rv = -1;
7942 *first_bad = BB_OFFSET(p[lo]);
7943 *bad_sectors = BB_LEN(p[lo]);
7944 }
7945 lo--;
7946 }
7947 }
7948
7949 if (read_seqretry(&bb->lock, seq))
7950 goto retry;
7951
7952 return rv;
7953}
7954EXPORT_SYMBOL_GPL(md_is_badblock);
7955
7956/*
7957 * Add a range of bad blocks to the table.
7958 * This might extend the table, or might contract it
7959 * if two adjacent ranges can be merged.
7960 * We binary-search to find the 'insertion' point, then
7961 * decide how best to handle it.
7962 */
7963static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7964 int acknowledged)
7965{
7966 u64 *p;
7967 int lo, hi;
7968 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04007969 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10007970
7971 if (bb->shift < 0)
7972 /* badblocks are disabled */
7973 return 0;
7974
7975 if (bb->shift) {
7976 /* round the start down, and the end up */
7977 sector_t next = s + sectors;
7978 s >>= bb->shift;
7979 next += (1<<bb->shift) - 1;
7980 next >>= bb->shift;
7981 sectors = next - s;
7982 }
7983
Bian Yu905b0292013-10-12 01:10:03 -04007984 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10007985
7986 p = bb->page;
7987 lo = 0;
7988 hi = bb->count;
7989 /* Find the last range that starts at-or-before 's' */
7990 while (hi - lo > 1) {
7991 int mid = (lo + hi) / 2;
7992 sector_t a = BB_OFFSET(p[mid]);
7993 if (a <= s)
7994 lo = mid;
7995 else
7996 hi = mid;
7997 }
7998 if (hi > lo && BB_OFFSET(p[lo]) > s)
7999 hi = lo;
8000
8001 if (hi > lo) {
8002 /* we found a range that might merge with the start
8003 * of our new range
8004 */
8005 sector_t a = BB_OFFSET(p[lo]);
8006 sector_t e = a + BB_LEN(p[lo]);
8007 int ack = BB_ACK(p[lo]);
8008 if (e >= s) {
8009 /* Yes, we can merge with a previous range */
8010 if (s == a && s + sectors >= e)
8011 /* new range covers old */
8012 ack = acknowledged;
8013 else
8014 ack = ack && acknowledged;
8015
8016 if (e < s + sectors)
8017 e = s + sectors;
8018 if (e - a <= BB_MAX_LEN) {
8019 p[lo] = BB_MAKE(a, e-a, ack);
8020 s = e;
8021 } else {
8022 /* does not all fit in one range,
8023 * make p[lo] maximal
8024 */
8025 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8026 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8027 s = a + BB_MAX_LEN;
8028 }
8029 sectors = e - s;
8030 }
8031 }
8032 if (sectors && hi < bb->count) {
8033 /* 'hi' points to the first range that starts after 's'.
8034 * Maybe we can merge with the start of that range */
8035 sector_t a = BB_OFFSET(p[hi]);
8036 sector_t e = a + BB_LEN(p[hi]);
8037 int ack = BB_ACK(p[hi]);
8038 if (a <= s + sectors) {
8039 /* merging is possible */
8040 if (e <= s + sectors) {
8041 /* full overlap */
8042 e = s + sectors;
8043 ack = acknowledged;
8044 } else
8045 ack = ack && acknowledged;
8046
8047 a = s;
8048 if (e - a <= BB_MAX_LEN) {
8049 p[hi] = BB_MAKE(a, e-a, ack);
8050 s = e;
8051 } else {
8052 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8053 s = a + BB_MAX_LEN;
8054 }
8055 sectors = e - s;
8056 lo = hi;
8057 hi++;
8058 }
8059 }
8060 if (sectors == 0 && hi < bb->count) {
8061 /* we might be able to combine lo and hi */
8062 /* Note: 's' is at the end of 'lo' */
8063 sector_t a = BB_OFFSET(p[hi]);
8064 int lolen = BB_LEN(p[lo]);
8065 int hilen = BB_LEN(p[hi]);
8066 int newlen = lolen + hilen - (s - a);
8067 if (s >= a && newlen < BB_MAX_LEN) {
8068 /* yes, we can combine them */
8069 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8070 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8071 memmove(p + hi, p + hi + 1,
8072 (bb->count - hi - 1) * 8);
8073 bb->count--;
8074 }
8075 }
8076 while (sectors) {
8077 /* didn't merge (it all).
8078 * Need to add a range just before 'hi' */
8079 if (bb->count >= MD_MAX_BADBLOCKS) {
8080 /* No room for more */
8081 rv = 0;
8082 break;
8083 } else {
8084 int this_sectors = sectors;
8085 memmove(p + hi + 1, p + hi,
8086 (bb->count - hi) * 8);
8087 bb->count++;
8088
8089 if (this_sectors > BB_MAX_LEN)
8090 this_sectors = BB_MAX_LEN;
8091 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8092 sectors -= this_sectors;
8093 s += this_sectors;
8094 }
8095 }
8096
8097 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008098 if (!acknowledged)
8099 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008100 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008101
8102 return rv;
8103}
8104
NeilBrown3cb03002011-10-11 16:45:26 +11008105int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008106 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008107{
NeilBrownc6563a82012-05-21 09:27:00 +10008108 int rv;
8109 if (is_new)
8110 s += rdev->new_data_offset;
8111 else
8112 s += rdev->data_offset;
8113 rv = md_set_badblocks(&rdev->badblocks,
8114 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008115 if (rv) {
8116 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008117 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008118 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8119 md_wakeup_thread(rdev->mddev->thread);
8120 }
8121 return rv;
8122}
8123EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8124
8125/*
8126 * Remove a range of bad blocks from the table.
8127 * This may involve extending the table if we spilt a region,
8128 * but it must not fail. So if the table becomes full, we just
8129 * drop the remove request.
8130 */
8131static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8132{
8133 u64 *p;
8134 int lo, hi;
8135 sector_t target = s + sectors;
8136 int rv = 0;
8137
8138 if (bb->shift > 0) {
8139 /* When clearing we round the start up and the end down.
8140 * This should not matter as the shift should align with
8141 * the block size and no rounding should ever be needed.
8142 * However it is better the think a block is bad when it
8143 * isn't than to think a block is not bad when it is.
8144 */
8145 s += (1<<bb->shift) - 1;
8146 s >>= bb->shift;
8147 target >>= bb->shift;
8148 sectors = target - s;
8149 }
8150
8151 write_seqlock_irq(&bb->lock);
8152
8153 p = bb->page;
8154 lo = 0;
8155 hi = bb->count;
8156 /* Find the last range that starts before 'target' */
8157 while (hi - lo > 1) {
8158 int mid = (lo + hi) / 2;
8159 sector_t a = BB_OFFSET(p[mid]);
8160 if (a < target)
8161 lo = mid;
8162 else
8163 hi = mid;
8164 }
8165 if (hi > lo) {
8166 /* p[lo] is the last range that could overlap the
8167 * current range. Earlier ranges could also overlap,
8168 * but only this one can overlap the end of the range.
8169 */
8170 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8171 /* Partial overlap, leave the tail of this range */
8172 int ack = BB_ACK(p[lo]);
8173 sector_t a = BB_OFFSET(p[lo]);
8174 sector_t end = a + BB_LEN(p[lo]);
8175
8176 if (a < s) {
8177 /* we need to split this range */
8178 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008179 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008180 goto out;
8181 }
8182 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8183 bb->count++;
8184 p[lo] = BB_MAKE(a, s-a, ack);
8185 lo++;
8186 }
8187 p[lo] = BB_MAKE(target, end - target, ack);
8188 /* there is no longer an overlap */
8189 hi = lo;
8190 lo--;
8191 }
8192 while (lo >= 0 &&
8193 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8194 /* This range does overlap */
8195 if (BB_OFFSET(p[lo]) < s) {
8196 /* Keep the early parts of this range. */
8197 int ack = BB_ACK(p[lo]);
8198 sector_t start = BB_OFFSET(p[lo]);
8199 p[lo] = BB_MAKE(start, s - start, ack);
8200 /* now low doesn't overlap, so.. */
8201 break;
8202 }
8203 lo--;
8204 }
8205 /* 'lo' is strictly before, 'hi' is strictly after,
8206 * anything between needs to be discarded
8207 */
8208 if (hi - lo > 1) {
8209 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8210 bb->count -= (hi - lo - 1);
8211 }
8212 }
8213
8214 bb->changed = 1;
8215out:
8216 write_sequnlock_irq(&bb->lock);
8217 return rv;
8218}
8219
NeilBrownc6563a82012-05-21 09:27:00 +10008220int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8221 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008222{
NeilBrownc6563a82012-05-21 09:27:00 +10008223 if (is_new)
8224 s += rdev->new_data_offset;
8225 else
8226 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008227 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008228 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008229}
8230EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8231
8232/*
8233 * Acknowledge all bad blocks in a list.
8234 * This only succeeds if ->changed is clear. It is used by
8235 * in-kernel metadata updates
8236 */
8237void md_ack_all_badblocks(struct badblocks *bb)
8238{
8239 if (bb->page == NULL || bb->changed)
8240 /* no point even trying */
8241 return;
8242 write_seqlock_irq(&bb->lock);
8243
majianpengecb178b2012-03-19 12:46:42 +11008244 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008245 u64 *p = bb->page;
8246 int i;
8247 for (i = 0; i < bb->count ; i++) {
8248 if (!BB_ACK(p[i])) {
8249 sector_t start = BB_OFFSET(p[i]);
8250 int len = BB_LEN(p[i]);
8251 p[i] = BB_MAKE(start, len, 1);
8252 }
8253 }
NeilBrownde393cd2011-07-28 11:31:48 +10008254 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008255 }
8256 write_sequnlock_irq(&bb->lock);
8257}
8258EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8259
NeilBrown16c791a2011-07-28 11:31:47 +10008260/* sysfs access to bad-blocks list.
8261 * We present two files.
8262 * 'bad-blocks' lists sector numbers and lengths of ranges that
8263 * are recorded as bad. The list is truncated to fit within
8264 * the one-page limit of sysfs.
8265 * Writing "sector length" to this file adds an acknowledged
8266 * bad block list.
8267 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8268 * been acknowledged. Writing to this file adds bad blocks
8269 * without acknowledging them. This is largely for testing.
8270 */
8271
8272static ssize_t
8273badblocks_show(struct badblocks *bb, char *page, int unack)
8274{
8275 size_t len;
8276 int i;
8277 u64 *p = bb->page;
8278 unsigned seq;
8279
8280 if (bb->shift < 0)
8281 return 0;
8282
8283retry:
8284 seq = read_seqbegin(&bb->lock);
8285
8286 len = 0;
8287 i = 0;
8288
8289 while (len < PAGE_SIZE && i < bb->count) {
8290 sector_t s = BB_OFFSET(p[i]);
8291 unsigned int length = BB_LEN(p[i]);
8292 int ack = BB_ACK(p[i]);
8293 i++;
8294
8295 if (unack && ack)
8296 continue;
8297
8298 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8299 (unsigned long long)s << bb->shift,
8300 length << bb->shift);
8301 }
NeilBrownde393cd2011-07-28 11:31:48 +10008302 if (unack && len == 0)
8303 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008304
8305 if (read_seqretry(&bb->lock, seq))
8306 goto retry;
8307
8308 return len;
8309}
8310
8311#define DO_DEBUG 1
8312
8313static ssize_t
8314badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8315{
8316 unsigned long long sector;
8317 int length;
8318 char newline;
8319#ifdef DO_DEBUG
8320 /* Allow clearing via sysfs *only* for testing/debugging.
8321 * Normally only a successful write may clear a badblock
8322 */
8323 int clear = 0;
8324 if (page[0] == '-') {
8325 clear = 1;
8326 page++;
8327 }
8328#endif /* DO_DEBUG */
8329
8330 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8331 case 3:
8332 if (newline != '\n')
8333 return -EINVAL;
8334 case 2:
8335 if (length <= 0)
8336 return -EINVAL;
8337 break;
8338 default:
8339 return -EINVAL;
8340 }
8341
8342#ifdef DO_DEBUG
8343 if (clear) {
8344 md_clear_badblocks(bb, sector, length);
8345 return len;
8346 }
8347#endif /* DO_DEBUG */
8348 if (md_set_badblocks(bb, sector, length, !unack))
8349 return len;
8350 else
8351 return -ENOSPC;
8352}
8353
Adrian Bunk75c96f82005-05-05 16:16:09 -07008354static int md_notify_reboot(struct notifier_block *this,
8355 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356{
8357 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008358 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008359 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360
NeilBrownc744a652012-03-19 12:46:37 +11008361 for_each_mddev(mddev, tmp) {
8362 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008363 if (mddev->pers)
8364 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008365 if (mddev->persistent)
8366 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008367 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008368 }
NeilBrownc744a652012-03-19 12:46:37 +11008369 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370 }
NeilBrownc744a652012-03-19 12:46:37 +11008371 /*
8372 * certain more exotic SCSI devices are known to be
8373 * volatile wrt too early system reboots. While the
8374 * right place to handle this issue is the given
8375 * driver, we do want to have a safe RAID driver ...
8376 */
8377 if (need_delay)
8378 mdelay(1000*1);
8379
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 return NOTIFY_DONE;
8381}
8382
Adrian Bunk75c96f82005-05-05 16:16:09 -07008383static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 .notifier_call = md_notify_reboot,
8385 .next = NULL,
8386 .priority = INT_MAX, /* before any real devices */
8387};
8388
8389static void md_geninit(void)
8390{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008391 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008393 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394}
8395
Adrian Bunk75c96f82005-05-05 16:16:09 -07008396static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397{
Tejun Heoe804ac72010-10-15 15:36:08 +02008398 int ret = -ENOMEM;
8399
Tejun Heoada609e2011-01-25 14:35:54 +01008400 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008401 if (!md_wq)
8402 goto err_wq;
8403
8404 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8405 if (!md_misc_wq)
8406 goto err_misc_wq;
8407
8408 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8409 goto err_md;
8410
8411 if ((ret = register_blkdev(0, "mdp")) < 0)
8412 goto err_mdp;
8413 mdp_major = ret;
8414
NeilBrownaf5628f2014-07-31 13:54:54 +10008415 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008416 md_probe, NULL, NULL);
8417 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 md_probe, NULL, NULL);
8419
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008421 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422
8423 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425
Tejun Heoe804ac72010-10-15 15:36:08 +02008426err_mdp:
8427 unregister_blkdev(MD_MAJOR, "md");
8428err_md:
8429 destroy_workqueue(md_misc_wq);
8430err_misc_wq:
8431 destroy_workqueue(md_wq);
8432err_wq:
8433 return ret;
8434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435
8436#ifndef MODULE
8437
8438/*
8439 * Searches all registered partitions for autorun RAID arrays
8440 * at boot time.
8441 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008442
8443static LIST_HEAD(all_detected_devices);
8444struct detected_devices_node {
8445 struct list_head list;
8446 dev_t dev;
8447};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
8449void md_autodetect_dev(dev_t dev)
8450{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008451 struct detected_devices_node *node_detected_dev;
8452
8453 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8454 if (node_detected_dev) {
8455 node_detected_dev->dev = dev;
8456 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8457 } else {
8458 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8459 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461}
8462
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463static void autostart_arrays(int part)
8464{
NeilBrown3cb03002011-10-11 16:45:26 +11008465 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008466 struct detected_devices_node *node_detected_dev;
8467 dev_t dev;
8468 int i_scanned, i_passed;
8469
8470 i_scanned = 0;
8471 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472
8473 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8474
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008475 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8476 i_scanned++;
8477 node_detected_dev = list_entry(all_detected_devices.next,
8478 struct detected_devices_node, list);
8479 list_del(&node_detected_dev->list);
8480 dev = node_detected_dev->dev;
8481 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008482 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483 if (IS_ERR(rdev))
8484 continue;
8485
NeilBrown403df472014-09-30 15:52:29 +10008486 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 continue;
NeilBrown403df472014-09-30 15:52:29 +10008488
NeilBrownd0fae182008-03-04 14:29:31 -08008489 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008491 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008492 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008493
8494 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8495 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496
8497 autorun_devices(part);
8498}
8499
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008500#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501
8502static __exit void md_exit(void)
8503{
NeilBrownfd01b882011-10-11 16:47:53 +11008504 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008506 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008507
NeilBrownaf5628f2014-07-31 13:54:54 +10008508 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008509 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008511 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 unregister_blkdev(mdp_major, "mdp");
8513 unregister_reboot_notifier(&md_notifier);
8514 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008515
8516 /* We cannot unload the modules while some process is
8517 * waiting for us in select() or poll() - wake them up
8518 */
8519 md_unloading = 1;
8520 while (waitqueue_active(&md_event_waiters)) {
8521 /* not safe to leave yet */
8522 wake_up(&md_event_waiters);
8523 msleep(delay);
8524 delay += delay;
8525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008527
NeilBrown29ac4aa2008-02-06 01:39:58 -08008528 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008530 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008532 destroy_workqueue(md_misc_wq);
8533 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534}
8535
Dan Williams685784a2007-07-09 11:56:42 -07008536subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537module_exit(md_exit)
8538
NeilBrownf91de922005-11-08 21:39:36 -08008539static int get_ro(char *buffer, struct kernel_param *kp)
8540{
8541 return sprintf(buffer, "%d", start_readonly);
8542}
8543static int set_ro(const char *val, struct kernel_param *kp)
8544{
8545 char *e;
8546 int num = simple_strtoul(val, &e, 10);
8547 if (*val && (*e == '\0' || *e == '\n')) {
8548 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008549 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008550 }
8551 return -EINVAL;
8552}
8553
NeilBrown80ca3a42006-07-10 04:44:18 -07008554module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8555module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11008556module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008557
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008559MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008560MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008561MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);