blob: d96aa809473662997b764c1c95b8fa6df8ef4596 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Kent Overstreet4f024f32013-10-11 15:44:27 -0700396 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 /* an empty barrier - all done */
398 bio_endio(bio, 0);
399 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200401 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 }
NeilBrown2b74e122010-12-09 15:59:01 +1100403
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
NeilBrownfd01b882011-10-11 16:47:53 +1100408void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100413 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 spin_unlock_irq(&mddev->write_lock);
416
NeilBrowna035fc32010-12-09 16:17:51 +1100417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
Tejun Heoe9c74692010-09-03 11:56:18 +0200420EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100421
NeilBrown74018dc2012-07-31 09:08:15 +0200422void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000423{
NeilBrown9cbb1752012-07-31 09:08:14 +0200424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
426 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000427}
NeilBrown9cbb1752012-07-31 09:08:14 +0200428EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000429
NeilBrownfd01b882011-10-11 16:47:53 +1100430static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 atomic_inc(&mddev->active);
433 return mddev;
434}
435
Dan Williams5fd3a172009-03-04 00:57:25 -0700436static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100437
NeilBrownfd01b882011-10-11 16:47:53 +1100438static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
NeilBrowna167f662010-10-26 18:31:13 +1100440 struct bio_set *bs = NULL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443 return;
NeilBrownd3374822009-01-09 08:31:10 +1100444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
447 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100448 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100449 bs = mddev->bio_set;
450 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100451 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100456 */
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200458 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100459 } else
460 kfree(mddev);
461 }
462 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100463 if (bs)
464 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrownfd01b882011-10-11 16:47:53 +1100467void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000483 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500484 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
488}
NeilBrown390ee602010-06-01 19:37:27 +1000489EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490
NeilBrownfd01b882011-10-11 16:47:53 +1100491static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownfd01b882011-10-11 16:47:53 +1100493 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown8f5f02c2011-02-16 13:58:51 +1100495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retry:
499 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrownefeb53c2009-01-09 08:31:10 +1100501 if (unit) {
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
504 mddev_get(mddev);
505 spin_unlock(&all_mddevs_lock);
506 kfree(new);
507 return mddev;
508 }
509
510 if (new) {
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
514 return new;
515 }
516 } else if (new) {
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
520 int is_free = 0;
521 int dev = 0;
522 while (!is_free) {
523 dev = MKDEV(MD_MAJOR, next_minor);
524 next_minor++;
525 if (next_minor > MINORMASK)
526 next_minor = 0;
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return NULL;
532 }
533
534 is_free = 1;
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
537 is_free = 0;
538 break;
539 }
540 }
541 new->unit = dev;
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 return new;
547 }
548 spin_unlock(&all_mddevs_lock);
549
NeilBrown9ffae0c2006-01-06 00:20:32 -0800550 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!new)
552 return NULL;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new->unit = unit;
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
557 else
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
NeilBrownfafd7fb2010-04-01 15:55:30 +1100560 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto retry;
563}
564
NeilBrown29f097c2013-11-14 17:54:51 +1100565static inline int __must_check mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
NeilBrown29f097c2013-11-14 17:54:51 +1100570/* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
572 */
573static inline void mddev_lock_nointr(struct mddev * mddev)
574{
575 mutex_lock(&mddev->reconfig_mutex);
576}
577
NeilBrownfd01b882011-10-11 16:47:53 +1100578static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100579{
580 return mutex_is_locked(&mddev->reconfig_mutex);
581}
582
NeilBrownfd01b882011-10-11 16:47:53 +1100583static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800585 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrownb6eb1272010-04-15 10:13:47 +1000588static struct attribute_group md_redundancy_group;
589
NeilBrownfd01b882011-10-11 16:47:53 +1100590static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
NeilBrowna64c8762010-04-14 17:15:37 +1000592 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
596 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
603 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 */
NeilBrowna64c8762010-04-14 17:15:37 +1000605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 mutex_unlock(&mddev->reconfig_mutex);
609
NeilBrown00bcb4a2010-06-01 19:37:23 +1000610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
619 }
NeilBrowna64c8762010-04-14 17:15:37 +1000620 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000621 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000622 } else
623 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chris Dunlop751e67c2011-10-19 16:48:26 +1100625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000627 */
628 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700629 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000630 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrown1ca69c42012-10-11 13:37:33 +1100633static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
634{
635 struct md_rdev *rdev;
636
637 rdev_for_each_rcu(rdev, mddev)
638 if (rdev->desc_nr == nr)
639 return rdev;
640
641 return NULL;
642}
643
644static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
NeilBrown3cb03002011-10-11 16:45:26 +1100646 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
NeilBrowndafb20f2012-03-19 12:46:39 +1100648 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (rdev->bdev->bd_dev == dev)
650 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return NULL;
653}
654
NeilBrown1ca69c42012-10-11 13:37:33 +1100655static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
656{
657 struct md_rdev *rdev;
658
659 rdev_for_each_rcu(rdev, mddev)
660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
662
663 return NULL;
664}
665
NeilBrown84fc4b52011-10-11 16:49:58 +1100666static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800667{
NeilBrown84fc4b52011-10-11 16:49:58 +1100668 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800669 list_for_each_entry(pers, &pers_list, list) {
670 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800671 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800672 if (strcmp(pers->name, clevel)==0)
673 return pers;
674 }
NeilBrown2604b702006-01-06 00:20:36 -0800675 return NULL;
676}
677
Andre Nollb73df2d2008-07-11 22:02:23 +1000678/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100679static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100681 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000682 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
NeilBrown3cb03002011-10-11 16:45:26 +1100685static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 if (rdev->sb_page)
688 MD_BUG();
689
690 rdev->sb_page = alloc_page(GFP_KERNEL);
691 if (!rdev->sb_page) {
692 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
696 return 0;
697}
698
NeilBrown545c8792012-05-22 13:54:30 +1000699void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800702 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 rdev->sb_loaded = 0;
704 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000705 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100706 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
NeilBrown2699b672011-07-28 11:31:47 +1000708 if (rdev->bb_page) {
709 put_page(rdev->bb_page);
710 rdev->bb_page = NULL;
711 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000712 kfree(rdev->badblocks.page);
713 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
NeilBrown545c8792012-05-22 13:54:30 +1000715EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
NeilBrown6712ecf2007-09-27 12:47:43 +0200717static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718{
NeilBrown3cb03002011-10-11 16:45:26 +1100719 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100720 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700721
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700722 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
723 printk("md: super_written gets error=%d, uptodate=%d\n",
724 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
725 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800726 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700727 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700728
NeilBrowna9701a32005-11-08 21:39:34 -0800729 if (atomic_dec_and_test(&mddev->pending_writes))
730 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700731 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732}
733
NeilBrownfd01b882011-10-11 16:47:53 +1100734void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735 sector_t sector, int size, struct page *page)
736{
737 /* write first size bytes of page to sector of rdev
738 * Increment mddev->pending_writes before returning
739 * and decrement it on completion, waking up sb_wait
740 * if zero is reached.
741 * If an error occurred, call md_error
742 */
NeilBrowna167f662010-10-26 18:31:13 +1100743 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100745 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700746 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700747 bio_add_page(bio, page, size, 0);
748 bio->bi_private = rdev;
749 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800750
NeilBrown7bfa19f2005-06-21 17:17:28 -0700751 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000752 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800753}
754
NeilBrownfd01b882011-10-11 16:47:53 +1100755void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800756{
Tejun Heoe9c74692010-09-03 11:56:18 +0200757 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000758 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700759}
760
NeilBrown3cb03002011-10-11 16:45:26 +1100761int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100762 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
NeilBrowna167f662010-10-26 18:31:13 +1100764 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int ret;
766
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100767 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
768 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100769 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700770 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000771 else if (rdev->mddev->reshape_position != MaxSector &&
772 (rdev->mddev->reshape_backwards ==
773 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700774 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100775 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700776 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700778 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
781 bio_put(bio);
782 return ret;
783}
NeilBrowna8745db2006-01-06 00:20:34 -0800784EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
NeilBrown3cb03002011-10-11 16:45:26 +1100786static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 char b[BDEVNAME_SIZE];
789 if (!rdev->sb_page) {
790 MD_BUG();
791 return -EINVAL;
792 }
793 if (rdev->sb_loaded)
794 return 0;
795
796
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100797 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto fail;
799 rdev->sb_loaded = 1;
800 return 0;
801
802fail:
803 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
804 bdevname(rdev->bdev,b));
805 return -EINVAL;
806}
807
808static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
809{
Andre Noll05710462008-07-11 22:02:20 +1000810 return sb1->set_uuid0 == sb2->set_uuid0 &&
811 sb1->set_uuid1 == sb2->set_uuid1 &&
812 sb1->set_uuid2 == sb2->set_uuid2 &&
813 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
817{
818 int ret;
819 mdp_super_t *tmp1, *tmp2;
820
821 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
822 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
823
824 if (!tmp1 || !tmp2) {
825 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100826 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto abort;
828 }
829
830 *tmp1 = *sb1;
831 *tmp2 = *sb2;
832
833 /*
834 * nr_disks is not constant
835 */
836 tmp1->nr_disks = 0;
837 tmp2->nr_disks = 0;
838
Andre Nollce0c8e02008-07-11 22:02:20 +1000839 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700841 kfree(tmp1);
842 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret;
844}
845
NeilBrown4d167f02007-05-09 02:35:37 -0700846
847static u32 md_csum_fold(u32 csum)
848{
849 csum = (csum & 0xffff) + (csum >> 16);
850 return (csum & 0xffff) + (csum >> 16);
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static unsigned int calc_sb_csum(mdp_super_t * sb)
854{
NeilBrown4d167f02007-05-09 02:35:37 -0700855 u64 newcsum = 0;
856 u32 *sb32 = (u32*)sb;
857 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 unsigned int disk_csum, csum;
859
860 disk_csum = sb->sb_csum;
861 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700862
863 for (i = 0; i < MD_SB_BYTES/4 ; i++)
864 newcsum += sb32[i];
865 csum = (newcsum & 0xffffffff) + (newcsum>>32);
866
867
868#ifdef CONFIG_ALPHA
869 /* This used to use csum_partial, which was wrong for several
870 * reasons including that different results are returned on
871 * different architectures. It isn't critical that we get exactly
872 * the same return value as before (we always csum_fold before
873 * testing, and that removes any differences). However as we
874 * know that csum_partial always returned a 16bit value on
875 * alphas, do a fold to maximise conformity to previous behaviour.
876 */
877 sb->sb_csum = md_csum_fold(disk_csum);
878#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return csum;
882}
883
884
885/*
886 * Handle superblock details.
887 * We want to be able to handle multiple superblock formats
888 * so we have a common interface to them all, and an array of
889 * different handlers.
890 * We rely on user-space to write the initial superblock, and support
891 * reading and updating of superblocks.
892 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100893 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * loads and validates a superblock on dev.
895 * if refdev != NULL, compare superblocks on both devices
896 * Return:
897 * 0 - dev has a superblock that is compatible with refdev
898 * 1 - dev has a superblock that is compatible and newer than refdev
899 * so dev should be used as the refdev in future
900 * -EINVAL superblock incompatible or invalid
901 * -othererror e.g. -EIO
902 *
NeilBrownfd01b882011-10-11 16:47:53 +1100903 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Verify that dev is acceptable into mddev.
905 * The first time, mddev->raid_disks will be 0, and data from
906 * dev should be merged in. Subsequent calls check that dev
907 * is new enough. Return 0 or -EINVAL
908 *
NeilBrownfd01b882011-10-11 16:47:53 +1100909 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * Update the superblock for rdev with data in mddev
911 * This does not write to disc.
912 *
913 */
914
915struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000916 char *name;
917 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000918 int (*load_super)(struct md_rdev *rdev,
919 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000920 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000921 int (*validate_super)(struct mddev *mddev,
922 struct md_rdev *rdev);
923 void (*sync_super)(struct mddev *mddev,
924 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100925 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000926 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000927 int (*allow_new_offset)(struct md_rdev *rdev,
928 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930
931/*
Andre Noll0894cc32009-06-18 08:49:23 +1000932 * Check that the given mddev has no bitmap.
933 *
934 * This function is called from the run method of all personalities that do not
935 * support bitmaps. It prints an error message and returns non-zero if mddev
936 * has a bitmap. Otherwise, it returns 0.
937 *
938 */
NeilBrownfd01b882011-10-11 16:47:53 +1100939int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000940{
NeilBrownc3d97142009-12-14 12:49:52 +1100941 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000942 return 0;
943 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
944 mdname(mddev), mddev->pers->name);
945 return 1;
946}
947EXPORT_SYMBOL(md_check_no_bitmap);
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * load_super for 0.90.0
951 */
NeilBrown3cb03002011-10-11 16:45:26 +1100952static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
955 mdp_super_t *sb;
956 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /*
Andre Noll0f420352008-07-11 22:02:23 +1000959 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * it's at the end of the disk.
961 *
962 * It also happens to be a multiple of 4Kb.
963 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100964 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
NeilBrown0002b272005-09-09 16:23:53 -0700966 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ret) return ret;
968
969 ret = -EINVAL;
970
971 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000972 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 if (sb->md_magic != MD_SB_MAGIC) {
975 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
976 b);
977 goto abort;
978 }
979
980 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800981 sb->minor_version < 90 ||
982 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
984 sb->major_version, sb->minor_version,
985 b);
986 goto abort;
987 }
988
989 if (sb->raid_disks <= 0)
990 goto abort;
991
NeilBrown4d167f02007-05-09 02:35:37 -0700992 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
994 b);
995 goto abort;
996 }
997
998 rdev->preferred_minor = sb->md_minor;
999 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001000 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001001 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001002 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (sb->level == LEVEL_MULTIPATH)
1005 rdev->desc_nr = -1;
1006 else
1007 rdev->desc_nr = sb->this_disk.number;
1008
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001009 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001011 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001013 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!uuid_equal(refsb, sb)) {
1015 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1016 b, bdevname(refdev->bdev,b2));
1017 goto abort;
1018 }
1019 if (!sb_equal(refsb, sb)) {
1020 printk(KERN_WARNING "md: %s has same UUID"
1021 " but different superblock to %s\n",
1022 b, bdevname(refdev->bdev, b2));
1023 goto abort;
1024 }
1025 ev1 = md_event(sb);
1026 ev2 = md_event(refsb);
1027 if (ev1 > ev2)
1028 ret = 1;
1029 else
1030 ret = 0;
1031 }
NeilBrown8190e752009-06-18 08:48:58 +10001032 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001033 /* Limit to 4TB as metadata cannot record more than that.
1034 * (not needed for Linear and RAID0 as metadata doesn't
1035 * record this size)
1036 */
1037 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001038 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
NeilBrown27a7b262011-09-10 17:21:28 +10001040 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001041 /* "this cannot possibly happen" ... */
1042 ret = -EINVAL;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 abort:
1045 return ret;
1046}
1047
1048/*
1049 * validate_super for 0.90.0
1050 */
NeilBrownfd01b882011-10-11 16:47:53 +11001051static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001054 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001055 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
NeilBrown41158c72005-06-21 17:17:25 -07001057 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001058 clear_bit(Faulty, &rdev->flags);
1059 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001060 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001061 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (mddev->raid_disks == 0) {
1064 mddev->major_version = 0;
1065 mddev->minor_version = sb->minor_version;
1066 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001067 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001068 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mddev->ctime = sb->ctime;
1070 mddev->utime = sb->utime;
1071 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001072 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 mddev->layout = sb->layout;
1074 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001075 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001076 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001077 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001078 mddev->bitmap_info.space = 0;
1079 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001080 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001081 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001082 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
NeilBrownf6705572006-03-27 01:18:11 -08001084 if (mddev->minor_version >= 91) {
1085 mddev->reshape_position = sb->reshape_position;
1086 mddev->delta_disks = sb->delta_disks;
1087 mddev->new_level = sb->new_level;
1088 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001089 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001090 if (mddev->delta_disks < 0)
1091 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001092 } else {
1093 mddev->reshape_position = MaxSector;
1094 mddev->delta_disks = 0;
1095 mddev->new_level = mddev->level;
1096 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001097 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001098 }
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (sb->state & (1<<MD_SB_CLEAN))
1101 mddev->recovery_cp = MaxSector;
1102 else {
1103 if (sb->events_hi == sb->cp_events_hi &&
1104 sb->events_lo == sb->cp_events_lo) {
1105 mddev->recovery_cp = sb->recovery_cp;
1106 } else
1107 mddev->recovery_cp = 0;
1108 }
1109
1110 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1111 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1112 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1113 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1114
1115 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001116
1117 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001118 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001119 mddev->bitmap_info.offset =
1120 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001121 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001122 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001123 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001124
NeilBrown41158c72005-06-21 17:17:25 -07001125 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001126 /* Insist on good event counter while assembling, except
1127 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001129 if (sb->disks[rdev->desc_nr].state & (
1130 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1131 if (ev1 < mddev->events)
1132 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001133 } else if (mddev->bitmap) {
1134 /* if adding to array with a bitmap, then we can accept an
1135 * older device ... but not too old.
1136 */
NeilBrown41158c72005-06-21 17:17:25 -07001137 if (ev1 < mddev->bitmap->events_cleared)
1138 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001139 if (ev1 < mddev->events)
1140 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001141 } else {
1142 if (ev1 < mddev->events)
1143 /* just a hot-add of a new device, leave raid_disk at -1 */
1144 return 0;
1145 }
NeilBrown41158c72005-06-21 17:17:25 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 desc = sb->disks + rdev->desc_nr;
1149
1150 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001151 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001152 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1153 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001154 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001156 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001157 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1158 /* active but not in sync implies recovery up to
1159 * reshape position. We don't know exactly where
1160 * that is, so set to zero for now */
1161 if (mddev->minor_version >= 91) {
1162 rdev->recovery_offset = 0;
1163 rdev->raid_disk = desc->raid_disk;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001166 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1167 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001168 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171}
1172
1173/*
1174 * sync_super for 0.90.0
1175 */
NeilBrownfd01b882011-10-11 16:47:53 +11001176static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001179 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* make rdev->sb match mddev data..
1184 *
1185 * 1/ zero out disks
1186 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1187 * 3/ any empty disks < next_spare become removed
1188 *
1189 * disks[0] gets initialised to REMOVED because
1190 * we cannot be sure from other fields if it has
1191 * been initialised or not.
1192 */
1193 int i;
1194 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1195
NeilBrown61181562005-09-09 16:24:02 -07001196 rdev->sb_size = MD_SB_BYTES;
1197
Namhyung Kim65a06f062011-07-27 11:00:36 +10001198 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 memset(sb, 0, sizeof(*sb));
1201
1202 sb->md_magic = MD_SB_MAGIC;
1203 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->patch_version = mddev->patch_version;
1205 sb->gvalid_words = 0; /* ignored */
1206 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1207 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1208 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1209 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1210
1211 sb->ctime = mddev->ctime;
1212 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001213 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sb->raid_disks = mddev->raid_disks;
1215 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001216 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->utime = mddev->utime;
1218 sb->state = 0;
1219 sb->events_hi = (mddev->events>>32);
1220 sb->events_lo = (u32)mddev->events;
1221
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position == MaxSector)
1223 sb->minor_version = 90;
1224 else {
1225 sb->minor_version = 91;
1226 sb->reshape_position = mddev->reshape_position;
1227 sb->new_level = mddev->new_level;
1228 sb->delta_disks = mddev->delta_disks;
1229 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001230 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001231 }
1232 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->in_sync)
1234 {
1235 sb->recovery_cp = mddev->recovery_cp;
1236 sb->cp_events_hi = (mddev->events>>32);
1237 sb->cp_events_lo = (u32)mddev->events;
1238 if (mddev->recovery_cp == MaxSector)
1239 sb->state = (1<< MD_SB_CLEAN);
1240 } else
1241 sb->recovery_cp = 0;
1242
1243 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001244 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownc3d97142009-12-14 12:49:52 +11001246 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001247 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001250 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 int is_active = test_bit(In_sync, &rdev2->flags);
1254
1255 if (rdev2->raid_disk >= 0 &&
1256 sb->minor_version >= 91)
1257 /* we have nowhere to store the recovery_offset,
1258 * but if it is not below the reshape_position,
1259 * we can piggy-back on that.
1260 */
1261 is_active = 1;
1262 if (rdev2->raid_disk < 0 ||
1263 test_bit(Faulty, &rdev2->flags))
1264 is_active = 0;
1265 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001269 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d = &sb->disks[rdev2->desc_nr];
1271 nr_disks++;
1272 d->number = rdev2->desc_nr;
1273 d->major = MAJOR(rdev2->bdev->bd_dev);
1274 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d->raid_disk = rdev2->raid_disk;
1277 else
1278 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001279 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (test_bit(In_sync, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 active++;
1286 working++;
1287 } else {
1288 d->state = 0;
1289 spare++;
1290 working++;
1291 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001292 if (test_bit(WriteMostly, &rdev2->flags))
1293 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* now set the "removed" and "faulty" bits on any missing devices */
1296 for (i=0 ; i < mddev->raid_disks ; i++) {
1297 mdp_disk_t *d = &sb->disks[i];
1298 if (d->state == 0 && d->number == 0) {
1299 d->number = i;
1300 d->raid_disk = i;
1301 d->state = (1<<MD_DISK_REMOVED);
1302 d->state |= (1<<MD_DISK_FAULTY);
1303 failed++;
1304 }
1305 }
1306 sb->nr_disks = nr_disks;
1307 sb->active_disks = active;
1308 sb->working_disks = working;
1309 sb->failed_disks = failed;
1310 sb->spare_disks = spare;
1311
1312 sb->this_disk = sb->disks[rdev->desc_nr];
1313 sb->sb_csum = calc_sb_csum(sb);
1314}
1315
1316/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317 * rdev_size_change for 0.90.0
1318 */
1319static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001320super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321{
Andre Noll58c0fed2009-03-31 14:33:13 +11001322 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001324 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001326 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001327 if (!num_sectors || num_sectors > rdev->sb_start)
1328 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001329 /* Limit to 4TB as metadata cannot record more than that.
1330 * 4TB == 2^32 KB, or 2*2^32 sectors.
1331 */
NeilBrown667a5312012-08-16 16:46:12 +10001332 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001333 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001334 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335 rdev->sb_page);
1336 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001337 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338}
1339
NeilBrownc6563a82012-05-21 09:27:00 +10001340static int
1341super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1342{
1343 /* non-zero offset changes not possible with v0.90 */
1344 return new_offset == 0;
1345}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346
1347/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * version 1 superblock
1349 */
1350
NeilBrown1c05b4b2006-10-21 10:24:08 -07001351static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001353 __le32 disk_csum;
1354 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 unsigned long long newcsum;
1356 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001357 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 disk_csum = sb->sb_csum;
1360 sb->sb_csum = 0;
1361 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001362 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 newcsum += le32_to_cpu(*isuper++);
1364
1365 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1369 sb->sb_csum = disk_csum;
1370 return cpu_to_le32(csum);
1371}
1372
NeilBrown2699b672011-07-28 11:31:47 +10001373static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1374 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001375static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct mdp_superblock_1 *sb;
1378 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001379 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001380 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001382 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
Andre Noll0f420352008-07-11 22:02:23 +10001385 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * It is always aligned to a 4K boundary and
1387 * depeding on minor_version, it can be:
1388 * 0: At least 8K, but less than 12K, from end of device
1389 * 1: At start of device
1390 * 2: 4K from start of device.
1391 */
1392 switch(minor_version) {
1393 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001394 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001395 sb_start -= 8*2;
1396 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001399 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001402 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 default:
1405 return -EINVAL;
1406 }
Andre Noll0f420352008-07-11 22:02:23 +10001407 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrown0002b272005-09-09 16:23:53 -07001409 /* superblock is rarely larger than 1K, but it can be larger,
1410 * and it is safe to read 4k, so we do that
1411 */
1412 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (ret) return ret;
1414
1415
Namhyung Kim65a06f062011-07-27 11:00:36 +10001416 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1419 sb->major_version != cpu_to_le32(1) ||
1420 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001421 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001422 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return -EINVAL;
1424
1425 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1426 printk("md: invalid superblock checksum on %s\n",
1427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
1430 if (le64_to_cpu(sb->data_size) < 10) {
1431 printk("md: data_size too small on %s\n",
1432 bdevname(rdev->bdev,b));
1433 return -EINVAL;
1434 }
NeilBrownc6563a82012-05-21 09:27:00 +10001435 if (sb->pad0 ||
1436 sb->pad3[0] ||
1437 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1438 /* Some padding is non-zero, might be a new feature */
1439 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 rdev->preferred_minor = 0xffff;
1442 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001443 rdev->new_data_offset = rdev->data_offset;
1444 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1445 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1446 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001447 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
NeilBrown0002b272005-09-09 16:23:53 -07001449 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001450 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001451 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001452 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1453
1454 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001455 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001456 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001457 if (minor_version
1458 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1459 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001460
NeilBrown31b65a02006-07-10 04:44:14 -07001461 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1462 rdev->desc_nr = -1;
1463 else
1464 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1465
NeilBrown2699b672011-07-28 11:31:47 +10001466 if (!rdev->bb_page) {
1467 rdev->bb_page = alloc_page(GFP_KERNEL);
1468 if (!rdev->bb_page)
1469 return -ENOMEM;
1470 }
1471 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1472 rdev->badblocks.count == 0) {
1473 /* need to load the bad block list.
1474 * Currently we limit it to one page.
1475 */
1476 s32 offset;
1477 sector_t bb_sector;
1478 u64 *bbp;
1479 int i;
1480 int sectors = le16_to_cpu(sb->bblog_size);
1481 if (sectors > (PAGE_SIZE / 512))
1482 return -EINVAL;
1483 offset = le32_to_cpu(sb->bblog_offset);
1484 if (offset == 0)
1485 return -EINVAL;
1486 bb_sector = (long long)offset;
1487 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1488 rdev->bb_page, READ, true))
1489 return -EIO;
1490 bbp = (u64 *)page_address(rdev->bb_page);
1491 rdev->badblocks.shift = sb->bblog_shift;
1492 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1493 u64 bb = le64_to_cpu(*bbp);
1494 int count = bb & (0x3ff);
1495 u64 sector = bb >> 10;
1496 sector <<= sb->bblog_shift;
1497 count <<= sb->bblog_shift;
1498 if (bb + 1 == 0)
1499 break;
1500 if (md_set_badblocks(&rdev->badblocks,
1501 sector, count, 1) == 0)
1502 return -EINVAL;
1503 }
NeilBrown486adf72013-04-24 11:42:44 +10001504 } else if (sb->bblog_offset != 0)
1505 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001506
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001507 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001508 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001509 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001511 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1514 sb->level != refsb->level ||
1515 sb->layout != refsb->layout ||
1516 sb->chunksize != refsb->chunksize) {
1517 printk(KERN_WARNING "md: %s has strangely different"
1518 " superblock to %s\n",
1519 bdevname(rdev->bdev,b),
1520 bdevname(refdev->bdev,b2));
1521 return -EINVAL;
1522 }
1523 ev1 = le64_to_cpu(sb->events);
1524 ev2 = le64_to_cpu(refsb->events);
1525
1526 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001527 ret = 1;
1528 else
1529 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
NeilBrownc6563a82012-05-21 09:27:00 +10001531 if (minor_version) {
1532 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1533 sectors -= rdev->data_offset;
1534 } else
1535 sectors = rdev->sb_start;
1536 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001538 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
NeilBrownfd01b882011-10-11 16:47:53 +11001542static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001544 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001545 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrown41158c72005-06-21 17:17:25 -07001547 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001548 clear_bit(Faulty, &rdev->flags);
1549 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001550 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001551 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (mddev->raid_disks == 0) {
1554 mddev->major_version = 1;
1555 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001556 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001557 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1559 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1560 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001561 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mddev->layout = le32_to_cpu(sb->layout);
1563 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001564 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001565 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001566 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001567 mddev->bitmap_info.space = 0;
1568 /* Default location for bitmap is 1K after superblock
1569 * using 3K - total of 4K
1570 */
NeilBrownc3d97142009-12-14 12:49:52 +11001571 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001572 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001573 mddev->reshape_backwards = 0;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1576 memcpy(mddev->uuid, sb->set_uuid, 16);
1577
1578 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001579
NeilBrown71c08052005-09-09 16:23:51 -07001580 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001581 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001582 mddev->bitmap_info.offset =
1583 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001584 /* Metadata doesn't record how much space is available.
1585 * For 1.0, we assume we can use up to the superblock
1586 * if before, else to 4K beyond superblock.
1587 * For others, assume no change is possible.
1588 */
1589 if (mddev->minor_version > 0)
1590 mddev->bitmap_info.space = 0;
1591 else if (mddev->bitmap_info.offset > 0)
1592 mddev->bitmap_info.space =
1593 8 - mddev->bitmap_info.offset;
1594 else
1595 mddev->bitmap_info.space =
1596 -mddev->bitmap_info.offset;
1597 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001598
NeilBrownf6705572006-03-27 01:18:11 -08001599 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1600 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1601 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1602 mddev->new_level = le32_to_cpu(sb->new_level);
1603 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001604 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001605 if (mddev->delta_disks < 0 ||
1606 (mddev->delta_disks == 0 &&
1607 (le32_to_cpu(sb->feature_map)
1608 & MD_FEATURE_RESHAPE_BACKWARDS)))
1609 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001610 } else {
1611 mddev->reshape_position = MaxSector;
1612 mddev->delta_disks = 0;
1613 mddev->new_level = mddev->level;
1614 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001615 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001616 }
1617
NeilBrown41158c72005-06-21 17:17:25 -07001618 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001619 /* Insist of good event counter while assembling, except for
1620 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001622 if (rdev->desc_nr >= 0 &&
1623 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1624 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1625 if (ev1 < mddev->events)
1626 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001627 } else if (mddev->bitmap) {
1628 /* If adding to array with a bitmap, then we can accept an
1629 * older device, but not too old.
1630 */
NeilBrown41158c72005-06-21 17:17:25 -07001631 if (ev1 < mddev->bitmap->events_cleared)
1632 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001633 if (ev1 < mddev->events)
1634 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001635 } else {
1636 if (ev1 < mddev->events)
1637 /* just a hot-add of a new device, leave raid_disk at -1 */
1638 return 0;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (mddev->level != LEVEL_MULTIPATH) {
1641 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001642 if (rdev->desc_nr < 0 ||
1643 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1644 role = 0xffff;
1645 rdev->desc_nr = -1;
1646 } else
1647 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 switch(role) {
1649 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001652 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654 default:
NeilBrownf4667222013-12-09 12:04:56 +11001655 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001657 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001658 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001659 if (!(le32_to_cpu(sb->feature_map) &
1660 MD_FEATURE_RECOVERY_BITMAP))
1661 rdev->saved_raid_disk = -1;
1662 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001663 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rdev->raid_disk = role;
1665 break;
1666 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001667 if (sb->devflags & WriteMostly1)
1668 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001669 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1670 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001671 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return 0;
1675}
1676
NeilBrownfd01b882011-10-11 16:47:53 +11001677static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001680 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int max_dev, i;
1682 /* make rdev->sb match mddev and rdev data. */
1683
Namhyung Kim65a06f062011-07-27 11:00:36 +10001684 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 sb->feature_map = 0;
1687 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001688 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 memset(sb->pad3, 0, sizeof(sb->pad3));
1690
1691 sb->utime = cpu_to_le64((__u64)mddev->utime);
1692 sb->events = cpu_to_le64(mddev->events);
1693 if (mddev->in_sync)
1694 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1695 else
1696 sb->resync_offset = cpu_to_le64(0);
1697
NeilBrown1c05b4b2006-10-21 10:24:08 -07001698 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001699
NeilBrownf0ca3402006-02-02 14:28:04 -08001700 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001701 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001702 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001703 sb->level = cpu_to_le32(mddev->level);
1704 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001705
NeilBrownaeb9b2112011-08-25 14:43:08 +10001706 if (test_bit(WriteMostly, &rdev->flags))
1707 sb->devflags |= WriteMostly1;
1708 else
1709 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001710 sb->data_offset = cpu_to_le64(rdev->data_offset);
1711 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001712
NeilBrownc3d97142009-12-14 12:49:52 +11001713 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1714 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001715 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001716 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001717
1718 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001719 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001720 sb->feature_map |=
1721 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1722 sb->recovery_offset =
1723 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001724 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1725 sb->feature_map |=
1726 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001727 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (test_bit(Replacement, &rdev->flags))
1729 sb->feature_map |=
1730 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001731
NeilBrownf6705572006-03-27 01:18:11 -08001732 if (mddev->reshape_position != MaxSector) {
1733 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1734 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1735 sb->new_layout = cpu_to_le32(mddev->new_layout);
1736 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1737 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001738 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001739 if (mddev->delta_disks == 0 &&
1740 mddev->reshape_backwards)
1741 sb->feature_map
1742 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001743 if (rdev->new_data_offset != rdev->data_offset) {
1744 sb->feature_map
1745 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1746 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1747 - rdev->data_offset));
1748 }
NeilBrownf6705572006-03-27 01:18:11 -08001749 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001750
NeilBrown2699b672011-07-28 11:31:47 +10001751 if (rdev->badblocks.count == 0)
1752 /* Nothing to do for bad blocks*/ ;
1753 else if (sb->bblog_offset == 0)
1754 /* Cannot record bad blocks on this device */
1755 md_error(mddev, rdev);
1756 else {
1757 struct badblocks *bb = &rdev->badblocks;
1758 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1759 u64 *p = bb->page;
1760 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1761 if (bb->changed) {
1762 unsigned seq;
1763
1764retry:
1765 seq = read_seqbegin(&bb->lock);
1766
1767 memset(bbp, 0xff, PAGE_SIZE);
1768
1769 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001770 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001771 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1772 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001773 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001774 }
NeilBrownd0962932012-03-19 12:46:41 +11001775 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001776 if (read_seqretry(&bb->lock, seq))
1777 goto retry;
1778
1779 bb->sector = (rdev->sb_start +
1780 (int)le32_to_cpu(sb->bblog_offset));
1781 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001782 }
1783 }
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001786 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (rdev2->desc_nr+1 > max_dev)
1788 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001789
NeilBrown70471da2009-08-03 10:59:57 +10001790 if (max_dev > le32_to_cpu(sb->max_dev)) {
1791 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001792 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001793 rdev->sb_size = max_dev * 2 + 256;
1794 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1795 if (rdev->sb_size & bmask)
1796 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001797 } else
1798 max_dev = le32_to_cpu(sb->max_dev);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 for (i=0; i<max_dev;i++)
1801 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1802
NeilBrowndafb20f2012-03-19 12:46:39 +11001803 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001805 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001807 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001809 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001810 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else
1812 sb->dev_roles[i] = cpu_to_le16(0xffff);
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sb->sb_csum = calc_sb_1_csum(sb);
1816}
1817
Chris Webb0cd17fe2008-06-28 08:31:46 +10001818static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001819super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001820{
1821 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001822 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001823 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001824 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001825 if (rdev->data_offset != rdev->new_data_offset)
1826 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001827 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001828 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001829 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001830 max_sectors -= rdev->data_offset;
1831 if (!num_sectors || num_sectors > max_sectors)
1832 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001833 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001834 /* minor version 0 with bitmap we can't move */
1835 return 0;
1836 } else {
1837 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001838 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001839 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001840 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001841 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001842 if (!num_sectors || num_sectors > max_sectors)
1843 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001844 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001846 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001847 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001848 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001849 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001850 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001851 rdev->sb_page);
1852 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001853 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001854
1855}
1856
1857static int
1858super_1_allow_new_offset(struct md_rdev *rdev,
1859 unsigned long long new_offset)
1860{
1861 /* All necessary checks on new >= old have been done */
1862 struct bitmap *bitmap;
1863 if (new_offset >= rdev->data_offset)
1864 return 1;
1865
1866 /* with 1.0 metadata, there is no metadata to tread on
1867 * so we can always move back */
1868 if (rdev->mddev->minor_version == 0)
1869 return 1;
1870
1871 /* otherwise we must be sure not to step on
1872 * any metadata, so stay:
1873 * 36K beyond start of superblock
1874 * beyond end of badblocks
1875 * beyond write-intent bitmap
1876 */
1877 if (rdev->sb_start + (32+4)*2 > new_offset)
1878 return 0;
1879 bitmap = rdev->mddev->bitmap;
1880 if (bitmap && !rdev->mddev->bitmap_info.file &&
1881 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001882 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001883 return 0;
1884 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1885 return 0;
1886
1887 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Adrian Bunk75c96f82005-05-05 16:16:09 -07001890static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 [0] = {
1892 .name = "0.90.0",
1893 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001894 .load_super = super_90_load,
1895 .validate_super = super_90_validate,
1896 .sync_super = super_90_sync,
1897 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001898 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 },
1900 [1] = {
1901 .name = "md-1",
1902 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001903 .load_super = super_1_load,
1904 .validate_super = super_1_validate,
1905 .sync_super = super_1_sync,
1906 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001907 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 },
1909};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
NeilBrownfd01b882011-10-11 16:47:53 +11001911static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001912{
1913 if (mddev->sync_super) {
1914 mddev->sync_super(mddev, rdev);
1915 return;
1916 }
1917
1918 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1919
1920 super_types[mddev->major_version].sync_super(mddev, rdev);
1921}
1922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
NeilBrown3cb03002011-10-11 16:45:26 +11001925 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown4b809912008-07-21 17:05:25 +10001927 rcu_read_lock();
1928 rdev_for_each_rcu(rdev, mddev1)
1929 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001930 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001931 rdev2->bdev->bd_contains) {
1932 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001933 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001934 }
1935 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938
1939static LIST_HEAD(pending_raid_disks);
1940
Andre Nollac5e7112009-08-03 10:59:47 +10001941/*
1942 * Try to register data integrity profile for an mddev
1943 *
1944 * This is called when an array is started and after a disk has been kicked
1945 * from the array. It only succeeds if all working and active component devices
1946 * are integrity capable with matching profiles.
1947 */
NeilBrownfd01b882011-10-11 16:47:53 +11001948int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001949{
NeilBrown3cb03002011-10-11 16:45:26 +11001950 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001951
Andre Nollac5e7112009-08-03 10:59:47 +10001952 if (list_empty(&mddev->disks))
1953 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001954 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1955 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001956 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001957 /* skip spares and non-functional disks */
1958 if (test_bit(Faulty, &rdev->flags))
1959 continue;
1960 if (rdev->raid_disk < 0)
1961 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001962 if (!reference) {
1963 /* Use the first rdev as the reference */
1964 reference = rdev;
1965 continue;
1966 }
1967 /* does this rdev's profile match the reference profile? */
1968 if (blk_integrity_compare(reference->bdev->bd_disk,
1969 rdev->bdev->bd_disk) < 0)
1970 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001971 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001972 if (!reference || !bdev_get_integrity(reference->bdev))
1973 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001974 /*
1975 * All component devices are integrity capable and have matching
1976 * profiles, register the common profile for the md device.
1977 */
1978 if (blk_integrity_register(mddev->gendisk,
1979 bdev_get_integrity(reference->bdev)) != 0) {
1980 printk(KERN_ERR "md: failed to register integrity for %s\n",
1981 mdname(mddev));
1982 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001983 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001984 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1985 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1986 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1987 mdname(mddev));
1988 return -EINVAL;
1989 }
Andre Nollac5e7112009-08-03 10:59:47 +10001990 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001991}
Andre Nollac5e7112009-08-03 10:59:47 +10001992EXPORT_SYMBOL(md_integrity_register);
1993
1994/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001995void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001996{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11001997 struct blk_integrity *bi_rdev;
1998 struct blk_integrity *bi_mddev;
1999
2000 if (!mddev->gendisk)
2001 return;
2002
2003 bi_rdev = bdev_get_integrity(rdev->bdev);
2004 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002005
2006 if (!bi_mddev) /* nothing to do */
2007 return;
2008 if (rdev->raid_disk < 0) /* skip spares */
2009 return;
2010 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2011 rdev->bdev->bd_disk) >= 0)
2012 return;
2013 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2014 blk_integrity_unregister(mddev->gendisk);
2015}
2016EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002017
NeilBrownfd01b882011-10-11 16:47:53 +11002018static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002020 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002021 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002022 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 if (rdev->mddev) {
2026 MD_BUG();
2027 return -EINVAL;
2028 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002029
2030 /* prevent duplicates */
2031 if (find_rdev(mddev, rdev->bdev->bd_dev))
2032 return -EEXIST;
2033
Andre Nolldd8ac332009-03-31 14:33:13 +11002034 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2035 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2036 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002037 if (mddev->pers) {
2038 /* Cannot change size, so fail
2039 * If mddev->level <= 0, then we don't care
2040 * about aligning sizes (e.g. linear)
2041 */
2042 if (mddev->level > 0)
2043 return -ENOSPC;
2044 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002045 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Verify rdev->desc_nr is unique.
2049 * If it is -1, assign a free number, else
2050 * check number is not in use
2051 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002052 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (rdev->desc_nr < 0) {
2054 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002055 if (mddev->pers)
2056 choice = mddev->raid_disks;
2057 while (find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 choice++;
2059 rdev->desc_nr = choice;
2060 } else {
NeilBrown4878e9e2014-09-25 17:00:11 +10002061 if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2062 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002066 rcu_read_unlock();
NeilBrownde01dfa2009-02-06 18:02:46 +11002067 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2068 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2069 mdname(mddev), mddev->max_disks);
2070 return -EBUSY;
2071 }
NeilBrown19133a42005-11-08 21:39:35 -08002072 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002073 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002074 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002077 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002079 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081
Tejun Heo0762b8b2008-08-25 19:56:12 +09002082 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002083 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2084 /* failure here is OK */;
2085 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002086
NeilBrown4b809912008-07-21 17:05:25 +10002087 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002088 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002089
2090 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002091 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002094
2095 fail:
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2097 b, mdname(mddev));
2098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
NeilBrown177a99b2008-02-06 01:39:56 -08002101static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002102{
NeilBrown3cb03002011-10-11 16:45:26 +11002103 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002104 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002105 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002106}
2107
NeilBrown3cb03002011-10-11 16:45:26 +11002108static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 char b[BDEVNAME_SIZE];
2111 if (!rdev->mddev) {
2112 MD_BUG();
2113 return;
2114 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002115 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002116 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2118 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002119 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002120 sysfs_put(rdev->sysfs_state);
2121 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002122 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002123 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002124 * writing to 'remove' to "dev/state". We also need
2125 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002126 */
NeilBrown4b809912008-07-21 17:05:25 +10002127 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002128 INIT_WORK(&rdev->del_work, md_delayed_delete);
2129 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002130 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
2133/*
2134 * prevent the device from being mounted, repartitioned or
2135 * otherwise reused by a RAID array (or any other kernel
2136 * subsystem), by bd_claiming the device.
2137 */
NeilBrown3cb03002011-10-11 16:45:26 +11002138static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 int err = 0;
2141 struct block_device *bdev;
2142 char b[BDEVNAME_SIZE];
2143
Tejun Heod4d77622010-11-13 11:55:18 +01002144 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002145 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (IS_ERR(bdev)) {
2147 printk(KERN_ERR "md: could not open %s.\n",
2148 __bdevname(dev, b));
2149 return PTR_ERR(bdev);
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 rdev->bdev = bdev;
2152 return err;
2153}
2154
NeilBrown3cb03002011-10-11 16:45:26 +11002155static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 struct block_device *bdev = rdev->bdev;
2158 rdev->bdev = NULL;
2159 if (!bdev)
2160 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002161 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164void md_autodetect_dev(dev_t dev);
2165
NeilBrown3cb03002011-10-11 16:45:26 +11002166static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 char b[BDEVNAME_SIZE];
2169 printk(KERN_INFO "md: export_rdev(%s)\n",
2170 bdevname(rdev->bdev,b));
2171 if (rdev->mddev)
2172 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002173 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002175 if (test_bit(AutoDetected, &rdev->flags))
2176 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#endif
2178 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
NeilBrown3cb03002011-10-11 16:45:26 +11002182static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 unbind_rdev_from_array(rdev);
2185 export_rdev(rdev);
2186}
2187
NeilBrownfd01b882011-10-11 16:47:53 +11002188static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
NeilBrown0638bb02014-09-25 17:43:47 +10002190 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
NeilBrown0638bb02014-09-25 17:43:47 +10002192 while (!list_empty(&mddev->disks)) {
2193 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2194 same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 kick_rdev_from_array(rdev);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 mddev->raid_disks = 0;
2198 mddev->major_version = 0;
2199}
2200
2201static void print_desc(mdp_disk_t *desc)
2202{
2203 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2204 desc->major,desc->minor,desc->raid_disk,desc->state);
2205}
2206
Cheng Renquancd2ac932009-01-09 08:31:08 +11002207static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 int i;
2210
2211 printk(KERN_INFO
2212 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2213 sb->major_version, sb->minor_version, sb->patch_version,
2214 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2215 sb->ctime);
2216 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2217 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2218 sb->md_minor, sb->layout, sb->chunk_size);
2219 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2220 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2221 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2222 sb->failed_disks, sb->spare_disks,
2223 sb->sb_csum, (unsigned long)sb->events_lo);
2224
2225 printk(KERN_INFO);
2226 for (i = 0; i < MD_SB_DISKS; i++) {
2227 mdp_disk_t *desc;
2228
2229 desc = sb->disks + i;
2230 if (desc->number || desc->major || desc->minor ||
2231 desc->raid_disk || (desc->state && (desc->state != 4))) {
2232 printk(" D %2d: ", i);
2233 print_desc(desc);
2234 }
2235 }
2236 printk(KERN_INFO "md: THIS: ");
2237 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Cheng Renquancd2ac932009-01-09 08:31:08 +11002240static void print_sb_1(struct mdp_superblock_1 *sb)
2241{
2242 __u8 *uuid;
2243
2244 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002245 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002246 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002247 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002248 le32_to_cpu(sb->major_version),
2249 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002250 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002251 sb->set_name,
2252 (unsigned long long)le64_to_cpu(sb->ctime)
2253 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2254
2255 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002256 printk(KERN_INFO
2257 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002258 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002259 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002260 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2261 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002262 le32_to_cpu(sb->level),
2263 (unsigned long long)le64_to_cpu(sb->size),
2264 le32_to_cpu(sb->raid_disks),
2265 le32_to_cpu(sb->layout),
2266 le32_to_cpu(sb->chunksize),
2267 (unsigned long long)le64_to_cpu(sb->data_offset),
2268 (unsigned long long)le64_to_cpu(sb->data_size),
2269 (unsigned long long)le64_to_cpu(sb->super_offset),
2270 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2271 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002272 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002273 sb->devflags,
2274 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2275 (unsigned long long)le64_to_cpu(sb->events),
2276 (unsigned long long)le64_to_cpu(sb->resync_offset),
2277 le32_to_cpu(sb->sb_csum),
2278 le32_to_cpu(sb->max_dev)
2279 );
2280}
2281
NeilBrown3cb03002011-10-11 16:45:26 +11002282static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002285 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2286 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002287 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2288 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002290 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2291 switch (major_version) {
2292 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002293 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002294 break;
2295 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002296 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002297 break;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 } else
2300 printk(KERN_INFO "md: no rdev superblock!\n");
2301}
2302
Adrian Bunk5e563412006-06-26 00:27:42 -07002303static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002305 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002306 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002307 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 char b[BDEVNAME_SIZE];
2309
2310 printk("\n");
2311 printk("md: **********************************\n");
2312 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2313 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002314 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
NeilBrown32a76272005-06-21 17:17:14 -07002316 if (mddev->bitmap)
2317 bitmap_print_sb(mddev->bitmap);
2318 else
2319 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002320 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 printk("<%s>", bdevname(rdev->bdev,b));
2322 printk("\n");
2323
NeilBrowndafb20f2012-03-19 12:46:39 +11002324 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002325 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327 printk("md: **********************************\n");
2328 printk("\n");
2329}
2330
2331
NeilBrownfd01b882011-10-11 16:47:53 +11002332static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
NeilBrown42543762006-06-26 00:27:57 -07002334 /* Update each superblock (in-memory image), but
2335 * if we are allowed to, skip spares which already
2336 * have the right event counter, or have one earlier
2337 * (which would mean they aren't being marked as dirty
2338 * with the rest of the array)
2339 */
NeilBrown3cb03002011-10-11 16:45:26 +11002340 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002341 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002342 if (rdev->sb_events == mddev->events ||
2343 (nospares &&
2344 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002345 rdev->sb_events+1 == mddev->events)) {
2346 /* Don't update this superblock */
2347 rdev->sb_loaded = 2;
2348 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002349 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002350 rdev->sb_loaded = 1;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353}
2354
NeilBrownfd01b882011-10-11 16:47:53 +11002355static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
NeilBrown3cb03002011-10-11 16:45:26 +11002357 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002358 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002359 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002360 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
NeilBrownd87f0642013-04-24 11:42:40 +10002362 if (mddev->ro) {
2363 if (force_change)
2364 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2365 return;
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002368 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002369 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002370 if (rdev->raid_disk >= 0 &&
2371 mddev->delta_disks >= 0 &&
2372 !test_bit(In_sync, &rdev->flags) &&
2373 mddev->curr_resync_completed > rdev->recovery_offset)
2374 rdev->recovery_offset = mddev->curr_resync_completed;
2375
2376 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002377 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002378 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002379 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002380 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002381 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002382 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002383 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002384 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002385 md_ack_all_badblocks(&rdev->badblocks);
2386 md_error(mddev, rdev);
2387 }
2388 clear_bit(Blocked, &rdev->flags);
2389 clear_bit(BlockedBadBlocks, &rdev->flags);
2390 wake_up(&rdev->blocked_wait);
2391 }
2392 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002393 wake_up(&mddev->sb_wait);
2394 return;
2395 }
2396
NeilBrowna9701a32005-11-08 21:39:34 -08002397 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002398
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002399 mddev->utime = get_seconds();
2400
NeilBrown850b2b422006-10-03 01:15:46 -07002401 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2402 force_change = 1;
2403 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2404 /* just a clean<-> dirty transition, possibly leave spares alone,
2405 * though if events isn't the right even/odd, we will have to do
2406 * spares after all
2407 */
2408 nospares = 1;
2409 if (force_change)
2410 nospares = 0;
2411 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002412 /* If the array is degraded, then skipping spares is both
2413 * dangerous and fairly pointless.
2414 * Dangerous because a device that was removed from the array
2415 * might have a event_count that still looks up-to-date,
2416 * so it can be re-added without a resync.
2417 * Pointless because if there are any spares to skip,
2418 * then a recovery will happen and soon that array won't
2419 * be degraded any more and the spare can go back to sleep then.
2420 */
NeilBrown850b2b422006-10-03 01:15:46 -07002421 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002422
NeilBrown06d91a52005-06-21 17:17:12 -07002423 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002424
2425 /* If this is just a dirty<->clean transition, and the array is clean
2426 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002427 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002428 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002429 && mddev->can_decrease_events
2430 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002431 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002432 mddev->can_decrease_events = 0;
2433 } else {
NeilBrown42543762006-06-26 00:27:57 -07002434 /* otherwise we have to go forward and ... */
2435 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002436 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (!mddev->events) {
2440 /*
2441 * oops, this 64-bit counter should never wrap.
2442 * Either we are in around ~1 trillion A.C., assuming
2443 * 1 reboot per second, or we have a bug:
2444 */
2445 MD_BUG();
2446 mddev->events --;
2447 }
NeilBrown2699b672011-07-28 11:31:47 +10002448
NeilBrowndafb20f2012-03-19 12:46:39 +11002449 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002450 if (rdev->badblocks.changed)
2451 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002452 if (test_bit(Faulty, &rdev->flags))
2453 set_bit(FaultRecorded, &rdev->flags);
2454 }
NeilBrown2699b672011-07-28 11:31:47 +10002455
NeilBrowne6910632008-02-06 01:39:51 -08002456 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002457 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
NeilBrown36a4e1f2011-10-07 14:23:17 +11002459 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2460 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
NeilBrown4ad13662007-07-17 04:06:13 -07002462 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002463 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002465
NeilBrown42543762006-06-26 00:27:57 -07002466 if (rdev->sb_loaded != 1)
2467 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
NeilBrownf4667222013-12-09 12:04:56 +11002469 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002470 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002471 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002472 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002473 pr_debug("md: (write) %s's sb offset: %llu\n",
2474 bdevname(rdev->bdev, b),
2475 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002476 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002477 if (rdev->badblocks.size) {
2478 md_super_write(mddev, rdev,
2479 rdev->badblocks.sector,
2480 rdev->badblocks.size << 9,
2481 rdev->bb_page);
2482 rdev->badblocks.size = 0;
2483 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002484
NeilBrownf4667222013-12-09 12:04:56 +11002485 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002486 pr_debug("md: %s (skipping faulty)\n",
2487 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002488
NeilBrown7bfa19f2005-06-21 17:17:28 -07002489 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /* only need to write one superblock... */
2491 break;
2492 }
NeilBrowna9701a32005-11-08 21:39:34 -08002493 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002494 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002495
NeilBrowna9701a32005-11-08 21:39:34 -08002496 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002497 if (mddev->in_sync != sync_req ||
2498 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002499 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002500 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002501 goto repeat;
2502 }
NeilBrown850b2b422006-10-03 01:15:46 -07002503 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002504 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002505 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002506 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2507 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002508
NeilBrowndafb20f2012-03-19 12:46:39 +11002509 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002510 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2511 clear_bit(Blocked, &rdev->flags);
2512
2513 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002514 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002515 clear_bit(BlockedBadBlocks, &rdev->flags);
2516 wake_up(&rdev->blocked_wait);
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Andre Noll7f6ce762008-03-23 18:34:54 +01002520/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002521 * We want to accept with case. For this we use cmd_match.
2522 */
2523static int cmd_match(const char *cmd, const char *str)
2524{
2525 /* See if cmd, written into a sysfs file, matches
2526 * str. They must either be the same, or cmd can
2527 * have a trailing newline
2528 */
2529 while (*cmd && *str && *cmd == *str) {
2530 cmd++;
2531 str++;
2532 }
2533 if (*cmd == '\n')
2534 cmd++;
2535 if (*str || *cmd)
2536 return 0;
2537 return 1;
2538}
2539
NeilBrown86e6ffd2005-11-08 21:39:24 -08002540struct rdev_sysfs_entry {
2541 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002542 ssize_t (*show)(struct md_rdev *, char *);
2543 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002544};
2545
2546static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002547state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548{
2549 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002550 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002551
NeilBrownde393cd2011-07-28 11:31:48 +10002552 if (test_bit(Faulty, &rdev->flags) ||
2553 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002554 len+= sprintf(page+len, "%sfaulty",sep);
2555 sep = ",";
2556 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002557 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002558 len += sprintf(page+len, "%sin_sync",sep);
2559 sep = ",";
2560 }
NeilBrownf6556752006-06-26 00:28:01 -07002561 if (test_bit(WriteMostly, &rdev->flags)) {
2562 len += sprintf(page+len, "%swrite_mostly",sep);
2563 sep = ",";
2564 }
NeilBrownde393cd2011-07-28 11:31:48 +10002565 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002566 (rdev->badblocks.unacked_exist
2567 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002568 len += sprintf(page+len, "%sblocked", sep);
2569 sep = ",";
2570 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002571 if (!test_bit(Faulty, &rdev->flags) &&
2572 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002573 len += sprintf(page+len, "%sspare", sep);
2574 sep = ",";
2575 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002576 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2577 len += sprintf(page+len, "%swrite_error", sep);
2578 sep = ",";
2579 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002580 if (test_bit(WantReplacement, &rdev->flags)) {
2581 len += sprintf(page+len, "%swant_replacement", sep);
2582 sep = ",";
2583 }
2584 if (test_bit(Replacement, &rdev->flags)) {
2585 len += sprintf(page+len, "%sreplacement", sep);
2586 sep = ",";
2587 }
2588
NeilBrown86e6ffd2005-11-08 21:39:24 -08002589 return len+sprintf(page+len, "\n");
2590}
2591
NeilBrown45dc2de2006-06-26 00:27:58 -07002592static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002593state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002594{
2595 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002596 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002597 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002598 * writemostly - sets write_mostly
2599 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002600 * blocked - sets the Blocked flags
2601 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002602 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002603 * -insync - clear Insync for a device with a slot assigned,
2604 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002605 * write_error - sets WriteErrorSeen
2606 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002607 */
2608 int err = -EINVAL;
2609 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2610 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002611 if (test_bit(Faulty, &rdev->flags))
2612 err = 0;
2613 else
2614 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002615 } else if (cmd_match(buf, "remove")) {
2616 if (rdev->raid_disk >= 0)
2617 err = -EBUSY;
2618 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002619 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002620 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002621 if (mddev->pers)
2622 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002623 md_new_event(mddev);
2624 err = 0;
2625 }
NeilBrownf6556752006-06-26 00:28:01 -07002626 } else if (cmd_match(buf, "writemostly")) {
2627 set_bit(WriteMostly, &rdev->flags);
2628 err = 0;
2629 } else if (cmd_match(buf, "-writemostly")) {
2630 clear_bit(WriteMostly, &rdev->flags);
2631 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002632 } else if (cmd_match(buf, "blocked")) {
2633 set_bit(Blocked, &rdev->flags);
2634 err = 0;
2635 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002636 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002637 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002638 /* metadata handler doesn't understand badblocks,
2639 * so we need to fail the device
2640 */
2641 md_error(rdev->mddev, rdev);
2642 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002643 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002644 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002645 wake_up(&rdev->blocked_wait);
2646 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2647 md_wakeup_thread(rdev->mddev->thread);
2648
2649 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002650 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2651 set_bit(In_sync, &rdev->flags);
2652 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002653 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2654 clear_bit(In_sync, &rdev->flags);
2655 rdev->saved_raid_disk = rdev->raid_disk;
2656 rdev->raid_disk = -1;
2657 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002658 } else if (cmd_match(buf, "write_error")) {
2659 set_bit(WriteErrorSeen, &rdev->flags);
2660 err = 0;
2661 } else if (cmd_match(buf, "-write_error")) {
2662 clear_bit(WriteErrorSeen, &rdev->flags);
2663 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002664 } else if (cmd_match(buf, "want_replacement")) {
2665 /* Any non-spare device that is not a replacement can
2666 * become want_replacement at any time, but we then need to
2667 * check if recovery is needed.
2668 */
2669 if (rdev->raid_disk >= 0 &&
2670 !test_bit(Replacement, &rdev->flags))
2671 set_bit(WantReplacement, &rdev->flags);
2672 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2673 md_wakeup_thread(rdev->mddev->thread);
2674 err = 0;
2675 } else if (cmd_match(buf, "-want_replacement")) {
2676 /* Clearing 'want_replacement' is always allowed.
2677 * Once replacements starts it is too late though.
2678 */
2679 err = 0;
2680 clear_bit(WantReplacement, &rdev->flags);
2681 } else if (cmd_match(buf, "replacement")) {
2682 /* Can only set a device as a replacement when array has not
2683 * yet been started. Once running, replacement is automatic
2684 * from spares, or by assigning 'slot'.
2685 */
2686 if (rdev->mddev->pers)
2687 err = -EBUSY;
2688 else {
2689 set_bit(Replacement, &rdev->flags);
2690 err = 0;
2691 }
2692 } else if (cmd_match(buf, "-replacement")) {
2693 /* Similarly, can only clear Replacement before start */
2694 if (rdev->mddev->pers)
2695 err = -EBUSY;
2696 else {
2697 clear_bit(Replacement, &rdev->flags);
2698 err = 0;
2699 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002700 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002701 if (!err)
2702 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002703 return err ? err : len;
2704}
NeilBrown80ca3a42006-07-10 04:44:18 -07002705static struct rdev_sysfs_entry rdev_state =
2706__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002707
2708static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002709errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002710{
2711 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2712}
2713
2714static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002715errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002716{
2717 char *e;
2718 unsigned long n = simple_strtoul(buf, &e, 10);
2719 if (*buf && (*e == 0 || *e == '\n')) {
2720 atomic_set(&rdev->corrected_errors, n);
2721 return len;
2722 }
2723 return -EINVAL;
2724}
2725static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002726__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002727
NeilBrown014236d2006-01-06 00:20:55 -08002728static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002729slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002730{
2731 if (rdev->raid_disk < 0)
2732 return sprintf(page, "none\n");
2733 else
2734 return sprintf(page, "%d\n", rdev->raid_disk);
2735}
2736
2737static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002738slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002739{
2740 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002741 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002742 int slot = simple_strtoul(buf, &e, 10);
2743 if (strncmp(buf, "none", 4)==0)
2744 slot = -1;
2745 else if (e==buf || (*e && *e!= '\n'))
2746 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002747 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002748 /* Setting 'slot' on an active array requires also
2749 * updating the 'rd%d' link, and communicating
2750 * with the personality with ->hot_*_disk.
2751 * For now we only support removing
2752 * failed/spare devices. This normally happens automatically,
2753 * but not when the metadata is externally managed.
2754 */
NeilBrownc303da62008-02-06 01:39:51 -08002755 if (rdev->raid_disk == -1)
2756 return -EEXIST;
2757 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002758 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002759 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002760 clear_bit(Blocked, &rdev->flags);
2761 remove_and_add_spares(rdev->mddev, rdev);
2762 if (rdev->raid_disk >= 0)
2763 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002764 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2765 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002767 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002768 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002769 */
2770
2771 if (rdev->raid_disk != -1)
2772 return -EBUSY;
2773
NeilBrownc6751b22011-02-02 11:57:13 +11002774 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2775 return -EBUSY;
2776
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 if (rdev->mddev->pers->hot_add_disk == NULL)
2778 return -EINVAL;
2779
NeilBrownba1b41b2011-01-14 09:14:34 +11002780 if (slot >= rdev->mddev->raid_disks &&
2781 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2782 return -ENOSPC;
2783
Neil Brown6c2fce22008-06-28 08:31:31 +10002784 rdev->raid_disk = slot;
2785 if (test_bit(In_sync, &rdev->flags))
2786 rdev->saved_raid_disk = slot;
2787 else
2788 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002789 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002790 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002791 err = rdev->mddev->pers->
2792 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002793 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002795 return err;
Neil Brown52664732008-06-28 08:31:44 +10002796 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002797 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002798 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002799 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002800 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002801 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002802 if (slot >= rdev->mddev->raid_disks &&
2803 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002804 return -ENOSPC;
2805 rdev->raid_disk = slot;
2806 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002807 clear_bit(Faulty, &rdev->flags);
2808 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002809 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002810 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002811 }
NeilBrown014236d2006-01-06 00:20:55 -08002812 return len;
2813}
2814
2815
2816static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002817__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002818
NeilBrown93c8cad2006-01-06 00:20:56 -08002819static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002820offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002821{
Andrew Morton6961ece2006-01-06 00:20:59 -08002822 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002823}
2824
2825static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002826offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002827{
NeilBrownc6563a82012-05-21 09:27:00 +10002828 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002829 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002830 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002831 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002832 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002834 /* Must set offset before size, so overlap checks
2835 * can be sane */
2836 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002837 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002838 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002839 return len;
2840}
2841
2842static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002843__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002844
NeilBrownc6563a82012-05-21 09:27:00 +10002845static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2846{
2847 return sprintf(page, "%llu\n",
2848 (unsigned long long)rdev->new_data_offset);
2849}
2850
2851static ssize_t new_offset_store(struct md_rdev *rdev,
2852 const char *buf, size_t len)
2853{
2854 unsigned long long new_offset;
2855 struct mddev *mddev = rdev->mddev;
2856
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002857 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002858 return -EINVAL;
2859
2860 if (mddev->sync_thread)
2861 return -EBUSY;
2862 if (new_offset == rdev->data_offset)
2863 /* reset is always permitted */
2864 ;
2865 else if (new_offset > rdev->data_offset) {
2866 /* must not push array size beyond rdev_sectors */
2867 if (new_offset - rdev->data_offset
2868 + mddev->dev_sectors > rdev->sectors)
2869 return -E2BIG;
2870 }
2871 /* Metadata worries about other space details. */
2872
2873 /* decreasing the offset is inconsistent with a backwards
2874 * reshape.
2875 */
2876 if (new_offset < rdev->data_offset &&
2877 mddev->reshape_backwards)
2878 return -EINVAL;
2879 /* Increasing offset is inconsistent with forwards
2880 * reshape. reshape_direction should be set to
2881 * 'backwards' first.
2882 */
2883 if (new_offset > rdev->data_offset &&
2884 !mddev->reshape_backwards)
2885 return -EINVAL;
2886
2887 if (mddev->pers && mddev->persistent &&
2888 !super_types[mddev->major_version]
2889 .allow_new_offset(rdev, new_offset))
2890 return -E2BIG;
2891 rdev->new_data_offset = new_offset;
2892 if (new_offset > rdev->data_offset)
2893 mddev->reshape_backwards = 1;
2894 else if (new_offset < rdev->data_offset)
2895 mddev->reshape_backwards = 0;
2896
2897 return len;
2898}
2899static struct rdev_sysfs_entry rdev_new_offset =
2900__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2901
NeilBrown83303b62006-01-06 00:21:06 -08002902static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002903rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002904{
Andre Nolldd8ac332009-03-31 14:33:13 +11002905 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002906}
2907
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2909{
2910 /* check if two start/length pairs overlap */
2911 if (s1+l1 <= s2)
2912 return 0;
2913 if (s2+l2 <= s1)
2914 return 0;
2915 return 1;
2916}
2917
Dan Williamsb522adc2009-03-31 15:00:31 +11002918static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2919{
2920 unsigned long long blocks;
2921 sector_t new;
2922
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002923 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002924 return -EINVAL;
2925
2926 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2927 return -EINVAL; /* sector conversion overflow */
2928
2929 new = blocks * 2;
2930 if (new != blocks * 2)
2931 return -EINVAL; /* unsigned long long to sector_t overflow */
2932
2933 *sectors = new;
2934 return 0;
2935}
2936
NeilBrown83303b62006-01-06 00:21:06 -08002937static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002938rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002939{
NeilBrownfd01b882011-10-11 16:47:53 +11002940 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002941 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002942 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002943
Dan Williamsb522adc2009-03-31 15:00:31 +11002944 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002945 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002946 if (rdev->data_offset != rdev->new_data_offset)
2947 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002948 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002949 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002950 sectors = super_types[my_mddev->major_version].
2951 rdev_size_change(rdev, sectors);
2952 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002953 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002954 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002955 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002956 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002957 if (!my_mddev->pers->resize)
2958 /* Cannot change size for RAID0 or Linear etc */
2959 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002960 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002961 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002962 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 rdev->sectors = sectors;
2965 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002966 /* need to check that all other rdevs with the same ->bdev
2967 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002968 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002969 * we have to change it back, we will have the lock again.
2970 */
NeilBrownfd01b882011-10-11 16:47:53 +11002971 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002972 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002973 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002974
NeilBrown27c529b2008-03-04 14:29:33 -08002975 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002976 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002977 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002978
NeilBrown29f097c2013-11-14 17:54:51 +11002979 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002980 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002981 if (rdev->bdev == rdev2->bdev &&
2982 rdev != rdev2 &&
2983 overlaps(rdev->data_offset, rdev->sectors,
2984 rdev2->data_offset,
2985 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002986 overlap = 1;
2987 break;
2988 }
2989 mddev_unlock(mddev);
2990 if (overlap) {
2991 mddev_put(mddev);
2992 break;
2993 }
2994 }
NeilBrown29f097c2013-11-14 17:54:51 +11002995 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 if (overlap) {
2997 /* Someone else could have slipped in a size
2998 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002999 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003000 * safe, and trust userspace not to race with
3001 * itself
3002 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003003 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003004 return -EBUSY;
3005 }
3006 }
NeilBrown83303b62006-01-06 00:21:06 -08003007 return len;
3008}
3009
3010static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003011__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003012
Dan Williams06e3c812009-12-12 21:17:12 -07003013
NeilBrown3cb03002011-10-11 16:45:26 +11003014static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003015{
3016 unsigned long long recovery_start = rdev->recovery_offset;
3017
3018 if (test_bit(In_sync, &rdev->flags) ||
3019 recovery_start == MaxSector)
3020 return sprintf(page, "none\n");
3021
3022 return sprintf(page, "%llu\n", recovery_start);
3023}
3024
NeilBrown3cb03002011-10-11 16:45:26 +11003025static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003026{
3027 unsigned long long recovery_start;
3028
3029 if (cmd_match(buf, "none"))
3030 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003031 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003032 return -EINVAL;
3033
3034 if (rdev->mddev->pers &&
3035 rdev->raid_disk >= 0)
3036 return -EBUSY;
3037
3038 rdev->recovery_offset = recovery_start;
3039 if (recovery_start == MaxSector)
3040 set_bit(In_sync, &rdev->flags);
3041 else
3042 clear_bit(In_sync, &rdev->flags);
3043 return len;
3044}
3045
3046static struct rdev_sysfs_entry rdev_recovery_start =
3047__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3048
NeilBrown16c791a2011-07-28 11:31:47 +10003049
3050static ssize_t
3051badblocks_show(struct badblocks *bb, char *page, int unack);
3052static ssize_t
3053badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3054
NeilBrown3cb03002011-10-11 16:45:26 +11003055static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003056{
3057 return badblocks_show(&rdev->badblocks, page, 0);
3058}
NeilBrown3cb03002011-10-11 16:45:26 +11003059static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003060{
NeilBrownde393cd2011-07-28 11:31:48 +10003061 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3062 /* Maybe that ack was all we needed */
3063 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3064 wake_up(&rdev->blocked_wait);
3065 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003066}
3067static struct rdev_sysfs_entry rdev_bad_blocks =
3068__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3069
3070
NeilBrown3cb03002011-10-11 16:45:26 +11003071static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003072{
3073 return badblocks_show(&rdev->badblocks, page, 1);
3074}
NeilBrown3cb03002011-10-11 16:45:26 +11003075static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003076{
3077 return badblocks_store(&rdev->badblocks, page, len, 1);
3078}
3079static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3080__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3081
NeilBrown86e6ffd2005-11-08 21:39:24 -08003082static struct attribute *rdev_default_attrs[] = {
3083 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003084 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003085 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003086 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003087 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003088 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003089 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003090 &rdev_bad_blocks.attr,
3091 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092 NULL,
3093};
3094static ssize_t
3095rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3096{
3097 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003098 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003099 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003100 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003101
3102 if (!entry->show)
3103 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003104
3105 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3106 if (!rv) {
3107 if (rdev->mddev == NULL)
3108 rv = -EBUSY;
3109 else
3110 rv = entry->show(rdev, page);
3111 mddev_unlock(mddev);
3112 }
3113 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003114}
3115
3116static ssize_t
3117rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3118 const char *page, size_t length)
3119{
3120 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003121 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003122 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003123 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003124
3125 if (!entry->store)
3126 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003127 if (!capable(CAP_SYS_ADMIN))
3128 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003129 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003130 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003131 if (rdev->mddev == NULL)
3132 rv = -EBUSY;
3133 else
3134 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003135 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003136 }
3137 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003138}
3139
3140static void rdev_free(struct kobject *ko)
3141{
NeilBrown3cb03002011-10-11 16:45:26 +11003142 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003143 kfree(rdev);
3144}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003145static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003146 .show = rdev_attr_show,
3147 .store = rdev_attr_store,
3148};
3149static struct kobj_type rdev_ktype = {
3150 .release = rdev_free,
3151 .sysfs_ops = &rdev_sysfs_ops,
3152 .default_attrs = rdev_default_attrs,
3153};
3154
NeilBrown3cb03002011-10-11 16:45:26 +11003155int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003156{
3157 rdev->desc_nr = -1;
3158 rdev->saved_raid_disk = -1;
3159 rdev->raid_disk = -1;
3160 rdev->flags = 0;
3161 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003162 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003163 rdev->sb_events = 0;
3164 rdev->last_read_error.tv_sec = 0;
3165 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003166 rdev->sb_loaded = 0;
3167 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003168 atomic_set(&rdev->nr_pending, 0);
3169 atomic_set(&rdev->read_errors, 0);
3170 atomic_set(&rdev->corrected_errors, 0);
3171
3172 INIT_LIST_HEAD(&rdev->same_set);
3173 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003174
3175 /* Add space to store bad block list.
3176 * This reserves the space even on arrays where it cannot
3177 * be used - I wonder if that matters
3178 */
3179 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003180 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003181 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3182 seqlock_init(&rdev->badblocks.lock);
3183 if (rdev->badblocks.page == NULL)
3184 return -ENOMEM;
3185
3186 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003187}
3188EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/*
3190 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3191 *
3192 * mark the device faulty if:
3193 *
3194 * - the device is nonexistent (zero size)
3195 * - the device has no valid superblock
3196 *
3197 * a faulty rdev _never_ has rdev->sb set.
3198 */
NeilBrown3cb03002011-10-11 16:45:26 +11003199static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 char b[BDEVNAME_SIZE];
3202 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003203 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 sector_t size;
3205
NeilBrown9ffae0c2006-01-06 00:20:32 -08003206 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (!rdev) {
3208 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3209 return ERR_PTR(-ENOMEM);
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
NeilBrown2230dfe2011-07-28 11:31:46 +10003212 err = md_rdev_init(rdev);
3213 if (err)
3214 goto abort_free;
3215 err = alloc_disk_sb(rdev);
3216 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 goto abort_free;
3218
NeilBrownc5d79ad2008-02-06 01:39:54 -08003219 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (err)
3221 goto abort_free;
3222
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003223 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003224
Mike Snitzer77304d22010-11-08 14:39:12 +01003225 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (!size) {
3227 printk(KERN_WARNING
3228 "md: %s has zero or unknown size, marking faulty!\n",
3229 bdevname(rdev->bdev,b));
3230 err = -EINVAL;
3231 goto abort_free;
3232 }
3233
3234 if (super_format >= 0) {
3235 err = super_types[super_format].
3236 load_super(rdev, NULL, super_minor);
3237 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003238 printk(KERN_WARNING
3239 "md: %s does not have a valid v%d.%d "
3240 "superblock, not importing!\n",
3241 bdevname(rdev->bdev,b),
3242 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 goto abort_free;
3244 }
3245 if (err < 0) {
3246 printk(KERN_WARNING
3247 "md: could not read %s's sb, not importing!\n",
3248 bdevname(rdev->bdev,b));
3249 goto abort_free;
3250 }
3251 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return rdev;
3254
3255abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003256 if (rdev->bdev)
3257 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003258 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 kfree(rdev);
3260 return ERR_PTR(err);
3261}
3262
3263/*
3264 * Check a full RAID array for plausibility
3265 */
3266
3267
NeilBrownfd01b882011-10-11 16:47:53 +11003268static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003271 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 char b[BDEVNAME_SIZE];
3273
3274 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003275 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 switch (super_types[mddev->major_version].
3277 load_super(rdev, freshest, mddev->minor_version)) {
3278 case 1:
3279 freshest = rdev;
3280 break;
3281 case 0:
3282 break;
3283 default:
3284 printk( KERN_ERR \
3285 "md: fatal superblock inconsistency in %s"
3286 " -- removing from array\n",
3287 bdevname(rdev->bdev,b));
3288 kick_rdev_from_array(rdev);
3289 }
3290
3291
3292 super_types[mddev->major_version].
3293 validate_super(mddev, freshest);
3294
3295 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003296 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003297 if (mddev->max_disks &&
3298 (rdev->desc_nr >= mddev->max_disks ||
3299 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003300 printk(KERN_WARNING
3301 "md: %s: %s: only %d devices permitted\n",
3302 mdname(mddev), bdevname(rdev->bdev, b),
3303 mddev->max_disks);
3304 kick_rdev_from_array(rdev);
3305 continue;
3306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (rdev != freshest)
3308 if (super_types[mddev->major_version].
3309 validate_super(mddev, rdev)) {
3310 printk(KERN_WARNING "md: kicking non-fresh %s"
3311 " from array!\n",
3312 bdevname(rdev->bdev,b));
3313 kick_rdev_from_array(rdev);
3314 continue;
3315 }
3316 if (mddev->level == LEVEL_MULTIPATH) {
3317 rdev->desc_nr = i++;
3318 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003319 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003320 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003321 rdev->raid_disk = -1;
3322 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
NeilBrown72e02072009-12-14 12:49:55 +11003327/* Read a fixed-point number.
3328 * Numbers in sysfs attributes should be in "standard" units where
3329 * possible, so time should be in seconds.
3330 * However we internally use a a much smaller unit such as
3331 * milliseconds or jiffies.
3332 * This function takes a decimal number with a possible fractional
3333 * component, and produces an integer which is the result of
3334 * multiplying that number by 10^'scale'.
3335 * all without any floating-point arithmetic.
3336 */
3337int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3338{
3339 unsigned long result = 0;
3340 long decimals = -1;
3341 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3342 if (*cp == '.')
3343 decimals = 0;
3344 else if (decimals < scale) {
3345 unsigned int value;
3346 value = *cp - '0';
3347 result = result * 10 + value;
3348 if (decimals >= 0)
3349 decimals++;
3350 }
3351 cp++;
3352 }
3353 if (*cp == '\n')
3354 cp++;
3355 if (*cp)
3356 return -EINVAL;
3357 if (decimals < 0)
3358 decimals = 0;
3359 while (decimals < scale) {
3360 result *= 10;
3361 decimals ++;
3362 }
3363 *res = result;
3364 return 0;
3365}
3366
3367
NeilBrown19052c02008-08-05 15:54:13 +10003368static void md_safemode_timeout(unsigned long data);
3369
NeilBrowneae17012005-11-08 21:39:23 -08003370static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003371safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003372{
3373 int msec = (mddev->safemode_delay*1000)/HZ;
3374 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3375}
3376static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003377safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003378{
NeilBrown16f17b32006-06-26 00:27:37 -07003379 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003380
NeilBrown72e02072009-12-14 12:49:55 +11003381 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003382 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003383 if (msec == 0)
3384 mddev->safemode_delay = 0;
3385 else {
NeilBrown19052c02008-08-05 15:54:13 +10003386 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003387 mddev->safemode_delay = (msec*HZ)/1000;
3388 if (mddev->safemode_delay == 0)
3389 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003390 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003391 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003392 }
3393 return len;
3394}
3395static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003396__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003397
3398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003399level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003400{
NeilBrown84fc4b52011-10-11 16:49:58 +11003401 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003402 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003403 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003404 else if (mddev->clevel[0])
3405 return sprintf(page, "%s\n", mddev->clevel);
3406 else if (mddev->level != LEVEL_NONE)
3407 return sprintf(page, "%d\n", mddev->level);
3408 else
3409 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003410}
3411
NeilBrownd9d166c2006-01-06 00:20:51 -08003412static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003413level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003414{
Dan Williamsf2859af2010-05-02 10:04:16 -07003415 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003416 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003417 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003418 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003419 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003420 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003421
3422 if (mddev->pers == NULL) {
3423 if (len == 0)
3424 return 0;
3425 if (len >= sizeof(mddev->clevel))
3426 return -ENOSPC;
3427 strncpy(mddev->clevel, buf, len);
3428 if (mddev->clevel[len-1] == '\n')
3429 len--;
3430 mddev->clevel[len] = 0;
3431 mddev->level = LEVEL_NONE;
3432 return rv;
3433 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003434 if (mddev->ro)
3435 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003436
3437 /* request to change the personality. Need to ensure:
3438 * - array is not engaged in resync/recovery/reshape
3439 * - old personality can be suspended
3440 * - new personality will access other array.
3441 */
3442
NeilBrownbb4f1e92010-08-08 21:18:03 +10003443 if (mddev->sync_thread ||
3444 mddev->reshape_position != MaxSector ||
3445 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003446 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003447
3448 if (!mddev->pers->quiesce) {
3449 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3450 mdname(mddev), mddev->pers->name);
3451 return -EINVAL;
3452 }
3453
3454 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003455 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003456 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003457 strncpy(clevel, buf, len);
3458 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003459 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003460 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003461 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003462 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003463
Dan Williamsf2859af2010-05-02 10:04:16 -07003464 if (request_module("md-%s", clevel) != 0)
3465 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003466 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003467 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003468 if (!pers || !try_module_get(pers->owner)) {
3469 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003470 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003471 return -EINVAL;
3472 }
3473 spin_unlock(&pers_lock);
3474
3475 if (pers == mddev->pers) {
3476 /* Nothing to do! */
3477 module_put(pers->owner);
3478 return rv;
3479 }
3480 if (!pers->takeover) {
3481 module_put(pers->owner);
3482 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003483 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003484 return -EINVAL;
3485 }
3486
NeilBrowndafb20f2012-03-19 12:46:39 +11003487 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003488 rdev->new_raid_disk = rdev->raid_disk;
3489
NeilBrown245f46c2009-03-31 14:39:39 +11003490 /* ->takeover must set new_* and/or delta_disks
3491 * if it succeeds, and may set them when it fails.
3492 */
3493 priv = pers->takeover(mddev);
3494 if (IS_ERR(priv)) {
3495 mddev->new_level = mddev->level;
3496 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003497 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003498 mddev->raid_disks -= mddev->delta_disks;
3499 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003500 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003501 module_put(pers->owner);
3502 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003504 return PTR_ERR(priv);
3505 }
3506
3507 /* Looks like we have a winner */
3508 mddev_suspend(mddev);
3509 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003510
3511 if (mddev->pers->sync_request == NULL &&
3512 pers->sync_request != NULL) {
3513 /* need to add the md_redundancy_group */
3514 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3515 printk(KERN_WARNING
3516 "md: cannot register extra attributes for %s\n",
3517 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003518 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003519 }
3520 if (mddev->pers->sync_request != NULL &&
3521 pers->sync_request == NULL) {
3522 /* need to remove the md_redundancy_group */
3523 if (mddev->to_remove == NULL)
3524 mddev->to_remove = &md_redundancy_group;
3525 }
3526
Trela Maciej54071b32010-03-08 16:02:42 +11003527 if (mddev->pers->sync_request == NULL &&
3528 mddev->external) {
3529 /* We are converting from a no-redundancy array
3530 * to a redundancy array and metadata is managed
3531 * externally so we need to be sure that writes
3532 * won't block due to a need to transition
3533 * clean->dirty
3534 * until external management is started.
3535 */
3536 mddev->in_sync = 0;
3537 mddev->safemode_delay = 0;
3538 mddev->safemode = 0;
3539 }
3540
NeilBrowndafb20f2012-03-19 12:46:39 +11003541 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003542 if (rdev->raid_disk < 0)
3543 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003544 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003545 rdev->new_raid_disk = -1;
3546 if (rdev->new_raid_disk == rdev->raid_disk)
3547 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003548 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003549 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003550 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003551 if (rdev->raid_disk < 0)
3552 continue;
3553 if (rdev->new_raid_disk == rdev->raid_disk)
3554 continue;
3555 rdev->raid_disk = rdev->new_raid_disk;
3556 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003557 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003558 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003559 if (sysfs_link_rdev(mddev, rdev))
3560 printk(KERN_WARNING "md: cannot register rd%d"
3561 " for %s after level change\n",
3562 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003563 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003564 }
3565
3566 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003567 mddev->pers = pers;
3568 mddev->private = priv;
3569 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3570 mddev->level = mddev->new_level;
3571 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003572 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003573 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003574 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003575 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003576 if (mddev->pers->sync_request == NULL) {
3577 /* this is now an array without redundancy, so
3578 * it must always be in_sync
3579 */
3580 mddev->in_sync = 1;
3581 del_timer_sync(&mddev->safemode_timer);
3582 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003583 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003584 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003585 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003586 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003587 if (!mddev->thread)
3588 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003589 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003590 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003591 return rv;
3592}
3593
3594static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003595__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003596
NeilBrownd4dbd022006-06-26 00:27:59 -07003597
3598static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003599layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003600{
3601 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003602 if (mddev->reshape_position != MaxSector &&
3603 mddev->layout != mddev->new_layout)
3604 return sprintf(page, "%d (%d)\n",
3605 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003606 return sprintf(page, "%d\n", mddev->layout);
3607}
3608
3609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003610layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003611{
3612 char *e;
3613 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003614
3615 if (!*buf || (*e && *e != '\n'))
3616 return -EINVAL;
3617
NeilBrownb3546032009-03-31 14:56:41 +11003618 if (mddev->pers) {
3619 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003620 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003621 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003622 if (mddev->ro)
3623 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003624 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003625 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003626 if (err) {
3627 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003628 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003629 }
NeilBrownb3546032009-03-31 14:56:41 +11003630 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003631 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003632 if (mddev->reshape_position == MaxSector)
3633 mddev->layout = n;
3634 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003635 return len;
3636}
3637static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003638__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003639
3640
NeilBrowneae17012005-11-08 21:39:23 -08003641static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003642raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003643{
NeilBrownbb636542005-11-08 21:39:45 -08003644 if (mddev->raid_disks == 0)
3645 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003646 if (mddev->reshape_position != MaxSector &&
3647 mddev->delta_disks != 0)
3648 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3649 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003650 return sprintf(page, "%d\n", mddev->raid_disks);
3651}
3652
NeilBrownfd01b882011-10-11 16:47:53 +11003653static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003654
3655static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003656raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003657{
NeilBrownda943b992006-01-06 00:20:54 -08003658 char *e;
3659 int rv = 0;
3660 unsigned long n = simple_strtoul(buf, &e, 10);
3661
3662 if (!*buf || (*e && *e != '\n'))
3663 return -EINVAL;
3664
3665 if (mddev->pers)
3666 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003667 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003668 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003669 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003670
3671 rdev_for_each(rdev, mddev) {
3672 if (olddisks < n &&
3673 rdev->data_offset < rdev->new_data_offset)
3674 return -EINVAL;
3675 if (olddisks > n &&
3676 rdev->data_offset > rdev->new_data_offset)
3677 return -EINVAL;
3678 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003679 mddev->delta_disks = n - olddisks;
3680 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003681 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003682 } else
NeilBrownda943b992006-01-06 00:20:54 -08003683 mddev->raid_disks = n;
3684 return rv ? rv : len;
3685}
3686static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003687__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003688
NeilBrown24dd4692005-11-08 21:39:26 -08003689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003690chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003691{
NeilBrown08a02ec2007-05-09 02:35:38 -07003692 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003693 mddev->chunk_sectors != mddev->new_chunk_sectors)
3694 return sprintf(page, "%d (%d)\n",
3695 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003696 mddev->chunk_sectors << 9);
3697 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003698}
3699
3700static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003701chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003702{
NeilBrown3b343802006-01-06 00:20:47 -08003703 char *e;
3704 unsigned long n = simple_strtoul(buf, &e, 10);
3705
NeilBrown3b343802006-01-06 00:20:47 -08003706 if (!*buf || (*e && *e != '\n'))
3707 return -EINVAL;
3708
NeilBrownb3546032009-03-31 14:56:41 +11003709 if (mddev->pers) {
3710 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003711 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003712 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003713 if (mddev->ro)
3714 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003715 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003716 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003717 if (err) {
3718 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003719 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003720 }
NeilBrownb3546032009-03-31 14:56:41 +11003721 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003722 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003723 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003724 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003725 }
NeilBrown3b343802006-01-06 00:20:47 -08003726 return len;
3727}
3728static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003729__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003730
NeilBrowna94213b2006-06-26 00:28:00 -07003731static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003732resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003733{
NeilBrownd1a7c502009-03-31 15:24:32 +11003734 if (mddev->recovery_cp == MaxSector)
3735 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003736 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3737}
3738
3739static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003740resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003741{
NeilBrowna94213b2006-06-26 00:28:00 -07003742 char *e;
3743 unsigned long long n = simple_strtoull(buf, &e, 10);
3744
NeilBrownb0986362011-05-11 15:52:21 +10003745 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003746 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003747 if (cmd_match(buf, "none"))
3748 n = MaxSector;
3749 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003750 return -EINVAL;
3751
3752 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003753 if (mddev->pers)
3754 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003755 return len;
3756}
3757static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003758__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003759
NeilBrown9e653b62006-06-26 00:27:58 -07003760/*
3761 * The array state can be:
3762 *
3763 * clear
3764 * No devices, no size, no level
3765 * Equivalent to STOP_ARRAY ioctl
3766 * inactive
3767 * May have some settings, but array is not active
3768 * all IO results in error
3769 * When written, doesn't tear down array, but just stops it
3770 * suspended (not supported yet)
3771 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003772 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003773 * readonly
3774 * no resync can happen. no superblocks get written.
3775 * write requests fail
3776 * read-auto
3777 * like readonly, but behaves like 'clean' on a write request.
3778 *
3779 * clean - no pending writes, but otherwise active.
3780 * When written to inactive array, starts without resync
3781 * If a write request arrives then
3782 * if metadata is known, mark 'dirty' and switch to 'active'.
3783 * if not known, block and switch to write-pending
3784 * If written to an active array that has pending writes, then fails.
3785 * active
3786 * fully active: IO and resync can be happening.
3787 * When written to inactive array, starts with resync
3788 *
3789 * write-pending
3790 * clean, but writes are blocked waiting for 'active' to be written.
3791 *
3792 * active-idle
3793 * like active, but no writes have been seen for a while (100msec).
3794 *
3795 */
3796enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3797 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003798static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003799 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3800 "write-pending", "active-idle", NULL };
3801
3802static int match_word(const char *word, char **list)
3803{
3804 int n;
3805 for (n=0; list[n]; n++)
3806 if (cmd_match(word, list[n]))
3807 break;
3808 return n;
3809}
3810
3811static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003812array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003813{
3814 enum array_state st = inactive;
3815
3816 if (mddev->pers)
3817 switch(mddev->ro) {
3818 case 1:
3819 st = readonly;
3820 break;
3821 case 2:
3822 st = read_auto;
3823 break;
3824 case 0:
3825 if (mddev->in_sync)
3826 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003827 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003828 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003829 else if (mddev->safemode)
3830 st = active_idle;
3831 else
3832 st = active;
3833 }
3834 else {
3835 if (list_empty(&mddev->disks) &&
3836 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003837 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003838 st = clear;
3839 else
3840 st = inactive;
3841 }
3842 return sprintf(page, "%s\n", array_states[st]);
3843}
3844
NeilBrowna05b7ea2012-07-19 15:59:18 +10003845static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3846static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003847static int do_md_run(struct mddev * mddev);
3848static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003849
3850static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003851array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003852{
3853 int err = -EINVAL;
3854 enum array_state st = match_word(buf, array_states);
3855 switch(st) {
3856 case bad_word:
3857 break;
3858 case clear:
3859 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003860 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003861 break;
3862 case inactive:
3863 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003864 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003865 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003866 else
NeilBrowne6910632008-02-06 01:39:51 -08003867 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003868 break;
3869 case suspended:
3870 break; /* not supported yet */
3871 case readonly:
3872 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003873 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003874 else {
3875 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003876 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003877 err = do_md_run(mddev);
3878 }
3879 break;
3880 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003881 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003882 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003883 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003884 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003885 err = restart_array(mddev);
3886 if (err == 0) {
3887 mddev->ro = 2;
3888 set_disk_ro(mddev->gendisk, 0);
3889 }
NeilBrown9e653b62006-06-26 00:27:58 -07003890 } else {
3891 mddev->ro = 2;
3892 err = do_md_run(mddev);
3893 }
3894 break;
3895 case clean:
3896 if (mddev->pers) {
3897 restart_array(mddev);
3898 spin_lock_irq(&mddev->write_lock);
3899 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003900 if (mddev->in_sync == 0) {
3901 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003902 if (mddev->safemode == 1)
3903 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003904 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003905 }
3906 err = 0;
3907 } else
3908 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003909 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003910 } else
3911 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003912 break;
3913 case active:
3914 if (mddev->pers) {
3915 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003916 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003917 wake_up(&mddev->sb_wait);
3918 err = 0;
3919 } else {
3920 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003921 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003922 err = do_md_run(mddev);
3923 }
3924 break;
3925 case write_pending:
3926 case active_idle:
3927 /* these cannot be set */
3928 break;
3929 }
3930 if (err)
3931 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003932 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003933 if (mddev->hold_active == UNTIL_IOCTL)
3934 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003935 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003936 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003937 }
NeilBrown9e653b62006-06-26 00:27:58 -07003938}
NeilBrown80ca3a42006-07-10 04:44:18 -07003939static struct md_sysfs_entry md_array_state =
3940__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003941
NeilBrown6d7ff7382006-01-06 00:21:16 -08003942static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003943max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003944 return sprintf(page, "%d\n",
3945 atomic_read(&mddev->max_corr_read_errors));
3946}
3947
3948static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003949max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003950{
3951 char *e;
3952 unsigned long n = simple_strtoul(buf, &e, 10);
3953
3954 if (*buf && (*e == 0 || *e == '\n')) {
3955 atomic_set(&mddev->max_corr_read_errors, n);
3956 return len;
3957 }
3958 return -EINVAL;
3959}
3960
3961static struct md_sysfs_entry max_corr_read_errors =
3962__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3963 max_corrected_read_errors_store);
3964
3965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003966null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003967{
3968 return -EINVAL;
3969}
3970
3971static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003972new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003973{
3974 /* buf must be %d:%d\n? giving major and minor numbers */
3975 /* The new device is added to the array.
3976 * If the array has a persistent superblock, we read the
3977 * superblock to initialise info and check validity.
3978 * Otherwise, only checking done is that in bind_rdev_to_array,
3979 * which mainly checks size.
3980 */
3981 char *e;
3982 int major = simple_strtoul(buf, &e, 10);
3983 int minor;
3984 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003985 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003986 int err;
3987
3988 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3989 return -EINVAL;
3990 minor = simple_strtoul(e+1, &e, 10);
3991 if (*e && *e != '\n')
3992 return -EINVAL;
3993 dev = MKDEV(major, minor);
3994 if (major != MAJOR(dev) ||
3995 minor != MINOR(dev))
3996 return -EOVERFLOW;
3997
3998
3999 if (mddev->persistent) {
4000 rdev = md_import_device(dev, mddev->major_version,
4001 mddev->minor_version);
4002 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004003 struct md_rdev *rdev0
4004 = list_entry(mddev->disks.next,
4005 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004006 err = super_types[mddev->major_version]
4007 .load_super(rdev, rdev0, mddev->minor_version);
4008 if (err < 0)
4009 goto out;
4010 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004011 } else if (mddev->external)
4012 rdev = md_import_device(dev, -2, -1);
4013 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004014 rdev = md_import_device(dev, -1, -1);
4015
4016 if (IS_ERR(rdev))
4017 return PTR_ERR(rdev);
4018 err = bind_rdev_to_array(rdev, mddev);
4019 out:
4020 if (err)
4021 export_rdev(rdev);
4022 return err ? err : len;
4023}
4024
4025static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004026__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004027
4028static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004029bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004030{
4031 char *end;
4032 unsigned long chunk, end_chunk;
4033
4034 if (!mddev->bitmap)
4035 goto out;
4036 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4037 while (*buf) {
4038 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4039 if (buf == end) break;
4040 if (*end == '-') { /* range */
4041 buf = end + 1;
4042 end_chunk = simple_strtoul(buf, &end, 0);
4043 if (buf == end) break;
4044 }
4045 if (*end && !isspace(*end)) break;
4046 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004047 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004048 }
4049 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4050out:
4051 return len;
4052}
4053
4054static struct md_sysfs_entry md_bitmap =
4055__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4056
4057static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004058size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004059{
Andre Noll58c0fed2009-03-31 14:33:13 +11004060 return sprintf(page, "%llu\n",
4061 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004062}
4063
NeilBrownfd01b882011-10-11 16:47:53 +11004064static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004065
4066static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004067size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004068{
4069 /* If array is inactive, we can reduce the component size, but
4070 * not increase it (except from 0).
4071 * If array is active, we can try an on-line resize
4072 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004073 sector_t sectors;
4074 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004075
Andre Noll58c0fed2009-03-31 14:33:13 +11004076 if (err < 0)
4077 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004078 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004079 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004080 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004081 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004082 if (mddev->dev_sectors == 0 ||
4083 mddev->dev_sectors > sectors)
4084 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004085 else
4086 err = -ENOSPC;
4087 }
4088 return err ? err : len;
4089}
4090
4091static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004092__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004093
NeilBrown8bb93aa2006-01-06 00:20:50 -08004094
Masanari Iida83f0d772012-10-30 00:18:08 +09004095/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004096 * This is one of
4097 * 'none' for arrays with no metadata (good luck...)
4098 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004099 * or N.M for internally known formats
4100 */
4101static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004102metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004103{
4104 if (mddev->persistent)
4105 return sprintf(page, "%d.%d\n",
4106 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004107 else if (mddev->external)
4108 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004109 else
4110 return sprintf(page, "none\n");
4111}
4112
4113static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004114metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004115{
4116 int major, minor;
4117 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004118 /* Changing the details of 'external' metadata is
4119 * always permitted. Otherwise there must be
4120 * no devices attached to the array.
4121 */
4122 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4123 ;
4124 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004125 return -EBUSY;
4126
4127 if (cmd_match(buf, "none")) {
4128 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004129 mddev->external = 0;
4130 mddev->major_version = 0;
4131 mddev->minor_version = 90;
4132 return len;
4133 }
4134 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004135 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004136 if (namelen >= sizeof(mddev->metadata_type))
4137 namelen = sizeof(mddev->metadata_type)-1;
4138 strncpy(mddev->metadata_type, buf+9, namelen);
4139 mddev->metadata_type[namelen] = 0;
4140 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4141 mddev->metadata_type[--namelen] = 0;
4142 mddev->persistent = 0;
4143 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004144 mddev->major_version = 0;
4145 mddev->minor_version = 90;
4146 return len;
4147 }
4148 major = simple_strtoul(buf, &e, 10);
4149 if (e==buf || *e != '.')
4150 return -EINVAL;
4151 buf = e+1;
4152 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004153 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004154 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004155 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004156 return -ENOENT;
4157 mddev->major_version = major;
4158 mddev->minor_version = minor;
4159 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004160 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004161 return len;
4162}
4163
4164static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004165__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004166
NeilBrowna35b0d62006-01-06 00:20:49 -08004167static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004168action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004169{
NeilBrown7eec3142005-11-08 21:39:44 -08004170 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004171 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4172 type = "frozen";
4173 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004174 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004175 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4176 type = "reshape";
4177 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004178 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4179 type = "resync";
4180 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4181 type = "check";
4182 else
4183 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004184 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004185 type = "recover";
4186 }
4187 return sprintf(page, "%s\n", type);
4188}
4189
4190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004191action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004192{
NeilBrown7eec3142005-11-08 21:39:44 -08004193 if (!mddev->pers || !mddev->pers->sync_request)
4194 return -EINVAL;
4195
NeilBrownb6a9ce62009-05-26 09:41:17 +10004196 if (cmd_match(page, "frozen"))
4197 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4198 else
4199 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4200
4201 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004202 if (mddev->sync_thread) {
4203 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004204 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004205 }
NeilBrown03c902e2006-01-06 00:20:46 -08004206 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4207 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004208 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004209 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004210 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004211 else if (cmd_match(page, "recover")) {
4212 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4213 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4214 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004215 int err;
4216 if (mddev->pers->start_reshape == NULL)
4217 return -EINVAL;
4218 err = mddev->pers->start_reshape(mddev);
4219 if (err)
4220 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004221 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004222 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004223 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004224 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004225 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004226 return -EINVAL;
4227 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4228 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004229 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004230 if (mddev->ro == 2) {
4231 /* A write to sync_action is enough to justify
4232 * canceling read-auto mode
4233 */
4234 mddev->ro = 0;
4235 md_wakeup_thread(mddev->sync_thread);
4236 }
NeilBrown03c902e2006-01-06 00:20:46 -08004237 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004238 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004239 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004240 return len;
4241}
4242
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004243static struct md_sysfs_entry md_scan_mode =
4244__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4245
4246static ssize_t
4247last_sync_action_show(struct mddev *mddev, char *page)
4248{
4249 return sprintf(page, "%s\n", mddev->last_sync_action);
4250}
4251
4252static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4253
NeilBrown9d888832005-11-08 21:39:26 -08004254static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004255mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004256{
4257 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004258 (unsigned long long)
4259 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004260}
4261
NeilBrown80ca3a42006-07-10 04:44:18 -07004262static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004263
NeilBrown88202a02006-01-06 00:21:36 -08004264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004265sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004266{
4267 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4268 mddev->sync_speed_min ? "local": "system");
4269}
4270
4271static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004272sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004273{
4274 int min;
4275 char *e;
4276 if (strncmp(buf, "system", 6)==0) {
4277 mddev->sync_speed_min = 0;
4278 return len;
4279 }
4280 min = simple_strtoul(buf, &e, 10);
4281 if (buf == e || (*e && *e != '\n') || min <= 0)
4282 return -EINVAL;
4283 mddev->sync_speed_min = min;
4284 return len;
4285}
4286
4287static struct md_sysfs_entry md_sync_min =
4288__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4289
4290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004292{
4293 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4294 mddev->sync_speed_max ? "local": "system");
4295}
4296
4297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004298sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004299{
4300 int max;
4301 char *e;
4302 if (strncmp(buf, "system", 6)==0) {
4303 mddev->sync_speed_max = 0;
4304 return len;
4305 }
4306 max = simple_strtoul(buf, &e, 10);
4307 if (buf == e || (*e && *e != '\n') || max <= 0)
4308 return -EINVAL;
4309 mddev->sync_speed_max = max;
4310 return len;
4311}
4312
4313static struct md_sysfs_entry md_sync_max =
4314__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4315
Iustin Popd7f3d292007-10-16 23:30:54 -07004316static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004317degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004318{
4319 return sprintf(page, "%d\n", mddev->degraded);
4320}
4321static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004325{
4326 return sprintf(page, "%d\n", mddev->parallel_resync);
4327}
4328
4329static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004330sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004331{
4332 long n;
4333
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004334 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004335 return -EINVAL;
4336
4337 if (n != 0 && n != 1)
4338 return -EINVAL;
4339
4340 mddev->parallel_resync = n;
4341
4342 if (mddev->sync_thread)
4343 wake_up(&resync_wait);
4344
4345 return len;
4346}
4347
4348/* force parallel resync, even with shared block devices */
4349static struct md_sysfs_entry md_sync_force_parallel =
4350__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4351 sync_force_parallel_show, sync_force_parallel_store);
4352
4353static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004354sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004355{
4356 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004357 if (mddev->curr_resync == 0)
4358 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004359 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4360 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004361 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004362 db = resync - mddev->resync_mark_cnt;
4363 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004364}
4365
NeilBrown80ca3a42006-07-10 04:44:18 -07004366static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004367
4368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004370{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004371 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004372
NeilBrownacb180b2009-04-14 16:28:34 +10004373 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4374 return sprintf(page, "none\n");
4375
NeilBrown72f36d52012-10-11 14:25:57 +11004376 if (mddev->curr_resync == 1 ||
4377 mddev->curr_resync == 2)
4378 return sprintf(page, "delayed\n");
4379
NeilBrownc804cde2012-05-21 09:28:33 +10004380 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4381 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004382 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004383 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004384 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004385
NeilBrownacb180b2009-04-14 16:28:34 +10004386 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004387 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004388}
4389
NeilBrown80ca3a42006-07-10 04:44:18 -07004390static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004391
NeilBrowne464eaf2006-03-27 01:18:14 -08004392static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004393min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004394{
4395 return sprintf(page, "%llu\n",
4396 (unsigned long long)mddev->resync_min);
4397}
4398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004399min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004400{
4401 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004402 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004403 return -EINVAL;
4404 if (min > mddev->resync_max)
4405 return -EINVAL;
4406 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4407 return -EBUSY;
4408
4409 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004410 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004411 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004412 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004413 return -EINVAL;
4414 }
4415 mddev->resync_min = min;
4416
4417 return len;
4418}
4419
4420static struct md_sysfs_entry md_min_sync =
4421__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4422
4423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004424max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004425{
4426 if (mddev->resync_max == MaxSector)
4427 return sprintf(page, "max\n");
4428 else
4429 return sprintf(page, "%llu\n",
4430 (unsigned long long)mddev->resync_max);
4431}
4432static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004433max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004434{
4435 if (strncmp(buf, "max", 3) == 0)
4436 mddev->resync_max = MaxSector;
4437 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004438 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004439 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004440 return -EINVAL;
4441 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004442 return -EINVAL;
4443 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004444 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004445 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4446 return -EBUSY;
4447
4448 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004449 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004450 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004451 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004452 return -EINVAL;
4453 }
4454 mddev->resync_max = max;
4455 }
4456 wake_up(&mddev->recovery_wait);
4457 return len;
4458}
4459
4460static struct md_sysfs_entry md_max_sync =
4461__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4462
4463static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004464suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004465{
4466 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4467}
4468
4469static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004470suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004471{
4472 char *e;
4473 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004474 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004475
NeilBrownb8d966e2009-07-01 11:14:04 +10004476 if (mddev->pers == NULL ||
4477 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004478 return -EINVAL;
4479 if (buf == e || (*e && *e != '\n'))
4480 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004481
4482 mddev->suspend_lo = new;
4483 if (new >= old)
4484 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004485 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004486 else {
4487 /* Expanding suspended region - need to wait */
4488 mddev->pers->quiesce(mddev, 1);
4489 mddev->pers->quiesce(mddev, 0);
4490 }
4491 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004492}
4493static struct md_sysfs_entry md_suspend_lo =
4494__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4495
4496
4497static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004498suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004499{
4500 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4501}
4502
4503static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004504suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004505{
4506 char *e;
4507 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004508 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004509
NeilBrownb8d966e2009-07-01 11:14:04 +10004510 if (mddev->pers == NULL ||
4511 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004512 return -EINVAL;
4513 if (buf == e || (*e && *e != '\n'))
4514 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004515
4516 mddev->suspend_hi = new;
4517 if (new <= old)
4518 /* Shrinking suspended region */
4519 mddev->pers->quiesce(mddev, 2);
4520 else {
4521 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004522 mddev->pers->quiesce(mddev, 1);
4523 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004524 }
4525 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004526}
4527static struct md_sysfs_entry md_suspend_hi =
4528__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4529
NeilBrown08a02ec2007-05-09 02:35:38 -07004530static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004531reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004532{
4533 if (mddev->reshape_position != MaxSector)
4534 return sprintf(page, "%llu\n",
4535 (unsigned long long)mddev->reshape_position);
4536 strcpy(page, "none\n");
4537 return 5;
4538}
4539
4540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004541reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004542{
NeilBrownc6563a82012-05-21 09:27:00 +10004543 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004544 char *e;
4545 unsigned long long new = simple_strtoull(buf, &e, 10);
4546 if (mddev->pers)
4547 return -EBUSY;
4548 if (buf == e || (*e && *e != '\n'))
4549 return -EINVAL;
4550 mddev->reshape_position = new;
4551 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004552 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004553 mddev->new_level = mddev->level;
4554 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004555 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004556 rdev_for_each(rdev, mddev)
4557 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004558 return len;
4559}
4560
4561static struct md_sysfs_entry md_reshape_position =
4562__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4563 reshape_position_store);
4564
Dan Williamsb522adc2009-03-31 15:00:31 +11004565static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004566reshape_direction_show(struct mddev *mddev, char *page)
4567{
4568 return sprintf(page, "%s\n",
4569 mddev->reshape_backwards ? "backwards" : "forwards");
4570}
4571
4572static ssize_t
4573reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4574{
4575 int backwards = 0;
4576 if (cmd_match(buf, "forwards"))
4577 backwards = 0;
4578 else if (cmd_match(buf, "backwards"))
4579 backwards = 1;
4580 else
4581 return -EINVAL;
4582 if (mddev->reshape_backwards == backwards)
4583 return len;
4584
4585 /* check if we are allowed to change */
4586 if (mddev->delta_disks)
4587 return -EBUSY;
4588
4589 if (mddev->persistent &&
4590 mddev->major_version == 0)
4591 return -EINVAL;
4592
4593 mddev->reshape_backwards = backwards;
4594 return len;
4595}
4596
4597static struct md_sysfs_entry md_reshape_direction =
4598__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4599 reshape_direction_store);
4600
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004603{
4604 if (mddev->external_size)
4605 return sprintf(page, "%llu\n",
4606 (unsigned long long)mddev->array_sectors/2);
4607 else
4608 return sprintf(page, "default\n");
4609}
4610
4611static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004612array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004613{
4614 sector_t sectors;
4615
4616 if (strncmp(buf, "default", 7) == 0) {
4617 if (mddev->pers)
4618 sectors = mddev->pers->size(mddev, 0, 0);
4619 else
4620 sectors = mddev->array_sectors;
4621
4622 mddev->external_size = 0;
4623 } else {
4624 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4625 return -EINVAL;
4626 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004627 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004628
4629 mddev->external_size = 1;
4630 }
4631
4632 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004633 if (mddev->pers) {
4634 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004635 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004636 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004637 return len;
4638}
4639
4640static struct md_sysfs_entry md_array_size =
4641__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4642 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004643
NeilBrowneae17012005-11-08 21:39:23 -08004644static struct attribute *md_default_attrs[] = {
4645 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004646 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004647 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004648 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004649 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004650 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004651 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004652 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004653 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004654 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004655 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004656 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004657 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004658 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004659 NULL,
4660};
4661
4662static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004663 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004664 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004665 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004666 &md_sync_min.attr,
4667 &md_sync_max.attr,
4668 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004669 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004670 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004671 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004672 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004673 &md_suspend_lo.attr,
4674 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004675 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004676 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004677 NULL,
4678};
NeilBrown411036f2005-11-08 21:39:40 -08004679static struct attribute_group md_redundancy_group = {
4680 .name = NULL,
4681 .attrs = md_redundancy_attrs,
4682};
4683
NeilBrowneae17012005-11-08 21:39:23 -08004684
4685static ssize_t
4686md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4687{
4688 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004689 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004690 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004691
4692 if (!entry->show)
4693 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004694 spin_lock(&all_mddevs_lock);
4695 if (list_empty(&mddev->all_mddevs)) {
4696 spin_unlock(&all_mddevs_lock);
4697 return -EBUSY;
4698 }
4699 mddev_get(mddev);
4700 spin_unlock(&all_mddevs_lock);
4701
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004702 rv = mddev_lock(mddev);
4703 if (!rv) {
4704 rv = entry->show(mddev, page);
4705 mddev_unlock(mddev);
4706 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004707 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004708 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004709}
4710
4711static ssize_t
4712md_attr_store(struct kobject *kobj, struct attribute *attr,
4713 const char *page, size_t length)
4714{
4715 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004716 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004717 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004718
4719 if (!entry->store)
4720 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004721 if (!capable(CAP_SYS_ADMIN))
4722 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004723 spin_lock(&all_mddevs_lock);
4724 if (list_empty(&mddev->all_mddevs)) {
4725 spin_unlock(&all_mddevs_lock);
4726 return -EBUSY;
4727 }
4728 mddev_get(mddev);
4729 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004730 if (entry->store == new_dev_store)
4731 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004732 rv = mddev_lock(mddev);
4733 if (!rv) {
4734 rv = entry->store(mddev, page, length);
4735 mddev_unlock(mddev);
4736 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004737 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004738 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004739}
4740
4741static void md_free(struct kobject *ko)
4742{
NeilBrownfd01b882011-10-11 16:47:53 +11004743 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004744
4745 if (mddev->sysfs_state)
4746 sysfs_put(mddev->sysfs_state);
4747
4748 if (mddev->gendisk) {
4749 del_gendisk(mddev->gendisk);
4750 put_disk(mddev->gendisk);
4751 }
4752 if (mddev->queue)
4753 blk_cleanup_queue(mddev->queue);
4754
NeilBrowneae17012005-11-08 21:39:23 -08004755 kfree(mddev);
4756}
4757
Emese Revfy52cf25d2010-01-19 02:58:23 +01004758static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004759 .show = md_attr_show,
4760 .store = md_attr_store,
4761};
4762static struct kobj_type md_ktype = {
4763 .release = md_free,
4764 .sysfs_ops = &md_sysfs_ops,
4765 .default_attrs = md_default_attrs,
4766};
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768int mdp_major = 0;
4769
Dan Williams5fd3a172009-03-04 00:57:25 -07004770static void mddev_delayed_delete(struct work_struct *ws)
4771{
NeilBrownfd01b882011-10-11 16:47:53 +11004772 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004773
NeilBrown43a70502009-12-14 12:49:55 +11004774 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004775 kobject_del(&mddev->kobj);
4776 kobject_put(&mddev->kobj);
4777}
4778
NeilBrownefeb53c2009-01-09 08:31:10 +11004779static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004781 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004782 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004784 int partitioned;
4785 int shift;
4786 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004787 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004790 return -ENODEV;
4791
4792 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4793 shift = partitioned ? MdpMinorShift : 0;
4794 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
Tejun Heoe804ac72010-10-15 15:36:08 +02004796 /* wait for any previous instance of this device to be
4797 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004798 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004799 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004800
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004801 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004802 error = -EEXIST;
4803 if (mddev->gendisk)
4804 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004805
4806 if (name) {
4807 /* Need to ensure that 'name' is not a duplicate.
4808 */
NeilBrownfd01b882011-10-11 16:47:53 +11004809 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004810 spin_lock(&all_mddevs_lock);
4811
4812 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4813 if (mddev2->gendisk &&
4814 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4815 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004816 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004817 }
4818 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
NeilBrown8b765392009-01-09 08:31:08 +11004820
NeilBrown0909dc42009-07-01 12:27:21 +10004821 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004822 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004823 if (!mddev->queue)
4824 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004825 mddev->queue->queuedata = mddev;
4826
NeilBrown409c57f2009-03-31 14:39:39 +11004827 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004828 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 disk = alloc_disk(1 << shift);
4831 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004832 blk_cleanup_queue(mddev->queue);
4833 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004834 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004836 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004838 if (name)
4839 strcpy(disk->disk_name, name);
4840 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004842 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 disk->fops = &md_fops;
4845 disk->private_data = mddev;
4846 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004847 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004848 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004849 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004850 * remove it now.
4851 */
4852 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004854 /* As soon as we call add_disk(), another thread could get
4855 * through to md_open, so make sure it doesn't get too far
4856 */
4857 mutex_lock(&mddev->open_mutex);
4858 add_disk(disk);
4859
Tejun Heoed9e1982008-08-25 19:56:05 +09004860 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4861 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004862 if (error) {
4863 /* This isn't possible, but as kobject_init_and_add is marked
4864 * __must_check, we must do something with the result
4865 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004866 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4867 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004868 error = 0;
4869 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004870 if (mddev->kobj.sd &&
4871 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004872 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004873 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004874 abort:
4875 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004876 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004877 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004878 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004879 }
NeilBrownd3374822009-01-09 08:31:10 +11004880 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004881 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004882}
4883
4884static struct kobject *md_probe(dev_t dev, int *part, void *data)
4885{
4886 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return NULL;
4888}
4889
NeilBrownefeb53c2009-01-09 08:31:10 +11004890static int add_named_array(const char *val, struct kernel_param *kp)
4891{
4892 /* val must be "md_*" where * is not all digits.
4893 * We allocate an array with a large free minor number, and
4894 * set the name to val. val must not already be an active name.
4895 */
4896 int len = strlen(val);
4897 char buf[DISK_NAME_LEN];
4898
4899 while (len && val[len-1] == '\n')
4900 len--;
4901 if (len >= DISK_NAME_LEN)
4902 return -E2BIG;
4903 strlcpy(buf, val, len+1);
4904 if (strncmp(buf, "md_", 3) != 0)
4905 return -EINVAL;
4906 return md_alloc(0, buf);
4907}
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909static void md_safemode_timeout(unsigned long data)
4910{
NeilBrownfd01b882011-10-11 16:47:53 +11004911 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Neil Brown0fd62b82008-06-28 08:31:36 +10004913 if (!atomic_read(&mddev->writes_pending)) {
4914 mddev->safemode = 1;
4915 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004916 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 md_wakeup_thread(mddev->thread);
4919}
4920
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004921static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
NeilBrownfd01b882011-10-11 16:47:53 +11004923int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924{
NeilBrown2604b702006-01-06 00:20:36 -08004925 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004926 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004927 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
NeilBrowna757e642005-04-16 15:26:42 -07004929 if (list_empty(&mddev->disks))
4930 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 if (mddev->pers)
4934 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004935 /* Cannot run until previous stop completes properly */
4936 if (mddev->sysfs_active)
4937 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 /*
4940 * Analyze all RAID superblock(s)
4941 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004942 if (!mddev->raid_disks) {
4943 if (!mddev->persistent)
4944 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004945 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
NeilBrownd9d166c2006-01-06 00:20:51 -08004948 if (mddev->level != LEVEL_NONE)
4949 request_module("md-level-%d", mddev->level);
4950 else if (mddev->clevel[0])
4951 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 /*
4954 * Drop all container device buffers, from now on
4955 * the only valid external interface is through the md
4956 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004958 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004959 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 continue;
4961 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004962 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004963
4964 /* perform some consistency tests on the device.
4965 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004966 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004967 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004968 if (rdev->meta_bdev) {
4969 /* Nothing to check */;
4970 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004971 if (mddev->dev_sectors &&
4972 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004973 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004974 printk("md: %s: data overlaps metadata\n",
4975 mdname(mddev));
4976 return -EINVAL;
4977 }
4978 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004979 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004980 > rdev->data_offset) {
4981 printk("md: %s: metadata overlaps data\n",
4982 mdname(mddev));
4983 return -EINVAL;
4984 }
4985 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004986 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988
NeilBrowna167f662010-10-26 18:31:13 +11004989 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07004990 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11004991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004993 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004994 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004996 if (mddev->level != LEVEL_NONE)
4997 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4998 mddev->level);
4999 else
5000 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5001 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return -EINVAL;
5003 }
NeilBrown2604b702006-01-06 00:20:36 -08005004 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005006 if (mddev->level != pers->level) {
5007 mddev->level = pers->level;
5008 mddev->new_level = pers->level;
5009 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005010 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
NeilBrownf6705572006-03-27 01:18:11 -08005012 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005013 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005014 /* This personality cannot handle reshaping... */
5015 mddev->pers = NULL;
5016 module_put(pers->owner);
5017 return -EINVAL;
5018 }
5019
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005020 if (pers->sync_request) {
5021 /* Warn if this is a potentially silly
5022 * configuration.
5023 */
5024 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005025 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005026 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005027
NeilBrowndafb20f2012-03-19 12:46:39 +11005028 rdev_for_each(rdev, mddev)
5029 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005030 if (rdev < rdev2 &&
5031 rdev->bdev->bd_contains ==
5032 rdev2->bdev->bd_contains) {
5033 printk(KERN_WARNING
5034 "%s: WARNING: %s appears to be"
5035 " on the same physical disk as"
5036 " %s.\n",
5037 mdname(mddev),
5038 bdevname(rdev->bdev,b),
5039 bdevname(rdev2->bdev,b2));
5040 warned = 1;
5041 }
5042 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005043
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005044 if (warned)
5045 printk(KERN_WARNING
5046 "True protection against single-disk"
5047 " failure might be compromised.\n");
5048 }
5049
NeilBrown657390d2005-08-26 18:34:16 -07005050 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005051 /* may be over-ridden by personality */
5052 mddev->resync_max_sectors = mddev->dev_sectors;
5053
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005054 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
NeilBrown0f9552b52009-12-30 12:08:50 +11005056 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005057 mddev->ro = 2; /* read-only, but switch on first write */
5058
NeilBrownb15c2e52006-01-06 00:20:16 -08005059 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005060 if (err)
5061 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005062 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5063 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5064 " but 'external_size' not in effect?\n", __func__);
5065 printk(KERN_ERR
5066 "md: invalid array_size %llu > default size %llu\n",
5067 (unsigned long long)mddev->array_sectors / 2,
5068 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5069 err = -EINVAL;
5070 mddev->pers->stop(mddev);
5071 }
NeilBrownef99bf42012-05-22 13:55:08 +10005072 if (err == 0 && mddev->pers->sync_request &&
5073 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005074 err = bitmap_create(mddev);
5075 if (err) {
5076 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5077 mdname(mddev), err);
5078 mddev->pers->stop(mddev);
5079 }
5080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 module_put(mddev->pers->owner);
5083 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005084 bitmap_destroy(mddev);
5085 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005087 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005088 if (mddev->kobj.sd &&
5089 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005090 printk(KERN_WARNING
5091 "md: cannot register extra attributes for %s\n",
5092 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005093 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005094 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005095 mddev->ro = 0;
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005098 atomic_set(&mddev->max_corr_read_errors,
5099 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 mddev->safemode = 0;
5101 mddev->safemode_timer.function = md_safemode_timeout;
5102 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005103 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005105 smp_wmb();
5106 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005107 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005108 if (rdev->raid_disk >= 0)
5109 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005110 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
5112 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5113
NeilBrown7a0a5352013-08-27 16:28:23 +10005114 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07005115 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
NeilBrownd7603b72006-01-06 00:20:30 -08005117 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005118 sysfs_notify_dirent_safe(mddev->sysfs_state);
5119 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005120 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return 0;
5122}
NeilBrown390ee602010-06-01 19:37:27 +10005123EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
NeilBrownfd01b882011-10-11 16:47:53 +11005125static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005126{
5127 int err;
5128
5129 err = md_run(mddev);
5130 if (err)
5131 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005132 err = bitmap_load(mddev);
5133 if (err) {
5134 bitmap_destroy(mddev);
5135 goto out;
5136 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005137
5138 md_wakeup_thread(mddev->thread);
5139 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5140
NeilBrownfe60b012010-03-29 11:10:42 +11005141 set_capacity(mddev->gendisk, mddev->array_sectors);
5142 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005143 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005144 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5145out:
5146 return err;
5147}
5148
NeilBrownfd01b882011-10-11 16:47:53 +11005149static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150{
5151 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Andre Noll80fab1d2008-07-11 22:02:21 +10005153 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005155 return -ENXIO;
5156 if (!mddev->pers)
5157 return -EINVAL;
5158 if (!mddev->ro)
5159 return -EBUSY;
5160 mddev->safemode = 0;
5161 mddev->ro = 0;
5162 set_disk_ro(disk, 0);
5163 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5164 mdname(mddev));
5165 /* Kick recovery or resync if necessary */
5166 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5167 md_wakeup_thread(mddev->thread);
5168 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005169 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
NeilBrownfd01b882011-10-11 16:47:53 +11005173static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005174{
5175 mddev->array_sectors = 0;
5176 mddev->external_size = 0;
5177 mddev->dev_sectors = 0;
5178 mddev->raid_disks = 0;
5179 mddev->recovery_cp = 0;
5180 mddev->resync_min = 0;
5181 mddev->resync_max = MaxSector;
5182 mddev->reshape_position = MaxSector;
5183 mddev->external = 0;
5184 mddev->persistent = 0;
5185 mddev->level = LEVEL_NONE;
5186 mddev->clevel[0] = 0;
5187 mddev->flags = 0;
5188 mddev->ro = 0;
5189 mddev->metadata_type[0] = 0;
5190 mddev->chunk_sectors = 0;
5191 mddev->ctime = mddev->utime = 0;
5192 mddev->layout = 0;
5193 mddev->max_disks = 0;
5194 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005195 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005196 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005197 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005198 mddev->new_level = LEVEL_NONE;
5199 mddev->new_layout = 0;
5200 mddev->new_chunk_sectors = 0;
5201 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005202 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005203 mddev->suspend_lo = mddev->suspend_hi = 0;
5204 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5205 mddev->recovery = 0;
5206 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005207 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005208 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005209 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005210 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005211 mddev->bitmap_info.offset = 0;
5212 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005213 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005214 mddev->bitmap_info.chunksize = 0;
5215 mddev->bitmap_info.daemon_sleep = 0;
5216 mddev->bitmap_info.max_write_behind = 0;
5217}
5218
NeilBrownfd01b882011-10-11 16:47:53 +11005219static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005220{
NeilBrown6b6204e2013-05-09 09:48:30 +10005221 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005222 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005223 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005224 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005225 }
5226
5227 del_timer_sync(&mddev->safemode_timer);
5228
5229 bitmap_flush(mddev);
5230 md_super_wait(mddev);
5231
NeilBrownb6d428c2013-04-24 11:42:42 +10005232 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005233 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005234 /* mark array as shutdown cleanly */
5235 mddev->in_sync = 1;
5236 md_update_sb(mddev, 1);
5237 }
5238}
NeilBrowndefad612011-01-14 09:14:33 +11005239
NeilBrownfd01b882011-10-11 16:47:53 +11005240void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005241{
NeilBrown29f097c2013-11-14 17:54:51 +11005242 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005243 __md_stop_writes(mddev);
5244 mddev_unlock(mddev);
5245}
NeilBrown390ee602010-06-01 19:37:27 +10005246EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005247
NeilBrown5eff3c42012-11-19 10:47:48 +11005248static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005249{
NeilBrown0ca69882011-01-14 09:14:33 +11005250 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005251 mddev->pers->stop(mddev);
5252 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5253 mddev->to_remove = &md_redundancy_group;
5254 module_put(mddev->pers->owner);
5255 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005256 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005257}
NeilBrown5eff3c42012-11-19 10:47:48 +11005258
5259void md_stop(struct mddev *mddev)
5260{
5261 /* stop the array and free an attached data structures.
5262 * This is called from dm-raid
5263 */
5264 __md_stop(mddev);
5265 bitmap_destroy(mddev);
5266 if (mddev->bio_set)
5267 bioset_free(mddev->bio_set);
5268}
5269
NeilBrown390ee602010-06-01 19:37:27 +10005270EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005271
NeilBrowna05b7ea2012-07-19 15:59:18 +10005272static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005273{
5274 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005275 int did_freeze = 0;
5276
5277 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5278 did_freeze = 1;
5279 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5280 md_wakeup_thread(mddev->thread);
5281 }
5282 if (mddev->sync_thread) {
5283 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5284 /* Thread might be blocked waiting for metadata update
5285 * which will now never happen */
5286 wake_up_process(mddev->sync_thread->tsk);
5287 }
5288 mddev_unlock(mddev);
5289 wait_event(resync_wait, mddev->sync_thread == NULL);
5290 mddev_lock_nointr(mddev);
5291
NeilBrowna4bd82d2010-03-29 13:23:10 +11005292 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005293 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005294 mddev->sync_thread ||
5295 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005296 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005297 if (did_freeze) {
5298 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5299 md_wakeup_thread(mddev->thread);
5300 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005301 err = -EBUSY;
5302 goto out;
5303 }
5304 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005305 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005306
5307 err = -ENXIO;
5308 if (mddev->ro==1)
5309 goto out;
5310 mddev->ro = 1;
5311 set_disk_ro(mddev->gendisk, 1);
5312 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005313 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005314 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005315 }
5316out:
5317 mutex_unlock(&mddev->open_mutex);
5318 return err;
5319}
5320
NeilBrown9e653b62006-06-26 00:27:58 -07005321/* mode:
5322 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005323 * 2 - stop but do not disassemble array
5324 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005325static int do_md_stop(struct mddev * mddev, int mode,
5326 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005329 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005330 int did_freeze = 0;
5331
5332 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5333 did_freeze = 1;
5334 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5335 md_wakeup_thread(mddev->thread);
5336 }
5337 if (mddev->sync_thread) {
5338 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5339 /* Thread might be blocked waiting for metadata update
5340 * which will now never happen */
5341 wake_up_process(mddev->sync_thread->tsk);
5342 }
5343 mddev_unlock(mddev);
5344 wait_event(resync_wait, mddev->sync_thread == NULL);
5345 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrownc8c00a62009-08-10 12:50:52 +10005347 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005348 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005349 mddev->sysfs_active ||
5350 mddev->sync_thread ||
5351 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005352 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005353 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005354 if (did_freeze) {
5355 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5356 md_wakeup_thread(mddev->thread);
5357 }
NeilBrown260fa032013-08-27 16:44:13 +10005358 return -EBUSY;
5359 }
NeilBrown6e17b022010-08-07 21:41:19 +10005360 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005361 if (mddev->ro)
5362 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005363
NeilBrowndefad612011-01-14 09:14:33 +11005364 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005365 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005366 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005367 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005368
NeilBrowna4bd82d2010-03-29 13:23:10 +11005369 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005370 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005371
NeilBrowndafb20f2012-03-19 12:46:39 +11005372 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005373 if (rdev->raid_disk >= 0)
5374 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005375
NeilBrowna4bd82d2010-03-29 13:23:10 +11005376 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005377 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005378 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005379 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005380
NeilBrowna4bd82d2010-03-29 13:23:10 +11005381 if (mddev->ro)
5382 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005383 } else
5384 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 /*
5386 * Free resources if final stop
5387 */
NeilBrown9e653b62006-06-26 00:27:58 -07005388 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5390
NeilBrown978f9462006-02-02 14:28:05 -08005391 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005392 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005393 fput(mddev->bitmap_info.file);
5394 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005395 }
NeilBrownc3d97142009-12-14 12:49:52 +11005396 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 export_array(mddev);
5399
NeilBrown6177b472010-03-29 11:37:13 +11005400 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005401 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005402 if (mddev->hold_active == UNTIL_STOP)
5403 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005404 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005405 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005406 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005407 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005411#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005412static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
NeilBrown3cb03002011-10-11 16:45:26 +11005414 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 int err;
5416
NeilBrowna757e642005-04-16 15:26:42 -07005417 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
5420 printk(KERN_INFO "md: running: ");
5421
NeilBrowndafb20f2012-03-19 12:46:39 +11005422 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 char b[BDEVNAME_SIZE];
5424 printk("<%s>", bdevname(rdev->bdev,b));
5425 }
5426 printk("\n");
5427
NeilBrownd710e132008-10-13 11:55:12 +11005428 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 if (err) {
5430 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005431 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433}
5434
5435/*
5436 * lets try to run arrays based on all disks that have arrived
5437 * until now. (those are in pending_raid_disks)
5438 *
5439 * the method: pick the first pending disk, collect all disks with
5440 * the same UUID, remove all from the pending list and put them into
5441 * the 'same_array' list. Then order this list based on superblock
5442 * update time (freshest comes first), kick out 'old' disks and
5443 * compare superblocks. If everything's fine then run it.
5444 *
5445 * If "unit" is allocated, then bump its reference count
5446 */
5447static void autorun_devices(int part)
5448{
NeilBrown3cb03002011-10-11 16:45:26 +11005449 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005450 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 char b[BDEVNAME_SIZE];
5452
5453 printk(KERN_INFO "md: autorun ...\n");
5454 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005455 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005457 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005459 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461 printk(KERN_INFO "md: considering %s ...\n",
5462 bdevname(rdev0->bdev,b));
5463 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005464 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 if (super_90_load(rdev, rdev0, 0) >= 0) {
5466 printk(KERN_INFO "md: adding %s ...\n",
5467 bdevname(rdev->bdev,b));
5468 list_move(&rdev->same_set, &candidates);
5469 }
5470 /*
5471 * now we have a set of devices, with all of them having
5472 * mostly sane superblocks. It's time to allocate the
5473 * mddev.
5474 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005475 if (part) {
5476 dev = MKDEV(mdp_major,
5477 rdev0->preferred_minor << MdpMinorShift);
5478 unit = MINOR(dev) >> MdpMinorShift;
5479 } else {
5480 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5481 unit = MINOR(dev);
5482 }
5483 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5485 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5486 break;
5487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 md_probe(dev, NULL, NULL);
5490 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005491 if (!mddev || !mddev->gendisk) {
5492 if (mddev)
5493 mddev_put(mddev);
5494 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 "md: cannot allocate memory for md drive.\n");
5496 break;
5497 }
5498 if (mddev_lock(mddev))
5499 printk(KERN_WARNING "md: %s locked, cannot run\n",
5500 mdname(mddev));
5501 else if (mddev->raid_disks || mddev->major_version
5502 || !list_empty(&mddev->disks)) {
5503 printk(KERN_WARNING
5504 "md: %s already running, cannot run %s\n",
5505 mdname(mddev), bdevname(rdev0->bdev,b));
5506 mddev_unlock(mddev);
5507 } else {
5508 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005509 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005510 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 list_del_init(&rdev->same_set);
5512 if (bind_rdev_to_array(rdev, mddev))
5513 export_rdev(rdev);
5514 }
5515 autorun_array(mddev);
5516 mddev_unlock(mddev);
5517 }
5518 /* on success, candidates will be empty, on error
5519 * it won't...
5520 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005521 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005522 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 mddev_put(mddev);
5526 }
5527 printk(KERN_INFO "md: ... autorun DONE.\n");
5528}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005529#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531static int get_version(void __user * arg)
5532{
5533 mdu_version_t ver;
5534
5535 ver.major = MD_MAJOR_VERSION;
5536 ver.minor = MD_MINOR_VERSION;
5537 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5538
5539 if (copy_to_user(arg, &ver, sizeof(ver)))
5540 return -EFAULT;
5541
5542 return 0;
5543}
5544
NeilBrownfd01b882011-10-11 16:47:53 +11005545static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
5547 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005548 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005549 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
NeilBrown1ca69c42012-10-11 13:37:33 +11005551 nr = working = insync = failed = spare = 0;
5552 rcu_read_lock();
5553 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005555 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 failed++;
5557 else {
5558 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005559 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005560 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 else
5562 spare++;
5563 }
5564 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005565 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 info.major_version = mddev->major_version;
5568 info.minor_version = mddev->minor_version;
5569 info.patch_version = MD_PATCHLEVEL_VERSION;
5570 info.ctime = mddev->ctime;
5571 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005572 info.size = mddev->dev_sectors / 2;
5573 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005574 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 info.nr_disks = nr;
5576 info.raid_disks = mddev->raid_disks;
5577 info.md_minor = mddev->md_minor;
5578 info.not_persistent= !mddev->persistent;
5579
5580 info.utime = mddev->utime;
5581 info.state = 0;
5582 if (mddev->in_sync)
5583 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005584 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005585 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005586 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 info.working_disks = working;
5588 info.failed_disks = failed;
5589 info.spare_disks = spare;
5590
5591 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005592 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 if (copy_to_user(arg, &info, sizeof(info)))
5595 return -EFAULT;
5596
5597 return 0;
5598}
5599
NeilBrownfd01b882011-10-11 16:47:53 +11005600static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005601{
5602 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5603 char *ptr, *buf = NULL;
5604 int err = -ENOMEM;
5605
NeilBrown60559da2013-07-16 16:44:29 +10005606 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005607
NeilBrown32a76272005-06-21 17:17:14 -07005608 if (!file)
5609 goto out;
5610
5611 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005612 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005613 file->pathname[0] = '\0';
5614 goto copy_out;
5615 }
5616
5617 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5618 if (!buf)
5619 goto out;
5620
NeilBrown1ec885c2012-05-22 13:55:10 +10005621 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5622 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005623 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005624 goto out;
5625
5626 strcpy(file->pathname, ptr);
5627
5628copy_out:
5629 err = 0;
5630 if (copy_to_user(arg, file, sizeof(*file)))
5631 err = -EFAULT;
5632out:
5633 kfree(buf);
5634 kfree(file);
5635 return err;
5636}
5637
NeilBrownfd01b882011-10-11 16:47:53 +11005638static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
5640 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005641 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 if (copy_from_user(&info, arg, sizeof(info)))
5644 return -EFAULT;
5645
NeilBrown1ca69c42012-10-11 13:37:33 +11005646 rcu_read_lock();
5647 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (rdev) {
5649 info.major = MAJOR(rdev->bdev->bd_dev);
5650 info.minor = MINOR(rdev->bdev->bd_dev);
5651 info.raid_disk = rdev->raid_disk;
5652 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005653 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005655 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 info.state |= (1<<MD_DISK_ACTIVE);
5657 info.state |= (1<<MD_DISK_SYNC);
5658 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005659 if (test_bit(WriteMostly, &rdev->flags))
5660 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 } else {
5662 info.major = info.minor = 0;
5663 info.raid_disk = -1;
5664 info.state = (1<<MD_DISK_REMOVED);
5665 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005666 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 if (copy_to_user(arg, &info, sizeof(info)))
5669 return -EFAULT;
5670
5671 return 0;
5672}
5673
NeilBrownfd01b882011-10-11 16:47:53 +11005674static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
5676 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005677 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 dev_t dev = MKDEV(info->major,info->minor);
5679
5680 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5681 return -EOVERFLOW;
5682
5683 if (!mddev->raid_disks) {
5684 int err;
5685 /* expecting a device which has a superblock */
5686 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5687 if (IS_ERR(rdev)) {
5688 printk(KERN_WARNING
5689 "md: md_import_device returned %ld\n",
5690 PTR_ERR(rdev));
5691 return PTR_ERR(rdev);
5692 }
5693 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005694 struct md_rdev *rdev0
5695 = list_entry(mddev->disks.next,
5696 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005697 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 .load_super(rdev, rdev0, mddev->minor_version);
5699 if (err < 0) {
5700 printk(KERN_WARNING
5701 "md: %s has different UUID to %s\n",
5702 bdevname(rdev->bdev,b),
5703 bdevname(rdev0->bdev,b2));
5704 export_rdev(rdev);
5705 return -EINVAL;
5706 }
5707 }
5708 err = bind_rdev_to_array(rdev, mddev);
5709 if (err)
5710 export_rdev(rdev);
5711 return err;
5712 }
5713
5714 /*
5715 * add_new_disk can be used once the array is assembled
5716 * to add "hot spares". They must already have a superblock
5717 * written
5718 */
5719 if (mddev->pers) {
5720 int err;
5721 if (!mddev->pers->hot_add_disk) {
5722 printk(KERN_WARNING
5723 "%s: personality does not support diskops!\n",
5724 mdname(mddev));
5725 return -EINVAL;
5726 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005727 if (mddev->persistent)
5728 rdev = md_import_device(dev, mddev->major_version,
5729 mddev->minor_version);
5730 else
5731 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 if (IS_ERR(rdev)) {
5733 printk(KERN_WARNING
5734 "md: md_import_device returned %ld\n",
5735 PTR_ERR(rdev));
5736 return PTR_ERR(rdev);
5737 }
NeilBrown1a855a02010-12-09 16:36:28 +11005738 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005739 if (!mddev->persistent) {
5740 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005741 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005742 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005743 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005744 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005745 } else
NeilBrown41158c72005-06-21 17:17:25 -07005746 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005747 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005748 } else
5749 super_types[mddev->major_version].
5750 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005751 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005752 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005753 /* This was a hot-add request, but events doesn't
5754 * match, so reject it.
5755 */
5756 export_rdev(rdev);
5757 return -EINVAL;
5758 }
5759
NeilBrownb2d444d2005-11-08 21:39:31 -08005760 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005761 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5762 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005763 else
5764 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005765
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 rdev->raid_disk = -1;
5767 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005768 if (!err && !mddev->pers->hot_remove_disk) {
5769 /* If there is hot_add_disk but no hot_remove_disk
5770 * then added disks for geometry changes,
5771 * and should be added immediately.
5772 */
5773 super_types[mddev->major_version].
5774 validate_super(mddev, rdev);
5775 err = mddev->pers->hot_add_disk(mddev, rdev);
5776 if (err)
5777 unbind_rdev_from_array(rdev);
5778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 if (err)
5780 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005781 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005782 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005783
NeilBrown7ceb17e2013-04-24 11:42:42 +10005784 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005785 if (mddev->degraded)
5786 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005787 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005788 if (!err)
5789 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005790 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 return err;
5792 }
5793
5794 /* otherwise, add_new_disk is only allowed
5795 * for major_version==0 superblocks
5796 */
5797 if (mddev->major_version != 0) {
5798 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5799 mdname(mddev));
5800 return -EINVAL;
5801 }
5802
5803 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5804 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005805 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 if (IS_ERR(rdev)) {
5807 printk(KERN_WARNING
5808 "md: error, md_import_device() returned %ld\n",
5809 PTR_ERR(rdev));
5810 return PTR_ERR(rdev);
5811 }
5812 rdev->desc_nr = info->number;
5813 if (info->raid_disk < mddev->raid_disks)
5814 rdev->raid_disk = info->raid_disk;
5815 else
5816 rdev->raid_disk = -1;
5817
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005819 if (info->state & (1<<MD_DISK_SYNC))
5820 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005822 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5823 set_bit(WriteMostly, &rdev->flags);
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 if (!mddev->persistent) {
5826 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005827 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5828 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005829 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005830 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
NeilBrown2bf071b2006-01-06 00:20:55 -08005832 err = bind_rdev_to_array(rdev, mddev);
5833 if (err) {
5834 export_rdev(rdev);
5835 return err;
5836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
5838
5839 return 0;
5840}
5841
NeilBrownfd01b882011-10-11 16:47:53 +11005842static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843{
5844 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005845 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 rdev = find_rdev(mddev, dev);
5848 if (!rdev)
5849 return -ENXIO;
5850
NeilBrown3ea8929d2013-04-24 11:42:41 +10005851 clear_bit(Blocked, &rdev->flags);
5852 remove_and_add_spares(mddev, rdev);
5853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 if (rdev->raid_disk >= 0)
5855 goto busy;
5856
5857 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005858 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005859 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
5861 return 0;
5862busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005863 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 bdevname(rdev->bdev,b), mdname(mddev));
5865 return -EBUSY;
5866}
5867
NeilBrownfd01b882011-10-11 16:47:53 +11005868static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869{
5870 char b[BDEVNAME_SIZE];
5871 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005872 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 if (!mddev->pers)
5875 return -ENODEV;
5876
5877 if (mddev->major_version != 0) {
5878 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5879 " version-0 superblocks.\n",
5880 mdname(mddev));
5881 return -EINVAL;
5882 }
5883 if (!mddev->pers->hot_add_disk) {
5884 printk(KERN_WARNING
5885 "%s: personality does not support diskops!\n",
5886 mdname(mddev));
5887 return -EINVAL;
5888 }
5889
NeilBrownd710e132008-10-13 11:55:12 +11005890 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 if (IS_ERR(rdev)) {
5892 printk(KERN_WARNING
5893 "md: error, md_import_device() returned %ld\n",
5894 PTR_ERR(rdev));
5895 return -EINVAL;
5896 }
5897
5898 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005899 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005901 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
NeilBrown8190e752009-06-18 08:48:58 +10005903 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrownb2d444d2005-11-08 21:39:31 -08005905 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 printk(KERN_WARNING
5907 "md: can not hot-add faulty %s disk to %s!\n",
5908 bdevname(rdev->bdev,b), mdname(mddev));
5909 err = -EINVAL;
5910 goto abort_export;
5911 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005912 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005914 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005915 err = bind_rdev_to_array(rdev, mddev);
5916 if (err)
5917 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 /*
5920 * The rest should better be atomic, we can have disk failures
5921 * noticed in interrupt contexts ...
5922 */
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 rdev->raid_disk = -1;
5925
NeilBrown850b2b422006-10-03 01:15:46 -07005926 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
5928 /*
5929 * Kick recovery, maybe this spare has to be added to the
5930 * array immediately.
5931 */
5932 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5933 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005934 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 return 0;
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937abort_export:
5938 export_rdev(rdev);
5939 return err;
5940}
5941
NeilBrownfd01b882011-10-11 16:47:53 +11005942static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005943{
NeilBrown035328c2014-04-09 12:25:40 +10005944 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005945
NeilBrown36fa3062005-09-09 16:23:45 -07005946 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005947 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005948 return -EBUSY;
5949 if (mddev->recovery || mddev->sync_thread)
5950 return -EBUSY;
5951 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005952 }
5953
NeilBrown36fa3062005-09-09 16:23:45 -07005954
5955 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005956 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005957 if (mddev->bitmap)
5958 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005959 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005960
NeilBrownc3d97142009-12-14 12:49:52 +11005961 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005962 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5963 mdname(mddev));
5964 return -EBADF;
5965 }
5966
NeilBrown035328c2014-04-09 12:25:40 +10005967 inode = mddev->bitmap_info.file->f_mapping->host;
5968 if (!S_ISREG(inode->i_mode)) {
5969 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5970 mdname(mddev));
5971 err = -EBADF;
5972 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5973 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5974 mdname(mddev));
5975 err = -EBADF;
5976 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005977 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5978 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005979 err = -EBUSY;
5980 }
5981 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005982 fput(mddev->bitmap_info.file);
5983 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005984 return err;
5985 }
NeilBrownc3d97142009-12-14 12:49:52 +11005986 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005987 } else if (mddev->bitmap == NULL)
5988 return -ENOENT; /* cannot remove what isn't there */
5989 err = 0;
5990 if (mddev->pers) {
5991 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005992 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005993 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005994 if (!err)
5995 err = bitmap_load(mddev);
5996 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005997 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005998 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005999 fd = -1; /* make sure to put the file */
6000 }
NeilBrown36fa3062005-09-09 16:23:45 -07006001 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006002 }
6003 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006004 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11006005 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11006006 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006007 }
6008
NeilBrown32a76272005-06-21 17:17:14 -07006009 return err;
6010}
6011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012/*
6013 * set_array_info is used two different ways
6014 * The original usage is when creating a new array.
6015 * In this usage, raid_disks is > 0 and it together with
6016 * level, size, not_persistent,layout,chunksize determine the
6017 * shape of the array.
6018 * This will always create an array with a type-0.90.0 superblock.
6019 * The newer usage is when assembling an array.
6020 * In this case raid_disks will be 0, and the major_version field is
6021 * use to determine which style super-blocks are to be found on the devices.
6022 * The minor and patch _version numbers are also kept incase the
6023 * super_block handler wishes to interpret them.
6024 */
NeilBrownfd01b882011-10-11 16:47:53 +11006025static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026{
6027
6028 if (info->raid_disks == 0) {
6029 /* just setting version number for superblock loading */
6030 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006031 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 super_types[info->major_version].name == NULL) {
6033 /* maybe try to auto-load a module? */
6034 printk(KERN_INFO
6035 "md: superblock version %d not known\n",
6036 info->major_version);
6037 return -EINVAL;
6038 }
6039 mddev->major_version = info->major_version;
6040 mddev->minor_version = info->minor_version;
6041 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006042 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006043 /* ensure mddev_put doesn't delete this now that there
6044 * is some minimal configuration.
6045 */
6046 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 return 0;
6048 }
6049 mddev->major_version = MD_MAJOR_VERSION;
6050 mddev->minor_version = MD_MINOR_VERSION;
6051 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6052 mddev->ctime = get_seconds();
6053
6054 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006055 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006056 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 mddev->raid_disks = info->raid_disks;
6058 /* don't set md_minor, it is determined by which /dev/md* was
6059 * openned
6060 */
6061 if (info->state & (1<<MD_SB_CLEAN))
6062 mddev->recovery_cp = MaxSector;
6063 else
6064 mddev->recovery_cp = 0;
6065 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006066 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006069 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 mddev->max_disks = MD_SB_DISKS;
6072
NeilBrowne6910632008-02-06 01:39:51 -08006073 if (mddev->persistent)
6074 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006075 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
NeilBrownc3d97142009-12-14 12:49:52 +11006077 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006078 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006079 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006080
NeilBrownf6705572006-03-27 01:18:11 -08006081 mddev->reshape_position = MaxSector;
6082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 /*
6084 * Generate a 128 bit UUID
6085 */
6086 get_random_bytes(mddev->uuid, 16);
6087
NeilBrownf6705572006-03-27 01:18:11 -08006088 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006089 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006090 mddev->new_layout = mddev->layout;
6091 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006092 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006093
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 return 0;
6095}
6096
NeilBrownfd01b882011-10-11 16:47:53 +11006097void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006098{
Dan Williamsb522adc2009-03-31 15:00:31 +11006099 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6100
6101 if (mddev->external_size)
6102 return;
6103
Dan Williams1f403622009-03-31 14:59:03 +11006104 mddev->array_sectors = array_sectors;
6105}
6106EXPORT_SYMBOL(md_set_array_sectors);
6107
NeilBrownfd01b882011-10-11 16:47:53 +11006108static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006109{
NeilBrown3cb03002011-10-11 16:45:26 +11006110 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006111 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006112 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006113
6114 if (mddev->pers->resize == NULL)
6115 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006116 /* The "num_sectors" is the number of sectors of each device that
6117 * is used. This can only make sense for arrays with redundancy.
6118 * linear and raid0 always use whatever space is available. We can only
6119 * consider changing this number if no resync or reconstruction is
6120 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006121 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006122 * of each device. If num_sectors is zero, we find the largest size
6123 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006124 */
6125 if (mddev->sync_thread)
6126 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006127 if (mddev->ro)
6128 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006129
NeilBrowndafb20f2012-03-19 12:46:39 +11006130 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006131 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006132
Andre Nolld71f9f82008-07-11 22:02:22 +10006133 if (fit && (num_sectors == 0 || num_sectors > avail))
6134 num_sectors = avail;
6135 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006136 return -ENOSPC;
6137 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006138 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006139 if (!rv)
6140 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006141 return rv;
6142}
6143
NeilBrownfd01b882011-10-11 16:47:53 +11006144static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006145{
6146 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006147 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006148 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006149 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006150 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006151 if (mddev->ro)
6152 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006153 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006154 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006155 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006156 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006157 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006158
6159 rdev_for_each(rdev, mddev) {
6160 if (mddev->raid_disks < raid_disks &&
6161 rdev->data_offset < rdev->new_data_offset)
6162 return -EINVAL;
6163 if (mddev->raid_disks > raid_disks &&
6164 rdev->data_offset > rdev->new_data_offset)
6165 return -EINVAL;
6166 }
6167
NeilBrown63c70c42006-03-27 01:18:13 -08006168 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006169 if (mddev->delta_disks < 0)
6170 mddev->reshape_backwards = 1;
6171 else if (mddev->delta_disks > 0)
6172 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006173
6174 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006175 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006176 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006177 mddev->reshape_backwards = 0;
6178 }
NeilBrownda943b992006-01-06 00:20:54 -08006179 return rv;
6180}
6181
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183/*
6184 * update_array_info is used to change the configuration of an
6185 * on-line array.
6186 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6187 * fields in the info are checked against the array.
6188 * Any differences that cannot be handled will cause an error.
6189 * Normally, only one change can be managed at a time.
6190 */
NeilBrownfd01b882011-10-11 16:47:53 +11006191static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192{
6193 int rv = 0;
6194 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006195 int state = 0;
6196
6197 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006198 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006199 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201 if (mddev->major_version != info->major_version ||
6202 mddev->minor_version != info->minor_version ||
6203/* mddev->patch_version != info->patch_version || */
6204 mddev->ctime != info->ctime ||
6205 mddev->level != info->level ||
6206/* mddev->layout != info->layout || */
6207 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006208 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006209 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6210 ((state^info->state) & 0xfffffe00)
6211 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 return -EINVAL;
6213 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006214 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6215 cnt++;
6216 if (mddev->raid_disks != info->raid_disks)
6217 cnt++;
6218 if (mddev->layout != info->layout)
6219 cnt++;
6220 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6221 cnt++;
6222 if (cnt == 0)
6223 return 0;
6224 if (cnt > 1)
6225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
6227 if (mddev->layout != info->layout) {
6228 /* Change layout
6229 * we don't need to do anything at the md level, the
6230 * personality will take care of it all.
6231 */
NeilBrown50ac1682009-06-18 08:47:55 +10006232 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006234 else {
6235 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006236 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006237 if (rv)
6238 mddev->new_layout = mddev->layout;
6239 return rv;
6240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006242 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006243 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
NeilBrownda943b992006-01-06 00:20:54 -08006245 if (mddev->raid_disks != info->raid_disks)
6246 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
NeilBrown36fa3062005-09-09 16:23:45 -07006248 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006249 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006250 return -EINVAL;
6251 if (mddev->recovery || mddev->sync_thread)
6252 return -EBUSY;
6253 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6254 /* add the bitmap */
6255 if (mddev->bitmap)
6256 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006257 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006258 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006259 mddev->bitmap_info.offset =
6260 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006261 mddev->bitmap_info.space =
6262 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006263 mddev->pers->quiesce(mddev, 1);
6264 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006265 if (!rv)
6266 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006267 if (rv)
6268 bitmap_destroy(mddev);
6269 mddev->pers->quiesce(mddev, 0);
6270 } else {
6271 /* remove the bitmap */
6272 if (!mddev->bitmap)
6273 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006274 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006275 return -EINVAL;
6276 mddev->pers->quiesce(mddev, 1);
6277 bitmap_destroy(mddev);
6278 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006279 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006280 }
6281 }
NeilBrown850b2b422006-10-03 01:15:46 -07006282 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 return rv;
6284}
6285
NeilBrownfd01b882011-10-11 16:47:53 +11006286static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287{
NeilBrown3cb03002011-10-11 16:45:26 +11006288 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006289 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
6291 if (mddev->pers == NULL)
6292 return -ENODEV;
6293
NeilBrown1ca69c42012-10-11 13:37:33 +11006294 rcu_read_lock();
6295 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006297 err = -ENODEV;
6298 else {
6299 md_error(mddev, rdev);
6300 if (!test_bit(Faulty, &rdev->flags))
6301 err = -EBUSY;
6302 }
6303 rcu_read_unlock();
6304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305}
6306
Andre Noll2f9618c2008-04-25 18:57:58 +02006307/*
6308 * We have a problem here : there is no easy way to give a CHS
6309 * virtual geometry. We currently pretend that we have a 2 heads
6310 * 4 sectors (with a BIG number of cylinders...). This drives
6311 * dosfs just mad... ;-)
6312 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006313static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6314{
NeilBrownfd01b882011-10-11 16:47:53 +11006315 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006316
6317 geo->heads = 2;
6318 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006319 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006320 return 0;
6321}
6322
Nicolas Schichancb335f82014-01-15 16:58:52 +01006323static inline bool md_ioctl_valid(unsigned int cmd)
6324{
6325 switch (cmd) {
6326 case ADD_NEW_DISK:
6327 case BLKROSET:
6328 case GET_ARRAY_INFO:
6329 case GET_BITMAP_FILE:
6330 case GET_DISK_INFO:
6331 case HOT_ADD_DISK:
6332 case HOT_REMOVE_DISK:
6333 case PRINT_RAID_DEBUG:
6334 case RAID_AUTORUN:
6335 case RAID_VERSION:
6336 case RESTART_ARRAY_RW:
6337 case RUN_ARRAY:
6338 case SET_ARRAY_INFO:
6339 case SET_BITMAP_FILE:
6340 case SET_DISK_FAULTY:
6341 case STOP_ARRAY:
6342 case STOP_ARRAY_RO:
6343 return true;
6344 default:
6345 return false;
6346 }
6347}
6348
Al Viroa39907f2008-03-02 10:31:15 -05006349static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 unsigned int cmd, unsigned long arg)
6351{
6352 int err = 0;
6353 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006354 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006355 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Nicolas Schichancb335f82014-01-15 16:58:52 +01006357 if (!md_ioctl_valid(cmd))
6358 return -ENOTTY;
6359
NeilBrown506c9e42011-12-23 10:17:26 +11006360 switch (cmd) {
6361 case RAID_VERSION:
6362 case GET_ARRAY_INFO:
6363 case GET_DISK_INFO:
6364 break;
6365 default:
6366 if (!capable(CAP_SYS_ADMIN))
6367 return -EACCES;
6368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370 /*
6371 * Commands dealing with the RAID driver but not any
6372 * particular array:
6373 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006374 switch (cmd) {
6375 case RAID_VERSION:
6376 err = get_version(argp);
6377 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
NeilBrownc02c0ae2012-12-11 13:39:21 +11006379 case PRINT_RAID_DEBUG:
6380 err = 0;
6381 md_print_devices();
6382 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006385 case RAID_AUTORUN:
6386 err = 0;
6387 autostart_arrays(arg);
6388 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006390 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 }
6392
6393 /*
6394 * Commands creating/starting a new array:
6395 */
6396
Al Viroa39907f2008-03-02 10:31:15 -05006397 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
6399 if (!mddev) {
6400 BUG();
6401 goto abort;
6402 }
6403
NeilBrown1ca69c42012-10-11 13:37:33 +11006404 /* Some actions do not requires the mutex */
6405 switch (cmd) {
6406 case GET_ARRAY_INFO:
6407 if (!mddev->raid_disks && !mddev->external)
6408 err = -ENODEV;
6409 else
6410 err = get_array_info(mddev, argp);
6411 goto abort;
6412
6413 case GET_DISK_INFO:
6414 if (!mddev->raid_disks && !mddev->external)
6415 err = -ENODEV;
6416 else
6417 err = get_disk_info(mddev, argp);
6418 goto abort;
6419
6420 case SET_DISK_FAULTY:
6421 err = set_disk_faulty(mddev, new_decode_dev(arg));
6422 goto abort;
6423 }
6424
NeilBrowna7a3f082012-12-11 13:35:54 +11006425 if (cmd == ADD_NEW_DISK)
6426 /* need to ensure md_delayed_delete() has completed */
6427 flush_workqueue(md_misc_wq);
6428
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006429 if (cmd == HOT_REMOVE_DISK)
6430 /* need to ensure recovery thread has run */
6431 wait_event_interruptible_timeout(mddev->sb_wait,
6432 !test_bit(MD_RECOVERY_NEEDED,
6433 &mddev->flags),
6434 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006435 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6436 /* Need to flush page cache, and ensure no-one else opens
6437 * and writes
6438 */
6439 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006440 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006441 mutex_unlock(&mddev->open_mutex);
6442 err = -EBUSY;
6443 goto abort;
6444 }
6445 set_bit(MD_STILL_CLOSED, &mddev->flags);
6446 mutex_unlock(&mddev->open_mutex);
6447 sync_blockdev(bdev);
6448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 err = mddev_lock(mddev);
6450 if (err) {
6451 printk(KERN_INFO
6452 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6453 err, cmd);
6454 goto abort;
6455 }
6456
NeilBrownc02c0ae2012-12-11 13:39:21 +11006457 if (cmd == SET_ARRAY_INFO) {
6458 mdu_array_info_t info;
6459 if (!arg)
6460 memset(&info, 0, sizeof(info));
6461 else if (copy_from_user(&info, argp, sizeof(info))) {
6462 err = -EFAULT;
6463 goto abort_unlock;
6464 }
6465 if (mddev->pers) {
6466 err = update_array_info(mddev, &info);
6467 if (err) {
6468 printk(KERN_WARNING "md: couldn't update"
6469 " array info. %d\n", err);
6470 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 }
6472 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006473 }
6474 if (!list_empty(&mddev->disks)) {
6475 printk(KERN_WARNING
6476 "md: array %s already has disks!\n",
6477 mdname(mddev));
6478 err = -EBUSY;
6479 goto abort_unlock;
6480 }
6481 if (mddev->raid_disks) {
6482 printk(KERN_WARNING
6483 "md: array %s already initialised!\n",
6484 mdname(mddev));
6485 err = -EBUSY;
6486 goto abort_unlock;
6487 }
6488 err = set_array_info(mddev, &info);
6489 if (err) {
6490 printk(KERN_WARNING "md: couldn't set"
6491 " array info. %d\n", err);
6492 goto abort_unlock;
6493 }
6494 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
6497 /*
6498 * Commands querying/configuring an existing array:
6499 */
NeilBrown32a76272005-06-21 17:17:14 -07006500 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006501 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006502 if ((!mddev->raid_disks && !mddev->external)
6503 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6504 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6505 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 err = -ENODEV;
6507 goto abort_unlock;
6508 }
6509
6510 /*
6511 * Commands even a read-only array can execute:
6512 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006513 switch (cmd) {
6514 case GET_BITMAP_FILE:
6515 err = get_bitmap_file(mddev, argp);
6516 goto done_unlock;
6517
6518 case RESTART_ARRAY_RW:
6519 err = restart_array(mddev);
6520 goto done_unlock;
6521
6522 case STOP_ARRAY:
6523 err = do_md_stop(mddev, 0, bdev);
6524 goto done_unlock;
6525
6526 case STOP_ARRAY_RO:
6527 err = md_set_readonly(mddev, bdev);
6528 goto done_unlock;
6529
NeilBrown3ea8929d2013-04-24 11:42:41 +10006530 case HOT_REMOVE_DISK:
6531 err = hot_remove_disk(mddev, new_decode_dev(arg));
6532 goto done_unlock;
6533
NeilBrown7ceb17e2013-04-24 11:42:42 +10006534 case ADD_NEW_DISK:
6535 /* We can support ADD_NEW_DISK on read-only arrays
6536 * on if we are re-adding a preexisting device.
6537 * So require mddev->pers and MD_DISK_SYNC.
6538 */
6539 if (mddev->pers) {
6540 mdu_disk_info_t info;
6541 if (copy_from_user(&info, argp, sizeof(info)))
6542 err = -EFAULT;
6543 else if (!(info.state & (1<<MD_DISK_SYNC)))
6544 /* Need to clear read-only for this */
6545 break;
6546 else
6547 err = add_new_disk(mddev, &info);
6548 goto done_unlock;
6549 }
6550 break;
6551
NeilBrownc02c0ae2012-12-11 13:39:21 +11006552 case BLKROSET:
6553 if (get_user(ro, (int __user *)(arg))) {
6554 err = -EFAULT;
6555 goto done_unlock;
6556 }
6557 err = -EINVAL;
6558
6559 /* if the bdev is going readonly the value of mddev->ro
6560 * does not matter, no writes are coming
6561 */
6562 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006563 goto done_unlock;
6564
NeilBrownc02c0ae2012-12-11 13:39:21 +11006565 /* are we are already prepared for writes? */
6566 if (mddev->ro != 1)
6567 goto done_unlock;
6568
6569 /* transitioning to readauto need only happen for
6570 * arrays that call md_write_start
6571 */
6572 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006574 if (err == 0) {
6575 mddev->ro = 2;
6576 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006577 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006578 }
6579 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 }
6581
6582 /*
6583 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006584 * superblock, so we do not allow them on read-only arrays.
6585 * However non-MD ioctls (e.g. get-size) will still come through
6586 * here and hit the 'default' below, so only disallow
6587 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006589 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006590 if (mddev->ro == 2) {
6591 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006592 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006593 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006594 /* mddev_unlock will wake thread */
6595 /* If a device failed while we were read-only, we
6596 * need to make sure the metadata is updated now.
6597 */
6598 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6599 mddev_unlock(mddev);
6600 wait_event(mddev->sb_wait,
6601 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6602 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006603 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006604 }
NeilBrownf91de922005-11-08 21:39:36 -08006605 } else {
6606 err = -EROFS;
6607 goto abort_unlock;
6608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 }
6610
NeilBrownc02c0ae2012-12-11 13:39:21 +11006611 switch (cmd) {
6612 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006614 mdu_disk_info_t info;
6615 if (copy_from_user(&info, argp, sizeof(info)))
6616 err = -EFAULT;
6617 else
6618 err = add_new_disk(mddev, &info);
6619 goto done_unlock;
6620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
NeilBrownc02c0ae2012-12-11 13:39:21 +11006622 case HOT_ADD_DISK:
6623 err = hot_add_disk(mddev, new_decode_dev(arg));
6624 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
NeilBrownc02c0ae2012-12-11 13:39:21 +11006626 case RUN_ARRAY:
6627 err = do_md_run(mddev);
6628 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
NeilBrownc02c0ae2012-12-11 13:39:21 +11006630 case SET_BITMAP_FILE:
6631 err = set_bitmap_file(mddev, (int)arg);
6632 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006633
NeilBrownc02c0ae2012-12-11 13:39:21 +11006634 default:
6635 err = -EINVAL;
6636 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 }
6638
6639done_unlock:
6640abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006641 if (mddev->hold_active == UNTIL_IOCTL &&
6642 err != -EINVAL)
6643 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 mddev_unlock(mddev);
6645
6646 return err;
6647done:
6648 if (err)
6649 MD_BUG();
6650abort:
6651 return err;
6652}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006653#ifdef CONFIG_COMPAT
6654static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6655 unsigned int cmd, unsigned long arg)
6656{
6657 switch (cmd) {
6658 case HOT_REMOVE_DISK:
6659 case HOT_ADD_DISK:
6660 case SET_DISK_FAULTY:
6661 case SET_BITMAP_FILE:
6662 /* These take in integer arg, do not convert */
6663 break;
6664 default:
6665 arg = (unsigned long)compat_ptr(arg);
6666 break;
6667 }
6668
6669 return md_ioctl(bdev, mode, cmd, arg);
6670}
6671#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Al Viroa39907f2008-03-02 10:31:15 -05006673static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674{
6675 /*
6676 * Succeed if we can lock the mddev, which confirms that
6677 * it isn't being stopped right now.
6678 */
NeilBrownfd01b882011-10-11 16:47:53 +11006679 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 int err;
6681
Yuanhan Liu0c098222012-05-22 13:55:32 +10006682 if (!mddev)
6683 return -ENODEV;
6684
NeilBrownd3374822009-01-09 08:31:10 +11006685 if (mddev->gendisk != bdev->bd_disk) {
6686 /* we are racing with mddev_put which is discarding this
6687 * bd_disk.
6688 */
6689 mddev_put(mddev);
6690 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006691 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006692 /* Then retry the open from the top */
6693 return -ERESTARTSYS;
6694 }
6695 BUG_ON(mddev != bdev->bd_disk->private_data);
6696
NeilBrownc8c00a62009-08-10 12:50:52 +10006697 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 goto out;
6699
6700 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006701 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006702 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006703 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006705 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 out:
6707 return err;
6708}
6709
Al Virodb2a1442013-05-05 21:52:57 -04006710static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711{
NeilBrownfd01b882011-10-11 16:47:53 +11006712 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006714 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006715 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006718
6719static int md_media_changed(struct gendisk *disk)
6720{
NeilBrownfd01b882011-10-11 16:47:53 +11006721 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006722
6723 return mddev->changed;
6724}
6725
6726static int md_revalidate(struct gendisk *disk)
6727{
NeilBrownfd01b882011-10-11 16:47:53 +11006728 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006729
6730 mddev->changed = 0;
6731 return 0;
6732}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006733static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734{
6735 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006736 .open = md_open,
6737 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006738 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006739#ifdef CONFIG_COMPAT
6740 .compat_ioctl = md_compat_ioctl,
6741#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006742 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006743 .media_changed = md_media_changed,
6744 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745};
6746
Adrian Bunk75c96f82005-05-05 16:16:09 -07006747static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
NeilBrown2b8bf342011-10-11 16:48:23 +11006749 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 /*
6752 * md_thread is a 'system-thread', it's priority should be very
6753 * high. We avoid resource deadlocks individually in each
6754 * raid personality. (RAID5 does preallocation) We also use RR and
6755 * the very same RT priority as kswapd, thus we will never get
6756 * into a priority inversion deadlock.
6757 *
6758 * we definitely have to have equal or higher priority than
6759 * bdflush, otherwise bdflush will deadlock if there are too
6760 * many dirty RAID5 blocks.
6761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
NeilBrown6985c432005-10-19 21:23:47 -07006763 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006764 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
NeilBrown93588e22005-11-15 00:09:12 -08006766 /* We need to wait INTERRUPTIBLE so that
6767 * we don't add to the load-average.
6768 * That means we need to be sure no signals are
6769 * pending
6770 */
6771 if (signal_pending(current))
6772 flush_signals(current);
6773
6774 wait_event_interruptible_timeout
6775 (thread->wqueue,
6776 test_bit(THREAD_WAKEUP, &thread->flags)
6777 || kthread_should_stop(),
6778 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
NeilBrown6c987912011-01-14 09:13:53 +11006780 clear_bit(THREAD_WAKEUP, &thread->flags);
6781 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006782 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006784
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 return 0;
6786}
6787
NeilBrown2b8bf342011-10-11 16:48:23 +11006788void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789{
6790 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006791 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 set_bit(THREAD_WAKEUP, &thread->flags);
6793 wake_up(&thread->wqueue);
6794 }
6795}
6796
Shaohua Li4ed87312012-10-11 13:34:00 +11006797struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6798 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799{
NeilBrown2b8bf342011-10-11 16:48:23 +11006800 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
NeilBrown2b8bf342011-10-11 16:48:23 +11006802 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 if (!thread)
6804 return NULL;
6805
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 init_waitqueue_head(&thread->wqueue);
6807
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 thread->run = run;
6809 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006810 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006811 thread->tsk = kthread_run(md_thread, thread,
6812 "%s_%s",
6813 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006814 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006815 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 kfree(thread);
6817 return NULL;
6818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return thread;
6820}
6821
NeilBrown2b8bf342011-10-11 16:48:23 +11006822void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823{
NeilBrown2b8bf342011-10-11 16:48:23 +11006824 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006825 if (!thread)
6826 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006827 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006828 /* Locking ensures that mddev_unlock does not wake_up a
6829 * non-existent thread
6830 */
6831 spin_lock(&pers_lock);
6832 *threadp = NULL;
6833 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006834
6835 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 kfree(thread);
6837}
6838
NeilBrownfd01b882011-10-11 16:47:53 +11006839void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840{
6841 if (!mddev) {
6842 MD_BUG();
6843 return;
6844 }
6845
NeilBrownb2d444d2005-11-08 21:39:31 -08006846 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006848
NeilBrownde393cd2011-07-28 11:31:48 +10006849 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 return;
6851 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006852 if (mddev->degraded)
6853 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006854 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6856 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6857 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006858 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006859 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006860 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861}
6862
6863/* seq_file implementation /proc/mdstat */
6864
6865static void status_unused(struct seq_file *seq)
6866{
6867 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006868 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
6870 seq_printf(seq, "unused devices: ");
6871
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006872 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 char b[BDEVNAME_SIZE];
6874 i++;
6875 seq_printf(seq, "%s ",
6876 bdevname(rdev->bdev,b));
6877 }
6878 if (!i)
6879 seq_printf(seq, "<none>");
6880
6881 seq_printf(seq, "\n");
6882}
6883
6884
NeilBrownfd01b882011-10-11 16:47:53 +11006885static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886{
NeilBrowndd71cf62009-05-07 12:49:35 +10006887 sector_t max_sectors, resync, res;
6888 unsigned long dt, db;
6889 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006890 int scale;
6891 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
NeilBrown72f36d52012-10-11 14:25:57 +11006893 if (mddev->curr_resync <= 3)
6894 resync = 0;
6895 else
6896 resync = mddev->curr_resync
6897 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
NeilBrownc804cde2012-05-21 09:28:33 +10006899 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6900 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006901 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006903 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904
6905 /*
6906 * Should not happen.
6907 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006908 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 MD_BUG();
6910 return;
6911 }
NeilBrown4588b422006-03-27 01:18:04 -08006912 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006913 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006914 * u32, as those are the requirements for sector_div.
6915 * Thus 'scale' must be at least 10
6916 */
6917 scale = 10;
6918 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006919 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006920 scale++;
6921 }
6922 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006923 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006924
6925 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 {
NeilBrown4588b422006-03-27 01:18:04 -08006927 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 seq_printf(seq, "[");
6929 for (i = 0; i < x; i++)
6930 seq_printf(seq, "=");
6931 seq_printf(seq, ">");
6932 for (i = 0; i < y; i++)
6933 seq_printf(seq, ".");
6934 seq_printf(seq, "] ");
6935 }
NeilBrown4588b422006-03-27 01:18:04 -08006936 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006937 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6938 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006939 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6940 "check" :
6941 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6942 "resync" : "recovery"))),
6943 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006944 (unsigned long long) resync/2,
6945 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
6947 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 * dt: time from mark until now
6949 * db: blocks written from mark until now
6950 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006951 *
6952 * rt is a sector_t, so could be 32bit or 64bit.
6953 * So we divide before multiply in case it is 32bit and close
6954 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006955 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006956 * near the end of resync when the number of remaining sectors
6957 * is close to 'db'.
6958 * We then divide rt by 32 after multiplying by db to compensate.
6959 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 */
6961 dt = ((jiffies - mddev->resync_mark) / HZ);
6962 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006963 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6964 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
NeilBrowndd71cf62009-05-07 12:49:35 +10006966 rt = max_sectors - resync; /* number of remaining sectors */
6967 sector_div(rt, db/32+1);
6968 rt *= dt;
6969 rt >>= 5;
6970
6971 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6972 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
NeilBrownff4e8d92006-07-10 04:44:16 -07006974 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975}
6976
6977static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6978{
6979 struct list_head *tmp;
6980 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006981 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
6983 if (l >= 0x10000)
6984 return NULL;
6985 if (!l--)
6986 /* header */
6987 return (void*)1;
6988
6989 spin_lock(&all_mddevs_lock);
6990 list_for_each(tmp,&all_mddevs)
6991 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006992 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 mddev_get(mddev);
6994 spin_unlock(&all_mddevs_lock);
6995 return mddev;
6996 }
6997 spin_unlock(&all_mddevs_lock);
6998 if (!l--)
6999 return (void*)2;/* tail */
7000 return NULL;
7001}
7002
7003static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7004{
7005 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007006 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
7008 ++*pos;
7009 if (v == (void*)2)
7010 return NULL;
7011
7012 spin_lock(&all_mddevs_lock);
7013 if (v == (void*)1)
7014 tmp = all_mddevs.next;
7015 else
7016 tmp = mddev->all_mddevs.next;
7017 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007018 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 else {
7020 next_mddev = (void*)2;
7021 *pos = 0x10000;
7022 }
7023 spin_unlock(&all_mddevs_lock);
7024
7025 if (v != (void*)1)
7026 mddev_put(mddev);
7027 return next_mddev;
7028
7029}
7030
7031static void md_seq_stop(struct seq_file *seq, void *v)
7032{
NeilBrownfd01b882011-10-11 16:47:53 +11007033 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035 if (mddev && v != (void*)1 && v != (void*)2)
7036 mddev_put(mddev);
7037}
7038
7039static int md_seq_show(struct seq_file *seq, void *v)
7040{
NeilBrownfd01b882011-10-11 16:47:53 +11007041 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007042 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007043 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007046 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 seq_printf(seq, "Personalities : ");
7048 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007049 list_for_each_entry(pers, &pers_list, list)
7050 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
7052 spin_unlock(&pers_lock);
7053 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007054 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 return 0;
7056 }
7057 if (v == (void*)2) {
7058 status_unused(seq);
7059 return 0;
7060 }
7061
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007062 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007064
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7066 seq_printf(seq, "%s : %sactive", mdname(mddev),
7067 mddev->pers ? "" : "in");
7068 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007069 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007071 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007072 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 seq_printf(seq, " %s", mddev->pers->name);
7074 }
7075
Andre Nolldd8ac332009-03-31 14:33:13 +11007076 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007077 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 char b[BDEVNAME_SIZE];
7079 seq_printf(seq, " %s[%d]",
7080 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007081 if (test_bit(WriteMostly, &rdev->flags))
7082 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007083 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 seq_printf(seq, "(F)");
7085 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007086 }
7087 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007088 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007089 if (test_bit(Replacement, &rdev->flags))
7090 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007091 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 }
7093
7094 if (!list_empty(&mddev->disks)) {
7095 if (mddev->pers)
7096 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007097 (unsigned long long)
7098 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 else
7100 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007101 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007103 if (mddev->persistent) {
7104 if (mddev->major_version != 0 ||
7105 mddev->minor_version != 90) {
7106 seq_printf(seq," super %d.%d",
7107 mddev->major_version,
7108 mddev->minor_version);
7109 }
NeilBrowne6910632008-02-06 01:39:51 -08007110 } else if (mddev->external)
7111 seq_printf(seq, " super external:%s",
7112 mddev->metadata_type);
7113 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007114 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
7116 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007117 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007119 if (mddev->pers->sync_request) {
7120 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007121 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007122 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007123 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007124 seq_printf(seq, "\tresync=DELAYED\n ");
7125 else if (mddev->recovery_cp < MaxSector)
7126 seq_printf(seq, "\tresync=PENDING\n ");
7127 }
NeilBrown32a76272005-06-21 17:17:14 -07007128 } else
7129 seq_printf(seq, "\n ");
7130
NeilBrown57148962012-03-19 12:46:40 +11007131 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 seq_printf(seq, "\n");
7134 }
7135 mddev_unlock(mddev);
7136
7137 return 0;
7138}
7139
Jan Engelhardt110518b2009-05-07 12:49:37 +10007140static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 .start = md_seq_start,
7142 .next = md_seq_next,
7143 .stop = md_seq_stop,
7144 .show = md_seq_show,
7145};
7146
7147static int md_seq_open(struct inode *inode, struct file *file)
7148{
Kay Sieversf1514632011-07-12 20:48:39 +02007149 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 int error;
7151
7152 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007153 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007154 return error;
7155
7156 seq = file->private_data;
7157 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 return error;
7159}
7160
NeilBrowne2f23b62014-04-09 14:33:51 +10007161static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007162static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7163{
Kay Sieversf1514632011-07-12 20:48:39 +02007164 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007165 int mask;
7166
NeilBrowne2f23b62014-04-09 14:33:51 +10007167 if (md_unloading)
7168 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;;
NeilBrownd7603b72006-01-06 00:20:30 -08007169 poll_wait(filp, &md_event_waiters, wait);
7170
7171 /* always allow read */
7172 mask = POLLIN | POLLRDNORM;
7173
Kay Sieversf1514632011-07-12 20:48:39 +02007174 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007175 mask |= POLLERR | POLLPRI;
7176 return mask;
7177}
7178
Arjan van de Venfa027c22007-02-12 00:55:33 -08007179static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007180 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 .open = md_seq_open,
7182 .read = seq_read,
7183 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007184 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007185 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186};
7187
NeilBrown84fc4b52011-10-11 16:49:58 +11007188int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007191 list_add_tail(&p->list, &pers_list);
7192 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 spin_unlock(&pers_lock);
7194 return 0;
7195}
7196
NeilBrown84fc4b52011-10-11 16:49:58 +11007197int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198{
NeilBrown2604b702006-01-06 00:20:36 -08007199 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007201 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 spin_unlock(&pers_lock);
7203 return 0;
7204}
7205
NeilBrownfd01b882011-10-11 16:47:53 +11007206static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207{
NeilBrown3cb03002011-10-11 16:45:26 +11007208 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007210 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007213 rcu_read_lock();
7214 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007216 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7217 (int)part_stat_read(&disk->part0, sectors[1]) -
7218 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007219 /* sync IO will cause sync_io to increase before the disk_stats
7220 * as sync_io is counted when a request starts, and
7221 * disk_stats is counted when it completes.
7222 * So resync activity will cause curr_events to be smaller than
7223 * when there was no such activity.
7224 * non-sync IO will cause disk_stat to increase without
7225 * increasing sync_io so curr_events will (eventually)
7226 * be larger than it was before. Once it becomes
7227 * substantially larger, the test below will cause
7228 * the array to appear non-idle, and resync will slow
7229 * down.
7230 * If there is a lot of outstanding resync activity when
7231 * we set last_event to curr_events, then all that activity
7232 * completing might cause the array to appear non-idle
7233 * and resync will be slowed down even though there might
7234 * not have been non-resync activity. This will only
7235 * happen once though. 'last_events' will soon reflect
7236 * the state where there is little or no outstanding
7237 * resync requests, and further resync activity will
7238 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007239 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007241 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 rdev->last_events = curr_events;
7243 idle = 0;
7244 }
7245 }
NeilBrown4b809912008-07-21 17:05:25 +10007246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 return idle;
7248}
7249
NeilBrownfd01b882011-10-11 16:47:53 +11007250void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251{
7252 /* another "blocks" (512byte) blocks have been synced */
7253 atomic_sub(blocks, &mddev->recovery_active);
7254 wake_up(&mddev->recovery_wait);
7255 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007256 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007257 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 md_wakeup_thread(mddev->thread);
7259 // stop recovery, signal do_sync ....
7260 }
7261}
7262
7263
NeilBrown06d91a52005-06-21 17:17:12 -07007264/* md_write_start(mddev, bi)
7265 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007266 * in superblock) before writing, schedule a superblock update
7267 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007268 */
NeilBrownfd01b882011-10-11 16:47:53 +11007269void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270{
Neil Brown0fd62b82008-06-28 08:31:36 +10007271 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007272 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007273 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007274
NeilBrownf91de922005-11-08 21:39:36 -08007275 BUG_ON(mddev->ro == 1);
7276 if (mddev->ro == 2) {
7277 /* need to switch to read/write */
7278 mddev->ro = 0;
7279 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7280 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007281 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007282 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007283 }
NeilBrown06d91a52005-06-21 17:17:12 -07007284 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007285 if (mddev->safemode == 1)
7286 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007287 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007288 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007289 if (mddev->in_sync) {
7290 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007291 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007292 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007293 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007294 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007295 }
NeilBrowna9701a32005-11-08 21:39:34 -08007296 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007297 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007298 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007299 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007300 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007301 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302}
7303
NeilBrownfd01b882011-10-11 16:47:53 +11007304void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305{
7306 if (atomic_dec_and_test(&mddev->writes_pending)) {
7307 if (mddev->safemode == 2)
7308 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007309 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7311 }
7312}
7313
NeilBrown2a2275d2007-01-26 00:57:11 -08007314/* md_allow_write(mddev)
7315 * Calling this ensures that the array is marked 'active' so that writes
7316 * may proceed without blocking. It is important to call this before
7317 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7318 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007319 *
7320 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7321 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007322 */
NeilBrownfd01b882011-10-11 16:47:53 +11007323int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007324{
7325 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007326 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007327 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007328 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007329 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007330 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007331
7332 spin_lock_irq(&mddev->write_lock);
7333 if (mddev->in_sync) {
7334 mddev->in_sync = 0;
7335 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007336 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007337 if (mddev->safemode_delay &&
7338 mddev->safemode == 0)
7339 mddev->safemode = 1;
7340 spin_unlock_irq(&mddev->write_lock);
7341 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007342 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007343 } else
7344 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007345
NeilBrown070dc6d2010-08-30 17:33:34 +10007346 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007347 return -EAGAIN;
7348 else
7349 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007350}
7351EXPORT_SYMBOL_GPL(md_allow_write);
7352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353#define SYNC_MARKS 10
7354#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007355#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007356void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357{
Shaohua Li4ed87312012-10-11 13:34:00 +11007358 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007359 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 unsigned int currspeed = 0,
7361 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007362 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007364 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 sector_t mark_cnt[SYNC_MARKS];
7366 int last_mark,m;
7367 struct list_head *tmp;
7368 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007369 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007370 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007371 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007372 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
7374 /* just incase thread restarts... */
7375 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7376 return;
NeilBrown3991b312014-05-28 13:39:23 +10007377 if (mddev->ro) {/* never try to sync a read-only array */
7378 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007379 return;
NeilBrown3991b312014-05-28 13:39:23 +10007380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrown61df9d92006-10-03 01:15:57 -07007382 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007383 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007384 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007385 action = "check";
7386 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007387 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007388 action = "repair";
7389 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007390 desc = "resync";
7391 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7392 desc = "reshape";
7393 else
7394 desc = "recovery";
7395
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007396 mddev->last_sync_action = action ?: desc;
7397
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 /* we overload curr_resync somewhat here.
7399 * 0 == not engaged in resync at all
7400 * 2 == checking that there is no conflict with another sync
7401 * 1 == like 2, but have yielded to allow conflicting resync to
7402 * commense
7403 * other == active in resync - this many blocks
7404 *
7405 * Before starting a resync we must have set curr_resync to
7406 * 2, and then checked that every "conflicting" array has curr_resync
7407 * less than ours. When we find one that is the same or higher
7408 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7409 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7410 * This will mean we have to start checking from the beginning again.
7411 *
7412 */
7413
7414 do {
7415 mddev->curr_resync = 2;
7416
7417 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007418 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007420 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 if (mddev2 == mddev)
7422 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007423 if (!mddev->parallel_resync
7424 && mddev2->curr_resync
7425 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 DEFINE_WAIT(wq);
7427 if (mddev < mddev2 && mddev->curr_resync == 2) {
7428 /* arbitrarily yield */
7429 mddev->curr_resync = 1;
7430 wake_up(&resync_wait);
7431 }
7432 if (mddev > mddev2 && mddev->curr_resync == 1)
7433 /* no need to wait here, we can wait the next
7434 * time 'round when curr_resync == 2
7435 */
7436 continue;
NeilBrown97441972008-09-19 11:49:54 +10007437 /* We need to wait 'interruptible' so as not to
7438 * contribute to the load average, and not to
7439 * be caught by 'softlockup'
7440 */
7441 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007442 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007443 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007444 printk(KERN_INFO "md: delaying %s of %s"
7445 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007447 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007449 if (signal_pending(current))
7450 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 schedule();
7452 finish_wait(&resync_wait, &wq);
7453 goto try_again;
7454 }
7455 finish_wait(&resync_wait, &wq);
7456 }
7457 }
7458 } while (mddev->curr_resync < 2);
7459
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007460 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007461 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007463 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 */
7465 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007466 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007467 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007468 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7469 j = mddev->resync_min;
7470 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007471 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007472
NeilBrownccfcc3c2006-03-27 01:18:09 -08007473 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007474 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007475 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007477 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007478 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007479 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007480 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007481 if (rdev->raid_disk >= 0 &&
7482 !test_bit(Faulty, &rdev->flags) &&
7483 !test_bit(In_sync, &rdev->flags) &&
7484 rdev->recovery_offset < j)
7485 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007486 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007487
7488 /* If there is a bitmap, we need to make sure all
7489 * writes that started before we added a spare
7490 * complete before we start doing a recovery.
7491 * Otherwise the write might complete and (via
7492 * bitmap_endwrite) set a bit in the bitmap after the
7493 * recovery has checked that bit and skipped that
7494 * region.
7495 */
7496 if (mddev->bitmap) {
7497 mddev->pers->quiesce(mddev, 1);
7498 mddev->pers->quiesce(mddev, 0);
7499 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501
NeilBrown61df9d92006-10-03 01:15:57 -07007502 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7503 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7504 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007505 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007506 "(but not more than %d KB/sec) for %s.\n",
7507 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508
NeilBrowneea1bf32009-03-31 14:27:02 +11007509 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007510
NeilBrown57afd892005-06-21 17:17:13 -07007511 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 for (m = 0; m < SYNC_MARKS; m++) {
7513 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007514 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 }
7516 last_mark = 0;
7517 mddev->resync_mark = mark[last_mark];
7518 mddev->resync_mark_cnt = mark_cnt[last_mark];
7519
7520 /*
7521 * Tune reconstruction:
7522 */
7523 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007524 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7525 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
7527 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 last_check = 0;
7529
7530 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007531 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007532 "md: resuming %s of %s from checkpoint.\n",
7533 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007535 } else
7536 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007537 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007538 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7539 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007540 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
majianpeng7c2c57c2012-07-03 12:12:26 +10007542 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007544 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
NeilBrown57afd892005-06-21 17:17:13 -07007546 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007547
NeilBrown7a91ee12009-05-26 12:57:21 +10007548 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7549 ((mddev->curr_resync > mddev->curr_resync_completed &&
7550 (mddev->curr_resync - mddev->curr_resync_completed)
7551 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007552 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007553 (j - mddev->curr_resync_completed)*2
7554 >= mddev->resync_max - mddev->curr_resync_completed
7555 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007556 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007557 wait_event(mddev->recovery_wait,
7558 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007559 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007560 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7561 j > mddev->recovery_cp)
7562 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007563 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007564 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007565 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007566 }
NeilBrownacb180b2009-04-14 16:28:34 +10007567
NeilBrownc91abf52013-11-19 12:02:01 +11007568 while (j >= mddev->resync_max &&
7569 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007570 /* As this condition is controlled by user-space,
7571 * we can block indefinitely, so use '_interruptible'
7572 * to avoid triggering warnings.
7573 */
7574 flush_signals(current); /* just in case */
7575 wait_event_interruptible(mddev->recovery_wait,
7576 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007577 || test_bit(MD_RECOVERY_INTR,
7578 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007579 }
NeilBrownacb180b2009-04-14 16:28:34 +10007580
NeilBrownc91abf52013-11-19 12:02:01 +11007581 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7582 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007583
NeilBrown57afd892005-06-21 17:17:13 -07007584 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007585 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007586 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007587 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 }
NeilBrown57afd892005-06-21 17:17:13 -07007590
7591 if (!skipped) { /* actual IO requested */
7592 io_sectors += sectors;
7593 atomic_add(sectors, &mddev->recovery_active);
7594 }
7595
NeilBrowne875ece2011-07-28 11:39:24 +10007596 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7597 break;
7598
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007600 if (j > 2)
7601 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007602 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007603 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007604 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007605 * visible in /proc/mdstat
7606 */
7607 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007608
7609 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 continue;
7611
NeilBrown57afd892005-06-21 17:17:13 -07007612 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 repeat:
7614 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7615 /* step marks */
7616 int next = (last_mark+1) % SYNC_MARKS;
7617
7618 mddev->resync_mark = mark[next];
7619 mddev->resync_mark_cnt = mark_cnt[next];
7620 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007621 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 last_mark = next;
7623 }
7624
NeilBrownc91abf52013-11-19 12:02:01 +11007625 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627
7628 /*
7629 * this loop exits only if either when we are slower than
7630 * the 'hard' speed limit, or the system was IO-idle for
7631 * a jiffy.
7632 * the system might be non-idle CPU-wise, but we only care
7633 * about not overloading the IO subsystem. (things like an
7634 * e2fsck being done on the RAID array should execute fast)
7635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636 cond_resched();
7637
Xiao Niac7e50a2014-08-07 09:37:41 -04007638 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7639 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007640 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641
NeilBrown88202a02006-01-06 00:21:36 -08007642 if (currspeed > speed_min(mddev)) {
7643 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007644 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007645 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 goto repeat;
7647 }
7648 }
7649 }
NeilBrownc91abf52013-11-19 12:02:01 +11007650 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7651 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7652 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 /*
7654 * this also signals 'finished resyncing' to md_stop
7655 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007656 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7658
7659 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007660 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
NeilBrowndfc70642008-05-23 13:04:39 -07007662 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007663 mddev->curr_resync > 2) {
7664 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7665 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7666 if (mddev->curr_resync >= mddev->recovery_cp) {
7667 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007668 "md: checkpointing %s of %s.\n",
7669 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007670 if (test_bit(MD_RECOVERY_ERROR,
7671 &mddev->recovery))
7672 mddev->recovery_cp =
7673 mddev->curr_resync_completed;
7674 else
7675 mddev->recovery_cp =
7676 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007677 }
7678 } else
7679 mddev->recovery_cp = MaxSector;
7680 } else {
7681 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7682 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007683 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007684 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007685 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007686 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007687 !test_bit(Faulty, &rdev->flags) &&
7688 !test_bit(In_sync, &rdev->flags) &&
7689 rdev->recovery_offset < mddev->curr_resync)
7690 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007691 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007694 skip:
NeilBrown17571282006-12-10 02:20:52 -08007695 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696
NeilBrownc07b70a2009-12-14 12:49:48 +11007697 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7698 /* We completed so min/max setting can be forgotten if used. */
7699 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7700 mddev->resync_min = 0;
7701 mddev->resync_max = MaxSector;
7702 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7703 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 mddev->curr_resync = 0;
7705 wake_up(&resync_wait);
7706 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7707 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709}
NeilBrown29269552006-03-27 01:18:10 -08007710EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
NeilBrown746d3202013-04-24 11:42:41 +10007712static int remove_and_add_spares(struct mddev *mddev,
7713 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007714{
NeilBrown3cb03002011-10-11 16:45:26 +11007715 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007716 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007717 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007718
NeilBrowndafb20f2012-03-19 12:46:39 +11007719 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007720 if ((this == NULL || rdev == this) &&
7721 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007722 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007723 (test_bit(Faulty, &rdev->flags) ||
7724 ! test_bit(In_sync, &rdev->flags)) &&
7725 atomic_read(&rdev->nr_pending)==0) {
7726 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007727 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007728 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007729 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007730 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007731 }
7732 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007733 if (removed && mddev->kobj.sd)
7734 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007735
NeilBrown746d3202013-04-24 11:42:41 +10007736 if (this)
7737 goto no_add;
7738
NeilBrowndafb20f2012-03-19 12:46:39 +11007739 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007740 if (rdev->raid_disk >= 0 &&
7741 !test_bit(In_sync, &rdev->flags) &&
7742 !test_bit(Faulty, &rdev->flags))
7743 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007744 if (rdev->raid_disk >= 0)
7745 continue;
7746 if (test_bit(Faulty, &rdev->flags))
7747 continue;
7748 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007749 ! (rdev->saved_raid_disk >= 0 &&
7750 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007751 continue;
7752
NeilBrown7eb41882014-01-14 15:55:14 +11007753 if (rdev->saved_raid_disk < 0)
7754 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007755 if (mddev->pers->
7756 hot_add_disk(mddev, rdev) == 0) {
7757 if (sysfs_link_rdev(mddev, rdev))
7758 /* failure here is OK */;
7759 spares++;
7760 md_new_event(mddev);
7761 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007762 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007763 }
NeilBrown746d3202013-04-24 11:42:41 +10007764no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007765 if (removed)
7766 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007767 return spares;
7768}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007769
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770/*
7771 * This routine is regularly called by all per-raid-array threads to
7772 * deal with generic issues like resync and super-block update.
7773 * Raid personalities that don't have a thread (linear/raid0) do not
7774 * need this as they never do any recovery or update the superblock.
7775 *
7776 * It does not do any resync itself, but rather "forks" off other threads
7777 * to do that as needed.
7778 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7779 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007780 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 * and wakeups up this thread which will reap the thread and finish up.
7782 * This thread also removes any faulty devices (with nr_pending == 0).
7783 *
7784 * The overall approach is:
7785 * 1/ if the superblock needs updating, update it.
7786 * 2/ If a recovery thread is running, don't do anything else.
7787 * 3/ If recovery has finished, clean up, possibly marking spares active.
7788 * 4/ If there are any faulty devices, remove them.
7789 * 5/ If array is degraded, try to add spares devices
7790 * 6/ If array has spares or is not in-sync, start a resync thread.
7791 */
NeilBrownfd01b882011-10-11 16:47:53 +11007792void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007794 if (mddev->suspended)
7795 return;
7796
NeilBrown5f404022005-06-21 17:17:16 -07007797 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007798 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799
NeilBrownfca4d842005-06-21 17:17:11 -07007800 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007801 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007802 printk(KERN_INFO "md: %s in immediate safe mode\n",
7803 mdname(mddev));
7804 mddev->safemode = 2;
7805 }
7806 flush_signals(current);
7807 }
7808
NeilBrownc89a8ee2008-08-05 15:54:13 +10007809 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007812 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007814 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007815 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007816 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7817 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 ))
7819 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007820
NeilBrowndf5b89b2006-03-27 01:18:20 -08007821 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007822 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007823
NeilBrownc89a8ee2008-08-05 15:54:13 +10007824 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007825 /* On a read-only array we can:
7826 * - remove failed devices
7827 * - add already-in_sync devices if the array itself
7828 * is in-sync.
7829 * As we only add devices that are already in-sync,
7830 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007831 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007832 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007833 /* There is no thread, but we need to call
7834 * ->spare_active and clear saved_raid_disk
7835 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007836 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007837 md_reap_sync_thread(mddev);
7838 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007839 goto unlock;
7840 }
7841
NeilBrown31a59e32008-04-30 00:52:30 -07007842 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007843 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007844 spin_lock_irq(&mddev->write_lock);
7845 if (mddev->safemode &&
7846 !atomic_read(&mddev->writes_pending) &&
7847 !mddev->in_sync &&
7848 mddev->recovery_cp == MaxSector) {
7849 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007850 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007851 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007852 }
7853 if (mddev->safemode == 1)
7854 mddev->safemode = 0;
7855 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007856 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007857 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007858 }
NeilBrownfca4d842005-06-21 17:17:11 -07007859
NeilBrown7a0a5352013-08-27 16:28:23 +10007860 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b422006-10-03 01:15:46 -07007861 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007862
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7864 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7865 /* resync/recovery still happening */
7866 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7867 goto unlock;
7868 }
7869 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007870 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871 goto unlock;
7872 }
Neil Brown72a23c22008-06-28 08:31:41 +10007873 /* Set RUNNING before clearing NEEDED to avoid
7874 * any transients in the value of "sync_action".
7875 */
NeilBrown72f36d52012-10-11 14:25:57 +11007876 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007877 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007878 /* Clear some bits that don't mean anything, but
7879 * might be left set
7880 */
NeilBrown24dd4692005-11-08 21:39:26 -08007881 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7882 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883
NeilBrowned209582012-04-24 10:23:14 +10007884 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7885 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007886 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007887 /* no recovery is running.
7888 * remove any failed drives, then
7889 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007890 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 * the personality to fail the re-add.
7892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007894 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007895 if (mddev->pers->check_reshape == NULL ||
7896 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007897 /* Cannot proceed */
7898 goto unlock;
7899 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007900 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007901 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007902 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7903 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007904 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007905 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007906 } else if (mddev->recovery_cp < MaxSector) {
7907 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007908 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007909 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7910 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007914 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007915 /* We are adding a device or devices to an array
7916 * which has the bitmap stored on all devices.
7917 * So make sure all bitmap pages get written
7918 */
7919 bitmap_write_all(mddev->bitmap);
7920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 mddev->sync_thread = md_register_thread(md_do_sync,
7922 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007923 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 if (!mddev->sync_thread) {
7925 printk(KERN_ERR "%s: could not start resync"
7926 " thread...\n",
7927 mdname(mddev));
7928 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007929 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7930 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7931 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7932 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7933 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007934 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007936 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007937 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938 }
7939 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007940 wake_up(&mddev->sb_wait);
7941
Neil Brown72a23c22008-06-28 08:31:41 +10007942 if (!mddev->sync_thread) {
7943 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7944 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7945 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007946 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007947 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949 mddev_unlock(mddev);
7950 }
7951}
7952
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007953void md_reap_sync_thread(struct mddev *mddev)
7954{
7955 struct md_rdev *rdev;
7956
7957 /* resync has finished, collect result */
7958 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007959 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007960 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7961 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7962 /* success...*/
7963 /* activate any spares */
7964 if (mddev->pers->spare_active(mddev)) {
7965 sysfs_notify(&mddev->kobj, NULL,
7966 "degraded");
7967 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7968 }
7969 }
7970 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7971 mddev->pers->finish_reshape)
7972 mddev->pers->finish_reshape(mddev);
7973
7974 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007975 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007976 */
NeilBrownf4667222013-12-09 12:04:56 +11007977 if (!mddev->degraded)
7978 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007979 rdev->saved_raid_disk = -1;
7980
7981 md_update_sb(mddev, 1);
7982 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7983 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7984 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7985 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7986 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7987 /* flag recovery needed just to double check */
7988 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7989 sysfs_notify_dirent_safe(mddev->sysfs_action);
7990 md_new_event(mddev);
7991 if (mddev->event_work.func)
7992 queue_work(md_misc_wq, &mddev->event_work);
7993}
7994
NeilBrownfd01b882011-10-11 16:47:53 +11007995void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007996{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007997 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007998 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007999 !test_bit(Blocked, &rdev->flags) &&
8000 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008001 msecs_to_jiffies(5000));
8002 rdev_dec_pending(rdev, mddev);
8003}
8004EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8005
NeilBrownc6563a82012-05-21 09:27:00 +10008006void md_finish_reshape(struct mddev *mddev)
8007{
8008 /* called be personality module when reshape completes. */
8009 struct md_rdev *rdev;
8010
8011 rdev_for_each(rdev, mddev) {
8012 if (rdev->data_offset > rdev->new_data_offset)
8013 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8014 else
8015 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8016 rdev->data_offset = rdev->new_data_offset;
8017 }
8018}
8019EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008020
8021/* Bad block management.
8022 * We can record which blocks on each device are 'bad' and so just
8023 * fail those blocks, or that stripe, rather than the whole device.
8024 * Entries in the bad-block table are 64bits wide. This comprises:
8025 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8026 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8027 * A 'shift' can be set so that larger blocks are tracked and
8028 * consequently larger devices can be covered.
8029 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8030 *
8031 * Locking of the bad-block table uses a seqlock so md_is_badblock
8032 * might need to retry if it is very unlucky.
8033 * We will sometimes want to check for bad blocks in a bi_end_io function,
8034 * so we use the write_seqlock_irq variant.
8035 *
8036 * When looking for a bad block we specify a range and want to
8037 * know if any block in the range is bad. So we binary-search
8038 * to the last range that starts at-or-before the given endpoint,
8039 * (or "before the sector after the target range")
8040 * then see if it ends after the given start.
8041 * We return
8042 * 0 if there are no known bad blocks in the range
8043 * 1 if there are known bad block which are all acknowledged
8044 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8045 * plus the start/length of the first bad section we overlap.
8046 */
8047int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8048 sector_t *first_bad, int *bad_sectors)
8049{
8050 int hi;
majianpengab056132012-11-06 17:13:44 +08008051 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008052 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008053 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008054 sector_t target = s + sectors;
8055 unsigned seq;
8056
8057 if (bb->shift > 0) {
8058 /* round the start down, and the end up */
8059 s >>= bb->shift;
8060 target += (1<<bb->shift) - 1;
8061 target >>= bb->shift;
8062 sectors = target - s;
8063 }
8064 /* 'target' is now the first block after the bad range */
8065
8066retry:
8067 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008068 lo = 0;
8069 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008070 hi = bb->count;
8071
8072 /* Binary search between lo and hi for 'target'
8073 * i.e. for the last range that starts before 'target'
8074 */
8075 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8076 * are known not to be the last range before target.
8077 * VARIANT: hi-lo is the number of possible
8078 * ranges, and decreases until it reaches 1
8079 */
8080 while (hi - lo > 1) {
8081 int mid = (lo + hi) / 2;
8082 sector_t a = BB_OFFSET(p[mid]);
8083 if (a < target)
8084 /* This could still be the one, earlier ranges
8085 * could not. */
8086 lo = mid;
8087 else
8088 /* This and later ranges are definitely out. */
8089 hi = mid;
8090 }
8091 /* 'lo' might be the last that started before target, but 'hi' isn't */
8092 if (hi > lo) {
8093 /* need to check all range that end after 's' to see if
8094 * any are unacknowledged.
8095 */
8096 while (lo >= 0 &&
8097 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8098 if (BB_OFFSET(p[lo]) < target) {
8099 /* starts before the end, and finishes after
8100 * the start, so they must overlap
8101 */
8102 if (rv != -1 && BB_ACK(p[lo]))
8103 rv = 1;
8104 else
8105 rv = -1;
8106 *first_bad = BB_OFFSET(p[lo]);
8107 *bad_sectors = BB_LEN(p[lo]);
8108 }
8109 lo--;
8110 }
8111 }
8112
8113 if (read_seqretry(&bb->lock, seq))
8114 goto retry;
8115
8116 return rv;
8117}
8118EXPORT_SYMBOL_GPL(md_is_badblock);
8119
8120/*
8121 * Add a range of bad blocks to the table.
8122 * This might extend the table, or might contract it
8123 * if two adjacent ranges can be merged.
8124 * We binary-search to find the 'insertion' point, then
8125 * decide how best to handle it.
8126 */
8127static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8128 int acknowledged)
8129{
8130 u64 *p;
8131 int lo, hi;
8132 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008133 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008134
8135 if (bb->shift < 0)
8136 /* badblocks are disabled */
8137 return 0;
8138
8139 if (bb->shift) {
8140 /* round the start down, and the end up */
8141 sector_t next = s + sectors;
8142 s >>= bb->shift;
8143 next += (1<<bb->shift) - 1;
8144 next >>= bb->shift;
8145 sectors = next - s;
8146 }
8147
Bian Yu905b0292013-10-12 01:10:03 -04008148 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008149
8150 p = bb->page;
8151 lo = 0;
8152 hi = bb->count;
8153 /* Find the last range that starts at-or-before 's' */
8154 while (hi - lo > 1) {
8155 int mid = (lo + hi) / 2;
8156 sector_t a = BB_OFFSET(p[mid]);
8157 if (a <= s)
8158 lo = mid;
8159 else
8160 hi = mid;
8161 }
8162 if (hi > lo && BB_OFFSET(p[lo]) > s)
8163 hi = lo;
8164
8165 if (hi > lo) {
8166 /* we found a range that might merge with the start
8167 * of our new range
8168 */
8169 sector_t a = BB_OFFSET(p[lo]);
8170 sector_t e = a + BB_LEN(p[lo]);
8171 int ack = BB_ACK(p[lo]);
8172 if (e >= s) {
8173 /* Yes, we can merge with a previous range */
8174 if (s == a && s + sectors >= e)
8175 /* new range covers old */
8176 ack = acknowledged;
8177 else
8178 ack = ack && acknowledged;
8179
8180 if (e < s + sectors)
8181 e = s + sectors;
8182 if (e - a <= BB_MAX_LEN) {
8183 p[lo] = BB_MAKE(a, e-a, ack);
8184 s = e;
8185 } else {
8186 /* does not all fit in one range,
8187 * make p[lo] maximal
8188 */
8189 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8190 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8191 s = a + BB_MAX_LEN;
8192 }
8193 sectors = e - s;
8194 }
8195 }
8196 if (sectors && hi < bb->count) {
8197 /* 'hi' points to the first range that starts after 's'.
8198 * Maybe we can merge with the start of that range */
8199 sector_t a = BB_OFFSET(p[hi]);
8200 sector_t e = a + BB_LEN(p[hi]);
8201 int ack = BB_ACK(p[hi]);
8202 if (a <= s + sectors) {
8203 /* merging is possible */
8204 if (e <= s + sectors) {
8205 /* full overlap */
8206 e = s + sectors;
8207 ack = acknowledged;
8208 } else
8209 ack = ack && acknowledged;
8210
8211 a = s;
8212 if (e - a <= BB_MAX_LEN) {
8213 p[hi] = BB_MAKE(a, e-a, ack);
8214 s = e;
8215 } else {
8216 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8217 s = a + BB_MAX_LEN;
8218 }
8219 sectors = e - s;
8220 lo = hi;
8221 hi++;
8222 }
8223 }
8224 if (sectors == 0 && hi < bb->count) {
8225 /* we might be able to combine lo and hi */
8226 /* Note: 's' is at the end of 'lo' */
8227 sector_t a = BB_OFFSET(p[hi]);
8228 int lolen = BB_LEN(p[lo]);
8229 int hilen = BB_LEN(p[hi]);
8230 int newlen = lolen + hilen - (s - a);
8231 if (s >= a && newlen < BB_MAX_LEN) {
8232 /* yes, we can combine them */
8233 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8234 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8235 memmove(p + hi, p + hi + 1,
8236 (bb->count - hi - 1) * 8);
8237 bb->count--;
8238 }
8239 }
8240 while (sectors) {
8241 /* didn't merge (it all).
8242 * Need to add a range just before 'hi' */
8243 if (bb->count >= MD_MAX_BADBLOCKS) {
8244 /* No room for more */
8245 rv = 0;
8246 break;
8247 } else {
8248 int this_sectors = sectors;
8249 memmove(p + hi + 1, p + hi,
8250 (bb->count - hi) * 8);
8251 bb->count++;
8252
8253 if (this_sectors > BB_MAX_LEN)
8254 this_sectors = BB_MAX_LEN;
8255 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8256 sectors -= this_sectors;
8257 s += this_sectors;
8258 }
8259 }
8260
8261 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008262 if (!acknowledged)
8263 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008264 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008265
8266 return rv;
8267}
8268
NeilBrown3cb03002011-10-11 16:45:26 +11008269int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008270 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008271{
NeilBrownc6563a82012-05-21 09:27:00 +10008272 int rv;
8273 if (is_new)
8274 s += rdev->new_data_offset;
8275 else
8276 s += rdev->data_offset;
8277 rv = md_set_badblocks(&rdev->badblocks,
8278 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008279 if (rv) {
8280 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008281 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008282 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8283 md_wakeup_thread(rdev->mddev->thread);
8284 }
8285 return rv;
8286}
8287EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8288
8289/*
8290 * Remove a range of bad blocks from the table.
8291 * This may involve extending the table if we spilt a region,
8292 * but it must not fail. So if the table becomes full, we just
8293 * drop the remove request.
8294 */
8295static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8296{
8297 u64 *p;
8298 int lo, hi;
8299 sector_t target = s + sectors;
8300 int rv = 0;
8301
8302 if (bb->shift > 0) {
8303 /* When clearing we round the start up and the end down.
8304 * This should not matter as the shift should align with
8305 * the block size and no rounding should ever be needed.
8306 * However it is better the think a block is bad when it
8307 * isn't than to think a block is not bad when it is.
8308 */
8309 s += (1<<bb->shift) - 1;
8310 s >>= bb->shift;
8311 target >>= bb->shift;
8312 sectors = target - s;
8313 }
8314
8315 write_seqlock_irq(&bb->lock);
8316
8317 p = bb->page;
8318 lo = 0;
8319 hi = bb->count;
8320 /* Find the last range that starts before 'target' */
8321 while (hi - lo > 1) {
8322 int mid = (lo + hi) / 2;
8323 sector_t a = BB_OFFSET(p[mid]);
8324 if (a < target)
8325 lo = mid;
8326 else
8327 hi = mid;
8328 }
8329 if (hi > lo) {
8330 /* p[lo] is the last range that could overlap the
8331 * current range. Earlier ranges could also overlap,
8332 * but only this one can overlap the end of the range.
8333 */
8334 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8335 /* Partial overlap, leave the tail of this range */
8336 int ack = BB_ACK(p[lo]);
8337 sector_t a = BB_OFFSET(p[lo]);
8338 sector_t end = a + BB_LEN(p[lo]);
8339
8340 if (a < s) {
8341 /* we need to split this range */
8342 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008343 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008344 goto out;
8345 }
8346 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8347 bb->count++;
8348 p[lo] = BB_MAKE(a, s-a, ack);
8349 lo++;
8350 }
8351 p[lo] = BB_MAKE(target, end - target, ack);
8352 /* there is no longer an overlap */
8353 hi = lo;
8354 lo--;
8355 }
8356 while (lo >= 0 &&
8357 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8358 /* This range does overlap */
8359 if (BB_OFFSET(p[lo]) < s) {
8360 /* Keep the early parts of this range. */
8361 int ack = BB_ACK(p[lo]);
8362 sector_t start = BB_OFFSET(p[lo]);
8363 p[lo] = BB_MAKE(start, s - start, ack);
8364 /* now low doesn't overlap, so.. */
8365 break;
8366 }
8367 lo--;
8368 }
8369 /* 'lo' is strictly before, 'hi' is strictly after,
8370 * anything between needs to be discarded
8371 */
8372 if (hi - lo > 1) {
8373 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8374 bb->count -= (hi - lo - 1);
8375 }
8376 }
8377
8378 bb->changed = 1;
8379out:
8380 write_sequnlock_irq(&bb->lock);
8381 return rv;
8382}
8383
NeilBrownc6563a82012-05-21 09:27:00 +10008384int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8385 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008386{
NeilBrownc6563a82012-05-21 09:27:00 +10008387 if (is_new)
8388 s += rdev->new_data_offset;
8389 else
8390 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008391 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008392 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008393}
8394EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8395
8396/*
8397 * Acknowledge all bad blocks in a list.
8398 * This only succeeds if ->changed is clear. It is used by
8399 * in-kernel metadata updates
8400 */
8401void md_ack_all_badblocks(struct badblocks *bb)
8402{
8403 if (bb->page == NULL || bb->changed)
8404 /* no point even trying */
8405 return;
8406 write_seqlock_irq(&bb->lock);
8407
majianpengecb178b2012-03-19 12:46:42 +11008408 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008409 u64 *p = bb->page;
8410 int i;
8411 for (i = 0; i < bb->count ; i++) {
8412 if (!BB_ACK(p[i])) {
8413 sector_t start = BB_OFFSET(p[i]);
8414 int len = BB_LEN(p[i]);
8415 p[i] = BB_MAKE(start, len, 1);
8416 }
8417 }
NeilBrownde393cd2011-07-28 11:31:48 +10008418 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008419 }
8420 write_sequnlock_irq(&bb->lock);
8421}
8422EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8423
NeilBrown16c791a2011-07-28 11:31:47 +10008424/* sysfs access to bad-blocks list.
8425 * We present two files.
8426 * 'bad-blocks' lists sector numbers and lengths of ranges that
8427 * are recorded as bad. The list is truncated to fit within
8428 * the one-page limit of sysfs.
8429 * Writing "sector length" to this file adds an acknowledged
8430 * bad block list.
8431 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8432 * been acknowledged. Writing to this file adds bad blocks
8433 * without acknowledging them. This is largely for testing.
8434 */
8435
8436static ssize_t
8437badblocks_show(struct badblocks *bb, char *page, int unack)
8438{
8439 size_t len;
8440 int i;
8441 u64 *p = bb->page;
8442 unsigned seq;
8443
8444 if (bb->shift < 0)
8445 return 0;
8446
8447retry:
8448 seq = read_seqbegin(&bb->lock);
8449
8450 len = 0;
8451 i = 0;
8452
8453 while (len < PAGE_SIZE && i < bb->count) {
8454 sector_t s = BB_OFFSET(p[i]);
8455 unsigned int length = BB_LEN(p[i]);
8456 int ack = BB_ACK(p[i]);
8457 i++;
8458
8459 if (unack && ack)
8460 continue;
8461
8462 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8463 (unsigned long long)s << bb->shift,
8464 length << bb->shift);
8465 }
NeilBrownde393cd2011-07-28 11:31:48 +10008466 if (unack && len == 0)
8467 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008468
8469 if (read_seqretry(&bb->lock, seq))
8470 goto retry;
8471
8472 return len;
8473}
8474
8475#define DO_DEBUG 1
8476
8477static ssize_t
8478badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8479{
8480 unsigned long long sector;
8481 int length;
8482 char newline;
8483#ifdef DO_DEBUG
8484 /* Allow clearing via sysfs *only* for testing/debugging.
8485 * Normally only a successful write may clear a badblock
8486 */
8487 int clear = 0;
8488 if (page[0] == '-') {
8489 clear = 1;
8490 page++;
8491 }
8492#endif /* DO_DEBUG */
8493
8494 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8495 case 3:
8496 if (newline != '\n')
8497 return -EINVAL;
8498 case 2:
8499 if (length <= 0)
8500 return -EINVAL;
8501 break;
8502 default:
8503 return -EINVAL;
8504 }
8505
8506#ifdef DO_DEBUG
8507 if (clear) {
8508 md_clear_badblocks(bb, sector, length);
8509 return len;
8510 }
8511#endif /* DO_DEBUG */
8512 if (md_set_badblocks(bb, sector, length, !unack))
8513 return len;
8514 else
8515 return -ENOSPC;
8516}
8517
Adrian Bunk75c96f82005-05-05 16:16:09 -07008518static int md_notify_reboot(struct notifier_block *this,
8519 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520{
8521 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008522 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008523 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524
NeilBrownc744a652012-03-19 12:46:37 +11008525 for_each_mddev(mddev, tmp) {
8526 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008527 if (mddev->pers)
8528 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008529 if (mddev->persistent)
8530 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008531 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008532 }
NeilBrownc744a652012-03-19 12:46:37 +11008533 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 }
NeilBrownc744a652012-03-19 12:46:37 +11008535 /*
8536 * certain more exotic SCSI devices are known to be
8537 * volatile wrt too early system reboots. While the
8538 * right place to handle this issue is the given
8539 * driver, we do want to have a safe RAID driver ...
8540 */
8541 if (need_delay)
8542 mdelay(1000*1);
8543
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 return NOTIFY_DONE;
8545}
8546
Adrian Bunk75c96f82005-05-05 16:16:09 -07008547static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 .notifier_call = md_notify_reboot,
8549 .next = NULL,
8550 .priority = INT_MAX, /* before any real devices */
8551};
8552
8553static void md_geninit(void)
8554{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008555 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008557 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558}
8559
Adrian Bunk75c96f82005-05-05 16:16:09 -07008560static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561{
Tejun Heoe804ac72010-10-15 15:36:08 +02008562 int ret = -ENOMEM;
8563
Tejun Heoada609e2011-01-25 14:35:54 +01008564 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008565 if (!md_wq)
8566 goto err_wq;
8567
8568 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8569 if (!md_misc_wq)
8570 goto err_misc_wq;
8571
8572 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8573 goto err_md;
8574
8575 if ((ret = register_blkdev(0, "mdp")) < 0)
8576 goto err_mdp;
8577 mdp_major = ret;
8578
NeilBrownaf5628f2014-07-31 13:54:54 +10008579 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008580 md_probe, NULL, NULL);
8581 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 md_probe, NULL, NULL);
8583
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008585 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
8587 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Tejun Heoe804ac72010-10-15 15:36:08 +02008590err_mdp:
8591 unregister_blkdev(MD_MAJOR, "md");
8592err_md:
8593 destroy_workqueue(md_misc_wq);
8594err_misc_wq:
8595 destroy_workqueue(md_wq);
8596err_wq:
8597 return ret;
8598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
8600#ifndef MODULE
8601
8602/*
8603 * Searches all registered partitions for autorun RAID arrays
8604 * at boot time.
8605 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008606
8607static LIST_HEAD(all_detected_devices);
8608struct detected_devices_node {
8609 struct list_head list;
8610 dev_t dev;
8611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612
8613void md_autodetect_dev(dev_t dev)
8614{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008615 struct detected_devices_node *node_detected_dev;
8616
8617 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8618 if (node_detected_dev) {
8619 node_detected_dev->dev = dev;
8620 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8621 } else {
8622 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8623 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625}
8626
8627
8628static void autostart_arrays(int part)
8629{
NeilBrown3cb03002011-10-11 16:45:26 +11008630 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008631 struct detected_devices_node *node_detected_dev;
8632 dev_t dev;
8633 int i_scanned, i_passed;
8634
8635 i_scanned = 0;
8636 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637
8638 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8639
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008640 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8641 i_scanned++;
8642 node_detected_dev = list_entry(all_detected_devices.next,
8643 struct detected_devices_node, list);
8644 list_del(&node_detected_dev->list);
8645 dev = node_detected_dev->dev;
8646 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008647 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008648 if (IS_ERR(rdev))
8649 continue;
8650
NeilBrownb2d444d2005-11-08 21:39:31 -08008651 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652 MD_BUG();
8653 continue;
8654 }
NeilBrownd0fae182008-03-04 14:29:31 -08008655 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008657 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008659
8660 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8661 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662
8663 autorun_devices(part);
8664}
8665
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008666#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667
8668static __exit void md_exit(void)
8669{
NeilBrownfd01b882011-10-11 16:47:53 +11008670 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008672 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008673
NeilBrownaf5628f2014-07-31 13:54:54 +10008674 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008675 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008677 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 unregister_blkdev(mdp_major, "mdp");
8679 unregister_reboot_notifier(&md_notifier);
8680 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008681
8682 /* We cannot unload the modules while some process is
8683 * waiting for us in select() or poll() - wake them up
8684 */
8685 md_unloading = 1;
8686 while (waitqueue_active(&md_event_waiters)) {
8687 /* not safe to leave yet */
8688 wake_up(&md_event_waiters);
8689 msleep(delay);
8690 delay += delay;
8691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008693
NeilBrown29ac4aa2008-02-06 01:39:58 -08008694 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008696 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008698 destroy_workqueue(md_misc_wq);
8699 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700}
8701
Dan Williams685784a2007-07-09 11:56:42 -07008702subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703module_exit(md_exit)
8704
NeilBrownf91de922005-11-08 21:39:36 -08008705static int get_ro(char *buffer, struct kernel_param *kp)
8706{
8707 return sprintf(buffer, "%d", start_readonly);
8708}
8709static int set_ro(const char *val, struct kernel_param *kp)
8710{
8711 char *e;
8712 int num = simple_strtoul(val, &e, 10);
8713 if (*val && (*e == '\0' || *e == '\n')) {
8714 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008715 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008716 }
8717 return -EINVAL;
8718}
8719
NeilBrown80ca3a42006-07-10 04:44:18 -07008720module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8721module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008722
NeilBrownefeb53c2009-01-09 08:31:10 +11008723module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008724
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725EXPORT_SYMBOL(register_md_personality);
8726EXPORT_SYMBOL(unregister_md_personality);
8727EXPORT_SYMBOL(md_error);
8728EXPORT_SYMBOL(md_done_sync);
8729EXPORT_SYMBOL(md_write_start);
8730EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731EXPORT_SYMBOL(md_register_thread);
8732EXPORT_SYMBOL(md_unregister_thread);
8733EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008735EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008737MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008738MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008739MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);