blob: 6be7146be5b3398ca0adf77715f855ac6fe50860 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
NeilBrown746d3202013-04-24 11:42:41 +100075static int remove_and_add_spares(struct mddev *mddev,
76 struct md_rdev *this);
77
Adrian Bunk5e563412006-06-26 00:27:42 -070078#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Robert Becker1e509152009-12-14 12:49:58 +110081 * Default number of read corrections we'll attempt on an rdev
82 * before ejecting it from the array. We divide the read error
83 * count by 2 for every hour elapsed between read errors.
84 */
85#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88 * is 1000 KB/sec, so the extra system load does not show up that much.
89 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070090 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * subsystem is idle. There is also an 'absolute maximum' reconstruction
92 * speed limit - in case reconstruction slows down your system despite
93 * idle IO detection.
94 *
95 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080096 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
99static int sysctl_speed_limit_min = 1000;
100static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +1100101static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800102{
103 return mddev->sync_speed_min ?
104 mddev->sync_speed_min : sysctl_speed_limit_min;
105}
106
NeilBrownfd01b882011-10-11 16:47:53 +1100107static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800108{
109 return mddev->sync_speed_max ?
110 mddev->sync_speed_max : sysctl_speed_limit_max;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113static struct ctl_table_header *raid_table_header;
114
Joe Perches82592c32013-11-14 15:16:18 +1100115static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_min",
118 .data = &sysctl_speed_limit_min,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .procname = "speed_limit_max",
125 .data = &sysctl_speed_limit_max,
126 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800128 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Joe Perches82592c32013-11-14 15:16:18 +1100133static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "raid",
136 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700137 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .child = raid_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Joe Perches82592c32013-11-14 15:16:18 +1100143static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .procname = "dev",
146 .maxlen = 0,
147 .mode = 0555,
148 .child = raid_dir_table,
149 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800150 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700153static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
NeilBrownf91de922005-11-08 21:39:36 -0800155static int start_readonly;
156
NeilBrowna167f662010-10-26 18:31:13 +1100157/* bio_clone_mddev
158 * like bio_clone, but with a local bio set
159 */
160
NeilBrowna167f662010-10-26 18:31:13 +1100161struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100162 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100163{
164 struct bio *b;
NeilBrowna167f662010-10-26 18:31:13 +1100165
166 if (!mddev || !mddev->bio_set)
167 return bio_alloc(gfp_mask, nr_iovecs);
168
Kent Overstreet395c72a2012-09-06 15:34:55 -0700169 b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100170 if (!b)
171 return NULL;
NeilBrowna167f662010-10-26 18:31:13 +1100172 return b;
173}
174EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100177 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100178{
NeilBrowna167f662010-10-26 18:31:13 +1100179 if (!mddev || !mddev->bio_set)
180 return bio_clone(bio, gfp_mask);
181
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700182 return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100183}
184EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
NeilBrownd7603b72006-01-06 00:20:30 -0800187 * We have a system wide 'event count' that is incremented
188 * on any 'interesting' event, and readers of /proc/mdstat
189 * can use 'poll' or 'select' to find out when the event
190 * count increases.
191 *
192 * Events are:
193 * start array, stop array, error, add device, remove device,
194 * start build, activate spare
195 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800196static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800197static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100198void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800199{
200 atomic_inc(&md_event_count);
201 wake_up(&md_event_waiters);
202}
NeilBrown29269552006-03-27 01:18:10 -0800203EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800204
NeilBrownc331eb02006-05-30 21:27:13 -0700205/* Alternate version that can be called from interrupts
206 * when calling sysfs_notify isn't needed.
207 */
NeilBrownfd01b882011-10-11 16:47:53 +1100208static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700209{
210 atomic_inc(&md_event_count);
211 wake_up(&md_event_waiters);
212}
213
NeilBrownd7603b72006-01-06 00:20:30 -0800214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Enables to iterate over all existing md arrays
216 * all_mddevs_lock protects this list.
217 */
218static LIST_HEAD(all_mddevs);
219static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222/*
223 * iterates through all used mddevs in the system.
224 * We take care to grab the all_mddevs_lock whenever navigating
225 * the list, and to always hold a refcount when unlocked.
226 * Any code which breaks out of this loop while own
227 * a reference to the current mddev and must mddev_put it.
228 */
NeilBrownfd01b882011-10-11 16:47:53 +1100229#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 \
231 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100232 _tmp = all_mddevs.next; \
233 _mddev = NULL;}); \
234 ({ if (_tmp != &all_mddevs) \
235 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100237 if (_mddev) mddev_put(_mddev); \
238 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
239 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100241 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 )
243
244
NeilBrown409c57f2009-03-31 14:39:39 +1100245/* Rather than calling directly into the personality make_request function,
246 * IO requests come here first so that we can check if the device is
247 * being suspended pending a reconfiguration.
248 * We hold a refcount over the call to ->make_request. By the time that
249 * call has finished, the bio has been linked into some internal structure
250 * and so is visible to ->quiesce(), so we don't need the refcount any more.
251 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200252static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
NeilBrown49077322010-03-25 16:20:56 +1100254 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100255 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100256 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500257 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100258
NeilBrown0ca69882011-01-14 09:14:33 +1100259 if (mddev == NULL || mddev->pers == NULL
260 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100261 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200262 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100263 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100264 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266 return;
267 }
NeilBrown0ca69882011-01-14 09:14:33 +1100268 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100269 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200270 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100271 DEFINE_WAIT(__wait);
272 for (;;) {
273 prepare_to_wait(&mddev->sb_wait, &__wait,
274 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200275 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100276 break;
277 rcu_read_unlock();
278 schedule();
279 rcu_read_lock();
280 }
281 finish_wait(&mddev->sb_wait, &__wait);
282 }
283 atomic_inc(&mddev->active_io);
284 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100285
Chris Masone91ece52011-02-07 19:21:48 -0500286 /*
287 * save the sectors now since our bio can
288 * go away inside make_request
289 */
290 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200291 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100292
293 cpu = part_stat_lock();
294 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500295 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100296 part_stat_unlock();
297
NeilBrown409c57f2009-03-31 14:39:39 +1100298 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
NeilBrown9e35b992010-04-06 14:23:02 +1000302/* mddev_suspend makes sure no new requests are submitted
303 * to the device, and that any requests that have been submitted
304 * are completely handled.
305 * Once ->stop is called and completes, the module will be completely
306 * unused.
307 */
NeilBrownfd01b882011-10-11 16:47:53 +1100308void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100309{
310 BUG_ON(mddev->suspended);
311 mddev->suspended = 1;
312 synchronize_rcu();
313 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500315
316 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100317}
NeilBrown390ee602010-06-01 19:37:27 +1000318EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100319
NeilBrownfd01b882011-10-11 16:47:53 +1100320void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100321{
322 mddev->suspended = 0;
323 wake_up(&mddev->sb_wait);
324 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500325
Jonathan Brassow47525e52012-05-22 13:55:29 +1000326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500327 md_wakeup_thread(mddev->thread);
328 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100329}
NeilBrown390ee602010-06-01 19:37:27 +1000330EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100331
NeilBrownfd01b882011-10-11 16:47:53 +1100332int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000333{
334 return mddev->suspended;
335}
336EXPORT_SYMBOL(mddev_congested);
337
NeilBrowna2826aa2009-12-14 12:49:49 +1100338/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200339 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100340 */
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343{
NeilBrown3cb03002011-10-11 16:45:26 +1100344 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100345 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100346
347 rdev_dec_pending(rdev, mddev);
348
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200351 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 }
353 bio_put(bio);
354}
355
NeilBrowna7a07e62010-12-09 16:04:25 +1100356static void md_submit_flush_data(struct work_struct *ws);
357
NeilBrowna035fc32010-12-09 16:17:51 +1100358static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100359{
NeilBrownfd01b882011-10-11 16:47:53 +1100360 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100361 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100362
NeilBrowna7a07e62010-12-09 16:04:25 +1100363 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100366 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100367 if (rdev->raid_disk >= 0 &&
368 !test_bit(Faulty, &rdev->flags)) {
369 /* Take two references, one is dropped
370 * when request finishes, one after
371 * we reclaim rcu_read_lock
372 */
373 struct bio *bi;
374 atomic_inc(&rdev->nr_pending);
375 atomic_inc(&rdev->nr_pending);
376 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000377 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 bi->bi_private = rdev;
380 bi->bi_bdev = rdev->bdev;
381 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200382 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 rdev_dec_pending(rdev, mddev);
385 }
386 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100387 if (atomic_dec_and_test(&mddev->flush_pending))
388 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389}
390
Tejun Heoe9c74692010-09-03 11:56:18 +0200391static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100392{
NeilBrownfd01b882011-10-11 16:47:53 +1100393 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200394 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100395
Kent Overstreet4f024f32013-10-11 15:44:27 -0700396 if (bio->bi_iter.bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 /* an empty barrier - all done */
398 bio_endio(bio, 0);
399 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200401 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100402 }
NeilBrown2b74e122010-12-09 15:59:01 +1100403
404 mddev->flush_bio = NULL;
405 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100406}
407
NeilBrownfd01b882011-10-11 16:47:53 +1100408void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100409{
410 spin_lock_irq(&mddev->write_lock);
411 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200412 !mddev->flush_bio,
Lukas Czernereed8c022012-11-30 11:42:40 +0100413 mddev->write_lock);
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 spin_unlock_irq(&mddev->write_lock);
416
NeilBrowna035fc32010-12-09 16:17:51 +1100417 INIT_WORK(&mddev->flush_work, submit_flushes);
418 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100419}
Tejun Heoe9c74692010-09-03 11:56:18 +0200420EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100421
NeilBrown74018dc2012-07-31 09:08:15 +0200422void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000423{
NeilBrown9cbb1752012-07-31 09:08:14 +0200424 struct mddev *mddev = cb->data;
425 md_wakeup_thread(mddev->thread);
426 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000427}
NeilBrown9cbb1752012-07-31 09:08:14 +0200428EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000429
NeilBrownfd01b882011-10-11 16:47:53 +1100430static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 atomic_inc(&mddev->active);
433 return mddev;
434}
435
Dan Williams5fd3a172009-03-04 00:57:25 -0700436static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100437
NeilBrownfd01b882011-10-11 16:47:53 +1100438static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
NeilBrowna167f662010-10-26 18:31:13 +1100440 struct bio_set *bs = NULL;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443 return;
NeilBrownd3374822009-01-09 08:31:10 +1100444 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100445 mddev->ctime == 0 && !mddev->hold_active) {
446 /* Array is not configured at all, and not held active,
447 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100448 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100449 bs = mddev->bio_set;
450 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100451 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200452 /* We did a probe so need to clean up. Call
453 * queue_work inside the spinlock so that
454 * flush_workqueue() after mddev_find will
455 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100456 */
457 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200458 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100459 } else
460 kfree(mddev);
461 }
462 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100463 if (bs)
464 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrownfd01b882011-10-11 16:47:53 +1100467void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000483 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500484 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100485 mddev->resync_min = 0;
486 mddev->resync_max = MaxSector;
487 mddev->level = LEVEL_NONE;
488}
NeilBrown390ee602010-06-01 19:37:27 +1000489EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100490
NeilBrownfd01b882011-10-11 16:47:53 +1100491static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownfd01b882011-10-11 16:47:53 +1100493 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown8f5f02c2011-02-16 13:58:51 +1100495 if (unit && MAJOR(unit) != MD_MAJOR)
496 unit &= ~((1<<MdpMinorShift)-1);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retry:
499 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
NeilBrownefeb53c2009-01-09 08:31:10 +1100501 if (unit) {
502 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503 if (mddev->unit == unit) {
504 mddev_get(mddev);
505 spin_unlock(&all_mddevs_lock);
506 kfree(new);
507 return mddev;
508 }
509
510 if (new) {
511 list_add(&new->all_mddevs, &all_mddevs);
512 spin_unlock(&all_mddevs_lock);
513 new->hold_active = UNTIL_IOCTL;
514 return new;
515 }
516 } else if (new) {
517 /* find an unused unit number */
518 static int next_minor = 512;
519 int start = next_minor;
520 int is_free = 0;
521 int dev = 0;
522 while (!is_free) {
523 dev = MKDEV(MD_MAJOR, next_minor);
524 next_minor++;
525 if (next_minor > MINORMASK)
526 next_minor = 0;
527 if (next_minor == start) {
528 /* Oh dear, all in use. */
529 spin_unlock(&all_mddevs_lock);
530 kfree(new);
531 return NULL;
532 }
533
534 is_free = 1;
535 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536 if (mddev->unit == dev) {
537 is_free = 0;
538 break;
539 }
540 }
541 new->unit = dev;
542 new->md_minor = MINOR(dev);
543 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 list_add(&new->all_mddevs, &all_mddevs);
545 spin_unlock(&all_mddevs_lock);
546 return new;
547 }
548 spin_unlock(&all_mddevs_lock);
549
NeilBrown9ffae0c2006-01-06 00:20:32 -0800550 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!new)
552 return NULL;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 new->unit = unit;
555 if (MAJOR(unit) == MD_MAJOR)
556 new->md_minor = MINOR(unit);
557 else
558 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
NeilBrownfafd7fb2010-04-01 15:55:30 +1100560 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto retry;
563}
564
NeilBrown29f097c2013-11-14 17:54:51 +1100565static inline int __must_check mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800567 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
NeilBrown29f097c2013-11-14 17:54:51 +1100570/* Sometimes we need to take the lock in a situation where
571 * failure due to interrupts is not acceptable.
572 */
573static inline void mddev_lock_nointr(struct mddev * mddev)
574{
575 mutex_lock(&mddev->reconfig_mutex);
576}
577
NeilBrownfd01b882011-10-11 16:47:53 +1100578static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100579{
580 return mutex_is_locked(&mddev->reconfig_mutex);
581}
582
NeilBrownfd01b882011-10-11 16:47:53 +1100583static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800585 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrownb6eb1272010-04-15 10:13:47 +1000588static struct attribute_group md_redundancy_group;
589
NeilBrownfd01b882011-10-11 16:47:53 +1100590static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
NeilBrowna64c8762010-04-14 17:15:37 +1000592 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000593 /* These cannot be removed under reconfig_mutex as
594 * an access to the files will try to take reconfig_mutex
595 * while holding the file unremovable, which leads to
596 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000597 * So hold set sysfs_active while the remove in happeing,
598 * and anything else which might set ->to_remove or my
599 * otherwise change the sysfs namespace will fail with
600 * -EBUSY if sysfs_active is still set.
601 * We set sysfs_active under reconfig_mutex and elsewhere
602 * test it under the same mutex to ensure its correct value
603 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000604 */
NeilBrowna64c8762010-04-14 17:15:37 +1000605 struct attribute_group *to_remove = mddev->to_remove;
606 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000607 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000608 mutex_unlock(&mddev->reconfig_mutex);
609
NeilBrown00bcb4a2010-06-01 19:37:23 +1000610 if (mddev->kobj.sd) {
611 if (to_remove != &md_redundancy_group)
612 sysfs_remove_group(&mddev->kobj, to_remove);
613 if (mddev->pers == NULL ||
614 mddev->pers->sync_request == NULL) {
615 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616 if (mddev->sysfs_action)
617 sysfs_put(mddev->sysfs_action);
618 mddev->sysfs_action = NULL;
619 }
NeilBrowna64c8762010-04-14 17:15:37 +1000620 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000621 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000622 } else
623 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chris Dunlop751e67c2011-10-19 16:48:26 +1100625 /* As we've dropped the mutex we need a spinlock to
626 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000627 */
628 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700629 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000630 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrownfd01b882011-10-11 16:47:53 +1100633static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
NeilBrown3cb03002011-10-11 16:45:26 +1100635 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
NeilBrowndafb20f2012-03-19 12:46:39 +1100637 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (rdev->desc_nr == nr)
639 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return NULL;
642}
643
NeilBrown1ca69c42012-10-11 13:37:33 +1100644static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
645{
646 struct md_rdev *rdev;
647
648 rdev_for_each_rcu(rdev, mddev)
649 if (rdev->desc_nr == nr)
650 return rdev;
651
652 return NULL;
653}
654
655static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
NeilBrown3cb03002011-10-11 16:45:26 +1100657 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
NeilBrowndafb20f2012-03-19 12:46:39 +1100659 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (rdev->bdev->bd_dev == dev)
661 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return NULL;
664}
665
NeilBrown1ca69c42012-10-11 13:37:33 +1100666static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
667{
668 struct md_rdev *rdev;
669
670 rdev_for_each_rcu(rdev, mddev)
671 if (rdev->bdev->bd_dev == dev)
672 return rdev;
673
674 return NULL;
675}
676
NeilBrown84fc4b52011-10-11 16:49:58 +1100677static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800678{
NeilBrown84fc4b52011-10-11 16:49:58 +1100679 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800680 list_for_each_entry(pers, &pers_list, list) {
681 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800682 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800683 if (strcmp(pers->name, clevel)==0)
684 return pers;
685 }
NeilBrown2604b702006-01-06 00:20:36 -0800686 return NULL;
687}
688
Andre Nollb73df2d2008-07-11 22:02:23 +1000689/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100690static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100692 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000693 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrown3cb03002011-10-11 16:45:26 +1100696static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 if (rdev->sb_page)
699 MD_BUG();
700
701 rdev->sb_page = alloc_page(GFP_KERNEL);
702 if (!rdev->sb_page) {
703 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000704 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 return 0;
708}
709
NeilBrown545c8792012-05-22 13:54:30 +1000710void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800713 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 rdev->sb_loaded = 0;
715 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000716 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100717 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
NeilBrown2699b672011-07-28 11:31:47 +1000719 if (rdev->bb_page) {
720 put_page(rdev->bb_page);
721 rdev->bb_page = NULL;
722 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000723 kfree(rdev->badblocks.page);
724 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
NeilBrown545c8792012-05-22 13:54:30 +1000726EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
NeilBrown6712ecf2007-09-27 12:47:43 +0200728static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700729{
NeilBrown3cb03002011-10-11 16:45:26 +1100730 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100731 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700732
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700733 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
734 printk("md: super_written gets error=%d, uptodate=%d\n",
735 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
736 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800737 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700738 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739
NeilBrowna9701a32005-11-08 21:39:34 -0800740 if (atomic_dec_and_test(&mddev->pending_writes))
741 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700742 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743}
744
NeilBrownfd01b882011-10-11 16:47:53 +1100745void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 sector_t sector, int size, struct page *page)
747{
748 /* write first size bytes of page to sector of rdev
749 * Increment mddev->pending_writes before returning
750 * and decrement it on completion, waking up sb_wait
751 * if zero is reached.
752 * If an error occurred, call md_error
753 */
NeilBrowna167f662010-10-26 18:31:13 +1100754 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100756 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700757 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700758 bio_add_page(bio, page, size, 0);
759 bio->bi_private = rdev;
760 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800761
NeilBrown7bfa19f2005-06-21 17:17:28 -0700762 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000763 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800764}
765
NeilBrownfd01b882011-10-11 16:47:53 +1100766void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800767{
Tejun Heoe9c74692010-09-03 11:56:18 +0200768 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000769 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700770}
771
NeilBrown3cb03002011-10-11 16:45:26 +1100772int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100773 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
NeilBrowna167f662010-10-26 18:31:13 +1100775 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int ret;
777
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100778 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
779 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100780 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700781 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000782 else if (rdev->mddev->reshape_position != MaxSector &&
783 (rdev->mddev->reshape_backwards ==
784 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700785 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100786 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700787 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 bio_add_page(bio, page, size, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -0700789 submit_bio_wait(rw, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
792 bio_put(bio);
793 return ret;
794}
NeilBrowna8745db2006-01-06 00:20:34 -0800795EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
NeilBrown3cb03002011-10-11 16:45:26 +1100797static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 char b[BDEVNAME_SIZE];
800 if (!rdev->sb_page) {
801 MD_BUG();
802 return -EINVAL;
803 }
804 if (rdev->sb_loaded)
805 return 0;
806
807
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100808 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto fail;
810 rdev->sb_loaded = 1;
811 return 0;
812
813fail:
814 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
815 bdevname(rdev->bdev,b));
816 return -EINVAL;
817}
818
819static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
820{
Andre Noll05710462008-07-11 22:02:20 +1000821 return sb1->set_uuid0 == sb2->set_uuid0 &&
822 sb1->set_uuid1 == sb2->set_uuid1 &&
823 sb1->set_uuid2 == sb2->set_uuid2 &&
824 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
828{
829 int ret;
830 mdp_super_t *tmp1, *tmp2;
831
832 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
833 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
834
835 if (!tmp1 || !tmp2) {
836 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100837 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto abort;
839 }
840
841 *tmp1 = *sb1;
842 *tmp2 = *sb2;
843
844 /*
845 * nr_disks is not constant
846 */
847 tmp1->nr_disks = 0;
848 tmp2->nr_disks = 0;
849
Andre Nollce0c8e02008-07-11 22:02:20 +1000850 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700852 kfree(tmp1);
853 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ret;
855}
856
NeilBrown4d167f02007-05-09 02:35:37 -0700857
858static u32 md_csum_fold(u32 csum)
859{
860 csum = (csum & 0xffff) + (csum >> 16);
861 return (csum & 0xffff) + (csum >> 16);
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static unsigned int calc_sb_csum(mdp_super_t * sb)
865{
NeilBrown4d167f02007-05-09 02:35:37 -0700866 u64 newcsum = 0;
867 u32 *sb32 = (u32*)sb;
868 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 unsigned int disk_csum, csum;
870
871 disk_csum = sb->sb_csum;
872 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700873
874 for (i = 0; i < MD_SB_BYTES/4 ; i++)
875 newcsum += sb32[i];
876 csum = (newcsum & 0xffffffff) + (newcsum>>32);
877
878
879#ifdef CONFIG_ALPHA
880 /* This used to use csum_partial, which was wrong for several
881 * reasons including that different results are returned on
882 * different architectures. It isn't critical that we get exactly
883 * the same return value as before (we always csum_fold before
884 * testing, and that removes any differences). However as we
885 * know that csum_partial always returned a 16bit value on
886 * alphas, do a fold to maximise conformity to previous behaviour.
887 */
888 sb->sb_csum = md_csum_fold(disk_csum);
889#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700891#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return csum;
893}
894
895
896/*
897 * Handle superblock details.
898 * We want to be able to handle multiple superblock formats
899 * so we have a common interface to them all, and an array of
900 * different handlers.
901 * We rely on user-space to write the initial superblock, and support
902 * reading and updating of superblocks.
903 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100904 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * loads and validates a superblock on dev.
906 * if refdev != NULL, compare superblocks on both devices
907 * Return:
908 * 0 - dev has a superblock that is compatible with refdev
909 * 1 - dev has a superblock that is compatible and newer than refdev
910 * so dev should be used as the refdev in future
911 * -EINVAL superblock incompatible or invalid
912 * -othererror e.g. -EIO
913 *
NeilBrownfd01b882011-10-11 16:47:53 +1100914 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * Verify that dev is acceptable into mddev.
916 * The first time, mddev->raid_disks will be 0, and data from
917 * dev should be merged in. Subsequent calls check that dev
918 * is new enough. Return 0 or -EINVAL
919 *
NeilBrownfd01b882011-10-11 16:47:53 +1100920 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * Update the superblock for rdev with data in mddev
922 * This does not write to disc.
923 *
924 */
925
926struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000927 char *name;
928 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000929 int (*load_super)(struct md_rdev *rdev,
930 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000931 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000932 int (*validate_super)(struct mddev *mddev,
933 struct md_rdev *rdev);
934 void (*sync_super)(struct mddev *mddev,
935 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +1100936 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000937 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +1000938 int (*allow_new_offset)(struct md_rdev *rdev,
939 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940};
941
942/*
Andre Noll0894cc32009-06-18 08:49:23 +1000943 * Check that the given mddev has no bitmap.
944 *
945 * This function is called from the run method of all personalities that do not
946 * support bitmaps. It prints an error message and returns non-zero if mddev
947 * has a bitmap. Otherwise, it returns 0.
948 *
949 */
NeilBrownfd01b882011-10-11 16:47:53 +1100950int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +1000951{
NeilBrownc3d97142009-12-14 12:49:52 +1100952 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000953 return 0;
954 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
955 mdname(mddev), mddev->pers->name);
956 return 1;
957}
958EXPORT_SYMBOL(md_check_no_bitmap);
959
960/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * load_super for 0.90.0
962 */
NeilBrown3cb03002011-10-11 16:45:26 +1100963static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
966 mdp_super_t *sb;
967 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /*
Andre Noll0f420352008-07-11 22:02:23 +1000970 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 * it's at the end of the disk.
972 *
973 * It also happens to be a multiple of 4Kb.
974 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100975 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
NeilBrown0002b272005-09-09 16:23:53 -0700977 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (ret) return ret;
979
980 ret = -EINVAL;
981
982 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +1000983 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (sb->md_magic != MD_SB_MAGIC) {
986 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
987 b);
988 goto abort;
989 }
990
991 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800992 sb->minor_version < 90 ||
993 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
995 sb->major_version, sb->minor_version,
996 b);
997 goto abort;
998 }
999
1000 if (sb->raid_disks <= 0)
1001 goto abort;
1002
NeilBrown4d167f02007-05-09 02:35:37 -07001003 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1005 b);
1006 goto abort;
1007 }
1008
1009 rdev->preferred_minor = sb->md_minor;
1010 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001011 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001012 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001013 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (sb->level == LEVEL_MULTIPATH)
1016 rdev->desc_nr = -1;
1017 else
1018 rdev->desc_nr = sb->this_disk.number;
1019
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001020 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001022 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001024 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (!uuid_equal(refsb, sb)) {
1026 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1027 b, bdevname(refdev->bdev,b2));
1028 goto abort;
1029 }
1030 if (!sb_equal(refsb, sb)) {
1031 printk(KERN_WARNING "md: %s has same UUID"
1032 " but different superblock to %s\n",
1033 b, bdevname(refdev->bdev, b2));
1034 goto abort;
1035 }
1036 ev1 = md_event(sb);
1037 ev2 = md_event(refsb);
1038 if (ev1 > ev2)
1039 ret = 1;
1040 else
1041 ret = 0;
1042 }
NeilBrown8190e752009-06-18 08:48:58 +10001043 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001044 /* Limit to 4TB as metadata cannot record more than that.
1045 * (not needed for Linear and RAID0 as metadata doesn't
1046 * record this size)
1047 */
1048 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001049 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
NeilBrown27a7b262011-09-10 17:21:28 +10001051 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001052 /* "this cannot possibly happen" ... */
1053 ret = -EINVAL;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 abort:
1056 return ret;
1057}
1058
1059/*
1060 * validate_super for 0.90.0
1061 */
NeilBrownfd01b882011-10-11 16:47:53 +11001062static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001065 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001066 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
NeilBrown41158c72005-06-21 17:17:25 -07001068 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001069 clear_bit(Faulty, &rdev->flags);
1070 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001071 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001072 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (mddev->raid_disks == 0) {
1075 mddev->major_version = 0;
1076 mddev->minor_version = sb->minor_version;
1077 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001078 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001079 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mddev->ctime = sb->ctime;
1081 mddev->utime = sb->utime;
1082 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001083 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 mddev->layout = sb->layout;
1085 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001086 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001087 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001088 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001089 mddev->bitmap_info.space = 0;
1090 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001091 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001092 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001093 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
NeilBrownf6705572006-03-27 01:18:11 -08001095 if (mddev->minor_version >= 91) {
1096 mddev->reshape_position = sb->reshape_position;
1097 mddev->delta_disks = sb->delta_disks;
1098 mddev->new_level = sb->new_level;
1099 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001100 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001101 if (mddev->delta_disks < 0)
1102 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001103 } else {
1104 mddev->reshape_position = MaxSector;
1105 mddev->delta_disks = 0;
1106 mddev->new_level = mddev->level;
1107 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001108 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001109 }
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (sb->state & (1<<MD_SB_CLEAN))
1112 mddev->recovery_cp = MaxSector;
1113 else {
1114 if (sb->events_hi == sb->cp_events_hi &&
1115 sb->events_lo == sb->cp_events_lo) {
1116 mddev->recovery_cp = sb->recovery_cp;
1117 } else
1118 mddev->recovery_cp = 0;
1119 }
1120
1121 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1122 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1123 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1124 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1125
1126 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001127
1128 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001129 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001130 mddev->bitmap_info.offset =
1131 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001132 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001133 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001134 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001135
NeilBrown41158c72005-06-21 17:17:25 -07001136 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001137 /* Insist on good event counter while assembling, except
1138 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001140 if (sb->disks[rdev->desc_nr].state & (
1141 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1142 if (ev1 < mddev->events)
1143 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001144 } else if (mddev->bitmap) {
1145 /* if adding to array with a bitmap, then we can accept an
1146 * older device ... but not too old.
1147 */
NeilBrown41158c72005-06-21 17:17:25 -07001148 if (ev1 < mddev->bitmap->events_cleared)
1149 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001150 if (ev1 < mddev->events)
1151 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001152 } else {
1153 if (ev1 < mddev->events)
1154 /* just a hot-add of a new device, leave raid_disk at -1 */
1155 return 0;
1156 }
NeilBrown41158c72005-06-21 17:17:25 -07001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 desc = sb->disks + rdev->desc_nr;
1160
1161 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001162 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001163 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1164 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001167 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001168 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1169 /* active but not in sync implies recovery up to
1170 * reshape position. We don't know exactly where
1171 * that is, so set to zero for now */
1172 if (mddev->minor_version >= 91) {
1173 rdev->recovery_offset = 0;
1174 rdev->raid_disk = desc->raid_disk;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001177 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1178 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001179 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001180 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 0;
1182}
1183
1184/*
1185 * sync_super for 0.90.0
1186 */
NeilBrownfd01b882011-10-11 16:47:53 +11001187static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001190 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* make rdev->sb match mddev data..
1195 *
1196 * 1/ zero out disks
1197 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1198 * 3/ any empty disks < next_spare become removed
1199 *
1200 * disks[0] gets initialised to REMOVED because
1201 * we cannot be sure from other fields if it has
1202 * been initialised or not.
1203 */
1204 int i;
1205 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1206
NeilBrown61181562005-09-09 16:24:02 -07001207 rdev->sb_size = MD_SB_BYTES;
1208
Namhyung Kim65a06f062011-07-27 11:00:36 +10001209 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 memset(sb, 0, sizeof(*sb));
1212
1213 sb->md_magic = MD_SB_MAGIC;
1214 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sb->patch_version = mddev->patch_version;
1216 sb->gvalid_words = 0; /* ignored */
1217 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1218 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1219 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1220 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1221
1222 sb->ctime = mddev->ctime;
1223 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001224 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 sb->raid_disks = mddev->raid_disks;
1226 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001227 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 sb->utime = mddev->utime;
1229 sb->state = 0;
1230 sb->events_hi = (mddev->events>>32);
1231 sb->events_lo = (u32)mddev->events;
1232
NeilBrownf6705572006-03-27 01:18:11 -08001233 if (mddev->reshape_position == MaxSector)
1234 sb->minor_version = 90;
1235 else {
1236 sb->minor_version = 91;
1237 sb->reshape_position = mddev->reshape_position;
1238 sb->new_level = mddev->new_level;
1239 sb->delta_disks = mddev->delta_disks;
1240 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001241 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001242 }
1243 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (mddev->in_sync)
1245 {
1246 sb->recovery_cp = mddev->recovery_cp;
1247 sb->cp_events_hi = (mddev->events>>32);
1248 sb->cp_events_lo = (u32)mddev->events;
1249 if (mddev->recovery_cp == MaxSector)
1250 sb->state = (1<< MD_SB_CLEAN);
1251 } else
1252 sb->recovery_cp = 0;
1253
1254 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001255 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
NeilBrownc3d97142009-12-14 12:49:52 +11001257 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001258 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001261 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001263 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001264 int is_active = test_bit(In_sync, &rdev2->flags);
1265
1266 if (rdev2->raid_disk >= 0 &&
1267 sb->minor_version >= 91)
1268 /* we have nowhere to store the recovery_offset,
1269 * but if it is not below the reshape_position,
1270 * we can piggy-back on that.
1271 */
1272 is_active = 1;
1273 if (rdev2->raid_disk < 0 ||
1274 test_bit(Faulty, &rdev2->flags))
1275 is_active = 0;
1276 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001277 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001279 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001280 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 d = &sb->disks[rdev2->desc_nr];
1282 nr_disks++;
1283 d->number = rdev2->desc_nr;
1284 d->major = MAJOR(rdev2->bdev->bd_dev);
1285 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001286 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 d->raid_disk = rdev2->raid_disk;
1288 else
1289 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001290 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001292 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001294 if (test_bit(In_sync, &rdev2->flags))
1295 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 active++;
1297 working++;
1298 } else {
1299 d->state = 0;
1300 spare++;
1301 working++;
1302 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001303 if (test_bit(WriteMostly, &rdev2->flags))
1304 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* now set the "removed" and "faulty" bits on any missing devices */
1307 for (i=0 ; i < mddev->raid_disks ; i++) {
1308 mdp_disk_t *d = &sb->disks[i];
1309 if (d->state == 0 && d->number == 0) {
1310 d->number = i;
1311 d->raid_disk = i;
1312 d->state = (1<<MD_DISK_REMOVED);
1313 d->state |= (1<<MD_DISK_FAULTY);
1314 failed++;
1315 }
1316 }
1317 sb->nr_disks = nr_disks;
1318 sb->active_disks = active;
1319 sb->working_disks = working;
1320 sb->failed_disks = failed;
1321 sb->spare_disks = spare;
1322
1323 sb->this_disk = sb->disks[rdev->desc_nr];
1324 sb->sb_csum = calc_sb_csum(sb);
1325}
1326
1327/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328 * rdev_size_change for 0.90.0
1329 */
1330static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001331super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001332{
Andre Noll58c0fed2009-03-31 14:33:13 +11001333 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001335 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001336 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001337 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001338 if (!num_sectors || num_sectors > rdev->sb_start)
1339 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001340 /* Limit to 4TB as metadata cannot record more than that.
1341 * 4TB == 2^32 KB, or 2*2^32 sectors.
1342 */
NeilBrown667a5312012-08-16 16:46:12 +10001343 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001344 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001345 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346 rdev->sb_page);
1347 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001348 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001349}
1350
NeilBrownc6563a82012-05-21 09:27:00 +10001351static int
1352super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1353{
1354 /* non-zero offset changes not possible with v0.90 */
1355 return new_offset == 0;
1356}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001357
1358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * version 1 superblock
1360 */
1361
NeilBrown1c05b4b2006-10-21 10:24:08 -07001362static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001364 __le32 disk_csum;
1365 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 unsigned long long newcsum;
1367 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001368 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 disk_csum = sb->sb_csum;
1371 sb->sb_csum = 0;
1372 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001373 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 newcsum += le32_to_cpu(*isuper++);
1375
1376 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001377 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1380 sb->sb_csum = disk_csum;
1381 return cpu_to_le32(csum);
1382}
1383
NeilBrown2699b672011-07-28 11:31:47 +10001384static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1385 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001386static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct mdp_superblock_1 *sb;
1389 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001390 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001391 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001393 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /*
Andre Noll0f420352008-07-11 22:02:23 +10001396 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 * It is always aligned to a 4K boundary and
1398 * depeding on minor_version, it can be:
1399 * 0: At least 8K, but less than 12K, from end of device
1400 * 1: At start of device
1401 * 2: 4K from start of device.
1402 */
1403 switch(minor_version) {
1404 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001405 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001406 sb_start -= 8*2;
1407 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001410 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001413 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 default:
1416 return -EINVAL;
1417 }
Andre Noll0f420352008-07-11 22:02:23 +10001418 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
NeilBrown0002b272005-09-09 16:23:53 -07001420 /* superblock is rarely larger than 1K, but it can be larger,
1421 * and it is safe to read 4k, so we do that
1422 */
1423 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (ret) return ret;
1425
1426
Namhyung Kim65a06f062011-07-27 11:00:36 +10001427 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1430 sb->major_version != cpu_to_le32(1) ||
1431 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001432 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001433 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -EINVAL;
1435
1436 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1437 printk("md: invalid superblock checksum on %s\n",
1438 bdevname(rdev->bdev,b));
1439 return -EINVAL;
1440 }
1441 if (le64_to_cpu(sb->data_size) < 10) {
1442 printk("md: data_size too small on %s\n",
1443 bdevname(rdev->bdev,b));
1444 return -EINVAL;
1445 }
NeilBrownc6563a82012-05-21 09:27:00 +10001446 if (sb->pad0 ||
1447 sb->pad3[0] ||
1448 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1449 /* Some padding is non-zero, might be a new feature */
1450 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 rdev->preferred_minor = 0xffff;
1453 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001454 rdev->new_data_offset = rdev->data_offset;
1455 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1456 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1457 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001458 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
NeilBrown0002b272005-09-09 16:23:53 -07001460 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001461 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001462 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001463 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1464
1465 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001466 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001467 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001468 if (minor_version
1469 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1470 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001471
NeilBrown31b65a02006-07-10 04:44:14 -07001472 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1473 rdev->desc_nr = -1;
1474 else
1475 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1476
NeilBrown2699b672011-07-28 11:31:47 +10001477 if (!rdev->bb_page) {
1478 rdev->bb_page = alloc_page(GFP_KERNEL);
1479 if (!rdev->bb_page)
1480 return -ENOMEM;
1481 }
1482 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1483 rdev->badblocks.count == 0) {
1484 /* need to load the bad block list.
1485 * Currently we limit it to one page.
1486 */
1487 s32 offset;
1488 sector_t bb_sector;
1489 u64 *bbp;
1490 int i;
1491 int sectors = le16_to_cpu(sb->bblog_size);
1492 if (sectors > (PAGE_SIZE / 512))
1493 return -EINVAL;
1494 offset = le32_to_cpu(sb->bblog_offset);
1495 if (offset == 0)
1496 return -EINVAL;
1497 bb_sector = (long long)offset;
1498 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1499 rdev->bb_page, READ, true))
1500 return -EIO;
1501 bbp = (u64 *)page_address(rdev->bb_page);
1502 rdev->badblocks.shift = sb->bblog_shift;
1503 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1504 u64 bb = le64_to_cpu(*bbp);
1505 int count = bb & (0x3ff);
1506 u64 sector = bb >> 10;
1507 sector <<= sb->bblog_shift;
1508 count <<= sb->bblog_shift;
1509 if (bb + 1 == 0)
1510 break;
1511 if (md_set_badblocks(&rdev->badblocks,
1512 sector, count, 1) == 0)
1513 return -EINVAL;
1514 }
NeilBrown486adf72013-04-24 11:42:44 +10001515 } else if (sb->bblog_offset != 0)
1516 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001517
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001518 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001519 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001520 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001522 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1525 sb->level != refsb->level ||
1526 sb->layout != refsb->layout ||
1527 sb->chunksize != refsb->chunksize) {
1528 printk(KERN_WARNING "md: %s has strangely different"
1529 " superblock to %s\n",
1530 bdevname(rdev->bdev,b),
1531 bdevname(refdev->bdev,b2));
1532 return -EINVAL;
1533 }
1534 ev1 = le64_to_cpu(sb->events);
1535 ev2 = le64_to_cpu(refsb->events);
1536
1537 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001538 ret = 1;
1539 else
1540 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
NeilBrownc6563a82012-05-21 09:27:00 +10001542 if (minor_version) {
1543 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1544 sectors -= rdev->data_offset;
1545 } else
1546 sectors = rdev->sb_start;
1547 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001549 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
NeilBrownfd01b882011-10-11 16:47:53 +11001553static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001555 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001556 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
NeilBrown41158c72005-06-21 17:17:25 -07001558 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001559 clear_bit(Faulty, &rdev->flags);
1560 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001561 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001562 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (mddev->raid_disks == 0) {
1565 mddev->major_version = 1;
1566 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001567 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001568 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1570 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1571 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001572 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 mddev->layout = le32_to_cpu(sb->layout);
1574 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001575 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001576 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001577 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001578 mddev->bitmap_info.space = 0;
1579 /* Default location for bitmap is 1K after superblock
1580 * using 3K - total of 4K
1581 */
NeilBrownc3d97142009-12-14 12:49:52 +11001582 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001583 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001584 mddev->reshape_backwards = 0;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1587 memcpy(mddev->uuid, sb->set_uuid, 16);
1588
1589 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001590
NeilBrown71c08052005-09-09 16:23:51 -07001591 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001592 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001593 mddev->bitmap_info.offset =
1594 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001595 /* Metadata doesn't record how much space is available.
1596 * For 1.0, we assume we can use up to the superblock
1597 * if before, else to 4K beyond superblock.
1598 * For others, assume no change is possible.
1599 */
1600 if (mddev->minor_version > 0)
1601 mddev->bitmap_info.space = 0;
1602 else if (mddev->bitmap_info.offset > 0)
1603 mddev->bitmap_info.space =
1604 8 - mddev->bitmap_info.offset;
1605 else
1606 mddev->bitmap_info.space =
1607 -mddev->bitmap_info.offset;
1608 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001609
NeilBrownf6705572006-03-27 01:18:11 -08001610 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1611 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1612 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1613 mddev->new_level = le32_to_cpu(sb->new_level);
1614 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001615 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001616 if (mddev->delta_disks < 0 ||
1617 (mddev->delta_disks == 0 &&
1618 (le32_to_cpu(sb->feature_map)
1619 & MD_FEATURE_RESHAPE_BACKWARDS)))
1620 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001621 } else {
1622 mddev->reshape_position = MaxSector;
1623 mddev->delta_disks = 0;
1624 mddev->new_level = mddev->level;
1625 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001626 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001627 }
1628
NeilBrown41158c72005-06-21 17:17:25 -07001629 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001630 /* Insist of good event counter while assembling, except for
1631 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001633 if (rdev->desc_nr >= 0 &&
1634 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1635 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1636 if (ev1 < mddev->events)
1637 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001638 } else if (mddev->bitmap) {
1639 /* If adding to array with a bitmap, then we can accept an
1640 * older device, but not too old.
1641 */
NeilBrown41158c72005-06-21 17:17:25 -07001642 if (ev1 < mddev->bitmap->events_cleared)
1643 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001644 if (ev1 < mddev->events)
1645 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001646 } else {
1647 if (ev1 < mddev->events)
1648 /* just a hot-add of a new device, leave raid_disk at -1 */
1649 return 0;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (mddev->level != LEVEL_MULTIPATH) {
1652 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001653 if (rdev->desc_nr < 0 ||
1654 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1655 role = 0xffff;
1656 rdev->desc_nr = -1;
1657 } else
1658 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 switch(role) {
1660 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001663 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 default:
NeilBrownf4667222013-12-09 12:04:56 +11001666 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001667 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001668 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001669 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001670 if (!(le32_to_cpu(sb->feature_map) &
1671 MD_FEATURE_RECOVERY_BITMAP))
1672 rdev->saved_raid_disk = -1;
1673 } else
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001674 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 rdev->raid_disk = role;
1676 break;
1677 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001678 if (sb->devflags & WriteMostly1)
1679 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001680 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1681 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001682 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001683 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
NeilBrownfd01b882011-10-11 16:47:53 +11001688static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001691 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int max_dev, i;
1693 /* make rdev->sb match mddev and rdev data. */
1694
Namhyung Kim65a06f062011-07-27 11:00:36 +10001695 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 sb->feature_map = 0;
1698 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001699 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 memset(sb->pad3, 0, sizeof(sb->pad3));
1701
1702 sb->utime = cpu_to_le64((__u64)mddev->utime);
1703 sb->events = cpu_to_le64(mddev->events);
1704 if (mddev->in_sync)
1705 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1706 else
1707 sb->resync_offset = cpu_to_le64(0);
1708
NeilBrown1c05b4b2006-10-21 10:24:08 -07001709 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001710
NeilBrownf0ca3402006-02-02 14:28:04 -08001711 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001712 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001713 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001714 sb->level = cpu_to_le32(mddev->level);
1715 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001716
NeilBrownaeb9b2112011-08-25 14:43:08 +10001717 if (test_bit(WriteMostly, &rdev->flags))
1718 sb->devflags |= WriteMostly1;
1719 else
1720 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001721 sb->data_offset = cpu_to_le64(rdev->data_offset);
1722 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001723
NeilBrownc3d97142009-12-14 12:49:52 +11001724 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1725 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001726 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001727 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001728
1729 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001730 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001731 sb->feature_map |=
1732 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1733 sb->recovery_offset =
1734 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001735 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1736 sb->feature_map |=
1737 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001738 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001739 if (test_bit(Replacement, &rdev->flags))
1740 sb->feature_map |=
1741 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001742
NeilBrownf6705572006-03-27 01:18:11 -08001743 if (mddev->reshape_position != MaxSector) {
1744 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1745 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1746 sb->new_layout = cpu_to_le32(mddev->new_layout);
1747 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1748 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001749 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001750 if (mddev->delta_disks == 0 &&
1751 mddev->reshape_backwards)
1752 sb->feature_map
1753 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001754 if (rdev->new_data_offset != rdev->data_offset) {
1755 sb->feature_map
1756 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1757 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1758 - rdev->data_offset));
1759 }
NeilBrownf6705572006-03-27 01:18:11 -08001760 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001761
NeilBrown2699b672011-07-28 11:31:47 +10001762 if (rdev->badblocks.count == 0)
1763 /* Nothing to do for bad blocks*/ ;
1764 else if (sb->bblog_offset == 0)
1765 /* Cannot record bad blocks on this device */
1766 md_error(mddev, rdev);
1767 else {
1768 struct badblocks *bb = &rdev->badblocks;
1769 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1770 u64 *p = bb->page;
1771 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1772 if (bb->changed) {
1773 unsigned seq;
1774
1775retry:
1776 seq = read_seqbegin(&bb->lock);
1777
1778 memset(bbp, 0xff, PAGE_SIZE);
1779
1780 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001781 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001782 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1783 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001784 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001785 }
NeilBrownd0962932012-03-19 12:46:41 +11001786 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001787 if (read_seqretry(&bb->lock, seq))
1788 goto retry;
1789
1790 bb->sector = (rdev->sb_start +
1791 (int)le32_to_cpu(sb->bblog_offset));
1792 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001793 }
1794 }
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001797 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (rdev2->desc_nr+1 > max_dev)
1799 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001800
NeilBrown70471da2009-08-03 10:59:57 +10001801 if (max_dev > le32_to_cpu(sb->max_dev)) {
1802 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001803 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001804 rdev->sb_size = max_dev * 2 + 256;
1805 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1806 if (rdev->sb_size & bmask)
1807 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001808 } else
1809 max_dev = le32_to_cpu(sb->max_dev);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 for (i=0; i<max_dev;i++)
1812 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1813
NeilBrowndafb20f2012-03-19 12:46:39 +11001814 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001816 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001818 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001820 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001821 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 else
1823 sb->dev_roles[i] = cpu_to_le16(0xffff);
1824 }
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 sb->sb_csum = calc_sb_1_csum(sb);
1827}
1828
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001830super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001831{
1832 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001833 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001834 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001835 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001836 if (rdev->data_offset != rdev->new_data_offset)
1837 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001838 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001839 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001840 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001841 max_sectors -= rdev->data_offset;
1842 if (!num_sectors || num_sectors > max_sectors)
1843 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001844 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001845 /* minor version 0 with bitmap we can't move */
1846 return 0;
1847 } else {
1848 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001849 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001850 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001851 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001852 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001853 if (!num_sectors || num_sectors > max_sectors)
1854 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001855 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001857 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001858 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001859 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001860 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001861 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001862 rdev->sb_page);
1863 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001864 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001865
1866}
1867
1868static int
1869super_1_allow_new_offset(struct md_rdev *rdev,
1870 unsigned long long new_offset)
1871{
1872 /* All necessary checks on new >= old have been done */
1873 struct bitmap *bitmap;
1874 if (new_offset >= rdev->data_offset)
1875 return 1;
1876
1877 /* with 1.0 metadata, there is no metadata to tread on
1878 * so we can always move back */
1879 if (rdev->mddev->minor_version == 0)
1880 return 1;
1881
1882 /* otherwise we must be sure not to step on
1883 * any metadata, so stay:
1884 * 36K beyond start of superblock
1885 * beyond end of badblocks
1886 * beyond write-intent bitmap
1887 */
1888 if (rdev->sb_start + (32+4)*2 > new_offset)
1889 return 0;
1890 bitmap = rdev->mddev->bitmap;
1891 if (bitmap && !rdev->mddev->bitmap_info.file &&
1892 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001893 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001894 return 0;
1895 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1896 return 0;
1897
1898 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Adrian Bunk75c96f82005-05-05 16:16:09 -07001901static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 [0] = {
1903 .name = "0.90.0",
1904 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001905 .load_super = super_90_load,
1906 .validate_super = super_90_validate,
1907 .sync_super = super_90_sync,
1908 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001909 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 },
1911 [1] = {
1912 .name = "md-1",
1913 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001914 .load_super = super_1_load,
1915 .validate_super = super_1_validate,
1916 .sync_super = super_1_sync,
1917 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001918 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 },
1920};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
NeilBrownfd01b882011-10-11 16:47:53 +11001922static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001923{
1924 if (mddev->sync_super) {
1925 mddev->sync_super(mddev, rdev);
1926 return;
1927 }
1928
1929 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1930
1931 super_types[mddev->major_version].sync_super(mddev, rdev);
1932}
1933
NeilBrownfd01b882011-10-11 16:47:53 +11001934static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
NeilBrown3cb03002011-10-11 16:45:26 +11001936 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
NeilBrown4b809912008-07-21 17:05:25 +10001938 rcu_read_lock();
1939 rdev_for_each_rcu(rdev, mddev1)
1940 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001941 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001942 rdev2->bdev->bd_contains) {
1943 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001944 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001945 }
1946 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return 0;
1948}
1949
1950static LIST_HEAD(pending_raid_disks);
1951
Andre Nollac5e7112009-08-03 10:59:47 +10001952/*
1953 * Try to register data integrity profile for an mddev
1954 *
1955 * This is called when an array is started and after a disk has been kicked
1956 * from the array. It only succeeds if all working and active component devices
1957 * are integrity capable with matching profiles.
1958 */
NeilBrownfd01b882011-10-11 16:47:53 +11001959int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001960{
NeilBrown3cb03002011-10-11 16:45:26 +11001961 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001962
Andre Nollac5e7112009-08-03 10:59:47 +10001963 if (list_empty(&mddev->disks))
1964 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001965 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1966 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001967 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001968 /* skip spares and non-functional disks */
1969 if (test_bit(Faulty, &rdev->flags))
1970 continue;
1971 if (rdev->raid_disk < 0)
1972 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001973 if (!reference) {
1974 /* Use the first rdev as the reference */
1975 reference = rdev;
1976 continue;
1977 }
1978 /* does this rdev's profile match the reference profile? */
1979 if (blk_integrity_compare(reference->bdev->bd_disk,
1980 rdev->bdev->bd_disk) < 0)
1981 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001982 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001983 if (!reference || !bdev_get_integrity(reference->bdev))
1984 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001985 /*
1986 * All component devices are integrity capable and have matching
1987 * profiles, register the common profile for the md device.
1988 */
1989 if (blk_integrity_register(mddev->gendisk,
1990 bdev_get_integrity(reference->bdev)) != 0) {
1991 printk(KERN_ERR "md: failed to register integrity for %s\n",
1992 mdname(mddev));
1993 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001994 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001995 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1996 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1997 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1998 mdname(mddev));
1999 return -EINVAL;
2000 }
Andre Nollac5e7112009-08-03 10:59:47 +10002001 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002002}
Andre Nollac5e7112009-08-03 10:59:47 +10002003EXPORT_SYMBOL(md_integrity_register);
2004
2005/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002006void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002007{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002008 struct blk_integrity *bi_rdev;
2009 struct blk_integrity *bi_mddev;
2010
2011 if (!mddev->gendisk)
2012 return;
2013
2014 bi_rdev = bdev_get_integrity(rdev->bdev);
2015 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002016
2017 if (!bi_mddev) /* nothing to do */
2018 return;
2019 if (rdev->raid_disk < 0) /* skip spares */
2020 return;
2021 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2022 rdev->bdev->bd_disk) >= 0)
2023 return;
2024 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2025 blk_integrity_unregister(mddev->gendisk);
2026}
2027EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002028
NeilBrownfd01b882011-10-11 16:47:53 +11002029static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002031 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002032 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002033 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002034 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 if (rdev->mddev) {
2037 MD_BUG();
2038 return -EINVAL;
2039 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002040
2041 /* prevent duplicates */
2042 if (find_rdev(mddev, rdev->bdev->bd_dev))
2043 return -EEXIST;
2044
Andre Nolldd8ac332009-03-31 14:33:13 +11002045 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2046 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2047 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002048 if (mddev->pers) {
2049 /* Cannot change size, so fail
2050 * If mddev->level <= 0, then we don't care
2051 * about aligning sizes (e.g. linear)
2052 */
2053 if (mddev->level > 0)
2054 return -ENOSPC;
2055 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002056 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /* Verify rdev->desc_nr is unique.
2060 * If it is -1, assign a free number, else
2061 * check number is not in use
2062 */
2063 if (rdev->desc_nr < 0) {
2064 int choice = 0;
2065 if (mddev->pers) choice = mddev->raid_disks;
2066 while (find_rdev_nr(mddev, choice))
2067 choice++;
2068 rdev->desc_nr = choice;
2069 } else {
2070 if (find_rdev_nr(mddev, rdev->desc_nr))
2071 return -EBUSY;
2072 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002073 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2074 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2075 mdname(mddev), mddev->max_disks);
2076 return -EBUSY;
2077 }
NeilBrown19133a42005-11-08 21:39:35 -08002078 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002079 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002080 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002083 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002084
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002085 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002086 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002087
Tejun Heo0762b8b2008-08-25 19:56:12 +09002088 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002089 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2090 /* failure here is OK */;
2091 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002092
NeilBrown4b809912008-07-21 17:05:25 +10002093 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002094 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002095
2096 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002097 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100
2101 fail:
2102 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2103 b, mdname(mddev));
2104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
NeilBrown177a99b2008-02-06 01:39:56 -08002107static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002108{
NeilBrown3cb03002011-10-11 16:45:26 +11002109 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002110 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002111 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002112}
2113
NeilBrown3cb03002011-10-11 16:45:26 +11002114static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 char b[BDEVNAME_SIZE];
2117 if (!rdev->mddev) {
2118 MD_BUG();
2119 return;
2120 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002121 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002122 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2124 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002125 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002126 sysfs_put(rdev->sysfs_state);
2127 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002128 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002129 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002130 * writing to 'remove' to "dev/state". We also need
2131 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002132 */
NeilBrown4b809912008-07-21 17:05:25 +10002133 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002134 INIT_WORK(&rdev->del_work, md_delayed_delete);
2135 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002136 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
2139/*
2140 * prevent the device from being mounted, repartitioned or
2141 * otherwise reused by a RAID array (or any other kernel
2142 * subsystem), by bd_claiming the device.
2143 */
NeilBrown3cb03002011-10-11 16:45:26 +11002144static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int err = 0;
2147 struct block_device *bdev;
2148 char b[BDEVNAME_SIZE];
2149
Tejun Heod4d77622010-11-13 11:55:18 +01002150 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002151 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (IS_ERR(bdev)) {
2153 printk(KERN_ERR "md: could not open %s.\n",
2154 __bdevname(dev, b));
2155 return PTR_ERR(bdev);
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 rdev->bdev = bdev;
2158 return err;
2159}
2160
NeilBrown3cb03002011-10-11 16:45:26 +11002161static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 struct block_device *bdev = rdev->bdev;
2164 rdev->bdev = NULL;
2165 if (!bdev)
2166 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002167 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
2170void md_autodetect_dev(dev_t dev);
2171
NeilBrown3cb03002011-10-11 16:45:26 +11002172static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 char b[BDEVNAME_SIZE];
2175 printk(KERN_INFO "md: export_rdev(%s)\n",
2176 bdevname(rdev->bdev,b));
2177 if (rdev->mddev)
2178 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002179 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002181 if (test_bit(AutoDetected, &rdev->flags))
2182 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183#endif
2184 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002185 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
NeilBrown3cb03002011-10-11 16:45:26 +11002188static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 unbind_rdev_from_array(rdev);
2191 export_rdev(rdev);
2192}
2193
NeilBrownfd01b882011-10-11 16:47:53 +11002194static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
NeilBrown3cb03002011-10-11 16:45:26 +11002196 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrowndafb20f2012-03-19 12:46:39 +11002198 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!rdev->mddev) {
2200 MD_BUG();
2201 continue;
2202 }
2203 kick_rdev_from_array(rdev);
2204 }
2205 if (!list_empty(&mddev->disks))
2206 MD_BUG();
2207 mddev->raid_disks = 0;
2208 mddev->major_version = 0;
2209}
2210
2211static void print_desc(mdp_disk_t *desc)
2212{
2213 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2214 desc->major,desc->minor,desc->raid_disk,desc->state);
2215}
2216
Cheng Renquancd2ac932009-01-09 08:31:08 +11002217static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 int i;
2220
2221 printk(KERN_INFO
2222 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2223 sb->major_version, sb->minor_version, sb->patch_version,
2224 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2225 sb->ctime);
2226 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2227 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2228 sb->md_minor, sb->layout, sb->chunk_size);
2229 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2230 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2231 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2232 sb->failed_disks, sb->spare_disks,
2233 sb->sb_csum, (unsigned long)sb->events_lo);
2234
2235 printk(KERN_INFO);
2236 for (i = 0; i < MD_SB_DISKS; i++) {
2237 mdp_disk_t *desc;
2238
2239 desc = sb->disks + i;
2240 if (desc->number || desc->major || desc->minor ||
2241 desc->raid_disk || (desc->state && (desc->state != 4))) {
2242 printk(" D %2d: ", i);
2243 print_desc(desc);
2244 }
2245 }
2246 printk(KERN_INFO "md: THIS: ");
2247 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Cheng Renquancd2ac932009-01-09 08:31:08 +11002250static void print_sb_1(struct mdp_superblock_1 *sb)
2251{
2252 __u8 *uuid;
2253
2254 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002255 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002256 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002257 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002258 le32_to_cpu(sb->major_version),
2259 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002260 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002261 sb->set_name,
2262 (unsigned long long)le64_to_cpu(sb->ctime)
2263 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2264
2265 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002266 printk(KERN_INFO
2267 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002268 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002269 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002270 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2271 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002272 le32_to_cpu(sb->level),
2273 (unsigned long long)le64_to_cpu(sb->size),
2274 le32_to_cpu(sb->raid_disks),
2275 le32_to_cpu(sb->layout),
2276 le32_to_cpu(sb->chunksize),
2277 (unsigned long long)le64_to_cpu(sb->data_offset),
2278 (unsigned long long)le64_to_cpu(sb->data_size),
2279 (unsigned long long)le64_to_cpu(sb->super_offset),
2280 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2281 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002282 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002283 sb->devflags,
2284 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2285 (unsigned long long)le64_to_cpu(sb->events),
2286 (unsigned long long)le64_to_cpu(sb->resync_offset),
2287 le32_to_cpu(sb->sb_csum),
2288 le32_to_cpu(sb->max_dev)
2289 );
2290}
2291
NeilBrown3cb03002011-10-11 16:45:26 +11002292static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002295 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2296 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002297 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2298 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002300 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2301 switch (major_version) {
2302 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002303 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002304 break;
2305 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002306 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002307 break;
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 } else
2310 printk(KERN_INFO "md: no rdev superblock!\n");
2311}
2312
Adrian Bunk5e563412006-06-26 00:27:42 -07002313static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002315 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002316 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002317 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 char b[BDEVNAME_SIZE];
2319
2320 printk("\n");
2321 printk("md: **********************************\n");
2322 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2323 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002324 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
NeilBrown32a76272005-06-21 17:17:14 -07002326 if (mddev->bitmap)
2327 bitmap_print_sb(mddev->bitmap);
2328 else
2329 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002330 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 printk("<%s>", bdevname(rdev->bdev,b));
2332 printk("\n");
2333
NeilBrowndafb20f2012-03-19 12:46:39 +11002334 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002335 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337 printk("md: **********************************\n");
2338 printk("\n");
2339}
2340
2341
NeilBrownfd01b882011-10-11 16:47:53 +11002342static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
NeilBrown42543762006-06-26 00:27:57 -07002344 /* Update each superblock (in-memory image), but
2345 * if we are allowed to, skip spares which already
2346 * have the right event counter, or have one earlier
2347 * (which would mean they aren't being marked as dirty
2348 * with the rest of the array)
2349 */
NeilBrown3cb03002011-10-11 16:45:26 +11002350 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002351 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002352 if (rdev->sb_events == mddev->events ||
2353 (nospares &&
2354 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002355 rdev->sb_events+1 == mddev->events)) {
2356 /* Don't update this superblock */
2357 rdev->sb_loaded = 2;
2358 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002359 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002360 rdev->sb_loaded = 1;
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363}
2364
NeilBrownfd01b882011-10-11 16:47:53 +11002365static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
NeilBrown3cb03002011-10-11 16:45:26 +11002367 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002368 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002369 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002370 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
NeilBrownd87f0642013-04-24 11:42:40 +10002372 if (mddev->ro) {
2373 if (force_change)
2374 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2375 return;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002378 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002379 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002380 if (rdev->raid_disk >= 0 &&
2381 mddev->delta_disks >= 0 &&
2382 !test_bit(In_sync, &rdev->flags) &&
2383 mddev->curr_resync_completed > rdev->recovery_offset)
2384 rdev->recovery_offset = mddev->curr_resync_completed;
2385
2386 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002387 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002388 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002389 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002390 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002391 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002392 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002393 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002394 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002395 md_ack_all_badblocks(&rdev->badblocks);
2396 md_error(mddev, rdev);
2397 }
2398 clear_bit(Blocked, &rdev->flags);
2399 clear_bit(BlockedBadBlocks, &rdev->flags);
2400 wake_up(&rdev->blocked_wait);
2401 }
2402 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002403 wake_up(&mddev->sb_wait);
2404 return;
2405 }
2406
NeilBrowna9701a32005-11-08 21:39:34 -08002407 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002408
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002409 mddev->utime = get_seconds();
2410
NeilBrown850b2b42006-10-03 01:15:46 -07002411 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2412 force_change = 1;
2413 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2414 /* just a clean<-> dirty transition, possibly leave spares alone,
2415 * though if events isn't the right even/odd, we will have to do
2416 * spares after all
2417 */
2418 nospares = 1;
2419 if (force_change)
2420 nospares = 0;
2421 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002422 /* If the array is degraded, then skipping spares is both
2423 * dangerous and fairly pointless.
2424 * Dangerous because a device that was removed from the array
2425 * might have a event_count that still looks up-to-date,
2426 * so it can be re-added without a resync.
2427 * Pointless because if there are any spares to skip,
2428 * then a recovery will happen and soon that array won't
2429 * be degraded any more and the spare can go back to sleep then.
2430 */
NeilBrown850b2b42006-10-03 01:15:46 -07002431 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002432
NeilBrown06d91a52005-06-21 17:17:12 -07002433 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002434
2435 /* If this is just a dirty<->clean transition, and the array is clean
2436 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002437 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002438 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002439 && mddev->can_decrease_events
2440 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002441 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002442 mddev->can_decrease_events = 0;
2443 } else {
NeilBrown42543762006-06-26 00:27:57 -07002444 /* otherwise we have to go forward and ... */
2445 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002446 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 if (!mddev->events) {
2450 /*
2451 * oops, this 64-bit counter should never wrap.
2452 * Either we are in around ~1 trillion A.C., assuming
2453 * 1 reboot per second, or we have a bug:
2454 */
2455 MD_BUG();
2456 mddev->events --;
2457 }
NeilBrown2699b672011-07-28 11:31:47 +10002458
NeilBrowndafb20f2012-03-19 12:46:39 +11002459 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002460 if (rdev->badblocks.changed)
2461 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002462 if (test_bit(Faulty, &rdev->flags))
2463 set_bit(FaultRecorded, &rdev->flags);
2464 }
NeilBrown2699b672011-07-28 11:31:47 +10002465
NeilBrowne6910632008-02-06 01:39:51 -08002466 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002467 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
NeilBrown36a4e1f2011-10-07 14:23:17 +11002469 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2470 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
NeilBrown4ad13662007-07-17 04:06:13 -07002472 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002473 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002475
NeilBrown42543762006-06-26 00:27:57 -07002476 if (rdev->sb_loaded != 1)
2477 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
NeilBrownf4667222013-12-09 12:04:56 +11002479 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002480 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002481 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002482 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002483 pr_debug("md: (write) %s's sb offset: %llu\n",
2484 bdevname(rdev->bdev, b),
2485 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002486 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002487 if (rdev->badblocks.size) {
2488 md_super_write(mddev, rdev,
2489 rdev->badblocks.sector,
2490 rdev->badblocks.size << 9,
2491 rdev->bb_page);
2492 rdev->badblocks.size = 0;
2493 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002494
NeilBrownf4667222013-12-09 12:04:56 +11002495 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002496 pr_debug("md: %s (skipping faulty)\n",
2497 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002498
NeilBrown7bfa19f2005-06-21 17:17:28 -07002499 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* only need to write one superblock... */
2501 break;
2502 }
NeilBrowna9701a32005-11-08 21:39:34 -08002503 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002504 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002505
NeilBrowna9701a32005-11-08 21:39:34 -08002506 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002507 if (mddev->in_sync != sync_req ||
2508 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002509 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002510 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002511 goto repeat;
2512 }
NeilBrown850b2b42006-10-03 01:15:46 -07002513 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002514 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002515 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002516 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2517 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002518
NeilBrowndafb20f2012-03-19 12:46:39 +11002519 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002520 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2521 clear_bit(Blocked, &rdev->flags);
2522
2523 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002524 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002525 clear_bit(BlockedBadBlocks, &rdev->flags);
2526 wake_up(&rdev->blocked_wait);
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Andre Noll7f6ce762008-03-23 18:34:54 +01002530/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002531 * We want to accept with case. For this we use cmd_match.
2532 */
2533static int cmd_match(const char *cmd, const char *str)
2534{
2535 /* See if cmd, written into a sysfs file, matches
2536 * str. They must either be the same, or cmd can
2537 * have a trailing newline
2538 */
2539 while (*cmd && *str && *cmd == *str) {
2540 cmd++;
2541 str++;
2542 }
2543 if (*cmd == '\n')
2544 cmd++;
2545 if (*str || *cmd)
2546 return 0;
2547 return 1;
2548}
2549
NeilBrown86e6ffd2005-11-08 21:39:24 -08002550struct rdev_sysfs_entry {
2551 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002552 ssize_t (*show)(struct md_rdev *, char *);
2553 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002554};
2555
2556static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002557state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002558{
2559 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002560 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561
NeilBrownde393cd2011-07-28 11:31:48 +10002562 if (test_bit(Faulty, &rdev->flags) ||
2563 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564 len+= sprintf(page+len, "%sfaulty",sep);
2565 sep = ",";
2566 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002567 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002568 len += sprintf(page+len, "%sin_sync",sep);
2569 sep = ",";
2570 }
NeilBrownf6556752006-06-26 00:28:01 -07002571 if (test_bit(WriteMostly, &rdev->flags)) {
2572 len += sprintf(page+len, "%swrite_mostly",sep);
2573 sep = ",";
2574 }
NeilBrownde393cd2011-07-28 11:31:48 +10002575 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002576 (rdev->badblocks.unacked_exist
2577 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002578 len += sprintf(page+len, "%sblocked", sep);
2579 sep = ",";
2580 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002581 if (!test_bit(Faulty, &rdev->flags) &&
2582 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002583 len += sprintf(page+len, "%sspare", sep);
2584 sep = ",";
2585 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002586 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2587 len += sprintf(page+len, "%swrite_error", sep);
2588 sep = ",";
2589 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002590 if (test_bit(WantReplacement, &rdev->flags)) {
2591 len += sprintf(page+len, "%swant_replacement", sep);
2592 sep = ",";
2593 }
2594 if (test_bit(Replacement, &rdev->flags)) {
2595 len += sprintf(page+len, "%sreplacement", sep);
2596 sep = ",";
2597 }
2598
NeilBrown86e6ffd2005-11-08 21:39:24 -08002599 return len+sprintf(page+len, "\n");
2600}
2601
NeilBrown45dc2de2006-06-26 00:27:58 -07002602static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002603state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002604{
2605 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002606 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002607 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002608 * writemostly - sets write_mostly
2609 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002610 * blocked - sets the Blocked flags
2611 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002612 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002613 * -insync - clear Insync for a device with a slot assigned,
2614 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002615 * write_error - sets WriteErrorSeen
2616 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002617 */
2618 int err = -EINVAL;
2619 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2620 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002621 if (test_bit(Faulty, &rdev->flags))
2622 err = 0;
2623 else
2624 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002625 } else if (cmd_match(buf, "remove")) {
2626 if (rdev->raid_disk >= 0)
2627 err = -EBUSY;
2628 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002629 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002630 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002631 if (mddev->pers)
2632 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002633 md_new_event(mddev);
2634 err = 0;
2635 }
NeilBrownf6556752006-06-26 00:28:01 -07002636 } else if (cmd_match(buf, "writemostly")) {
2637 set_bit(WriteMostly, &rdev->flags);
2638 err = 0;
2639 } else if (cmd_match(buf, "-writemostly")) {
2640 clear_bit(WriteMostly, &rdev->flags);
2641 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002642 } else if (cmd_match(buf, "blocked")) {
2643 set_bit(Blocked, &rdev->flags);
2644 err = 0;
2645 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002646 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002647 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002648 /* metadata handler doesn't understand badblocks,
2649 * so we need to fail the device
2650 */
2651 md_error(rdev->mddev, rdev);
2652 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002653 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002654 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002655 wake_up(&rdev->blocked_wait);
2656 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2657 md_wakeup_thread(rdev->mddev->thread);
2658
2659 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002660 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2661 set_bit(In_sync, &rdev->flags);
2662 err = 0;
NeilBrownf4667222013-12-09 12:04:56 +11002663 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) {
2664 clear_bit(In_sync, &rdev->flags);
2665 rdev->saved_raid_disk = rdev->raid_disk;
2666 rdev->raid_disk = -1;
2667 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002668 } else if (cmd_match(buf, "write_error")) {
2669 set_bit(WriteErrorSeen, &rdev->flags);
2670 err = 0;
2671 } else if (cmd_match(buf, "-write_error")) {
2672 clear_bit(WriteErrorSeen, &rdev->flags);
2673 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002674 } else if (cmd_match(buf, "want_replacement")) {
2675 /* Any non-spare device that is not a replacement can
2676 * become want_replacement at any time, but we then need to
2677 * check if recovery is needed.
2678 */
2679 if (rdev->raid_disk >= 0 &&
2680 !test_bit(Replacement, &rdev->flags))
2681 set_bit(WantReplacement, &rdev->flags);
2682 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2683 md_wakeup_thread(rdev->mddev->thread);
2684 err = 0;
2685 } else if (cmd_match(buf, "-want_replacement")) {
2686 /* Clearing 'want_replacement' is always allowed.
2687 * Once replacements starts it is too late though.
2688 */
2689 err = 0;
2690 clear_bit(WantReplacement, &rdev->flags);
2691 } else if (cmd_match(buf, "replacement")) {
2692 /* Can only set a device as a replacement when array has not
2693 * yet been started. Once running, replacement is automatic
2694 * from spares, or by assigning 'slot'.
2695 */
2696 if (rdev->mddev->pers)
2697 err = -EBUSY;
2698 else {
2699 set_bit(Replacement, &rdev->flags);
2700 err = 0;
2701 }
2702 } else if (cmd_match(buf, "-replacement")) {
2703 /* Similarly, can only clear Replacement before start */
2704 if (rdev->mddev->pers)
2705 err = -EBUSY;
2706 else {
2707 clear_bit(Replacement, &rdev->flags);
2708 err = 0;
2709 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002710 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002711 if (!err)
2712 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002713 return err ? err : len;
2714}
NeilBrown80ca3a42006-07-10 04:44:18 -07002715static struct rdev_sysfs_entry rdev_state =
2716__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002717
2718static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002719errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002720{
2721 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2722}
2723
2724static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002725errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002726{
2727 char *e;
2728 unsigned long n = simple_strtoul(buf, &e, 10);
2729 if (*buf && (*e == 0 || *e == '\n')) {
2730 atomic_set(&rdev->corrected_errors, n);
2731 return len;
2732 }
2733 return -EINVAL;
2734}
2735static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002736__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002737
NeilBrown014236d2006-01-06 00:20:55 -08002738static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002739slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002740{
2741 if (rdev->raid_disk < 0)
2742 return sprintf(page, "none\n");
2743 else
2744 return sprintf(page, "%d\n", rdev->raid_disk);
2745}
2746
2747static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002748slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002749{
2750 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002751 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002752 int slot = simple_strtoul(buf, &e, 10);
2753 if (strncmp(buf, "none", 4)==0)
2754 slot = -1;
2755 else if (e==buf || (*e && *e!= '\n'))
2756 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002757 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002758 /* Setting 'slot' on an active array requires also
2759 * updating the 'rd%d' link, and communicating
2760 * with the personality with ->hot_*_disk.
2761 * For now we only support removing
2762 * failed/spare devices. This normally happens automatically,
2763 * but not when the metadata is externally managed.
2764 */
NeilBrownc303da62008-02-06 01:39:51 -08002765 if (rdev->raid_disk == -1)
2766 return -EEXIST;
2767 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002768 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002769 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10002770 clear_bit(Blocked, &rdev->flags);
2771 remove_and_add_spares(rdev->mddev, rdev);
2772 if (rdev->raid_disk >= 0)
2773 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08002774 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2775 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002777 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002778 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002779 */
2780
2781 if (rdev->raid_disk != -1)
2782 return -EBUSY;
2783
NeilBrownc6751b22011-02-02 11:57:13 +11002784 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2785 return -EBUSY;
2786
Neil Brown6c2fce22008-06-28 08:31:31 +10002787 if (rdev->mddev->pers->hot_add_disk == NULL)
2788 return -EINVAL;
2789
NeilBrownba1b41b2011-01-14 09:14:34 +11002790 if (slot >= rdev->mddev->raid_disks &&
2791 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2792 return -ENOSPC;
2793
Neil Brown6c2fce22008-06-28 08:31:31 +10002794 rdev->raid_disk = slot;
2795 if (test_bit(In_sync, &rdev->flags))
2796 rdev->saved_raid_disk = slot;
2797 else
2798 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002799 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11002800 clear_bit(Bitmap_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002801 err = rdev->mddev->pers->
2802 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002803 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002804 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002805 return err;
Neil Brown52664732008-06-28 08:31:44 +10002806 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002807 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002808 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002809 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002810 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002811 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002812 if (slot >= rdev->mddev->raid_disks &&
2813 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002814 return -ENOSPC;
2815 rdev->raid_disk = slot;
2816 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002817 clear_bit(Faulty, &rdev->flags);
2818 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002819 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002820 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002821 }
NeilBrown014236d2006-01-06 00:20:55 -08002822 return len;
2823}
2824
2825
2826static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002827__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002828
NeilBrown93c8cad2006-01-06 00:20:56 -08002829static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002830offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002831{
Andrew Morton6961ece2006-01-06 00:20:59 -08002832 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002833}
2834
2835static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002836offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002837{
NeilBrownc6563a82012-05-21 09:27:00 +10002838 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002839 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002840 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002841 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002842 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002843 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002844 /* Must set offset before size, so overlap checks
2845 * can be sane */
2846 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002847 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002848 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002849 return len;
2850}
2851
2852static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002853__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002854
NeilBrownc6563a82012-05-21 09:27:00 +10002855static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2856{
2857 return sprintf(page, "%llu\n",
2858 (unsigned long long)rdev->new_data_offset);
2859}
2860
2861static ssize_t new_offset_store(struct md_rdev *rdev,
2862 const char *buf, size_t len)
2863{
2864 unsigned long long new_offset;
2865 struct mddev *mddev = rdev->mddev;
2866
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002867 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10002868 return -EINVAL;
2869
2870 if (mddev->sync_thread)
2871 return -EBUSY;
2872 if (new_offset == rdev->data_offset)
2873 /* reset is always permitted */
2874 ;
2875 else if (new_offset > rdev->data_offset) {
2876 /* must not push array size beyond rdev_sectors */
2877 if (new_offset - rdev->data_offset
2878 + mddev->dev_sectors > rdev->sectors)
2879 return -E2BIG;
2880 }
2881 /* Metadata worries about other space details. */
2882
2883 /* decreasing the offset is inconsistent with a backwards
2884 * reshape.
2885 */
2886 if (new_offset < rdev->data_offset &&
2887 mddev->reshape_backwards)
2888 return -EINVAL;
2889 /* Increasing offset is inconsistent with forwards
2890 * reshape. reshape_direction should be set to
2891 * 'backwards' first.
2892 */
2893 if (new_offset > rdev->data_offset &&
2894 !mddev->reshape_backwards)
2895 return -EINVAL;
2896
2897 if (mddev->pers && mddev->persistent &&
2898 !super_types[mddev->major_version]
2899 .allow_new_offset(rdev, new_offset))
2900 return -E2BIG;
2901 rdev->new_data_offset = new_offset;
2902 if (new_offset > rdev->data_offset)
2903 mddev->reshape_backwards = 1;
2904 else if (new_offset < rdev->data_offset)
2905 mddev->reshape_backwards = 0;
2906
2907 return len;
2908}
2909static struct rdev_sysfs_entry rdev_new_offset =
2910__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2911
NeilBrown83303b62006-01-06 00:21:06 -08002912static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002913rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002914{
Andre Nolldd8ac332009-03-31 14:33:13 +11002915 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002916}
2917
NeilBrownc5d79ad2008-02-06 01:39:54 -08002918static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2919{
2920 /* check if two start/length pairs overlap */
2921 if (s1+l1 <= s2)
2922 return 0;
2923 if (s2+l2 <= s1)
2924 return 0;
2925 return 1;
2926}
2927
Dan Williamsb522adc2009-03-31 15:00:31 +11002928static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2929{
2930 unsigned long long blocks;
2931 sector_t new;
2932
Jingoo Hanb29bebd2013-06-01 16:15:16 +09002933 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11002934 return -EINVAL;
2935
2936 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2937 return -EINVAL; /* sector conversion overflow */
2938
2939 new = blocks * 2;
2940 if (new != blocks * 2)
2941 return -EINVAL; /* unsigned long long to sector_t overflow */
2942
2943 *sectors = new;
2944 return 0;
2945}
2946
NeilBrown83303b62006-01-06 00:21:06 -08002947static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002948rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002949{
NeilBrownfd01b882011-10-11 16:47:53 +11002950 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002951 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002952 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002953
Dan Williamsb522adc2009-03-31 15:00:31 +11002954 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002955 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002956 if (rdev->data_offset != rdev->new_data_offset)
2957 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002958 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002959 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002960 sectors = super_types[my_mddev->major_version].
2961 rdev_size_change(rdev, sectors);
2962 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002963 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002964 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002965 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002966 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11002967 if (!my_mddev->pers->resize)
2968 /* Cannot change size for RAID0 or Linear etc */
2969 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002970 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002971 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002972 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002973
Andre Nolldd8ac332009-03-31 14:33:13 +11002974 rdev->sectors = sectors;
2975 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002976 /* need to check that all other rdevs with the same ->bdev
2977 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002978 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002979 * we have to change it back, we will have the lock again.
2980 */
NeilBrownfd01b882011-10-11 16:47:53 +11002981 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002982 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002983 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002984
NeilBrown27c529b2008-03-04 14:29:33 -08002985 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002986 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002987 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002988
NeilBrown29f097c2013-11-14 17:54:51 +11002989 mddev_lock_nointr(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002990 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002991 if (rdev->bdev == rdev2->bdev &&
2992 rdev != rdev2 &&
2993 overlaps(rdev->data_offset, rdev->sectors,
2994 rdev2->data_offset,
2995 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002996 overlap = 1;
2997 break;
2998 }
2999 mddev_unlock(mddev);
3000 if (overlap) {
3001 mddev_put(mddev);
3002 break;
3003 }
3004 }
NeilBrown29f097c2013-11-14 17:54:51 +11003005 mddev_lock_nointr(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003006 if (overlap) {
3007 /* Someone else could have slipped in a size
3008 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003009 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 * safe, and trust userspace not to race with
3011 * itself
3012 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003013 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003014 return -EBUSY;
3015 }
3016 }
NeilBrown83303b62006-01-06 00:21:06 -08003017 return len;
3018}
3019
3020static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003021__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003022
Dan Williams06e3c812009-12-12 21:17:12 -07003023
NeilBrown3cb03002011-10-11 16:45:26 +11003024static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003025{
3026 unsigned long long recovery_start = rdev->recovery_offset;
3027
3028 if (test_bit(In_sync, &rdev->flags) ||
3029 recovery_start == MaxSector)
3030 return sprintf(page, "none\n");
3031
3032 return sprintf(page, "%llu\n", recovery_start);
3033}
3034
NeilBrown3cb03002011-10-11 16:45:26 +11003035static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003036{
3037 unsigned long long recovery_start;
3038
3039 if (cmd_match(buf, "none"))
3040 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003041 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003042 return -EINVAL;
3043
3044 if (rdev->mddev->pers &&
3045 rdev->raid_disk >= 0)
3046 return -EBUSY;
3047
3048 rdev->recovery_offset = recovery_start;
3049 if (recovery_start == MaxSector)
3050 set_bit(In_sync, &rdev->flags);
3051 else
3052 clear_bit(In_sync, &rdev->flags);
3053 return len;
3054}
3055
3056static struct rdev_sysfs_entry rdev_recovery_start =
3057__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3058
NeilBrown16c791a2011-07-28 11:31:47 +10003059
3060static ssize_t
3061badblocks_show(struct badblocks *bb, char *page, int unack);
3062static ssize_t
3063badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3064
NeilBrown3cb03002011-10-11 16:45:26 +11003065static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003066{
3067 return badblocks_show(&rdev->badblocks, page, 0);
3068}
NeilBrown3cb03002011-10-11 16:45:26 +11003069static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003070{
NeilBrownde393cd2011-07-28 11:31:48 +10003071 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3072 /* Maybe that ack was all we needed */
3073 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3074 wake_up(&rdev->blocked_wait);
3075 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003076}
3077static struct rdev_sysfs_entry rdev_bad_blocks =
3078__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3079
3080
NeilBrown3cb03002011-10-11 16:45:26 +11003081static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003082{
3083 return badblocks_show(&rdev->badblocks, page, 1);
3084}
NeilBrown3cb03002011-10-11 16:45:26 +11003085static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003086{
3087 return badblocks_store(&rdev->badblocks, page, len, 1);
3088}
3089static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3090__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3091
NeilBrown86e6ffd2005-11-08 21:39:24 -08003092static struct attribute *rdev_default_attrs[] = {
3093 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003094 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003095 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003096 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003097 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003098 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003099 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003100 &rdev_bad_blocks.attr,
3101 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003102 NULL,
3103};
3104static ssize_t
3105rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3106{
3107 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003108 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003109 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003110 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003111
3112 if (!entry->show)
3113 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003114
3115 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3116 if (!rv) {
3117 if (rdev->mddev == NULL)
3118 rv = -EBUSY;
3119 else
3120 rv = entry->show(rdev, page);
3121 mddev_unlock(mddev);
3122 }
3123 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003124}
3125
3126static ssize_t
3127rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3128 const char *page, size_t length)
3129{
3130 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003131 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003132 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003133 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003134
3135 if (!entry->store)
3136 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003137 if (!capable(CAP_SYS_ADMIN))
3138 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003139 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003140 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003141 if (rdev->mddev == NULL)
3142 rv = -EBUSY;
3143 else
3144 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003145 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003146 }
3147 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003148}
3149
3150static void rdev_free(struct kobject *ko)
3151{
NeilBrown3cb03002011-10-11 16:45:26 +11003152 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003153 kfree(rdev);
3154}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003155static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003156 .show = rdev_attr_show,
3157 .store = rdev_attr_store,
3158};
3159static struct kobj_type rdev_ktype = {
3160 .release = rdev_free,
3161 .sysfs_ops = &rdev_sysfs_ops,
3162 .default_attrs = rdev_default_attrs,
3163};
3164
NeilBrown3cb03002011-10-11 16:45:26 +11003165int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003166{
3167 rdev->desc_nr = -1;
3168 rdev->saved_raid_disk = -1;
3169 rdev->raid_disk = -1;
3170 rdev->flags = 0;
3171 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003172 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003173 rdev->sb_events = 0;
3174 rdev->last_read_error.tv_sec = 0;
3175 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003176 rdev->sb_loaded = 0;
3177 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003178 atomic_set(&rdev->nr_pending, 0);
3179 atomic_set(&rdev->read_errors, 0);
3180 atomic_set(&rdev->corrected_errors, 0);
3181
3182 INIT_LIST_HEAD(&rdev->same_set);
3183 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003184
3185 /* Add space to store bad block list.
3186 * This reserves the space even on arrays where it cannot
3187 * be used - I wonder if that matters
3188 */
3189 rdev->badblocks.count = 0;
NeilBrown486adf72013-04-24 11:42:44 +10003190 rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
NeilBrown2230dfe2011-07-28 11:31:46 +10003191 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3192 seqlock_init(&rdev->badblocks.lock);
3193 if (rdev->badblocks.page == NULL)
3194 return -ENOMEM;
3195
3196 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003197}
3198EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
3200 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3201 *
3202 * mark the device faulty if:
3203 *
3204 * - the device is nonexistent (zero size)
3205 * - the device has no valid superblock
3206 *
3207 * a faulty rdev _never_ has rdev->sb set.
3208 */
NeilBrown3cb03002011-10-11 16:45:26 +11003209static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 char b[BDEVNAME_SIZE];
3212 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003213 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 sector_t size;
3215
NeilBrown9ffae0c2006-01-06 00:20:32 -08003216 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (!rdev) {
3218 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3219 return ERR_PTR(-ENOMEM);
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
NeilBrown2230dfe2011-07-28 11:31:46 +10003222 err = md_rdev_init(rdev);
3223 if (err)
3224 goto abort_free;
3225 err = alloc_disk_sb(rdev);
3226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto abort_free;
3228
NeilBrownc5d79ad2008-02-06 01:39:54 -08003229 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (err)
3231 goto abort_free;
3232
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003233 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003234
Mike Snitzer77304d22010-11-08 14:39:12 +01003235 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!size) {
3237 printk(KERN_WARNING
3238 "md: %s has zero or unknown size, marking faulty!\n",
3239 bdevname(rdev->bdev,b));
3240 err = -EINVAL;
3241 goto abort_free;
3242 }
3243
3244 if (super_format >= 0) {
3245 err = super_types[super_format].
3246 load_super(rdev, NULL, super_minor);
3247 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003248 printk(KERN_WARNING
3249 "md: %s does not have a valid v%d.%d "
3250 "superblock, not importing!\n",
3251 bdevname(rdev->bdev,b),
3252 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto abort_free;
3254 }
3255 if (err < 0) {
3256 printk(KERN_WARNING
3257 "md: could not read %s's sb, not importing!\n",
3258 bdevname(rdev->bdev,b));
3259 goto abort_free;
3260 }
3261 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return rdev;
3264
3265abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003266 if (rdev->bdev)
3267 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003268 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 kfree(rdev);
3270 return ERR_PTR(err);
3271}
3272
3273/*
3274 * Check a full RAID array for plausibility
3275 */
3276
3277
NeilBrownfd01b882011-10-11 16:47:53 +11003278static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
3280 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003281 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 char b[BDEVNAME_SIZE];
3283
3284 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003285 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 switch (super_types[mddev->major_version].
3287 load_super(rdev, freshest, mddev->minor_version)) {
3288 case 1:
3289 freshest = rdev;
3290 break;
3291 case 0:
3292 break;
3293 default:
3294 printk( KERN_ERR \
3295 "md: fatal superblock inconsistency in %s"
3296 " -- removing from array\n",
3297 bdevname(rdev->bdev,b));
3298 kick_rdev_from_array(rdev);
3299 }
3300
3301
3302 super_types[mddev->major_version].
3303 validate_super(mddev, freshest);
3304
3305 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003306 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003307 if (mddev->max_disks &&
3308 (rdev->desc_nr >= mddev->max_disks ||
3309 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003310 printk(KERN_WARNING
3311 "md: %s: %s: only %d devices permitted\n",
3312 mdname(mddev), bdevname(rdev->bdev, b),
3313 mddev->max_disks);
3314 kick_rdev_from_array(rdev);
3315 continue;
3316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (rdev != freshest)
3318 if (super_types[mddev->major_version].
3319 validate_super(mddev, rdev)) {
3320 printk(KERN_WARNING "md: kicking non-fresh %s"
3321 " from array!\n",
3322 bdevname(rdev->bdev,b));
3323 kick_rdev_from_array(rdev);
3324 continue;
3325 }
3326 if (mddev->level == LEVEL_MULTIPATH) {
3327 rdev->desc_nr = i++;
3328 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003329 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003330 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003331 rdev->raid_disk = -1;
3332 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
3334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
NeilBrown72e02072009-12-14 12:49:55 +11003337/* Read a fixed-point number.
3338 * Numbers in sysfs attributes should be in "standard" units where
3339 * possible, so time should be in seconds.
3340 * However we internally use a a much smaller unit such as
3341 * milliseconds or jiffies.
3342 * This function takes a decimal number with a possible fractional
3343 * component, and produces an integer which is the result of
3344 * multiplying that number by 10^'scale'.
3345 * all without any floating-point arithmetic.
3346 */
3347int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3348{
3349 unsigned long result = 0;
3350 long decimals = -1;
3351 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3352 if (*cp == '.')
3353 decimals = 0;
3354 else if (decimals < scale) {
3355 unsigned int value;
3356 value = *cp - '0';
3357 result = result * 10 + value;
3358 if (decimals >= 0)
3359 decimals++;
3360 }
3361 cp++;
3362 }
3363 if (*cp == '\n')
3364 cp++;
3365 if (*cp)
3366 return -EINVAL;
3367 if (decimals < 0)
3368 decimals = 0;
3369 while (decimals < scale) {
3370 result *= 10;
3371 decimals ++;
3372 }
3373 *res = result;
3374 return 0;
3375}
3376
3377
NeilBrown19052c02008-08-05 15:54:13 +10003378static void md_safemode_timeout(unsigned long data);
3379
NeilBrowneae17012005-11-08 21:39:23 -08003380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003381safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003382{
3383 int msec = (mddev->safemode_delay*1000)/HZ;
3384 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3385}
3386static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003387safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003388{
NeilBrown16f17b32006-06-26 00:27:37 -07003389 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003390
NeilBrown72e02072009-12-14 12:49:55 +11003391 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003392 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003393 if (msec == 0)
3394 mddev->safemode_delay = 0;
3395 else {
NeilBrown19052c02008-08-05 15:54:13 +10003396 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003397 mddev->safemode_delay = (msec*HZ)/1000;
3398 if (mddev->safemode_delay == 0)
3399 mddev->safemode_delay = 1;
NeilBrown275c51c2013-08-08 09:20:32 +10003400 if (mddev->safemode_delay < old_delay || old_delay == 0)
NeilBrown19052c02008-08-05 15:54:13 +10003401 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003402 }
3403 return len;
3404}
3405static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003406__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003407
3408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003409level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003410{
NeilBrown84fc4b52011-10-11 16:49:58 +11003411 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003412 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003413 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003414 else if (mddev->clevel[0])
3415 return sprintf(page, "%s\n", mddev->clevel);
3416 else if (mddev->level != LEVEL_NONE)
3417 return sprintf(page, "%d\n", mddev->level);
3418 else
3419 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003420}
3421
NeilBrownd9d166c2006-01-06 00:20:51 -08003422static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003423level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003424{
Dan Williamsf2859af2010-05-02 10:04:16 -07003425 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003426 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003427 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003428 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003429 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003430 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003431
3432 if (mddev->pers == NULL) {
3433 if (len == 0)
3434 return 0;
3435 if (len >= sizeof(mddev->clevel))
3436 return -ENOSPC;
3437 strncpy(mddev->clevel, buf, len);
3438 if (mddev->clevel[len-1] == '\n')
3439 len--;
3440 mddev->clevel[len] = 0;
3441 mddev->level = LEVEL_NONE;
3442 return rv;
3443 }
NeilBrownbd8839e2014-05-28 13:39:21 +10003444 if (mddev->ro)
3445 return -EROFS;
NeilBrown245f46c2009-03-31 14:39:39 +11003446
3447 /* request to change the personality. Need to ensure:
3448 * - array is not engaged in resync/recovery/reshape
3449 * - old personality can be suspended
3450 * - new personality will access other array.
3451 */
3452
NeilBrownbb4f1e92010-08-08 21:18:03 +10003453 if (mddev->sync_thread ||
3454 mddev->reshape_position != MaxSector ||
3455 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003456 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003457
3458 if (!mddev->pers->quiesce) {
3459 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3460 mdname(mddev), mddev->pers->name);
3461 return -EINVAL;
3462 }
3463
3464 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003465 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003466 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003467 strncpy(clevel, buf, len);
3468 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003469 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003470 clevel[len] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003471 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003472 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003473
Dan Williamsf2859af2010-05-02 10:04:16 -07003474 if (request_module("md-%s", clevel) != 0)
3475 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003476 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003477 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003478 if (!pers || !try_module_get(pers->owner)) {
3479 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003480 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003481 return -EINVAL;
3482 }
3483 spin_unlock(&pers_lock);
3484
3485 if (pers == mddev->pers) {
3486 /* Nothing to do! */
3487 module_put(pers->owner);
3488 return rv;
3489 }
3490 if (!pers->takeover) {
3491 module_put(pers->owner);
3492 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003493 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003494 return -EINVAL;
3495 }
3496
NeilBrowndafb20f2012-03-19 12:46:39 +11003497 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003498 rdev->new_raid_disk = rdev->raid_disk;
3499
NeilBrown245f46c2009-03-31 14:39:39 +11003500 /* ->takeover must set new_* and/or delta_disks
3501 * if it succeeds, and may set them when it fails.
3502 */
3503 priv = pers->takeover(mddev);
3504 if (IS_ERR(priv)) {
3505 mddev->new_level = mddev->level;
3506 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003507 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003508 mddev->raid_disks -= mddev->delta_disks;
3509 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003510 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003511 module_put(pers->owner);
3512 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003513 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003514 return PTR_ERR(priv);
3515 }
3516
3517 /* Looks like we have a winner */
3518 mddev_suspend(mddev);
3519 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003520
3521 if (mddev->pers->sync_request == NULL &&
3522 pers->sync_request != NULL) {
3523 /* need to add the md_redundancy_group */
3524 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3525 printk(KERN_WARNING
3526 "md: cannot register extra attributes for %s\n",
3527 mdname(mddev));
Tejun Heo388975c2013-09-11 23:19:13 -04003528 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003529 }
3530 if (mddev->pers->sync_request != NULL &&
3531 pers->sync_request == NULL) {
3532 /* need to remove the md_redundancy_group */
3533 if (mddev->to_remove == NULL)
3534 mddev->to_remove = &md_redundancy_group;
3535 }
3536
Trela Maciej54071b32010-03-08 16:02:42 +11003537 if (mddev->pers->sync_request == NULL &&
3538 mddev->external) {
3539 /* We are converting from a no-redundancy array
3540 * to a redundancy array and metadata is managed
3541 * externally so we need to be sure that writes
3542 * won't block due to a need to transition
3543 * clean->dirty
3544 * until external management is started.
3545 */
3546 mddev->in_sync = 0;
3547 mddev->safemode_delay = 0;
3548 mddev->safemode = 0;
3549 }
3550
NeilBrowndafb20f2012-03-19 12:46:39 +11003551 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003552 if (rdev->raid_disk < 0)
3553 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003554 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003555 rdev->new_raid_disk = -1;
3556 if (rdev->new_raid_disk == rdev->raid_disk)
3557 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003558 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003559 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003560 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003561 if (rdev->raid_disk < 0)
3562 continue;
3563 if (rdev->new_raid_disk == rdev->raid_disk)
3564 continue;
3565 rdev->raid_disk = rdev->new_raid_disk;
3566 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003567 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003568 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003569 if (sysfs_link_rdev(mddev, rdev))
3570 printk(KERN_WARNING "md: cannot register rd%d"
3571 " for %s after level change\n",
3572 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003573 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003574 }
3575
3576 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003577 mddev->pers = pers;
3578 mddev->private = priv;
3579 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3580 mddev->level = mddev->new_level;
3581 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003582 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003583 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003584 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003585 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003586 if (mddev->pers->sync_request == NULL) {
3587 /* this is now an array without redundancy, so
3588 * it must always be in_sync
3589 */
3590 mddev->in_sync = 1;
3591 del_timer_sync(&mddev->safemode_timer);
3592 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003593 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003594 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003595 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003596 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003597 if (!mddev->thread)
3598 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003599 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003600 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003601 return rv;
3602}
3603
3604static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003605__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003606
NeilBrownd4dbd022006-06-26 00:27:59 -07003607
3608static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003609layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003610{
3611 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003612 if (mddev->reshape_position != MaxSector &&
3613 mddev->layout != mddev->new_layout)
3614 return sprintf(page, "%d (%d)\n",
3615 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003616 return sprintf(page, "%d\n", mddev->layout);
3617}
3618
3619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003620layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003621{
3622 char *e;
3623 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003624
3625 if (!*buf || (*e && *e != '\n'))
3626 return -EINVAL;
3627
NeilBrownb3546032009-03-31 14:56:41 +11003628 if (mddev->pers) {
3629 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003630 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003631 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003632 if (mddev->ro)
3633 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003634 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003635 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003636 if (err) {
3637 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003638 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003639 }
NeilBrownb3546032009-03-31 14:56:41 +11003640 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003641 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003642 if (mddev->reshape_position == MaxSector)
3643 mddev->layout = n;
3644 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003645 return len;
3646}
3647static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003648__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003649
3650
NeilBrowneae17012005-11-08 21:39:23 -08003651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003652raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003653{
NeilBrownbb636542005-11-08 21:39:45 -08003654 if (mddev->raid_disks == 0)
3655 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003656 if (mddev->reshape_position != MaxSector &&
3657 mddev->delta_disks != 0)
3658 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3659 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003660 return sprintf(page, "%d\n", mddev->raid_disks);
3661}
3662
NeilBrownfd01b882011-10-11 16:47:53 +11003663static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003664
3665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003666raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003667{
NeilBrownda943b992006-01-06 00:20:54 -08003668 char *e;
3669 int rv = 0;
3670 unsigned long n = simple_strtoul(buf, &e, 10);
3671
3672 if (!*buf || (*e && *e != '\n'))
3673 return -EINVAL;
3674
3675 if (mddev->pers)
3676 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003677 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003678 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003679 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003680
3681 rdev_for_each(rdev, mddev) {
3682 if (olddisks < n &&
3683 rdev->data_offset < rdev->new_data_offset)
3684 return -EINVAL;
3685 if (olddisks > n &&
3686 rdev->data_offset > rdev->new_data_offset)
3687 return -EINVAL;
3688 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003689 mddev->delta_disks = n - olddisks;
3690 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003691 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003692 } else
NeilBrownda943b992006-01-06 00:20:54 -08003693 mddev->raid_disks = n;
3694 return rv ? rv : len;
3695}
3696static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003697__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003698
NeilBrown24dd4692005-11-08 21:39:26 -08003699static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003700chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003701{
NeilBrown08a02ec2007-05-09 02:35:38 -07003702 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003703 mddev->chunk_sectors != mddev->new_chunk_sectors)
3704 return sprintf(page, "%d (%d)\n",
3705 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003706 mddev->chunk_sectors << 9);
3707 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003708}
3709
3710static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003711chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003712{
NeilBrown3b343802006-01-06 00:20:47 -08003713 char *e;
3714 unsigned long n = simple_strtoul(buf, &e, 10);
3715
NeilBrown3b343802006-01-06 00:20:47 -08003716 if (!*buf || (*e && *e != '\n'))
3717 return -EINVAL;
3718
NeilBrownb3546032009-03-31 14:56:41 +11003719 if (mddev->pers) {
3720 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003721 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003722 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10003723 if (mddev->ro)
3724 return -EROFS;
NeilBrown597a7112009-06-18 08:47:42 +10003725 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003726 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003727 if (err) {
3728 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003729 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003730 }
NeilBrownb3546032009-03-31 14:56:41 +11003731 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003732 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003733 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003734 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003735 }
NeilBrown3b343802006-01-06 00:20:47 -08003736 return len;
3737}
3738static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003739__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003740
NeilBrowna94213b2006-06-26 00:28:00 -07003741static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003742resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003743{
NeilBrownd1a7c502009-03-31 15:24:32 +11003744 if (mddev->recovery_cp == MaxSector)
3745 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003746 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3747}
3748
3749static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003750resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003751{
NeilBrowna94213b2006-06-26 00:28:00 -07003752 char *e;
3753 unsigned long long n = simple_strtoull(buf, &e, 10);
3754
NeilBrownb0986362011-05-11 15:52:21 +10003755 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003756 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003757 if (cmd_match(buf, "none"))
3758 n = MaxSector;
3759 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003760 return -EINVAL;
3761
3762 mddev->recovery_cp = n;
NeilBrowndb07d852012-10-11 14:22:17 +11003763 if (mddev->pers)
3764 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowna94213b2006-06-26 00:28:00 -07003765 return len;
3766}
3767static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003768__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003769
NeilBrown9e653b62006-06-26 00:27:58 -07003770/*
3771 * The array state can be:
3772 *
3773 * clear
3774 * No devices, no size, no level
3775 * Equivalent to STOP_ARRAY ioctl
3776 * inactive
3777 * May have some settings, but array is not active
3778 * all IO results in error
3779 * When written, doesn't tear down array, but just stops it
3780 * suspended (not supported yet)
3781 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003782 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003783 * readonly
3784 * no resync can happen. no superblocks get written.
3785 * write requests fail
3786 * read-auto
3787 * like readonly, but behaves like 'clean' on a write request.
3788 *
3789 * clean - no pending writes, but otherwise active.
3790 * When written to inactive array, starts without resync
3791 * If a write request arrives then
3792 * if metadata is known, mark 'dirty' and switch to 'active'.
3793 * if not known, block and switch to write-pending
3794 * If written to an active array that has pending writes, then fails.
3795 * active
3796 * fully active: IO and resync can be happening.
3797 * When written to inactive array, starts with resync
3798 *
3799 * write-pending
3800 * clean, but writes are blocked waiting for 'active' to be written.
3801 *
3802 * active-idle
3803 * like active, but no writes have been seen for a while (100msec).
3804 *
3805 */
3806enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3807 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003808static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003809 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3810 "write-pending", "active-idle", NULL };
3811
3812static int match_word(const char *word, char **list)
3813{
3814 int n;
3815 for (n=0; list[n]; n++)
3816 if (cmd_match(word, list[n]))
3817 break;
3818 return n;
3819}
3820
3821static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003822array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003823{
3824 enum array_state st = inactive;
3825
3826 if (mddev->pers)
3827 switch(mddev->ro) {
3828 case 1:
3829 st = readonly;
3830 break;
3831 case 2:
3832 st = read_auto;
3833 break;
3834 case 0:
3835 if (mddev->in_sync)
3836 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003837 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003838 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003839 else if (mddev->safemode)
3840 st = active_idle;
3841 else
3842 st = active;
3843 }
3844 else {
3845 if (list_empty(&mddev->disks) &&
3846 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003847 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003848 st = clear;
3849 else
3850 st = inactive;
3851 }
3852 return sprintf(page, "%s\n", array_states[st]);
3853}
3854
NeilBrowna05b7ea2012-07-19 15:59:18 +10003855static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3856static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003857static int do_md_run(struct mddev * mddev);
3858static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003859
3860static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003861array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003862{
3863 int err = -EINVAL;
3864 enum array_state st = match_word(buf, array_states);
3865 switch(st) {
3866 case bad_word:
3867 break;
3868 case clear:
3869 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10003870 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003871 break;
3872 case inactive:
3873 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003874 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003875 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003876 else
NeilBrowne6910632008-02-06 01:39:51 -08003877 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003878 break;
3879 case suspended:
3880 break; /* not supported yet */
3881 case readonly:
3882 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003883 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003884 else {
3885 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003886 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003887 err = do_md_run(mddev);
3888 }
3889 break;
3890 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003891 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003892 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003893 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003894 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003895 err = restart_array(mddev);
3896 if (err == 0) {
3897 mddev->ro = 2;
3898 set_disk_ro(mddev->gendisk, 0);
3899 }
NeilBrown9e653b62006-06-26 00:27:58 -07003900 } else {
3901 mddev->ro = 2;
3902 err = do_md_run(mddev);
3903 }
3904 break;
3905 case clean:
3906 if (mddev->pers) {
3907 restart_array(mddev);
3908 spin_lock_irq(&mddev->write_lock);
3909 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003910 if (mddev->in_sync == 0) {
3911 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003912 if (mddev->safemode == 1)
3913 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003914 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003915 }
3916 err = 0;
3917 } else
3918 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003919 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003920 } else
3921 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003922 break;
3923 case active:
3924 if (mddev->pers) {
3925 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003926 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003927 wake_up(&mddev->sb_wait);
3928 err = 0;
3929 } else {
3930 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003931 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003932 err = do_md_run(mddev);
3933 }
3934 break;
3935 case write_pending:
3936 case active_idle:
3937 /* these cannot be set */
3938 break;
3939 }
3940 if (err)
3941 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003942 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003943 if (mddev->hold_active == UNTIL_IOCTL)
3944 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003945 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003946 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003947 }
NeilBrown9e653b62006-06-26 00:27:58 -07003948}
NeilBrown80ca3a42006-07-10 04:44:18 -07003949static struct md_sysfs_entry md_array_state =
3950__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003951
NeilBrown6d7ff7382006-01-06 00:21:16 -08003952static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003953max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003954 return sprintf(page, "%d\n",
3955 atomic_read(&mddev->max_corr_read_errors));
3956}
3957
3958static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003959max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003960{
3961 char *e;
3962 unsigned long n = simple_strtoul(buf, &e, 10);
3963
3964 if (*buf && (*e == 0 || *e == '\n')) {
3965 atomic_set(&mddev->max_corr_read_errors, n);
3966 return len;
3967 }
3968 return -EINVAL;
3969}
3970
3971static struct md_sysfs_entry max_corr_read_errors =
3972__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3973 max_corrected_read_errors_store);
3974
3975static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003976null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003977{
3978 return -EINVAL;
3979}
3980
3981static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003982new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003983{
3984 /* buf must be %d:%d\n? giving major and minor numbers */
3985 /* The new device is added to the array.
3986 * If the array has a persistent superblock, we read the
3987 * superblock to initialise info and check validity.
3988 * Otherwise, only checking done is that in bind_rdev_to_array,
3989 * which mainly checks size.
3990 */
3991 char *e;
3992 int major = simple_strtoul(buf, &e, 10);
3993 int minor;
3994 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003995 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003996 int err;
3997
3998 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3999 return -EINVAL;
4000 minor = simple_strtoul(e+1, &e, 10);
4001 if (*e && *e != '\n')
4002 return -EINVAL;
4003 dev = MKDEV(major, minor);
4004 if (major != MAJOR(dev) ||
4005 minor != MINOR(dev))
4006 return -EOVERFLOW;
4007
4008
4009 if (mddev->persistent) {
4010 rdev = md_import_device(dev, mddev->major_version,
4011 mddev->minor_version);
4012 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004013 struct md_rdev *rdev0
4014 = list_entry(mddev->disks.next,
4015 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004016 err = super_types[mddev->major_version]
4017 .load_super(rdev, rdev0, mddev->minor_version);
4018 if (err < 0)
4019 goto out;
4020 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004021 } else if (mddev->external)
4022 rdev = md_import_device(dev, -2, -1);
4023 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004024 rdev = md_import_device(dev, -1, -1);
4025
4026 if (IS_ERR(rdev))
4027 return PTR_ERR(rdev);
4028 err = bind_rdev_to_array(rdev, mddev);
4029 out:
4030 if (err)
4031 export_rdev(rdev);
4032 return err ? err : len;
4033}
4034
4035static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004036__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004037
4038static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004039bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004040{
4041 char *end;
4042 unsigned long chunk, end_chunk;
4043
4044 if (!mddev->bitmap)
4045 goto out;
4046 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4047 while (*buf) {
4048 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4049 if (buf == end) break;
4050 if (*end == '-') { /* range */
4051 buf = end + 1;
4052 end_chunk = simple_strtoul(buf, &end, 0);
4053 if (buf == end) break;
4054 }
4055 if (*end && !isspace(*end)) break;
4056 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004057 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004058 }
4059 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4060out:
4061 return len;
4062}
4063
4064static struct md_sysfs_entry md_bitmap =
4065__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4066
4067static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004068size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004069{
Andre Noll58c0fed2009-03-31 14:33:13 +11004070 return sprintf(page, "%llu\n",
4071 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004072}
4073
NeilBrownfd01b882011-10-11 16:47:53 +11004074static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004075
4076static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004077size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004078{
4079 /* If array is inactive, we can reduce the component size, but
4080 * not increase it (except from 0).
4081 * If array is active, we can try an on-line resize
4082 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004083 sector_t sectors;
4084 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004085
Andre Noll58c0fed2009-03-31 14:33:13 +11004086 if (err < 0)
4087 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004088 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004089 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004090 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004091 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004092 if (mddev->dev_sectors == 0 ||
4093 mddev->dev_sectors > sectors)
4094 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004095 else
4096 err = -ENOSPC;
4097 }
4098 return err ? err : len;
4099}
4100
4101static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004102__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004103
NeilBrown8bb93aa2006-01-06 00:20:50 -08004104
Masanari Iida83f0d772012-10-30 00:18:08 +09004105/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004106 * This is one of
4107 * 'none' for arrays with no metadata (good luck...)
4108 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004109 * or N.M for internally known formats
4110 */
4111static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004112metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004113{
4114 if (mddev->persistent)
4115 return sprintf(page, "%d.%d\n",
4116 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004117 else if (mddev->external)
4118 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004119 else
4120 return sprintf(page, "none\n");
4121}
4122
4123static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004124metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004125{
4126 int major, minor;
4127 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004128 /* Changing the details of 'external' metadata is
4129 * always permitted. Otherwise there must be
4130 * no devices attached to the array.
4131 */
4132 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4133 ;
4134 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004135 return -EBUSY;
4136
4137 if (cmd_match(buf, "none")) {
4138 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004139 mddev->external = 0;
4140 mddev->major_version = 0;
4141 mddev->minor_version = 90;
4142 return len;
4143 }
4144 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004145 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004146 if (namelen >= sizeof(mddev->metadata_type))
4147 namelen = sizeof(mddev->metadata_type)-1;
4148 strncpy(mddev->metadata_type, buf+9, namelen);
4149 mddev->metadata_type[namelen] = 0;
4150 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4151 mddev->metadata_type[--namelen] = 0;
4152 mddev->persistent = 0;
4153 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004154 mddev->major_version = 0;
4155 mddev->minor_version = 90;
4156 return len;
4157 }
4158 major = simple_strtoul(buf, &e, 10);
4159 if (e==buf || *e != '.')
4160 return -EINVAL;
4161 buf = e+1;
4162 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004163 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004164 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004165 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004166 return -ENOENT;
4167 mddev->major_version = major;
4168 mddev->minor_version = minor;
4169 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004170 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004171 return len;
4172}
4173
4174static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004175__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004176
NeilBrowna35b0d62006-01-06 00:20:49 -08004177static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004178action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004179{
NeilBrown7eec3142005-11-08 21:39:44 -08004180 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004181 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4182 type = "frozen";
4183 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004184 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004185 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4186 type = "reshape";
4187 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004188 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4189 type = "resync";
4190 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4191 type = "check";
4192 else
4193 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004194 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004195 type = "recover";
4196 }
4197 return sprintf(page, "%s\n", type);
4198}
4199
4200static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004201action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004202{
NeilBrown7eec3142005-11-08 21:39:44 -08004203 if (!mddev->pers || !mddev->pers->sync_request)
4204 return -EINVAL;
4205
NeilBrownb6a9ce62009-05-26 09:41:17 +10004206 if (cmd_match(page, "frozen"))
4207 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4208 else
4209 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4210
4211 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004212 if (mddev->sync_thread) {
4213 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10004214 md_reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004215 }
NeilBrown03c902e2006-01-06 00:20:46 -08004216 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4217 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004218 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004219 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004220 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004221 else if (cmd_match(page, "recover")) {
4222 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4223 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4224 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004225 int err;
4226 if (mddev->pers->start_reshape == NULL)
4227 return -EINVAL;
4228 err = mddev->pers->start_reshape(mddev);
4229 if (err)
4230 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004231 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004232 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004233 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004234 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004235 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004236 return -EINVAL;
4237 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4238 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004239 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004240 if (mddev->ro == 2) {
4241 /* A write to sync_action is enough to justify
4242 * canceling read-auto mode
4243 */
4244 mddev->ro = 0;
4245 md_wakeup_thread(mddev->sync_thread);
4246 }
NeilBrown03c902e2006-01-06 00:20:46 -08004247 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004248 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004249 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004250 return len;
4251}
4252
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004253static struct md_sysfs_entry md_scan_mode =
4254__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4255
4256static ssize_t
4257last_sync_action_show(struct mddev *mddev, char *page)
4258{
4259 return sprintf(page, "%s\n", mddev->last_sync_action);
4260}
4261
4262static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4263
NeilBrown9d888832005-11-08 21:39:26 -08004264static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004265mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004266{
4267 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004268 (unsigned long long)
4269 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004270}
4271
NeilBrown80ca3a42006-07-10 04:44:18 -07004272static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004273
NeilBrown88202a02006-01-06 00:21:36 -08004274static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004275sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004276{
4277 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4278 mddev->sync_speed_min ? "local": "system");
4279}
4280
4281static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004282sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004283{
4284 int min;
4285 char *e;
4286 if (strncmp(buf, "system", 6)==0) {
4287 mddev->sync_speed_min = 0;
4288 return len;
4289 }
4290 min = simple_strtoul(buf, &e, 10);
4291 if (buf == e || (*e && *e != '\n') || min <= 0)
4292 return -EINVAL;
4293 mddev->sync_speed_min = min;
4294 return len;
4295}
4296
4297static struct md_sysfs_entry md_sync_min =
4298__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4299
4300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004302{
4303 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4304 mddev->sync_speed_max ? "local": "system");
4305}
4306
4307static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004308sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004309{
4310 int max;
4311 char *e;
4312 if (strncmp(buf, "system", 6)==0) {
4313 mddev->sync_speed_max = 0;
4314 return len;
4315 }
4316 max = simple_strtoul(buf, &e, 10);
4317 if (buf == e || (*e && *e != '\n') || max <= 0)
4318 return -EINVAL;
4319 mddev->sync_speed_max = max;
4320 return len;
4321}
4322
4323static struct md_sysfs_entry md_sync_max =
4324__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4325
Iustin Popd7f3d292007-10-16 23:30:54 -07004326static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004327degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004328{
4329 return sprintf(page, "%d\n", mddev->degraded);
4330}
4331static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004332
4333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004334sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004335{
4336 return sprintf(page, "%d\n", mddev->parallel_resync);
4337}
4338
4339static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004340sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004341{
4342 long n;
4343
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004344 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004345 return -EINVAL;
4346
4347 if (n != 0 && n != 1)
4348 return -EINVAL;
4349
4350 mddev->parallel_resync = n;
4351
4352 if (mddev->sync_thread)
4353 wake_up(&resync_wait);
4354
4355 return len;
4356}
4357
4358/* force parallel resync, even with shared block devices */
4359static struct md_sysfs_entry md_sync_force_parallel =
4360__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4361 sync_force_parallel_show, sync_force_parallel_store);
4362
4363static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004364sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004365{
4366 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004367 if (mddev->curr_resync == 0)
4368 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004369 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4370 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004371 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004372 db = resync - mddev->resync_mark_cnt;
4373 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004374}
4375
NeilBrown80ca3a42006-07-10 04:44:18 -07004376static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004377
4378static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004379sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004380{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004381 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004382
NeilBrownacb180b2009-04-14 16:28:34 +10004383 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4384 return sprintf(page, "none\n");
4385
NeilBrown72f36d52012-10-11 14:25:57 +11004386 if (mddev->curr_resync == 1 ||
4387 mddev->curr_resync == 2)
4388 return sprintf(page, "delayed\n");
4389
NeilBrownc804cde2012-05-21 09:28:33 +10004390 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4391 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004392 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004393 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004394 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004395
NeilBrownacb180b2009-04-14 16:28:34 +10004396 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004397 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004398}
4399
NeilBrown80ca3a42006-07-10 04:44:18 -07004400static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004401
NeilBrowne464eaf2006-03-27 01:18:14 -08004402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004404{
4405 return sprintf(page, "%llu\n",
4406 (unsigned long long)mddev->resync_min);
4407}
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004410{
4411 unsigned long long min;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004412 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004413 return -EINVAL;
4414 if (min > mddev->resync_max)
4415 return -EINVAL;
4416 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4417 return -EBUSY;
4418
4419 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004420 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004421 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004422 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004423 return -EINVAL;
4424 }
4425 mddev->resync_min = min;
4426
4427 return len;
4428}
4429
4430static struct md_sysfs_entry md_min_sync =
4431__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4432
4433static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004434max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004435{
4436 if (mddev->resync_max == MaxSector)
4437 return sprintf(page, "max\n");
4438 else
4439 return sprintf(page, "%llu\n",
4440 (unsigned long long)mddev->resync_max);
4441}
4442static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004443max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004444{
4445 if (strncmp(buf, "max", 3) == 0)
4446 mddev->resync_max = MaxSector;
4447 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004448 unsigned long long max;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004449 if (kstrtoull(buf, 10, &max))
Neil Brown5e96ee62008-06-28 08:31:24 +10004450 return -EINVAL;
4451 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004452 return -EINVAL;
4453 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004454 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004455 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4456 return -EBUSY;
4457
4458 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004459 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004460 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004461 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004462 return -EINVAL;
4463 }
4464 mddev->resync_max = max;
4465 }
4466 wake_up(&mddev->recovery_wait);
4467 return len;
4468}
4469
4470static struct md_sysfs_entry md_max_sync =
4471__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4472
4473static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004474suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004475{
4476 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4477}
4478
4479static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004480suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004481{
4482 char *e;
4483 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004484 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004485
NeilBrownb8d966e2009-07-01 11:14:04 +10004486 if (mddev->pers == NULL ||
4487 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004488 return -EINVAL;
4489 if (buf == e || (*e && *e != '\n'))
4490 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004491
4492 mddev->suspend_lo = new;
4493 if (new >= old)
4494 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004495 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004496 else {
4497 /* Expanding suspended region - need to wait */
4498 mddev->pers->quiesce(mddev, 1);
4499 mddev->pers->quiesce(mddev, 0);
4500 }
4501 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004502}
4503static struct md_sysfs_entry md_suspend_lo =
4504__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4505
4506
4507static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004508suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004509{
4510 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4511}
4512
4513static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004514suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004515{
4516 char *e;
4517 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004518 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004519
NeilBrownb8d966e2009-07-01 11:14:04 +10004520 if (mddev->pers == NULL ||
4521 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004522 return -EINVAL;
4523 if (buf == e || (*e && *e != '\n'))
4524 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004525
4526 mddev->suspend_hi = new;
4527 if (new <= old)
4528 /* Shrinking suspended region */
4529 mddev->pers->quiesce(mddev, 2);
4530 else {
4531 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004532 mddev->pers->quiesce(mddev, 1);
4533 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004534 }
4535 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004536}
4537static struct md_sysfs_entry md_suspend_hi =
4538__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4539
NeilBrown08a02ec2007-05-09 02:35:38 -07004540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004541reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004542{
4543 if (mddev->reshape_position != MaxSector)
4544 return sprintf(page, "%llu\n",
4545 (unsigned long long)mddev->reshape_position);
4546 strcpy(page, "none\n");
4547 return 5;
4548}
4549
4550static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004551reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004552{
NeilBrownc6563a82012-05-21 09:27:00 +10004553 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004554 char *e;
4555 unsigned long long new = simple_strtoull(buf, &e, 10);
4556 if (mddev->pers)
4557 return -EBUSY;
4558 if (buf == e || (*e && *e != '\n'))
4559 return -EINVAL;
4560 mddev->reshape_position = new;
4561 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004562 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004563 mddev->new_level = mddev->level;
4564 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004565 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004566 rdev_for_each(rdev, mddev)
4567 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004568 return len;
4569}
4570
4571static struct md_sysfs_entry md_reshape_position =
4572__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4573 reshape_position_store);
4574
Dan Williamsb522adc2009-03-31 15:00:31 +11004575static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004576reshape_direction_show(struct mddev *mddev, char *page)
4577{
4578 return sprintf(page, "%s\n",
4579 mddev->reshape_backwards ? "backwards" : "forwards");
4580}
4581
4582static ssize_t
4583reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4584{
4585 int backwards = 0;
4586 if (cmd_match(buf, "forwards"))
4587 backwards = 0;
4588 else if (cmd_match(buf, "backwards"))
4589 backwards = 1;
4590 else
4591 return -EINVAL;
4592 if (mddev->reshape_backwards == backwards)
4593 return len;
4594
4595 /* check if we are allowed to change */
4596 if (mddev->delta_disks)
4597 return -EBUSY;
4598
4599 if (mddev->persistent &&
4600 mddev->major_version == 0)
4601 return -EINVAL;
4602
4603 mddev->reshape_backwards = backwards;
4604 return len;
4605}
4606
4607static struct md_sysfs_entry md_reshape_direction =
4608__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4609 reshape_direction_store);
4610
4611static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004612array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004613{
4614 if (mddev->external_size)
4615 return sprintf(page, "%llu\n",
4616 (unsigned long long)mddev->array_sectors/2);
4617 else
4618 return sprintf(page, "default\n");
4619}
4620
4621static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004622array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004623{
4624 sector_t sectors;
4625
4626 if (strncmp(buf, "default", 7) == 0) {
4627 if (mddev->pers)
4628 sectors = mddev->pers->size(mddev, 0, 0);
4629 else
4630 sectors = mddev->array_sectors;
4631
4632 mddev->external_size = 0;
4633 } else {
4634 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4635 return -EINVAL;
4636 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004637 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004638
4639 mddev->external_size = 1;
4640 }
4641
4642 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004643 if (mddev->pers) {
4644 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004645 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004646 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004647 return len;
4648}
4649
4650static struct md_sysfs_entry md_array_size =
4651__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4652 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004653
NeilBrowneae17012005-11-08 21:39:23 -08004654static struct attribute *md_default_attrs[] = {
4655 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004656 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004657 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004658 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004659 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004660 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004661 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004662 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004663 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004664 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004665 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004666 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004667 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004668 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004669 NULL,
4670};
4671
4672static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004673 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004674 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004675 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004676 &md_sync_min.attr,
4677 &md_sync_max.attr,
4678 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004679 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004680 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004681 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004682 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004683 &md_suspend_lo.attr,
4684 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004685 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004686 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004687 NULL,
4688};
NeilBrown411036f2005-11-08 21:39:40 -08004689static struct attribute_group md_redundancy_group = {
4690 .name = NULL,
4691 .attrs = md_redundancy_attrs,
4692};
4693
NeilBrowneae17012005-11-08 21:39:23 -08004694
4695static ssize_t
4696md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4697{
4698 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004699 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004700 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004701
4702 if (!entry->show)
4703 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004704 spin_lock(&all_mddevs_lock);
4705 if (list_empty(&mddev->all_mddevs)) {
4706 spin_unlock(&all_mddevs_lock);
4707 return -EBUSY;
4708 }
4709 mddev_get(mddev);
4710 spin_unlock(&all_mddevs_lock);
4711
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004712 rv = mddev_lock(mddev);
4713 if (!rv) {
4714 rv = entry->show(mddev, page);
4715 mddev_unlock(mddev);
4716 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004717 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004718 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004719}
4720
4721static ssize_t
4722md_attr_store(struct kobject *kobj, struct attribute *attr,
4723 const char *page, size_t length)
4724{
4725 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004726 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004727 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004728
4729 if (!entry->store)
4730 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004731 if (!capable(CAP_SYS_ADMIN))
4732 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004733 spin_lock(&all_mddevs_lock);
4734 if (list_empty(&mddev->all_mddevs)) {
4735 spin_unlock(&all_mddevs_lock);
4736 return -EBUSY;
4737 }
4738 mddev_get(mddev);
4739 spin_unlock(&all_mddevs_lock);
NeilBrowna7a3f082012-12-11 13:35:54 +11004740 if (entry->store == new_dev_store)
4741 flush_workqueue(md_misc_wq);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004742 rv = mddev_lock(mddev);
4743 if (!rv) {
4744 rv = entry->store(mddev, page, length);
4745 mddev_unlock(mddev);
4746 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004747 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004748 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004749}
4750
4751static void md_free(struct kobject *ko)
4752{
NeilBrownfd01b882011-10-11 16:47:53 +11004753 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004754
4755 if (mddev->sysfs_state)
4756 sysfs_put(mddev->sysfs_state);
4757
4758 if (mddev->gendisk) {
4759 del_gendisk(mddev->gendisk);
4760 put_disk(mddev->gendisk);
4761 }
4762 if (mddev->queue)
4763 blk_cleanup_queue(mddev->queue);
4764
NeilBrowneae17012005-11-08 21:39:23 -08004765 kfree(mddev);
4766}
4767
Emese Revfy52cf25d2010-01-19 02:58:23 +01004768static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004769 .show = md_attr_show,
4770 .store = md_attr_store,
4771};
4772static struct kobj_type md_ktype = {
4773 .release = md_free,
4774 .sysfs_ops = &md_sysfs_ops,
4775 .default_attrs = md_default_attrs,
4776};
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778int mdp_major = 0;
4779
Dan Williams5fd3a172009-03-04 00:57:25 -07004780static void mddev_delayed_delete(struct work_struct *ws)
4781{
NeilBrownfd01b882011-10-11 16:47:53 +11004782 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004783
NeilBrown43a70502009-12-14 12:49:55 +11004784 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004785 kobject_del(&mddev->kobj);
4786 kobject_put(&mddev->kobj);
4787}
4788
NeilBrownefeb53c2009-01-09 08:31:10 +11004789static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004791 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004792 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004794 int partitioned;
4795 int shift;
4796 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004797 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
4799 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004800 return -ENODEV;
4801
4802 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4803 shift = partitioned ? MdpMinorShift : 0;
4804 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Tejun Heoe804ac72010-10-15 15:36:08 +02004806 /* wait for any previous instance of this device to be
4807 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004808 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004809 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004810
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004811 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004812 error = -EEXIST;
4813 if (mddev->gendisk)
4814 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004815
4816 if (name) {
4817 /* Need to ensure that 'name' is not a duplicate.
4818 */
NeilBrownfd01b882011-10-11 16:47:53 +11004819 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004820 spin_lock(&all_mddevs_lock);
4821
4822 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4823 if (mddev2->gendisk &&
4824 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4825 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004826 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004827 }
4828 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
NeilBrown8b765392009-01-09 08:31:08 +11004830
NeilBrown0909dc42009-07-01 12:27:21 +10004831 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004832 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004833 if (!mddev->queue)
4834 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004835 mddev->queue->queuedata = mddev;
4836
NeilBrown409c57f2009-03-31 14:39:39 +11004837 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004838 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 disk = alloc_disk(1 << shift);
4841 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004842 blk_cleanup_queue(mddev->queue);
4843 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004844 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004846 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004848 if (name)
4849 strcpy(disk->disk_name, name);
4850 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004852 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 disk->fops = &md_fops;
4855 disk->private_data = mddev;
4856 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004857 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004858 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004859 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004860 * remove it now.
4861 */
4862 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004864 /* As soon as we call add_disk(), another thread could get
4865 * through to md_open, so make sure it doesn't get too far
4866 */
4867 mutex_lock(&mddev->open_mutex);
4868 add_disk(disk);
4869
Tejun Heoed9e1982008-08-25 19:56:05 +09004870 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4871 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004872 if (error) {
4873 /* This isn't possible, but as kobject_init_and_add is marked
4874 * __must_check, we must do something with the result
4875 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004876 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4877 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004878 error = 0;
4879 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004880 if (mddev->kobj.sd &&
4881 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004882 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004883 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004884 abort:
4885 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004886 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004887 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004888 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004889 }
NeilBrownd3374822009-01-09 08:31:10 +11004890 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004891 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004892}
4893
4894static struct kobject *md_probe(dev_t dev, int *part, void *data)
4895{
4896 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return NULL;
4898}
4899
NeilBrownefeb53c2009-01-09 08:31:10 +11004900static int add_named_array(const char *val, struct kernel_param *kp)
4901{
4902 /* val must be "md_*" where * is not all digits.
4903 * We allocate an array with a large free minor number, and
4904 * set the name to val. val must not already be an active name.
4905 */
4906 int len = strlen(val);
4907 char buf[DISK_NAME_LEN];
4908
4909 while (len && val[len-1] == '\n')
4910 len--;
4911 if (len >= DISK_NAME_LEN)
4912 return -E2BIG;
4913 strlcpy(buf, val, len+1);
4914 if (strncmp(buf, "md_", 3) != 0)
4915 return -EINVAL;
4916 return md_alloc(0, buf);
4917}
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919static void md_safemode_timeout(unsigned long data)
4920{
NeilBrownfd01b882011-10-11 16:47:53 +11004921 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Neil Brown0fd62b82008-06-28 08:31:36 +10004923 if (!atomic_read(&mddev->writes_pending)) {
4924 mddev->safemode = 1;
4925 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004926 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 md_wakeup_thread(mddev->thread);
4929}
4930
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004931static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
NeilBrownfd01b882011-10-11 16:47:53 +11004933int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
NeilBrown2604b702006-01-06 00:20:36 -08004935 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004936 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004937 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
NeilBrowna757e642005-04-16 15:26:42 -07004939 if (list_empty(&mddev->disks))
4940 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 if (mddev->pers)
4944 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004945 /* Cannot run until previous stop completes properly */
4946 if (mddev->sysfs_active)
4947 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 /*
4950 * Analyze all RAID superblock(s)
4951 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004952 if (!mddev->raid_disks) {
4953 if (!mddev->persistent)
4954 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004955 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
NeilBrownd9d166c2006-01-06 00:20:51 -08004958 if (mddev->level != LEVEL_NONE)
4959 request_module("md-level-%d", mddev->level);
4960 else if (mddev->clevel[0])
4961 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 /*
4964 * Drop all container device buffers, from now on
4965 * the only valid external interface is through the md
4966 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004968 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004969 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 continue;
4971 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004972 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004973
4974 /* perform some consistency tests on the device.
4975 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004976 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004977 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004978 if (rdev->meta_bdev) {
4979 /* Nothing to check */;
4980 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004981 if (mddev->dev_sectors &&
4982 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004983 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004984 printk("md: %s: data overlaps metadata\n",
4985 mdname(mddev));
4986 return -EINVAL;
4987 }
4988 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004989 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004990 > rdev->data_offset) {
4991 printk("md: %s: metadata overlaps data\n",
4992 mdname(mddev));
4993 return -EINVAL;
4994 }
4995 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004996 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 }
4998
NeilBrowna167f662010-10-26 18:31:13 +11004999 if (mddev->bio_set == NULL)
Kent Overstreet395c72a2012-09-06 15:34:55 -07005000 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
NeilBrowna167f662010-10-26 18:31:13 +11005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005003 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005004 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005006 if (mddev->level != LEVEL_NONE)
5007 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5008 mddev->level);
5009 else
5010 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5011 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return -EINVAL;
5013 }
NeilBrown2604b702006-01-06 00:20:36 -08005014 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005016 if (mddev->level != pers->level) {
5017 mddev->level = pers->level;
5018 mddev->new_level = pers->level;
5019 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005020 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
NeilBrownf6705572006-03-27 01:18:11 -08005022 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005023 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005024 /* This personality cannot handle reshaping... */
5025 mddev->pers = NULL;
5026 module_put(pers->owner);
5027 return -EINVAL;
5028 }
5029
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005030 if (pers->sync_request) {
5031 /* Warn if this is a potentially silly
5032 * configuration.
5033 */
5034 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005035 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005036 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005037
NeilBrowndafb20f2012-03-19 12:46:39 +11005038 rdev_for_each(rdev, mddev)
5039 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005040 if (rdev < rdev2 &&
5041 rdev->bdev->bd_contains ==
5042 rdev2->bdev->bd_contains) {
5043 printk(KERN_WARNING
5044 "%s: WARNING: %s appears to be"
5045 " on the same physical disk as"
5046 " %s.\n",
5047 mdname(mddev),
5048 bdevname(rdev->bdev,b),
5049 bdevname(rdev2->bdev,b2));
5050 warned = 1;
5051 }
5052 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005053
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005054 if (warned)
5055 printk(KERN_WARNING
5056 "True protection against single-disk"
5057 " failure might be compromised.\n");
5058 }
5059
NeilBrown657390d2005-08-26 18:34:16 -07005060 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005061 /* may be over-ridden by personality */
5062 mddev->resync_max_sectors = mddev->dev_sectors;
5063
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005064 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
NeilBrown0f9552b52009-12-30 12:08:50 +11005066 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005067 mddev->ro = 2; /* read-only, but switch on first write */
5068
NeilBrownb15c2e52006-01-06 00:20:16 -08005069 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005070 if (err)
5071 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005072 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5073 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5074 " but 'external_size' not in effect?\n", __func__);
5075 printk(KERN_ERR
5076 "md: invalid array_size %llu > default size %llu\n",
5077 (unsigned long long)mddev->array_sectors / 2,
5078 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5079 err = -EINVAL;
5080 mddev->pers->stop(mddev);
5081 }
NeilBrownef99bf42012-05-22 13:55:08 +10005082 if (err == 0 && mddev->pers->sync_request &&
5083 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005084 err = bitmap_create(mddev);
5085 if (err) {
5086 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5087 mdname(mddev), err);
5088 mddev->pers->stop(mddev);
5089 }
5090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 module_put(mddev->pers->owner);
5093 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005094 bitmap_destroy(mddev);
5095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005097 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005098 if (mddev->kobj.sd &&
5099 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005100 printk(KERN_WARNING
5101 "md: cannot register extra attributes for %s\n",
5102 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005103 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005104 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005105 mddev->ro = 0;
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005108 atomic_set(&mddev->max_corr_read_errors,
5109 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 mddev->safemode = 0;
5111 mddev->safemode_timer.function = md_safemode_timeout;
5112 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005113 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005115 smp_wmb();
5116 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005117 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005118 if (rdev->raid_disk >= 0)
5119 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005120 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5123
NeilBrown7a0a5352013-08-27 16:28:23 +10005124 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07005125 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
NeilBrownd7603b72006-01-06 00:20:30 -08005127 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005128 sysfs_notify_dirent_safe(mddev->sysfs_state);
5129 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005130 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return 0;
5132}
NeilBrown390ee602010-06-01 19:37:27 +10005133EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
NeilBrownfd01b882011-10-11 16:47:53 +11005135static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005136{
5137 int err;
5138
5139 err = md_run(mddev);
5140 if (err)
5141 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005142 err = bitmap_load(mddev);
5143 if (err) {
5144 bitmap_destroy(mddev);
5145 goto out;
5146 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005147
5148 md_wakeup_thread(mddev->thread);
5149 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5150
NeilBrownfe60b012010-03-29 11:10:42 +11005151 set_capacity(mddev->gendisk, mddev->array_sectors);
5152 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005153 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005154 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5155out:
5156 return err;
5157}
5158
NeilBrownfd01b882011-10-11 16:47:53 +11005159static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
5161 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Andre Noll80fab1d2008-07-11 22:02:21 +10005163 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005165 return -ENXIO;
5166 if (!mddev->pers)
5167 return -EINVAL;
5168 if (!mddev->ro)
5169 return -EBUSY;
5170 mddev->safemode = 0;
5171 mddev->ro = 0;
5172 set_disk_ro(disk, 0);
5173 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5174 mdname(mddev));
5175 /* Kick recovery or resync if necessary */
5176 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5177 md_wakeup_thread(mddev->thread);
5178 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005179 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181}
5182
NeilBrownfd01b882011-10-11 16:47:53 +11005183static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005184{
5185 mddev->array_sectors = 0;
5186 mddev->external_size = 0;
5187 mddev->dev_sectors = 0;
5188 mddev->raid_disks = 0;
5189 mddev->recovery_cp = 0;
5190 mddev->resync_min = 0;
5191 mddev->resync_max = MaxSector;
5192 mddev->reshape_position = MaxSector;
5193 mddev->external = 0;
5194 mddev->persistent = 0;
5195 mddev->level = LEVEL_NONE;
5196 mddev->clevel[0] = 0;
5197 mddev->flags = 0;
5198 mddev->ro = 0;
5199 mddev->metadata_type[0] = 0;
5200 mddev->chunk_sectors = 0;
5201 mddev->ctime = mddev->utime = 0;
5202 mddev->layout = 0;
5203 mddev->max_disks = 0;
5204 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005205 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005206 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005207 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005208 mddev->new_level = LEVEL_NONE;
5209 mddev->new_layout = 0;
5210 mddev->new_chunk_sectors = 0;
5211 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005212 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005213 mddev->suspend_lo = mddev->suspend_hi = 0;
5214 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5215 mddev->recovery = 0;
5216 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005217 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005218 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005219 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005220 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005221 mddev->bitmap_info.offset = 0;
5222 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005223 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005224 mddev->bitmap_info.chunksize = 0;
5225 mddev->bitmap_info.daemon_sleep = 0;
5226 mddev->bitmap_info.max_write_behind = 0;
5227}
5228
NeilBrownfd01b882011-10-11 16:47:53 +11005229static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005230{
NeilBrown6b6204e2013-05-09 09:48:30 +10005231 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrowna047e122010-03-29 12:07:53 +11005232 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005233 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005234 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005235 }
5236
5237 del_timer_sync(&mddev->safemode_timer);
5238
5239 bitmap_flush(mddev);
5240 md_super_wait(mddev);
5241
NeilBrownb6d428c2013-04-24 11:42:42 +10005242 if (mddev->ro == 0 &&
NeilBrown7a0a5352013-08-27 16:28:23 +10005243 (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
NeilBrowna047e122010-03-29 12:07:53 +11005244 /* mark array as shutdown cleanly */
5245 mddev->in_sync = 1;
5246 md_update_sb(mddev, 1);
5247 }
5248}
NeilBrowndefad612011-01-14 09:14:33 +11005249
NeilBrownfd01b882011-10-11 16:47:53 +11005250void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005251{
NeilBrown29f097c2013-11-14 17:54:51 +11005252 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005253 __md_stop_writes(mddev);
5254 mddev_unlock(mddev);
5255}
NeilBrown390ee602010-06-01 19:37:27 +10005256EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005257
NeilBrown5eff3c42012-11-19 10:47:48 +11005258static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005259{
NeilBrown0ca69882011-01-14 09:14:33 +11005260 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->pers->stop(mddev);
5262 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5263 mddev->to_remove = &md_redundancy_group;
5264 module_put(mddev->pers->owner);
5265 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005266 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005267}
NeilBrown5eff3c42012-11-19 10:47:48 +11005268
5269void md_stop(struct mddev *mddev)
5270{
5271 /* stop the array and free an attached data structures.
5272 * This is called from dm-raid
5273 */
5274 __md_stop(mddev);
5275 bitmap_destroy(mddev);
5276 if (mddev->bio_set)
5277 bioset_free(mddev->bio_set);
5278}
5279
NeilBrown390ee602010-06-01 19:37:27 +10005280EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005281
NeilBrowna05b7ea2012-07-19 15:59:18 +10005282static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005283{
5284 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11005285 int did_freeze = 0;
5286
5287 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5288 did_freeze = 1;
5289 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5290 md_wakeup_thread(mddev->thread);
5291 }
5292 if (mddev->sync_thread) {
5293 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5294 /* Thread might be blocked waiting for metadata update
5295 * which will now never happen */
5296 wake_up_process(mddev->sync_thread->tsk);
5297 }
5298 mddev_unlock(mddev);
5299 wait_event(resync_wait, mddev->sync_thread == NULL);
5300 mddev_lock_nointr(mddev);
5301
NeilBrowna4bd82d2010-03-29 13:23:10 +11005302 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005303 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005304 mddev->sync_thread ||
5305 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005306 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11005307 if (did_freeze) {
5308 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5309 md_wakeup_thread(mddev->thread);
5310 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11005311 err = -EBUSY;
5312 goto out;
5313 }
5314 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005315 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005316
5317 err = -ENXIO;
5318 if (mddev->ro==1)
5319 goto out;
5320 mddev->ro = 1;
5321 set_disk_ro(mddev->gendisk, 1);
5322 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005323 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11005324 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005325 }
5326out:
5327 mutex_unlock(&mddev->open_mutex);
5328 return err;
5329}
5330
NeilBrown9e653b62006-06-26 00:27:58 -07005331/* mode:
5332 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005333 * 2 - stop but do not disassemble array
5334 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005335static int do_md_stop(struct mddev * mddev, int mode,
5336 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005339 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11005340 int did_freeze = 0;
5341
5342 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5343 did_freeze = 1;
5344 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5345 md_wakeup_thread(mddev->thread);
5346 }
5347 if (mddev->sync_thread) {
5348 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5349 /* Thread might be blocked waiting for metadata update
5350 * which will now never happen */
5351 wake_up_process(mddev->sync_thread->tsk);
5352 }
5353 mddev_unlock(mddev);
5354 wait_event(resync_wait, mddev->sync_thread == NULL);
5355 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
NeilBrownc8c00a62009-08-10 12:50:52 +10005357 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10005358 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11005359 mddev->sysfs_active ||
5360 mddev->sync_thread ||
5361 (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005362 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005363 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11005364 if (did_freeze) {
5365 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5366 md_wakeup_thread(mddev->thread);
5367 }
NeilBrown260fa032013-08-27 16:44:13 +10005368 return -EBUSY;
5369 }
NeilBrown6e17b022010-08-07 21:41:19 +10005370 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005371 if (mddev->ro)
5372 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005373
NeilBrowndefad612011-01-14 09:14:33 +11005374 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11005375 __md_stop(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005376 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005377 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005378
NeilBrowna4bd82d2010-03-29 13:23:10 +11005379 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005380 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005381
NeilBrowndafb20f2012-03-19 12:46:39 +11005382 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005383 if (rdev->raid_disk >= 0)
5384 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005385
NeilBrowna4bd82d2010-03-29 13:23:10 +11005386 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005387 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005388 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005389 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005390
NeilBrowna4bd82d2010-03-29 13:23:10 +11005391 if (mddev->ro)
5392 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005393 } else
5394 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 /*
5396 * Free resources if final stop
5397 */
NeilBrown9e653b62006-06-26 00:27:58 -07005398 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5400
NeilBrown978f9462006-02-02 14:28:05 -08005401 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005402 if (mddev->bitmap_info.file) {
NeilBrownc3d97142009-12-14 12:49:52 +11005403 fput(mddev->bitmap_info.file);
5404 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005405 }
NeilBrownc3d97142009-12-14 12:49:52 +11005406 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 export_array(mddev);
5409
NeilBrown6177b472010-03-29 11:37:13 +11005410 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005411 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005412 if (mddev->hold_active == UNTIL_STOP)
5413 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005414 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005415 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005416 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005417 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005421#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005422static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
NeilBrown3cb03002011-10-11 16:45:26 +11005424 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 int err;
5426
NeilBrowna757e642005-04-16 15:26:42 -07005427 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 printk(KERN_INFO "md: running: ");
5431
NeilBrowndafb20f2012-03-19 12:46:39 +11005432 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 char b[BDEVNAME_SIZE];
5434 printk("<%s>", bdevname(rdev->bdev,b));
5435 }
5436 printk("\n");
5437
NeilBrownd710e132008-10-13 11:55:12 +11005438 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 if (err) {
5440 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005441 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
5443}
5444
5445/*
5446 * lets try to run arrays based on all disks that have arrived
5447 * until now. (those are in pending_raid_disks)
5448 *
5449 * the method: pick the first pending disk, collect all disks with
5450 * the same UUID, remove all from the pending list and put them into
5451 * the 'same_array' list. Then order this list based on superblock
5452 * update time (freshest comes first), kick out 'old' disks and
5453 * compare superblocks. If everything's fine then run it.
5454 *
5455 * If "unit" is allocated, then bump its reference count
5456 */
5457static void autorun_devices(int part)
5458{
NeilBrown3cb03002011-10-11 16:45:26 +11005459 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005460 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 char b[BDEVNAME_SIZE];
5462
5463 printk(KERN_INFO "md: autorun ...\n");
5464 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005465 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005467 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005469 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 printk(KERN_INFO "md: considering %s ...\n",
5472 bdevname(rdev0->bdev,b));
5473 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005474 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 if (super_90_load(rdev, rdev0, 0) >= 0) {
5476 printk(KERN_INFO "md: adding %s ...\n",
5477 bdevname(rdev->bdev,b));
5478 list_move(&rdev->same_set, &candidates);
5479 }
5480 /*
5481 * now we have a set of devices, with all of them having
5482 * mostly sane superblocks. It's time to allocate the
5483 * mddev.
5484 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005485 if (part) {
5486 dev = MKDEV(mdp_major,
5487 rdev0->preferred_minor << MdpMinorShift);
5488 unit = MINOR(dev) >> MdpMinorShift;
5489 } else {
5490 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5491 unit = MINOR(dev);
5492 }
5493 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5495 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5496 break;
5497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
5499 md_probe(dev, NULL, NULL);
5500 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005501 if (!mddev || !mddev->gendisk) {
5502 if (mddev)
5503 mddev_put(mddev);
5504 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 "md: cannot allocate memory for md drive.\n");
5506 break;
5507 }
5508 if (mddev_lock(mddev))
5509 printk(KERN_WARNING "md: %s locked, cannot run\n",
5510 mdname(mddev));
5511 else if (mddev->raid_disks || mddev->major_version
5512 || !list_empty(&mddev->disks)) {
5513 printk(KERN_WARNING
5514 "md: %s already running, cannot run %s\n",
5515 mdname(mddev), bdevname(rdev0->bdev,b));
5516 mddev_unlock(mddev);
5517 } else {
5518 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005519 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005520 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 list_del_init(&rdev->same_set);
5522 if (bind_rdev_to_array(rdev, mddev))
5523 export_rdev(rdev);
5524 }
5525 autorun_array(mddev);
5526 mddev_unlock(mddev);
5527 }
5528 /* on success, candidates will be empty, on error
5529 * it won't...
5530 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005531 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005532 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 mddev_put(mddev);
5536 }
5537 printk(KERN_INFO "md: ... autorun DONE.\n");
5538}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005539#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541static int get_version(void __user * arg)
5542{
5543 mdu_version_t ver;
5544
5545 ver.major = MD_MAJOR_VERSION;
5546 ver.minor = MD_MINOR_VERSION;
5547 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5548
5549 if (copy_to_user(arg, &ver, sizeof(ver)))
5550 return -EFAULT;
5551
5552 return 0;
5553}
5554
NeilBrownfd01b882011-10-11 16:47:53 +11005555static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556{
5557 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005558 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005559 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
NeilBrown1ca69c42012-10-11 13:37:33 +11005561 nr = working = insync = failed = spare = 0;
5562 rcu_read_lock();
5563 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005565 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 failed++;
5567 else {
5568 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005569 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005570 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 else
5572 spare++;
5573 }
5574 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005575 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 info.major_version = mddev->major_version;
5578 info.minor_version = mddev->minor_version;
5579 info.patch_version = MD_PATCHLEVEL_VERSION;
5580 info.ctime = mddev->ctime;
5581 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005582 info.size = mddev->dev_sectors / 2;
5583 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005584 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 info.nr_disks = nr;
5586 info.raid_disks = mddev->raid_disks;
5587 info.md_minor = mddev->md_minor;
5588 info.not_persistent= !mddev->persistent;
5589
5590 info.utime = mddev->utime;
5591 info.state = 0;
5592 if (mddev->in_sync)
5593 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005594 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10005595 info.state |= (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005596 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 info.working_disks = working;
5598 info.failed_disks = failed;
5599 info.spare_disks = spare;
5600
5601 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005602 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 if (copy_to_user(arg, &info, sizeof(info)))
5605 return -EFAULT;
5606
5607 return 0;
5608}
5609
NeilBrownfd01b882011-10-11 16:47:53 +11005610static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005611{
5612 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5613 char *ptr, *buf = NULL;
5614 int err = -ENOMEM;
5615
NeilBrown60559da2013-07-16 16:44:29 +10005616 file = kmalloc(sizeof(*file), GFP_NOIO);
NeilBrown2a2275d2007-01-26 00:57:11 -08005617
NeilBrown32a76272005-06-21 17:17:14 -07005618 if (!file)
5619 goto out;
5620
5621 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005622 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005623 file->pathname[0] = '\0';
5624 goto copy_out;
5625 }
5626
5627 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5628 if (!buf)
5629 goto out;
5630
NeilBrown1ec885c2012-05-22 13:55:10 +10005631 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5632 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005633 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005634 goto out;
5635
5636 strcpy(file->pathname, ptr);
5637
5638copy_out:
5639 err = 0;
5640 if (copy_to_user(arg, file, sizeof(*file)))
5641 err = -EFAULT;
5642out:
5643 kfree(buf);
5644 kfree(file);
5645 return err;
5646}
5647
NeilBrownfd01b882011-10-11 16:47:53 +11005648static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
5650 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005651 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 if (copy_from_user(&info, arg, sizeof(info)))
5654 return -EFAULT;
5655
NeilBrown1ca69c42012-10-11 13:37:33 +11005656 rcu_read_lock();
5657 rdev = find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 if (rdev) {
5659 info.major = MAJOR(rdev->bdev->bd_dev);
5660 info.minor = MINOR(rdev->bdev->bd_dev);
5661 info.raid_disk = rdev->raid_disk;
5662 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005663 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005665 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 info.state |= (1<<MD_DISK_ACTIVE);
5667 info.state |= (1<<MD_DISK_SYNC);
5668 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005669 if (test_bit(WriteMostly, &rdev->flags))
5670 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 } else {
5672 info.major = info.minor = 0;
5673 info.raid_disk = -1;
5674 info.state = (1<<MD_DISK_REMOVED);
5675 }
NeilBrown1ca69c42012-10-11 13:37:33 +11005676 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 if (copy_to_user(arg, &info, sizeof(info)))
5679 return -EFAULT;
5680
5681 return 0;
5682}
5683
NeilBrownfd01b882011-10-11 16:47:53 +11005684static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005687 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 dev_t dev = MKDEV(info->major,info->minor);
5689
5690 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5691 return -EOVERFLOW;
5692
5693 if (!mddev->raid_disks) {
5694 int err;
5695 /* expecting a device which has a superblock */
5696 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5697 if (IS_ERR(rdev)) {
5698 printk(KERN_WARNING
5699 "md: md_import_device returned %ld\n",
5700 PTR_ERR(rdev));
5701 return PTR_ERR(rdev);
5702 }
5703 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005704 struct md_rdev *rdev0
5705 = list_entry(mddev->disks.next,
5706 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005707 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 .load_super(rdev, rdev0, mddev->minor_version);
5709 if (err < 0) {
5710 printk(KERN_WARNING
5711 "md: %s has different UUID to %s\n",
5712 bdevname(rdev->bdev,b),
5713 bdevname(rdev0->bdev,b2));
5714 export_rdev(rdev);
5715 return -EINVAL;
5716 }
5717 }
5718 err = bind_rdev_to_array(rdev, mddev);
5719 if (err)
5720 export_rdev(rdev);
5721 return err;
5722 }
5723
5724 /*
5725 * add_new_disk can be used once the array is assembled
5726 * to add "hot spares". They must already have a superblock
5727 * written
5728 */
5729 if (mddev->pers) {
5730 int err;
5731 if (!mddev->pers->hot_add_disk) {
5732 printk(KERN_WARNING
5733 "%s: personality does not support diskops!\n",
5734 mdname(mddev));
5735 return -EINVAL;
5736 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005737 if (mddev->persistent)
5738 rdev = md_import_device(dev, mddev->major_version,
5739 mddev->minor_version);
5740 else
5741 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 if (IS_ERR(rdev)) {
5743 printk(KERN_WARNING
5744 "md: md_import_device returned %ld\n",
5745 PTR_ERR(rdev));
5746 return PTR_ERR(rdev);
5747 }
NeilBrown1a855a02010-12-09 16:36:28 +11005748 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005749 if (!mddev->persistent) {
5750 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005751 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005752 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005753 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11005754 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11005755 } else
NeilBrown41158c72005-06-21 17:17:25 -07005756 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11005757 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07005758 } else
5759 super_types[mddev->major_version].
5760 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005761 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005762 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005763 /* This was a hot-add request, but events doesn't
5764 * match, so reject it.
5765 */
5766 export_rdev(rdev);
5767 return -EINVAL;
5768 }
5769
NeilBrownb2d444d2005-11-08 21:39:31 -08005770 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005771 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5772 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005773 else
5774 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 rdev->raid_disk = -1;
5777 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005778 if (!err && !mddev->pers->hot_remove_disk) {
5779 /* If there is hot_add_disk but no hot_remove_disk
5780 * then added disks for geometry changes,
5781 * and should be added immediately.
5782 */
5783 super_types[mddev->major_version].
5784 validate_super(mddev, rdev);
5785 err = mddev->pers->hot_add_disk(mddev, rdev);
5786 if (err)
5787 unbind_rdev_from_array(rdev);
5788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 if (err)
5790 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005791 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005792 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005793
NeilBrown7ceb17e2013-04-24 11:42:42 +10005794 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Neil Brown72a23c22008-06-28 08:31:41 +10005795 if (mddev->degraded)
5796 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005798 if (!err)
5799 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005800 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 return err;
5802 }
5803
5804 /* otherwise, add_new_disk is only allowed
5805 * for major_version==0 superblocks
5806 */
5807 if (mddev->major_version != 0) {
5808 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5809 mdname(mddev));
5810 return -EINVAL;
5811 }
5812
5813 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5814 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005815 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 if (IS_ERR(rdev)) {
5817 printk(KERN_WARNING
5818 "md: error, md_import_device() returned %ld\n",
5819 PTR_ERR(rdev));
5820 return PTR_ERR(rdev);
5821 }
5822 rdev->desc_nr = info->number;
5823 if (info->raid_disk < mddev->raid_disks)
5824 rdev->raid_disk = info->raid_disk;
5825 else
5826 rdev->raid_disk = -1;
5827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005829 if (info->state & (1<<MD_DISK_SYNC))
5830 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005832 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5833 set_bit(WriteMostly, &rdev->flags);
5834
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 if (!mddev->persistent) {
5836 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005837 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5838 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005839 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005840 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
NeilBrown2bf071b2006-01-06 00:20:55 -08005842 err = bind_rdev_to_array(rdev, mddev);
5843 if (err) {
5844 export_rdev(rdev);
5845 return err;
5846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 }
5848
5849 return 0;
5850}
5851
NeilBrownfd01b882011-10-11 16:47:53 +11005852static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853{
5854 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005855 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 rdev = find_rdev(mddev, dev);
5858 if (!rdev)
5859 return -ENXIO;
5860
NeilBrown3ea8929d2013-04-24 11:42:41 +10005861 clear_bit(Blocked, &rdev->flags);
5862 remove_and_add_spares(mddev, rdev);
5863
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 if (rdev->raid_disk >= 0)
5865 goto busy;
5866
5867 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005868 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005869 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 return 0;
5872busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005873 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 bdevname(rdev->bdev,b), mdname(mddev));
5875 return -EBUSY;
5876}
5877
NeilBrownfd01b882011-10-11 16:47:53 +11005878static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879{
5880 char b[BDEVNAME_SIZE];
5881 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005882 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884 if (!mddev->pers)
5885 return -ENODEV;
5886
5887 if (mddev->major_version != 0) {
5888 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5889 " version-0 superblocks.\n",
5890 mdname(mddev));
5891 return -EINVAL;
5892 }
5893 if (!mddev->pers->hot_add_disk) {
5894 printk(KERN_WARNING
5895 "%s: personality does not support diskops!\n",
5896 mdname(mddev));
5897 return -EINVAL;
5898 }
5899
NeilBrownd710e132008-10-13 11:55:12 +11005900 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (IS_ERR(rdev)) {
5902 printk(KERN_WARNING
5903 "md: error, md_import_device() returned %ld\n",
5904 PTR_ERR(rdev));
5905 return -EINVAL;
5906 }
5907
5908 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005909 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005911 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
NeilBrown8190e752009-06-18 08:48:58 +10005913 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
NeilBrownb2d444d2005-11-08 21:39:31 -08005915 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 printk(KERN_WARNING
5917 "md: can not hot-add faulty %s disk to %s!\n",
5918 bdevname(rdev->bdev,b), mdname(mddev));
5919 err = -EINVAL;
5920 goto abort_export;
5921 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005922 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005924 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005925 err = bind_rdev_to_array(rdev, mddev);
5926 if (err)
5927 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 /*
5930 * The rest should better be atomic, we can have disk failures
5931 * noticed in interrupt contexts ...
5932 */
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 rdev->raid_disk = -1;
5935
NeilBrown850b2b42006-10-03 01:15:46 -07005936 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
5938 /*
5939 * Kick recovery, maybe this spare has to be added to the
5940 * array immediately.
5941 */
5942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5943 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005944 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return 0;
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947abort_export:
5948 export_rdev(rdev);
5949 return err;
5950}
5951
NeilBrownfd01b882011-10-11 16:47:53 +11005952static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005953{
NeilBrown035328c2014-04-09 12:25:40 +10005954 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07005955
NeilBrown36fa3062005-09-09 16:23:45 -07005956 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10005957 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07005958 return -EBUSY;
5959 if (mddev->recovery || mddev->sync_thread)
5960 return -EBUSY;
5961 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005962 }
5963
NeilBrown36fa3062005-09-09 16:23:45 -07005964
5965 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10005966 struct inode *inode;
NeilBrown36fa3062005-09-09 16:23:45 -07005967 if (mddev->bitmap)
5968 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005969 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005970
NeilBrownc3d97142009-12-14 12:49:52 +11005971 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005972 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5973 mdname(mddev));
5974 return -EBADF;
5975 }
5976
NeilBrown035328c2014-04-09 12:25:40 +10005977 inode = mddev->bitmap_info.file->f_mapping->host;
5978 if (!S_ISREG(inode->i_mode)) {
5979 printk(KERN_ERR "%s: error: bitmap file must be a regular file\n",
5980 mdname(mddev));
5981 err = -EBADF;
5982 } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) {
5983 printk(KERN_ERR "%s: error: bitmap file must open for write\n",
5984 mdname(mddev));
5985 err = -EBADF;
5986 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown36fa3062005-09-09 16:23:45 -07005987 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5988 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10005989 err = -EBUSY;
5990 }
5991 if (err) {
NeilBrownc3d97142009-12-14 12:49:52 +11005992 fput(mddev->bitmap_info.file);
5993 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005994 return err;
5995 }
NeilBrownc3d97142009-12-14 12:49:52 +11005996 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005997 } else if (mddev->bitmap == NULL)
5998 return -ENOENT; /* cannot remove what isn't there */
5999 err = 0;
6000 if (mddev->pers) {
6001 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10006002 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07006003 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006004 if (!err)
6005 err = bitmap_load(mddev);
6006 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006007 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07006008 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006009 fd = -1; /* make sure to put the file */
6010 }
NeilBrown36fa3062005-09-09 16:23:45 -07006011 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006012 }
6013 if (fd < 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006014 if (mddev->bitmap_info.file)
NeilBrownc3d97142009-12-14 12:49:52 +11006015 fput(mddev->bitmap_info.file);
NeilBrownc3d97142009-12-14 12:49:52 +11006016 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006017 }
6018
NeilBrown32a76272005-06-21 17:17:14 -07006019 return err;
6020}
6021
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022/*
6023 * set_array_info is used two different ways
6024 * The original usage is when creating a new array.
6025 * In this usage, raid_disks is > 0 and it together with
6026 * level, size, not_persistent,layout,chunksize determine the
6027 * shape of the array.
6028 * This will always create an array with a type-0.90.0 superblock.
6029 * The newer usage is when assembling an array.
6030 * In this case raid_disks will be 0, and the major_version field is
6031 * use to determine which style super-blocks are to be found on the devices.
6032 * The minor and patch _version numbers are also kept incase the
6033 * super_block handler wishes to interpret them.
6034 */
NeilBrownfd01b882011-10-11 16:47:53 +11006035static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
6037
6038 if (info->raid_disks == 0) {
6039 /* just setting version number for superblock loading */
6040 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006041 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 super_types[info->major_version].name == NULL) {
6043 /* maybe try to auto-load a module? */
6044 printk(KERN_INFO
6045 "md: superblock version %d not known\n",
6046 info->major_version);
6047 return -EINVAL;
6048 }
6049 mddev->major_version = info->major_version;
6050 mddev->minor_version = info->minor_version;
6051 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006052 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006053 /* ensure mddev_put doesn't delete this now that there
6054 * is some minimal configuration.
6055 */
6056 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 return 0;
6058 }
6059 mddev->major_version = MD_MAJOR_VERSION;
6060 mddev->minor_version = MD_MINOR_VERSION;
6061 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6062 mddev->ctime = get_seconds();
6063
6064 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006065 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006066 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 mddev->raid_disks = info->raid_disks;
6068 /* don't set md_minor, it is determined by which /dev/md* was
6069 * openned
6070 */
6071 if (info->state & (1<<MD_SB_CLEAN))
6072 mddev->recovery_cp = MaxSector;
6073 else
6074 mddev->recovery_cp = 0;
6075 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006076 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006079 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081 mddev->max_disks = MD_SB_DISKS;
6082
NeilBrowne6910632008-02-06 01:39:51 -08006083 if (mddev->persistent)
6084 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006085 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
NeilBrownc3d97142009-12-14 12:49:52 +11006087 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006088 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006089 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006090
NeilBrownf6705572006-03-27 01:18:11 -08006091 mddev->reshape_position = MaxSector;
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 /*
6094 * Generate a 128 bit UUID
6095 */
6096 get_random_bytes(mddev->uuid, 16);
6097
NeilBrownf6705572006-03-27 01:18:11 -08006098 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006099 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006100 mddev->new_layout = mddev->layout;
6101 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006102 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return 0;
6105}
6106
NeilBrownfd01b882011-10-11 16:47:53 +11006107void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006108{
Dan Williamsb522adc2009-03-31 15:00:31 +11006109 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6110
6111 if (mddev->external_size)
6112 return;
6113
Dan Williams1f403622009-03-31 14:59:03 +11006114 mddev->array_sectors = array_sectors;
6115}
6116EXPORT_SYMBOL(md_set_array_sectors);
6117
NeilBrownfd01b882011-10-11 16:47:53 +11006118static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006119{
NeilBrown3cb03002011-10-11 16:45:26 +11006120 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006121 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006122 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006123
6124 if (mddev->pers->resize == NULL)
6125 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006126 /* The "num_sectors" is the number of sectors of each device that
6127 * is used. This can only make sense for arrays with redundancy.
6128 * linear and raid0 always use whatever space is available. We can only
6129 * consider changing this number if no resync or reconstruction is
6130 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006131 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006132 * of each device. If num_sectors is zero, we find the largest size
6133 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006134 */
6135 if (mddev->sync_thread)
6136 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006137 if (mddev->ro)
6138 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006139
NeilBrowndafb20f2012-03-19 12:46:39 +11006140 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006141 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006142
Andre Nolld71f9f82008-07-11 22:02:22 +10006143 if (fit && (num_sectors == 0 || num_sectors > avail))
6144 num_sectors = avail;
6145 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006146 return -ENOSPC;
6147 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006148 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006149 if (!rv)
6150 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006151 return rv;
6152}
6153
NeilBrownfd01b882011-10-11 16:47:53 +11006154static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006155{
6156 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006157 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006158 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006159 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006160 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006161 if (mddev->ro)
6162 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006163 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006164 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006165 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006166 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006167 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006168
6169 rdev_for_each(rdev, mddev) {
6170 if (mddev->raid_disks < raid_disks &&
6171 rdev->data_offset < rdev->new_data_offset)
6172 return -EINVAL;
6173 if (mddev->raid_disks > raid_disks &&
6174 rdev->data_offset > rdev->new_data_offset)
6175 return -EINVAL;
6176 }
6177
NeilBrown63c70c42006-03-27 01:18:13 -08006178 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006179 if (mddev->delta_disks < 0)
6180 mddev->reshape_backwards = 1;
6181 else if (mddev->delta_disks > 0)
6182 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006183
6184 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006185 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006186 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006187 mddev->reshape_backwards = 0;
6188 }
NeilBrownda943b992006-01-06 00:20:54 -08006189 return rv;
6190}
6191
6192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193/*
6194 * update_array_info is used to change the configuration of an
6195 * on-line array.
6196 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6197 * fields in the info are checked against the array.
6198 * Any differences that cannot be handled will cause an error.
6199 * Normally, only one change can be managed at a time.
6200 */
NeilBrownfd01b882011-10-11 16:47:53 +11006201static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
6203 int rv = 0;
6204 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006205 int state = 0;
6206
6207 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006208 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006209 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 if (mddev->major_version != info->major_version ||
6212 mddev->minor_version != info->minor_version ||
6213/* mddev->patch_version != info->patch_version || */
6214 mddev->ctime != info->ctime ||
6215 mddev->level != info->level ||
6216/* mddev->layout != info->layout || */
6217 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006218 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006219 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6220 ((state^info->state) & 0xfffffe00)
6221 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 return -EINVAL;
6223 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006224 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6225 cnt++;
6226 if (mddev->raid_disks != info->raid_disks)
6227 cnt++;
6228 if (mddev->layout != info->layout)
6229 cnt++;
6230 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6231 cnt++;
6232 if (cnt == 0)
6233 return 0;
6234 if (cnt > 1)
6235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 if (mddev->layout != info->layout) {
6238 /* Change layout
6239 * we don't need to do anything at the md level, the
6240 * personality will take care of it all.
6241 */
NeilBrown50ac1682009-06-18 08:47:55 +10006242 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006244 else {
6245 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006246 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006247 if (rv)
6248 mddev->new_layout = mddev->layout;
6249 return rv;
6250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006252 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006253 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
NeilBrownda943b992006-01-06 00:20:54 -08006255 if (mddev->raid_disks != info->raid_disks)
6256 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
NeilBrown36fa3062005-09-09 16:23:45 -07006258 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006259 if (mddev->pers->quiesce == NULL || mddev->thread == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07006260 return -EINVAL;
6261 if (mddev->recovery || mddev->sync_thread)
6262 return -EBUSY;
6263 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6264 /* add the bitmap */
6265 if (mddev->bitmap)
6266 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006267 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006268 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006269 mddev->bitmap_info.offset =
6270 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006271 mddev->bitmap_info.space =
6272 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006273 mddev->pers->quiesce(mddev, 1);
6274 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006275 if (!rv)
6276 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006277 if (rv)
6278 bitmap_destroy(mddev);
6279 mddev->pers->quiesce(mddev, 0);
6280 } else {
6281 /* remove the bitmap */
6282 if (!mddev->bitmap)
6283 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006284 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006285 return -EINVAL;
6286 mddev->pers->quiesce(mddev, 1);
6287 bitmap_destroy(mddev);
6288 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006289 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006290 }
6291 }
NeilBrown850b2b42006-10-03 01:15:46 -07006292 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 return rv;
6294}
6295
NeilBrownfd01b882011-10-11 16:47:53 +11006296static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297{
NeilBrown3cb03002011-10-11 16:45:26 +11006298 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11006299 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
6301 if (mddev->pers == NULL)
6302 return -ENODEV;
6303
NeilBrown1ca69c42012-10-11 13:37:33 +11006304 rcu_read_lock();
6305 rdev = find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11006307 err = -ENODEV;
6308 else {
6309 md_error(mddev, rdev);
6310 if (!test_bit(Faulty, &rdev->flags))
6311 err = -EBUSY;
6312 }
6313 rcu_read_unlock();
6314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315}
6316
Andre Noll2f9618c2008-04-25 18:57:58 +02006317/*
6318 * We have a problem here : there is no easy way to give a CHS
6319 * virtual geometry. We currently pretend that we have a 2 heads
6320 * 4 sectors (with a BIG number of cylinders...). This drives
6321 * dosfs just mad... ;-)
6322 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006323static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6324{
NeilBrownfd01b882011-10-11 16:47:53 +11006325 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006326
6327 geo->heads = 2;
6328 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006329 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006330 return 0;
6331}
6332
Nicolas Schichancb335f82014-01-15 16:58:52 +01006333static inline bool md_ioctl_valid(unsigned int cmd)
6334{
6335 switch (cmd) {
6336 case ADD_NEW_DISK:
6337 case BLKROSET:
6338 case GET_ARRAY_INFO:
6339 case GET_BITMAP_FILE:
6340 case GET_DISK_INFO:
6341 case HOT_ADD_DISK:
6342 case HOT_REMOVE_DISK:
6343 case PRINT_RAID_DEBUG:
6344 case RAID_AUTORUN:
6345 case RAID_VERSION:
6346 case RESTART_ARRAY_RW:
6347 case RUN_ARRAY:
6348 case SET_ARRAY_INFO:
6349 case SET_BITMAP_FILE:
6350 case SET_DISK_FAULTY:
6351 case STOP_ARRAY:
6352 case STOP_ARRAY_RO:
6353 return true;
6354 default:
6355 return false;
6356 }
6357}
6358
Al Viroa39907f2008-03-02 10:31:15 -05006359static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 unsigned int cmd, unsigned long arg)
6361{
6362 int err = 0;
6363 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006364 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006365 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
Nicolas Schichancb335f82014-01-15 16:58:52 +01006367 if (!md_ioctl_valid(cmd))
6368 return -ENOTTY;
6369
NeilBrown506c9e42011-12-23 10:17:26 +11006370 switch (cmd) {
6371 case RAID_VERSION:
6372 case GET_ARRAY_INFO:
6373 case GET_DISK_INFO:
6374 break;
6375 default:
6376 if (!capable(CAP_SYS_ADMIN))
6377 return -EACCES;
6378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
6380 /*
6381 * Commands dealing with the RAID driver but not any
6382 * particular array:
6383 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006384 switch (cmd) {
6385 case RAID_VERSION:
6386 err = get_version(argp);
6387 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
NeilBrownc02c0ae2012-12-11 13:39:21 +11006389 case PRINT_RAID_DEBUG:
6390 err = 0;
6391 md_print_devices();
6392 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
6394#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11006395 case RAID_AUTORUN:
6396 err = 0;
6397 autostart_arrays(arg);
6398 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11006400 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 }
6402
6403 /*
6404 * Commands creating/starting a new array:
6405 */
6406
Al Viroa39907f2008-03-02 10:31:15 -05006407 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
6409 if (!mddev) {
6410 BUG();
6411 goto abort;
6412 }
6413
NeilBrown1ca69c42012-10-11 13:37:33 +11006414 /* Some actions do not requires the mutex */
6415 switch (cmd) {
6416 case GET_ARRAY_INFO:
6417 if (!mddev->raid_disks && !mddev->external)
6418 err = -ENODEV;
6419 else
6420 err = get_array_info(mddev, argp);
6421 goto abort;
6422
6423 case GET_DISK_INFO:
6424 if (!mddev->raid_disks && !mddev->external)
6425 err = -ENODEV;
6426 else
6427 err = get_disk_info(mddev, argp);
6428 goto abort;
6429
6430 case SET_DISK_FAULTY:
6431 err = set_disk_faulty(mddev, new_decode_dev(arg));
6432 goto abort;
6433 }
6434
NeilBrowna7a3f082012-12-11 13:35:54 +11006435 if (cmd == ADD_NEW_DISK)
6436 /* need to ensure md_delayed_delete() has completed */
6437 flush_workqueue(md_misc_wq);
6438
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02006439 if (cmd == HOT_REMOVE_DISK)
6440 /* need to ensure recovery thread has run */
6441 wait_event_interruptible_timeout(mddev->sb_wait,
6442 !test_bit(MD_RECOVERY_NEEDED,
6443 &mddev->flags),
6444 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10006445 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6446 /* Need to flush page cache, and ensure no-one else opens
6447 * and writes
6448 */
6449 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006450 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10006451 mutex_unlock(&mddev->open_mutex);
6452 err = -EBUSY;
6453 goto abort;
6454 }
6455 set_bit(MD_STILL_CLOSED, &mddev->flags);
6456 mutex_unlock(&mddev->open_mutex);
6457 sync_blockdev(bdev);
6458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 err = mddev_lock(mddev);
6460 if (err) {
6461 printk(KERN_INFO
6462 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6463 err, cmd);
6464 goto abort;
6465 }
6466
NeilBrownc02c0ae2012-12-11 13:39:21 +11006467 if (cmd == SET_ARRAY_INFO) {
6468 mdu_array_info_t info;
6469 if (!arg)
6470 memset(&info, 0, sizeof(info));
6471 else if (copy_from_user(&info, argp, sizeof(info))) {
6472 err = -EFAULT;
6473 goto abort_unlock;
6474 }
6475 if (mddev->pers) {
6476 err = update_array_info(mddev, &info);
6477 if (err) {
6478 printk(KERN_WARNING "md: couldn't update"
6479 " array info. %d\n", err);
6480 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
6482 goto done_unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11006483 }
6484 if (!list_empty(&mddev->disks)) {
6485 printk(KERN_WARNING
6486 "md: array %s already has disks!\n",
6487 mdname(mddev));
6488 err = -EBUSY;
6489 goto abort_unlock;
6490 }
6491 if (mddev->raid_disks) {
6492 printk(KERN_WARNING
6493 "md: array %s already initialised!\n",
6494 mdname(mddev));
6495 err = -EBUSY;
6496 goto abort_unlock;
6497 }
6498 err = set_array_info(mddev, &info);
6499 if (err) {
6500 printk(KERN_WARNING "md: couldn't set"
6501 " array info. %d\n", err);
6502 goto abort_unlock;
6503 }
6504 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 }
6506
6507 /*
6508 * Commands querying/configuring an existing array:
6509 */
NeilBrown32a76272005-06-21 17:17:14 -07006510 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006511 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006512 if ((!mddev->raid_disks && !mddev->external)
6513 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6514 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6515 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 err = -ENODEV;
6517 goto abort_unlock;
6518 }
6519
6520 /*
6521 * Commands even a read-only array can execute:
6522 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11006523 switch (cmd) {
6524 case GET_BITMAP_FILE:
6525 err = get_bitmap_file(mddev, argp);
6526 goto done_unlock;
6527
6528 case RESTART_ARRAY_RW:
6529 err = restart_array(mddev);
6530 goto done_unlock;
6531
6532 case STOP_ARRAY:
6533 err = do_md_stop(mddev, 0, bdev);
6534 goto done_unlock;
6535
6536 case STOP_ARRAY_RO:
6537 err = md_set_readonly(mddev, bdev);
6538 goto done_unlock;
6539
NeilBrown3ea8929d2013-04-24 11:42:41 +10006540 case HOT_REMOVE_DISK:
6541 err = hot_remove_disk(mddev, new_decode_dev(arg));
6542 goto done_unlock;
6543
NeilBrown7ceb17e2013-04-24 11:42:42 +10006544 case ADD_NEW_DISK:
6545 /* We can support ADD_NEW_DISK on read-only arrays
6546 * on if we are re-adding a preexisting device.
6547 * So require mddev->pers and MD_DISK_SYNC.
6548 */
6549 if (mddev->pers) {
6550 mdu_disk_info_t info;
6551 if (copy_from_user(&info, argp, sizeof(info)))
6552 err = -EFAULT;
6553 else if (!(info.state & (1<<MD_DISK_SYNC)))
6554 /* Need to clear read-only for this */
6555 break;
6556 else
6557 err = add_new_disk(mddev, &info);
6558 goto done_unlock;
6559 }
6560 break;
6561
NeilBrownc02c0ae2012-12-11 13:39:21 +11006562 case BLKROSET:
6563 if (get_user(ro, (int __user *)(arg))) {
6564 err = -EFAULT;
6565 goto done_unlock;
6566 }
6567 err = -EINVAL;
6568
6569 /* if the bdev is going readonly the value of mddev->ro
6570 * does not matter, no writes are coming
6571 */
6572 if (ro)
NeilBrown32a76272005-06-21 17:17:14 -07006573 goto done_unlock;
6574
NeilBrownc02c0ae2012-12-11 13:39:21 +11006575 /* are we are already prepared for writes? */
6576 if (mddev->ro != 1)
6577 goto done_unlock;
6578
6579 /* transitioning to readauto need only happen for
6580 * arrays that call md_write_start
6581 */
6582 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11006584 if (err == 0) {
6585 mddev->ro = 2;
6586 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10006587 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11006588 }
6589 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 }
6591
6592 /*
6593 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006594 * superblock, so we do not allow them on read-only arrays.
6595 * However non-MD ioctls (e.g. get-size) will still come through
6596 * here and hit the 'default' below, so only disallow
6597 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006599 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006600 if (mddev->ro == 2) {
6601 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006602 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006603 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11006604 /* mddev_unlock will wake thread */
6605 /* If a device failed while we were read-only, we
6606 * need to make sure the metadata is updated now.
6607 */
6608 if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6609 mddev_unlock(mddev);
6610 wait_event(mddev->sb_wait,
6611 !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6612 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown29f097c2013-11-14 17:54:51 +11006613 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11006614 }
NeilBrownf91de922005-11-08 21:39:36 -08006615 } else {
6616 err = -EROFS;
6617 goto abort_unlock;
6618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 }
6620
NeilBrownc02c0ae2012-12-11 13:39:21 +11006621 switch (cmd) {
6622 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11006624 mdu_disk_info_t info;
6625 if (copy_from_user(&info, argp, sizeof(info)))
6626 err = -EFAULT;
6627 else
6628 err = add_new_disk(mddev, &info);
6629 goto done_unlock;
6630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
NeilBrownc02c0ae2012-12-11 13:39:21 +11006632 case HOT_ADD_DISK:
6633 err = hot_add_disk(mddev, new_decode_dev(arg));
6634 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
NeilBrownc02c0ae2012-12-11 13:39:21 +11006636 case RUN_ARRAY:
6637 err = do_md_run(mddev);
6638 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
NeilBrownc02c0ae2012-12-11 13:39:21 +11006640 case SET_BITMAP_FILE:
6641 err = set_bitmap_file(mddev, (int)arg);
6642 goto done_unlock;
NeilBrown32a76272005-06-21 17:17:14 -07006643
NeilBrownc02c0ae2012-12-11 13:39:21 +11006644 default:
6645 err = -EINVAL;
6646 goto abort_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 }
6648
6649done_unlock:
6650abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006651 if (mddev->hold_active == UNTIL_IOCTL &&
6652 err != -EINVAL)
6653 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 mddev_unlock(mddev);
6655
6656 return err;
6657done:
6658 if (err)
6659 MD_BUG();
6660abort:
6661 return err;
6662}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006663#ifdef CONFIG_COMPAT
6664static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6665 unsigned int cmd, unsigned long arg)
6666{
6667 switch (cmd) {
6668 case HOT_REMOVE_DISK:
6669 case HOT_ADD_DISK:
6670 case SET_DISK_FAULTY:
6671 case SET_BITMAP_FILE:
6672 /* These take in integer arg, do not convert */
6673 break;
6674 default:
6675 arg = (unsigned long)compat_ptr(arg);
6676 break;
6677 }
6678
6679 return md_ioctl(bdev, mode, cmd, arg);
6680}
6681#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682
Al Viroa39907f2008-03-02 10:31:15 -05006683static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684{
6685 /*
6686 * Succeed if we can lock the mddev, which confirms that
6687 * it isn't being stopped right now.
6688 */
NeilBrownfd01b882011-10-11 16:47:53 +11006689 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 int err;
6691
Yuanhan Liu0c098222012-05-22 13:55:32 +10006692 if (!mddev)
6693 return -ENODEV;
6694
NeilBrownd3374822009-01-09 08:31:10 +11006695 if (mddev->gendisk != bdev->bd_disk) {
6696 /* we are racing with mddev_put which is discarding this
6697 * bd_disk.
6698 */
6699 mddev_put(mddev);
6700 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006701 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006702 /* Then retry the open from the top */
6703 return -ERESTARTSYS;
6704 }
6705 BUG_ON(mddev != bdev->bd_disk->private_data);
6706
NeilBrownc8c00a62009-08-10 12:50:52 +10006707 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 goto out;
6709
6710 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006711 atomic_inc(&mddev->openers);
NeilBrown260fa032013-08-27 16:44:13 +10006712 clear_bit(MD_STILL_CLOSED, &mddev->flags);
NeilBrownc8c00a62009-08-10 12:50:52 +10006713 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006715 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 out:
6717 return err;
6718}
6719
Al Virodb2a1442013-05-05 21:52:57 -04006720static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721{
NeilBrownfd01b882011-10-11 16:47:53 +11006722 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006724 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006725 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006728
6729static int md_media_changed(struct gendisk *disk)
6730{
NeilBrownfd01b882011-10-11 16:47:53 +11006731 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006732
6733 return mddev->changed;
6734}
6735
6736static int md_revalidate(struct gendisk *disk)
6737{
NeilBrownfd01b882011-10-11 16:47:53 +11006738 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006739
6740 mddev->changed = 0;
6741 return 0;
6742}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006743static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744{
6745 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006746 .open = md_open,
6747 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006748 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006749#ifdef CONFIG_COMPAT
6750 .compat_ioctl = md_compat_ioctl,
6751#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006752 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006753 .media_changed = md_media_changed,
6754 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755};
6756
Adrian Bunk75c96f82005-05-05 16:16:09 -07006757static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758{
NeilBrown2b8bf342011-10-11 16:48:23 +11006759 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 /*
6762 * md_thread is a 'system-thread', it's priority should be very
6763 * high. We avoid resource deadlocks individually in each
6764 * raid personality. (RAID5 does preallocation) We also use RR and
6765 * the very same RT priority as kswapd, thus we will never get
6766 * into a priority inversion deadlock.
6767 *
6768 * we definitely have to have equal or higher priority than
6769 * bdflush, otherwise bdflush will deadlock if there are too
6770 * many dirty RAID5 blocks.
6771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
NeilBrown6985c432005-10-19 21:23:47 -07006773 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006774 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
NeilBrown93588e22005-11-15 00:09:12 -08006776 /* We need to wait INTERRUPTIBLE so that
6777 * we don't add to the load-average.
6778 * That means we need to be sure no signals are
6779 * pending
6780 */
6781 if (signal_pending(current))
6782 flush_signals(current);
6783
6784 wait_event_interruptible_timeout
6785 (thread->wqueue,
6786 test_bit(THREAD_WAKEUP, &thread->flags)
6787 || kthread_should_stop(),
6788 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
NeilBrown6c987912011-01-14 09:13:53 +11006790 clear_bit(THREAD_WAKEUP, &thread->flags);
6791 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11006792 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006794
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 return 0;
6796}
6797
NeilBrown2b8bf342011-10-11 16:48:23 +11006798void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799{
6800 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006801 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 set_bit(THREAD_WAKEUP, &thread->flags);
6803 wake_up(&thread->wqueue);
6804 }
6805}
6806
Shaohua Li4ed87312012-10-11 13:34:00 +11006807struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6808 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809{
NeilBrown2b8bf342011-10-11 16:48:23 +11006810 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
NeilBrown2b8bf342011-10-11 16:48:23 +11006812 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 if (!thread)
6814 return NULL;
6815
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 init_waitqueue_head(&thread->wqueue);
6817
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 thread->run = run;
6819 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006820 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006821 thread->tsk = kthread_run(md_thread, thread,
6822 "%s_%s",
6823 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006824 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006825 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 kfree(thread);
6827 return NULL;
6828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 return thread;
6830}
6831
NeilBrown2b8bf342011-10-11 16:48:23 +11006832void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833{
NeilBrown2b8bf342011-10-11 16:48:23 +11006834 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006835 if (!thread)
6836 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006837 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006838 /* Locking ensures that mddev_unlock does not wake_up a
6839 * non-existent thread
6840 */
6841 spin_lock(&pers_lock);
6842 *threadp = NULL;
6843 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006844
6845 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 kfree(thread);
6847}
6848
NeilBrownfd01b882011-10-11 16:47:53 +11006849void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850{
6851 if (!mddev) {
6852 MD_BUG();
6853 return;
6854 }
6855
NeilBrownb2d444d2005-11-08 21:39:31 -08006856 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006858
NeilBrownde393cd2011-07-28 11:31:48 +10006859 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 return;
6861 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006862 if (mddev->degraded)
6863 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006864 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6866 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6867 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006868 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006869 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006870 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871}
6872
6873/* seq_file implementation /proc/mdstat */
6874
6875static void status_unused(struct seq_file *seq)
6876{
6877 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006878 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
6880 seq_printf(seq, "unused devices: ");
6881
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006882 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 char b[BDEVNAME_SIZE];
6884 i++;
6885 seq_printf(seq, "%s ",
6886 bdevname(rdev->bdev,b));
6887 }
6888 if (!i)
6889 seq_printf(seq, "<none>");
6890
6891 seq_printf(seq, "\n");
6892}
6893
6894
NeilBrownfd01b882011-10-11 16:47:53 +11006895static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
NeilBrowndd71cf62009-05-07 12:49:35 +10006897 sector_t max_sectors, resync, res;
6898 unsigned long dt, db;
6899 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006900 int scale;
6901 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
NeilBrown72f36d52012-10-11 14:25:57 +11006903 if (mddev->curr_resync <= 3)
6904 resync = 0;
6905 else
6906 resync = mddev->curr_resync
6907 - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
NeilBrownc804cde2012-05-21 09:28:33 +10006909 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6910 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006911 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006913 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915 /*
6916 * Should not happen.
6917 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006918 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 MD_BUG();
6920 return;
6921 }
NeilBrown4588b422006-03-27 01:18:04 -08006922 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006923 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006924 * u32, as those are the requirements for sector_div.
6925 * Thus 'scale' must be at least 10
6926 */
6927 scale = 10;
6928 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006929 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006930 scale++;
6931 }
6932 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006933 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006934
6935 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 {
NeilBrown4588b422006-03-27 01:18:04 -08006937 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 seq_printf(seq, "[");
6939 for (i = 0; i < x; i++)
6940 seq_printf(seq, "=");
6941 seq_printf(seq, ">");
6942 for (i = 0; i < y; i++)
6943 seq_printf(seq, ".");
6944 seq_printf(seq, "] ");
6945 }
NeilBrown4588b422006-03-27 01:18:04 -08006946 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006947 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6948 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006949 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6950 "check" :
6951 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6952 "resync" : "recovery"))),
6953 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006954 (unsigned long long) resync/2,
6955 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
6957 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 * dt: time from mark until now
6959 * db: blocks written from mark until now
6960 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006961 *
6962 * rt is a sector_t, so could be 32bit or 64bit.
6963 * So we divide before multiply in case it is 32bit and close
6964 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006965 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006966 * near the end of resync when the number of remaining sectors
6967 * is close to 'db'.
6968 * We then divide rt by 32 after multiplying by db to compensate.
6969 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 */
6971 dt = ((jiffies - mddev->resync_mark) / HZ);
6972 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006973 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6974 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
NeilBrowndd71cf62009-05-07 12:49:35 +10006976 rt = max_sectors - resync; /* number of remaining sectors */
6977 sector_div(rt, db/32+1);
6978 rt *= dt;
6979 rt >>= 5;
6980
6981 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6982 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983
NeilBrownff4e8d92006-07-10 04:44:16 -07006984 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985}
6986
6987static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6988{
6989 struct list_head *tmp;
6990 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006991 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
6993 if (l >= 0x10000)
6994 return NULL;
6995 if (!l--)
6996 /* header */
6997 return (void*)1;
6998
6999 spin_lock(&all_mddevs_lock);
7000 list_for_each(tmp,&all_mddevs)
7001 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007002 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 mddev_get(mddev);
7004 spin_unlock(&all_mddevs_lock);
7005 return mddev;
7006 }
7007 spin_unlock(&all_mddevs_lock);
7008 if (!l--)
7009 return (void*)2;/* tail */
7010 return NULL;
7011}
7012
7013static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7014{
7015 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007016 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
7018 ++*pos;
7019 if (v == (void*)2)
7020 return NULL;
7021
7022 spin_lock(&all_mddevs_lock);
7023 if (v == (void*)1)
7024 tmp = all_mddevs.next;
7025 else
7026 tmp = mddev->all_mddevs.next;
7027 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007028 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 else {
7030 next_mddev = (void*)2;
7031 *pos = 0x10000;
7032 }
7033 spin_unlock(&all_mddevs_lock);
7034
7035 if (v != (void*)1)
7036 mddev_put(mddev);
7037 return next_mddev;
7038
7039}
7040
7041static void md_seq_stop(struct seq_file *seq, void *v)
7042{
NeilBrownfd01b882011-10-11 16:47:53 +11007043 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045 if (mddev && v != (void*)1 && v != (void*)2)
7046 mddev_put(mddev);
7047}
7048
7049static int md_seq_show(struct seq_file *seq, void *v)
7050{
NeilBrownfd01b882011-10-11 16:47:53 +11007051 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007052 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007053 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
7055 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007056 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 seq_printf(seq, "Personalities : ");
7058 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007059 list_for_each_entry(pers, &pers_list, list)
7060 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062 spin_unlock(&pers_lock);
7063 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007064 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 return 0;
7066 }
7067 if (v == (void*)2) {
7068 status_unused(seq);
7069 return 0;
7070 }
7071
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007072 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07007074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7076 seq_printf(seq, "%s : %sactive", mdname(mddev),
7077 mddev->pers ? "" : "in");
7078 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007079 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007081 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007082 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 seq_printf(seq, " %s", mddev->pers->name);
7084 }
7085
Andre Nolldd8ac332009-03-31 14:33:13 +11007086 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11007087 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 char b[BDEVNAME_SIZE];
7089 seq_printf(seq, " %s[%d]",
7090 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007091 if (test_bit(WriteMostly, &rdev->flags))
7092 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007093 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 seq_printf(seq, "(F)");
7095 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007096 }
7097 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007098 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007099 if (test_bit(Replacement, &rdev->flags))
7100 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007101 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 }
7103
7104 if (!list_empty(&mddev->disks)) {
7105 if (mddev->pers)
7106 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007107 (unsigned long long)
7108 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 else
7110 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007111 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007113 if (mddev->persistent) {
7114 if (mddev->major_version != 0 ||
7115 mddev->minor_version != 90) {
7116 seq_printf(seq," super %d.%d",
7117 mddev->major_version,
7118 mddev->minor_version);
7119 }
NeilBrowne6910632008-02-06 01:39:51 -08007120 } else if (mddev->external)
7121 seq_printf(seq, " super external:%s",
7122 mddev->metadata_type);
7123 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007124 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
7126 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007127 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007129 if (mddev->pers->sync_request) {
7130 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007131 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007132 seq_printf(seq, "\n ");
NeilBrown72f36d52012-10-11 14:25:57 +11007133 } else if (mddev->curr_resync >= 1)
NeilBrown8e1b39d2005-11-08 21:39:41 -08007134 seq_printf(seq, "\tresync=DELAYED\n ");
7135 else if (mddev->recovery_cp < MaxSector)
7136 seq_printf(seq, "\tresync=PENDING\n ");
7137 }
NeilBrown32a76272005-06-21 17:17:14 -07007138 } else
7139 seq_printf(seq, "\n ");
7140
NeilBrown57148962012-03-19 12:46:40 +11007141 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
7143 seq_printf(seq, "\n");
7144 }
7145 mddev_unlock(mddev);
7146
7147 return 0;
7148}
7149
Jan Engelhardt110518b2009-05-07 12:49:37 +10007150static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 .start = md_seq_start,
7152 .next = md_seq_next,
7153 .stop = md_seq_stop,
7154 .show = md_seq_show,
7155};
7156
7157static int md_seq_open(struct inode *inode, struct file *file)
7158{
Kay Sieversf1514632011-07-12 20:48:39 +02007159 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 int error;
7161
7162 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007163 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007164 return error;
7165
7166 seq = file->private_data;
7167 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 return error;
7169}
7170
NeilBrowne2f23b62014-04-09 14:33:51 +10007171static int md_unloading;
NeilBrownd7603b72006-01-06 00:20:30 -08007172static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7173{
Kay Sieversf1514632011-07-12 20:48:39 +02007174 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007175 int mask;
7176
NeilBrowne2f23b62014-04-09 14:33:51 +10007177 if (md_unloading)
7178 return POLLIN|POLLRDNORM|POLLERR|POLLPRI;;
NeilBrownd7603b72006-01-06 00:20:30 -08007179 poll_wait(filp, &md_event_waiters, wait);
7180
7181 /* always allow read */
7182 mask = POLLIN | POLLRDNORM;
7183
Kay Sieversf1514632011-07-12 20:48:39 +02007184 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007185 mask |= POLLERR | POLLPRI;
7186 return mask;
7187}
7188
Arjan van de Venfa027c22007-02-12 00:55:33 -08007189static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007190 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 .open = md_seq_open,
7192 .read = seq_read,
7193 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007194 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007195 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196};
7197
NeilBrown84fc4b52011-10-11 16:49:58 +11007198int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007201 list_add_tail(&p->list, &pers_list);
7202 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 spin_unlock(&pers_lock);
7204 return 0;
7205}
7206
NeilBrown84fc4b52011-10-11 16:49:58 +11007207int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208{
NeilBrown2604b702006-01-06 00:20:36 -08007209 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007211 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 spin_unlock(&pers_lock);
7213 return 0;
7214}
7215
NeilBrownfd01b882011-10-11 16:47:53 +11007216static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217{
NeilBrown3cb03002011-10-11 16:45:26 +11007218 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007220 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
7222 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007223 rcu_read_lock();
7224 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007226 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7227 (int)part_stat_read(&disk->part0, sectors[1]) -
7228 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007229 /* sync IO will cause sync_io to increase before the disk_stats
7230 * as sync_io is counted when a request starts, and
7231 * disk_stats is counted when it completes.
7232 * So resync activity will cause curr_events to be smaller than
7233 * when there was no such activity.
7234 * non-sync IO will cause disk_stat to increase without
7235 * increasing sync_io so curr_events will (eventually)
7236 * be larger than it was before. Once it becomes
7237 * substantially larger, the test below will cause
7238 * the array to appear non-idle, and resync will slow
7239 * down.
7240 * If there is a lot of outstanding resync activity when
7241 * we set last_event to curr_events, then all that activity
7242 * completing might cause the array to appear non-idle
7243 * and resync will be slowed down even though there might
7244 * not have been non-resync activity. This will only
7245 * happen once though. 'last_events' will soon reflect
7246 * the state where there is little or no outstanding
7247 * resync requests, and further resync activity will
7248 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007249 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007251 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 rdev->last_events = curr_events;
7253 idle = 0;
7254 }
7255 }
NeilBrown4b809912008-07-21 17:05:25 +10007256 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 return idle;
7258}
7259
NeilBrownfd01b882011-10-11 16:47:53 +11007260void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261{
7262 /* another "blocks" (512byte) blocks have been synced */
7263 atomic_sub(blocks, &mddev->recovery_active);
7264 wake_up(&mddev->recovery_wait);
7265 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007266 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08007267 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 md_wakeup_thread(mddev->thread);
7269 // stop recovery, signal do_sync ....
7270 }
7271}
7272
7273
NeilBrown06d91a52005-06-21 17:17:12 -07007274/* md_write_start(mddev, bi)
7275 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007276 * in superblock) before writing, schedule a superblock update
7277 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007278 */
NeilBrownfd01b882011-10-11 16:47:53 +11007279void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280{
Neil Brown0fd62b82008-06-28 08:31:36 +10007281 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007282 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007283 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007284
NeilBrownf91de922005-11-08 21:39:36 -08007285 BUG_ON(mddev->ro == 1);
7286 if (mddev->ro == 2) {
7287 /* need to switch to read/write */
7288 mddev->ro = 0;
7289 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7290 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007291 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007292 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007293 }
NeilBrown06d91a52005-06-21 17:17:12 -07007294 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007295 if (mddev->safemode == 1)
7296 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007297 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007298 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007299 if (mddev->in_sync) {
7300 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007301 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007302 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007303 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007304 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007305 }
NeilBrowna9701a32005-11-08 21:39:34 -08007306 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007307 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007308 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007309 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007310 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007311 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312}
7313
NeilBrownfd01b882011-10-11 16:47:53 +11007314void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315{
7316 if (atomic_dec_and_test(&mddev->writes_pending)) {
7317 if (mddev->safemode == 2)
7318 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007319 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7321 }
7322}
7323
NeilBrown2a2275d2007-01-26 00:57:11 -08007324/* md_allow_write(mddev)
7325 * Calling this ensures that the array is marked 'active' so that writes
7326 * may proceed without blocking. It is important to call this before
7327 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7328 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007329 *
7330 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7331 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007332 */
NeilBrownfd01b882011-10-11 16:47:53 +11007333int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007334{
7335 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007336 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007337 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007338 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007339 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007340 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007341
7342 spin_lock_irq(&mddev->write_lock);
7343 if (mddev->in_sync) {
7344 mddev->in_sync = 0;
7345 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007346 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007347 if (mddev->safemode_delay &&
7348 mddev->safemode == 0)
7349 mddev->safemode = 1;
7350 spin_unlock_irq(&mddev->write_lock);
7351 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007352 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007353 } else
7354 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007355
NeilBrown070dc6d2010-08-30 17:33:34 +10007356 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007357 return -EAGAIN;
7358 else
7359 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007360}
7361EXPORT_SYMBOL_GPL(md_allow_write);
7362
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363#define SYNC_MARKS 10
7364#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11007365#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11007366void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367{
Shaohua Li4ed87312012-10-11 13:34:00 +11007368 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11007369 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 unsigned int currspeed = 0,
7371 window;
Xiao Niac7e50a2014-08-07 09:37:41 -04007372 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11007374 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 sector_t mark_cnt[SYNC_MARKS];
7376 int last_mark,m;
7377 struct list_head *tmp;
7378 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007379 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007380 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007381 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10007382 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
7384 /* just incase thread restarts... */
7385 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7386 return;
NeilBrown3991b312014-05-28 13:39:23 +10007387 if (mddev->ro) {/* never try to sync a read-only array */
7388 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007389 return;
NeilBrown3991b312014-05-28 13:39:23 +10007390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
NeilBrown61df9d92006-10-03 01:15:57 -07007392 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007393 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007394 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007395 action = "check";
7396 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07007397 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007398 action = "repair";
7399 } else
NeilBrown61df9d92006-10-03 01:15:57 -07007400 desc = "resync";
7401 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7402 desc = "reshape";
7403 else
7404 desc = "recovery";
7405
Jonathan Brassowc4a39552013-06-25 01:23:59 -05007406 mddev->last_sync_action = action ?: desc;
7407
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 /* we overload curr_resync somewhat here.
7409 * 0 == not engaged in resync at all
7410 * 2 == checking that there is no conflict with another sync
7411 * 1 == like 2, but have yielded to allow conflicting resync to
7412 * commense
7413 * other == active in resync - this many blocks
7414 *
7415 * Before starting a resync we must have set curr_resync to
7416 * 2, and then checked that every "conflicting" array has curr_resync
7417 * less than ours. When we find one that is the same or higher
7418 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7419 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7420 * This will mean we have to start checking from the beginning again.
7421 *
7422 */
7423
7424 do {
7425 mddev->curr_resync = 2;
7426
7427 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007428 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007430 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 if (mddev2 == mddev)
7432 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007433 if (!mddev->parallel_resync
7434 && mddev2->curr_resync
7435 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 DEFINE_WAIT(wq);
7437 if (mddev < mddev2 && mddev->curr_resync == 2) {
7438 /* arbitrarily yield */
7439 mddev->curr_resync = 1;
7440 wake_up(&resync_wait);
7441 }
7442 if (mddev > mddev2 && mddev->curr_resync == 1)
7443 /* no need to wait here, we can wait the next
7444 * time 'round when curr_resync == 2
7445 */
7446 continue;
NeilBrown97441972008-09-19 11:49:54 +10007447 /* We need to wait 'interruptible' so as not to
7448 * contribute to the load average, and not to
7449 * be caught by 'softlockup'
7450 */
7451 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11007452 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07007453 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007454 printk(KERN_INFO "md: delaying %s of %s"
7455 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007457 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007459 if (signal_pending(current))
7460 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 schedule();
7462 finish_wait(&resync_wait, &wq);
7463 goto try_again;
7464 }
7465 finish_wait(&resync_wait, &wq);
7466 }
7467 }
7468 } while (mddev->curr_resync < 2);
7469
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007470 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007471 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007473 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 */
7475 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11007476 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007477 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007478 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7479 j = mddev->resync_min;
7480 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007481 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007482
NeilBrownccfcc3c2006-03-27 01:18:09 -08007483 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007484 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007485 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007487 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007488 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007489 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007490 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007491 if (rdev->raid_disk >= 0 &&
7492 !test_bit(Faulty, &rdev->flags) &&
7493 !test_bit(In_sync, &rdev->flags) &&
7494 rdev->recovery_offset < j)
7495 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007496 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10007497
7498 /* If there is a bitmap, we need to make sure all
7499 * writes that started before we added a spare
7500 * complete before we start doing a recovery.
7501 * Otherwise the write might complete and (via
7502 * bitmap_endwrite) set a bit in the bitmap after the
7503 * recovery has checked that bit and skipped that
7504 * region.
7505 */
7506 if (mddev->bitmap) {
7507 mddev->pers->quiesce(mddev, 1);
7508 mddev->pers->quiesce(mddev, 0);
7509 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511
NeilBrown61df9d92006-10-03 01:15:57 -07007512 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7513 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7514 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007515 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007516 "(but not more than %d KB/sec) for %s.\n",
7517 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518
NeilBrowneea1bf32009-03-31 14:27:02 +11007519 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007520
NeilBrown57afd892005-06-21 17:17:13 -07007521 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 for (m = 0; m < SYNC_MARKS; m++) {
7523 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007524 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 }
7526 last_mark = 0;
7527 mddev->resync_mark = mark[last_mark];
7528 mddev->resync_mark_cnt = mark_cnt[last_mark];
7529
7530 /*
7531 * Tune reconstruction:
7532 */
7533 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007534 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7535 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
7537 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 last_check = 0;
7539
7540 if (j>2) {
NeilBrownc91abf52013-11-19 12:02:01 +11007541 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007542 "md: resuming %s of %s from checkpoint.\n",
7543 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007545 } else
7546 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11007547 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11007548 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7549 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11007550 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
majianpeng7c2c57c2012-07-03 12:12:26 +10007552 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007554 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555
NeilBrown57afd892005-06-21 17:17:13 -07007556 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007557
NeilBrown7a91ee12009-05-26 12:57:21 +10007558 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7559 ((mddev->curr_resync > mddev->curr_resync_completed &&
7560 (mddev->curr_resync - mddev->curr_resync_completed)
7561 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11007562 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10007563 (j - mddev->curr_resync_completed)*2
7564 >= mddev->resync_max - mddev->curr_resync_completed
7565 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007566 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007567 wait_event(mddev->recovery_wait,
7568 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007569 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11007570 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7571 j > mddev->recovery_cp)
7572 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11007573 update_time = jiffies;
NeilBrown070dc6d2010-08-30 17:33:34 +10007574 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007575 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007576 }
NeilBrownacb180b2009-04-14 16:28:34 +10007577
NeilBrownc91abf52013-11-19 12:02:01 +11007578 while (j >= mddev->resync_max &&
7579 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10007580 /* As this condition is controlled by user-space,
7581 * we can block indefinitely, so use '_interruptible'
7582 * to avoid triggering warnings.
7583 */
7584 flush_signals(current); /* just in case */
7585 wait_event_interruptible(mddev->recovery_wait,
7586 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11007587 || test_bit(MD_RECOVERY_INTR,
7588 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10007589 }
NeilBrownacb180b2009-04-14 16:28:34 +10007590
NeilBrownc91abf52013-11-19 12:02:01 +11007591 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7592 break;
NeilBrownacb180b2009-04-14 16:28:34 +10007593
NeilBrown57afd892005-06-21 17:17:13 -07007594 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007595 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007596 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11007598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 }
NeilBrown57afd892005-06-21 17:17:13 -07007600
7601 if (!skipped) { /* actual IO requested */
7602 io_sectors += sectors;
7603 atomic_add(sectors, &mddev->recovery_active);
7604 }
7605
NeilBrowne875ece2011-07-28 11:39:24 +10007606 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7607 break;
7608
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 j += sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11007610 if (j > 2)
7611 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007612 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007613 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007614 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007615 * visible in /proc/mdstat
7616 */
7617 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007618
7619 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 continue;
7621
NeilBrown57afd892005-06-21 17:17:13 -07007622 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 repeat:
7624 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7625 /* step marks */
7626 int next = (last_mark+1) % SYNC_MARKS;
7627
7628 mddev->resync_mark = mark[next];
7629 mddev->resync_mark_cnt = mark_cnt[next];
7630 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007631 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 last_mark = next;
7633 }
7634
NeilBrownc91abf52013-11-19 12:02:01 +11007635 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637
7638 /*
7639 * this loop exits only if either when we are slower than
7640 * the 'hard' speed limit, or the system was IO-idle for
7641 * a jiffy.
7642 * the system might be non-idle CPU-wise, but we only care
7643 * about not overloading the IO subsystem. (things like an
7644 * e2fsck being done on the RAID array should execute fast)
7645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 cond_resched();
7647
Xiao Niac7e50a2014-08-07 09:37:41 -04007648 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
7649 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07007650 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
NeilBrown88202a02006-01-06 00:21:36 -08007652 if (currspeed > speed_min(mddev)) {
7653 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007654 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007655 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 goto repeat;
7657 }
7658 }
7659 }
NeilBrownc91abf52013-11-19 12:02:01 +11007660 printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7661 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7662 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 /*
7664 * this also signals 'finished resyncing' to md_stop
7665 */
majianpeng7c2c57c2012-07-03 12:12:26 +10007666 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7668
7669 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007670 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671
NeilBrowndfc70642008-05-23 13:04:39 -07007672 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007673 mddev->curr_resync > 2) {
7674 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7675 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7676 if (mddev->curr_resync >= mddev->recovery_cp) {
7677 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007678 "md: checkpointing %s of %s.\n",
7679 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08007680 if (test_bit(MD_RECOVERY_ERROR,
7681 &mddev->recovery))
7682 mddev->recovery_cp =
7683 mddev->curr_resync_completed;
7684 else
7685 mddev->recovery_cp =
7686 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007687 }
7688 } else
7689 mddev->recovery_cp = MaxSector;
7690 } else {
7691 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7692 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007693 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007694 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007695 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007696 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007697 !test_bit(Faulty, &rdev->flags) &&
7698 !test_bit(In_sync, &rdev->flags) &&
7699 rdev->recovery_offset < mddev->curr_resync)
7700 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007701 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007704 skip:
NeilBrown17571282006-12-10 02:20:52 -08007705 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
NeilBrownc07b70a2009-12-14 12:49:48 +11007707 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7708 /* We completed so min/max setting can be forgotten if used. */
7709 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7710 mddev->resync_min = 0;
7711 mddev->resync_max = MaxSector;
7712 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7713 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 mddev->curr_resync = 0;
7715 wake_up(&resync_wait);
7716 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7717 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719}
NeilBrown29269552006-03-27 01:18:10 -08007720EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721
NeilBrown746d3202013-04-24 11:42:41 +10007722static int remove_and_add_spares(struct mddev *mddev,
7723 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007724{
NeilBrown3cb03002011-10-11 16:45:26 +11007725 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007726 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007727 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007728
NeilBrowndafb20f2012-03-19 12:46:39 +11007729 rdev_for_each(rdev, mddev)
NeilBrown746d3202013-04-24 11:42:41 +10007730 if ((this == NULL || rdev == this) &&
7731 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007732 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007733 (test_bit(Faulty, &rdev->flags) ||
7734 ! test_bit(In_sync, &rdev->flags)) &&
7735 atomic_read(&rdev->nr_pending)==0) {
7736 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007737 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007738 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007739 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007740 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007741 }
7742 }
Jonathan Brassow90584fc2013-03-07 16:24:26 -06007743 if (removed && mddev->kobj.sd)
7744 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007745
NeilBrown746d3202013-04-24 11:42:41 +10007746 if (this)
7747 goto no_add;
7748
NeilBrowndafb20f2012-03-19 12:46:39 +11007749 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007750 if (rdev->raid_disk >= 0 &&
7751 !test_bit(In_sync, &rdev->flags) &&
7752 !test_bit(Faulty, &rdev->flags))
7753 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007754 if (rdev->raid_disk >= 0)
7755 continue;
7756 if (test_bit(Faulty, &rdev->flags))
7757 continue;
7758 if (mddev->ro &&
NeilBrown8313b8e2013-12-12 10:13:33 +11007759 ! (rdev->saved_raid_disk >= 0 &&
7760 !test_bit(Bitmap_sync, &rdev->flags)))
NeilBrown7ceb17e2013-04-24 11:42:42 +10007761 continue;
7762
NeilBrown7eb41882014-01-14 15:55:14 +11007763 if (rdev->saved_raid_disk < 0)
7764 rdev->recovery_offset = 0;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007765 if (mddev->pers->
7766 hot_add_disk(mddev, rdev) == 0) {
7767 if (sysfs_link_rdev(mddev, rdev))
7768 /* failure here is OK */;
7769 spares++;
7770 md_new_event(mddev);
7771 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrowndfc70642008-05-23 13:04:39 -07007772 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007773 }
NeilBrown746d3202013-04-24 11:42:41 +10007774no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10007775 if (removed)
7776 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007777 return spares;
7778}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007779
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780/*
7781 * This routine is regularly called by all per-raid-array threads to
7782 * deal with generic issues like resync and super-block update.
7783 * Raid personalities that don't have a thread (linear/raid0) do not
7784 * need this as they never do any recovery or update the superblock.
7785 *
7786 * It does not do any resync itself, but rather "forks" off other threads
7787 * to do that as needed.
7788 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7789 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007790 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 * and wakeups up this thread which will reap the thread and finish up.
7792 * This thread also removes any faulty devices (with nr_pending == 0).
7793 *
7794 * The overall approach is:
7795 * 1/ if the superblock needs updating, update it.
7796 * 2/ If a recovery thread is running, don't do anything else.
7797 * 3/ If recovery has finished, clean up, possibly marking spares active.
7798 * 4/ If there are any faulty devices, remove them.
7799 * 5/ If array is degraded, try to add spares devices
7800 * 6/ If array has spares or is not in-sync, start a resync thread.
7801 */
NeilBrownfd01b882011-10-11 16:47:53 +11007802void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007804 if (mddev->suspended)
7805 return;
7806
NeilBrown5f404022005-06-21 17:17:16 -07007807 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007808 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
NeilBrownfca4d842005-06-21 17:17:11 -07007810 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007811 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007812 printk(KERN_INFO "md: %s in immediate safe mode\n",
7813 mdname(mddev));
7814 mddev->safemode = 2;
7815 }
7816 flush_signals(current);
7817 }
7818
NeilBrownc89a8ee2008-08-05 15:54:13 +10007819 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7820 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 if ( ! (
NeilBrown142d44c2013-11-28 10:34:18 +11007822 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007824 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007825 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007826 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7827 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007828 ))
7829 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007830
NeilBrowndf5b89b2006-03-27 01:18:20 -08007831 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007832 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007833
NeilBrownc89a8ee2008-08-05 15:54:13 +10007834 if (mddev->ro) {
NeilBrown7ceb17e2013-04-24 11:42:42 +10007835 /* On a read-only array we can:
7836 * - remove failed devices
7837 * - add already-in_sync devices if the array itself
7838 * is in-sync.
7839 * As we only add devices that are already in-sync,
7840 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10007841 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10007842 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11007843 /* There is no thread, but we need to call
7844 * ->spare_active and clear saved_raid_disk
7845 */
NeilBrown2ac295a2014-05-29 11:40:03 +10007846 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11007847 md_reap_sync_thread(mddev);
7848 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownc89a8ee2008-08-05 15:54:13 +10007849 goto unlock;
7850 }
7851
NeilBrown31a59e32008-04-30 00:52:30 -07007852 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007853 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007854 spin_lock_irq(&mddev->write_lock);
7855 if (mddev->safemode &&
7856 !atomic_read(&mddev->writes_pending) &&
7857 !mddev->in_sync &&
7858 mddev->recovery_cp == MaxSector) {
7859 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007860 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007861 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007862 }
7863 if (mddev->safemode == 1)
7864 mddev->safemode = 0;
7865 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007866 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007867 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007868 }
NeilBrownfca4d842005-06-21 17:17:11 -07007869
NeilBrown7a0a5352013-08-27 16:28:23 +10007870 if (mddev->flags & MD_UPDATE_SB_FLAGS)
NeilBrown850b2b42006-10-03 01:15:46 -07007871 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007872
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7874 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7875 /* resync/recovery still happening */
7876 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7877 goto unlock;
7878 }
7879 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007880 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 goto unlock;
7882 }
Neil Brown72a23c22008-06-28 08:31:41 +10007883 /* Set RUNNING before clearing NEEDED to avoid
7884 * any transients in the value of "sync_action".
7885 */
NeilBrown72f36d52012-10-11 14:25:57 +11007886 mddev->curr_resync_completed = 0;
Neil Brown72a23c22008-06-28 08:31:41 +10007887 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007888 /* Clear some bits that don't mean anything, but
7889 * might be left set
7890 */
NeilBrown24dd4692005-11-08 21:39:26 -08007891 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7892 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893
NeilBrowned209582012-04-24 10:23:14 +10007894 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7895 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007896 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 /* no recovery is running.
7898 * remove any failed drives, then
7899 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11007900 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 * the personality to fail the re-add.
7902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007904 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007905 if (mddev->pers->check_reshape == NULL ||
7906 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007907 /* Cannot proceed */
7908 goto unlock;
7909 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007910 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10007911 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007912 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7913 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007914 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007915 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007916 } else if (mddev->recovery_cp < MaxSector) {
7917 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007918 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007919 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7920 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007922
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007924 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007925 /* We are adding a device or devices to an array
7926 * which has the bitmap stored on all devices.
7927 * So make sure all bitmap pages get written
7928 */
7929 bitmap_write_all(mddev->bitmap);
7930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 mddev->sync_thread = md_register_thread(md_do_sync,
7932 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007933 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934 if (!mddev->sync_thread) {
7935 printk(KERN_ERR "%s: could not start resync"
7936 " thread...\n",
7937 mdname(mddev));
7938 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007939 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7940 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7941 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7942 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7943 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007944 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007946 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007947 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 }
7949 unlock:
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007950 wake_up(&mddev->sb_wait);
7951
Neil Brown72a23c22008-06-28 08:31:41 +10007952 if (!mddev->sync_thread) {
7953 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7954 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7955 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007956 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007957 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959 mddev_unlock(mddev);
7960 }
7961}
7962
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007963void md_reap_sync_thread(struct mddev *mddev)
7964{
7965 struct md_rdev *rdev;
7966
7967 /* resync has finished, collect result */
7968 md_unregister_thread(&mddev->sync_thread);
NeilBrown30b8feb2013-11-14 15:16:17 +11007969 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007970 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7971 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7972 /* success...*/
7973 /* activate any spares */
7974 if (mddev->pers->spare_active(mddev)) {
7975 sysfs_notify(&mddev->kobj, NULL,
7976 "degraded");
7977 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7978 }
7979 }
7980 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7981 mddev->pers->finish_reshape)
7982 mddev->pers->finish_reshape(mddev);
7983
7984 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11007985 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007986 */
NeilBrownf4667222013-12-09 12:04:56 +11007987 if (!mddev->degraded)
7988 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10007989 rdev->saved_raid_disk = -1;
7990
7991 md_update_sb(mddev, 1);
7992 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7993 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7994 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7995 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7996 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7997 /* flag recovery needed just to double check */
7998 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7999 sysfs_notify_dirent_safe(mddev->sysfs_action);
8000 md_new_event(mddev);
8001 if (mddev->event_work.func)
8002 queue_work(md_misc_wq, &mddev->event_work);
8003}
8004
NeilBrownfd01b882011-10-11 16:47:53 +11008005void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07008006{
NeilBrown00bcb4a2010-06-01 19:37:23 +10008007 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07008008 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10008009 !test_bit(Blocked, &rdev->flags) &&
8010 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07008011 msecs_to_jiffies(5000));
8012 rdev_dec_pending(rdev, mddev);
8013}
8014EXPORT_SYMBOL(md_wait_for_blocked_rdev);
8015
NeilBrownc6563a82012-05-21 09:27:00 +10008016void md_finish_reshape(struct mddev *mddev)
8017{
8018 /* called be personality module when reshape completes. */
8019 struct md_rdev *rdev;
8020
8021 rdev_for_each(rdev, mddev) {
8022 if (rdev->data_offset > rdev->new_data_offset)
8023 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
8024 else
8025 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
8026 rdev->data_offset = rdev->new_data_offset;
8027 }
8028}
8029EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10008030
8031/* Bad block management.
8032 * We can record which blocks on each device are 'bad' and so just
8033 * fail those blocks, or that stripe, rather than the whole device.
8034 * Entries in the bad-block table are 64bits wide. This comprises:
8035 * Length of bad-range, in sectors: 0-511 for lengths 1-512
8036 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
8037 * A 'shift' can be set so that larger blocks are tracked and
8038 * consequently larger devices can be covered.
8039 * 'Acknowledged' flag - 1 bit. - the most significant bit.
8040 *
8041 * Locking of the bad-block table uses a seqlock so md_is_badblock
8042 * might need to retry if it is very unlucky.
8043 * We will sometimes want to check for bad blocks in a bi_end_io function,
8044 * so we use the write_seqlock_irq variant.
8045 *
8046 * When looking for a bad block we specify a range and want to
8047 * know if any block in the range is bad. So we binary-search
8048 * to the last range that starts at-or-before the given endpoint,
8049 * (or "before the sector after the target range")
8050 * then see if it ends after the given start.
8051 * We return
8052 * 0 if there are no known bad blocks in the range
8053 * 1 if there are known bad block which are all acknowledged
8054 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8055 * plus the start/length of the first bad section we overlap.
8056 */
8057int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8058 sector_t *first_bad, int *bad_sectors)
8059{
8060 int hi;
majianpengab056132012-11-06 17:13:44 +08008061 int lo;
NeilBrown2230dfe2011-07-28 11:31:46 +10008062 u64 *p = bb->page;
majianpengab056132012-11-06 17:13:44 +08008063 int rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10008064 sector_t target = s + sectors;
8065 unsigned seq;
8066
8067 if (bb->shift > 0) {
8068 /* round the start down, and the end up */
8069 s >>= bb->shift;
8070 target += (1<<bb->shift) - 1;
8071 target >>= bb->shift;
8072 sectors = target - s;
8073 }
8074 /* 'target' is now the first block after the bad range */
8075
8076retry:
8077 seq = read_seqbegin(&bb->lock);
majianpengab056132012-11-06 17:13:44 +08008078 lo = 0;
8079 rv = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008080 hi = bb->count;
8081
8082 /* Binary search between lo and hi for 'target'
8083 * i.e. for the last range that starts before 'target'
8084 */
8085 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8086 * are known not to be the last range before target.
8087 * VARIANT: hi-lo is the number of possible
8088 * ranges, and decreases until it reaches 1
8089 */
8090 while (hi - lo > 1) {
8091 int mid = (lo + hi) / 2;
8092 sector_t a = BB_OFFSET(p[mid]);
8093 if (a < target)
8094 /* This could still be the one, earlier ranges
8095 * could not. */
8096 lo = mid;
8097 else
8098 /* This and later ranges are definitely out. */
8099 hi = mid;
8100 }
8101 /* 'lo' might be the last that started before target, but 'hi' isn't */
8102 if (hi > lo) {
8103 /* need to check all range that end after 's' to see if
8104 * any are unacknowledged.
8105 */
8106 while (lo >= 0 &&
8107 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8108 if (BB_OFFSET(p[lo]) < target) {
8109 /* starts before the end, and finishes after
8110 * the start, so they must overlap
8111 */
8112 if (rv != -1 && BB_ACK(p[lo]))
8113 rv = 1;
8114 else
8115 rv = -1;
8116 *first_bad = BB_OFFSET(p[lo]);
8117 *bad_sectors = BB_LEN(p[lo]);
8118 }
8119 lo--;
8120 }
8121 }
8122
8123 if (read_seqretry(&bb->lock, seq))
8124 goto retry;
8125
8126 return rv;
8127}
8128EXPORT_SYMBOL_GPL(md_is_badblock);
8129
8130/*
8131 * Add a range of bad blocks to the table.
8132 * This might extend the table, or might contract it
8133 * if two adjacent ranges can be merged.
8134 * We binary-search to find the 'insertion' point, then
8135 * decide how best to handle it.
8136 */
8137static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8138 int acknowledged)
8139{
8140 u64 *p;
8141 int lo, hi;
8142 int rv = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008143 unsigned long flags;
NeilBrown2230dfe2011-07-28 11:31:46 +10008144
8145 if (bb->shift < 0)
8146 /* badblocks are disabled */
8147 return 0;
8148
8149 if (bb->shift) {
8150 /* round the start down, and the end up */
8151 sector_t next = s + sectors;
8152 s >>= bb->shift;
8153 next += (1<<bb->shift) - 1;
8154 next >>= bb->shift;
8155 sectors = next - s;
8156 }
8157
Bian Yu905b0292013-10-12 01:10:03 -04008158 write_seqlock_irqsave(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008159
8160 p = bb->page;
8161 lo = 0;
8162 hi = bb->count;
8163 /* Find the last range that starts at-or-before 's' */
8164 while (hi - lo > 1) {
8165 int mid = (lo + hi) / 2;
8166 sector_t a = BB_OFFSET(p[mid]);
8167 if (a <= s)
8168 lo = mid;
8169 else
8170 hi = mid;
8171 }
8172 if (hi > lo && BB_OFFSET(p[lo]) > s)
8173 hi = lo;
8174
8175 if (hi > lo) {
8176 /* we found a range that might merge with the start
8177 * of our new range
8178 */
8179 sector_t a = BB_OFFSET(p[lo]);
8180 sector_t e = a + BB_LEN(p[lo]);
8181 int ack = BB_ACK(p[lo]);
8182 if (e >= s) {
8183 /* Yes, we can merge with a previous range */
8184 if (s == a && s + sectors >= e)
8185 /* new range covers old */
8186 ack = acknowledged;
8187 else
8188 ack = ack && acknowledged;
8189
8190 if (e < s + sectors)
8191 e = s + sectors;
8192 if (e - a <= BB_MAX_LEN) {
8193 p[lo] = BB_MAKE(a, e-a, ack);
8194 s = e;
8195 } else {
8196 /* does not all fit in one range,
8197 * make p[lo] maximal
8198 */
8199 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8200 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8201 s = a + BB_MAX_LEN;
8202 }
8203 sectors = e - s;
8204 }
8205 }
8206 if (sectors && hi < bb->count) {
8207 /* 'hi' points to the first range that starts after 's'.
8208 * Maybe we can merge with the start of that range */
8209 sector_t a = BB_OFFSET(p[hi]);
8210 sector_t e = a + BB_LEN(p[hi]);
8211 int ack = BB_ACK(p[hi]);
8212 if (a <= s + sectors) {
8213 /* merging is possible */
8214 if (e <= s + sectors) {
8215 /* full overlap */
8216 e = s + sectors;
8217 ack = acknowledged;
8218 } else
8219 ack = ack && acknowledged;
8220
8221 a = s;
8222 if (e - a <= BB_MAX_LEN) {
8223 p[hi] = BB_MAKE(a, e-a, ack);
8224 s = e;
8225 } else {
8226 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8227 s = a + BB_MAX_LEN;
8228 }
8229 sectors = e - s;
8230 lo = hi;
8231 hi++;
8232 }
8233 }
8234 if (sectors == 0 && hi < bb->count) {
8235 /* we might be able to combine lo and hi */
8236 /* Note: 's' is at the end of 'lo' */
8237 sector_t a = BB_OFFSET(p[hi]);
8238 int lolen = BB_LEN(p[lo]);
8239 int hilen = BB_LEN(p[hi]);
8240 int newlen = lolen + hilen - (s - a);
8241 if (s >= a && newlen < BB_MAX_LEN) {
8242 /* yes, we can combine them */
8243 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8244 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8245 memmove(p + hi, p + hi + 1,
8246 (bb->count - hi - 1) * 8);
8247 bb->count--;
8248 }
8249 }
8250 while (sectors) {
8251 /* didn't merge (it all).
8252 * Need to add a range just before 'hi' */
8253 if (bb->count >= MD_MAX_BADBLOCKS) {
8254 /* No room for more */
8255 rv = 0;
8256 break;
8257 } else {
8258 int this_sectors = sectors;
8259 memmove(p + hi + 1, p + hi,
8260 (bb->count - hi) * 8);
8261 bb->count++;
8262
8263 if (this_sectors > BB_MAX_LEN)
8264 this_sectors = BB_MAX_LEN;
8265 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8266 sectors -= this_sectors;
8267 s += this_sectors;
8268 }
8269 }
8270
8271 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008272 if (!acknowledged)
8273 bb->unacked_exist = 1;
Bian Yu905b0292013-10-12 01:10:03 -04008274 write_sequnlock_irqrestore(&bb->lock, flags);
NeilBrown2230dfe2011-07-28 11:31:46 +10008275
8276 return rv;
8277}
8278
NeilBrown3cb03002011-10-11 16:45:26 +11008279int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008280 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008281{
NeilBrownc6563a82012-05-21 09:27:00 +10008282 int rv;
8283 if (is_new)
8284 s += rdev->new_data_offset;
8285 else
8286 s += rdev->data_offset;
8287 rv = md_set_badblocks(&rdev->badblocks,
8288 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008289 if (rv) {
8290 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008291 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008292 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8293 md_wakeup_thread(rdev->mddev->thread);
8294 }
8295 return rv;
8296}
8297EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8298
8299/*
8300 * Remove a range of bad blocks from the table.
8301 * This may involve extending the table if we spilt a region,
8302 * but it must not fail. So if the table becomes full, we just
8303 * drop the remove request.
8304 */
8305static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8306{
8307 u64 *p;
8308 int lo, hi;
8309 sector_t target = s + sectors;
8310 int rv = 0;
8311
8312 if (bb->shift > 0) {
8313 /* When clearing we round the start up and the end down.
8314 * This should not matter as the shift should align with
8315 * the block size and no rounding should ever be needed.
8316 * However it is better the think a block is bad when it
8317 * isn't than to think a block is not bad when it is.
8318 */
8319 s += (1<<bb->shift) - 1;
8320 s >>= bb->shift;
8321 target >>= bb->shift;
8322 sectors = target - s;
8323 }
8324
8325 write_seqlock_irq(&bb->lock);
8326
8327 p = bb->page;
8328 lo = 0;
8329 hi = bb->count;
8330 /* Find the last range that starts before 'target' */
8331 while (hi - lo > 1) {
8332 int mid = (lo + hi) / 2;
8333 sector_t a = BB_OFFSET(p[mid]);
8334 if (a < target)
8335 lo = mid;
8336 else
8337 hi = mid;
8338 }
8339 if (hi > lo) {
8340 /* p[lo] is the last range that could overlap the
8341 * current range. Earlier ranges could also overlap,
8342 * but only this one can overlap the end of the range.
8343 */
8344 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8345 /* Partial overlap, leave the tail of this range */
8346 int ack = BB_ACK(p[lo]);
8347 sector_t a = BB_OFFSET(p[lo]);
8348 sector_t end = a + BB_LEN(p[lo]);
8349
8350 if (a < s) {
8351 /* we need to split this range */
8352 if (bb->count >= MD_MAX_BADBLOCKS) {
NeilBrown8b32bf52014-05-28 13:39:22 +10008353 rv = -ENOSPC;
NeilBrown2230dfe2011-07-28 11:31:46 +10008354 goto out;
8355 }
8356 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8357 bb->count++;
8358 p[lo] = BB_MAKE(a, s-a, ack);
8359 lo++;
8360 }
8361 p[lo] = BB_MAKE(target, end - target, ack);
8362 /* there is no longer an overlap */
8363 hi = lo;
8364 lo--;
8365 }
8366 while (lo >= 0 &&
8367 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8368 /* This range does overlap */
8369 if (BB_OFFSET(p[lo]) < s) {
8370 /* Keep the early parts of this range. */
8371 int ack = BB_ACK(p[lo]);
8372 sector_t start = BB_OFFSET(p[lo]);
8373 p[lo] = BB_MAKE(start, s - start, ack);
8374 /* now low doesn't overlap, so.. */
8375 break;
8376 }
8377 lo--;
8378 }
8379 /* 'lo' is strictly before, 'hi' is strictly after,
8380 * anything between needs to be discarded
8381 */
8382 if (hi - lo > 1) {
8383 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8384 bb->count -= (hi - lo - 1);
8385 }
8386 }
8387
8388 bb->changed = 1;
8389out:
8390 write_sequnlock_irq(&bb->lock);
8391 return rv;
8392}
8393
NeilBrownc6563a82012-05-21 09:27:00 +10008394int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8395 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008396{
NeilBrownc6563a82012-05-21 09:27:00 +10008397 if (is_new)
8398 s += rdev->new_data_offset;
8399 else
8400 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008401 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008402 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008403}
8404EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8405
8406/*
8407 * Acknowledge all bad blocks in a list.
8408 * This only succeeds if ->changed is clear. It is used by
8409 * in-kernel metadata updates
8410 */
8411void md_ack_all_badblocks(struct badblocks *bb)
8412{
8413 if (bb->page == NULL || bb->changed)
8414 /* no point even trying */
8415 return;
8416 write_seqlock_irq(&bb->lock);
8417
majianpengecb178b2012-03-19 12:46:42 +11008418 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008419 u64 *p = bb->page;
8420 int i;
8421 for (i = 0; i < bb->count ; i++) {
8422 if (!BB_ACK(p[i])) {
8423 sector_t start = BB_OFFSET(p[i]);
8424 int len = BB_LEN(p[i]);
8425 p[i] = BB_MAKE(start, len, 1);
8426 }
8427 }
NeilBrownde393cd2011-07-28 11:31:48 +10008428 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008429 }
8430 write_sequnlock_irq(&bb->lock);
8431}
8432EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8433
NeilBrown16c791a2011-07-28 11:31:47 +10008434/* sysfs access to bad-blocks list.
8435 * We present two files.
8436 * 'bad-blocks' lists sector numbers and lengths of ranges that
8437 * are recorded as bad. The list is truncated to fit within
8438 * the one-page limit of sysfs.
8439 * Writing "sector length" to this file adds an acknowledged
8440 * bad block list.
8441 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8442 * been acknowledged. Writing to this file adds bad blocks
8443 * without acknowledging them. This is largely for testing.
8444 */
8445
8446static ssize_t
8447badblocks_show(struct badblocks *bb, char *page, int unack)
8448{
8449 size_t len;
8450 int i;
8451 u64 *p = bb->page;
8452 unsigned seq;
8453
8454 if (bb->shift < 0)
8455 return 0;
8456
8457retry:
8458 seq = read_seqbegin(&bb->lock);
8459
8460 len = 0;
8461 i = 0;
8462
8463 while (len < PAGE_SIZE && i < bb->count) {
8464 sector_t s = BB_OFFSET(p[i]);
8465 unsigned int length = BB_LEN(p[i]);
8466 int ack = BB_ACK(p[i]);
8467 i++;
8468
8469 if (unack && ack)
8470 continue;
8471
8472 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8473 (unsigned long long)s << bb->shift,
8474 length << bb->shift);
8475 }
NeilBrownde393cd2011-07-28 11:31:48 +10008476 if (unack && len == 0)
8477 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008478
8479 if (read_seqretry(&bb->lock, seq))
8480 goto retry;
8481
8482 return len;
8483}
8484
8485#define DO_DEBUG 1
8486
8487static ssize_t
8488badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8489{
8490 unsigned long long sector;
8491 int length;
8492 char newline;
8493#ifdef DO_DEBUG
8494 /* Allow clearing via sysfs *only* for testing/debugging.
8495 * Normally only a successful write may clear a badblock
8496 */
8497 int clear = 0;
8498 if (page[0] == '-') {
8499 clear = 1;
8500 page++;
8501 }
8502#endif /* DO_DEBUG */
8503
8504 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8505 case 3:
8506 if (newline != '\n')
8507 return -EINVAL;
8508 case 2:
8509 if (length <= 0)
8510 return -EINVAL;
8511 break;
8512 default:
8513 return -EINVAL;
8514 }
8515
8516#ifdef DO_DEBUG
8517 if (clear) {
8518 md_clear_badblocks(bb, sector, length);
8519 return len;
8520 }
8521#endif /* DO_DEBUG */
8522 if (md_set_badblocks(bb, sector, length, !unack))
8523 return len;
8524 else
8525 return -ENOSPC;
8526}
8527
Adrian Bunk75c96f82005-05-05 16:16:09 -07008528static int md_notify_reboot(struct notifier_block *this,
8529 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530{
8531 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008532 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008533 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534
NeilBrownc744a652012-03-19 12:46:37 +11008535 for_each_mddev(mddev, tmp) {
8536 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008537 if (mddev->pers)
8538 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10008539 if (mddev->persistent)
8540 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11008541 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008542 }
NeilBrownc744a652012-03-19 12:46:37 +11008543 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 }
NeilBrownc744a652012-03-19 12:46:37 +11008545 /*
8546 * certain more exotic SCSI devices are known to be
8547 * volatile wrt too early system reboots. While the
8548 * right place to handle this issue is the given
8549 * driver, we do want to have a safe RAID driver ...
8550 */
8551 if (need_delay)
8552 mdelay(1000*1);
8553
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 return NOTIFY_DONE;
8555}
8556
Adrian Bunk75c96f82005-05-05 16:16:09 -07008557static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 .notifier_call = md_notify_reboot,
8559 .next = NULL,
8560 .priority = INT_MAX, /* before any real devices */
8561};
8562
8563static void md_geninit(void)
8564{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008565 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008567 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568}
8569
Adrian Bunk75c96f82005-05-05 16:16:09 -07008570static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571{
Tejun Heoe804ac72010-10-15 15:36:08 +02008572 int ret = -ENOMEM;
8573
Tejun Heoada609e2011-01-25 14:35:54 +01008574 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008575 if (!md_wq)
8576 goto err_wq;
8577
8578 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8579 if (!md_misc_wq)
8580 goto err_misc_wq;
8581
8582 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8583 goto err_md;
8584
8585 if ((ret = register_blkdev(0, "mdp")) < 0)
8586 goto err_mdp;
8587 mdp_major = ret;
8588
NeilBrownaf5628f2014-07-31 13:54:54 +10008589 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008590 md_probe, NULL, NULL);
8591 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 md_probe, NULL, NULL);
8593
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008595 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596
8597 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
Tejun Heoe804ac72010-10-15 15:36:08 +02008600err_mdp:
8601 unregister_blkdev(MD_MAJOR, "md");
8602err_md:
8603 destroy_workqueue(md_misc_wq);
8604err_misc_wq:
8605 destroy_workqueue(md_wq);
8606err_wq:
8607 return ret;
8608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609
8610#ifndef MODULE
8611
8612/*
8613 * Searches all registered partitions for autorun RAID arrays
8614 * at boot time.
8615 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008616
8617static LIST_HEAD(all_detected_devices);
8618struct detected_devices_node {
8619 struct list_head list;
8620 dev_t dev;
8621};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622
8623void md_autodetect_dev(dev_t dev)
8624{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008625 struct detected_devices_node *node_detected_dev;
8626
8627 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8628 if (node_detected_dev) {
8629 node_detected_dev->dev = dev;
8630 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8631 } else {
8632 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8633 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008635}
8636
8637
8638static void autostart_arrays(int part)
8639{
NeilBrown3cb03002011-10-11 16:45:26 +11008640 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008641 struct detected_devices_node *node_detected_dev;
8642 dev_t dev;
8643 int i_scanned, i_passed;
8644
8645 i_scanned = 0;
8646 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
8648 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8649
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008650 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8651 i_scanned++;
8652 node_detected_dev = list_entry(all_detected_devices.next,
8653 struct detected_devices_node, list);
8654 list_del(&node_detected_dev->list);
8655 dev = node_detected_dev->dev;
8656 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008657 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 if (IS_ERR(rdev))
8659 continue;
8660
NeilBrownb2d444d2005-11-08 21:39:31 -08008661 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662 MD_BUG();
8663 continue;
8664 }
NeilBrownd0fae182008-03-04 14:29:31 -08008665 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008667 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008669
8670 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8671 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672
8673 autorun_devices(part);
8674}
8675
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008676#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677
8678static __exit void md_exit(void)
8679{
NeilBrownfd01b882011-10-11 16:47:53 +11008680 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10008682 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008683
NeilBrownaf5628f2014-07-31 13:54:54 +10008684 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07008685 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008687 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 unregister_blkdev(mdp_major, "mdp");
8689 unregister_reboot_notifier(&md_notifier);
8690 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10008691
8692 /* We cannot unload the modules while some process is
8693 * waiting for us in select() or poll() - wake them up
8694 */
8695 md_unloading = 1;
8696 while (waitqueue_active(&md_event_waiters)) {
8697 /* not safe to leave yet */
8698 wake_up(&md_event_waiters);
8699 msleep(delay);
8700 delay += delay;
8701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10008703
NeilBrown29ac4aa2008-02-06 01:39:58 -08008704 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008706 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008708 destroy_workqueue(md_misc_wq);
8709 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710}
8711
Dan Williams685784a2007-07-09 11:56:42 -07008712subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713module_exit(md_exit)
8714
NeilBrownf91de922005-11-08 21:39:36 -08008715static int get_ro(char *buffer, struct kernel_param *kp)
8716{
8717 return sprintf(buffer, "%d", start_readonly);
8718}
8719static int set_ro(const char *val, struct kernel_param *kp)
8720{
8721 char *e;
8722 int num = simple_strtoul(val, &e, 10);
8723 if (*val && (*e == '\0' || *e == '\n')) {
8724 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008725 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008726 }
8727 return -EINVAL;
8728}
8729
NeilBrown80ca3a42006-07-10 04:44:18 -07008730module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8731module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008732
NeilBrownefeb53c2009-01-09 08:31:10 +11008733module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008734
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735EXPORT_SYMBOL(register_md_personality);
8736EXPORT_SYMBOL(unregister_md_personality);
8737EXPORT_SYMBOL(md_error);
8738EXPORT_SYMBOL(md_done_sync);
8739EXPORT_SYMBOL(md_write_start);
8740EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741EXPORT_SYMBOL(md_register_thread);
8742EXPORT_SYMBOL(md_unregister_thread);
8743EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744EXPORT_SYMBOL(md_check_recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008745EXPORT_SYMBOL(md_reap_sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008747MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008748MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008749MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);