blob: 44bb1d52dd4ce1b4dc22cdd25cba107645e879ae [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
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
158static void mddev_bio_destructor(struct bio *bio)
159{
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 mddevp = (void*)bio;
163 mddev = mddevp[-1];
164
165 bio_free(bio, mddev->bio_set);
166}
167
168struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100169 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100170{
171 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100172 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100173
174 if (!mddev || !mddev->bio_set)
175 return bio_alloc(gfp_mask, nr_iovecs);
176
177 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
178 mddev->bio_set);
179 if (!b)
180 return NULL;
181 mddevp = (void*)b;
182 mddevp[-1] = mddev;
183 b->bi_destructor = mddev_bio_destructor;
184 return b;
185}
186EXPORT_SYMBOL_GPL(bio_alloc_mddev);
187
188struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100189 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100190{
191 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100193
194 if (!mddev || !mddev->bio_set)
195 return bio_clone(bio, gfp_mask);
196
197 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
198 mddev->bio_set);
199 if (!b)
200 return NULL;
201 mddevp = (void*)b;
202 mddevp[-1] = mddev;
203 b->bi_destructor = mddev_bio_destructor;
204 __bio_clone(b, bio);
205 if (bio_integrity(bio)) {
206 int ret;
207
208 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
209
210 if (ret < 0) {
211 bio_put(b);
212 return NULL;
213 }
214 }
215
216 return b;
217}
218EXPORT_SYMBOL_GPL(bio_clone_mddev);
219
NeilBrownd2eb35a2011-07-28 11:31:48 +1000220void md_trim_bio(struct bio *bio, int offset, int size)
221{
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 */
226 int i;
227 struct bio_vec *bvec;
228 int sofar = 0;
229
230 size <<= 9;
231 if (offset == 0 && size == bio->bi_size)
232 return;
233
234 bio->bi_sector += offset;
235 bio->bi_size = size;
236 offset <<= 9;
237 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
238
239 while (bio->bi_idx < bio->bi_vcnt &&
240 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
241 /* remove this whole bio_vec */
242 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
243 bio->bi_idx++;
244 }
245 if (bio->bi_idx < bio->bi_vcnt) {
246 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
247 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
248 }
249 /* avoid any complications with bi_idx being non-zero*/
250 if (bio->bi_idx) {
251 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
252 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
253 bio->bi_vcnt -= bio->bi_idx;
254 bio->bi_idx = 0;
255 }
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec, bio, i) {
258 if (sofar + bvec->bv_len > size)
259 bvec->bv_len = size - sofar;
260 if (bvec->bv_len == 0) {
261 bio->bi_vcnt = i;
262 break;
263 }
264 sofar += bvec->bv_len;
265 }
266}
267EXPORT_SYMBOL_GPL(md_trim_bio);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
NeilBrownd7603b72006-01-06 00:20:30 -0800270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
273 * count increases.
274 *
275 * Events are:
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
278 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800279static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800280static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100281void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800282{
283 atomic_inc(&md_event_count);
284 wake_up(&md_event_waiters);
285}
NeilBrown29269552006-03-27 01:18:10 -0800286EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800287
NeilBrownc331eb02006-05-30 21:27:13 -0700288/* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
290 */
NeilBrownfd01b882011-10-11 16:47:53 +1100291static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700292{
293 atomic_inc(&md_event_count);
294 wake_up(&md_event_waiters);
295}
296
NeilBrownd7603b72006-01-06 00:20:30 -0800297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
300 */
301static LIST_HEAD(all_mddevs);
302static DEFINE_SPINLOCK(all_mddevs_lock);
303
304
305/*
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 \
314 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100315 _tmp = all_mddevs.next; \
316 _mddev = NULL;}); \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100324 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 )
326
327
NeilBrown409c57f2009-03-31 14:39:39 +1100328/* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200335static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
NeilBrown49077322010-03-25 16:20:56 +1100337 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100338 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100339 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500340 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100341
NeilBrown0ca69882011-01-14 09:14:33 +1100342 if (mddev == NULL || mddev->pers == NULL
343 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100344 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200345 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100346 }
NeilBrown0ca69882011-01-14 09:14:33 +1100347 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100348 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200349 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100350 DEFINE_WAIT(__wait);
351 for (;;) {
352 prepare_to_wait(&mddev->sb_wait, &__wait,
353 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200354 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100355 break;
356 rcu_read_unlock();
357 schedule();
358 rcu_read_lock();
359 }
360 finish_wait(&mddev->sb_wait, &__wait);
361 }
362 atomic_inc(&mddev->active_io);
363 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100364
Chris Masone91ece52011-02-07 19:21:48 -0500365 /*
366 * save the sectors now since our bio can
367 * go away inside make_request
368 */
369 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200370 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100371
372 cpu = part_stat_lock();
373 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500374 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100375 part_stat_unlock();
376
NeilBrown409c57f2009-03-31 14:39:39 +1100377 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
378 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
NeilBrown9e35b992010-04-06 14:23:02 +1000381/* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
385 * unused.
386 */
NeilBrownfd01b882011-10-11 16:47:53 +1100387void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100388{
389 BUG_ON(mddev->suspended);
390 mddev->suspended = 1;
391 synchronize_rcu();
392 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
393 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500394
395 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100396}
NeilBrown390ee602010-06-01 19:37:27 +1000397EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100398
NeilBrownfd01b882011-10-11 16:47:53 +1100399void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100400{
401 mddev->suspended = 0;
402 wake_up(&mddev->sb_wait);
403 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500404
405 md_wakeup_thread(mddev->thread);
406 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100407}
NeilBrown390ee602010-06-01 19:37:27 +1000408EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100409
NeilBrownfd01b882011-10-11 16:47:53 +1100410int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000411{
412 return mddev->suspended;
413}
414EXPORT_SYMBOL(mddev_congested);
415
NeilBrowna2826aa2009-12-14 12:49:49 +1100416/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200417 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 */
419
Tejun Heoe9c74692010-09-03 11:56:18 +0200420static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100421{
NeilBrown3cb03002011-10-11 16:45:26 +1100422 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100423 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100424
425 rdev_dec_pending(rdev, mddev);
426
427 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200429 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 }
431 bio_put(bio);
432}
433
NeilBrowna7a07e62010-12-09 16:04:25 +1100434static void md_submit_flush_data(struct work_struct *ws);
435
NeilBrowna035fc32010-12-09 16:17:51 +1100436static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100437{
NeilBrownfd01b882011-10-11 16:47:53 +1100438 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100439 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100440
NeilBrowna7a07e62010-12-09 16:04:25 +1100441 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
442 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100443 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100444 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100445 if (rdev->raid_disk >= 0 &&
446 !test_bit(Faulty, &rdev->flags)) {
447 /* Take two references, one is dropped
448 * when request finishes, one after
449 * we reclaim rcu_read_lock
450 */
451 struct bio *bi;
452 atomic_inc(&rdev->nr_pending);
453 atomic_inc(&rdev->nr_pending);
454 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000455 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200456 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100457 bi->bi_private = rdev;
458 bi->bi_bdev = rdev->bdev;
459 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200460 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100461 rcu_read_lock();
462 rdev_dec_pending(rdev, mddev);
463 }
464 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100465 if (atomic_dec_and_test(&mddev->flush_pending))
466 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100467}
468
Tejun Heoe9c74692010-09-03 11:56:18 +0200469static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100470{
NeilBrownfd01b882011-10-11 16:47:53 +1100471 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200472 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100473
Tejun Heoe9c74692010-09-03 11:56:18 +0200474 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100475 /* an empty barrier - all done */
476 bio_endio(bio, 0);
477 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200478 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200479 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100480 }
NeilBrown2b74e122010-12-09 15:59:01 +1100481
482 mddev->flush_bio = NULL;
483 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484}
485
NeilBrownfd01b882011-10-11 16:47:53 +1100486void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100487{
488 spin_lock_irq(&mddev->write_lock);
489 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200490 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100491 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200492 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100493 spin_unlock_irq(&mddev->write_lock);
494
NeilBrowna035fc32010-12-09 16:17:51 +1100495 INIT_WORK(&mddev->flush_work, submit_flushes);
496 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497}
Tejun Heoe9c74692010-09-03 11:56:18 +0200498EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100499
NeilBrown97658cd2011-04-18 18:25:42 +1000500/* Support for plugging.
501 * This mirrors the plugging support in request_queue, but does not
502 * require having a whole queue or request structures.
503 * We allocate an md_plug_cb for each md device and each thread it gets
504 * plugged on. This links tot the private plug_handle structure in the
505 * personality data where we keep a count of the number of outstanding
506 * plugs so other code can see if a plug is active.
507 */
508struct md_plug_cb {
509 struct blk_plug_cb cb;
NeilBrownfd01b882011-10-11 16:47:53 +1100510 struct mddev *mddev;
NeilBrown97658cd2011-04-18 18:25:42 +1000511};
512
513static void plugger_unplug(struct blk_plug_cb *cb)
514{
515 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
516 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
517 md_wakeup_thread(mdcb->mddev->thread);
518 kfree(mdcb);
519}
520
521/* Check that an unplug wakeup will come shortly.
522 * If not, wakeup the md thread immediately
523 */
NeilBrownfd01b882011-10-11 16:47:53 +1100524int mddev_check_plugged(struct mddev *mddev)
NeilBrown97658cd2011-04-18 18:25:42 +1000525{
526 struct blk_plug *plug = current->plug;
527 struct md_plug_cb *mdcb;
528
529 if (!plug)
530 return 0;
531
532 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
533 if (mdcb->cb.callback == plugger_unplug &&
534 mdcb->mddev == mddev) {
535 /* Already on the list, move to top */
536 if (mdcb != list_first_entry(&plug->cb_list,
537 struct md_plug_cb,
538 cb.list))
539 list_move(&mdcb->cb.list, &plug->cb_list);
540 return 1;
541 }
542 }
543 /* Not currently on the callback list */
544 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
545 if (!mdcb)
546 return 0;
547
548 mdcb->mddev = mddev;
549 mdcb->cb.callback = plugger_unplug;
550 atomic_inc(&mddev->plug_cnt);
551 list_add(&mdcb->cb.list, &plug->cb_list);
552 return 1;
553}
554EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000555
NeilBrownfd01b882011-10-11 16:47:53 +1100556static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 atomic_inc(&mddev->active);
559 return mddev;
560}
561
Dan Williams5fd3a172009-03-04 00:57:25 -0700562static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100563
NeilBrownfd01b882011-10-11 16:47:53 +1100564static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
NeilBrowna167f662010-10-26 18:31:13 +1100566 struct bio_set *bs = NULL;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
569 return;
NeilBrownd3374822009-01-09 08:31:10 +1100570 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100571 mddev->ctime == 0 && !mddev->hold_active) {
572 /* Array is not configured at all, and not held active,
573 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100574 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100575 bs = mddev->bio_set;
576 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100577 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200578 /* We did a probe so need to clean up. Call
579 * queue_work inside the spinlock so that
580 * flush_workqueue() after mddev_find will
581 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100582 */
583 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200584 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100585 } else
586 kfree(mddev);
587 }
588 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100589 if (bs)
590 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
NeilBrownfd01b882011-10-11 16:47:53 +1100593void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100594{
595 mutex_init(&mddev->open_mutex);
596 mutex_init(&mddev->reconfig_mutex);
597 mutex_init(&mddev->bitmap_info.mutex);
598 INIT_LIST_HEAD(&mddev->disks);
599 INIT_LIST_HEAD(&mddev->all_mddevs);
600 init_timer(&mddev->safemode_timer);
601 atomic_set(&mddev->active, 1);
602 atomic_set(&mddev->openers, 0);
603 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000604 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100605 spin_lock_init(&mddev->write_lock);
606 atomic_set(&mddev->flush_pending, 0);
607 init_waitqueue_head(&mddev->sb_wait);
608 init_waitqueue_head(&mddev->recovery_wait);
609 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000610 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100611 mddev->resync_min = 0;
612 mddev->resync_max = MaxSector;
613 mddev->level = LEVEL_NONE;
614}
NeilBrown390ee602010-06-01 19:37:27 +1000615EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100616
NeilBrownfd01b882011-10-11 16:47:53 +1100617static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
NeilBrownfd01b882011-10-11 16:47:53 +1100619 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
NeilBrown8f5f02c2011-02-16 13:58:51 +1100621 if (unit && MAJOR(unit) != MD_MAJOR)
622 unit &= ~((1<<MdpMinorShift)-1);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 retry:
625 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
NeilBrownefeb53c2009-01-09 08:31:10 +1100627 if (unit) {
628 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
629 if (mddev->unit == unit) {
630 mddev_get(mddev);
631 spin_unlock(&all_mddevs_lock);
632 kfree(new);
633 return mddev;
634 }
635
636 if (new) {
637 list_add(&new->all_mddevs, &all_mddevs);
638 spin_unlock(&all_mddevs_lock);
639 new->hold_active = UNTIL_IOCTL;
640 return new;
641 }
642 } else if (new) {
643 /* find an unused unit number */
644 static int next_minor = 512;
645 int start = next_minor;
646 int is_free = 0;
647 int dev = 0;
648 while (!is_free) {
649 dev = MKDEV(MD_MAJOR, next_minor);
650 next_minor++;
651 if (next_minor > MINORMASK)
652 next_minor = 0;
653 if (next_minor == start) {
654 /* Oh dear, all in use. */
655 spin_unlock(&all_mddevs_lock);
656 kfree(new);
657 return NULL;
658 }
659
660 is_free = 1;
661 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
662 if (mddev->unit == dev) {
663 is_free = 0;
664 break;
665 }
666 }
667 new->unit = dev;
668 new->md_minor = MINOR(dev);
669 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 list_add(&new->all_mddevs, &all_mddevs);
671 spin_unlock(&all_mddevs_lock);
672 return new;
673 }
674 spin_unlock(&all_mddevs_lock);
675
NeilBrown9ffae0c2006-01-06 00:20:32 -0800676 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!new)
678 return NULL;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 new->unit = unit;
681 if (MAJOR(unit) == MD_MAJOR)
682 new->md_minor = MINOR(unit);
683 else
684 new->md_minor = MINOR(unit) >> MdpMinorShift;
685
NeilBrownfafd7fb2010-04-01 15:55:30 +1100686 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto retry;
689}
690
NeilBrownfd01b882011-10-11 16:47:53 +1100691static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800693 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrownfd01b882011-10-11 16:47:53 +1100696static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100697{
698 return mutex_is_locked(&mddev->reconfig_mutex);
699}
700
NeilBrownfd01b882011-10-11 16:47:53 +1100701static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800703 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
NeilBrownb6eb1272010-04-15 10:13:47 +1000706static struct attribute_group md_redundancy_group;
707
NeilBrownfd01b882011-10-11 16:47:53 +1100708static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
NeilBrowna64c8762010-04-14 17:15:37 +1000710 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000711 /* These cannot be removed under reconfig_mutex as
712 * an access to the files will try to take reconfig_mutex
713 * while holding the file unremovable, which leads to
714 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000715 * So hold set sysfs_active while the remove in happeing,
716 * and anything else which might set ->to_remove or my
717 * otherwise change the sysfs namespace will fail with
718 * -EBUSY if sysfs_active is still set.
719 * We set sysfs_active under reconfig_mutex and elsewhere
720 * test it under the same mutex to ensure its correct value
721 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000722 */
NeilBrowna64c8762010-04-14 17:15:37 +1000723 struct attribute_group *to_remove = mddev->to_remove;
724 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000725 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000726 mutex_unlock(&mddev->reconfig_mutex);
727
NeilBrown00bcb4a2010-06-01 19:37:23 +1000728 if (mddev->kobj.sd) {
729 if (to_remove != &md_redundancy_group)
730 sysfs_remove_group(&mddev->kobj, to_remove);
731 if (mddev->pers == NULL ||
732 mddev->pers->sync_request == NULL) {
733 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
734 if (mddev->sysfs_action)
735 sysfs_put(mddev->sysfs_action);
736 mddev->sysfs_action = NULL;
737 }
NeilBrowna64c8762010-04-14 17:15:37 +1000738 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000739 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000740 } else
741 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Chris Dunlop751e67c2011-10-19 16:48:26 +1100743 /* As we've dropped the mutex we need a spinlock to
744 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000745 */
746 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700747 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000748 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
NeilBrownfd01b882011-10-11 16:47:53 +1100751static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
NeilBrown3cb03002011-10-11 16:45:26 +1100753 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrowndafb20f2012-03-19 12:46:39 +1100755 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (rdev->desc_nr == nr)
757 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return NULL;
760}
761
NeilBrownfd01b882011-10-11 16:47:53 +1100762static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
NeilBrown3cb03002011-10-11 16:45:26 +1100764 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
NeilBrowndafb20f2012-03-19 12:46:39 +1100766 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (rdev->bdev->bd_dev == dev)
768 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return NULL;
771}
772
NeilBrown84fc4b52011-10-11 16:49:58 +1100773static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800774{
NeilBrown84fc4b52011-10-11 16:49:58 +1100775 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800776 list_for_each_entry(pers, &pers_list, list) {
777 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800778 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800779 if (strcmp(pers->name, clevel)==0)
780 return pers;
781 }
NeilBrown2604b702006-01-06 00:20:36 -0800782 return NULL;
783}
784
Andre Nollb73df2d2008-07-11 22:02:23 +1000785/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100786static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100788 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000789 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
NeilBrown3cb03002011-10-11 16:45:26 +1100792static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 if (rdev->sb_page)
795 MD_BUG();
796
797 rdev->sb_page = alloc_page(GFP_KERNEL);
798 if (!rdev->sb_page) {
799 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000800 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
803 return 0;
804}
805
NeilBrown3cb03002011-10-11 16:45:26 +1100806static void free_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800809 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rdev->sb_loaded = 0;
811 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000812 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100813 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
NeilBrown2699b672011-07-28 11:31:47 +1000815 if (rdev->bb_page) {
816 put_page(rdev->bb_page);
817 rdev->bb_page = NULL;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821
NeilBrown6712ecf2007-09-27 12:47:43 +0200822static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700823{
NeilBrown3cb03002011-10-11 16:45:26 +1100824 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100825 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700826
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700827 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
828 printk("md: super_written gets error=%d, uptodate=%d\n",
829 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
830 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800831 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700832 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700833
NeilBrowna9701a32005-11-08 21:39:34 -0800834 if (atomic_dec_and_test(&mddev->pending_writes))
835 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700836 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700837}
838
NeilBrownfd01b882011-10-11 16:47:53 +1100839void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700840 sector_t sector, int size, struct page *page)
841{
842 /* write first size bytes of page to sector of rdev
843 * Increment mddev->pending_writes before returning
844 * and decrement it on completion, waking up sb_wait
845 * if zero is reached.
846 * If an error occurred, call md_error
847 */
NeilBrowna167f662010-10-26 18:31:13 +1100848 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700849
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100850 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700851 bio->bi_sector = sector;
852 bio_add_page(bio, page, size, 0);
853 bio->bi_private = rdev;
854 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800855
NeilBrown7bfa19f2005-06-21 17:17:28 -0700856 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000857 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800858}
859
NeilBrownfd01b882011-10-11 16:47:53 +1100860void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800861{
Tejun Heoe9c74692010-09-03 11:56:18 +0200862 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800863 DEFINE_WAIT(wq);
864 for(;;) {
865 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
866 if (atomic_read(&mddev->pending_writes)==0)
867 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800868 schedule();
869 }
870 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700871}
872
NeilBrown6712ecf2007-09-27 12:47:43 +0200873static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
NeilBrown3cb03002011-10-11 16:45:26 +1100878int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100879 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
NeilBrowna167f662010-10-26 18:31:13 +1100881 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct completion event;
883 int ret;
884
Jens Axboe721a9602011-03-09 11:56:30 +0100885 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100887 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
888 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100889 if (metadata_op)
890 bio->bi_sector = sector + rdev->sb_start;
891 else
892 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 bio_add_page(bio, page, size, 0);
894 init_completion(&event);
895 bio->bi_private = &event;
896 bio->bi_end_io = bi_complete;
897 submit_bio(rw, bio);
898 wait_for_completion(&event);
899
900 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
901 bio_put(bio);
902 return ret;
903}
NeilBrowna8745db2006-01-06 00:20:34 -0800904EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
NeilBrown3cb03002011-10-11 16:45:26 +1100906static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 char b[BDEVNAME_SIZE];
909 if (!rdev->sb_page) {
910 MD_BUG();
911 return -EINVAL;
912 }
913 if (rdev->sb_loaded)
914 return 0;
915
916
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100917 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto fail;
919 rdev->sb_loaded = 1;
920 return 0;
921
922fail:
923 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
924 bdevname(rdev->bdev,b));
925 return -EINVAL;
926}
927
928static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
929{
Andre Noll05710462008-07-11 22:02:20 +1000930 return sb1->set_uuid0 == sb2->set_uuid0 &&
931 sb1->set_uuid1 == sb2->set_uuid1 &&
932 sb1->set_uuid2 == sb2->set_uuid2 &&
933 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
937{
938 int ret;
939 mdp_super_t *tmp1, *tmp2;
940
941 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
942 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
943
944 if (!tmp1 || !tmp2) {
945 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100946 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto abort;
948 }
949
950 *tmp1 = *sb1;
951 *tmp2 = *sb2;
952
953 /*
954 * nr_disks is not constant
955 */
956 tmp1->nr_disks = 0;
957 tmp2->nr_disks = 0;
958
Andre Nollce0c8e02008-07-11 22:02:20 +1000959 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700961 kfree(tmp1);
962 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return ret;
964}
965
NeilBrown4d167f02007-05-09 02:35:37 -0700966
967static u32 md_csum_fold(u32 csum)
968{
969 csum = (csum & 0xffff) + (csum >> 16);
970 return (csum & 0xffff) + (csum >> 16);
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973static unsigned int calc_sb_csum(mdp_super_t * sb)
974{
NeilBrown4d167f02007-05-09 02:35:37 -0700975 u64 newcsum = 0;
976 u32 *sb32 = (u32*)sb;
977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned int disk_csum, csum;
979
980 disk_csum = sb->sb_csum;
981 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700982
983 for (i = 0; i < MD_SB_BYTES/4 ; i++)
984 newcsum += sb32[i];
985 csum = (newcsum & 0xffffffff) + (newcsum>>32);
986
987
988#ifdef CONFIG_ALPHA
989 /* This used to use csum_partial, which was wrong for several
990 * reasons including that different results are returned on
991 * different architectures. It isn't critical that we get exactly
992 * the same return value as before (we always csum_fold before
993 * testing, and that removes any differences). However as we
994 * know that csum_partial always returned a 16bit value on
995 * alphas, do a fold to maximise conformity to previous behaviour.
996 */
997 sb->sb_csum = md_csum_fold(disk_csum);
998#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return csum;
1002}
1003
1004
1005/*
1006 * Handle superblock details.
1007 * We want to be able to handle multiple superblock formats
1008 * so we have a common interface to them all, and an array of
1009 * different handlers.
1010 * We rely on user-space to write the initial superblock, and support
1011 * reading and updating of superblocks.
1012 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001013 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * loads and validates a superblock on dev.
1015 * if refdev != NULL, compare superblocks on both devices
1016 * Return:
1017 * 0 - dev has a superblock that is compatible with refdev
1018 * 1 - dev has a superblock that is compatible and newer than refdev
1019 * so dev should be used as the refdev in future
1020 * -EINVAL superblock incompatible or invalid
1021 * -othererror e.g. -EIO
1022 *
NeilBrownfd01b882011-10-11 16:47:53 +11001023 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * Verify that dev is acceptable into mddev.
1025 * The first time, mddev->raid_disks will be 0, and data from
1026 * dev should be merged in. Subsequent calls check that dev
1027 * is new enough. Return 0 or -EINVAL
1028 *
NeilBrownfd01b882011-10-11 16:47:53 +11001029 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * Update the superblock for rdev with data in mddev
1031 * This does not write to disc.
1032 *
1033 */
1034
1035struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001036 char *name;
1037 struct module *owner;
NeilBrown3cb03002011-10-11 16:45:26 +11001038 int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001039 int minor_version);
NeilBrownfd01b882011-10-11 16:47:53 +11001040 int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev);
1041 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001042 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001043 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044};
1045
1046/*
Andre Noll0894cc32009-06-18 08:49:23 +10001047 * Check that the given mddev has no bitmap.
1048 *
1049 * This function is called from the run method of all personalities that do not
1050 * support bitmaps. It prints an error message and returns non-zero if mddev
1051 * has a bitmap. Otherwise, it returns 0.
1052 *
1053 */
NeilBrownfd01b882011-10-11 16:47:53 +11001054int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001055{
NeilBrownc3d97142009-12-14 12:49:52 +11001056 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001057 return 0;
1058 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1059 mdname(mddev), mddev->pers->name);
1060 return 1;
1061}
1062EXPORT_SYMBOL(md_check_no_bitmap);
1063
1064/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * load_super for 0.90.0
1066 */
NeilBrown3cb03002011-10-11 16:45:26 +11001067static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1070 mdp_super_t *sb;
1071 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
Andre Noll0f420352008-07-11 22:02:23 +10001074 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 * it's at the end of the disk.
1076 *
1077 * It also happens to be a multiple of 4Kb.
1078 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001079 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
NeilBrown0002b272005-09-09 16:23:53 -07001081 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ret) return ret;
1083
1084 ret = -EINVAL;
1085
1086 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001087 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (sb->md_magic != MD_SB_MAGIC) {
1090 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1091 b);
1092 goto abort;
1093 }
1094
1095 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001096 sb->minor_version < 90 ||
1097 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1099 sb->major_version, sb->minor_version,
1100 b);
1101 goto abort;
1102 }
1103
1104 if (sb->raid_disks <= 0)
1105 goto abort;
1106
NeilBrown4d167f02007-05-09 02:35:37 -07001107 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1109 b);
1110 goto abort;
1111 }
1112
1113 rdev->preferred_minor = sb->md_minor;
1114 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001115 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001116 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (sb->level == LEVEL_MULTIPATH)
1119 rdev->desc_nr = -1;
1120 else
1121 rdev->desc_nr = sb->this_disk.number;
1122
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001123 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001125 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001127 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (!uuid_equal(refsb, sb)) {
1129 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1130 b, bdevname(refdev->bdev,b2));
1131 goto abort;
1132 }
1133 if (!sb_equal(refsb, sb)) {
1134 printk(KERN_WARNING "md: %s has same UUID"
1135 " but different superblock to %s\n",
1136 b, bdevname(refdev->bdev, b2));
1137 goto abort;
1138 }
1139 ev1 = md_event(sb);
1140 ev2 = md_event(refsb);
1141 if (ev1 > ev2)
1142 ret = 1;
1143 else
1144 ret = 0;
1145 }
NeilBrown8190e752009-06-18 08:48:58 +10001146 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001147 /* Limit to 4TB as metadata cannot record more than that */
1148 if (rdev->sectors >= (2ULL << 32))
1149 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
NeilBrown27a7b262011-09-10 17:21:28 +10001151 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001152 /* "this cannot possibly happen" ... */
1153 ret = -EINVAL;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 abort:
1156 return ret;
1157}
1158
1159/*
1160 * validate_super for 0.90.0
1161 */
NeilBrownfd01b882011-10-11 16:47:53 +11001162static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001165 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001166 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
NeilBrown41158c72005-06-21 17:17:25 -07001168 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001169 clear_bit(Faulty, &rdev->flags);
1170 clear_bit(In_sync, &rdev->flags);
1171 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (mddev->raid_disks == 0) {
1174 mddev->major_version = 0;
1175 mddev->minor_version = sb->minor_version;
1176 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001177 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001178 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mddev->ctime = sb->ctime;
1180 mddev->utime = sb->utime;
1181 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001182 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 mddev->layout = sb->layout;
1184 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001185 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001186 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001187 mddev->bitmap_info.offset = 0;
1188 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001189 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
NeilBrownf6705572006-03-27 01:18:11 -08001191 if (mddev->minor_version >= 91) {
1192 mddev->reshape_position = sb->reshape_position;
1193 mddev->delta_disks = sb->delta_disks;
1194 mddev->new_level = sb->new_level;
1195 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001196 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001197 if (mddev->delta_disks < 0)
1198 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001199 } else {
1200 mddev->reshape_position = MaxSector;
1201 mddev->delta_disks = 0;
1202 mddev->new_level = mddev->level;
1203 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001204 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001205 }
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (sb->state & (1<<MD_SB_CLEAN))
1208 mddev->recovery_cp = MaxSector;
1209 else {
1210 if (sb->events_hi == sb->cp_events_hi &&
1211 sb->events_lo == sb->cp_events_lo) {
1212 mddev->recovery_cp = sb->recovery_cp;
1213 } else
1214 mddev->recovery_cp = 0;
1215 }
1216
1217 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1218 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1219 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1220 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1221
1222 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001223
1224 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001225 mddev->bitmap_info.file == NULL)
1226 mddev->bitmap_info.offset =
1227 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001228
NeilBrown41158c72005-06-21 17:17:25 -07001229 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001230 /* Insist on good event counter while assembling, except
1231 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001233 if (sb->disks[rdev->desc_nr].state & (
1234 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1235 if (ev1 < mddev->events)
1236 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001237 } else if (mddev->bitmap) {
1238 /* if adding to array with a bitmap, then we can accept an
1239 * older device ... but not too old.
1240 */
NeilBrown41158c72005-06-21 17:17:25 -07001241 if (ev1 < mddev->bitmap->events_cleared)
1242 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001243 } else {
1244 if (ev1 < mddev->events)
1245 /* just a hot-add of a new device, leave raid_disk at -1 */
1246 return 0;
1247 }
NeilBrown41158c72005-06-21 17:17:25 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 desc = sb->disks + rdev->desc_nr;
1251
1252 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001253 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001254 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1255 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001256 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001258 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1259 /* active but not in sync implies recovery up to
1260 * reshape position. We don't know exactly where
1261 * that is, so set to zero for now */
1262 if (mddev->minor_version >= 91) {
1263 rdev->recovery_offset = 0;
1264 rdev->raid_disk = desc->raid_disk;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001267 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1268 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001269 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001270 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return 0;
1272}
1273
1274/*
1275 * sync_super for 0.90.0
1276 */
NeilBrownfd01b882011-10-11 16:47:53 +11001277static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001280 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /* make rdev->sb match mddev data..
1285 *
1286 * 1/ zero out disks
1287 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1288 * 3/ any empty disks < next_spare become removed
1289 *
1290 * disks[0] gets initialised to REMOVED because
1291 * we cannot be sure from other fields if it has
1292 * been initialised or not.
1293 */
1294 int i;
1295 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1296
NeilBrown61181562005-09-09 16:24:02 -07001297 rdev->sb_size = MD_SB_BYTES;
1298
Namhyung Kim65a06f062011-07-27 11:00:36 +10001299 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 memset(sb, 0, sizeof(*sb));
1302
1303 sb->md_magic = MD_SB_MAGIC;
1304 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 sb->patch_version = mddev->patch_version;
1306 sb->gvalid_words = 0; /* ignored */
1307 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1308 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1309 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1310 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1311
1312 sb->ctime = mddev->ctime;
1313 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001314 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sb->raid_disks = mddev->raid_disks;
1316 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001317 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sb->utime = mddev->utime;
1319 sb->state = 0;
1320 sb->events_hi = (mddev->events>>32);
1321 sb->events_lo = (u32)mddev->events;
1322
NeilBrownf6705572006-03-27 01:18:11 -08001323 if (mddev->reshape_position == MaxSector)
1324 sb->minor_version = 90;
1325 else {
1326 sb->minor_version = 91;
1327 sb->reshape_position = mddev->reshape_position;
1328 sb->new_level = mddev->new_level;
1329 sb->delta_disks = mddev->delta_disks;
1330 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001331 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001332 }
1333 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (mddev->in_sync)
1335 {
1336 sb->recovery_cp = mddev->recovery_cp;
1337 sb->cp_events_hi = (mddev->events>>32);
1338 sb->cp_events_lo = (u32)mddev->events;
1339 if (mddev->recovery_cp == MaxSector)
1340 sb->state = (1<< MD_SB_CLEAN);
1341 } else
1342 sb->recovery_cp = 0;
1343
1344 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001345 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
NeilBrownc3d97142009-12-14 12:49:52 +11001347 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001348 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001351 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001353 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001354 int is_active = test_bit(In_sync, &rdev2->flags);
1355
1356 if (rdev2->raid_disk >= 0 &&
1357 sb->minor_version >= 91)
1358 /* we have nowhere to store the recovery_offset,
1359 * but if it is not below the reshape_position,
1360 * we can piggy-back on that.
1361 */
1362 is_active = 1;
1363 if (rdev2->raid_disk < 0 ||
1364 test_bit(Faulty, &rdev2->flags))
1365 is_active = 0;
1366 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001367 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001369 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001370 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 d = &sb->disks[rdev2->desc_nr];
1372 nr_disks++;
1373 d->number = rdev2->desc_nr;
1374 d->major = MAJOR(rdev2->bdev->bd_dev);
1375 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001376 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 d->raid_disk = rdev2->raid_disk;
1378 else
1379 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001380 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001382 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001384 if (test_bit(In_sync, &rdev2->flags))
1385 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 active++;
1387 working++;
1388 } else {
1389 d->state = 0;
1390 spare++;
1391 working++;
1392 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001393 if (test_bit(WriteMostly, &rdev2->flags))
1394 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* now set the "removed" and "faulty" bits on any missing devices */
1397 for (i=0 ; i < mddev->raid_disks ; i++) {
1398 mdp_disk_t *d = &sb->disks[i];
1399 if (d->state == 0 && d->number == 0) {
1400 d->number = i;
1401 d->raid_disk = i;
1402 d->state = (1<<MD_DISK_REMOVED);
1403 d->state |= (1<<MD_DISK_FAULTY);
1404 failed++;
1405 }
1406 }
1407 sb->nr_disks = nr_disks;
1408 sb->active_disks = active;
1409 sb->working_disks = working;
1410 sb->failed_disks = failed;
1411 sb->spare_disks = spare;
1412
1413 sb->this_disk = sb->disks[rdev->desc_nr];
1414 sb->sb_csum = calc_sb_csum(sb);
1415}
1416
1417/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001418 * rdev_size_change for 0.90.0
1419 */
1420static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001421super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001422{
Andre Noll58c0fed2009-03-31 14:33:13 +11001423 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001424 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001425 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001426 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001427 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001428 if (!num_sectors || num_sectors > rdev->sb_start)
1429 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001430 /* Limit to 4TB as metadata cannot record more than that.
1431 * 4TB == 2^32 KB, or 2*2^32 sectors.
1432 */
1433 if (num_sectors >= (2ULL << 32))
1434 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001435 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001436 rdev->sb_page);
1437 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001438 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001439}
1440
1441
1442/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * version 1 superblock
1444 */
1445
NeilBrown1c05b4b2006-10-21 10:24:08 -07001446static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001448 __le32 disk_csum;
1449 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 unsigned long long newcsum;
1451 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001452 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int i;
1454
1455 disk_csum = sb->sb_csum;
1456 sb->sb_csum = 0;
1457 newcsum = 0;
1458 for (i=0; size>=4; size -= 4 )
1459 newcsum += le32_to_cpu(*isuper++);
1460
1461 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001462 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1465 sb->sb_csum = disk_csum;
1466 return cpu_to_le32(csum);
1467}
1468
NeilBrown2699b672011-07-28 11:31:47 +10001469static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1470 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001471static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 struct mdp_superblock_1 *sb;
1474 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001475 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001477 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /*
Andre Noll0f420352008-07-11 22:02:23 +10001480 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * It is always aligned to a 4K boundary and
1482 * depeding on minor_version, it can be:
1483 * 0: At least 8K, but less than 12K, from end of device
1484 * 1: At start of device
1485 * 2: 4K from start of device.
1486 */
1487 switch(minor_version) {
1488 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001489 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001490 sb_start -= 8*2;
1491 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
1493 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001494 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001497 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499 default:
1500 return -EINVAL;
1501 }
Andre Noll0f420352008-07-11 22:02:23 +10001502 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
NeilBrown0002b272005-09-09 16:23:53 -07001504 /* superblock is rarely larger than 1K, but it can be larger,
1505 * and it is safe to read 4k, so we do that
1506 */
1507 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (ret) return ret;
1509
1510
Namhyung Kim65a06f062011-07-27 11:00:36 +10001511 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1514 sb->major_version != cpu_to_le32(1) ||
1515 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001516 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001517 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EINVAL;
1519
1520 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1521 printk("md: invalid superblock checksum on %s\n",
1522 bdevname(rdev->bdev,b));
1523 return -EINVAL;
1524 }
1525 if (le64_to_cpu(sb->data_size) < 10) {
1526 printk("md: data_size too small on %s\n",
1527 bdevname(rdev->bdev,b));
1528 return -EINVAL;
1529 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 rdev->preferred_minor = 0xffff;
1532 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001533 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
NeilBrown0002b272005-09-09 16:23:53 -07001535 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001536 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001537 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001538 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1539
1540 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001541 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001542 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001543
NeilBrown31b65a02006-07-10 04:44:14 -07001544 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1545 rdev->desc_nr = -1;
1546 else
1547 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1548
NeilBrown2699b672011-07-28 11:31:47 +10001549 if (!rdev->bb_page) {
1550 rdev->bb_page = alloc_page(GFP_KERNEL);
1551 if (!rdev->bb_page)
1552 return -ENOMEM;
1553 }
1554 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1555 rdev->badblocks.count == 0) {
1556 /* need to load the bad block list.
1557 * Currently we limit it to one page.
1558 */
1559 s32 offset;
1560 sector_t bb_sector;
1561 u64 *bbp;
1562 int i;
1563 int sectors = le16_to_cpu(sb->bblog_size);
1564 if (sectors > (PAGE_SIZE / 512))
1565 return -EINVAL;
1566 offset = le32_to_cpu(sb->bblog_offset);
1567 if (offset == 0)
1568 return -EINVAL;
1569 bb_sector = (long long)offset;
1570 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1571 rdev->bb_page, READ, true))
1572 return -EIO;
1573 bbp = (u64 *)page_address(rdev->bb_page);
1574 rdev->badblocks.shift = sb->bblog_shift;
1575 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1576 u64 bb = le64_to_cpu(*bbp);
1577 int count = bb & (0x3ff);
1578 u64 sector = bb >> 10;
1579 sector <<= sb->bblog_shift;
1580 count <<= sb->bblog_shift;
1581 if (bb + 1 == 0)
1582 break;
1583 if (md_set_badblocks(&rdev->badblocks,
1584 sector, count, 1) == 0)
1585 return -EINVAL;
1586 }
1587 } else if (sb->bblog_offset == 0)
1588 rdev->badblocks.shift = -1;
1589
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001590 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001591 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001592 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001594 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1597 sb->level != refsb->level ||
1598 sb->layout != refsb->layout ||
1599 sb->chunksize != refsb->chunksize) {
1600 printk(KERN_WARNING "md: %s has strangely different"
1601 " superblock to %s\n",
1602 bdevname(rdev->bdev,b),
1603 bdevname(refdev->bdev,b2));
1604 return -EINVAL;
1605 }
1606 ev1 = le64_to_cpu(sb->events);
1607 ev2 = le64_to_cpu(refsb->events);
1608
1609 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001610 ret = 1;
1611 else
1612 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
NeilBrowna1801f82008-03-04 14:29:31 -08001614 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001615 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001616 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001618 rdev->sectors = rdev->sb_start;
1619 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001621 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001622 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001623 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
NeilBrownfd01b882011-10-11 16:47:53 +11001627static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001629 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001630 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
NeilBrown41158c72005-06-21 17:17:25 -07001632 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001633 clear_bit(Faulty, &rdev->flags);
1634 clear_bit(In_sync, &rdev->flags);
1635 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (mddev->raid_disks == 0) {
1638 mddev->major_version = 1;
1639 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001640 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001641 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1643 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1644 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001645 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 mddev->layout = le32_to_cpu(sb->layout);
1647 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001648 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001649 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001650 mddev->bitmap_info.offset = 0;
1651 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001652 mddev->reshape_backwards = 0;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1655 memcpy(mddev->uuid, sb->set_uuid, 16);
1656
1657 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001658
NeilBrown71c08052005-09-09 16:23:51 -07001659 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001660 mddev->bitmap_info.file == NULL )
1661 mddev->bitmap_info.offset =
1662 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001663
NeilBrownf6705572006-03-27 01:18:11 -08001664 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1665 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1666 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1667 mddev->new_level = le32_to_cpu(sb->new_level);
1668 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001669 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001670 if (mddev->delta_disks < 0 ||
1671 (mddev->delta_disks == 0 &&
1672 (le32_to_cpu(sb->feature_map)
1673 & MD_FEATURE_RESHAPE_BACKWARDS)))
1674 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001675 } else {
1676 mddev->reshape_position = MaxSector;
1677 mddev->delta_disks = 0;
1678 mddev->new_level = mddev->level;
1679 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001680 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001681 }
1682
NeilBrown41158c72005-06-21 17:17:25 -07001683 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001684 /* Insist of good event counter while assembling, except for
1685 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001687 if (rdev->desc_nr >= 0 &&
1688 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1689 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1690 if (ev1 < mddev->events)
1691 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001692 } else if (mddev->bitmap) {
1693 /* If adding to array with a bitmap, then we can accept an
1694 * older device, but not too old.
1695 */
NeilBrown41158c72005-06-21 17:17:25 -07001696 if (ev1 < mddev->bitmap->events_cleared)
1697 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001698 } else {
1699 if (ev1 < mddev->events)
1700 /* just a hot-add of a new device, leave raid_disk at -1 */
1701 return 0;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (mddev->level != LEVEL_MULTIPATH) {
1704 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001705 if (rdev->desc_nr < 0 ||
1706 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1707 role = 0xffff;
1708 rdev->desc_nr = -1;
1709 } else
1710 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 switch(role) {
1712 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
1714 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001715 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 break;
1717 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001718 if ((le32_to_cpu(sb->feature_map) &
1719 MD_FEATURE_RECOVERY_OFFSET))
1720 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1721 else
1722 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 rdev->raid_disk = role;
1724 break;
1725 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001726 if (sb->devflags & WriteMostly1)
1727 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001728 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1729 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001730 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001731 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return 0;
1734}
1735
NeilBrownfd01b882011-10-11 16:47:53 +11001736static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001739 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int max_dev, i;
1741 /* make rdev->sb match mddev and rdev data. */
1742
Namhyung Kim65a06f062011-07-27 11:00:36 +10001743 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 sb->feature_map = 0;
1746 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001747 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 memset(sb->pad3, 0, sizeof(sb->pad3));
1750
1751 sb->utime = cpu_to_le64((__u64)mddev->utime);
1752 sb->events = cpu_to_le64(mddev->events);
1753 if (mddev->in_sync)
1754 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1755 else
1756 sb->resync_offset = cpu_to_le64(0);
1757
NeilBrown1c05b4b2006-10-21 10:24:08 -07001758 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001759
NeilBrownf0ca3402006-02-02 14:28:04 -08001760 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001761 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001762 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001763 sb->level = cpu_to_le32(mddev->level);
1764 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001765
NeilBrownaeb9b2112011-08-25 14:43:08 +10001766 if (test_bit(WriteMostly, &rdev->flags))
1767 sb->devflags |= WriteMostly1;
1768 else
1769 sb->devflags &= ~WriteMostly1;
1770
NeilBrownc3d97142009-12-14 12:49:52 +11001771 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1772 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001773 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001774 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001775
1776 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001777 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001778 sb->feature_map |=
1779 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1780 sb->recovery_offset =
1781 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001782 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001783 if (test_bit(Replacement, &rdev->flags))
1784 sb->feature_map |=
1785 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001786
NeilBrownf6705572006-03-27 01:18:11 -08001787 if (mddev->reshape_position != MaxSector) {
1788 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1789 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1790 sb->new_layout = cpu_to_le32(mddev->new_layout);
1791 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1792 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001793 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001794 if (mddev->delta_disks == 0 &&
1795 mddev->reshape_backwards)
1796 sb->feature_map
1797 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownf6705572006-03-27 01:18:11 -08001798 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001799
NeilBrown2699b672011-07-28 11:31:47 +10001800 if (rdev->badblocks.count == 0)
1801 /* Nothing to do for bad blocks*/ ;
1802 else if (sb->bblog_offset == 0)
1803 /* Cannot record bad blocks on this device */
1804 md_error(mddev, rdev);
1805 else {
1806 struct badblocks *bb = &rdev->badblocks;
1807 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1808 u64 *p = bb->page;
1809 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1810 if (bb->changed) {
1811 unsigned seq;
1812
1813retry:
1814 seq = read_seqbegin(&bb->lock);
1815
1816 memset(bbp, 0xff, PAGE_SIZE);
1817
1818 for (i = 0 ; i < bb->count ; i++) {
1819 u64 internal_bb = *p++;
1820 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1821 | BB_LEN(internal_bb));
1822 *bbp++ = cpu_to_le64(store_bb);
1823 }
NeilBrownd0962932012-03-19 12:46:41 +11001824 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001825 if (read_seqretry(&bb->lock, seq))
1826 goto retry;
1827
1828 bb->sector = (rdev->sb_start +
1829 (int)le32_to_cpu(sb->bblog_offset));
1830 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001831 }
1832 }
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001835 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (rdev2->desc_nr+1 > max_dev)
1837 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001838
NeilBrown70471da2009-08-03 10:59:57 +10001839 if (max_dev > le32_to_cpu(sb->max_dev)) {
1840 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001841 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001842 rdev->sb_size = max_dev * 2 + 256;
1843 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1844 if (rdev->sb_size & bmask)
1845 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001846 } else
1847 max_dev = le32_to_cpu(sb->max_dev);
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 for (i=0; i<max_dev;i++)
1850 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1851
NeilBrowndafb20f2012-03-19 12:46:39 +11001852 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001854 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001856 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001858 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001859 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 else
1861 sb->dev_roles[i] = cpu_to_le16(0xffff);
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 sb->sb_csum = calc_sb_1_csum(sb);
1865}
1866
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001868super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001869{
1870 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001871 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001872 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001873 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001874 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001876 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 max_sectors -= rdev->data_offset;
1878 if (!num_sectors || num_sectors > max_sectors)
1879 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001880 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 /* minor version 0 with bitmap we can't move */
1882 return 0;
1883 } else {
1884 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001885 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001886 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001887 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001888 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001889 if (!num_sectors || num_sectors > max_sectors)
1890 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001891 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001892 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001893 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001894 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001895 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001896 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001897 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 rdev->sb_page);
1899 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001900 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Adrian Bunk75c96f82005-05-05 16:16:09 -07001903static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 [0] = {
1905 .name = "0.90.0",
1906 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 .load_super = super_90_load,
1908 .validate_super = super_90_validate,
1909 .sync_super = super_90_sync,
1910 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 },
1912 [1] = {
1913 .name = "md-1",
1914 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001915 .load_super = super_1_load,
1916 .validate_super = super_1_validate,
1917 .sync_super = super_1_sync,
1918 .rdev_size_change = super_1_rdev_size_change,
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{
2008 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
2009 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
2010
2011 if (!bi_mddev) /* nothing to do */
2012 return;
2013 if (rdev->raid_disk < 0) /* skip spares */
2014 return;
2015 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2016 rdev->bdev->bd_disk) >= 0)
2017 return;
2018 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2019 blk_integrity_unregister(mddev->gendisk);
2020}
2021EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002022
NeilBrownfd01b882011-10-11 16:47:53 +11002023static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002025 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002026 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002027 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002028 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 if (rdev->mddev) {
2031 MD_BUG();
2032 return -EINVAL;
2033 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002034
2035 /* prevent duplicates */
2036 if (find_rdev(mddev, rdev->bdev->bd_dev))
2037 return -EEXIST;
2038
Andre Nolldd8ac332009-03-31 14:33:13 +11002039 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2040 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2041 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002042 if (mddev->pers) {
2043 /* Cannot change size, so fail
2044 * If mddev->level <= 0, then we don't care
2045 * about aligning sizes (e.g. linear)
2046 */
2047 if (mddev->level > 0)
2048 return -ENOSPC;
2049 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002050 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 /* Verify rdev->desc_nr is unique.
2054 * If it is -1, assign a free number, else
2055 * check number is not in use
2056 */
2057 if (rdev->desc_nr < 0) {
2058 int choice = 0;
2059 if (mddev->pers) choice = mddev->raid_disks;
2060 while (find_rdev_nr(mddev, choice))
2061 choice++;
2062 rdev->desc_nr = choice;
2063 } else {
2064 if (find_rdev_nr(mddev, rdev->desc_nr))
2065 return -EBUSY;
2066 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002067 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2068 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2069 mdname(mddev), mddev->max_disks);
2070 return -EBUSY;
2071 }
NeilBrown19133a42005-11-08 21:39:35 -08002072 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002073 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002074 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002077 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002079 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081
Tejun Heo0762b8b2008-08-25 19:56:12 +09002082 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002083 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2084 /* failure here is OK */;
2085 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002086
NeilBrown4b809912008-07-21 17:05:25 +10002087 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002088 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002089
2090 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002091 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002094
2095 fail:
2096 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2097 b, mdname(mddev));
2098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
NeilBrown177a99b2008-02-06 01:39:56 -08002101static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002102{
NeilBrown3cb03002011-10-11 16:45:26 +11002103 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002104 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002105 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002106}
2107
NeilBrown3cb03002011-10-11 16:45:26 +11002108static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 char b[BDEVNAME_SIZE];
2111 if (!rdev->mddev) {
2112 MD_BUG();
2113 return;
2114 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002115 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002116 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2118 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002119 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002120 sysfs_put(rdev->sysfs_state);
2121 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002122 kfree(rdev->badblocks.page);
2123 rdev->badblocks.count = 0;
2124 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002125 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002126 * writing to 'remove' to "dev/state". We also need
2127 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002128 */
NeilBrown4b809912008-07-21 17:05:25 +10002129 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002130 INIT_WORK(&rdev->del_work, md_delayed_delete);
2131 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002132 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135/*
2136 * prevent the device from being mounted, repartitioned or
2137 * otherwise reused by a RAID array (or any other kernel
2138 * subsystem), by bd_claiming the device.
2139 */
NeilBrown3cb03002011-10-11 16:45:26 +11002140static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 int err = 0;
2143 struct block_device *bdev;
2144 char b[BDEVNAME_SIZE];
2145
Tejun Heod4d77622010-11-13 11:55:18 +01002146 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002147 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (IS_ERR(bdev)) {
2149 printk(KERN_ERR "md: could not open %s.\n",
2150 __bdevname(dev, b));
2151 return PTR_ERR(bdev);
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 rdev->bdev = bdev;
2154 return err;
2155}
2156
NeilBrown3cb03002011-10-11 16:45:26 +11002157static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct block_device *bdev = rdev->bdev;
2160 rdev->bdev = NULL;
2161 if (!bdev)
2162 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002163 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166void md_autodetect_dev(dev_t dev);
2167
NeilBrown3cb03002011-10-11 16:45:26 +11002168static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 char b[BDEVNAME_SIZE];
2171 printk(KERN_INFO "md: export_rdev(%s)\n",
2172 bdevname(rdev->bdev,b));
2173 if (rdev->mddev)
2174 MD_BUG();
2175 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002177 if (test_bit(AutoDetected, &rdev->flags))
2178 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179#endif
2180 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002181 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
NeilBrown3cb03002011-10-11 16:45:26 +11002184static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 unbind_rdev_from_array(rdev);
2187 export_rdev(rdev);
2188}
2189
NeilBrownfd01b882011-10-11 16:47:53 +11002190static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
NeilBrown3cb03002011-10-11 16:45:26 +11002192 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
NeilBrowndafb20f2012-03-19 12:46:39 +11002194 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (!rdev->mddev) {
2196 MD_BUG();
2197 continue;
2198 }
2199 kick_rdev_from_array(rdev);
2200 }
2201 if (!list_empty(&mddev->disks))
2202 MD_BUG();
2203 mddev->raid_disks = 0;
2204 mddev->major_version = 0;
2205}
2206
2207static void print_desc(mdp_disk_t *desc)
2208{
2209 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2210 desc->major,desc->minor,desc->raid_disk,desc->state);
2211}
2212
Cheng Renquancd2ac932009-01-09 08:31:08 +11002213static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 int i;
2216
2217 printk(KERN_INFO
2218 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2219 sb->major_version, sb->minor_version, sb->patch_version,
2220 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2221 sb->ctime);
2222 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2223 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2224 sb->md_minor, sb->layout, sb->chunk_size);
2225 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2226 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2227 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2228 sb->failed_disks, sb->spare_disks,
2229 sb->sb_csum, (unsigned long)sb->events_lo);
2230
2231 printk(KERN_INFO);
2232 for (i = 0; i < MD_SB_DISKS; i++) {
2233 mdp_disk_t *desc;
2234
2235 desc = sb->disks + i;
2236 if (desc->number || desc->major || desc->minor ||
2237 desc->raid_disk || (desc->state && (desc->state != 4))) {
2238 printk(" D %2d: ", i);
2239 print_desc(desc);
2240 }
2241 }
2242 printk(KERN_INFO "md: THIS: ");
2243 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Cheng Renquancd2ac932009-01-09 08:31:08 +11002246static void print_sb_1(struct mdp_superblock_1 *sb)
2247{
2248 __u8 *uuid;
2249
2250 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002251 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002252 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002253 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002254 le32_to_cpu(sb->major_version),
2255 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002256 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002257 sb->set_name,
2258 (unsigned long long)le64_to_cpu(sb->ctime)
2259 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2260
2261 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002262 printk(KERN_INFO
2263 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002264 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002265 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002266 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2267 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002268 le32_to_cpu(sb->level),
2269 (unsigned long long)le64_to_cpu(sb->size),
2270 le32_to_cpu(sb->raid_disks),
2271 le32_to_cpu(sb->layout),
2272 le32_to_cpu(sb->chunksize),
2273 (unsigned long long)le64_to_cpu(sb->data_offset),
2274 (unsigned long long)le64_to_cpu(sb->data_size),
2275 (unsigned long long)le64_to_cpu(sb->super_offset),
2276 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2277 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002278 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002279 sb->devflags,
2280 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2281 (unsigned long long)le64_to_cpu(sb->events),
2282 (unsigned long long)le64_to_cpu(sb->resync_offset),
2283 le32_to_cpu(sb->sb_csum),
2284 le32_to_cpu(sb->max_dev)
2285 );
2286}
2287
NeilBrown3cb03002011-10-11 16:45:26 +11002288static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002291 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2292 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002293 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2294 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002296 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2297 switch (major_version) {
2298 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002299 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002300 break;
2301 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002302 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002303 break;
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 } else
2306 printk(KERN_INFO "md: no rdev superblock!\n");
2307}
2308
Adrian Bunk5e563412006-06-26 00:27:42 -07002309static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002311 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002312 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002313 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 char b[BDEVNAME_SIZE];
2315
2316 printk("\n");
2317 printk("md: **********************************\n");
2318 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2319 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002320 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
NeilBrown32a76272005-06-21 17:17:14 -07002322 if (mddev->bitmap)
2323 bitmap_print_sb(mddev->bitmap);
2324 else
2325 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002326 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 printk("<%s>", bdevname(rdev->bdev,b));
2328 printk("\n");
2329
NeilBrowndafb20f2012-03-19 12:46:39 +11002330 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002331 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333 printk("md: **********************************\n");
2334 printk("\n");
2335}
2336
2337
NeilBrownfd01b882011-10-11 16:47:53 +11002338static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
NeilBrown42543762006-06-26 00:27:57 -07002340 /* Update each superblock (in-memory image), but
2341 * if we are allowed to, skip spares which already
2342 * have the right event counter, or have one earlier
2343 * (which would mean they aren't being marked as dirty
2344 * with the rest of the array)
2345 */
NeilBrown3cb03002011-10-11 16:45:26 +11002346 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002347 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002348 if (rdev->sb_events == mddev->events ||
2349 (nospares &&
2350 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002351 rdev->sb_events+1 == mddev->events)) {
2352 /* Don't update this superblock */
2353 rdev->sb_loaded = 2;
2354 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002355 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002356 rdev->sb_loaded = 1;
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359}
2360
NeilBrownfd01b882011-10-11 16:47:53 +11002361static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
NeilBrown3cb03002011-10-11 16:45:26 +11002363 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002364 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002365 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002366 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002369 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002370 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002371 if (rdev->raid_disk >= 0 &&
2372 mddev->delta_disks >= 0 &&
2373 !test_bit(In_sync, &rdev->flags) &&
2374 mddev->curr_resync_completed > rdev->recovery_offset)
2375 rdev->recovery_offset = mddev->curr_resync_completed;
2376
2377 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002378 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002379 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002380 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002381 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002382 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002383 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002384 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002385 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002386 md_ack_all_badblocks(&rdev->badblocks);
2387 md_error(mddev, rdev);
2388 }
2389 clear_bit(Blocked, &rdev->flags);
2390 clear_bit(BlockedBadBlocks, &rdev->flags);
2391 wake_up(&rdev->blocked_wait);
2392 }
2393 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002394 wake_up(&mddev->sb_wait);
2395 return;
2396 }
2397
NeilBrowna9701a32005-11-08 21:39:34 -08002398 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002399
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002400 mddev->utime = get_seconds();
2401
NeilBrown850b2b422006-10-03 01:15:46 -07002402 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2403 force_change = 1;
2404 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2405 /* just a clean<-> dirty transition, possibly leave spares alone,
2406 * though if events isn't the right even/odd, we will have to do
2407 * spares after all
2408 */
2409 nospares = 1;
2410 if (force_change)
2411 nospares = 0;
2412 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002413 /* If the array is degraded, then skipping spares is both
2414 * dangerous and fairly pointless.
2415 * Dangerous because a device that was removed from the array
2416 * might have a event_count that still looks up-to-date,
2417 * so it can be re-added without a resync.
2418 * Pointless because if there are any spares to skip,
2419 * then a recovery will happen and soon that array won't
2420 * be degraded any more and the spare can go back to sleep then.
2421 */
NeilBrown850b2b422006-10-03 01:15:46 -07002422 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002423
NeilBrown06d91a52005-06-21 17:17:12 -07002424 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002425
2426 /* If this is just a dirty<->clean transition, and the array is clean
2427 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002428 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002429 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002430 && mddev->can_decrease_events
2431 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002432 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002433 mddev->can_decrease_events = 0;
2434 } else {
NeilBrown42543762006-06-26 00:27:57 -07002435 /* otherwise we have to go forward and ... */
2436 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002437 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (!mddev->events) {
2441 /*
2442 * oops, this 64-bit counter should never wrap.
2443 * Either we are in around ~1 trillion A.C., assuming
2444 * 1 reboot per second, or we have a bug:
2445 */
2446 MD_BUG();
2447 mddev->events --;
2448 }
NeilBrown2699b672011-07-28 11:31:47 +10002449
NeilBrowndafb20f2012-03-19 12:46:39 +11002450 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002451 if (rdev->badblocks.changed)
2452 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002453 if (test_bit(Faulty, &rdev->flags))
2454 set_bit(FaultRecorded, &rdev->flags);
2455 }
NeilBrown2699b672011-07-28 11:31:47 +10002456
NeilBrowne6910632008-02-06 01:39:51 -08002457 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002458 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
NeilBrown36a4e1f2011-10-07 14:23:17 +11002460 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2461 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
NeilBrown4ad13662007-07-17 04:06:13 -07002463 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002464 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002466
NeilBrown42543762006-06-26 00:27:57 -07002467 if (rdev->sb_loaded != 1)
2468 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002470 if (!test_bit(Faulty, &rdev->flags) &&
2471 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002472 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002473 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002474 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002475 pr_debug("md: (write) %s's sb offset: %llu\n",
2476 bdevname(rdev->bdev, b),
2477 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002478 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002479 if (rdev->badblocks.size) {
2480 md_super_write(mddev, rdev,
2481 rdev->badblocks.sector,
2482 rdev->badblocks.size << 9,
2483 rdev->bb_page);
2484 rdev->badblocks.size = 0;
2485 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002486
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002487 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002488 pr_debug("md: %s (skipping faulty)\n",
2489 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002490 else
2491 pr_debug("(skipping incremental s/r ");
2492
NeilBrown7bfa19f2005-06-21 17:17:28 -07002493 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* only need to write one superblock... */
2495 break;
2496 }
NeilBrowna9701a32005-11-08 21:39:34 -08002497 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002498 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002499
NeilBrowna9701a32005-11-08 21:39:34 -08002500 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002501 if (mddev->in_sync != sync_req ||
2502 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002503 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002504 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002505 goto repeat;
2506 }
NeilBrown850b2b422006-10-03 01:15:46 -07002507 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002508 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002509 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002510 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2511 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002512
NeilBrowndafb20f2012-03-19 12:46:39 +11002513 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002514 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2515 clear_bit(Blocked, &rdev->flags);
2516
2517 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002518 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002519 clear_bit(BlockedBadBlocks, &rdev->flags);
2520 wake_up(&rdev->blocked_wait);
2521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Andre Noll7f6ce762008-03-23 18:34:54 +01002524/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002525 * We want to accept with case. For this we use cmd_match.
2526 */
2527static int cmd_match(const char *cmd, const char *str)
2528{
2529 /* See if cmd, written into a sysfs file, matches
2530 * str. They must either be the same, or cmd can
2531 * have a trailing newline
2532 */
2533 while (*cmd && *str && *cmd == *str) {
2534 cmd++;
2535 str++;
2536 }
2537 if (*cmd == '\n')
2538 cmd++;
2539 if (*str || *cmd)
2540 return 0;
2541 return 1;
2542}
2543
NeilBrown86e6ffd2005-11-08 21:39:24 -08002544struct rdev_sysfs_entry {
2545 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002546 ssize_t (*show)(struct md_rdev *, char *);
2547 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548};
2549
2550static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002551state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002552{
2553 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002554 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555
NeilBrownde393cd2011-07-28 11:31:48 +10002556 if (test_bit(Faulty, &rdev->flags) ||
2557 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002558 len+= sprintf(page+len, "%sfaulty",sep);
2559 sep = ",";
2560 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002561 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002562 len += sprintf(page+len, "%sin_sync",sep);
2563 sep = ",";
2564 }
NeilBrownf6556752006-06-26 00:28:01 -07002565 if (test_bit(WriteMostly, &rdev->flags)) {
2566 len += sprintf(page+len, "%swrite_mostly",sep);
2567 sep = ",";
2568 }
NeilBrownde393cd2011-07-28 11:31:48 +10002569 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002570 (rdev->badblocks.unacked_exist
2571 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002572 len += sprintf(page+len, "%sblocked", sep);
2573 sep = ",";
2574 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002575 if (!test_bit(Faulty, &rdev->flags) &&
2576 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577 len += sprintf(page+len, "%sspare", sep);
2578 sep = ",";
2579 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002580 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2581 len += sprintf(page+len, "%swrite_error", sep);
2582 sep = ",";
2583 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002584 if (test_bit(WantReplacement, &rdev->flags)) {
2585 len += sprintf(page+len, "%swant_replacement", sep);
2586 sep = ",";
2587 }
2588 if (test_bit(Replacement, &rdev->flags)) {
2589 len += sprintf(page+len, "%sreplacement", sep);
2590 sep = ",";
2591 }
2592
NeilBrown86e6ffd2005-11-08 21:39:24 -08002593 return len+sprintf(page+len, "\n");
2594}
2595
NeilBrown45dc2de2006-06-26 00:27:58 -07002596static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002597state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002598{
2599 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002600 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002601 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002602 * writemostly - sets write_mostly
2603 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002604 * blocked - sets the Blocked flags
2605 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002606 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002607 * write_error - sets WriteErrorSeen
2608 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002609 */
2610 int err = -EINVAL;
2611 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2612 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002613 if (test_bit(Faulty, &rdev->flags))
2614 err = 0;
2615 else
2616 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002617 } else if (cmd_match(buf, "remove")) {
2618 if (rdev->raid_disk >= 0)
2619 err = -EBUSY;
2620 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002621 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002622 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002623 if (mddev->pers)
2624 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002625 md_new_event(mddev);
2626 err = 0;
2627 }
NeilBrownf6556752006-06-26 00:28:01 -07002628 } else if (cmd_match(buf, "writemostly")) {
2629 set_bit(WriteMostly, &rdev->flags);
2630 err = 0;
2631 } else if (cmd_match(buf, "-writemostly")) {
2632 clear_bit(WriteMostly, &rdev->flags);
2633 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002634 } else if (cmd_match(buf, "blocked")) {
2635 set_bit(Blocked, &rdev->flags);
2636 err = 0;
2637 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002638 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002639 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002640 /* metadata handler doesn't understand badblocks,
2641 * so we need to fail the device
2642 */
2643 md_error(rdev->mddev, rdev);
2644 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002645 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002646 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002647 wake_up(&rdev->blocked_wait);
2648 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2649 md_wakeup_thread(rdev->mddev->thread);
2650
2651 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002652 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2653 set_bit(In_sync, &rdev->flags);
2654 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002655 } else if (cmd_match(buf, "write_error")) {
2656 set_bit(WriteErrorSeen, &rdev->flags);
2657 err = 0;
2658 } else if (cmd_match(buf, "-write_error")) {
2659 clear_bit(WriteErrorSeen, &rdev->flags);
2660 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002661 } else if (cmd_match(buf, "want_replacement")) {
2662 /* Any non-spare device that is not a replacement can
2663 * become want_replacement at any time, but we then need to
2664 * check if recovery is needed.
2665 */
2666 if (rdev->raid_disk >= 0 &&
2667 !test_bit(Replacement, &rdev->flags))
2668 set_bit(WantReplacement, &rdev->flags);
2669 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2670 md_wakeup_thread(rdev->mddev->thread);
2671 err = 0;
2672 } else if (cmd_match(buf, "-want_replacement")) {
2673 /* Clearing 'want_replacement' is always allowed.
2674 * Once replacements starts it is too late though.
2675 */
2676 err = 0;
2677 clear_bit(WantReplacement, &rdev->flags);
2678 } else if (cmd_match(buf, "replacement")) {
2679 /* Can only set a device as a replacement when array has not
2680 * yet been started. Once running, replacement is automatic
2681 * from spares, or by assigning 'slot'.
2682 */
2683 if (rdev->mddev->pers)
2684 err = -EBUSY;
2685 else {
2686 set_bit(Replacement, &rdev->flags);
2687 err = 0;
2688 }
2689 } else if (cmd_match(buf, "-replacement")) {
2690 /* Similarly, can only clear Replacement before start */
2691 if (rdev->mddev->pers)
2692 err = -EBUSY;
2693 else {
2694 clear_bit(Replacement, &rdev->flags);
2695 err = 0;
2696 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002697 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002698 if (!err)
2699 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002700 return err ? err : len;
2701}
NeilBrown80ca3a42006-07-10 04:44:18 -07002702static struct rdev_sysfs_entry rdev_state =
2703__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002704
2705static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002706errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002707{
2708 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2709}
2710
2711static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002712errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002713{
2714 char *e;
2715 unsigned long n = simple_strtoul(buf, &e, 10);
2716 if (*buf && (*e == 0 || *e == '\n')) {
2717 atomic_set(&rdev->corrected_errors, n);
2718 return len;
2719 }
2720 return -EINVAL;
2721}
2722static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002723__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724
NeilBrown014236d2006-01-06 00:20:55 -08002725static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002726slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002727{
2728 if (rdev->raid_disk < 0)
2729 return sprintf(page, "none\n");
2730 else
2731 return sprintf(page, "%d\n", rdev->raid_disk);
2732}
2733
2734static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002735slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002736{
2737 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002738 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002739 int slot = simple_strtoul(buf, &e, 10);
2740 if (strncmp(buf, "none", 4)==0)
2741 slot = -1;
2742 else if (e==buf || (*e && *e!= '\n'))
2743 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002744 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002745 /* Setting 'slot' on an active array requires also
2746 * updating the 'rd%d' link, and communicating
2747 * with the personality with ->hot_*_disk.
2748 * For now we only support removing
2749 * failed/spare devices. This normally happens automatically,
2750 * but not when the metadata is externally managed.
2751 */
NeilBrownc303da62008-02-06 01:39:51 -08002752 if (rdev->raid_disk == -1)
2753 return -EEXIST;
2754 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002755 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002756 return -EINVAL;
2757 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002758 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002759 if (err)
2760 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002761 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002762 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002763 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2764 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002765 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002766 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002767 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002768 */
2769
2770 if (rdev->raid_disk != -1)
2771 return -EBUSY;
2772
NeilBrownc6751b22011-02-02 11:57:13 +11002773 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2774 return -EBUSY;
2775
Neil Brown6c2fce22008-06-28 08:31:31 +10002776 if (rdev->mddev->pers->hot_add_disk == NULL)
2777 return -EINVAL;
2778
NeilBrownba1b41b2011-01-14 09:14:34 +11002779 if (slot >= rdev->mddev->raid_disks &&
2780 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2781 return -ENOSPC;
2782
Neil Brown6c2fce22008-06-28 08:31:31 +10002783 rdev->raid_disk = slot;
2784 if (test_bit(In_sync, &rdev->flags))
2785 rdev->saved_raid_disk = slot;
2786 else
2787 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002788 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002789 err = rdev->mddev->pers->
2790 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002791 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002792 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002793 return err;
Neil Brown52664732008-06-28 08:31:44 +10002794 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002795 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002796 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002797 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002798 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002799 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002800 if (slot >= rdev->mddev->raid_disks &&
2801 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002802 return -ENOSPC;
2803 rdev->raid_disk = slot;
2804 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002805 clear_bit(Faulty, &rdev->flags);
2806 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002807 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002808 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002809 }
NeilBrown014236d2006-01-06 00:20:55 -08002810 return len;
2811}
2812
2813
2814static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002815__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002816
NeilBrown93c8cad2006-01-06 00:20:56 -08002817static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002818offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002819{
Andrew Morton6961ece2006-01-06 00:20:59 -08002820 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002821}
2822
2823static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002824offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002825{
2826 char *e;
2827 unsigned long long offset = simple_strtoull(buf, &e, 10);
2828 if (e==buf || (*e && *e != '\n'))
2829 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002830 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002831 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002832 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002833 /* Must set offset before size, so overlap checks
2834 * can be sane */
2835 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002836 rdev->data_offset = offset;
2837 return len;
2838}
2839
2840static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002841__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002842
NeilBrown83303b62006-01-06 00:21:06 -08002843static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002844rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002845{
Andre Nolldd8ac332009-03-31 14:33:13 +11002846 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002847}
2848
NeilBrownc5d79ad2008-02-06 01:39:54 -08002849static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2850{
2851 /* check if two start/length pairs overlap */
2852 if (s1+l1 <= s2)
2853 return 0;
2854 if (s2+l2 <= s1)
2855 return 0;
2856 return 1;
2857}
2858
Dan Williamsb522adc2009-03-31 15:00:31 +11002859static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2860{
2861 unsigned long long blocks;
2862 sector_t new;
2863
2864 if (strict_strtoull(buf, 10, &blocks) < 0)
2865 return -EINVAL;
2866
2867 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2868 return -EINVAL; /* sector conversion overflow */
2869
2870 new = blocks * 2;
2871 if (new != blocks * 2)
2872 return -EINVAL; /* unsigned long long to sector_t overflow */
2873
2874 *sectors = new;
2875 return 0;
2876}
2877
NeilBrown83303b62006-01-06 00:21:06 -08002878static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002879rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002880{
NeilBrownfd01b882011-10-11 16:47:53 +11002881 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002882 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002883 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002884
Dan Williamsb522adc2009-03-31 15:00:31 +11002885 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002886 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002887 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002888 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002889 sectors = super_types[my_mddev->major_version].
2890 rdev_size_change(rdev, sectors);
2891 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002892 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002893 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002894 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002895 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002896 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002897 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002898 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002899
Andre Nolldd8ac332009-03-31 14:33:13 +11002900 rdev->sectors = sectors;
2901 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002902 /* need to check that all other rdevs with the same ->bdev
2903 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002904 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002905 * we have to change it back, we will have the lock again.
2906 */
NeilBrownfd01b882011-10-11 16:47:53 +11002907 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002909 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002910
NeilBrown27c529b2008-03-04 14:29:33 -08002911 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002912 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002913 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002914
2915 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002916 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002917 if (rdev->bdev == rdev2->bdev &&
2918 rdev != rdev2 &&
2919 overlaps(rdev->data_offset, rdev->sectors,
2920 rdev2->data_offset,
2921 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002922 overlap = 1;
2923 break;
2924 }
2925 mddev_unlock(mddev);
2926 if (overlap) {
2927 mddev_put(mddev);
2928 break;
2929 }
2930 }
NeilBrown27c529b2008-03-04 14:29:33 -08002931 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002932 if (overlap) {
2933 /* Someone else could have slipped in a size
2934 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002935 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002936 * safe, and trust userspace not to race with
2937 * itself
2938 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002939 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002940 return -EBUSY;
2941 }
2942 }
NeilBrown83303b62006-01-06 00:21:06 -08002943 return len;
2944}
2945
2946static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002947__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002948
Dan Williams06e3c812009-12-12 21:17:12 -07002949
NeilBrown3cb03002011-10-11 16:45:26 +11002950static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002951{
2952 unsigned long long recovery_start = rdev->recovery_offset;
2953
2954 if (test_bit(In_sync, &rdev->flags) ||
2955 recovery_start == MaxSector)
2956 return sprintf(page, "none\n");
2957
2958 return sprintf(page, "%llu\n", recovery_start);
2959}
2960
NeilBrown3cb03002011-10-11 16:45:26 +11002961static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002962{
2963 unsigned long long recovery_start;
2964
2965 if (cmd_match(buf, "none"))
2966 recovery_start = MaxSector;
2967 else if (strict_strtoull(buf, 10, &recovery_start))
2968 return -EINVAL;
2969
2970 if (rdev->mddev->pers &&
2971 rdev->raid_disk >= 0)
2972 return -EBUSY;
2973
2974 rdev->recovery_offset = recovery_start;
2975 if (recovery_start == MaxSector)
2976 set_bit(In_sync, &rdev->flags);
2977 else
2978 clear_bit(In_sync, &rdev->flags);
2979 return len;
2980}
2981
2982static struct rdev_sysfs_entry rdev_recovery_start =
2983__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2984
NeilBrown16c791a2011-07-28 11:31:47 +10002985
2986static ssize_t
2987badblocks_show(struct badblocks *bb, char *page, int unack);
2988static ssize_t
2989badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2990
NeilBrown3cb03002011-10-11 16:45:26 +11002991static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002992{
2993 return badblocks_show(&rdev->badblocks, page, 0);
2994}
NeilBrown3cb03002011-10-11 16:45:26 +11002995static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002996{
NeilBrownde393cd2011-07-28 11:31:48 +10002997 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2998 /* Maybe that ack was all we needed */
2999 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3000 wake_up(&rdev->blocked_wait);
3001 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003002}
3003static struct rdev_sysfs_entry rdev_bad_blocks =
3004__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3005
3006
NeilBrown3cb03002011-10-11 16:45:26 +11003007static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003008{
3009 return badblocks_show(&rdev->badblocks, page, 1);
3010}
NeilBrown3cb03002011-10-11 16:45:26 +11003011static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003012{
3013 return badblocks_store(&rdev->badblocks, page, len, 1);
3014}
3015static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3016__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3017
NeilBrown86e6ffd2005-11-08 21:39:24 -08003018static struct attribute *rdev_default_attrs[] = {
3019 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003020 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003021 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003022 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003023 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003024 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003025 &rdev_bad_blocks.attr,
3026 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003027 NULL,
3028};
3029static ssize_t
3030rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3031{
3032 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003033 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003034 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003035 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003036
3037 if (!entry->show)
3038 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003039
3040 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3041 if (!rv) {
3042 if (rdev->mddev == NULL)
3043 rv = -EBUSY;
3044 else
3045 rv = entry->show(rdev, page);
3046 mddev_unlock(mddev);
3047 }
3048 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003049}
3050
3051static ssize_t
3052rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3053 const char *page, size_t length)
3054{
3055 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003056 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003057 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003058 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003059
3060 if (!entry->store)
3061 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003062 if (!capable(CAP_SYS_ADMIN))
3063 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003064 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003065 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003066 if (rdev->mddev == NULL)
3067 rv = -EBUSY;
3068 else
3069 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003070 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003071 }
3072 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003073}
3074
3075static void rdev_free(struct kobject *ko)
3076{
NeilBrown3cb03002011-10-11 16:45:26 +11003077 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003078 kfree(rdev);
3079}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003080static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003081 .show = rdev_attr_show,
3082 .store = rdev_attr_store,
3083};
3084static struct kobj_type rdev_ktype = {
3085 .release = rdev_free,
3086 .sysfs_ops = &rdev_sysfs_ops,
3087 .default_attrs = rdev_default_attrs,
3088};
3089
NeilBrown3cb03002011-10-11 16:45:26 +11003090int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003091{
3092 rdev->desc_nr = -1;
3093 rdev->saved_raid_disk = -1;
3094 rdev->raid_disk = -1;
3095 rdev->flags = 0;
3096 rdev->data_offset = 0;
3097 rdev->sb_events = 0;
3098 rdev->last_read_error.tv_sec = 0;
3099 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003100 rdev->sb_loaded = 0;
3101 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003102 atomic_set(&rdev->nr_pending, 0);
3103 atomic_set(&rdev->read_errors, 0);
3104 atomic_set(&rdev->corrected_errors, 0);
3105
3106 INIT_LIST_HEAD(&rdev->same_set);
3107 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003108
3109 /* Add space to store bad block list.
3110 * This reserves the space even on arrays where it cannot
3111 * be used - I wonder if that matters
3112 */
3113 rdev->badblocks.count = 0;
3114 rdev->badblocks.shift = 0;
3115 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3116 seqlock_init(&rdev->badblocks.lock);
3117 if (rdev->badblocks.page == NULL)
3118 return -ENOMEM;
3119
3120 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003121}
3122EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123/*
3124 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3125 *
3126 * mark the device faulty if:
3127 *
3128 * - the device is nonexistent (zero size)
3129 * - the device has no valid superblock
3130 *
3131 * a faulty rdev _never_ has rdev->sb set.
3132 */
NeilBrown3cb03002011-10-11 16:45:26 +11003133static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
3135 char b[BDEVNAME_SIZE];
3136 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003137 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 sector_t size;
3139
NeilBrown9ffae0c2006-01-06 00:20:32 -08003140 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (!rdev) {
3142 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3143 return ERR_PTR(-ENOMEM);
3144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
NeilBrown2230dfe2011-07-28 11:31:46 +10003146 err = md_rdev_init(rdev);
3147 if (err)
3148 goto abort_free;
3149 err = alloc_disk_sb(rdev);
3150 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 goto abort_free;
3152
NeilBrownc5d79ad2008-02-06 01:39:54 -08003153 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (err)
3155 goto abort_free;
3156
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003157 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158
Mike Snitzer77304d22010-11-08 14:39:12 +01003159 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (!size) {
3161 printk(KERN_WARNING
3162 "md: %s has zero or unknown size, marking faulty!\n",
3163 bdevname(rdev->bdev,b));
3164 err = -EINVAL;
3165 goto abort_free;
3166 }
3167
3168 if (super_format >= 0) {
3169 err = super_types[super_format].
3170 load_super(rdev, NULL, super_minor);
3171 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003172 printk(KERN_WARNING
3173 "md: %s does not have a valid v%d.%d "
3174 "superblock, not importing!\n",
3175 bdevname(rdev->bdev,b),
3176 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 goto abort_free;
3178 }
3179 if (err < 0) {
3180 printk(KERN_WARNING
3181 "md: could not read %s's sb, not importing!\n",
3182 bdevname(rdev->bdev,b));
3183 goto abort_free;
3184 }
3185 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003186 if (super_format == -1)
3187 /* hot-add for 0.90, or non-persistent: so no badblocks */
3188 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return rdev;
3191
3192abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003193 if (rdev->bdev)
3194 unlock_rdev(rdev);
3195 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003196 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 kfree(rdev);
3198 return ERR_PTR(err);
3199}
3200
3201/*
3202 * Check a full RAID array for plausibility
3203 */
3204
3205
NeilBrownfd01b882011-10-11 16:47:53 +11003206static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003209 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 char b[BDEVNAME_SIZE];
3211
3212 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003213 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 switch (super_types[mddev->major_version].
3215 load_super(rdev, freshest, mddev->minor_version)) {
3216 case 1:
3217 freshest = rdev;
3218 break;
3219 case 0:
3220 break;
3221 default:
3222 printk( KERN_ERR \
3223 "md: fatal superblock inconsistency in %s"
3224 " -- removing from array\n",
3225 bdevname(rdev->bdev,b));
3226 kick_rdev_from_array(rdev);
3227 }
3228
3229
3230 super_types[mddev->major_version].
3231 validate_super(mddev, freshest);
3232
3233 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003234 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003235 if (mddev->max_disks &&
3236 (rdev->desc_nr >= mddev->max_disks ||
3237 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003238 printk(KERN_WARNING
3239 "md: %s: %s: only %d devices permitted\n",
3240 mdname(mddev), bdevname(rdev->bdev, b),
3241 mddev->max_disks);
3242 kick_rdev_from_array(rdev);
3243 continue;
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (rdev != freshest)
3246 if (super_types[mddev->major_version].
3247 validate_super(mddev, rdev)) {
3248 printk(KERN_WARNING "md: kicking non-fresh %s"
3249 " from array!\n",
3250 bdevname(rdev->bdev,b));
3251 kick_rdev_from_array(rdev);
3252 continue;
3253 }
3254 if (mddev->level == LEVEL_MULTIPATH) {
3255 rdev->desc_nr = i++;
3256 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003257 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003258 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003259 rdev->raid_disk = -1;
3260 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
NeilBrown72e02072009-12-14 12:49:55 +11003265/* Read a fixed-point number.
3266 * Numbers in sysfs attributes should be in "standard" units where
3267 * possible, so time should be in seconds.
3268 * However we internally use a a much smaller unit such as
3269 * milliseconds or jiffies.
3270 * This function takes a decimal number with a possible fractional
3271 * component, and produces an integer which is the result of
3272 * multiplying that number by 10^'scale'.
3273 * all without any floating-point arithmetic.
3274 */
3275int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3276{
3277 unsigned long result = 0;
3278 long decimals = -1;
3279 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3280 if (*cp == '.')
3281 decimals = 0;
3282 else if (decimals < scale) {
3283 unsigned int value;
3284 value = *cp - '0';
3285 result = result * 10 + value;
3286 if (decimals >= 0)
3287 decimals++;
3288 }
3289 cp++;
3290 }
3291 if (*cp == '\n')
3292 cp++;
3293 if (*cp)
3294 return -EINVAL;
3295 if (decimals < 0)
3296 decimals = 0;
3297 while (decimals < scale) {
3298 result *= 10;
3299 decimals ++;
3300 }
3301 *res = result;
3302 return 0;
3303}
3304
3305
NeilBrown19052c02008-08-05 15:54:13 +10003306static void md_safemode_timeout(unsigned long data);
3307
NeilBrowneae17012005-11-08 21:39:23 -08003308static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003309safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003310{
3311 int msec = (mddev->safemode_delay*1000)/HZ;
3312 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3313}
3314static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003315safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003316{
NeilBrown16f17b32006-06-26 00:27:37 -07003317 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003318
NeilBrown72e02072009-12-14 12:49:55 +11003319 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003320 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003321 if (msec == 0)
3322 mddev->safemode_delay = 0;
3323 else {
NeilBrown19052c02008-08-05 15:54:13 +10003324 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003325 mddev->safemode_delay = (msec*HZ)/1000;
3326 if (mddev->safemode_delay == 0)
3327 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003328 if (mddev->safemode_delay < old_delay)
3329 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003330 }
3331 return len;
3332}
3333static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003334__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003335
3336static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003337level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003338{
NeilBrown84fc4b52011-10-11 16:49:58 +11003339 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003340 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003341 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003342 else if (mddev->clevel[0])
3343 return sprintf(page, "%s\n", mddev->clevel);
3344 else if (mddev->level != LEVEL_NONE)
3345 return sprintf(page, "%d\n", mddev->level);
3346 else
3347 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003348}
3349
NeilBrownd9d166c2006-01-06 00:20:51 -08003350static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003351level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003352{
Dan Williamsf2859af2010-05-02 10:04:16 -07003353 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003354 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003355 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003356 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003357 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003358 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003359
3360 if (mddev->pers == NULL) {
3361 if (len == 0)
3362 return 0;
3363 if (len >= sizeof(mddev->clevel))
3364 return -ENOSPC;
3365 strncpy(mddev->clevel, buf, len);
3366 if (mddev->clevel[len-1] == '\n')
3367 len--;
3368 mddev->clevel[len] = 0;
3369 mddev->level = LEVEL_NONE;
3370 return rv;
3371 }
3372
3373 /* request to change the personality. Need to ensure:
3374 * - array is not engaged in resync/recovery/reshape
3375 * - old personality can be suspended
3376 * - new personality will access other array.
3377 */
3378
NeilBrownbb4f1e92010-08-08 21:18:03 +10003379 if (mddev->sync_thread ||
3380 mddev->reshape_position != MaxSector ||
3381 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003382 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003383
3384 if (!mddev->pers->quiesce) {
3385 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3386 mdname(mddev), mddev->pers->name);
3387 return -EINVAL;
3388 }
3389
3390 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003391 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003392 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003393 strncpy(clevel, buf, len);
3394 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003395 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003396 clevel[len] = 0;
3397 if (strict_strtol(clevel, 10, &level))
3398 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003399
Dan Williamsf2859af2010-05-02 10:04:16 -07003400 if (request_module("md-%s", clevel) != 0)
3401 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003402 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003403 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003404 if (!pers || !try_module_get(pers->owner)) {
3405 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003406 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003407 return -EINVAL;
3408 }
3409 spin_unlock(&pers_lock);
3410
3411 if (pers == mddev->pers) {
3412 /* Nothing to do! */
3413 module_put(pers->owner);
3414 return rv;
3415 }
3416 if (!pers->takeover) {
3417 module_put(pers->owner);
3418 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003419 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003420 return -EINVAL;
3421 }
3422
NeilBrowndafb20f2012-03-19 12:46:39 +11003423 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003424 rdev->new_raid_disk = rdev->raid_disk;
3425
NeilBrown245f46c2009-03-31 14:39:39 +11003426 /* ->takeover must set new_* and/or delta_disks
3427 * if it succeeds, and may set them when it fails.
3428 */
3429 priv = pers->takeover(mddev);
3430 if (IS_ERR(priv)) {
3431 mddev->new_level = mddev->level;
3432 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003433 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003434 mddev->raid_disks -= mddev->delta_disks;
3435 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003436 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003437 module_put(pers->owner);
3438 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003439 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003440 return PTR_ERR(priv);
3441 }
3442
3443 /* Looks like we have a winner */
3444 mddev_suspend(mddev);
3445 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003446
3447 if (mddev->pers->sync_request == NULL &&
3448 pers->sync_request != NULL) {
3449 /* need to add the md_redundancy_group */
3450 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3451 printk(KERN_WARNING
3452 "md: cannot register extra attributes for %s\n",
3453 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003454 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003455 }
3456 if (mddev->pers->sync_request != NULL &&
3457 pers->sync_request == NULL) {
3458 /* need to remove the md_redundancy_group */
3459 if (mddev->to_remove == NULL)
3460 mddev->to_remove = &md_redundancy_group;
3461 }
3462
Trela Maciej54071b32010-03-08 16:02:42 +11003463 if (mddev->pers->sync_request == NULL &&
3464 mddev->external) {
3465 /* We are converting from a no-redundancy array
3466 * to a redundancy array and metadata is managed
3467 * externally so we need to be sure that writes
3468 * won't block due to a need to transition
3469 * clean->dirty
3470 * until external management is started.
3471 */
3472 mddev->in_sync = 0;
3473 mddev->safemode_delay = 0;
3474 mddev->safemode = 0;
3475 }
3476
NeilBrowndafb20f2012-03-19 12:46:39 +11003477 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003478 if (rdev->raid_disk < 0)
3479 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003480 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003481 rdev->new_raid_disk = -1;
3482 if (rdev->new_raid_disk == rdev->raid_disk)
3483 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003484 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003485 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003486 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003487 if (rdev->raid_disk < 0)
3488 continue;
3489 if (rdev->new_raid_disk == rdev->raid_disk)
3490 continue;
3491 rdev->raid_disk = rdev->new_raid_disk;
3492 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003493 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003494 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003495 if (sysfs_link_rdev(mddev, rdev))
3496 printk(KERN_WARNING "md: cannot register rd%d"
3497 " for %s after level change\n",
3498 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003499 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003500 }
3501
3502 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003503 mddev->pers = pers;
3504 mddev->private = priv;
3505 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3506 mddev->level = mddev->new_level;
3507 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003508 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003509 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003510 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003511 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003512 if (mddev->pers->sync_request == NULL) {
3513 /* this is now an array without redundancy, so
3514 * it must always be in_sync
3515 */
3516 mddev->in_sync = 1;
3517 del_timer_sync(&mddev->safemode_timer);
3518 }
NeilBrown245f46c2009-03-31 14:39:39 +11003519 pers->run(mddev);
3520 mddev_resume(mddev);
3521 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3522 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3523 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003524 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003525 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003526 return rv;
3527}
3528
3529static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003530__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003531
NeilBrownd4dbd022006-06-26 00:27:59 -07003532
3533static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003534layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003535{
3536 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003537 if (mddev->reshape_position != MaxSector &&
3538 mddev->layout != mddev->new_layout)
3539 return sprintf(page, "%d (%d)\n",
3540 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003541 return sprintf(page, "%d\n", mddev->layout);
3542}
3543
3544static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003545layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003546{
3547 char *e;
3548 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003549
3550 if (!*buf || (*e && *e != '\n'))
3551 return -EINVAL;
3552
NeilBrownb3546032009-03-31 14:56:41 +11003553 if (mddev->pers) {
3554 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003555 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003556 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003557 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003558 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003559 if (err) {
3560 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003561 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003562 }
NeilBrownb3546032009-03-31 14:56:41 +11003563 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003564 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003565 if (mddev->reshape_position == MaxSector)
3566 mddev->layout = n;
3567 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003568 return len;
3569}
3570static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003571__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003572
3573
NeilBrowneae17012005-11-08 21:39:23 -08003574static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003575raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003576{
NeilBrownbb636542005-11-08 21:39:45 -08003577 if (mddev->raid_disks == 0)
3578 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003579 if (mddev->reshape_position != MaxSector &&
3580 mddev->delta_disks != 0)
3581 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3582 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003583 return sprintf(page, "%d\n", mddev->raid_disks);
3584}
3585
NeilBrownfd01b882011-10-11 16:47:53 +11003586static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003587
3588static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003589raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003590{
NeilBrownda943b992006-01-06 00:20:54 -08003591 char *e;
3592 int rv = 0;
3593 unsigned long n = simple_strtoul(buf, &e, 10);
3594
3595 if (!*buf || (*e && *e != '\n'))
3596 return -EINVAL;
3597
3598 if (mddev->pers)
3599 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003600 else if (mddev->reshape_position != MaxSector) {
3601 int olddisks = mddev->raid_disks - mddev->delta_disks;
3602 mddev->delta_disks = n - olddisks;
3603 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003604 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003605 } else
NeilBrownda943b992006-01-06 00:20:54 -08003606 mddev->raid_disks = n;
3607 return rv ? rv : len;
3608}
3609static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003610__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003611
NeilBrown24dd4692005-11-08 21:39:26 -08003612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003613chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003614{
NeilBrown08a02ec2007-05-09 02:35:38 -07003615 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003616 mddev->chunk_sectors != mddev->new_chunk_sectors)
3617 return sprintf(page, "%d (%d)\n",
3618 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003619 mddev->chunk_sectors << 9);
3620 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003621}
3622
3623static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003624chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003625{
NeilBrown3b343802006-01-06 00:20:47 -08003626 char *e;
3627 unsigned long n = simple_strtoul(buf, &e, 10);
3628
NeilBrown3b343802006-01-06 00:20:47 -08003629 if (!*buf || (*e && *e != '\n'))
3630 return -EINVAL;
3631
NeilBrownb3546032009-03-31 14:56:41 +11003632 if (mddev->pers) {
3633 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003634 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003635 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003636 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003637 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003638 if (err) {
3639 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003640 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003641 }
NeilBrownb3546032009-03-31 14:56:41 +11003642 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003643 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003644 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003645 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003646 }
NeilBrown3b343802006-01-06 00:20:47 -08003647 return len;
3648}
3649static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003650__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003651
NeilBrowna94213b2006-06-26 00:28:00 -07003652static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003653resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003654{
NeilBrownd1a7c502009-03-31 15:24:32 +11003655 if (mddev->recovery_cp == MaxSector)
3656 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003657 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3658}
3659
3660static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003661resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003662{
NeilBrowna94213b2006-06-26 00:28:00 -07003663 char *e;
3664 unsigned long long n = simple_strtoull(buf, &e, 10);
3665
NeilBrownb0986362011-05-11 15:52:21 +10003666 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003667 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003668 if (cmd_match(buf, "none"))
3669 n = MaxSector;
3670 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003671 return -EINVAL;
3672
3673 mddev->recovery_cp = n;
3674 return len;
3675}
3676static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003677__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003678
NeilBrown9e653b62006-06-26 00:27:58 -07003679/*
3680 * The array state can be:
3681 *
3682 * clear
3683 * No devices, no size, no level
3684 * Equivalent to STOP_ARRAY ioctl
3685 * inactive
3686 * May have some settings, but array is not active
3687 * all IO results in error
3688 * When written, doesn't tear down array, but just stops it
3689 * suspended (not supported yet)
3690 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003691 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003692 * readonly
3693 * no resync can happen. no superblocks get written.
3694 * write requests fail
3695 * read-auto
3696 * like readonly, but behaves like 'clean' on a write request.
3697 *
3698 * clean - no pending writes, but otherwise active.
3699 * When written to inactive array, starts without resync
3700 * If a write request arrives then
3701 * if metadata is known, mark 'dirty' and switch to 'active'.
3702 * if not known, block and switch to write-pending
3703 * If written to an active array that has pending writes, then fails.
3704 * active
3705 * fully active: IO and resync can be happening.
3706 * When written to inactive array, starts with resync
3707 *
3708 * write-pending
3709 * clean, but writes are blocked waiting for 'active' to be written.
3710 *
3711 * active-idle
3712 * like active, but no writes have been seen for a while (100msec).
3713 *
3714 */
3715enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3716 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003717static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003718 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3719 "write-pending", "active-idle", NULL };
3720
3721static int match_word(const char *word, char **list)
3722{
3723 int n;
3724 for (n=0; list[n]; n++)
3725 if (cmd_match(word, list[n]))
3726 break;
3727 return n;
3728}
3729
3730static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003731array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003732{
3733 enum array_state st = inactive;
3734
3735 if (mddev->pers)
3736 switch(mddev->ro) {
3737 case 1:
3738 st = readonly;
3739 break;
3740 case 2:
3741 st = read_auto;
3742 break;
3743 case 0:
3744 if (mddev->in_sync)
3745 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003746 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003747 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003748 else if (mddev->safemode)
3749 st = active_idle;
3750 else
3751 st = active;
3752 }
3753 else {
3754 if (list_empty(&mddev->disks) &&
3755 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003756 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003757 st = clear;
3758 else
3759 st = inactive;
3760 }
3761 return sprintf(page, "%s\n", array_states[st]);
3762}
3763
NeilBrownfd01b882011-10-11 16:47:53 +11003764static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3765static int md_set_readonly(struct mddev * mddev, int is_open);
3766static int do_md_run(struct mddev * mddev);
3767static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003768
3769static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003770array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003771{
3772 int err = -EINVAL;
3773 enum array_state st = match_word(buf, array_states);
3774 switch(st) {
3775 case bad_word:
3776 break;
3777 case clear:
3778 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003779 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003780 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003781 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003782 break;
3783 case inactive:
3784 /* stopping an active array */
3785 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003786 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003787 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003788 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003789 } else
3790 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003791 break;
3792 case suspended:
3793 break; /* not supported yet */
3794 case readonly:
3795 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003796 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003797 else {
3798 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003799 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003800 err = do_md_run(mddev);
3801 }
3802 break;
3803 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003804 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003805 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003806 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003807 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003808 err = restart_array(mddev);
3809 if (err == 0) {
3810 mddev->ro = 2;
3811 set_disk_ro(mddev->gendisk, 0);
3812 }
NeilBrown9e653b62006-06-26 00:27:58 -07003813 } else {
3814 mddev->ro = 2;
3815 err = do_md_run(mddev);
3816 }
3817 break;
3818 case clean:
3819 if (mddev->pers) {
3820 restart_array(mddev);
3821 spin_lock_irq(&mddev->write_lock);
3822 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003823 if (mddev->in_sync == 0) {
3824 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003825 if (mddev->safemode == 1)
3826 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003827 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003828 }
3829 err = 0;
3830 } else
3831 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003832 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003833 } else
3834 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003835 break;
3836 case active:
3837 if (mddev->pers) {
3838 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003839 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003840 wake_up(&mddev->sb_wait);
3841 err = 0;
3842 } else {
3843 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003844 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003845 err = do_md_run(mddev);
3846 }
3847 break;
3848 case write_pending:
3849 case active_idle:
3850 /* these cannot be set */
3851 break;
3852 }
3853 if (err)
3854 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003855 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003856 if (mddev->hold_active == UNTIL_IOCTL)
3857 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003858 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003859 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003860 }
NeilBrown9e653b62006-06-26 00:27:58 -07003861}
NeilBrown80ca3a42006-07-10 04:44:18 -07003862static struct md_sysfs_entry md_array_state =
3863__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003864
NeilBrown6d7ff7382006-01-06 00:21:16 -08003865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003866max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003867 return sprintf(page, "%d\n",
3868 atomic_read(&mddev->max_corr_read_errors));
3869}
3870
3871static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003872max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003873{
3874 char *e;
3875 unsigned long n = simple_strtoul(buf, &e, 10);
3876
3877 if (*buf && (*e == 0 || *e == '\n')) {
3878 atomic_set(&mddev->max_corr_read_errors, n);
3879 return len;
3880 }
3881 return -EINVAL;
3882}
3883
3884static struct md_sysfs_entry max_corr_read_errors =
3885__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3886 max_corrected_read_errors_store);
3887
3888static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003889null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003890{
3891 return -EINVAL;
3892}
3893
3894static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003895new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08003896{
3897 /* buf must be %d:%d\n? giving major and minor numbers */
3898 /* The new device is added to the array.
3899 * If the array has a persistent superblock, we read the
3900 * superblock to initialise info and check validity.
3901 * Otherwise, only checking done is that in bind_rdev_to_array,
3902 * which mainly checks size.
3903 */
3904 char *e;
3905 int major = simple_strtoul(buf, &e, 10);
3906 int minor;
3907 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003908 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08003909 int err;
3910
3911 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3912 return -EINVAL;
3913 minor = simple_strtoul(e+1, &e, 10);
3914 if (*e && *e != '\n')
3915 return -EINVAL;
3916 dev = MKDEV(major, minor);
3917 if (major != MAJOR(dev) ||
3918 minor != MINOR(dev))
3919 return -EOVERFLOW;
3920
3921
3922 if (mddev->persistent) {
3923 rdev = md_import_device(dev, mddev->major_version,
3924 mddev->minor_version);
3925 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003926 struct md_rdev *rdev0
3927 = list_entry(mddev->disks.next,
3928 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08003929 err = super_types[mddev->major_version]
3930 .load_super(rdev, rdev0, mddev->minor_version);
3931 if (err < 0)
3932 goto out;
3933 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003934 } else if (mddev->external)
3935 rdev = md_import_device(dev, -2, -1);
3936 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003937 rdev = md_import_device(dev, -1, -1);
3938
3939 if (IS_ERR(rdev))
3940 return PTR_ERR(rdev);
3941 err = bind_rdev_to_array(rdev, mddev);
3942 out:
3943 if (err)
3944 export_rdev(rdev);
3945 return err ? err : len;
3946}
3947
3948static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003949__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003950
3951static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003952bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003953{
3954 char *end;
3955 unsigned long chunk, end_chunk;
3956
3957 if (!mddev->bitmap)
3958 goto out;
3959 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3960 while (*buf) {
3961 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3962 if (buf == end) break;
3963 if (*end == '-') { /* range */
3964 buf = end + 1;
3965 end_chunk = simple_strtoul(buf, &end, 0);
3966 if (buf == end) break;
3967 }
3968 if (*end && !isspace(*end)) break;
3969 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003970 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003971 }
3972 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3973out:
3974 return len;
3975}
3976
3977static struct md_sysfs_entry md_bitmap =
3978__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3979
3980static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003981size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003982{
Andre Noll58c0fed2009-03-31 14:33:13 +11003983 return sprintf(page, "%llu\n",
3984 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003985}
3986
NeilBrownfd01b882011-10-11 16:47:53 +11003987static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003988
3989static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003990size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003991{
3992 /* If array is inactive, we can reduce the component size, but
3993 * not increase it (except from 0).
3994 * If array is active, we can try an on-line resize
3995 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003996 sector_t sectors;
3997 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003998
Andre Noll58c0fed2009-03-31 14:33:13 +11003999 if (err < 0)
4000 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004001 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004002 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004003 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004004 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004005 if (mddev->dev_sectors == 0 ||
4006 mddev->dev_sectors > sectors)
4007 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004008 else
4009 err = -ENOSPC;
4010 }
4011 return err ? err : len;
4012}
4013
4014static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004015__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004016
NeilBrown8bb93aa2006-01-06 00:20:50 -08004017
4018/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004019 * This is one of
4020 * 'none' for arrays with no metadata (good luck...)
4021 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004022 * or N.M for internally known formats
4023 */
4024static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004025metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004026{
4027 if (mddev->persistent)
4028 return sprintf(page, "%d.%d\n",
4029 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004030 else if (mddev->external)
4031 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004032 else
4033 return sprintf(page, "none\n");
4034}
4035
4036static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004037metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004038{
4039 int major, minor;
4040 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004041 /* Changing the details of 'external' metadata is
4042 * always permitted. Otherwise there must be
4043 * no devices attached to the array.
4044 */
4045 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4046 ;
4047 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004048 return -EBUSY;
4049
4050 if (cmd_match(buf, "none")) {
4051 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004052 mddev->external = 0;
4053 mddev->major_version = 0;
4054 mddev->minor_version = 90;
4055 return len;
4056 }
4057 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004058 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004059 if (namelen >= sizeof(mddev->metadata_type))
4060 namelen = sizeof(mddev->metadata_type)-1;
4061 strncpy(mddev->metadata_type, buf+9, namelen);
4062 mddev->metadata_type[namelen] = 0;
4063 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4064 mddev->metadata_type[--namelen] = 0;
4065 mddev->persistent = 0;
4066 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004067 mddev->major_version = 0;
4068 mddev->minor_version = 90;
4069 return len;
4070 }
4071 major = simple_strtoul(buf, &e, 10);
4072 if (e==buf || *e != '.')
4073 return -EINVAL;
4074 buf = e+1;
4075 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004076 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004077 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004078 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004079 return -ENOENT;
4080 mddev->major_version = major;
4081 mddev->minor_version = minor;
4082 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004083 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004084 return len;
4085}
4086
4087static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004088__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004089
NeilBrowna35b0d62006-01-06 00:20:49 -08004090static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004091action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004092{
NeilBrown7eec3142005-11-08 21:39:44 -08004093 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004094 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4095 type = "frozen";
4096 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004097 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004098 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4099 type = "reshape";
4100 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004101 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4102 type = "resync";
4103 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4104 type = "check";
4105 else
4106 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004107 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004108 type = "recover";
4109 }
4110 return sprintf(page, "%s\n", type);
4111}
4112
NeilBrownfd01b882011-10-11 16:47:53 +11004113static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004114
NeilBrown24dd4692005-11-08 21:39:26 -08004115static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004116action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004117{
NeilBrown7eec3142005-11-08 21:39:44 -08004118 if (!mddev->pers || !mddev->pers->sync_request)
4119 return -EINVAL;
4120
NeilBrownb6a9ce62009-05-26 09:41:17 +10004121 if (cmd_match(page, "frozen"))
4122 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4123 else
4124 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4125
4126 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004127 if (mddev->sync_thread) {
4128 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004129 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004130 }
NeilBrown03c902e2006-01-06 00:20:46 -08004131 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4132 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004133 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004134 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004135 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004136 else if (cmd_match(page, "recover")) {
4137 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4138 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4139 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004140 int err;
4141 if (mddev->pers->start_reshape == NULL)
4142 return -EINVAL;
4143 err = mddev->pers->start_reshape(mddev);
4144 if (err)
4145 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004146 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004147 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004148 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004149 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004150 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004151 return -EINVAL;
4152 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4153 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004154 }
NeilBrown03c902e2006-01-06 00:20:46 -08004155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004156 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004157 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004158 return len;
4159}
4160
NeilBrown9d888832005-11-08 21:39:26 -08004161static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004162mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004163{
4164 return sprintf(page, "%llu\n",
4165 (unsigned long long) mddev->resync_mismatches);
4166}
4167
NeilBrown80ca3a42006-07-10 04:44:18 -07004168static struct md_sysfs_entry md_scan_mode =
4169__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004170
NeilBrown96de1e62005-11-08 21:39:39 -08004171
NeilBrown80ca3a42006-07-10 04:44:18 -07004172static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004173
NeilBrown88202a02006-01-06 00:21:36 -08004174static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004175sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004176{
4177 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4178 mddev->sync_speed_min ? "local": "system");
4179}
4180
4181static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004182sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004183{
4184 int min;
4185 char *e;
4186 if (strncmp(buf, "system", 6)==0) {
4187 mddev->sync_speed_min = 0;
4188 return len;
4189 }
4190 min = simple_strtoul(buf, &e, 10);
4191 if (buf == e || (*e && *e != '\n') || min <= 0)
4192 return -EINVAL;
4193 mddev->sync_speed_min = min;
4194 return len;
4195}
4196
4197static struct md_sysfs_entry md_sync_min =
4198__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4199
4200static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004201sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004202{
4203 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4204 mddev->sync_speed_max ? "local": "system");
4205}
4206
4207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004209{
4210 int max;
4211 char *e;
4212 if (strncmp(buf, "system", 6)==0) {
4213 mddev->sync_speed_max = 0;
4214 return len;
4215 }
4216 max = simple_strtoul(buf, &e, 10);
4217 if (buf == e || (*e && *e != '\n') || max <= 0)
4218 return -EINVAL;
4219 mddev->sync_speed_max = max;
4220 return len;
4221}
4222
4223static struct md_sysfs_entry md_sync_max =
4224__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4225
Iustin Popd7f3d292007-10-16 23:30:54 -07004226static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004227degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004228{
4229 return sprintf(page, "%d\n", mddev->degraded);
4230}
4231static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004232
4233static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004234sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004235{
4236 return sprintf(page, "%d\n", mddev->parallel_resync);
4237}
4238
4239static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004240sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004241{
4242 long n;
4243
4244 if (strict_strtol(buf, 10, &n))
4245 return -EINVAL;
4246
4247 if (n != 0 && n != 1)
4248 return -EINVAL;
4249
4250 mddev->parallel_resync = n;
4251
4252 if (mddev->sync_thread)
4253 wake_up(&resync_wait);
4254
4255 return len;
4256}
4257
4258/* force parallel resync, even with shared block devices */
4259static struct md_sysfs_entry md_sync_force_parallel =
4260__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4261 sync_force_parallel_show, sync_force_parallel_store);
4262
4263static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004264sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004265{
4266 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004267 if (mddev->curr_resync == 0)
4268 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004269 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4270 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004271 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004272 db = resync - mddev->resync_mark_cnt;
4273 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004274}
4275
NeilBrown80ca3a42006-07-10 04:44:18 -07004276static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004277
4278static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004279sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004280{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004281 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004282
NeilBrownacb180b2009-04-14 16:28:34 +10004283 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4284 return sprintf(page, "none\n");
4285
NeilBrown88202a02006-01-06 00:21:36 -08004286 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004287 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004288 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004289 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004290
NeilBrownacb180b2009-04-14 16:28:34 +10004291 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004292 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004293}
4294
NeilBrown80ca3a42006-07-10 04:44:18 -07004295static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004296
NeilBrowne464eaf2006-03-27 01:18:14 -08004297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004298min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004299{
4300 return sprintf(page, "%llu\n",
4301 (unsigned long long)mddev->resync_min);
4302}
4303static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004304min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004305{
4306 unsigned long long min;
4307 if (strict_strtoull(buf, 10, &min))
4308 return -EINVAL;
4309 if (min > mddev->resync_max)
4310 return -EINVAL;
4311 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4312 return -EBUSY;
4313
4314 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004315 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004316 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004317 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004318 return -EINVAL;
4319 }
4320 mddev->resync_min = min;
4321
4322 return len;
4323}
4324
4325static struct md_sysfs_entry md_min_sync =
4326__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4327
4328static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004329max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004330{
4331 if (mddev->resync_max == MaxSector)
4332 return sprintf(page, "max\n");
4333 else
4334 return sprintf(page, "%llu\n",
4335 (unsigned long long)mddev->resync_max);
4336}
4337static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004338max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004339{
4340 if (strncmp(buf, "max", 3) == 0)
4341 mddev->resync_max = MaxSector;
4342 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004343 unsigned long long max;
4344 if (strict_strtoull(buf, 10, &max))
4345 return -EINVAL;
4346 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004347 return -EINVAL;
4348 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004349 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004350 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4351 return -EBUSY;
4352
4353 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004354 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004355 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004356 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004357 return -EINVAL;
4358 }
4359 mddev->resync_max = max;
4360 }
4361 wake_up(&mddev->recovery_wait);
4362 return len;
4363}
4364
4365static struct md_sysfs_entry md_max_sync =
4366__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4367
4368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004370{
4371 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4372}
4373
4374static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004375suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004376{
4377 char *e;
4378 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004379 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004380
NeilBrownb8d966e2009-07-01 11:14:04 +10004381 if (mddev->pers == NULL ||
4382 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004383 return -EINVAL;
4384 if (buf == e || (*e && *e != '\n'))
4385 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004386
4387 mddev->suspend_lo = new;
4388 if (new >= old)
4389 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004390 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004391 else {
4392 /* Expanding suspended region - need to wait */
4393 mddev->pers->quiesce(mddev, 1);
4394 mddev->pers->quiesce(mddev, 0);
4395 }
4396 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004397}
4398static struct md_sysfs_entry md_suspend_lo =
4399__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4400
4401
4402static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004403suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004404{
4405 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4406}
4407
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004410{
4411 char *e;
4412 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004413 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004414
NeilBrownb8d966e2009-07-01 11:14:04 +10004415 if (mddev->pers == NULL ||
4416 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004417 return -EINVAL;
4418 if (buf == e || (*e && *e != '\n'))
4419 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004420
4421 mddev->suspend_hi = new;
4422 if (new <= old)
4423 /* Shrinking suspended region */
4424 mddev->pers->quiesce(mddev, 2);
4425 else {
4426 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004427 mddev->pers->quiesce(mddev, 1);
4428 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004429 }
4430 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004431}
4432static struct md_sysfs_entry md_suspend_hi =
4433__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4434
NeilBrown08a02ec2007-05-09 02:35:38 -07004435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004436reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004437{
4438 if (mddev->reshape_position != MaxSector)
4439 return sprintf(page, "%llu\n",
4440 (unsigned long long)mddev->reshape_position);
4441 strcpy(page, "none\n");
4442 return 5;
4443}
4444
4445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004446reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004447{
4448 char *e;
4449 unsigned long long new = simple_strtoull(buf, &e, 10);
4450 if (mddev->pers)
4451 return -EBUSY;
4452 if (buf == e || (*e && *e != '\n'))
4453 return -EINVAL;
4454 mddev->reshape_position = new;
4455 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004456 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004457 mddev->new_level = mddev->level;
4458 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004459 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004460 return len;
4461}
4462
4463static struct md_sysfs_entry md_reshape_position =
4464__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4465 reshape_position_store);
4466
Dan Williamsb522adc2009-03-31 15:00:31 +11004467static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004468reshape_direction_show(struct mddev *mddev, char *page)
4469{
4470 return sprintf(page, "%s\n",
4471 mddev->reshape_backwards ? "backwards" : "forwards");
4472}
4473
4474static ssize_t
4475reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4476{
4477 int backwards = 0;
4478 if (cmd_match(buf, "forwards"))
4479 backwards = 0;
4480 else if (cmd_match(buf, "backwards"))
4481 backwards = 1;
4482 else
4483 return -EINVAL;
4484 if (mddev->reshape_backwards == backwards)
4485 return len;
4486
4487 /* check if we are allowed to change */
4488 if (mddev->delta_disks)
4489 return -EBUSY;
4490
4491 if (mddev->persistent &&
4492 mddev->major_version == 0)
4493 return -EINVAL;
4494
4495 mddev->reshape_backwards = backwards;
4496 return len;
4497}
4498
4499static struct md_sysfs_entry md_reshape_direction =
4500__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4501 reshape_direction_store);
4502
4503static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004504array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004505{
4506 if (mddev->external_size)
4507 return sprintf(page, "%llu\n",
4508 (unsigned long long)mddev->array_sectors/2);
4509 else
4510 return sprintf(page, "default\n");
4511}
4512
4513static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004514array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004515{
4516 sector_t sectors;
4517
4518 if (strncmp(buf, "default", 7) == 0) {
4519 if (mddev->pers)
4520 sectors = mddev->pers->size(mddev, 0, 0);
4521 else
4522 sectors = mddev->array_sectors;
4523
4524 mddev->external_size = 0;
4525 } else {
4526 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4527 return -EINVAL;
4528 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004529 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004530
4531 mddev->external_size = 1;
4532 }
4533
4534 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004535 if (mddev->pers) {
4536 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004537 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004538 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004539 return len;
4540}
4541
4542static struct md_sysfs_entry md_array_size =
4543__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4544 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004545
NeilBrowneae17012005-11-08 21:39:23 -08004546static struct attribute *md_default_attrs[] = {
4547 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004548 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004549 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004550 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004551 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004552 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004553 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004554 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004555 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004556 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004557 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004558 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004559 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004560 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004561 NULL,
4562};
4563
4564static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004565 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004566 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004567 &md_sync_min.attr,
4568 &md_sync_max.attr,
4569 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004570 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004571 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004572 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004573 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004574 &md_suspend_lo.attr,
4575 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004576 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004577 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004578 NULL,
4579};
NeilBrown411036f2005-11-08 21:39:40 -08004580static struct attribute_group md_redundancy_group = {
4581 .name = NULL,
4582 .attrs = md_redundancy_attrs,
4583};
4584
NeilBrowneae17012005-11-08 21:39:23 -08004585
4586static ssize_t
4587md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4588{
4589 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004590 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004591 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004592
4593 if (!entry->show)
4594 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004595 spin_lock(&all_mddevs_lock);
4596 if (list_empty(&mddev->all_mddevs)) {
4597 spin_unlock(&all_mddevs_lock);
4598 return -EBUSY;
4599 }
4600 mddev_get(mddev);
4601 spin_unlock(&all_mddevs_lock);
4602
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004603 rv = mddev_lock(mddev);
4604 if (!rv) {
4605 rv = entry->show(mddev, page);
4606 mddev_unlock(mddev);
4607 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004608 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004609 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004610}
4611
4612static ssize_t
4613md_attr_store(struct kobject *kobj, struct attribute *attr,
4614 const char *page, size_t length)
4615{
4616 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004617 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004618 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004619
4620 if (!entry->store)
4621 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004622 if (!capable(CAP_SYS_ADMIN))
4623 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004624 spin_lock(&all_mddevs_lock);
4625 if (list_empty(&mddev->all_mddevs)) {
4626 spin_unlock(&all_mddevs_lock);
4627 return -EBUSY;
4628 }
4629 mddev_get(mddev);
4630 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004631 rv = mddev_lock(mddev);
4632 if (!rv) {
4633 rv = entry->store(mddev, page, length);
4634 mddev_unlock(mddev);
4635 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004636 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004637 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004638}
4639
4640static void md_free(struct kobject *ko)
4641{
NeilBrownfd01b882011-10-11 16:47:53 +11004642 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004643
4644 if (mddev->sysfs_state)
4645 sysfs_put(mddev->sysfs_state);
4646
4647 if (mddev->gendisk) {
4648 del_gendisk(mddev->gendisk);
4649 put_disk(mddev->gendisk);
4650 }
4651 if (mddev->queue)
4652 blk_cleanup_queue(mddev->queue);
4653
NeilBrowneae17012005-11-08 21:39:23 -08004654 kfree(mddev);
4655}
4656
Emese Revfy52cf25d2010-01-19 02:58:23 +01004657static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004658 .show = md_attr_show,
4659 .store = md_attr_store,
4660};
4661static struct kobj_type md_ktype = {
4662 .release = md_free,
4663 .sysfs_ops = &md_sysfs_ops,
4664 .default_attrs = md_default_attrs,
4665};
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667int mdp_major = 0;
4668
Dan Williams5fd3a172009-03-04 00:57:25 -07004669static void mddev_delayed_delete(struct work_struct *ws)
4670{
NeilBrownfd01b882011-10-11 16:47:53 +11004671 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004672
NeilBrown43a70502009-12-14 12:49:55 +11004673 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004674 kobject_del(&mddev->kobj);
4675 kobject_put(&mddev->kobj);
4676}
4677
NeilBrownefeb53c2009-01-09 08:31:10 +11004678static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004680 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004681 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004683 int partitioned;
4684 int shift;
4685 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004686 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004689 return -ENODEV;
4690
4691 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4692 shift = partitioned ? MdpMinorShift : 0;
4693 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Tejun Heoe804ac72010-10-15 15:36:08 +02004695 /* wait for any previous instance of this device to be
4696 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004697 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004698 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004699
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004700 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004701 error = -EEXIST;
4702 if (mddev->gendisk)
4703 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004704
4705 if (name) {
4706 /* Need to ensure that 'name' is not a duplicate.
4707 */
NeilBrownfd01b882011-10-11 16:47:53 +11004708 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004709 spin_lock(&all_mddevs_lock);
4710
4711 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4712 if (mddev2->gendisk &&
4713 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4714 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004715 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004716 }
4717 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
NeilBrown8b765392009-01-09 08:31:08 +11004719
NeilBrown0909dc42009-07-01 12:27:21 +10004720 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004721 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004722 if (!mddev->queue)
4723 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004724 mddev->queue->queuedata = mddev;
4725
NeilBrown409c57f2009-03-31 14:39:39 +11004726 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004727 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 disk = alloc_disk(1 << shift);
4730 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004731 blk_cleanup_queue(mddev->queue);
4732 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004733 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004735 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004737 if (name)
4738 strcpy(disk->disk_name, name);
4739 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004741 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 disk->fops = &md_fops;
4744 disk->private_data = mddev;
4745 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004746 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004747 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004748 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004749 * remove it now.
4750 */
4751 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004753 /* As soon as we call add_disk(), another thread could get
4754 * through to md_open, so make sure it doesn't get too far
4755 */
4756 mutex_lock(&mddev->open_mutex);
4757 add_disk(disk);
4758
Tejun Heoed9e1982008-08-25 19:56:05 +09004759 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4760 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004761 if (error) {
4762 /* This isn't possible, but as kobject_init_and_add is marked
4763 * __must_check, we must do something with the result
4764 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004765 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4766 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004767 error = 0;
4768 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004769 if (mddev->kobj.sd &&
4770 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004771 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004772 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004773 abort:
4774 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004775 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004776 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004777 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004778 }
NeilBrownd3374822009-01-09 08:31:10 +11004779 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004780 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004781}
4782
4783static struct kobject *md_probe(dev_t dev, int *part, void *data)
4784{
4785 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 return NULL;
4787}
4788
NeilBrownefeb53c2009-01-09 08:31:10 +11004789static int add_named_array(const char *val, struct kernel_param *kp)
4790{
4791 /* val must be "md_*" where * is not all digits.
4792 * We allocate an array with a large free minor number, and
4793 * set the name to val. val must not already be an active name.
4794 */
4795 int len = strlen(val);
4796 char buf[DISK_NAME_LEN];
4797
4798 while (len && val[len-1] == '\n')
4799 len--;
4800 if (len >= DISK_NAME_LEN)
4801 return -E2BIG;
4802 strlcpy(buf, val, len+1);
4803 if (strncmp(buf, "md_", 3) != 0)
4804 return -EINVAL;
4805 return md_alloc(0, buf);
4806}
4807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808static void md_safemode_timeout(unsigned long data)
4809{
NeilBrownfd01b882011-10-11 16:47:53 +11004810 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Neil Brown0fd62b82008-06-28 08:31:36 +10004812 if (!atomic_read(&mddev->writes_pending)) {
4813 mddev->safemode = 1;
4814 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004815 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 md_wakeup_thread(mddev->thread);
4818}
4819
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004820static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
NeilBrownfd01b882011-10-11 16:47:53 +11004822int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
NeilBrown2604b702006-01-06 00:20:36 -08004824 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004825 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004826 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
NeilBrowna757e642005-04-16 15:26:42 -07004828 if (list_empty(&mddev->disks))
4829 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
4832 if (mddev->pers)
4833 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004834 /* Cannot run until previous stop completes properly */
4835 if (mddev->sysfs_active)
4836 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 /*
4839 * Analyze all RAID superblock(s)
4840 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004841 if (!mddev->raid_disks) {
4842 if (!mddev->persistent)
4843 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004844 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
NeilBrownd9d166c2006-01-06 00:20:51 -08004847 if (mddev->level != LEVEL_NONE)
4848 request_module("md-level-%d", mddev->level);
4849 else if (mddev->clevel[0])
4850 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
4852 /*
4853 * Drop all container device buffers, from now on
4854 * the only valid external interface is through the md
4855 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004857 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004858 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 continue;
4860 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004861 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004862
4863 /* perform some consistency tests on the device.
4864 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004865 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004866 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004867 if (rdev->meta_bdev) {
4868 /* Nothing to check */;
4869 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004870 if (mddev->dev_sectors &&
4871 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004872 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004873 printk("md: %s: data overlaps metadata\n",
4874 mdname(mddev));
4875 return -EINVAL;
4876 }
4877 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004878 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004879 > rdev->data_offset) {
4880 printk("md: %s: metadata overlaps data\n",
4881 mdname(mddev));
4882 return -EINVAL;
4883 }
4884 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004885 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
4887
NeilBrowna167f662010-10-26 18:31:13 +11004888 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004889 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004890 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004893 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004894 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004896 if (mddev->level != LEVEL_NONE)
4897 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4898 mddev->level);
4899 else
4900 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4901 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return -EINVAL;
4903 }
NeilBrown2604b702006-01-06 00:20:36 -08004904 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004906 if (mddev->level != pers->level) {
4907 mddev->level = pers->level;
4908 mddev->new_level = pers->level;
4909 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004910 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
NeilBrownf6705572006-03-27 01:18:11 -08004912 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004913 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004914 /* This personality cannot handle reshaping... */
4915 mddev->pers = NULL;
4916 module_put(pers->owner);
4917 return -EINVAL;
4918 }
4919
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004920 if (pers->sync_request) {
4921 /* Warn if this is a potentially silly
4922 * configuration.
4923 */
4924 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004925 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004926 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004927
NeilBrowndafb20f2012-03-19 12:46:39 +11004928 rdev_for_each(rdev, mddev)
4929 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004930 if (rdev < rdev2 &&
4931 rdev->bdev->bd_contains ==
4932 rdev2->bdev->bd_contains) {
4933 printk(KERN_WARNING
4934 "%s: WARNING: %s appears to be"
4935 " on the same physical disk as"
4936 " %s.\n",
4937 mdname(mddev),
4938 bdevname(rdev->bdev,b),
4939 bdevname(rdev2->bdev,b2));
4940 warned = 1;
4941 }
4942 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004943
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004944 if (warned)
4945 printk(KERN_WARNING
4946 "True protection against single-disk"
4947 " failure might be compromised.\n");
4948 }
4949
NeilBrown657390d2005-08-26 18:34:16 -07004950 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004951 /* may be over-ridden by personality */
4952 mddev->resync_max_sectors = mddev->dev_sectors;
4953
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004954 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
NeilBrown0f9552b52009-12-30 12:08:50 +11004956 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004957 mddev->ro = 2; /* read-only, but switch on first write */
4958
NeilBrownb15c2e52006-01-06 00:20:16 -08004959 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004960 if (err)
4961 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004962 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4963 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4964 " but 'external_size' not in effect?\n", __func__);
4965 printk(KERN_ERR
4966 "md: invalid array_size %llu > default size %llu\n",
4967 (unsigned long long)mddev->array_sectors / 2,
4968 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4969 err = -EINVAL;
4970 mddev->pers->stop(mddev);
4971 }
4972 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004973 err = bitmap_create(mddev);
4974 if (err) {
4975 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4976 mdname(mddev), err);
4977 mddev->pers->stop(mddev);
4978 }
4979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 module_put(mddev->pers->owner);
4982 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004983 bitmap_destroy(mddev);
4984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004986 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004987 if (mddev->kobj.sd &&
4988 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004989 printk(KERN_WARNING
4990 "md: cannot register extra attributes for %s\n",
4991 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004992 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004993 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004994 mddev->ro = 0;
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004997 atomic_set(&mddev->max_corr_read_errors,
4998 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 mddev->safemode = 0;
5000 mddev->safemode_timer.function = md_safemode_timeout;
5001 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005002 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005004 smp_wmb();
5005 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005006 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005007 if (rdev->raid_disk >= 0)
5008 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005009 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5012
NeilBrown850b2b422006-10-03 01:15:46 -07005013 if (mddev->flags)
5014 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
NeilBrownd7603b72006-01-06 00:20:30 -08005016 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005017 sysfs_notify_dirent_safe(mddev->sysfs_state);
5018 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005019 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 return 0;
5021}
NeilBrown390ee602010-06-01 19:37:27 +10005022EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrownfd01b882011-10-11 16:47:53 +11005024static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005025{
5026 int err;
5027
5028 err = md_run(mddev);
5029 if (err)
5030 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005031 err = bitmap_load(mddev);
5032 if (err) {
5033 bitmap_destroy(mddev);
5034 goto out;
5035 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005036
5037 md_wakeup_thread(mddev->thread);
5038 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5039
NeilBrownfe60b012010-03-29 11:10:42 +11005040 set_capacity(mddev->gendisk, mddev->array_sectors);
5041 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005042 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005043 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5044out:
5045 return err;
5046}
5047
NeilBrownfd01b882011-10-11 16:47:53 +11005048static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
5050 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Andre Noll80fab1d2008-07-11 22:02:21 +10005052 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005054 return -ENXIO;
5055 if (!mddev->pers)
5056 return -EINVAL;
5057 if (!mddev->ro)
5058 return -EBUSY;
5059 mddev->safemode = 0;
5060 mddev->ro = 0;
5061 set_disk_ro(disk, 0);
5062 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5063 mdname(mddev));
5064 /* Kick recovery or resync if necessary */
5065 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5066 md_wakeup_thread(mddev->thread);
5067 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005068 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070}
5071
NeilBrownacc55e22006-06-26 00:27:47 -07005072/* similar to deny_write_access, but accounts for our holding a reference
5073 * to the file ourselves */
5074static int deny_bitmap_write_access(struct file * file)
5075{
5076 struct inode *inode = file->f_mapping->host;
5077
5078 spin_lock(&inode->i_lock);
5079 if (atomic_read(&inode->i_writecount) > 1) {
5080 spin_unlock(&inode->i_lock);
5081 return -ETXTBSY;
5082 }
5083 atomic_set(&inode->i_writecount, -1);
5084 spin_unlock(&inode->i_lock);
5085
5086 return 0;
5087}
5088
NeilBrown43a70502009-12-14 12:49:55 +11005089void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005090{
5091 struct inode *inode = file->f_mapping->host;
5092
5093 spin_lock(&inode->i_lock);
5094 atomic_set(&inode->i_writecount, 1);
5095 spin_unlock(&inode->i_lock);
5096}
5097
NeilBrownfd01b882011-10-11 16:47:53 +11005098static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005099{
5100 mddev->array_sectors = 0;
5101 mddev->external_size = 0;
5102 mddev->dev_sectors = 0;
5103 mddev->raid_disks = 0;
5104 mddev->recovery_cp = 0;
5105 mddev->resync_min = 0;
5106 mddev->resync_max = MaxSector;
5107 mddev->reshape_position = MaxSector;
5108 mddev->external = 0;
5109 mddev->persistent = 0;
5110 mddev->level = LEVEL_NONE;
5111 mddev->clevel[0] = 0;
5112 mddev->flags = 0;
5113 mddev->ro = 0;
5114 mddev->metadata_type[0] = 0;
5115 mddev->chunk_sectors = 0;
5116 mddev->ctime = mddev->utime = 0;
5117 mddev->layout = 0;
5118 mddev->max_disks = 0;
5119 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005120 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005121 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005122 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005123 mddev->new_level = LEVEL_NONE;
5124 mddev->new_layout = 0;
5125 mddev->new_chunk_sectors = 0;
5126 mddev->curr_resync = 0;
5127 mddev->resync_mismatches = 0;
5128 mddev->suspend_lo = mddev->suspend_hi = 0;
5129 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5130 mddev->recovery = 0;
5131 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005132 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005133 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005134 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005135 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005136 mddev->bitmap_info.offset = 0;
5137 mddev->bitmap_info.default_offset = 0;
5138 mddev->bitmap_info.chunksize = 0;
5139 mddev->bitmap_info.daemon_sleep = 0;
5140 mddev->bitmap_info.max_write_behind = 0;
5141}
5142
NeilBrownfd01b882011-10-11 16:47:53 +11005143static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005144{
5145 if (mddev->sync_thread) {
5146 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5147 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005148 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005149 }
5150
5151 del_timer_sync(&mddev->safemode_timer);
5152
5153 bitmap_flush(mddev);
5154 md_super_wait(mddev);
5155
5156 if (!mddev->in_sync || mddev->flags) {
5157 /* mark array as shutdown cleanly */
5158 mddev->in_sync = 1;
5159 md_update_sb(mddev, 1);
5160 }
5161}
NeilBrowndefad612011-01-14 09:14:33 +11005162
NeilBrownfd01b882011-10-11 16:47:53 +11005163void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005164{
5165 mddev_lock(mddev);
5166 __md_stop_writes(mddev);
5167 mddev_unlock(mddev);
5168}
NeilBrown390ee602010-06-01 19:37:27 +10005169EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005170
NeilBrownfd01b882011-10-11 16:47:53 +11005171void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005172{
NeilBrown0ca69882011-01-14 09:14:33 +11005173 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005174 mddev->pers->stop(mddev);
5175 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5176 mddev->to_remove = &md_redundancy_group;
5177 module_put(mddev->pers->owner);
5178 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005179 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005180}
NeilBrown390ee602010-06-01 19:37:27 +10005181EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005182
NeilBrownfd01b882011-10-11 16:47:53 +11005183static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005184{
5185 int err = 0;
5186 mutex_lock(&mddev->open_mutex);
5187 if (atomic_read(&mddev->openers) > is_open) {
5188 printk("md: %s still in use.\n",mdname(mddev));
5189 err = -EBUSY;
5190 goto out;
5191 }
5192 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005193 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005194
5195 err = -ENXIO;
5196 if (mddev->ro==1)
5197 goto out;
5198 mddev->ro = 1;
5199 set_disk_ro(mddev->gendisk, 1);
5200 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005201 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005202 err = 0;
5203 }
5204out:
5205 mutex_unlock(&mddev->open_mutex);
5206 return err;
5207}
5208
NeilBrown9e653b62006-06-26 00:27:58 -07005209/* mode:
5210 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005211 * 2 - stop but do not disassemble array
5212 */
NeilBrownfd01b882011-10-11 16:47:53 +11005213static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005216 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
NeilBrownc8c00a62009-08-10 12:50:52 +10005218 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005219 if (atomic_read(&mddev->openers) > is_open ||
5220 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005221 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005222 mutex_unlock(&mddev->open_mutex);
5223 return -EBUSY;
5224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrown6e17b022010-08-07 21:41:19 +10005226 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005227 if (mddev->ro)
5228 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005229
NeilBrowndefad612011-01-14 09:14:33 +11005230 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005231 md_stop(mddev);
5232 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005233 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005234
NeilBrowna4bd82d2010-03-29 13:23:10 +11005235 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005236 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005237
NeilBrowndafb20f2012-03-19 12:46:39 +11005238 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005239 if (rdev->raid_disk >= 0)
5240 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005241
NeilBrowna4bd82d2010-03-29 13:23:10 +11005242 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005243 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005244 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005245 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005246
NeilBrowna4bd82d2010-03-29 13:23:10 +11005247 if (mddev->ro)
5248 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005249 } else
5250 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 /*
5252 * Free resources if final stop
5253 */
NeilBrown9e653b62006-06-26 00:27:58 -07005254 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5256
NeilBrown978f9462006-02-02 14:28:05 -08005257 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005258 if (mddev->bitmap_info.file) {
5259 restore_bitmap_write_access(mddev->bitmap_info.file);
5260 fput(mddev->bitmap_info.file);
5261 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005262 }
NeilBrownc3d97142009-12-14 12:49:52 +11005263 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 export_array(mddev);
5266
NeilBrown6177b472010-03-29 11:37:13 +11005267 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005268 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005269 if (mddev->hold_active == UNTIL_STOP)
5270 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005271 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005272 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005273 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005274 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276}
5277
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005278#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005279static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280{
NeilBrown3cb03002011-10-11 16:45:26 +11005281 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 int err;
5283
NeilBrowna757e642005-04-16 15:26:42 -07005284 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286
5287 printk(KERN_INFO "md: running: ");
5288
NeilBrowndafb20f2012-03-19 12:46:39 +11005289 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 char b[BDEVNAME_SIZE];
5291 printk("<%s>", bdevname(rdev->bdev,b));
5292 }
5293 printk("\n");
5294
NeilBrownd710e132008-10-13 11:55:12 +11005295 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 if (err) {
5297 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005298 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
5300}
5301
5302/*
5303 * lets try to run arrays based on all disks that have arrived
5304 * until now. (those are in pending_raid_disks)
5305 *
5306 * the method: pick the first pending disk, collect all disks with
5307 * the same UUID, remove all from the pending list and put them into
5308 * the 'same_array' list. Then order this list based on superblock
5309 * update time (freshest comes first), kick out 'old' disks and
5310 * compare superblocks. If everything's fine then run it.
5311 *
5312 * If "unit" is allocated, then bump its reference count
5313 */
5314static void autorun_devices(int part)
5315{
NeilBrown3cb03002011-10-11 16:45:26 +11005316 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005317 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 char b[BDEVNAME_SIZE];
5319
5320 printk(KERN_INFO "md: autorun ...\n");
5321 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005322 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005324 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005326 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
5328 printk(KERN_INFO "md: considering %s ...\n",
5329 bdevname(rdev0->bdev,b));
5330 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005331 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 if (super_90_load(rdev, rdev0, 0) >= 0) {
5333 printk(KERN_INFO "md: adding %s ...\n",
5334 bdevname(rdev->bdev,b));
5335 list_move(&rdev->same_set, &candidates);
5336 }
5337 /*
5338 * now we have a set of devices, with all of them having
5339 * mostly sane superblocks. It's time to allocate the
5340 * mddev.
5341 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005342 if (part) {
5343 dev = MKDEV(mdp_major,
5344 rdev0->preferred_minor << MdpMinorShift);
5345 unit = MINOR(dev) >> MdpMinorShift;
5346 } else {
5347 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5348 unit = MINOR(dev);
5349 }
5350 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5352 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5353 break;
5354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 md_probe(dev, NULL, NULL);
5357 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005358 if (!mddev || !mddev->gendisk) {
5359 if (mddev)
5360 mddev_put(mddev);
5361 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 "md: cannot allocate memory for md drive.\n");
5363 break;
5364 }
5365 if (mddev_lock(mddev))
5366 printk(KERN_WARNING "md: %s locked, cannot run\n",
5367 mdname(mddev));
5368 else if (mddev->raid_disks || mddev->major_version
5369 || !list_empty(&mddev->disks)) {
5370 printk(KERN_WARNING
5371 "md: %s already running, cannot run %s\n",
5372 mdname(mddev), bdevname(rdev0->bdev,b));
5373 mddev_unlock(mddev);
5374 } else {
5375 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005376 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005377 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 list_del_init(&rdev->same_set);
5379 if (bind_rdev_to_array(rdev, mddev))
5380 export_rdev(rdev);
5381 }
5382 autorun_array(mddev);
5383 mddev_unlock(mddev);
5384 }
5385 /* on success, candidates will be empty, on error
5386 * it won't...
5387 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005388 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005389 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 mddev_put(mddev);
5393 }
5394 printk(KERN_INFO "md: ... autorun DONE.\n");
5395}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005396#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398static int get_version(void __user * arg)
5399{
5400 mdu_version_t ver;
5401
5402 ver.major = MD_MAJOR_VERSION;
5403 ver.minor = MD_MINOR_VERSION;
5404 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5405
5406 if (copy_to_user(arg, &ver, sizeof(ver)))
5407 return -EFAULT;
5408
5409 return 0;
5410}
5411
NeilBrownfd01b882011-10-11 16:47:53 +11005412static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
5414 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005415 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005416 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
NeilBrowna9f326e2009-09-23 18:06:41 +10005418 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005419 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005421 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 failed++;
5423 else {
5424 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005425 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005426 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 else
5428 spare++;
5429 }
5430 }
5431
5432 info.major_version = mddev->major_version;
5433 info.minor_version = mddev->minor_version;
5434 info.patch_version = MD_PATCHLEVEL_VERSION;
5435 info.ctime = mddev->ctime;
5436 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005437 info.size = mddev->dev_sectors / 2;
5438 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005439 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 info.nr_disks = nr;
5441 info.raid_disks = mddev->raid_disks;
5442 info.md_minor = mddev->md_minor;
5443 info.not_persistent= !mddev->persistent;
5444
5445 info.utime = mddev->utime;
5446 info.state = 0;
5447 if (mddev->in_sync)
5448 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005449 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005450 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005451 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 info.working_disks = working;
5453 info.failed_disks = failed;
5454 info.spare_disks = spare;
5455
5456 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005457 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 if (copy_to_user(arg, &info, sizeof(info)))
5460 return -EFAULT;
5461
5462 return 0;
5463}
5464
NeilBrownfd01b882011-10-11 16:47:53 +11005465static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005466{
5467 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5468 char *ptr, *buf = NULL;
5469 int err = -ENOMEM;
5470
Dan Williamsb5470dc2008-06-27 21:44:04 -07005471 if (md_allow_write(mddev))
5472 file = kmalloc(sizeof(*file), GFP_NOIO);
5473 else
5474 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005475
NeilBrown32a76272005-06-21 17:17:14 -07005476 if (!file)
5477 goto out;
5478
5479 /* bitmap disabled, zero the first byte and copy out */
5480 if (!mddev->bitmap || !mddev->bitmap->file) {
5481 file->pathname[0] = '\0';
5482 goto copy_out;
5483 }
5484
5485 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5486 if (!buf)
5487 goto out;
5488
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005489 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5490 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005491 goto out;
5492
5493 strcpy(file->pathname, ptr);
5494
5495copy_out:
5496 err = 0;
5497 if (copy_to_user(arg, file, sizeof(*file)))
5498 err = -EFAULT;
5499out:
5500 kfree(buf);
5501 kfree(file);
5502 return err;
5503}
5504
NeilBrownfd01b882011-10-11 16:47:53 +11005505static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506{
5507 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005508 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
5510 if (copy_from_user(&info, arg, sizeof(info)))
5511 return -EFAULT;
5512
Andre Noll26ef3792008-07-11 22:02:21 +10005513 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (rdev) {
5515 info.major = MAJOR(rdev->bdev->bd_dev);
5516 info.minor = MINOR(rdev->bdev->bd_dev);
5517 info.raid_disk = rdev->raid_disk;
5518 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005519 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005521 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 info.state |= (1<<MD_DISK_ACTIVE);
5523 info.state |= (1<<MD_DISK_SYNC);
5524 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005525 if (test_bit(WriteMostly, &rdev->flags))
5526 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 } else {
5528 info.major = info.minor = 0;
5529 info.raid_disk = -1;
5530 info.state = (1<<MD_DISK_REMOVED);
5531 }
5532
5533 if (copy_to_user(arg, &info, sizeof(info)))
5534 return -EFAULT;
5535
5536 return 0;
5537}
5538
NeilBrownfd01b882011-10-11 16:47:53 +11005539static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
5541 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005542 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 dev_t dev = MKDEV(info->major,info->minor);
5544
5545 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5546 return -EOVERFLOW;
5547
5548 if (!mddev->raid_disks) {
5549 int err;
5550 /* expecting a device which has a superblock */
5551 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5552 if (IS_ERR(rdev)) {
5553 printk(KERN_WARNING
5554 "md: md_import_device returned %ld\n",
5555 PTR_ERR(rdev));
5556 return PTR_ERR(rdev);
5557 }
5558 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005559 struct md_rdev *rdev0
5560 = list_entry(mddev->disks.next,
5561 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005562 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 .load_super(rdev, rdev0, mddev->minor_version);
5564 if (err < 0) {
5565 printk(KERN_WARNING
5566 "md: %s has different UUID to %s\n",
5567 bdevname(rdev->bdev,b),
5568 bdevname(rdev0->bdev,b2));
5569 export_rdev(rdev);
5570 return -EINVAL;
5571 }
5572 }
5573 err = bind_rdev_to_array(rdev, mddev);
5574 if (err)
5575 export_rdev(rdev);
5576 return err;
5577 }
5578
5579 /*
5580 * add_new_disk can be used once the array is assembled
5581 * to add "hot spares". They must already have a superblock
5582 * written
5583 */
5584 if (mddev->pers) {
5585 int err;
5586 if (!mddev->pers->hot_add_disk) {
5587 printk(KERN_WARNING
5588 "%s: personality does not support diskops!\n",
5589 mdname(mddev));
5590 return -EINVAL;
5591 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005592 if (mddev->persistent)
5593 rdev = md_import_device(dev, mddev->major_version,
5594 mddev->minor_version);
5595 else
5596 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 if (IS_ERR(rdev)) {
5598 printk(KERN_WARNING
5599 "md: md_import_device returned %ld\n",
5600 PTR_ERR(rdev));
5601 return PTR_ERR(rdev);
5602 }
NeilBrown1a855a02010-12-09 16:36:28 +11005603 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005604 if (!mddev->persistent) {
5605 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005606 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005607 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005608 set_bit(In_sync, &rdev->flags);
5609 } else
NeilBrown41158c72005-06-21 17:17:25 -07005610 rdev->raid_disk = -1;
5611 } else
5612 super_types[mddev->major_version].
5613 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005614 if ((info->state & (1<<MD_DISK_SYNC)) &&
5615 (!test_bit(In_sync, &rdev->flags) ||
5616 rdev->raid_disk != info->raid_disk)) {
5617 /* This was a hot-add request, but events doesn't
5618 * match, so reject it.
5619 */
5620 export_rdev(rdev);
5621 return -EINVAL;
5622 }
5623
NeilBrown1a855a02010-12-09 16:36:28 +11005624 if (test_bit(In_sync, &rdev->flags))
5625 rdev->saved_raid_disk = rdev->raid_disk;
5626 else
5627 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005628
NeilBrownb2d444d2005-11-08 21:39:31 -08005629 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005630 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5631 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005632 else
5633 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 rdev->raid_disk = -1;
5636 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005637 if (!err && !mddev->pers->hot_remove_disk) {
5638 /* If there is hot_add_disk but no hot_remove_disk
5639 * then added disks for geometry changes,
5640 * and should be added immediately.
5641 */
5642 super_types[mddev->major_version].
5643 validate_super(mddev, rdev);
5644 err = mddev->pers->hot_add_disk(mddev, rdev);
5645 if (err)
5646 unbind_rdev_from_array(rdev);
5647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (err)
5649 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005650 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005651 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005652
NeilBrown17571282006-12-10 02:20:52 -08005653 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005654 if (mddev->degraded)
5655 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005656 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005657 if (!err)
5658 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005659 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 return err;
5661 }
5662
5663 /* otherwise, add_new_disk is only allowed
5664 * for major_version==0 superblocks
5665 */
5666 if (mddev->major_version != 0) {
5667 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5668 mdname(mddev));
5669 return -EINVAL;
5670 }
5671
5672 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5673 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005674 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 if (IS_ERR(rdev)) {
5676 printk(KERN_WARNING
5677 "md: error, md_import_device() returned %ld\n",
5678 PTR_ERR(rdev));
5679 return PTR_ERR(rdev);
5680 }
5681 rdev->desc_nr = info->number;
5682 if (info->raid_disk < mddev->raid_disks)
5683 rdev->raid_disk = info->raid_disk;
5684 else
5685 rdev->raid_disk = -1;
5686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005688 if (info->state & (1<<MD_DISK_SYNC))
5689 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005691 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5692 set_bit(WriteMostly, &rdev->flags);
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 if (!mddev->persistent) {
5695 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005696 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5697 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005698 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005699 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
NeilBrown2bf071b2006-01-06 00:20:55 -08005701 err = bind_rdev_to_array(rdev, mddev);
5702 if (err) {
5703 export_rdev(rdev);
5704 return err;
5705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 }
5707
5708 return 0;
5709}
5710
NeilBrownfd01b882011-10-11 16:47:53 +11005711static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712{
5713 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005714 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 rdev = find_rdev(mddev, dev);
5717 if (!rdev)
5718 return -ENXIO;
5719
5720 if (rdev->raid_disk >= 0)
5721 goto busy;
5722
5723 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005724 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005725 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 return 0;
5728busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005729 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 bdevname(rdev->bdev,b), mdname(mddev));
5731 return -EBUSY;
5732}
5733
NeilBrownfd01b882011-10-11 16:47:53 +11005734static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
5736 char b[BDEVNAME_SIZE];
5737 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005738 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740 if (!mddev->pers)
5741 return -ENODEV;
5742
5743 if (mddev->major_version != 0) {
5744 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5745 " version-0 superblocks.\n",
5746 mdname(mddev));
5747 return -EINVAL;
5748 }
5749 if (!mddev->pers->hot_add_disk) {
5750 printk(KERN_WARNING
5751 "%s: personality does not support diskops!\n",
5752 mdname(mddev));
5753 return -EINVAL;
5754 }
5755
NeilBrownd710e132008-10-13 11:55:12 +11005756 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 if (IS_ERR(rdev)) {
5758 printk(KERN_WARNING
5759 "md: error, md_import_device() returned %ld\n",
5760 PTR_ERR(rdev));
5761 return -EINVAL;
5762 }
5763
5764 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005765 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005767 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
NeilBrown8190e752009-06-18 08:48:58 +10005769 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
NeilBrownb2d444d2005-11-08 21:39:31 -08005771 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 printk(KERN_WARNING
5773 "md: can not hot-add faulty %s disk to %s!\n",
5774 bdevname(rdev->bdev,b), mdname(mddev));
5775 err = -EINVAL;
5776 goto abort_export;
5777 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005778 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005780 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005781 err = bind_rdev_to_array(rdev, mddev);
5782 if (err)
5783 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 /*
5786 * The rest should better be atomic, we can have disk failures
5787 * noticed in interrupt contexts ...
5788 */
5789
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 rdev->raid_disk = -1;
5791
NeilBrown850b2b422006-10-03 01:15:46 -07005792 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794 /*
5795 * Kick recovery, maybe this spare has to be added to the
5796 * array immediately.
5797 */
5798 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5799 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005800 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 return 0;
5802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803abort_export:
5804 export_rdev(rdev);
5805 return err;
5806}
5807
NeilBrownfd01b882011-10-11 16:47:53 +11005808static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005809{
5810 int err;
5811
NeilBrown36fa3062005-09-09 16:23:45 -07005812 if (mddev->pers) {
5813 if (!mddev->pers->quiesce)
5814 return -EBUSY;
5815 if (mddev->recovery || mddev->sync_thread)
5816 return -EBUSY;
5817 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005818 }
5819
NeilBrown36fa3062005-09-09 16:23:45 -07005820
5821 if (fd >= 0) {
5822 if (mddev->bitmap)
5823 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005824 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005825
NeilBrownc3d97142009-12-14 12:49:52 +11005826 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005827 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5828 mdname(mddev));
5829 return -EBADF;
5830 }
5831
NeilBrownc3d97142009-12-14 12:49:52 +11005832 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005833 if (err) {
5834 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5835 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005836 fput(mddev->bitmap_info.file);
5837 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005838 return err;
5839 }
NeilBrownc3d97142009-12-14 12:49:52 +11005840 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005841 } else if (mddev->bitmap == NULL)
5842 return -ENOENT; /* cannot remove what isn't there */
5843 err = 0;
5844 if (mddev->pers) {
5845 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005846 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005847 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005848 if (!err)
5849 err = bitmap_load(mddev);
5850 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005851 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005852 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005853 fd = -1; /* make sure to put the file */
5854 }
NeilBrown36fa3062005-09-09 16:23:45 -07005855 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005856 }
5857 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005858 if (mddev->bitmap_info.file) {
5859 restore_bitmap_write_access(mddev->bitmap_info.file);
5860 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005861 }
NeilBrownc3d97142009-12-14 12:49:52 +11005862 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005863 }
5864
NeilBrown32a76272005-06-21 17:17:14 -07005865 return err;
5866}
5867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868/*
5869 * set_array_info is used two different ways
5870 * The original usage is when creating a new array.
5871 * In this usage, raid_disks is > 0 and it together with
5872 * level, size, not_persistent,layout,chunksize determine the
5873 * shape of the array.
5874 * This will always create an array with a type-0.90.0 superblock.
5875 * The newer usage is when assembling an array.
5876 * In this case raid_disks will be 0, and the major_version field is
5877 * use to determine which style super-blocks are to be found on the devices.
5878 * The minor and patch _version numbers are also kept incase the
5879 * super_block handler wishes to interpret them.
5880 */
NeilBrownfd01b882011-10-11 16:47:53 +11005881static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882{
5883
5884 if (info->raid_disks == 0) {
5885 /* just setting version number for superblock loading */
5886 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005887 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 super_types[info->major_version].name == NULL) {
5889 /* maybe try to auto-load a module? */
5890 printk(KERN_INFO
5891 "md: superblock version %d not known\n",
5892 info->major_version);
5893 return -EINVAL;
5894 }
5895 mddev->major_version = info->major_version;
5896 mddev->minor_version = info->minor_version;
5897 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005898 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005899 /* ensure mddev_put doesn't delete this now that there
5900 * is some minimal configuration.
5901 */
5902 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 return 0;
5904 }
5905 mddev->major_version = MD_MAJOR_VERSION;
5906 mddev->minor_version = MD_MINOR_VERSION;
5907 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5908 mddev->ctime = get_seconds();
5909
5910 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005911 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005912 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 mddev->raid_disks = info->raid_disks;
5914 /* don't set md_minor, it is determined by which /dev/md* was
5915 * openned
5916 */
5917 if (info->state & (1<<MD_SB_CLEAN))
5918 mddev->recovery_cp = MaxSector;
5919 else
5920 mddev->recovery_cp = 0;
5921 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005922 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005925 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 mddev->max_disks = MD_SB_DISKS;
5928
NeilBrowne6910632008-02-06 01:39:51 -08005929 if (mddev->persistent)
5930 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
NeilBrownc3d97142009-12-14 12:49:52 +11005933 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5934 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005935
NeilBrownf6705572006-03-27 01:18:11 -08005936 mddev->reshape_position = MaxSector;
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 /*
5939 * Generate a 128 bit UUID
5940 */
5941 get_random_bytes(mddev->uuid, 16);
5942
NeilBrownf6705572006-03-27 01:18:11 -08005943 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005944 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005945 mddev->new_layout = mddev->layout;
5946 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005947 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08005948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 return 0;
5950}
5951
NeilBrownfd01b882011-10-11 16:47:53 +11005952void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005953{
Dan Williamsb522adc2009-03-31 15:00:31 +11005954 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5955
5956 if (mddev->external_size)
5957 return;
5958
Dan Williams1f403622009-03-31 14:59:03 +11005959 mddev->array_sectors = array_sectors;
5960}
5961EXPORT_SYMBOL(md_set_array_sectors);
5962
NeilBrownfd01b882011-10-11 16:47:53 +11005963static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005964{
NeilBrown3cb03002011-10-11 16:45:26 +11005965 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005966 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005967 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005968
5969 if (mddev->pers->resize == NULL)
5970 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005971 /* The "num_sectors" is the number of sectors of each device that
5972 * is used. This can only make sense for arrays with redundancy.
5973 * linear and raid0 always use whatever space is available. We can only
5974 * consider changing this number if no resync or reconstruction is
5975 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005976 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005977 * of each device. If num_sectors is zero, we find the largest size
5978 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005979 */
5980 if (mddev->sync_thread)
5981 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005982 if (mddev->bitmap)
5983 /* Sorry, cannot grow a bitmap yet, just remove it,
5984 * grow, and re-add.
5985 */
5986 return -EBUSY;
NeilBrowndafb20f2012-03-19 12:46:39 +11005987 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005988 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005989
Andre Nolld71f9f82008-07-11 22:02:22 +10005990 if (fit && (num_sectors == 0 || num_sectors > avail))
5991 num_sectors = avail;
5992 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005993 return -ENOSPC;
5994 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005995 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005996 if (!rv)
5997 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005998 return rv;
5999}
6000
NeilBrownfd01b882011-10-11 16:47:53 +11006001static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006002{
6003 int rv;
6004 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006005 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006006 return -EINVAL;
6007 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006008 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006009 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006010 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006011 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08006012 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006013 if (mddev->delta_disks < 0)
6014 mddev->reshape_backwards = 1;
6015 else if (mddev->delta_disks > 0)
6016 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006017
6018 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006019 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006020 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006021 mddev->reshape_backwards = 0;
6022 }
NeilBrownda943b992006-01-06 00:20:54 -08006023 return rv;
6024}
6025
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027/*
6028 * update_array_info is used to change the configuration of an
6029 * on-line array.
6030 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6031 * fields in the info are checked against the array.
6032 * Any differences that cannot be handled will cause an error.
6033 * Normally, only one change can be managed at a time.
6034 */
NeilBrownfd01b882011-10-11 16:47:53 +11006035static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
6037 int rv = 0;
6038 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006039 int state = 0;
6040
6041 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006042 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006043 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
6045 if (mddev->major_version != info->major_version ||
6046 mddev->minor_version != info->minor_version ||
6047/* mddev->patch_version != info->patch_version || */
6048 mddev->ctime != info->ctime ||
6049 mddev->level != info->level ||
6050/* mddev->layout != info->layout || */
6051 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006052 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006053 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6054 ((state^info->state) & 0xfffffe00)
6055 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 return -EINVAL;
6057 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006058 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6059 cnt++;
6060 if (mddev->raid_disks != info->raid_disks)
6061 cnt++;
6062 if (mddev->layout != info->layout)
6063 cnt++;
6064 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6065 cnt++;
6066 if (cnt == 0)
6067 return 0;
6068 if (cnt > 1)
6069 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 if (mddev->layout != info->layout) {
6072 /* Change layout
6073 * we don't need to do anything at the md level, the
6074 * personality will take care of it all.
6075 */
NeilBrown50ac1682009-06-18 08:47:55 +10006076 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006078 else {
6079 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006080 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006081 if (rv)
6082 mddev->new_layout = mddev->layout;
6083 return rv;
6084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006086 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006087 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
NeilBrownda943b992006-01-06 00:20:54 -08006089 if (mddev->raid_disks != info->raid_disks)
6090 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
NeilBrown36fa3062005-09-09 16:23:45 -07006092 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6093 if (mddev->pers->quiesce == NULL)
6094 return -EINVAL;
6095 if (mddev->recovery || mddev->sync_thread)
6096 return -EBUSY;
6097 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6098 /* add the bitmap */
6099 if (mddev->bitmap)
6100 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006101 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006102 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006103 mddev->bitmap_info.offset =
6104 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07006105 mddev->pers->quiesce(mddev, 1);
6106 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006107 if (!rv)
6108 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006109 if (rv)
6110 bitmap_destroy(mddev);
6111 mddev->pers->quiesce(mddev, 0);
6112 } else {
6113 /* remove the bitmap */
6114 if (!mddev->bitmap)
6115 return -ENOENT;
6116 if (mddev->bitmap->file)
6117 return -EINVAL;
6118 mddev->pers->quiesce(mddev, 1);
6119 bitmap_destroy(mddev);
6120 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006121 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006122 }
6123 }
NeilBrown850b2b422006-10-03 01:15:46 -07006124 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return rv;
6126}
6127
NeilBrownfd01b882011-10-11 16:47:53 +11006128static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
NeilBrown3cb03002011-10-11 16:45:26 +11006130 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
6132 if (mddev->pers == NULL)
6133 return -ENODEV;
6134
6135 rdev = find_rdev(mddev, dev);
6136 if (!rdev)
6137 return -ENODEV;
6138
6139 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006140 if (!test_bit(Faulty, &rdev->flags))
6141 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 return 0;
6143}
6144
Andre Noll2f9618c2008-04-25 18:57:58 +02006145/*
6146 * We have a problem here : there is no easy way to give a CHS
6147 * virtual geometry. We currently pretend that we have a 2 heads
6148 * 4 sectors (with a BIG number of cylinders...). This drives
6149 * dosfs just mad... ;-)
6150 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006151static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6152{
NeilBrownfd01b882011-10-11 16:47:53 +11006153 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006154
6155 geo->heads = 2;
6156 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006157 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006158 return 0;
6159}
6160
Al Viroa39907f2008-03-02 10:31:15 -05006161static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 unsigned int cmd, unsigned long arg)
6163{
6164 int err = 0;
6165 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006166 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006167 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
NeilBrown506c9e42011-12-23 10:17:26 +11006169 switch (cmd) {
6170 case RAID_VERSION:
6171 case GET_ARRAY_INFO:
6172 case GET_DISK_INFO:
6173 break;
6174 default:
6175 if (!capable(CAP_SYS_ADMIN))
6176 return -EACCES;
6177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
6179 /*
6180 * Commands dealing with the RAID driver but not any
6181 * particular array:
6182 */
6183 switch (cmd)
6184 {
6185 case RAID_VERSION:
6186 err = get_version(argp);
6187 goto done;
6188
6189 case PRINT_RAID_DEBUG:
6190 err = 0;
6191 md_print_devices();
6192 goto done;
6193
6194#ifndef MODULE
6195 case RAID_AUTORUN:
6196 err = 0;
6197 autostart_arrays(arg);
6198 goto done;
6199#endif
6200 default:;
6201 }
6202
6203 /*
6204 * Commands creating/starting a new array:
6205 */
6206
Al Viroa39907f2008-03-02 10:31:15 -05006207 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 if (!mddev) {
6210 BUG();
6211 goto abort;
6212 }
6213
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 err = mddev_lock(mddev);
6215 if (err) {
6216 printk(KERN_INFO
6217 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6218 err, cmd);
6219 goto abort;
6220 }
6221
6222 switch (cmd)
6223 {
6224 case SET_ARRAY_INFO:
6225 {
6226 mdu_array_info_t info;
6227 if (!arg)
6228 memset(&info, 0, sizeof(info));
6229 else if (copy_from_user(&info, argp, sizeof(info))) {
6230 err = -EFAULT;
6231 goto abort_unlock;
6232 }
6233 if (mddev->pers) {
6234 err = update_array_info(mddev, &info);
6235 if (err) {
6236 printk(KERN_WARNING "md: couldn't update"
6237 " array info. %d\n", err);
6238 goto abort_unlock;
6239 }
6240 goto done_unlock;
6241 }
6242 if (!list_empty(&mddev->disks)) {
6243 printk(KERN_WARNING
6244 "md: array %s already has disks!\n",
6245 mdname(mddev));
6246 err = -EBUSY;
6247 goto abort_unlock;
6248 }
6249 if (mddev->raid_disks) {
6250 printk(KERN_WARNING
6251 "md: array %s already initialised!\n",
6252 mdname(mddev));
6253 err = -EBUSY;
6254 goto abort_unlock;
6255 }
6256 err = set_array_info(mddev, &info);
6257 if (err) {
6258 printk(KERN_WARNING "md: couldn't set"
6259 " array info. %d\n", err);
6260 goto abort_unlock;
6261 }
6262 }
6263 goto done_unlock;
6264
6265 default:;
6266 }
6267
6268 /*
6269 * Commands querying/configuring an existing array:
6270 */
NeilBrown32a76272005-06-21 17:17:14 -07006271 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006272 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006273 if ((!mddev->raid_disks && !mddev->external)
6274 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6275 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6276 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 err = -ENODEV;
6278 goto abort_unlock;
6279 }
6280
6281 /*
6282 * Commands even a read-only array can execute:
6283 */
6284 switch (cmd)
6285 {
6286 case GET_ARRAY_INFO:
6287 err = get_array_info(mddev, argp);
6288 goto done_unlock;
6289
NeilBrown32a76272005-06-21 17:17:14 -07006290 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006291 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006292 goto done_unlock;
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 case GET_DISK_INFO:
6295 err = get_disk_info(mddev, argp);
6296 goto done_unlock;
6297
6298 case RESTART_ARRAY_RW:
6299 err = restart_array(mddev);
6300 goto done_unlock;
6301
6302 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006303 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 goto done_unlock;
6305
6306 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006307 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 goto done_unlock;
6309
Dan Williamse2218352010-05-12 08:25:37 +10006310 case BLKROSET:
6311 if (get_user(ro, (int __user *)(arg))) {
6312 err = -EFAULT;
6313 goto done_unlock;
6314 }
6315 err = -EINVAL;
6316
6317 /* if the bdev is going readonly the value of mddev->ro
6318 * does not matter, no writes are coming
6319 */
6320 if (ro)
6321 goto done_unlock;
6322
6323 /* are we are already prepared for writes? */
6324 if (mddev->ro != 1)
6325 goto done_unlock;
6326
6327 /* transitioning to readauto need only happen for
6328 * arrays that call md_write_start
6329 */
6330 if (mddev->pers) {
6331 err = restart_array(mddev);
6332 if (err == 0) {
6333 mddev->ro = 2;
6334 set_disk_ro(mddev->gendisk, 0);
6335 }
6336 }
6337 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 }
6339
6340 /*
6341 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006342 * superblock, so we do not allow them on read-only arrays.
6343 * However non-MD ioctls (e.g. get-size) will still come through
6344 * here and hit the 'default' below, so only disallow
6345 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006347 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006348 if (mddev->ro == 2) {
6349 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006350 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006351 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6352 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006353 } else {
6354 err = -EROFS;
6355 goto abort_unlock;
6356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 }
6358
6359 switch (cmd)
6360 {
6361 case ADD_NEW_DISK:
6362 {
6363 mdu_disk_info_t info;
6364 if (copy_from_user(&info, argp, sizeof(info)))
6365 err = -EFAULT;
6366 else
6367 err = add_new_disk(mddev, &info);
6368 goto done_unlock;
6369 }
6370
6371 case HOT_REMOVE_DISK:
6372 err = hot_remove_disk(mddev, new_decode_dev(arg));
6373 goto done_unlock;
6374
6375 case HOT_ADD_DISK:
6376 err = hot_add_disk(mddev, new_decode_dev(arg));
6377 goto done_unlock;
6378
6379 case SET_DISK_FAULTY:
6380 err = set_disk_faulty(mddev, new_decode_dev(arg));
6381 goto done_unlock;
6382
6383 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006384 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 goto done_unlock;
6386
NeilBrown32a76272005-06-21 17:17:14 -07006387 case SET_BITMAP_FILE:
6388 err = set_bitmap_file(mddev, (int)arg);
6389 goto done_unlock;
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 err = -EINVAL;
6393 goto abort_unlock;
6394 }
6395
6396done_unlock:
6397abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006398 if (mddev->hold_active == UNTIL_IOCTL &&
6399 err != -EINVAL)
6400 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 mddev_unlock(mddev);
6402
6403 return err;
6404done:
6405 if (err)
6406 MD_BUG();
6407abort:
6408 return err;
6409}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006410#ifdef CONFIG_COMPAT
6411static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6412 unsigned int cmd, unsigned long arg)
6413{
6414 switch (cmd) {
6415 case HOT_REMOVE_DISK:
6416 case HOT_ADD_DISK:
6417 case SET_DISK_FAULTY:
6418 case SET_BITMAP_FILE:
6419 /* These take in integer arg, do not convert */
6420 break;
6421 default:
6422 arg = (unsigned long)compat_ptr(arg);
6423 break;
6424 }
6425
6426 return md_ioctl(bdev, mode, cmd, arg);
6427}
6428#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
Al Viroa39907f2008-03-02 10:31:15 -05006430static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431{
6432 /*
6433 * Succeed if we can lock the mddev, which confirms that
6434 * it isn't being stopped right now.
6435 */
NeilBrownfd01b882011-10-11 16:47:53 +11006436 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 int err;
6438
NeilBrownd3374822009-01-09 08:31:10 +11006439 if (mddev->gendisk != bdev->bd_disk) {
6440 /* we are racing with mddev_put which is discarding this
6441 * bd_disk.
6442 */
6443 mddev_put(mddev);
6444 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006445 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006446 /* Then retry the open from the top */
6447 return -ERESTARTSYS;
6448 }
6449 BUG_ON(mddev != bdev->bd_disk->private_data);
6450
NeilBrownc8c00a62009-08-10 12:50:52 +10006451 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 goto out;
6453
6454 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006455 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006456 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006458 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 out:
6460 return err;
6461}
6462
Al Viroa39907f2008-03-02 10:31:15 -05006463static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464{
NeilBrownfd01b882011-10-11 16:47:53 +11006465 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006467 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006468 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 mddev_put(mddev);
6470
6471 return 0;
6472}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006473
6474static int md_media_changed(struct gendisk *disk)
6475{
NeilBrownfd01b882011-10-11 16:47:53 +11006476 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006477
6478 return mddev->changed;
6479}
6480
6481static int md_revalidate(struct gendisk *disk)
6482{
NeilBrownfd01b882011-10-11 16:47:53 +11006483 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006484
6485 mddev->changed = 0;
6486 return 0;
6487}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006488static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489{
6490 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006491 .open = md_open,
6492 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006493 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006494#ifdef CONFIG_COMPAT
6495 .compat_ioctl = md_compat_ioctl,
6496#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006497 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006498 .media_changed = md_media_changed,
6499 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500};
6501
Adrian Bunk75c96f82005-05-05 16:16:09 -07006502static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
NeilBrown2b8bf342011-10-11 16:48:23 +11006504 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 /*
6507 * md_thread is a 'system-thread', it's priority should be very
6508 * high. We avoid resource deadlocks individually in each
6509 * raid personality. (RAID5 does preallocation) We also use RR and
6510 * the very same RT priority as kswapd, thus we will never get
6511 * into a priority inversion deadlock.
6512 *
6513 * we definitely have to have equal or higher priority than
6514 * bdflush, otherwise bdflush will deadlock if there are too
6515 * many dirty RAID5 blocks.
6516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
NeilBrown6985c432005-10-19 21:23:47 -07006518 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006519 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
NeilBrown93588e22005-11-15 00:09:12 -08006521 /* We need to wait INTERRUPTIBLE so that
6522 * we don't add to the load-average.
6523 * That means we need to be sure no signals are
6524 * pending
6525 */
6526 if (signal_pending(current))
6527 flush_signals(current);
6528
6529 wait_event_interruptible_timeout
6530 (thread->wqueue,
6531 test_bit(THREAD_WAKEUP, &thread->flags)
6532 || kthread_should_stop(),
6533 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
NeilBrown6c987912011-01-14 09:13:53 +11006535 clear_bit(THREAD_WAKEUP, &thread->flags);
6536 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006537 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006539
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 return 0;
6541}
6542
NeilBrown2b8bf342011-10-11 16:48:23 +11006543void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544{
6545 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006546 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 set_bit(THREAD_WAKEUP, &thread->flags);
6548 wake_up(&thread->wqueue);
6549 }
6550}
6551
NeilBrown2b8bf342011-10-11 16:48:23 +11006552struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 const char *name)
6554{
NeilBrown2b8bf342011-10-11 16:48:23 +11006555 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
NeilBrown2b8bf342011-10-11 16:48:23 +11006557 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 if (!thread)
6559 return NULL;
6560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 init_waitqueue_head(&thread->wqueue);
6562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 thread->run = run;
6564 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006565 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006566 thread->tsk = kthread_run(md_thread, thread,
6567 "%s_%s",
6568 mdname(thread->mddev),
6569 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006570 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 kfree(thread);
6572 return NULL;
6573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 return thread;
6575}
6576
NeilBrown2b8bf342011-10-11 16:48:23 +11006577void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578{
NeilBrown2b8bf342011-10-11 16:48:23 +11006579 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006580 if (!thread)
6581 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006582 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006583 /* Locking ensures that mddev_unlock does not wake_up a
6584 * non-existent thread
6585 */
6586 spin_lock(&pers_lock);
6587 *threadp = NULL;
6588 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006589
6590 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 kfree(thread);
6592}
6593
NeilBrownfd01b882011-10-11 16:47:53 +11006594void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595{
6596 if (!mddev) {
6597 MD_BUG();
6598 return;
6599 }
6600
NeilBrownb2d444d2005-11-08 21:39:31 -08006601 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006603
NeilBrownde393cd2011-07-28 11:31:48 +10006604 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 return;
6606 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006607 if (mddev->degraded)
6608 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006609 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6612 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006613 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006614 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006615 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616}
6617
6618/* seq_file implementation /proc/mdstat */
6619
6620static void status_unused(struct seq_file *seq)
6621{
6622 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006623 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
6625 seq_printf(seq, "unused devices: ");
6626
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006627 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 char b[BDEVNAME_SIZE];
6629 i++;
6630 seq_printf(seq, "%s ",
6631 bdevname(rdev->bdev,b));
6632 }
6633 if (!i)
6634 seq_printf(seq, "<none>");
6635
6636 seq_printf(seq, "\n");
6637}
6638
6639
NeilBrownfd01b882011-10-11 16:47:53 +11006640static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641{
NeilBrowndd71cf62009-05-07 12:49:35 +10006642 sector_t max_sectors, resync, res;
6643 unsigned long dt, db;
6644 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006645 int scale;
6646 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
NeilBrowndd71cf62009-05-07 12:49:35 +10006648 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649
6650 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006651 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006653 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654
6655 /*
6656 * Should not happen.
6657 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006658 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 MD_BUG();
6660 return;
6661 }
NeilBrown4588b422006-03-27 01:18:04 -08006662 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006663 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006664 * u32, as those are the requirements for sector_div.
6665 * Thus 'scale' must be at least 10
6666 */
6667 scale = 10;
6668 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006669 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006670 scale++;
6671 }
6672 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006673 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006674
6675 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 {
NeilBrown4588b422006-03-27 01:18:04 -08006677 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 seq_printf(seq, "[");
6679 for (i = 0; i < x; i++)
6680 seq_printf(seq, "=");
6681 seq_printf(seq, ">");
6682 for (i = 0; i < y; i++)
6683 seq_printf(seq, ".");
6684 seq_printf(seq, "] ");
6685 }
NeilBrown4588b422006-03-27 01:18:04 -08006686 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006687 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6688 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006689 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6690 "check" :
6691 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6692 "resync" : "recovery"))),
6693 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006694 (unsigned long long) resync/2,
6695 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
6697 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 * dt: time from mark until now
6699 * db: blocks written from mark until now
6700 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006701 *
6702 * rt is a sector_t, so could be 32bit or 64bit.
6703 * So we divide before multiply in case it is 32bit and close
6704 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006705 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006706 * near the end of resync when the number of remaining sectors
6707 * is close to 'db'.
6708 * We then divide rt by 32 after multiplying by db to compensate.
6709 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 */
6711 dt = ((jiffies - mddev->resync_mark) / HZ);
6712 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006713 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6714 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
NeilBrowndd71cf62009-05-07 12:49:35 +10006716 rt = max_sectors - resync; /* number of remaining sectors */
6717 sector_div(rt, db/32+1);
6718 rt *= dt;
6719 rt >>= 5;
6720
6721 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6722 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
NeilBrownff4e8d92006-07-10 04:44:16 -07006724 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
6727static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6728{
6729 struct list_head *tmp;
6730 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006731 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
6733 if (l >= 0x10000)
6734 return NULL;
6735 if (!l--)
6736 /* header */
6737 return (void*)1;
6738
6739 spin_lock(&all_mddevs_lock);
6740 list_for_each(tmp,&all_mddevs)
6741 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006742 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 mddev_get(mddev);
6744 spin_unlock(&all_mddevs_lock);
6745 return mddev;
6746 }
6747 spin_unlock(&all_mddevs_lock);
6748 if (!l--)
6749 return (void*)2;/* tail */
6750 return NULL;
6751}
6752
6753static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6754{
6755 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006756 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
6758 ++*pos;
6759 if (v == (void*)2)
6760 return NULL;
6761
6762 spin_lock(&all_mddevs_lock);
6763 if (v == (void*)1)
6764 tmp = all_mddevs.next;
6765 else
6766 tmp = mddev->all_mddevs.next;
6767 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006768 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 else {
6770 next_mddev = (void*)2;
6771 *pos = 0x10000;
6772 }
6773 spin_unlock(&all_mddevs_lock);
6774
6775 if (v != (void*)1)
6776 mddev_put(mddev);
6777 return next_mddev;
6778
6779}
6780
6781static void md_seq_stop(struct seq_file *seq, void *v)
6782{
NeilBrownfd01b882011-10-11 16:47:53 +11006783 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
6785 if (mddev && v != (void*)1 && v != (void*)2)
6786 mddev_put(mddev);
6787}
6788
6789static int md_seq_show(struct seq_file *seq, void *v)
6790{
NeilBrownfd01b882011-10-11 16:47:53 +11006791 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006792 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006793 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
6795 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006796 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 seq_printf(seq, "Personalities : ");
6798 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006799 list_for_each_entry(pers, &pers_list, list)
6800 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
6802 spin_unlock(&pers_lock);
6803 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006804 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 return 0;
6806 }
6807 if (v == (void*)2) {
6808 status_unused(seq);
6809 return 0;
6810 }
6811
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006812 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6816 seq_printf(seq, "%s : %sactive", mdname(mddev),
6817 mddev->pers ? "" : "in");
6818 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006819 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006821 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006822 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 seq_printf(seq, " %s", mddev->pers->name);
6824 }
6825
Andre Nolldd8ac332009-03-31 14:33:13 +11006826 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006827 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 char b[BDEVNAME_SIZE];
6829 seq_printf(seq, " %s[%d]",
6830 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006831 if (test_bit(WriteMostly, &rdev->flags))
6832 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006833 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 seq_printf(seq, "(F)");
6835 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006836 }
6837 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006838 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006839 if (test_bit(Replacement, &rdev->flags))
6840 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006841 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 }
6843
6844 if (!list_empty(&mddev->disks)) {
6845 if (mddev->pers)
6846 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006847 (unsigned long long)
6848 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 else
6850 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006851 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006853 if (mddev->persistent) {
6854 if (mddev->major_version != 0 ||
6855 mddev->minor_version != 90) {
6856 seq_printf(seq," super %d.%d",
6857 mddev->major_version,
6858 mddev->minor_version);
6859 }
NeilBrowne6910632008-02-06 01:39:51 -08006860 } else if (mddev->external)
6861 seq_printf(seq, " super external:%s",
6862 mddev->metadata_type);
6863 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006864 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
6866 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006867 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006869 if (mddev->pers->sync_request) {
6870 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006871 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006872 seq_printf(seq, "\n ");
6873 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6874 seq_printf(seq, "\tresync=DELAYED\n ");
6875 else if (mddev->recovery_cp < MaxSector)
6876 seq_printf(seq, "\tresync=PENDING\n ");
6877 }
NeilBrown32a76272005-06-21 17:17:14 -07006878 } else
6879 seq_printf(seq, "\n ");
6880
NeilBrown57148962012-03-19 12:46:40 +11006881 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
6883 seq_printf(seq, "\n");
6884 }
6885 mddev_unlock(mddev);
6886
6887 return 0;
6888}
6889
Jan Engelhardt110518b2009-05-07 12:49:37 +10006890static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 .start = md_seq_start,
6892 .next = md_seq_next,
6893 .stop = md_seq_stop,
6894 .show = md_seq_show,
6895};
6896
6897static int md_seq_open(struct inode *inode, struct file *file)
6898{
Kay Sieversf1514632011-07-12 20:48:39 +02006899 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 int error;
6901
6902 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006903 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006904 return error;
6905
6906 seq = file->private_data;
6907 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 return error;
6909}
6910
NeilBrownd7603b72006-01-06 00:20:30 -08006911static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6912{
Kay Sieversf1514632011-07-12 20:48:39 +02006913 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006914 int mask;
6915
6916 poll_wait(filp, &md_event_waiters, wait);
6917
6918 /* always allow read */
6919 mask = POLLIN | POLLRDNORM;
6920
Kay Sieversf1514632011-07-12 20:48:39 +02006921 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006922 mask |= POLLERR | POLLPRI;
6923 return mask;
6924}
6925
Arjan van de Venfa027c22007-02-12 00:55:33 -08006926static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006927 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 .open = md_seq_open,
6929 .read = seq_read,
6930 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006931 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006932 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933};
6934
NeilBrown84fc4b52011-10-11 16:49:58 +11006935int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006938 list_add_tail(&p->list, &pers_list);
6939 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 spin_unlock(&pers_lock);
6941 return 0;
6942}
6943
NeilBrown84fc4b52011-10-11 16:49:58 +11006944int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945{
NeilBrown2604b702006-01-06 00:20:36 -08006946 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006948 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 spin_unlock(&pers_lock);
6950 return 0;
6951}
6952
NeilBrownfd01b882011-10-11 16:47:53 +11006953static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954{
NeilBrown3cb03002011-10-11 16:45:26 +11006955 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006957 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
6959 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006960 rcu_read_lock();
6961 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006963 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6964 (int)part_stat_read(&disk->part0, sectors[1]) -
6965 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006966 /* sync IO will cause sync_io to increase before the disk_stats
6967 * as sync_io is counted when a request starts, and
6968 * disk_stats is counted when it completes.
6969 * So resync activity will cause curr_events to be smaller than
6970 * when there was no such activity.
6971 * non-sync IO will cause disk_stat to increase without
6972 * increasing sync_io so curr_events will (eventually)
6973 * be larger than it was before. Once it becomes
6974 * substantially larger, the test below will cause
6975 * the array to appear non-idle, and resync will slow
6976 * down.
6977 * If there is a lot of outstanding resync activity when
6978 * we set last_event to curr_events, then all that activity
6979 * completing might cause the array to appear non-idle
6980 * and resync will be slowed down even though there might
6981 * not have been non-resync activity. This will only
6982 * happen once though. 'last_events' will soon reflect
6983 * the state where there is little or no outstanding
6984 * resync requests, and further resync activity will
6985 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006988 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 rdev->last_events = curr_events;
6990 idle = 0;
6991 }
6992 }
NeilBrown4b809912008-07-21 17:05:25 +10006993 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 return idle;
6995}
6996
NeilBrownfd01b882011-10-11 16:47:53 +11006997void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998{
6999 /* another "blocks" (512byte) blocks have been synced */
7000 atomic_sub(blocks, &mddev->recovery_active);
7001 wake_up(&mddev->recovery_wait);
7002 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007003 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 md_wakeup_thread(mddev->thread);
7005 // stop recovery, signal do_sync ....
7006 }
7007}
7008
7009
NeilBrown06d91a52005-06-21 17:17:12 -07007010/* md_write_start(mddev, bi)
7011 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007012 * in superblock) before writing, schedule a superblock update
7013 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007014 */
NeilBrownfd01b882011-10-11 16:47:53 +11007015void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016{
Neil Brown0fd62b82008-06-28 08:31:36 +10007017 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007018 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007019 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007020
NeilBrownf91de922005-11-08 21:39:36 -08007021 BUG_ON(mddev->ro == 1);
7022 if (mddev->ro == 2) {
7023 /* need to switch to read/write */
7024 mddev->ro = 0;
7025 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7026 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007027 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007028 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007029 }
NeilBrown06d91a52005-06-21 17:17:12 -07007030 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007031 if (mddev->safemode == 1)
7032 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007033 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007034 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007035 if (mddev->in_sync) {
7036 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007037 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007038 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007039 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007040 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007041 }
NeilBrowna9701a32005-11-08 21:39:34 -08007042 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007043 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007044 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007045 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007046 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007047 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048}
7049
NeilBrownfd01b882011-10-11 16:47:53 +11007050void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051{
7052 if (atomic_dec_and_test(&mddev->writes_pending)) {
7053 if (mddev->safemode == 2)
7054 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007055 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7057 }
7058}
7059
NeilBrown2a2275d2007-01-26 00:57:11 -08007060/* md_allow_write(mddev)
7061 * Calling this ensures that the array is marked 'active' so that writes
7062 * may proceed without blocking. It is important to call this before
7063 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7064 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007065 *
7066 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7067 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007068 */
NeilBrownfd01b882011-10-11 16:47:53 +11007069int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007070{
7071 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007072 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007073 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007074 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007075 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007076 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007077
7078 spin_lock_irq(&mddev->write_lock);
7079 if (mddev->in_sync) {
7080 mddev->in_sync = 0;
7081 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007082 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007083 if (mddev->safemode_delay &&
7084 mddev->safemode == 0)
7085 mddev->safemode = 1;
7086 spin_unlock_irq(&mddev->write_lock);
7087 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007088 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007089 } else
7090 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007091
NeilBrown070dc6d2010-08-30 17:33:34 +10007092 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007093 return -EAGAIN;
7094 else
7095 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007096}
7097EXPORT_SYMBOL_GPL(md_allow_write);
7098
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099#define SYNC_MARKS 10
7100#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007101void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
NeilBrownfd01b882011-10-11 16:47:53 +11007103 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 unsigned int currspeed = 0,
7105 window;
NeilBrown57afd892005-06-21 17:17:13 -07007106 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 unsigned long mark[SYNC_MARKS];
7108 sector_t mark_cnt[SYNC_MARKS];
7109 int last_mark,m;
7110 struct list_head *tmp;
7111 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007112 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007113 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007114 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
7116 /* just incase thread restarts... */
7117 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7118 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007119 if (mddev->ro) /* never try to sync a read-only array */
7120 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
NeilBrown61df9d92006-10-03 01:15:57 -07007122 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7123 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7124 desc = "data-check";
7125 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7126 desc = "requested-resync";
7127 else
7128 desc = "resync";
7129 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7130 desc = "reshape";
7131 else
7132 desc = "recovery";
7133
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 /* we overload curr_resync somewhat here.
7135 * 0 == not engaged in resync at all
7136 * 2 == checking that there is no conflict with another sync
7137 * 1 == like 2, but have yielded to allow conflicting resync to
7138 * commense
7139 * other == active in resync - this many blocks
7140 *
7141 * Before starting a resync we must have set curr_resync to
7142 * 2, and then checked that every "conflicting" array has curr_resync
7143 * less than ours. When we find one that is the same or higher
7144 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7145 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7146 * This will mean we have to start checking from the beginning again.
7147 *
7148 */
7149
7150 do {
7151 mddev->curr_resync = 2;
7152
7153 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007154 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007155 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007156
7157 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007159 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 if (mddev2 == mddev)
7161 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007162 if (!mddev->parallel_resync
7163 && mddev2->curr_resync
7164 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 DEFINE_WAIT(wq);
7166 if (mddev < mddev2 && mddev->curr_resync == 2) {
7167 /* arbitrarily yield */
7168 mddev->curr_resync = 1;
7169 wake_up(&resync_wait);
7170 }
7171 if (mddev > mddev2 && mddev->curr_resync == 1)
7172 /* no need to wait here, we can wait the next
7173 * time 'round when curr_resync == 2
7174 */
7175 continue;
NeilBrown97441972008-09-19 11:49:54 +10007176 /* We need to wait 'interruptible' so as not to
7177 * contribute to the load average, and not to
7178 * be caught by 'softlockup'
7179 */
7180 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007181 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007182 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007183 printk(KERN_INFO "md: delaying %s of %s"
7184 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007186 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007188 if (signal_pending(current))
7189 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 schedule();
7191 finish_wait(&resync_wait, &wq);
7192 goto try_again;
7193 }
7194 finish_wait(&resync_wait, &wq);
7195 }
7196 }
7197 } while (mddev->curr_resync < 2);
7198
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007199 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007200 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007202 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 */
7204 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007205 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007206 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007207 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7208 j = mddev->resync_min;
7209 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007210 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007211
NeilBrownccfcc3c2006-03-27 01:18:09 -08007212 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007213 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007214 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007216 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007217 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007218 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007219 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007220 if (rdev->raid_disk >= 0 &&
7221 !test_bit(Faulty, &rdev->flags) &&
7222 !test_bit(In_sync, &rdev->flags) &&
7223 rdev->recovery_offset < j)
7224 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007225 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227
NeilBrown61df9d92006-10-03 01:15:57 -07007228 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7229 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7230 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007231 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007232 "(but not more than %d KB/sec) for %s.\n",
7233 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
NeilBrowneea1bf32009-03-31 14:27:02 +11007235 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007236
NeilBrown57afd892005-06-21 17:17:13 -07007237 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 for (m = 0; m < SYNC_MARKS; m++) {
7239 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007240 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 }
7242 last_mark = 0;
7243 mddev->resync_mark = mark[last_mark];
7244 mddev->resync_mark_cnt = mark_cnt[last_mark];
7245
7246 /*
7247 * Tune reconstruction:
7248 */
7249 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007250 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7251 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252
7253 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 last_check = 0;
7255
7256 if (j>2) {
7257 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007258 "md: resuming %s of %s from checkpoint.\n",
7259 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 mddev->curr_resync = j;
7261 }
NeilBrown75d3da42011-01-14 09:14:34 +11007262 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263
7264 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007265 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266
NeilBrown57afd892005-06-21 17:17:13 -07007267 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007268
NeilBrown7a91ee12009-05-26 12:57:21 +10007269 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7270 ((mddev->curr_resync > mddev->curr_resync_completed &&
7271 (mddev->curr_resync - mddev->curr_resync_completed)
7272 > (max_sectors >> 4)) ||
7273 (j - mddev->curr_resync_completed)*2
7274 >= mddev->resync_max - mddev->curr_resync_completed
7275 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007276 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007277 wait_event(mddev->recovery_wait,
7278 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007279 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007280 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007281 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007282 }
NeilBrownacb180b2009-04-14 16:28:34 +10007283
NeilBrowne62e58a2009-07-01 13:15:35 +10007284 while (j >= mddev->resync_max && !kthread_should_stop()) {
7285 /* As this condition is controlled by user-space,
7286 * we can block indefinitely, so use '_interruptible'
7287 * to avoid triggering warnings.
7288 */
7289 flush_signals(current); /* just in case */
7290 wait_event_interruptible(mddev->recovery_wait,
7291 mddev->resync_max > j
7292 || kthread_should_stop());
7293 }
NeilBrownacb180b2009-04-14 16:28:34 +10007294
7295 if (kthread_should_stop())
7296 goto interrupted;
7297
NeilBrown57afd892005-06-21 17:17:13 -07007298 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007299 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007300 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007301 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 goto out;
7303 }
NeilBrown57afd892005-06-21 17:17:13 -07007304
7305 if (!skipped) { /* actual IO requested */
7306 io_sectors += sectors;
7307 atomic_add(sectors, &mddev->recovery_active);
7308 }
7309
NeilBrowne875ece2011-07-28 11:39:24 +10007310 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7311 break;
7312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 j += sectors;
7314 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007315 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007316 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007317 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007318 * visible in /proc/mdstat
7319 */
7320 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007321
7322 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 continue;
7324
NeilBrown57afd892005-06-21 17:17:13 -07007325 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 repeat:
7327 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7328 /* step marks */
7329 int next = (last_mark+1) % SYNC_MARKS;
7330
7331 mddev->resync_mark = mark[next];
7332 mddev->resync_mark_cnt = mark_cnt[next];
7333 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007334 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 last_mark = next;
7336 }
7337
7338
NeilBrownc6207272008-02-06 01:39:52 -08007339 if (kthread_should_stop())
7340 goto interrupted;
7341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342
7343 /*
7344 * this loop exits only if either when we are slower than
7345 * the 'hard' speed limit, or the system was IO-idle for
7346 * a jiffy.
7347 * the system might be non-idle CPU-wise, but we only care
7348 * about not overloading the IO subsystem. (things like an
7349 * e2fsck being done on the RAID array should execute fast)
7350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 cond_resched();
7352
NeilBrown57afd892005-06-21 17:17:13 -07007353 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7354 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
NeilBrown88202a02006-01-06 00:21:36 -08007356 if (currspeed > speed_min(mddev)) {
7357 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007358 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007359 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 goto repeat;
7361 }
7362 }
7363 }
NeilBrown61df9d92006-10-03 01:15:57 -07007364 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 /*
7366 * this also signals 'finished resyncing' to md_stop
7367 */
7368 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7370
7371 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007372 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
NeilBrowndfc70642008-05-23 13:04:39 -07007374 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007375 mddev->curr_resync > 2) {
7376 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7377 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7378 if (mddev->curr_resync >= mddev->recovery_cp) {
7379 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007380 "md: checkpointing %s of %s.\n",
7381 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007382 mddev->recovery_cp =
7383 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007384 }
7385 } else
7386 mddev->recovery_cp = MaxSector;
7387 } else {
7388 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7389 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007390 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007391 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007392 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007393 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007394 !test_bit(Faulty, &rdev->flags) &&
7395 !test_bit(In_sync, &rdev->flags) &&
7396 rdev->recovery_offset < mddev->curr_resync)
7397 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007398 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007401 skip:
NeilBrown17571282006-12-10 02:20:52 -08007402 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
NeilBrownc07b70a2009-12-14 12:49:48 +11007404 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7405 /* We completed so min/max setting can be forgotten if used. */
7406 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7407 mddev->resync_min = 0;
7408 mddev->resync_max = MaxSector;
7409 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7410 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 mddev->curr_resync = 0;
7412 wake_up(&resync_wait);
7413 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7414 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007415 return;
7416
7417 interrupted:
7418 /*
7419 * got a signal, exit.
7420 */
7421 printk(KERN_INFO
7422 "md: md_do_sync() got signal ... exiting\n");
7423 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7424 goto out;
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426}
NeilBrown29269552006-03-27 01:18:10 -08007427EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428
NeilBrownfd01b882011-10-11 16:47:53 +11007429static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007430{
NeilBrown3cb03002011-10-11 16:45:26 +11007431 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007432 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007433 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007434
NeilBrown97e4f422009-03-31 14:33:13 +11007435 mddev->curr_resync_completed = 0;
7436
NeilBrowndafb20f2012-03-19 12:46:39 +11007437 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007438 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007439 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007440 (test_bit(Faulty, &rdev->flags) ||
7441 ! test_bit(In_sync, &rdev->flags)) &&
7442 atomic_read(&rdev->nr_pending)==0) {
7443 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007444 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007445 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007446 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007447 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007448 }
7449 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007450 if (removed)
7451 sysfs_notify(&mddev->kobj, NULL,
7452 "degraded");
7453
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007454
NeilBrowndafb20f2012-03-19 12:46:39 +11007455 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007456 if (rdev->raid_disk >= 0 &&
7457 !test_bit(In_sync, &rdev->flags) &&
7458 !test_bit(Faulty, &rdev->flags))
7459 spares++;
7460 if (rdev->raid_disk < 0
7461 && !test_bit(Faulty, &rdev->flags)) {
7462 rdev->recovery_offset = 0;
7463 if (mddev->pers->
7464 hot_add_disk(mddev, rdev) == 0) {
7465 if (sysfs_link_rdev(mddev, rdev))
7466 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007467 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007468 md_new_event(mddev);
7469 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007470 }
NeilBrowndfc70642008-05-23 13:04:39 -07007471 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007472 }
7473 return spares;
7474}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007475
NeilBrownfd01b882011-10-11 16:47:53 +11007476static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007477{
NeilBrown3cb03002011-10-11 16:45:26 +11007478 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007479
7480 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007481 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007482 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7483 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7484 /* success...*/
7485 /* activate any spares */
7486 if (mddev->pers->spare_active(mddev))
7487 sysfs_notify(&mddev->kobj, NULL,
7488 "degraded");
7489 }
7490 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7491 mddev->pers->finish_reshape)
7492 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007493
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007494 /* If array is no-longer degraded, then any saved_raid_disk
7495 * information must be scrapped. Also if any device is now
7496 * In_sync we must scrape the saved_raid_disk for that device
7497 * do the superblock for an incrementally recovered device
7498 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007499 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007500 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007501 if (!mddev->degraded ||
7502 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007503 rdev->saved_raid_disk = -1;
7504
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007505 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007506 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7507 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7508 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7509 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7510 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7511 /* flag recovery needed just to double check */
7512 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7513 sysfs_notify_dirent_safe(mddev->sysfs_action);
7514 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007515 if (mddev->event_work.func)
7516 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007517}
7518
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519/*
7520 * This routine is regularly called by all per-raid-array threads to
7521 * deal with generic issues like resync and super-block update.
7522 * Raid personalities that don't have a thread (linear/raid0) do not
7523 * need this as they never do any recovery or update the superblock.
7524 *
7525 * It does not do any resync itself, but rather "forks" off other threads
7526 * to do that as needed.
7527 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7528 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007529 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 * and wakeups up this thread which will reap the thread and finish up.
7531 * This thread also removes any faulty devices (with nr_pending == 0).
7532 *
7533 * The overall approach is:
7534 * 1/ if the superblock needs updating, update it.
7535 * 2/ If a recovery thread is running, don't do anything else.
7536 * 3/ If recovery has finished, clean up, possibly marking spares active.
7537 * 4/ If there are any faulty devices, remove them.
7538 * 5/ If array is degraded, try to add spares devices
7539 * 6/ If array has spares or is not in-sync, start a resync thread.
7540 */
NeilBrownfd01b882011-10-11 16:47:53 +11007541void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007543 if (mddev->suspended)
7544 return;
7545
NeilBrown5f404022005-06-21 17:17:16 -07007546 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007547 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
NeilBrownfca4d842005-06-21 17:17:11 -07007549 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007550 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007551 printk(KERN_INFO "md: %s in immediate safe mode\n",
7552 mdname(mddev));
7553 mddev->safemode = 2;
7554 }
7555 flush_signals(current);
7556 }
7557
NeilBrownc89a8ee2008-08-05 15:54:13 +10007558 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7559 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007561 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007563 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007564 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007565 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7566 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 ))
7568 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007569
NeilBrowndf5b89b2006-03-27 01:18:20 -08007570 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007571 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007572
NeilBrownc89a8ee2008-08-05 15:54:13 +10007573 if (mddev->ro) {
7574 /* Only thing we do on a ro array is remove
7575 * failed devices.
7576 */
NeilBrown3cb03002011-10-11 16:45:26 +11007577 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007578 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007579 if (rdev->raid_disk >= 0 &&
7580 !test_bit(Blocked, &rdev->flags) &&
7581 test_bit(Faulty, &rdev->flags) &&
7582 atomic_read(&rdev->nr_pending)==0) {
7583 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007584 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007585 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007586 rdev->raid_disk = -1;
7587 }
7588 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007589 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7590 goto unlock;
7591 }
7592
NeilBrown31a59e32008-04-30 00:52:30 -07007593 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007594 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007595 spin_lock_irq(&mddev->write_lock);
7596 if (mddev->safemode &&
7597 !atomic_read(&mddev->writes_pending) &&
7598 !mddev->in_sync &&
7599 mddev->recovery_cp == MaxSector) {
7600 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007601 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007602 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007603 }
7604 if (mddev->safemode == 1)
7605 mddev->safemode = 0;
7606 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007607 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007608 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007609 }
NeilBrownfca4d842005-06-21 17:17:11 -07007610
NeilBrown850b2b422006-10-03 01:15:46 -07007611 if (mddev->flags)
7612 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007613
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7615 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7616 /* resync/recovery still happening */
7617 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7618 goto unlock;
7619 }
7620 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007621 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 goto unlock;
7623 }
Neil Brown72a23c22008-06-28 08:31:41 +10007624 /* Set RUNNING before clearing NEEDED to avoid
7625 * any transients in the value of "sync_action".
7626 */
7627 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007628 /* Clear some bits that don't mean anything, but
7629 * might be left set
7630 */
NeilBrown24dd4692005-11-08 21:39:26 -08007631 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7632 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
NeilBrowned209582012-04-24 10:23:14 +10007634 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7635 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007636 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 /* no recovery is running.
7638 * remove any failed drives, then
7639 * add spares if possible.
7640 * Spare are also removed and re-added, to allow
7641 * the personality to fail the re-add.
7642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007644 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007645 if (mddev->pers->check_reshape == NULL ||
7646 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007647 /* Cannot proceed */
7648 goto unlock;
7649 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007650 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007651 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007652 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7653 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007654 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007655 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007656 } else if (mddev->recovery_cp < MaxSector) {
7657 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007658 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007659 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7660 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007662
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007664 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7665 /* We are adding a device or devices to an array
7666 * which has the bitmap stored on all devices.
7667 * So make sure all bitmap pages get written
7668 */
7669 bitmap_write_all(mddev->bitmap);
7670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 mddev->sync_thread = md_register_thread(md_do_sync,
7672 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007673 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 if (!mddev->sync_thread) {
7675 printk(KERN_ERR "%s: could not start resync"
7676 " thread...\n",
7677 mdname(mddev));
7678 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007679 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7680 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7681 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7682 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7683 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007684 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007686 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007687 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 }
7689 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007690 if (!mddev->sync_thread) {
7691 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7692 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7693 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007694 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007695 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 mddev_unlock(mddev);
7698 }
7699}
7700
NeilBrownfd01b882011-10-11 16:47:53 +11007701void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007702{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007703 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007704 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007705 !test_bit(Blocked, &rdev->flags) &&
7706 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007707 msecs_to_jiffies(5000));
7708 rdev_dec_pending(rdev, mddev);
7709}
7710EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7711
NeilBrown2230dfe2011-07-28 11:31:46 +10007712
7713/* Bad block management.
7714 * We can record which blocks on each device are 'bad' and so just
7715 * fail those blocks, or that stripe, rather than the whole device.
7716 * Entries in the bad-block table are 64bits wide. This comprises:
7717 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7718 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7719 * A 'shift' can be set so that larger blocks are tracked and
7720 * consequently larger devices can be covered.
7721 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7722 *
7723 * Locking of the bad-block table uses a seqlock so md_is_badblock
7724 * might need to retry if it is very unlucky.
7725 * We will sometimes want to check for bad blocks in a bi_end_io function,
7726 * so we use the write_seqlock_irq variant.
7727 *
7728 * When looking for a bad block we specify a range and want to
7729 * know if any block in the range is bad. So we binary-search
7730 * to the last range that starts at-or-before the given endpoint,
7731 * (or "before the sector after the target range")
7732 * then see if it ends after the given start.
7733 * We return
7734 * 0 if there are no known bad blocks in the range
7735 * 1 if there are known bad block which are all acknowledged
7736 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7737 * plus the start/length of the first bad section we overlap.
7738 */
7739int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7740 sector_t *first_bad, int *bad_sectors)
7741{
7742 int hi;
7743 int lo = 0;
7744 u64 *p = bb->page;
7745 int rv = 0;
7746 sector_t target = s + sectors;
7747 unsigned seq;
7748
7749 if (bb->shift > 0) {
7750 /* round the start down, and the end up */
7751 s >>= bb->shift;
7752 target += (1<<bb->shift) - 1;
7753 target >>= bb->shift;
7754 sectors = target - s;
7755 }
7756 /* 'target' is now the first block after the bad range */
7757
7758retry:
7759 seq = read_seqbegin(&bb->lock);
7760
7761 hi = bb->count;
7762
7763 /* Binary search between lo and hi for 'target'
7764 * i.e. for the last range that starts before 'target'
7765 */
7766 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7767 * are known not to be the last range before target.
7768 * VARIANT: hi-lo is the number of possible
7769 * ranges, and decreases until it reaches 1
7770 */
7771 while (hi - lo > 1) {
7772 int mid = (lo + hi) / 2;
7773 sector_t a = BB_OFFSET(p[mid]);
7774 if (a < target)
7775 /* This could still be the one, earlier ranges
7776 * could not. */
7777 lo = mid;
7778 else
7779 /* This and later ranges are definitely out. */
7780 hi = mid;
7781 }
7782 /* 'lo' might be the last that started before target, but 'hi' isn't */
7783 if (hi > lo) {
7784 /* need to check all range that end after 's' to see if
7785 * any are unacknowledged.
7786 */
7787 while (lo >= 0 &&
7788 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7789 if (BB_OFFSET(p[lo]) < target) {
7790 /* starts before the end, and finishes after
7791 * the start, so they must overlap
7792 */
7793 if (rv != -1 && BB_ACK(p[lo]))
7794 rv = 1;
7795 else
7796 rv = -1;
7797 *first_bad = BB_OFFSET(p[lo]);
7798 *bad_sectors = BB_LEN(p[lo]);
7799 }
7800 lo--;
7801 }
7802 }
7803
7804 if (read_seqretry(&bb->lock, seq))
7805 goto retry;
7806
7807 return rv;
7808}
7809EXPORT_SYMBOL_GPL(md_is_badblock);
7810
7811/*
7812 * Add a range of bad blocks to the table.
7813 * This might extend the table, or might contract it
7814 * if two adjacent ranges can be merged.
7815 * We binary-search to find the 'insertion' point, then
7816 * decide how best to handle it.
7817 */
7818static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7819 int acknowledged)
7820{
7821 u64 *p;
7822 int lo, hi;
7823 int rv = 1;
7824
7825 if (bb->shift < 0)
7826 /* badblocks are disabled */
7827 return 0;
7828
7829 if (bb->shift) {
7830 /* round the start down, and the end up */
7831 sector_t next = s + sectors;
7832 s >>= bb->shift;
7833 next += (1<<bb->shift) - 1;
7834 next >>= bb->shift;
7835 sectors = next - s;
7836 }
7837
7838 write_seqlock_irq(&bb->lock);
7839
7840 p = bb->page;
7841 lo = 0;
7842 hi = bb->count;
7843 /* Find the last range that starts at-or-before 's' */
7844 while (hi - lo > 1) {
7845 int mid = (lo + hi) / 2;
7846 sector_t a = BB_OFFSET(p[mid]);
7847 if (a <= s)
7848 lo = mid;
7849 else
7850 hi = mid;
7851 }
7852 if (hi > lo && BB_OFFSET(p[lo]) > s)
7853 hi = lo;
7854
7855 if (hi > lo) {
7856 /* we found a range that might merge with the start
7857 * of our new range
7858 */
7859 sector_t a = BB_OFFSET(p[lo]);
7860 sector_t e = a + BB_LEN(p[lo]);
7861 int ack = BB_ACK(p[lo]);
7862 if (e >= s) {
7863 /* Yes, we can merge with a previous range */
7864 if (s == a && s + sectors >= e)
7865 /* new range covers old */
7866 ack = acknowledged;
7867 else
7868 ack = ack && acknowledged;
7869
7870 if (e < s + sectors)
7871 e = s + sectors;
7872 if (e - a <= BB_MAX_LEN) {
7873 p[lo] = BB_MAKE(a, e-a, ack);
7874 s = e;
7875 } else {
7876 /* does not all fit in one range,
7877 * make p[lo] maximal
7878 */
7879 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7880 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7881 s = a + BB_MAX_LEN;
7882 }
7883 sectors = e - s;
7884 }
7885 }
7886 if (sectors && hi < bb->count) {
7887 /* 'hi' points to the first range that starts after 's'.
7888 * Maybe we can merge with the start of that range */
7889 sector_t a = BB_OFFSET(p[hi]);
7890 sector_t e = a + BB_LEN(p[hi]);
7891 int ack = BB_ACK(p[hi]);
7892 if (a <= s + sectors) {
7893 /* merging is possible */
7894 if (e <= s + sectors) {
7895 /* full overlap */
7896 e = s + sectors;
7897 ack = acknowledged;
7898 } else
7899 ack = ack && acknowledged;
7900
7901 a = s;
7902 if (e - a <= BB_MAX_LEN) {
7903 p[hi] = BB_MAKE(a, e-a, ack);
7904 s = e;
7905 } else {
7906 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7907 s = a + BB_MAX_LEN;
7908 }
7909 sectors = e - s;
7910 lo = hi;
7911 hi++;
7912 }
7913 }
7914 if (sectors == 0 && hi < bb->count) {
7915 /* we might be able to combine lo and hi */
7916 /* Note: 's' is at the end of 'lo' */
7917 sector_t a = BB_OFFSET(p[hi]);
7918 int lolen = BB_LEN(p[lo]);
7919 int hilen = BB_LEN(p[hi]);
7920 int newlen = lolen + hilen - (s - a);
7921 if (s >= a && newlen < BB_MAX_LEN) {
7922 /* yes, we can combine them */
7923 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7924 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7925 memmove(p + hi, p + hi + 1,
7926 (bb->count - hi - 1) * 8);
7927 bb->count--;
7928 }
7929 }
7930 while (sectors) {
7931 /* didn't merge (it all).
7932 * Need to add a range just before 'hi' */
7933 if (bb->count >= MD_MAX_BADBLOCKS) {
7934 /* No room for more */
7935 rv = 0;
7936 break;
7937 } else {
7938 int this_sectors = sectors;
7939 memmove(p + hi + 1, p + hi,
7940 (bb->count - hi) * 8);
7941 bb->count++;
7942
7943 if (this_sectors > BB_MAX_LEN)
7944 this_sectors = BB_MAX_LEN;
7945 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7946 sectors -= this_sectors;
7947 s += this_sectors;
7948 }
7949 }
7950
7951 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007952 if (!acknowledged)
7953 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007954 write_sequnlock_irq(&bb->lock);
7955
7956 return rv;
7957}
7958
NeilBrown3cb03002011-10-11 16:45:26 +11007959int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007960 int acknowledged)
7961{
7962 int rv = md_set_badblocks(&rdev->badblocks,
7963 s + rdev->data_offset, sectors, acknowledged);
7964 if (rv) {
7965 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007966 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007967 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7968 md_wakeup_thread(rdev->mddev->thread);
7969 }
7970 return rv;
7971}
7972EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7973
7974/*
7975 * Remove a range of bad blocks from the table.
7976 * This may involve extending the table if we spilt a region,
7977 * but it must not fail. So if the table becomes full, we just
7978 * drop the remove request.
7979 */
7980static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7981{
7982 u64 *p;
7983 int lo, hi;
7984 sector_t target = s + sectors;
7985 int rv = 0;
7986
7987 if (bb->shift > 0) {
7988 /* When clearing we round the start up and the end down.
7989 * This should not matter as the shift should align with
7990 * the block size and no rounding should ever be needed.
7991 * However it is better the think a block is bad when it
7992 * isn't than to think a block is not bad when it is.
7993 */
7994 s += (1<<bb->shift) - 1;
7995 s >>= bb->shift;
7996 target >>= bb->shift;
7997 sectors = target - s;
7998 }
7999
8000 write_seqlock_irq(&bb->lock);
8001
8002 p = bb->page;
8003 lo = 0;
8004 hi = bb->count;
8005 /* Find the last range that starts before 'target' */
8006 while (hi - lo > 1) {
8007 int mid = (lo + hi) / 2;
8008 sector_t a = BB_OFFSET(p[mid]);
8009 if (a < target)
8010 lo = mid;
8011 else
8012 hi = mid;
8013 }
8014 if (hi > lo) {
8015 /* p[lo] is the last range that could overlap the
8016 * current range. Earlier ranges could also overlap,
8017 * but only this one can overlap the end of the range.
8018 */
8019 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8020 /* Partial overlap, leave the tail of this range */
8021 int ack = BB_ACK(p[lo]);
8022 sector_t a = BB_OFFSET(p[lo]);
8023 sector_t end = a + BB_LEN(p[lo]);
8024
8025 if (a < s) {
8026 /* we need to split this range */
8027 if (bb->count >= MD_MAX_BADBLOCKS) {
8028 rv = 0;
8029 goto out;
8030 }
8031 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8032 bb->count++;
8033 p[lo] = BB_MAKE(a, s-a, ack);
8034 lo++;
8035 }
8036 p[lo] = BB_MAKE(target, end - target, ack);
8037 /* there is no longer an overlap */
8038 hi = lo;
8039 lo--;
8040 }
8041 while (lo >= 0 &&
8042 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8043 /* This range does overlap */
8044 if (BB_OFFSET(p[lo]) < s) {
8045 /* Keep the early parts of this range. */
8046 int ack = BB_ACK(p[lo]);
8047 sector_t start = BB_OFFSET(p[lo]);
8048 p[lo] = BB_MAKE(start, s - start, ack);
8049 /* now low doesn't overlap, so.. */
8050 break;
8051 }
8052 lo--;
8053 }
8054 /* 'lo' is strictly before, 'hi' is strictly after,
8055 * anything between needs to be discarded
8056 */
8057 if (hi - lo > 1) {
8058 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8059 bb->count -= (hi - lo - 1);
8060 }
8061 }
8062
8063 bb->changed = 1;
8064out:
8065 write_sequnlock_irq(&bb->lock);
8066 return rv;
8067}
8068
NeilBrown3cb03002011-10-11 16:45:26 +11008069int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10008070{
8071 return md_clear_badblocks(&rdev->badblocks,
8072 s + rdev->data_offset,
8073 sectors);
8074}
8075EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8076
8077/*
8078 * Acknowledge all bad blocks in a list.
8079 * This only succeeds if ->changed is clear. It is used by
8080 * in-kernel metadata updates
8081 */
8082void md_ack_all_badblocks(struct badblocks *bb)
8083{
8084 if (bb->page == NULL || bb->changed)
8085 /* no point even trying */
8086 return;
8087 write_seqlock_irq(&bb->lock);
8088
majianpengecb178b2012-03-19 12:46:42 +11008089 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008090 u64 *p = bb->page;
8091 int i;
8092 for (i = 0; i < bb->count ; i++) {
8093 if (!BB_ACK(p[i])) {
8094 sector_t start = BB_OFFSET(p[i]);
8095 int len = BB_LEN(p[i]);
8096 p[i] = BB_MAKE(start, len, 1);
8097 }
8098 }
NeilBrownde393cd2011-07-28 11:31:48 +10008099 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008100 }
8101 write_sequnlock_irq(&bb->lock);
8102}
8103EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8104
NeilBrown16c791a2011-07-28 11:31:47 +10008105/* sysfs access to bad-blocks list.
8106 * We present two files.
8107 * 'bad-blocks' lists sector numbers and lengths of ranges that
8108 * are recorded as bad. The list is truncated to fit within
8109 * the one-page limit of sysfs.
8110 * Writing "sector length" to this file adds an acknowledged
8111 * bad block list.
8112 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8113 * been acknowledged. Writing to this file adds bad blocks
8114 * without acknowledging them. This is largely for testing.
8115 */
8116
8117static ssize_t
8118badblocks_show(struct badblocks *bb, char *page, int unack)
8119{
8120 size_t len;
8121 int i;
8122 u64 *p = bb->page;
8123 unsigned seq;
8124
8125 if (bb->shift < 0)
8126 return 0;
8127
8128retry:
8129 seq = read_seqbegin(&bb->lock);
8130
8131 len = 0;
8132 i = 0;
8133
8134 while (len < PAGE_SIZE && i < bb->count) {
8135 sector_t s = BB_OFFSET(p[i]);
8136 unsigned int length = BB_LEN(p[i]);
8137 int ack = BB_ACK(p[i]);
8138 i++;
8139
8140 if (unack && ack)
8141 continue;
8142
8143 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8144 (unsigned long long)s << bb->shift,
8145 length << bb->shift);
8146 }
NeilBrownde393cd2011-07-28 11:31:48 +10008147 if (unack && len == 0)
8148 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008149
8150 if (read_seqretry(&bb->lock, seq))
8151 goto retry;
8152
8153 return len;
8154}
8155
8156#define DO_DEBUG 1
8157
8158static ssize_t
8159badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8160{
8161 unsigned long long sector;
8162 int length;
8163 char newline;
8164#ifdef DO_DEBUG
8165 /* Allow clearing via sysfs *only* for testing/debugging.
8166 * Normally only a successful write may clear a badblock
8167 */
8168 int clear = 0;
8169 if (page[0] == '-') {
8170 clear = 1;
8171 page++;
8172 }
8173#endif /* DO_DEBUG */
8174
8175 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8176 case 3:
8177 if (newline != '\n')
8178 return -EINVAL;
8179 case 2:
8180 if (length <= 0)
8181 return -EINVAL;
8182 break;
8183 default:
8184 return -EINVAL;
8185 }
8186
8187#ifdef DO_DEBUG
8188 if (clear) {
8189 md_clear_badblocks(bb, sector, length);
8190 return len;
8191 }
8192#endif /* DO_DEBUG */
8193 if (md_set_badblocks(bb, sector, length, !unack))
8194 return len;
8195 else
8196 return -ENOSPC;
8197}
8198
Adrian Bunk75c96f82005-05-05 16:16:09 -07008199static int md_notify_reboot(struct notifier_block *this,
8200 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201{
8202 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008203 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008204 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205
NeilBrownc744a652012-03-19 12:46:37 +11008206 for_each_mddev(mddev, tmp) {
8207 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008208 if (mddev->pers)
8209 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008210 mddev->safemode = 2;
8211 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008212 }
NeilBrownc744a652012-03-19 12:46:37 +11008213 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 }
NeilBrownc744a652012-03-19 12:46:37 +11008215 /*
8216 * certain more exotic SCSI devices are known to be
8217 * volatile wrt too early system reboots. While the
8218 * right place to handle this issue is the given
8219 * driver, we do want to have a safe RAID driver ...
8220 */
8221 if (need_delay)
8222 mdelay(1000*1);
8223
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224 return NOTIFY_DONE;
8225}
8226
Adrian Bunk75c96f82005-05-05 16:16:09 -07008227static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 .notifier_call = md_notify_reboot,
8229 .next = NULL,
8230 .priority = INT_MAX, /* before any real devices */
8231};
8232
8233static void md_geninit(void)
8234{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008235 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008237 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238}
8239
Adrian Bunk75c96f82005-05-05 16:16:09 -07008240static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241{
Tejun Heoe804ac72010-10-15 15:36:08 +02008242 int ret = -ENOMEM;
8243
Tejun Heoada609e2011-01-25 14:35:54 +01008244 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008245 if (!md_wq)
8246 goto err_wq;
8247
8248 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8249 if (!md_misc_wq)
8250 goto err_misc_wq;
8251
8252 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8253 goto err_md;
8254
8255 if ((ret = register_blkdev(0, "mdp")) < 0)
8256 goto err_mdp;
8257 mdp_major = ret;
8258
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008259 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008260 md_probe, NULL, NULL);
8261 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262 md_probe, NULL, NULL);
8263
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008265 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
8267 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269
Tejun Heoe804ac72010-10-15 15:36:08 +02008270err_mdp:
8271 unregister_blkdev(MD_MAJOR, "md");
8272err_md:
8273 destroy_workqueue(md_misc_wq);
8274err_misc_wq:
8275 destroy_workqueue(md_wq);
8276err_wq:
8277 return ret;
8278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
8280#ifndef MODULE
8281
8282/*
8283 * Searches all registered partitions for autorun RAID arrays
8284 * at boot time.
8285 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008286
8287static LIST_HEAD(all_detected_devices);
8288struct detected_devices_node {
8289 struct list_head list;
8290 dev_t dev;
8291};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292
8293void md_autodetect_dev(dev_t dev)
8294{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008295 struct detected_devices_node *node_detected_dev;
8296
8297 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8298 if (node_detected_dev) {
8299 node_detected_dev->dev = dev;
8300 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8301 } else {
8302 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8303 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305}
8306
8307
8308static void autostart_arrays(int part)
8309{
NeilBrown3cb03002011-10-11 16:45:26 +11008310 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008311 struct detected_devices_node *node_detected_dev;
8312 dev_t dev;
8313 int i_scanned, i_passed;
8314
8315 i_scanned = 0;
8316 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
8318 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8319
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008320 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8321 i_scanned++;
8322 node_detected_dev = list_entry(all_detected_devices.next,
8323 struct detected_devices_node, list);
8324 list_del(&node_detected_dev->list);
8325 dev = node_detected_dev->dev;
8326 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008327 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 if (IS_ERR(rdev))
8329 continue;
8330
NeilBrownb2d444d2005-11-08 21:39:31 -08008331 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332 MD_BUG();
8333 continue;
8334 }
NeilBrownd0fae182008-03-04 14:29:31 -08008335 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008337 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008339
8340 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8341 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008342
8343 autorun_devices(part);
8344}
8345
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008346#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
8348static __exit void md_exit(void)
8349{
NeilBrownfd01b882011-10-11 16:47:53 +11008350 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008352
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008353 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008354 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008356 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 unregister_blkdev(mdp_major, "mdp");
8358 unregister_reboot_notifier(&md_notifier);
8359 unregister_sysctl_table(raid_table_header);
8360 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008361 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008363 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008365 destroy_workqueue(md_misc_wq);
8366 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367}
8368
Dan Williams685784a2007-07-09 11:56:42 -07008369subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370module_exit(md_exit)
8371
NeilBrownf91de922005-11-08 21:39:36 -08008372static int get_ro(char *buffer, struct kernel_param *kp)
8373{
8374 return sprintf(buffer, "%d", start_readonly);
8375}
8376static int set_ro(const char *val, struct kernel_param *kp)
8377{
8378 char *e;
8379 int num = simple_strtoul(val, &e, 10);
8380 if (*val && (*e == '\0' || *e == '\n')) {
8381 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008382 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008383 }
8384 return -EINVAL;
8385}
8386
NeilBrown80ca3a42006-07-10 04:44:18 -07008387module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8388module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008389
NeilBrownefeb53c2009-01-09 08:31:10 +11008390module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008391
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392EXPORT_SYMBOL(register_md_personality);
8393EXPORT_SYMBOL(unregister_md_personality);
8394EXPORT_SYMBOL(md_error);
8395EXPORT_SYMBOL(md_done_sync);
8396EXPORT_SYMBOL(md_write_start);
8397EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398EXPORT_SYMBOL(md_register_thread);
8399EXPORT_SYMBOL(md_unregister_thread);
8400EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401EXPORT_SYMBOL(md_check_recovery);
8402MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008403MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008404MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008405MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);