blob: 308e87b417e05a74e4429bac63e1eb2e7af43885 [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 Viroff01bb482011-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
Jonathan Brassow47525e52012-05-22 13:55:29 +1000405 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500406 md_wakeup_thread(mddev->thread);
407 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100408}
NeilBrown390ee602010-06-01 19:37:27 +1000409EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100410
NeilBrownfd01b882011-10-11 16:47:53 +1100411int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000412{
413 return mddev->suspended;
414}
415EXPORT_SYMBOL(mddev_congested);
416
NeilBrowna2826aa2009-12-14 12:49:49 +1100417/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 */
420
Tejun Heoe9c74692010-09-03 11:56:18 +0200421static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100422{
NeilBrown3cb03002011-10-11 16:45:26 +1100423 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100424 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100425
426 rdev_dec_pending(rdev, mddev);
427
428 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200429 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 }
432 bio_put(bio);
433}
434
NeilBrowna7a07e62010-12-09 16:04:25 +1100435static void md_submit_flush_data(struct work_struct *ws);
436
NeilBrowna035fc32010-12-09 16:17:51 +1100437static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100438{
NeilBrownfd01b882011-10-11 16:47:53 +1100439 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100440 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100441
NeilBrowna7a07e62010-12-09 16:04:25 +1100442 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
443 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100445 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100446 if (rdev->raid_disk >= 0 &&
447 !test_bit(Faulty, &rdev->flags)) {
448 /* Take two references, one is dropped
449 * when request finishes, one after
450 * we reclaim rcu_read_lock
451 */
452 struct bio *bi;
453 atomic_inc(&rdev->nr_pending);
454 atomic_inc(&rdev->nr_pending);
455 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000456 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200457 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100458 bi->bi_private = rdev;
459 bi->bi_bdev = rdev->bdev;
460 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200461 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100462 rcu_read_lock();
463 rdev_dec_pending(rdev, mddev);
464 }
465 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100466 if (atomic_dec_and_test(&mddev->flush_pending))
467 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100468}
469
Tejun Heoe9c74692010-09-03 11:56:18 +0200470static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100471{
NeilBrownfd01b882011-10-11 16:47:53 +1100472 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200473 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100474
Tejun Heoe9c74692010-09-03 11:56:18 +0200475 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100476 /* an empty barrier - all done */
477 bio_endio(bio, 0);
478 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200479 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200480 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 }
NeilBrown2b74e122010-12-09 15:59:01 +1100482
483 mddev->flush_bio = NULL;
484 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100485}
486
NeilBrownfd01b882011-10-11 16:47:53 +1100487void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100488{
489 spin_lock_irq(&mddev->write_lock);
490 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200491 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100492 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200493 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100494 spin_unlock_irq(&mddev->write_lock);
495
NeilBrowna035fc32010-12-09 16:17:51 +1100496 INIT_WORK(&mddev->flush_work, submit_flushes);
497 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100498}
Tejun Heoe9c74692010-09-03 11:56:18 +0200499EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100500
NeilBrown74018dc2012-07-31 09:08:15 +0200501void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
NeilBrown97658cd2011-04-18 18:25:42 +1000502{
NeilBrown9cbb1752012-07-31 09:08:14 +0200503 struct mddev *mddev = cb->data;
504 md_wakeup_thread(mddev->thread);
505 kfree(cb);
NeilBrown97658cd2011-04-18 18:25:42 +1000506}
NeilBrown9cbb1752012-07-31 09:08:14 +0200507EXPORT_SYMBOL(md_unplug);
NeilBrown2ac87402010-06-01 19:37:29 +1000508
NeilBrownfd01b882011-10-11 16:47:53 +1100509static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 atomic_inc(&mddev->active);
512 return mddev;
513}
514
Dan Williams5fd3a172009-03-04 00:57:25 -0700515static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100516
NeilBrownfd01b882011-10-11 16:47:53 +1100517static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
NeilBrowna167f662010-10-26 18:31:13 +1100519 struct bio_set *bs = NULL;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
522 return;
NeilBrownd3374822009-01-09 08:31:10 +1100523 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100524 mddev->ctime == 0 && !mddev->hold_active) {
525 /* Array is not configured at all, and not held active,
526 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100527 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100528 bs = mddev->bio_set;
529 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100530 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200531 /* We did a probe so need to clean up. Call
532 * queue_work inside the spinlock so that
533 * flush_workqueue() after mddev_find will
534 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100535 */
536 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200537 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100538 } else
539 kfree(mddev);
540 }
541 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100542 if (bs)
543 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
NeilBrownfd01b882011-10-11 16:47:53 +1100546void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100547{
548 mutex_init(&mddev->open_mutex);
549 mutex_init(&mddev->reconfig_mutex);
550 mutex_init(&mddev->bitmap_info.mutex);
551 INIT_LIST_HEAD(&mddev->disks);
552 INIT_LIST_HEAD(&mddev->all_mddevs);
553 init_timer(&mddev->safemode_timer);
554 atomic_set(&mddev->active, 1);
555 atomic_set(&mddev->openers, 0);
556 atomic_set(&mddev->active_io, 0);
557 spin_lock_init(&mddev->write_lock);
558 atomic_set(&mddev->flush_pending, 0);
559 init_waitqueue_head(&mddev->sb_wait);
560 init_waitqueue_head(&mddev->recovery_wait);
561 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000562 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100563 mddev->resync_min = 0;
564 mddev->resync_max = MaxSector;
565 mddev->level = LEVEL_NONE;
566}
NeilBrown390ee602010-06-01 19:37:27 +1000567EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100568
NeilBrownfd01b882011-10-11 16:47:53 +1100569static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
NeilBrownfd01b882011-10-11 16:47:53 +1100571 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
NeilBrown8f5f02c2011-02-16 13:58:51 +1100573 if (unit && MAJOR(unit) != MD_MAJOR)
574 unit &= ~((1<<MdpMinorShift)-1);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 retry:
577 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
NeilBrownefeb53c2009-01-09 08:31:10 +1100579 if (unit) {
580 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
581 if (mddev->unit == unit) {
582 mddev_get(mddev);
583 spin_unlock(&all_mddevs_lock);
584 kfree(new);
585 return mddev;
586 }
587
588 if (new) {
589 list_add(&new->all_mddevs, &all_mddevs);
590 spin_unlock(&all_mddevs_lock);
591 new->hold_active = UNTIL_IOCTL;
592 return new;
593 }
594 } else if (new) {
595 /* find an unused unit number */
596 static int next_minor = 512;
597 int start = next_minor;
598 int is_free = 0;
599 int dev = 0;
600 while (!is_free) {
601 dev = MKDEV(MD_MAJOR, next_minor);
602 next_minor++;
603 if (next_minor > MINORMASK)
604 next_minor = 0;
605 if (next_minor == start) {
606 /* Oh dear, all in use. */
607 spin_unlock(&all_mddevs_lock);
608 kfree(new);
609 return NULL;
610 }
611
612 is_free = 1;
613 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
614 if (mddev->unit == dev) {
615 is_free = 0;
616 break;
617 }
618 }
619 new->unit = dev;
620 new->md_minor = MINOR(dev);
621 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 list_add(&new->all_mddevs, &all_mddevs);
623 spin_unlock(&all_mddevs_lock);
624 return new;
625 }
626 spin_unlock(&all_mddevs_lock);
627
NeilBrown9ffae0c2006-01-06 00:20:32 -0800628 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!new)
630 return NULL;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 new->unit = unit;
633 if (MAJOR(unit) == MD_MAJOR)
634 new->md_minor = MINOR(unit);
635 else
636 new->md_minor = MINOR(unit) >> MdpMinorShift;
637
NeilBrownfafd7fb2010-04-01 15:55:30 +1100638 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto retry;
641}
642
NeilBrownfd01b882011-10-11 16:47:53 +1100643static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800645 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
NeilBrownfd01b882011-10-11 16:47:53 +1100648static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100649{
650 return mutex_is_locked(&mddev->reconfig_mutex);
651}
652
NeilBrownfd01b882011-10-11 16:47:53 +1100653static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800655 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
NeilBrownb6eb1272010-04-15 10:13:47 +1000658static struct attribute_group md_redundancy_group;
659
NeilBrownfd01b882011-10-11 16:47:53 +1100660static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
NeilBrowna64c8762010-04-14 17:15:37 +1000662 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000663 /* These cannot be removed under reconfig_mutex as
664 * an access to the files will try to take reconfig_mutex
665 * while holding the file unremovable, which leads to
666 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000667 * So hold set sysfs_active while the remove in happeing,
668 * and anything else which might set ->to_remove or my
669 * otherwise change the sysfs namespace will fail with
670 * -EBUSY if sysfs_active is still set.
671 * We set sysfs_active under reconfig_mutex and elsewhere
672 * test it under the same mutex to ensure its correct value
673 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000674 */
NeilBrowna64c8762010-04-14 17:15:37 +1000675 struct attribute_group *to_remove = mddev->to_remove;
676 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000677 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000678 mutex_unlock(&mddev->reconfig_mutex);
679
NeilBrown00bcb4a2010-06-01 19:37:23 +1000680 if (mddev->kobj.sd) {
681 if (to_remove != &md_redundancy_group)
682 sysfs_remove_group(&mddev->kobj, to_remove);
683 if (mddev->pers == NULL ||
684 mddev->pers->sync_request == NULL) {
685 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
686 if (mddev->sysfs_action)
687 sysfs_put(mddev->sysfs_action);
688 mddev->sysfs_action = NULL;
689 }
NeilBrowna64c8762010-04-14 17:15:37 +1000690 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000691 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000692 } else
693 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Chris Dunlop751e67c2011-10-19 16:48:26 +1100695 /* As we've dropped the mutex we need a spinlock to
696 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000697 */
698 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700699 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000700 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
NeilBrownfd01b882011-10-11 16:47:53 +1100703static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
NeilBrown3cb03002011-10-11 16:45:26 +1100705 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
NeilBrowndafb20f2012-03-19 12:46:39 +1100707 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (rdev->desc_nr == nr)
709 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return NULL;
712}
713
NeilBrownfd01b882011-10-11 16:47:53 +1100714static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
NeilBrown3cb03002011-10-11 16:45:26 +1100716 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
NeilBrowndafb20f2012-03-19 12:46:39 +1100718 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (rdev->bdev->bd_dev == dev)
720 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return NULL;
723}
724
NeilBrown84fc4b52011-10-11 16:49:58 +1100725static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800726{
NeilBrown84fc4b52011-10-11 16:49:58 +1100727 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800728 list_for_each_entry(pers, &pers_list, list) {
729 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800730 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800731 if (strcmp(pers->name, clevel)==0)
732 return pers;
733 }
NeilBrown2604b702006-01-06 00:20:36 -0800734 return NULL;
735}
736
Andre Nollb73df2d2008-07-11 22:02:23 +1000737/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100738static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100740 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000741 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
NeilBrown3cb03002011-10-11 16:45:26 +1100744static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 if (rdev->sb_page)
747 MD_BUG();
748
749 rdev->sb_page = alloc_page(GFP_KERNEL);
750 if (!rdev->sb_page) {
751 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000752 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 return 0;
756}
757
NeilBrown545c8792012-05-22 13:54:30 +1000758void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800761 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rdev->sb_loaded = 0;
763 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000764 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100765 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
NeilBrown2699b672011-07-28 11:31:47 +1000767 if (rdev->bb_page) {
768 put_page(rdev->bb_page);
769 rdev->bb_page = NULL;
770 }
NeilBrown4fa2f322012-05-22 13:55:01 +1000771 kfree(rdev->badblocks.page);
772 rdev->badblocks.page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
NeilBrown545c8792012-05-22 13:54:30 +1000774EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
NeilBrown6712ecf2007-09-27 12:47:43 +0200776static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777{
NeilBrown3cb03002011-10-11 16:45:26 +1100778 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100779 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700780
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700781 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
782 printk("md: super_written gets error=%d, uptodate=%d\n",
783 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
784 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800785 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700786 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700787
NeilBrowna9701a32005-11-08 21:39:34 -0800788 if (atomic_dec_and_test(&mddev->pending_writes))
789 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700790 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700791}
792
NeilBrownfd01b882011-10-11 16:47:53 +1100793void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794 sector_t sector, int size, struct page *page)
795{
796 /* write first size bytes of page to sector of rdev
797 * Increment mddev->pending_writes before returning
798 * and decrement it on completion, waking up sb_wait
799 * if zero is reached.
800 * If an error occurred, call md_error
801 */
NeilBrowna167f662010-10-26 18:31:13 +1100802 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700803
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100804 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700805 bio->bi_sector = sector;
806 bio_add_page(bio, page, size, 0);
807 bio->bi_private = rdev;
808 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800809
NeilBrown7bfa19f2005-06-21 17:17:28 -0700810 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000811 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800812}
813
NeilBrownfd01b882011-10-11 16:47:53 +1100814void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800815{
Tejun Heoe9c74692010-09-03 11:56:18 +0200816 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800817 DEFINE_WAIT(wq);
818 for(;;) {
819 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
820 if (atomic_read(&mddev->pending_writes)==0)
821 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800822 schedule();
823 }
824 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700825}
826
NeilBrown6712ecf2007-09-27 12:47:43 +0200827static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
NeilBrown3cb03002011-10-11 16:45:26 +1100832int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100833 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
NeilBrowna167f662010-10-26 18:31:13 +1100835 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct completion event;
837 int ret;
838
Jens Axboe721a9602011-03-09 11:56:30 +0100839 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100841 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
842 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100843 if (metadata_op)
844 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000845 else if (rdev->mddev->reshape_position != MaxSector &&
846 (rdev->mddev->reshape_backwards ==
847 (sector >= rdev->mddev->reshape_position)))
848 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100849 else
850 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 bio_add_page(bio, page, size, 0);
852 init_completion(&event);
853 bio->bi_private = &event;
854 bio->bi_end_io = bi_complete;
855 submit_bio(rw, bio);
856 wait_for_completion(&event);
857
858 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
859 bio_put(bio);
860 return ret;
861}
NeilBrowna8745db2006-01-06 00:20:34 -0800862EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
NeilBrown3cb03002011-10-11 16:45:26 +1100864static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 char b[BDEVNAME_SIZE];
867 if (!rdev->sb_page) {
868 MD_BUG();
869 return -EINVAL;
870 }
871 if (rdev->sb_loaded)
872 return 0;
873
874
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100875 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto fail;
877 rdev->sb_loaded = 1;
878 return 0;
879
880fail:
881 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
882 bdevname(rdev->bdev,b));
883 return -EINVAL;
884}
885
886static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
887{
Andre Noll05710462008-07-11 22:02:20 +1000888 return sb1->set_uuid0 == sb2->set_uuid0 &&
889 sb1->set_uuid1 == sb2->set_uuid1 &&
890 sb1->set_uuid2 == sb2->set_uuid2 &&
891 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
895{
896 int ret;
897 mdp_super_t *tmp1, *tmp2;
898
899 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
900 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
901
902 if (!tmp1 || !tmp2) {
903 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100904 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto abort;
906 }
907
908 *tmp1 = *sb1;
909 *tmp2 = *sb2;
910
911 /*
912 * nr_disks is not constant
913 */
914 tmp1->nr_disks = 0;
915 tmp2->nr_disks = 0;
916
Andre Nollce0c8e02008-07-11 22:02:20 +1000917 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700919 kfree(tmp1);
920 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return ret;
922}
923
NeilBrown4d167f02007-05-09 02:35:37 -0700924
925static u32 md_csum_fold(u32 csum)
926{
927 csum = (csum & 0xffff) + (csum >> 16);
928 return (csum & 0xffff) + (csum >> 16);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static unsigned int calc_sb_csum(mdp_super_t * sb)
932{
NeilBrown4d167f02007-05-09 02:35:37 -0700933 u64 newcsum = 0;
934 u32 *sb32 = (u32*)sb;
935 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned int disk_csum, csum;
937
938 disk_csum = sb->sb_csum;
939 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700940
941 for (i = 0; i < MD_SB_BYTES/4 ; i++)
942 newcsum += sb32[i];
943 csum = (newcsum & 0xffffffff) + (newcsum>>32);
944
945
946#ifdef CONFIG_ALPHA
947 /* This used to use csum_partial, which was wrong for several
948 * reasons including that different results are returned on
949 * different architectures. It isn't critical that we get exactly
950 * the same return value as before (we always csum_fold before
951 * testing, and that removes any differences). However as we
952 * know that csum_partial always returned a 16bit value on
953 * alphas, do a fold to maximise conformity to previous behaviour.
954 */
955 sb->sb_csum = md_csum_fold(disk_csum);
956#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return csum;
960}
961
962
963/*
964 * Handle superblock details.
965 * We want to be able to handle multiple superblock formats
966 * so we have a common interface to them all, and an array of
967 * different handlers.
968 * We rely on user-space to write the initial superblock, and support
969 * reading and updating of superblocks.
970 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +1100971 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * loads and validates a superblock on dev.
973 * if refdev != NULL, compare superblocks on both devices
974 * Return:
975 * 0 - dev has a superblock that is compatible with refdev
976 * 1 - dev has a superblock that is compatible and newer than refdev
977 * so dev should be used as the refdev in future
978 * -EINVAL superblock incompatible or invalid
979 * -othererror e.g. -EIO
980 *
NeilBrownfd01b882011-10-11 16:47:53 +1100981 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * Verify that dev is acceptable into mddev.
983 * The first time, mddev->raid_disks will be 0, and data from
984 * dev should be merged in. Subsequent calls check that dev
985 * is new enough. Return 0 or -EINVAL
986 *
NeilBrownfd01b882011-10-11 16:47:53 +1100987 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * Update the superblock for rdev with data in mddev
989 * This does not write to disc.
990 *
991 */
992
993struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000994 char *name;
995 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +1000996 int (*load_super)(struct md_rdev *rdev,
997 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +1000998 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +1000999 int (*validate_super)(struct mddev *mddev,
1000 struct md_rdev *rdev);
1001 void (*sync_super)(struct mddev *mddev,
1002 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001003 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001004 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001005 int (*allow_new_offset)(struct md_rdev *rdev,
1006 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007};
1008
1009/*
Andre Noll0894cc32009-06-18 08:49:23 +10001010 * Check that the given mddev has no bitmap.
1011 *
1012 * This function is called from the run method of all personalities that do not
1013 * support bitmaps. It prints an error message and returns non-zero if mddev
1014 * has a bitmap. Otherwise, it returns 0.
1015 *
1016 */
NeilBrownfd01b882011-10-11 16:47:53 +11001017int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001018{
NeilBrownc3d97142009-12-14 12:49:52 +11001019 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001020 return 0;
1021 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1022 mdname(mddev), mddev->pers->name);
1023 return 1;
1024}
1025EXPORT_SYMBOL(md_check_no_bitmap);
1026
1027/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * load_super for 0.90.0
1029 */
NeilBrown3cb03002011-10-11 16:45:26 +11001030static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1033 mdp_super_t *sb;
1034 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
Andre Noll0f420352008-07-11 22:02:23 +10001037 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * it's at the end of the disk.
1039 *
1040 * It also happens to be a multiple of 4Kb.
1041 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001042 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
NeilBrown0002b272005-09-09 16:23:53 -07001044 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (ret) return ret;
1046
1047 ret = -EINVAL;
1048
1049 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001050 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (sb->md_magic != MD_SB_MAGIC) {
1053 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1054 b);
1055 goto abort;
1056 }
1057
1058 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001059 sb->minor_version < 90 ||
1060 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1062 sb->major_version, sb->minor_version,
1063 b);
1064 goto abort;
1065 }
1066
1067 if (sb->raid_disks <= 0)
1068 goto abort;
1069
NeilBrown4d167f02007-05-09 02:35:37 -07001070 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1072 b);
1073 goto abort;
1074 }
1075
1076 rdev->preferred_minor = sb->md_minor;
1077 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001078 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001079 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001080 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (sb->level == LEVEL_MULTIPATH)
1083 rdev->desc_nr = -1;
1084 else
1085 rdev->desc_nr = sb->this_disk.number;
1086
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001087 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001089 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001091 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!uuid_equal(refsb, sb)) {
1093 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1094 b, bdevname(refdev->bdev,b2));
1095 goto abort;
1096 }
1097 if (!sb_equal(refsb, sb)) {
1098 printk(KERN_WARNING "md: %s has same UUID"
1099 " but different superblock to %s\n",
1100 b, bdevname(refdev->bdev, b2));
1101 goto abort;
1102 }
1103 ev1 = md_event(sb);
1104 ev2 = md_event(refsb);
1105 if (ev1 > ev2)
1106 ret = 1;
1107 else
1108 ret = 0;
1109 }
NeilBrown8190e752009-06-18 08:48:58 +10001110 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001111 /* Limit to 4TB as metadata cannot record more than that.
1112 * (not needed for Linear and RAID0 as metadata doesn't
1113 * record this size)
1114 */
1115 if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001116 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
NeilBrown27a7b262011-09-10 17:21:28 +10001118 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001119 /* "this cannot possibly happen" ... */
1120 ret = -EINVAL;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 abort:
1123 return ret;
1124}
1125
1126/*
1127 * validate_super for 0.90.0
1128 */
NeilBrownfd01b882011-10-11 16:47:53 +11001129static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001132 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001133 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
NeilBrown41158c72005-06-21 17:17:25 -07001135 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001136 clear_bit(Faulty, &rdev->flags);
1137 clear_bit(In_sync, &rdev->flags);
1138 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (mddev->raid_disks == 0) {
1141 mddev->major_version = 0;
1142 mddev->minor_version = sb->minor_version;
1143 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001144 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001145 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mddev->ctime = sb->ctime;
1147 mddev->utime = sb->utime;
1148 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001149 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 mddev->layout = sb->layout;
1151 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001152 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001153 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001154 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001155 mddev->bitmap_info.space = 0;
1156 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001157 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001158 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001159 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
NeilBrownf6705572006-03-27 01:18:11 -08001161 if (mddev->minor_version >= 91) {
1162 mddev->reshape_position = sb->reshape_position;
1163 mddev->delta_disks = sb->delta_disks;
1164 mddev->new_level = sb->new_level;
1165 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001166 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001167 if (mddev->delta_disks < 0)
1168 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001169 } else {
1170 mddev->reshape_position = MaxSector;
1171 mddev->delta_disks = 0;
1172 mddev->new_level = mddev->level;
1173 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001174 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001175 }
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (sb->state & (1<<MD_SB_CLEAN))
1178 mddev->recovery_cp = MaxSector;
1179 else {
1180 if (sb->events_hi == sb->cp_events_hi &&
1181 sb->events_lo == sb->cp_events_lo) {
1182 mddev->recovery_cp = sb->recovery_cp;
1183 } else
1184 mddev->recovery_cp = 0;
1185 }
1186
1187 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1188 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1189 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1190 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1191
1192 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001193
1194 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001195 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001196 mddev->bitmap_info.offset =
1197 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001198 mddev->bitmap_info.space =
1199 mddev->bitmap_info.space;
1200 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001201
NeilBrown41158c72005-06-21 17:17:25 -07001202 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001203 /* Insist on good event counter while assembling, except
1204 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001206 if (sb->disks[rdev->desc_nr].state & (
1207 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1208 if (ev1 < mddev->events)
1209 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001210 } else if (mddev->bitmap) {
1211 /* if adding to array with a bitmap, then we can accept an
1212 * older device ... but not too old.
1213 */
NeilBrown41158c72005-06-21 17:17:25 -07001214 if (ev1 < mddev->bitmap->events_cleared)
1215 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001216 } else {
1217 if (ev1 < mddev->events)
1218 /* just a hot-add of a new device, leave raid_disk at -1 */
1219 return 0;
1220 }
NeilBrown41158c72005-06-21 17:17:25 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 desc = sb->disks + rdev->desc_nr;
1224
1225 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001226 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001227 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1228 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001229 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001231 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1232 /* active but not in sync implies recovery up to
1233 * reshape position. We don't know exactly where
1234 * that is, so set to zero for now */
1235 if (mddev->minor_version >= 91) {
1236 rdev->recovery_offset = 0;
1237 rdev->raid_disk = desc->raid_disk;
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001240 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1241 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001242 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001243 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
1247/*
1248 * sync_super for 0.90.0
1249 */
NeilBrownfd01b882011-10-11 16:47:53 +11001250static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001253 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* make rdev->sb match mddev data..
1258 *
1259 * 1/ zero out disks
1260 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1261 * 3/ any empty disks < next_spare become removed
1262 *
1263 * disks[0] gets initialised to REMOVED because
1264 * we cannot be sure from other fields if it has
1265 * been initialised or not.
1266 */
1267 int i;
1268 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1269
NeilBrown61181562005-09-09 16:24:02 -07001270 rdev->sb_size = MD_SB_BYTES;
1271
Namhyung Kim65a06f062011-07-27 11:00:36 +10001272 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 memset(sb, 0, sizeof(*sb));
1275
1276 sb->md_magic = MD_SB_MAGIC;
1277 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 sb->patch_version = mddev->patch_version;
1279 sb->gvalid_words = 0; /* ignored */
1280 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1281 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1282 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1283 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1284
1285 sb->ctime = mddev->ctime;
1286 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001287 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sb->raid_disks = mddev->raid_disks;
1289 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001290 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 sb->utime = mddev->utime;
1292 sb->state = 0;
1293 sb->events_hi = (mddev->events>>32);
1294 sb->events_lo = (u32)mddev->events;
1295
NeilBrownf6705572006-03-27 01:18:11 -08001296 if (mddev->reshape_position == MaxSector)
1297 sb->minor_version = 90;
1298 else {
1299 sb->minor_version = 91;
1300 sb->reshape_position = mddev->reshape_position;
1301 sb->new_level = mddev->new_level;
1302 sb->delta_disks = mddev->delta_disks;
1303 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001304 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001305 }
1306 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (mddev->in_sync)
1308 {
1309 sb->recovery_cp = mddev->recovery_cp;
1310 sb->cp_events_hi = (mddev->events>>32);
1311 sb->cp_events_lo = (u32)mddev->events;
1312 if (mddev->recovery_cp == MaxSector)
1313 sb->state = (1<< MD_SB_CLEAN);
1314 } else
1315 sb->recovery_cp = 0;
1316
1317 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001318 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
NeilBrownc3d97142009-12-14 12:49:52 +11001320 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001321 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001324 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001326 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001327 int is_active = test_bit(In_sync, &rdev2->flags);
1328
1329 if (rdev2->raid_disk >= 0 &&
1330 sb->minor_version >= 91)
1331 /* we have nowhere to store the recovery_offset,
1332 * but if it is not below the reshape_position,
1333 * we can piggy-back on that.
1334 */
1335 is_active = 1;
1336 if (rdev2->raid_disk < 0 ||
1337 test_bit(Faulty, &rdev2->flags))
1338 is_active = 0;
1339 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001340 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001342 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001343 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 d = &sb->disks[rdev2->desc_nr];
1345 nr_disks++;
1346 d->number = rdev2->desc_nr;
1347 d->major = MAJOR(rdev2->bdev->bd_dev);
1348 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001349 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 d->raid_disk = rdev2->raid_disk;
1351 else
1352 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001353 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001355 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001357 if (test_bit(In_sync, &rdev2->flags))
1358 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 active++;
1360 working++;
1361 } else {
1362 d->state = 0;
1363 spare++;
1364 working++;
1365 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001366 if (test_bit(WriteMostly, &rdev2->flags))
1367 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* now set the "removed" and "faulty" bits on any missing devices */
1370 for (i=0 ; i < mddev->raid_disks ; i++) {
1371 mdp_disk_t *d = &sb->disks[i];
1372 if (d->state == 0 && d->number == 0) {
1373 d->number = i;
1374 d->raid_disk = i;
1375 d->state = (1<<MD_DISK_REMOVED);
1376 d->state |= (1<<MD_DISK_FAULTY);
1377 failed++;
1378 }
1379 }
1380 sb->nr_disks = nr_disks;
1381 sb->active_disks = active;
1382 sb->working_disks = working;
1383 sb->failed_disks = failed;
1384 sb->spare_disks = spare;
1385
1386 sb->this_disk = sb->disks[rdev->desc_nr];
1387 sb->sb_csum = calc_sb_csum(sb);
1388}
1389
1390/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001391 * rdev_size_change for 0.90.0
1392 */
1393static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001394super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001395{
Andre Noll58c0fed2009-03-31 14:33:13 +11001396 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001397 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001398 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001399 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001400 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001401 if (!num_sectors || num_sectors > rdev->sb_start)
1402 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001403 /* Limit to 4TB as metadata cannot record more than that.
1404 * 4TB == 2^32 KB, or 2*2^32 sectors.
1405 */
NeilBrown667a5312012-08-16 16:46:12 +10001406 if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
NeilBrown27a7b262011-09-10 17:21:28 +10001407 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001408 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001409 rdev->sb_page);
1410 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001411 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412}
1413
NeilBrownc6563a82012-05-21 09:27:00 +10001414static int
1415super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1416{
1417 /* non-zero offset changes not possible with v0.90 */
1418 return new_offset == 0;
1419}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420
1421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * version 1 superblock
1423 */
1424
NeilBrown1c05b4b2006-10-21 10:24:08 -07001425static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001427 __le32 disk_csum;
1428 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 unsigned long long newcsum;
1430 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001431 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int i;
1433
1434 disk_csum = sb->sb_csum;
1435 sb->sb_csum = 0;
1436 newcsum = 0;
1437 for (i=0; size>=4; size -= 4 )
1438 newcsum += le32_to_cpu(*isuper++);
1439
1440 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001441 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1444 sb->sb_csum = disk_csum;
1445 return cpu_to_le32(csum);
1446}
1447
NeilBrown2699b672011-07-28 11:31:47 +10001448static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1449 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001450static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 struct mdp_superblock_1 *sb;
1453 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001454 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001455 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001457 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /*
Andre Noll0f420352008-07-11 22:02:23 +10001460 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 * It is always aligned to a 4K boundary and
1462 * depeding on minor_version, it can be:
1463 * 0: At least 8K, but less than 12K, from end of device
1464 * 1: At start of device
1465 * 2: 4K from start of device.
1466 */
1467 switch(minor_version) {
1468 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001469 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001470 sb_start -= 8*2;
1471 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001474 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001477 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479 default:
1480 return -EINVAL;
1481 }
Andre Noll0f420352008-07-11 22:02:23 +10001482 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
NeilBrown0002b272005-09-09 16:23:53 -07001484 /* superblock is rarely larger than 1K, but it can be larger,
1485 * and it is safe to read 4k, so we do that
1486 */
1487 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (ret) return ret;
1489
1490
Namhyung Kim65a06f062011-07-27 11:00:36 +10001491 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1494 sb->major_version != cpu_to_le32(1) ||
1495 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001496 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001497 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return -EINVAL;
1499
1500 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1501 printk("md: invalid superblock checksum on %s\n",
1502 bdevname(rdev->bdev,b));
1503 return -EINVAL;
1504 }
1505 if (le64_to_cpu(sb->data_size) < 10) {
1506 printk("md: data_size too small on %s\n",
1507 bdevname(rdev->bdev,b));
1508 return -EINVAL;
1509 }
NeilBrownc6563a82012-05-21 09:27:00 +10001510 if (sb->pad0 ||
1511 sb->pad3[0] ||
1512 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1513 /* Some padding is non-zero, might be a new feature */
1514 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 rdev->preferred_minor = 0xffff;
1517 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001518 rdev->new_data_offset = rdev->data_offset;
1519 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1520 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1521 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001522 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
NeilBrown0002b272005-09-09 16:23:53 -07001524 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001525 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001526 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001527 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1528
1529 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001530 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001531 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001532 if (minor_version
1533 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1534 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001535
NeilBrown31b65a02006-07-10 04:44:14 -07001536 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1537 rdev->desc_nr = -1;
1538 else
1539 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1540
NeilBrown2699b672011-07-28 11:31:47 +10001541 if (!rdev->bb_page) {
1542 rdev->bb_page = alloc_page(GFP_KERNEL);
1543 if (!rdev->bb_page)
1544 return -ENOMEM;
1545 }
1546 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1547 rdev->badblocks.count == 0) {
1548 /* need to load the bad block list.
1549 * Currently we limit it to one page.
1550 */
1551 s32 offset;
1552 sector_t bb_sector;
1553 u64 *bbp;
1554 int i;
1555 int sectors = le16_to_cpu(sb->bblog_size);
1556 if (sectors > (PAGE_SIZE / 512))
1557 return -EINVAL;
1558 offset = le32_to_cpu(sb->bblog_offset);
1559 if (offset == 0)
1560 return -EINVAL;
1561 bb_sector = (long long)offset;
1562 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1563 rdev->bb_page, READ, true))
1564 return -EIO;
1565 bbp = (u64 *)page_address(rdev->bb_page);
1566 rdev->badblocks.shift = sb->bblog_shift;
1567 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1568 u64 bb = le64_to_cpu(*bbp);
1569 int count = bb & (0x3ff);
1570 u64 sector = bb >> 10;
1571 sector <<= sb->bblog_shift;
1572 count <<= sb->bblog_shift;
1573 if (bb + 1 == 0)
1574 break;
1575 if (md_set_badblocks(&rdev->badblocks,
1576 sector, count, 1) == 0)
1577 return -EINVAL;
1578 }
1579 } else if (sb->bblog_offset == 0)
1580 rdev->badblocks.shift = -1;
1581
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001582 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001583 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001584 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001586 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1589 sb->level != refsb->level ||
1590 sb->layout != refsb->layout ||
1591 sb->chunksize != refsb->chunksize) {
1592 printk(KERN_WARNING "md: %s has strangely different"
1593 " superblock to %s\n",
1594 bdevname(rdev->bdev,b),
1595 bdevname(refdev->bdev,b2));
1596 return -EINVAL;
1597 }
1598 ev1 = le64_to_cpu(sb->events);
1599 ev2 = le64_to_cpu(refsb->events);
1600
1601 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001602 ret = 1;
1603 else
1604 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
NeilBrownc6563a82012-05-21 09:27:00 +10001606 if (minor_version) {
1607 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1608 sectors -= rdev->data_offset;
1609 } else
1610 sectors = rdev->sb_start;
1611 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001613 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001614 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
NeilBrownfd01b882011-10-11 16:47:53 +11001617static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001619 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001620 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
NeilBrown41158c72005-06-21 17:17:25 -07001622 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001623 clear_bit(Faulty, &rdev->flags);
1624 clear_bit(In_sync, &rdev->flags);
1625 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (mddev->raid_disks == 0) {
1628 mddev->major_version = 1;
1629 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001630 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001631 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1633 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1634 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001635 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 mddev->layout = le32_to_cpu(sb->layout);
1637 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001638 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001639 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001640 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001641 mddev->bitmap_info.space = 0;
1642 /* Default location for bitmap is 1K after superblock
1643 * using 3K - total of 4K
1644 */
NeilBrownc3d97142009-12-14 12:49:52 +11001645 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001646 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001647 mddev->reshape_backwards = 0;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1650 memcpy(mddev->uuid, sb->set_uuid, 16);
1651
1652 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001653
NeilBrown71c08052005-09-09 16:23:51 -07001654 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001655 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001656 mddev->bitmap_info.offset =
1657 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001658 /* Metadata doesn't record how much space is available.
1659 * For 1.0, we assume we can use up to the superblock
1660 * if before, else to 4K beyond superblock.
1661 * For others, assume no change is possible.
1662 */
1663 if (mddev->minor_version > 0)
1664 mddev->bitmap_info.space = 0;
1665 else if (mddev->bitmap_info.offset > 0)
1666 mddev->bitmap_info.space =
1667 8 - mddev->bitmap_info.offset;
1668 else
1669 mddev->bitmap_info.space =
1670 -mddev->bitmap_info.offset;
1671 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001672
NeilBrownf6705572006-03-27 01:18:11 -08001673 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1674 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1675 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1676 mddev->new_level = le32_to_cpu(sb->new_level);
1677 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001678 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001679 if (mddev->delta_disks < 0 ||
1680 (mddev->delta_disks == 0 &&
1681 (le32_to_cpu(sb->feature_map)
1682 & MD_FEATURE_RESHAPE_BACKWARDS)))
1683 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001684 } else {
1685 mddev->reshape_position = MaxSector;
1686 mddev->delta_disks = 0;
1687 mddev->new_level = mddev->level;
1688 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001689 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001690 }
1691
NeilBrown41158c72005-06-21 17:17:25 -07001692 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001693 /* Insist of good event counter while assembling, except for
1694 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001696 if (rdev->desc_nr >= 0 &&
1697 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1698 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1699 if (ev1 < mddev->events)
1700 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001701 } else if (mddev->bitmap) {
1702 /* If adding to array with a bitmap, then we can accept an
1703 * older device, but not too old.
1704 */
NeilBrown41158c72005-06-21 17:17:25 -07001705 if (ev1 < mddev->bitmap->events_cleared)
1706 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001707 } else {
1708 if (ev1 < mddev->events)
1709 /* just a hot-add of a new device, leave raid_disk at -1 */
1710 return 0;
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (mddev->level != LEVEL_MULTIPATH) {
1713 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001714 if (rdev->desc_nr < 0 ||
1715 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1716 role = 0xffff;
1717 rdev->desc_nr = -1;
1718 } else
1719 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 switch(role) {
1721 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001724 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 break;
1726 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001727 if ((le32_to_cpu(sb->feature_map) &
1728 MD_FEATURE_RECOVERY_OFFSET))
1729 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1730 else
1731 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 rdev->raid_disk = role;
1733 break;
1734 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001735 if (sb->devflags & WriteMostly1)
1736 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001737 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1738 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001739 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001740 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
1744
NeilBrownfd01b882011-10-11 16:47:53 +11001745static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001748 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int max_dev, i;
1750 /* make rdev->sb match mddev and rdev data. */
1751
Namhyung Kim65a06f062011-07-27 11:00:36 +10001752 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 sb->feature_map = 0;
1755 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001756 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 memset(sb->pad3, 0, sizeof(sb->pad3));
1758
1759 sb->utime = cpu_to_le64((__u64)mddev->utime);
1760 sb->events = cpu_to_le64(mddev->events);
1761 if (mddev->in_sync)
1762 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1763 else
1764 sb->resync_offset = cpu_to_le64(0);
1765
NeilBrown1c05b4b2006-10-21 10:24:08 -07001766 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001767
NeilBrownf0ca3402006-02-02 14:28:04 -08001768 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001769 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001770 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001771 sb->level = cpu_to_le32(mddev->level);
1772 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001773
NeilBrownaeb9b212011-08-25 14:43:08 +10001774 if (test_bit(WriteMostly, &rdev->flags))
1775 sb->devflags |= WriteMostly1;
1776 else
1777 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001778 sb->data_offset = cpu_to_le64(rdev->data_offset);
1779 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b212011-08-25 14:43:08 +10001780
NeilBrownc3d97142009-12-14 12:49:52 +11001781 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1782 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001783 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001784 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001785
1786 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001787 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001788 sb->feature_map |=
1789 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1790 sb->recovery_offset =
1791 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001792 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001793 if (test_bit(Replacement, &rdev->flags))
1794 sb->feature_map |=
1795 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001796
NeilBrownf6705572006-03-27 01:18:11 -08001797 if (mddev->reshape_position != MaxSector) {
1798 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1799 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1800 sb->new_layout = cpu_to_le32(mddev->new_layout);
1801 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1802 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001803 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001804 if (mddev->delta_disks == 0 &&
1805 mddev->reshape_backwards)
1806 sb->feature_map
1807 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001808 if (rdev->new_data_offset != rdev->data_offset) {
1809 sb->feature_map
1810 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1811 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1812 - rdev->data_offset));
1813 }
NeilBrownf6705572006-03-27 01:18:11 -08001814 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001815
NeilBrown2699b672011-07-28 11:31:47 +10001816 if (rdev->badblocks.count == 0)
1817 /* Nothing to do for bad blocks*/ ;
1818 else if (sb->bblog_offset == 0)
1819 /* Cannot record bad blocks on this device */
1820 md_error(mddev, rdev);
1821 else {
1822 struct badblocks *bb = &rdev->badblocks;
1823 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1824 u64 *p = bb->page;
1825 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1826 if (bb->changed) {
1827 unsigned seq;
1828
1829retry:
1830 seq = read_seqbegin(&bb->lock);
1831
1832 memset(bbp, 0xff, PAGE_SIZE);
1833
1834 for (i = 0 ; i < bb->count ; i++) {
1835 u64 internal_bb = *p++;
1836 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1837 | BB_LEN(internal_bb));
1838 *bbp++ = cpu_to_le64(store_bb);
1839 }
NeilBrownd0962932012-03-19 12:46:41 +11001840 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001841 if (read_seqretry(&bb->lock, seq))
1842 goto retry;
1843
1844 bb->sector = (rdev->sb_start +
1845 (int)le32_to_cpu(sb->bblog_offset));
1846 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001847 }
1848 }
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001851 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (rdev2->desc_nr+1 > max_dev)
1853 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001854
NeilBrown70471da2009-08-03 10:59:57 +10001855 if (max_dev > le32_to_cpu(sb->max_dev)) {
1856 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001857 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001858 rdev->sb_size = max_dev * 2 + 256;
1859 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1860 if (rdev->sb_size & bmask)
1861 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001862 } else
1863 max_dev = le32_to_cpu(sb->max_dev);
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 for (i=0; i<max_dev;i++)
1866 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1867
NeilBrowndafb20f2012-03-19 12:46:39 +11001868 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001870 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001872 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001874 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001875 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 else
1877 sb->dev_roles[i] = cpu_to_le16(0xffff);
1878 }
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 sb->sb_csum = calc_sb_1_csum(sb);
1881}
1882
Chris Webb0cd17fe2008-06-28 08:31:46 +10001883static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001884super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001885{
1886 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001887 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001888 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001889 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001890 if (rdev->data_offset != rdev->new_data_offset)
1891 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001892 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001894 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001895 max_sectors -= rdev->data_offset;
1896 if (!num_sectors || num_sectors > max_sectors)
1897 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001898 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001899 /* minor version 0 with bitmap we can't move */
1900 return 0;
1901 } else {
1902 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001903 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001904 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001905 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001906 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001907 if (!num_sectors || num_sectors > max_sectors)
1908 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001909 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001910 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001911 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001912 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001913 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001914 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001915 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001916 rdev->sb_page);
1917 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001918 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001919
1920}
1921
1922static int
1923super_1_allow_new_offset(struct md_rdev *rdev,
1924 unsigned long long new_offset)
1925{
1926 /* All necessary checks on new >= old have been done */
1927 struct bitmap *bitmap;
1928 if (new_offset >= rdev->data_offset)
1929 return 1;
1930
1931 /* with 1.0 metadata, there is no metadata to tread on
1932 * so we can always move back */
1933 if (rdev->mddev->minor_version == 0)
1934 return 1;
1935
1936 /* otherwise we must be sure not to step on
1937 * any metadata, so stay:
1938 * 36K beyond start of superblock
1939 * beyond end of badblocks
1940 * beyond write-intent bitmap
1941 */
1942 if (rdev->sb_start + (32+4)*2 > new_offset)
1943 return 0;
1944 bitmap = rdev->mddev->bitmap;
1945 if (bitmap && !rdev->mddev->bitmap_info.file &&
1946 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001947 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001948 return 0;
1949 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1950 return 0;
1951
1952 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001953}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Adrian Bunk75c96f82005-05-05 16:16:09 -07001955static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 [0] = {
1957 .name = "0.90.0",
1958 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001959 .load_super = super_90_load,
1960 .validate_super = super_90_validate,
1961 .sync_super = super_90_sync,
1962 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001963 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 },
1965 [1] = {
1966 .name = "md-1",
1967 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001968 .load_super = super_1_load,
1969 .validate_super = super_1_validate,
1970 .sync_super = super_1_sync,
1971 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001972 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 },
1974};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
NeilBrownfd01b882011-10-11 16:47:53 +11001976static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001977{
1978 if (mddev->sync_super) {
1979 mddev->sync_super(mddev, rdev);
1980 return;
1981 }
1982
1983 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1984
1985 super_types[mddev->major_version].sync_super(mddev, rdev);
1986}
1987
NeilBrownfd01b882011-10-11 16:47:53 +11001988static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
NeilBrown3cb03002011-10-11 16:45:26 +11001990 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
NeilBrown4b809912008-07-21 17:05:25 +10001992 rcu_read_lock();
1993 rdev_for_each_rcu(rdev, mddev1)
1994 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001995 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001996 rdev2->bdev->bd_contains) {
1997 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001998 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001999 }
2000 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return 0;
2002}
2003
2004static LIST_HEAD(pending_raid_disks);
2005
Andre Nollac5e7112009-08-03 10:59:47 +10002006/*
2007 * Try to register data integrity profile for an mddev
2008 *
2009 * This is called when an array is started and after a disk has been kicked
2010 * from the array. It only succeeds if all working and active component devices
2011 * are integrity capable with matching profiles.
2012 */
NeilBrownfd01b882011-10-11 16:47:53 +11002013int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002014{
NeilBrown3cb03002011-10-11 16:45:26 +11002015 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002016
Andre Nollac5e7112009-08-03 10:59:47 +10002017 if (list_empty(&mddev->disks))
2018 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002019 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2020 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002021 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002022 /* skip spares and non-functional disks */
2023 if (test_bit(Faulty, &rdev->flags))
2024 continue;
2025 if (rdev->raid_disk < 0)
2026 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002027 if (!reference) {
2028 /* Use the first rdev as the reference */
2029 reference = rdev;
2030 continue;
2031 }
2032 /* does this rdev's profile match the reference profile? */
2033 if (blk_integrity_compare(reference->bdev->bd_disk,
2034 rdev->bdev->bd_disk) < 0)
2035 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002036 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002037 if (!reference || !bdev_get_integrity(reference->bdev))
2038 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002039 /*
2040 * All component devices are integrity capable and have matching
2041 * profiles, register the common profile for the md device.
2042 */
2043 if (blk_integrity_register(mddev->gendisk,
2044 bdev_get_integrity(reference->bdev)) != 0) {
2045 printk(KERN_ERR "md: failed to register integrity for %s\n",
2046 mdname(mddev));
2047 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002048 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002049 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2050 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2051 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2052 mdname(mddev));
2053 return -EINVAL;
2054 }
Andre Nollac5e7112009-08-03 10:59:47 +10002055 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002056}
Andre Nollac5e7112009-08-03 10:59:47 +10002057EXPORT_SYMBOL(md_integrity_register);
2058
2059/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002060void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002061{
2062 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
2063 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
2064
2065 if (!bi_mddev) /* nothing to do */
2066 return;
2067 if (rdev->raid_disk < 0) /* skip spares */
2068 return;
2069 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2070 rdev->bdev->bd_disk) >= 0)
2071 return;
2072 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2073 blk_integrity_unregister(mddev->gendisk);
2074}
2075EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002076
NeilBrownfd01b882011-10-11 16:47:53 +11002077static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002079 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002080 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002081 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002082 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 if (rdev->mddev) {
2085 MD_BUG();
2086 return -EINVAL;
2087 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002088
2089 /* prevent duplicates */
2090 if (find_rdev(mddev, rdev->bdev->bd_dev))
2091 return -EEXIST;
2092
Andre Nolldd8ac332009-03-31 14:33:13 +11002093 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2094 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2095 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002096 if (mddev->pers) {
2097 /* Cannot change size, so fail
2098 * If mddev->level <= 0, then we don't care
2099 * about aligning sizes (e.g. linear)
2100 */
2101 if (mddev->level > 0)
2102 return -ENOSPC;
2103 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002104 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 /* Verify rdev->desc_nr is unique.
2108 * If it is -1, assign a free number, else
2109 * check number is not in use
2110 */
2111 if (rdev->desc_nr < 0) {
2112 int choice = 0;
2113 if (mddev->pers) choice = mddev->raid_disks;
2114 while (find_rdev_nr(mddev, choice))
2115 choice++;
2116 rdev->desc_nr = choice;
2117 } else {
2118 if (find_rdev_nr(mddev, rdev->desc_nr))
2119 return -EBUSY;
2120 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002121 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2122 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2123 mdname(mddev), mddev->max_disks);
2124 return -EBUSY;
2125 }
NeilBrown19133a42005-11-08 21:39:35 -08002126 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002127 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002128 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002131 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002132
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002133 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002134 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002135
Tejun Heo0762b8b2008-08-25 19:56:12 +09002136 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002137 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2138 /* failure here is OK */;
2139 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002140
NeilBrown4b809912008-07-21 17:05:25 +10002141 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002142 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002143
2144 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002145 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002148
2149 fail:
2150 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2151 b, mdname(mddev));
2152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
NeilBrown177a99b2008-02-06 01:39:56 -08002155static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002156{
NeilBrown3cb03002011-10-11 16:45:26 +11002157 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002158 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002159 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002160}
2161
NeilBrown3cb03002011-10-11 16:45:26 +11002162static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 char b[BDEVNAME_SIZE];
2165 if (!rdev->mddev) {
2166 MD_BUG();
2167 return;
2168 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002169 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002170 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2172 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002173 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002174 sysfs_put(rdev->sysfs_state);
2175 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002176 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002177 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002178 * writing to 'remove' to "dev/state". We also need
2179 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002180 */
NeilBrown4b809912008-07-21 17:05:25 +10002181 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002182 INIT_WORK(&rdev->del_work, md_delayed_delete);
2183 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002184 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187/*
2188 * prevent the device from being mounted, repartitioned or
2189 * otherwise reused by a RAID array (or any other kernel
2190 * subsystem), by bd_claiming the device.
2191 */
NeilBrown3cb03002011-10-11 16:45:26 +11002192static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int err = 0;
2195 struct block_device *bdev;
2196 char b[BDEVNAME_SIZE];
2197
Tejun Heod4d77622010-11-13 11:55:18 +01002198 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002199 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (IS_ERR(bdev)) {
2201 printk(KERN_ERR "md: could not open %s.\n",
2202 __bdevname(dev, b));
2203 return PTR_ERR(bdev);
2204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 rdev->bdev = bdev;
2206 return err;
2207}
2208
NeilBrown3cb03002011-10-11 16:45:26 +11002209static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 struct block_device *bdev = rdev->bdev;
2212 rdev->bdev = NULL;
2213 if (!bdev)
2214 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002215 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218void md_autodetect_dev(dev_t dev);
2219
NeilBrown3cb03002011-10-11 16:45:26 +11002220static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 char b[BDEVNAME_SIZE];
2223 printk(KERN_INFO "md: export_rdev(%s)\n",
2224 bdevname(rdev->bdev,b));
2225 if (rdev->mddev)
2226 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002227 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002229 if (test_bit(AutoDetected, &rdev->flags))
2230 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231#endif
2232 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002233 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
NeilBrown3cb03002011-10-11 16:45:26 +11002236static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 unbind_rdev_from_array(rdev);
2239 export_rdev(rdev);
2240}
2241
NeilBrownfd01b882011-10-11 16:47:53 +11002242static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
NeilBrown3cb03002011-10-11 16:45:26 +11002244 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
NeilBrowndafb20f2012-03-19 12:46:39 +11002246 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!rdev->mddev) {
2248 MD_BUG();
2249 continue;
2250 }
2251 kick_rdev_from_array(rdev);
2252 }
2253 if (!list_empty(&mddev->disks))
2254 MD_BUG();
2255 mddev->raid_disks = 0;
2256 mddev->major_version = 0;
2257}
2258
2259static void print_desc(mdp_disk_t *desc)
2260{
2261 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2262 desc->major,desc->minor,desc->raid_disk,desc->state);
2263}
2264
Cheng Renquancd2ac932009-01-09 08:31:08 +11002265static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 int i;
2268
2269 printk(KERN_INFO
2270 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2271 sb->major_version, sb->minor_version, sb->patch_version,
2272 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2273 sb->ctime);
2274 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2275 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2276 sb->md_minor, sb->layout, sb->chunk_size);
2277 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2278 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2279 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2280 sb->failed_disks, sb->spare_disks,
2281 sb->sb_csum, (unsigned long)sb->events_lo);
2282
2283 printk(KERN_INFO);
2284 for (i = 0; i < MD_SB_DISKS; i++) {
2285 mdp_disk_t *desc;
2286
2287 desc = sb->disks + i;
2288 if (desc->number || desc->major || desc->minor ||
2289 desc->raid_disk || (desc->state && (desc->state != 4))) {
2290 printk(" D %2d: ", i);
2291 print_desc(desc);
2292 }
2293 }
2294 printk(KERN_INFO "md: THIS: ");
2295 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Cheng Renquancd2ac932009-01-09 08:31:08 +11002298static void print_sb_1(struct mdp_superblock_1 *sb)
2299{
2300 __u8 *uuid;
2301
2302 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002303 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002304 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002305 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002306 le32_to_cpu(sb->major_version),
2307 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002308 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002309 sb->set_name,
2310 (unsigned long long)le64_to_cpu(sb->ctime)
2311 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2312
2313 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002314 printk(KERN_INFO
2315 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002316 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002317 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002318 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2319 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002320 le32_to_cpu(sb->level),
2321 (unsigned long long)le64_to_cpu(sb->size),
2322 le32_to_cpu(sb->raid_disks),
2323 le32_to_cpu(sb->layout),
2324 le32_to_cpu(sb->chunksize),
2325 (unsigned long long)le64_to_cpu(sb->data_offset),
2326 (unsigned long long)le64_to_cpu(sb->data_size),
2327 (unsigned long long)le64_to_cpu(sb->super_offset),
2328 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2329 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002330 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002331 sb->devflags,
2332 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2333 (unsigned long long)le64_to_cpu(sb->events),
2334 (unsigned long long)le64_to_cpu(sb->resync_offset),
2335 le32_to_cpu(sb->sb_csum),
2336 le32_to_cpu(sb->max_dev)
2337 );
2338}
2339
NeilBrown3cb03002011-10-11 16:45:26 +11002340static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002343 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2344 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002345 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2346 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002348 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2349 switch (major_version) {
2350 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002351 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002352 break;
2353 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002354 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002355 break;
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 } else
2358 printk(KERN_INFO "md: no rdev superblock!\n");
2359}
2360
Adrian Bunk5e563412006-06-26 00:27:42 -07002361static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002363 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002364 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002365 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 char b[BDEVNAME_SIZE];
2367
2368 printk("\n");
2369 printk("md: **********************************\n");
2370 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2371 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002372 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
NeilBrown32a76272005-06-21 17:17:14 -07002374 if (mddev->bitmap)
2375 bitmap_print_sb(mddev->bitmap);
2376 else
2377 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002378 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 printk("<%s>", bdevname(rdev->bdev,b));
2380 printk("\n");
2381
NeilBrowndafb20f2012-03-19 12:46:39 +11002382 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002383 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385 printk("md: **********************************\n");
2386 printk("\n");
2387}
2388
2389
NeilBrownfd01b882011-10-11 16:47:53 +11002390static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
NeilBrown42543762006-06-26 00:27:57 -07002392 /* Update each superblock (in-memory image), but
2393 * if we are allowed to, skip spares which already
2394 * have the right event counter, or have one earlier
2395 * (which would mean they aren't being marked as dirty
2396 * with the rest of the array)
2397 */
NeilBrown3cb03002011-10-11 16:45:26 +11002398 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002399 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002400 if (rdev->sb_events == mddev->events ||
2401 (nospares &&
2402 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002403 rdev->sb_events+1 == mddev->events)) {
2404 /* Don't update this superblock */
2405 rdev->sb_loaded = 2;
2406 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002407 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002408 rdev->sb_loaded = 1;
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411}
2412
NeilBrownfd01b882011-10-11 16:47:53 +11002413static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
NeilBrown3cb03002011-10-11 16:45:26 +11002415 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002416 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002417 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002418 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002421 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002422 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002423 if (rdev->raid_disk >= 0 &&
2424 mddev->delta_disks >= 0 &&
2425 !test_bit(In_sync, &rdev->flags) &&
2426 mddev->curr_resync_completed > rdev->recovery_offset)
2427 rdev->recovery_offset = mddev->curr_resync_completed;
2428
2429 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002430 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002431 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002432 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002433 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002434 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002435 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002436 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002437 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002438 md_ack_all_badblocks(&rdev->badblocks);
2439 md_error(mddev, rdev);
2440 }
2441 clear_bit(Blocked, &rdev->flags);
2442 clear_bit(BlockedBadBlocks, &rdev->flags);
2443 wake_up(&rdev->blocked_wait);
2444 }
2445 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002446 wake_up(&mddev->sb_wait);
2447 return;
2448 }
2449
NeilBrowna9701a32005-11-08 21:39:34 -08002450 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002451
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002452 mddev->utime = get_seconds();
2453
NeilBrown850b2b42006-10-03 01:15:46 -07002454 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2455 force_change = 1;
2456 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2457 /* just a clean<-> dirty transition, possibly leave spares alone,
2458 * though if events isn't the right even/odd, we will have to do
2459 * spares after all
2460 */
2461 nospares = 1;
2462 if (force_change)
2463 nospares = 0;
2464 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002465 /* If the array is degraded, then skipping spares is both
2466 * dangerous and fairly pointless.
2467 * Dangerous because a device that was removed from the array
2468 * might have a event_count that still looks up-to-date,
2469 * so it can be re-added without a resync.
2470 * Pointless because if there are any spares to skip,
2471 * then a recovery will happen and soon that array won't
2472 * be degraded any more and the spare can go back to sleep then.
2473 */
NeilBrown850b2b42006-10-03 01:15:46 -07002474 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002475
NeilBrown06d91a52005-06-21 17:17:12 -07002476 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002477
2478 /* If this is just a dirty<->clean transition, and the array is clean
2479 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002480 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002481 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002482 && mddev->can_decrease_events
2483 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002484 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002485 mddev->can_decrease_events = 0;
2486 } else {
NeilBrown42543762006-06-26 00:27:57 -07002487 /* otherwise we have to go forward and ... */
2488 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002489 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 if (!mddev->events) {
2493 /*
2494 * oops, this 64-bit counter should never wrap.
2495 * Either we are in around ~1 trillion A.C., assuming
2496 * 1 reboot per second, or we have a bug:
2497 */
2498 MD_BUG();
2499 mddev->events --;
2500 }
NeilBrown2699b672011-07-28 11:31:47 +10002501
NeilBrowndafb20f2012-03-19 12:46:39 +11002502 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002503 if (rdev->badblocks.changed)
2504 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002505 if (test_bit(Faulty, &rdev->flags))
2506 set_bit(FaultRecorded, &rdev->flags);
2507 }
NeilBrown2699b672011-07-28 11:31:47 +10002508
NeilBrowne6910632008-02-06 01:39:51 -08002509 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002510 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
NeilBrown36a4e1f2011-10-07 14:23:17 +11002512 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2513 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
NeilBrown4ad13662007-07-17 04:06:13 -07002515 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002516 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002518
NeilBrown42543762006-06-26 00:27:57 -07002519 if (rdev->sb_loaded != 1)
2520 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002522 if (!test_bit(Faulty, &rdev->flags) &&
2523 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002524 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002525 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002526 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002527 pr_debug("md: (write) %s's sb offset: %llu\n",
2528 bdevname(rdev->bdev, b),
2529 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002530 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002531 if (rdev->badblocks.size) {
2532 md_super_write(mddev, rdev,
2533 rdev->badblocks.sector,
2534 rdev->badblocks.size << 9,
2535 rdev->bb_page);
2536 rdev->badblocks.size = 0;
2537 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002538
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002539 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002540 pr_debug("md: %s (skipping faulty)\n",
2541 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002542 else
2543 pr_debug("(skipping incremental s/r ");
2544
NeilBrown7bfa19f2005-06-21 17:17:28 -07002545 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /* only need to write one superblock... */
2547 break;
2548 }
NeilBrowna9701a32005-11-08 21:39:34 -08002549 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002550 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002551
NeilBrowna9701a32005-11-08 21:39:34 -08002552 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002553 if (mddev->in_sync != sync_req ||
2554 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002555 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002556 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002557 goto repeat;
2558 }
NeilBrown850b2b42006-10-03 01:15:46 -07002559 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002560 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002561 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002562 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2563 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002564
NeilBrowndafb20f2012-03-19 12:46:39 +11002565 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002566 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2567 clear_bit(Blocked, &rdev->flags);
2568
2569 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002570 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002571 clear_bit(BlockedBadBlocks, &rdev->flags);
2572 wake_up(&rdev->blocked_wait);
2573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Andre Noll7f6ce762008-03-23 18:34:54 +01002576/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002577 * We want to accept with case. For this we use cmd_match.
2578 */
2579static int cmd_match(const char *cmd, const char *str)
2580{
2581 /* See if cmd, written into a sysfs file, matches
2582 * str. They must either be the same, or cmd can
2583 * have a trailing newline
2584 */
2585 while (*cmd && *str && *cmd == *str) {
2586 cmd++;
2587 str++;
2588 }
2589 if (*cmd == '\n')
2590 cmd++;
2591 if (*str || *cmd)
2592 return 0;
2593 return 1;
2594}
2595
NeilBrown86e6ffd2005-11-08 21:39:24 -08002596struct rdev_sysfs_entry {
2597 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002598 ssize_t (*show)(struct md_rdev *, char *);
2599 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002600};
2601
2602static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002603state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604{
2605 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002606 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002607
NeilBrownde393cd2011-07-28 11:31:48 +10002608 if (test_bit(Faulty, &rdev->flags) ||
2609 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002610 len+= sprintf(page+len, "%sfaulty",sep);
2611 sep = ",";
2612 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002613 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002614 len += sprintf(page+len, "%sin_sync",sep);
2615 sep = ",";
2616 }
NeilBrownf6556752006-06-26 00:28:01 -07002617 if (test_bit(WriteMostly, &rdev->flags)) {
2618 len += sprintf(page+len, "%swrite_mostly",sep);
2619 sep = ",";
2620 }
NeilBrownde393cd2011-07-28 11:31:48 +10002621 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002622 (rdev->badblocks.unacked_exist
2623 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002624 len += sprintf(page+len, "%sblocked", sep);
2625 sep = ",";
2626 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002627 if (!test_bit(Faulty, &rdev->flags) &&
2628 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002629 len += sprintf(page+len, "%sspare", sep);
2630 sep = ",";
2631 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002632 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2633 len += sprintf(page+len, "%swrite_error", sep);
2634 sep = ",";
2635 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002636 if (test_bit(WantReplacement, &rdev->flags)) {
2637 len += sprintf(page+len, "%swant_replacement", sep);
2638 sep = ",";
2639 }
2640 if (test_bit(Replacement, &rdev->flags)) {
2641 len += sprintf(page+len, "%sreplacement", sep);
2642 sep = ",";
2643 }
2644
NeilBrown86e6ffd2005-11-08 21:39:24 -08002645 return len+sprintf(page+len, "\n");
2646}
2647
NeilBrown45dc2de2006-06-26 00:27:58 -07002648static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002649state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002650{
2651 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002652 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002653 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002654 * writemostly - sets write_mostly
2655 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002656 * blocked - sets the Blocked flags
2657 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002658 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002659 * write_error - sets WriteErrorSeen
2660 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002661 */
2662 int err = -EINVAL;
2663 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2664 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002665 if (test_bit(Faulty, &rdev->flags))
2666 err = 0;
2667 else
2668 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002669 } else if (cmd_match(buf, "remove")) {
2670 if (rdev->raid_disk >= 0)
2671 err = -EBUSY;
2672 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002673 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002674 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002675 if (mddev->pers)
2676 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002677 md_new_event(mddev);
2678 err = 0;
2679 }
NeilBrownf6556752006-06-26 00:28:01 -07002680 } else if (cmd_match(buf, "writemostly")) {
2681 set_bit(WriteMostly, &rdev->flags);
2682 err = 0;
2683 } else if (cmd_match(buf, "-writemostly")) {
2684 clear_bit(WriteMostly, &rdev->flags);
2685 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002686 } else if (cmd_match(buf, "blocked")) {
2687 set_bit(Blocked, &rdev->flags);
2688 err = 0;
2689 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002690 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002691 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002692 /* metadata handler doesn't understand badblocks,
2693 * so we need to fail the device
2694 */
2695 md_error(rdev->mddev, rdev);
2696 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002697 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002698 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002699 wake_up(&rdev->blocked_wait);
2700 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2701 md_wakeup_thread(rdev->mddev->thread);
2702
2703 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002704 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2705 set_bit(In_sync, &rdev->flags);
2706 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002707 } else if (cmd_match(buf, "write_error")) {
2708 set_bit(WriteErrorSeen, &rdev->flags);
2709 err = 0;
2710 } else if (cmd_match(buf, "-write_error")) {
2711 clear_bit(WriteErrorSeen, &rdev->flags);
2712 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002713 } else if (cmd_match(buf, "want_replacement")) {
2714 /* Any non-spare device that is not a replacement can
2715 * become want_replacement at any time, but we then need to
2716 * check if recovery is needed.
2717 */
2718 if (rdev->raid_disk >= 0 &&
2719 !test_bit(Replacement, &rdev->flags))
2720 set_bit(WantReplacement, &rdev->flags);
2721 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2722 md_wakeup_thread(rdev->mddev->thread);
2723 err = 0;
2724 } else if (cmd_match(buf, "-want_replacement")) {
2725 /* Clearing 'want_replacement' is always allowed.
2726 * Once replacements starts it is too late though.
2727 */
2728 err = 0;
2729 clear_bit(WantReplacement, &rdev->flags);
2730 } else if (cmd_match(buf, "replacement")) {
2731 /* Can only set a device as a replacement when array has not
2732 * yet been started. Once running, replacement is automatic
2733 * from spares, or by assigning 'slot'.
2734 */
2735 if (rdev->mddev->pers)
2736 err = -EBUSY;
2737 else {
2738 set_bit(Replacement, &rdev->flags);
2739 err = 0;
2740 }
2741 } else if (cmd_match(buf, "-replacement")) {
2742 /* Similarly, can only clear Replacement before start */
2743 if (rdev->mddev->pers)
2744 err = -EBUSY;
2745 else {
2746 clear_bit(Replacement, &rdev->flags);
2747 err = 0;
2748 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002749 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002750 if (!err)
2751 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002752 return err ? err : len;
2753}
NeilBrown80ca3a42006-07-10 04:44:18 -07002754static struct rdev_sysfs_entry rdev_state =
2755__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002756
2757static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002758errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002759{
2760 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2761}
2762
2763static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002764errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002765{
2766 char *e;
2767 unsigned long n = simple_strtoul(buf, &e, 10);
2768 if (*buf && (*e == 0 || *e == '\n')) {
2769 atomic_set(&rdev->corrected_errors, n);
2770 return len;
2771 }
2772 return -EINVAL;
2773}
2774static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002775__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002776
NeilBrown014236d2006-01-06 00:20:55 -08002777static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002778slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002779{
2780 if (rdev->raid_disk < 0)
2781 return sprintf(page, "none\n");
2782 else
2783 return sprintf(page, "%d\n", rdev->raid_disk);
2784}
2785
2786static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002787slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002788{
2789 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002790 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002791 int slot = simple_strtoul(buf, &e, 10);
2792 if (strncmp(buf, "none", 4)==0)
2793 slot = -1;
2794 else if (e==buf || (*e && *e!= '\n'))
2795 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002796 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002797 /* Setting 'slot' on an active array requires also
2798 * updating the 'rd%d' link, and communicating
2799 * with the personality with ->hot_*_disk.
2800 * For now we only support removing
2801 * failed/spare devices. This normally happens automatically,
2802 * but not when the metadata is externally managed.
2803 */
NeilBrownc303da62008-02-06 01:39:51 -08002804 if (rdev->raid_disk == -1)
2805 return -EEXIST;
2806 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002807 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002808 return -EINVAL;
2809 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002810 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002811 if (err)
2812 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002813 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002814 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002815 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2816 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002818 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002819 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002820 */
2821
2822 if (rdev->raid_disk != -1)
2823 return -EBUSY;
2824
NeilBrownc6751b22011-02-02 11:57:13 +11002825 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2826 return -EBUSY;
2827
Neil Brown6c2fce22008-06-28 08:31:31 +10002828 if (rdev->mddev->pers->hot_add_disk == NULL)
2829 return -EINVAL;
2830
NeilBrownba1b41b2011-01-14 09:14:34 +11002831 if (slot >= rdev->mddev->raid_disks &&
2832 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2833 return -ENOSPC;
2834
Neil Brown6c2fce22008-06-28 08:31:31 +10002835 rdev->raid_disk = slot;
2836 if (test_bit(In_sync, &rdev->flags))
2837 rdev->saved_raid_disk = slot;
2838 else
2839 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002840 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 err = rdev->mddev->pers->
2842 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002843 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002844 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002845 return err;
Neil Brown52664732008-06-28 08:31:44 +10002846 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002847 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002848 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002849 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002850 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002851 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002852 if (slot >= rdev->mddev->raid_disks &&
2853 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002854 return -ENOSPC;
2855 rdev->raid_disk = slot;
2856 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002857 clear_bit(Faulty, &rdev->flags);
2858 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002859 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002860 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002861 }
NeilBrown014236d2006-01-06 00:20:55 -08002862 return len;
2863}
2864
2865
2866static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002867__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002868
NeilBrown93c8cad2006-01-06 00:20:56 -08002869static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002870offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002871{
Andrew Morton6961ece2006-01-06 00:20:59 -08002872 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002873}
2874
2875static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002876offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002877{
NeilBrownc6563a82012-05-21 09:27:00 +10002878 unsigned long long offset;
2879 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002880 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002881 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002882 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002883 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002884 /* Must set offset before size, so overlap checks
2885 * can be sane */
2886 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002887 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002888 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002889 return len;
2890}
2891
2892static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002893__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002894
NeilBrownc6563a82012-05-21 09:27:00 +10002895static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2896{
2897 return sprintf(page, "%llu\n",
2898 (unsigned long long)rdev->new_data_offset);
2899}
2900
2901static ssize_t new_offset_store(struct md_rdev *rdev,
2902 const char *buf, size_t len)
2903{
2904 unsigned long long new_offset;
2905 struct mddev *mddev = rdev->mddev;
2906
2907 if (strict_strtoull(buf, 10, &new_offset) < 0)
2908 return -EINVAL;
2909
2910 if (mddev->sync_thread)
2911 return -EBUSY;
2912 if (new_offset == rdev->data_offset)
2913 /* reset is always permitted */
2914 ;
2915 else if (new_offset > rdev->data_offset) {
2916 /* must not push array size beyond rdev_sectors */
2917 if (new_offset - rdev->data_offset
2918 + mddev->dev_sectors > rdev->sectors)
2919 return -E2BIG;
2920 }
2921 /* Metadata worries about other space details. */
2922
2923 /* decreasing the offset is inconsistent with a backwards
2924 * reshape.
2925 */
2926 if (new_offset < rdev->data_offset &&
2927 mddev->reshape_backwards)
2928 return -EINVAL;
2929 /* Increasing offset is inconsistent with forwards
2930 * reshape. reshape_direction should be set to
2931 * 'backwards' first.
2932 */
2933 if (new_offset > rdev->data_offset &&
2934 !mddev->reshape_backwards)
2935 return -EINVAL;
2936
2937 if (mddev->pers && mddev->persistent &&
2938 !super_types[mddev->major_version]
2939 .allow_new_offset(rdev, new_offset))
2940 return -E2BIG;
2941 rdev->new_data_offset = new_offset;
2942 if (new_offset > rdev->data_offset)
2943 mddev->reshape_backwards = 1;
2944 else if (new_offset < rdev->data_offset)
2945 mddev->reshape_backwards = 0;
2946
2947 return len;
2948}
2949static struct rdev_sysfs_entry rdev_new_offset =
2950__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2951
NeilBrown83303b62006-01-06 00:21:06 -08002952static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002953rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002954{
Andre Nolldd8ac332009-03-31 14:33:13 +11002955 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002956}
2957
NeilBrownc5d79ad2008-02-06 01:39:54 -08002958static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2959{
2960 /* check if two start/length pairs overlap */
2961 if (s1+l1 <= s2)
2962 return 0;
2963 if (s2+l2 <= s1)
2964 return 0;
2965 return 1;
2966}
2967
Dan Williamsb522adc2009-03-31 15:00:31 +11002968static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2969{
2970 unsigned long long blocks;
2971 sector_t new;
2972
2973 if (strict_strtoull(buf, 10, &blocks) < 0)
2974 return -EINVAL;
2975
2976 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2977 return -EINVAL; /* sector conversion overflow */
2978
2979 new = blocks * 2;
2980 if (new != blocks * 2)
2981 return -EINVAL; /* unsigned long long to sector_t overflow */
2982
2983 *sectors = new;
2984 return 0;
2985}
2986
NeilBrown83303b62006-01-06 00:21:06 -08002987static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002988rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002989{
NeilBrownfd01b882011-10-11 16:47:53 +11002990 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002991 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002992 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002993
Dan Williamsb522adc2009-03-31 15:00:31 +11002994 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002995 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002996 if (rdev->data_offset != rdev->new_data_offset)
2997 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002998 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002999 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003000 sectors = super_types[my_mddev->major_version].
3001 rdev_size_change(rdev, sectors);
3002 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003003 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003004 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003005 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003006 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003007 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003008 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003009 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003010
Andre Nolldd8ac332009-03-31 14:33:13 +11003011 rdev->sectors = sectors;
3012 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 /* need to check that all other rdevs with the same ->bdev
3014 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003015 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016 * we have to change it back, we will have the lock again.
3017 */
NeilBrownfd01b882011-10-11 16:47:53 +11003018 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003019 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003020 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003021
NeilBrown27c529b2008-03-04 14:29:33 -08003022 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003023 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003024 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003025
3026 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003027 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003028 if (rdev->bdev == rdev2->bdev &&
3029 rdev != rdev2 &&
3030 overlaps(rdev->data_offset, rdev->sectors,
3031 rdev2->data_offset,
3032 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003033 overlap = 1;
3034 break;
3035 }
3036 mddev_unlock(mddev);
3037 if (overlap) {
3038 mddev_put(mddev);
3039 break;
3040 }
3041 }
NeilBrown27c529b2008-03-04 14:29:33 -08003042 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003043 if (overlap) {
3044 /* Someone else could have slipped in a size
3045 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003046 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003047 * safe, and trust userspace not to race with
3048 * itself
3049 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003050 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003051 return -EBUSY;
3052 }
3053 }
NeilBrown83303b62006-01-06 00:21:06 -08003054 return len;
3055}
3056
3057static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003058__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003059
Dan Williams06e3c812009-12-12 21:17:12 -07003060
NeilBrown3cb03002011-10-11 16:45:26 +11003061static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003062{
3063 unsigned long long recovery_start = rdev->recovery_offset;
3064
3065 if (test_bit(In_sync, &rdev->flags) ||
3066 recovery_start == MaxSector)
3067 return sprintf(page, "none\n");
3068
3069 return sprintf(page, "%llu\n", recovery_start);
3070}
3071
NeilBrown3cb03002011-10-11 16:45:26 +11003072static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003073{
3074 unsigned long long recovery_start;
3075
3076 if (cmd_match(buf, "none"))
3077 recovery_start = MaxSector;
3078 else if (strict_strtoull(buf, 10, &recovery_start))
3079 return -EINVAL;
3080
3081 if (rdev->mddev->pers &&
3082 rdev->raid_disk >= 0)
3083 return -EBUSY;
3084
3085 rdev->recovery_offset = recovery_start;
3086 if (recovery_start == MaxSector)
3087 set_bit(In_sync, &rdev->flags);
3088 else
3089 clear_bit(In_sync, &rdev->flags);
3090 return len;
3091}
3092
3093static struct rdev_sysfs_entry rdev_recovery_start =
3094__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3095
NeilBrown16c791a2011-07-28 11:31:47 +10003096
3097static ssize_t
3098badblocks_show(struct badblocks *bb, char *page, int unack);
3099static ssize_t
3100badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3101
NeilBrown3cb03002011-10-11 16:45:26 +11003102static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003103{
3104 return badblocks_show(&rdev->badblocks, page, 0);
3105}
NeilBrown3cb03002011-10-11 16:45:26 +11003106static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003107{
NeilBrownde393cd2011-07-28 11:31:48 +10003108 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3109 /* Maybe that ack was all we needed */
3110 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3111 wake_up(&rdev->blocked_wait);
3112 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003113}
3114static struct rdev_sysfs_entry rdev_bad_blocks =
3115__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3116
3117
NeilBrown3cb03002011-10-11 16:45:26 +11003118static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003119{
3120 return badblocks_show(&rdev->badblocks, page, 1);
3121}
NeilBrown3cb03002011-10-11 16:45:26 +11003122static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003123{
3124 return badblocks_store(&rdev->badblocks, page, len, 1);
3125}
3126static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3127__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3128
NeilBrown86e6ffd2005-11-08 21:39:24 -08003129static struct attribute *rdev_default_attrs[] = {
3130 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003131 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003132 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003133 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003134 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003135 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003136 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003137 &rdev_bad_blocks.attr,
3138 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003139 NULL,
3140};
3141static ssize_t
3142rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3143{
3144 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003145 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003146 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003147 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003148
3149 if (!entry->show)
3150 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003151
3152 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3153 if (!rv) {
3154 if (rdev->mddev == NULL)
3155 rv = -EBUSY;
3156 else
3157 rv = entry->show(rdev, page);
3158 mddev_unlock(mddev);
3159 }
3160 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003161}
3162
3163static ssize_t
3164rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3165 const char *page, size_t length)
3166{
3167 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003168 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003169 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003170 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003171
3172 if (!entry->store)
3173 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003174 if (!capable(CAP_SYS_ADMIN))
3175 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003176 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003177 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003178 if (rdev->mddev == NULL)
3179 rv = -EBUSY;
3180 else
3181 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003182 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003183 }
3184 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003185}
3186
3187static void rdev_free(struct kobject *ko)
3188{
NeilBrown3cb03002011-10-11 16:45:26 +11003189 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003190 kfree(rdev);
3191}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003192static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003193 .show = rdev_attr_show,
3194 .store = rdev_attr_store,
3195};
3196static struct kobj_type rdev_ktype = {
3197 .release = rdev_free,
3198 .sysfs_ops = &rdev_sysfs_ops,
3199 .default_attrs = rdev_default_attrs,
3200};
3201
NeilBrown3cb03002011-10-11 16:45:26 +11003202int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003203{
3204 rdev->desc_nr = -1;
3205 rdev->saved_raid_disk = -1;
3206 rdev->raid_disk = -1;
3207 rdev->flags = 0;
3208 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003209 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003210 rdev->sb_events = 0;
3211 rdev->last_read_error.tv_sec = 0;
3212 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003213 rdev->sb_loaded = 0;
3214 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003215 atomic_set(&rdev->nr_pending, 0);
3216 atomic_set(&rdev->read_errors, 0);
3217 atomic_set(&rdev->corrected_errors, 0);
3218
3219 INIT_LIST_HEAD(&rdev->same_set);
3220 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003221
3222 /* Add space to store bad block list.
3223 * This reserves the space even on arrays where it cannot
3224 * be used - I wonder if that matters
3225 */
3226 rdev->badblocks.count = 0;
3227 rdev->badblocks.shift = 0;
3228 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3229 seqlock_init(&rdev->badblocks.lock);
3230 if (rdev->badblocks.page == NULL)
3231 return -ENOMEM;
3232
3233 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003234}
3235EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236/*
3237 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3238 *
3239 * mark the device faulty if:
3240 *
3241 * - the device is nonexistent (zero size)
3242 * - the device has no valid superblock
3243 *
3244 * a faulty rdev _never_ has rdev->sb set.
3245 */
NeilBrown3cb03002011-10-11 16:45:26 +11003246static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 char b[BDEVNAME_SIZE];
3249 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003250 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 sector_t size;
3252
NeilBrown9ffae0c2006-01-06 00:20:32 -08003253 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (!rdev) {
3255 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3256 return ERR_PTR(-ENOMEM);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
NeilBrown2230dfe2011-07-28 11:31:46 +10003259 err = md_rdev_init(rdev);
3260 if (err)
3261 goto abort_free;
3262 err = alloc_disk_sb(rdev);
3263 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 goto abort_free;
3265
NeilBrownc5d79ad2008-02-06 01:39:54 -08003266 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (err)
3268 goto abort_free;
3269
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003270 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003271
Mike Snitzer77304d22010-11-08 14:39:12 +01003272 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (!size) {
3274 printk(KERN_WARNING
3275 "md: %s has zero or unknown size, marking faulty!\n",
3276 bdevname(rdev->bdev,b));
3277 err = -EINVAL;
3278 goto abort_free;
3279 }
3280
3281 if (super_format >= 0) {
3282 err = super_types[super_format].
3283 load_super(rdev, NULL, super_minor);
3284 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003285 printk(KERN_WARNING
3286 "md: %s does not have a valid v%d.%d "
3287 "superblock, not importing!\n",
3288 bdevname(rdev->bdev,b),
3289 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 goto abort_free;
3291 }
3292 if (err < 0) {
3293 printk(KERN_WARNING
3294 "md: could not read %s's sb, not importing!\n",
3295 bdevname(rdev->bdev,b));
3296 goto abort_free;
3297 }
3298 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003299 if (super_format == -1)
3300 /* hot-add for 0.90, or non-persistent: so no badblocks */
3301 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return rdev;
3304
3305abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003306 if (rdev->bdev)
3307 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003308 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 kfree(rdev);
3310 return ERR_PTR(err);
3311}
3312
3313/*
3314 * Check a full RAID array for plausibility
3315 */
3316
3317
NeilBrownfd01b882011-10-11 16:47:53 +11003318static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003321 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 char b[BDEVNAME_SIZE];
3323
3324 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003325 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 switch (super_types[mddev->major_version].
3327 load_super(rdev, freshest, mddev->minor_version)) {
3328 case 1:
3329 freshest = rdev;
3330 break;
3331 case 0:
3332 break;
3333 default:
3334 printk( KERN_ERR \
3335 "md: fatal superblock inconsistency in %s"
3336 " -- removing from array\n",
3337 bdevname(rdev->bdev,b));
3338 kick_rdev_from_array(rdev);
3339 }
3340
3341
3342 super_types[mddev->major_version].
3343 validate_super(mddev, freshest);
3344
3345 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003346 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003347 if (mddev->max_disks &&
3348 (rdev->desc_nr >= mddev->max_disks ||
3349 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003350 printk(KERN_WARNING
3351 "md: %s: %s: only %d devices permitted\n",
3352 mdname(mddev), bdevname(rdev->bdev, b),
3353 mddev->max_disks);
3354 kick_rdev_from_array(rdev);
3355 continue;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (rdev != freshest)
3358 if (super_types[mddev->major_version].
3359 validate_super(mddev, rdev)) {
3360 printk(KERN_WARNING "md: kicking non-fresh %s"
3361 " from array!\n",
3362 bdevname(rdev->bdev,b));
3363 kick_rdev_from_array(rdev);
3364 continue;
3365 }
3366 if (mddev->level == LEVEL_MULTIPATH) {
3367 rdev->desc_nr = i++;
3368 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003369 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003370 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003371 rdev->raid_disk = -1;
3372 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
NeilBrown72e02072009-12-14 12:49:55 +11003377/* Read a fixed-point number.
3378 * Numbers in sysfs attributes should be in "standard" units where
3379 * possible, so time should be in seconds.
3380 * However we internally use a a much smaller unit such as
3381 * milliseconds or jiffies.
3382 * This function takes a decimal number with a possible fractional
3383 * component, and produces an integer which is the result of
3384 * multiplying that number by 10^'scale'.
3385 * all without any floating-point arithmetic.
3386 */
3387int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3388{
3389 unsigned long result = 0;
3390 long decimals = -1;
3391 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3392 if (*cp == '.')
3393 decimals = 0;
3394 else if (decimals < scale) {
3395 unsigned int value;
3396 value = *cp - '0';
3397 result = result * 10 + value;
3398 if (decimals >= 0)
3399 decimals++;
3400 }
3401 cp++;
3402 }
3403 if (*cp == '\n')
3404 cp++;
3405 if (*cp)
3406 return -EINVAL;
3407 if (decimals < 0)
3408 decimals = 0;
3409 while (decimals < scale) {
3410 result *= 10;
3411 decimals ++;
3412 }
3413 *res = result;
3414 return 0;
3415}
3416
3417
NeilBrown19052c02008-08-05 15:54:13 +10003418static void md_safemode_timeout(unsigned long data);
3419
NeilBrowneae17012005-11-08 21:39:23 -08003420static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003421safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003422{
3423 int msec = (mddev->safemode_delay*1000)/HZ;
3424 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3425}
3426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003427safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003428{
NeilBrown16f17b32006-06-26 00:27:37 -07003429 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003430
NeilBrown72e02072009-12-14 12:49:55 +11003431 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003432 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003433 if (msec == 0)
3434 mddev->safemode_delay = 0;
3435 else {
NeilBrown19052c02008-08-05 15:54:13 +10003436 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003437 mddev->safemode_delay = (msec*HZ)/1000;
3438 if (mddev->safemode_delay == 0)
3439 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003440 if (mddev->safemode_delay < old_delay)
3441 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003442 }
3443 return len;
3444}
3445static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003446__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003447
3448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003449level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003450{
NeilBrown84fc4b52011-10-11 16:49:58 +11003451 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003452 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003453 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003454 else if (mddev->clevel[0])
3455 return sprintf(page, "%s\n", mddev->clevel);
3456 else if (mddev->level != LEVEL_NONE)
3457 return sprintf(page, "%d\n", mddev->level);
3458 else
3459 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003460}
3461
NeilBrownd9d166c2006-01-06 00:20:51 -08003462static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003463level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003464{
Dan Williamsf2859af2010-05-02 10:04:16 -07003465 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003466 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003467 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003468 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003469 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003470 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003471
3472 if (mddev->pers == NULL) {
3473 if (len == 0)
3474 return 0;
3475 if (len >= sizeof(mddev->clevel))
3476 return -ENOSPC;
3477 strncpy(mddev->clevel, buf, len);
3478 if (mddev->clevel[len-1] == '\n')
3479 len--;
3480 mddev->clevel[len] = 0;
3481 mddev->level = LEVEL_NONE;
3482 return rv;
3483 }
3484
3485 /* request to change the personality. Need to ensure:
3486 * - array is not engaged in resync/recovery/reshape
3487 * - old personality can be suspended
3488 * - new personality will access other array.
3489 */
3490
NeilBrownbb4f1e92010-08-08 21:18:03 +10003491 if (mddev->sync_thread ||
3492 mddev->reshape_position != MaxSector ||
3493 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003494 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003495
3496 if (!mddev->pers->quiesce) {
3497 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3498 mdname(mddev), mddev->pers->name);
3499 return -EINVAL;
3500 }
3501
3502 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003503 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003504 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 strncpy(clevel, buf, len);
3506 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003507 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003508 clevel[len] = 0;
3509 if (strict_strtol(clevel, 10, &level))
3510 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003511
Dan Williamsf2859af2010-05-02 10:04:16 -07003512 if (request_module("md-%s", clevel) != 0)
3513 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003514 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003515 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003516 if (!pers || !try_module_get(pers->owner)) {
3517 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003518 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003519 return -EINVAL;
3520 }
3521 spin_unlock(&pers_lock);
3522
3523 if (pers == mddev->pers) {
3524 /* Nothing to do! */
3525 module_put(pers->owner);
3526 return rv;
3527 }
3528 if (!pers->takeover) {
3529 module_put(pers->owner);
3530 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003531 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003532 return -EINVAL;
3533 }
3534
NeilBrowndafb20f2012-03-19 12:46:39 +11003535 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003536 rdev->new_raid_disk = rdev->raid_disk;
3537
NeilBrown245f46c2009-03-31 14:39:39 +11003538 /* ->takeover must set new_* and/or delta_disks
3539 * if it succeeds, and may set them when it fails.
3540 */
3541 priv = pers->takeover(mddev);
3542 if (IS_ERR(priv)) {
3543 mddev->new_level = mddev->level;
3544 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003545 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003546 mddev->raid_disks -= mddev->delta_disks;
3547 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003548 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003549 module_put(pers->owner);
3550 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003551 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003552 return PTR_ERR(priv);
3553 }
3554
3555 /* Looks like we have a winner */
3556 mddev_suspend(mddev);
3557 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003558
3559 if (mddev->pers->sync_request == NULL &&
3560 pers->sync_request != NULL) {
3561 /* need to add the md_redundancy_group */
3562 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3563 printk(KERN_WARNING
3564 "md: cannot register extra attributes for %s\n",
3565 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003566 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003567 }
3568 if (mddev->pers->sync_request != NULL &&
3569 pers->sync_request == NULL) {
3570 /* need to remove the md_redundancy_group */
3571 if (mddev->to_remove == NULL)
3572 mddev->to_remove = &md_redundancy_group;
3573 }
3574
Trela Maciej54071b32010-03-08 16:02:42 +11003575 if (mddev->pers->sync_request == NULL &&
3576 mddev->external) {
3577 /* We are converting from a no-redundancy array
3578 * to a redundancy array and metadata is managed
3579 * externally so we need to be sure that writes
3580 * won't block due to a need to transition
3581 * clean->dirty
3582 * until external management is started.
3583 */
3584 mddev->in_sync = 0;
3585 mddev->safemode_delay = 0;
3586 mddev->safemode = 0;
3587 }
3588
NeilBrowndafb20f2012-03-19 12:46:39 +11003589 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 if (rdev->raid_disk < 0)
3591 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003592 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003593 rdev->new_raid_disk = -1;
3594 if (rdev->new_raid_disk == rdev->raid_disk)
3595 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003596 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003597 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003598 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003599 if (rdev->raid_disk < 0)
3600 continue;
3601 if (rdev->new_raid_disk == rdev->raid_disk)
3602 continue;
3603 rdev->raid_disk = rdev->new_raid_disk;
3604 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003605 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003606 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003607 if (sysfs_link_rdev(mddev, rdev))
3608 printk(KERN_WARNING "md: cannot register rd%d"
3609 " for %s after level change\n",
3610 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003611 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003612 }
3613
3614 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003615 mddev->pers = pers;
3616 mddev->private = priv;
3617 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3618 mddev->level = mddev->new_level;
3619 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003620 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003621 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003622 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003623 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003624 if (mddev->pers->sync_request == NULL) {
3625 /* this is now an array without redundancy, so
3626 * it must always be in_sync
3627 */
3628 mddev->in_sync = 1;
3629 del_timer_sync(&mddev->safemode_timer);
3630 }
NeilBrown245f46c2009-03-31 14:39:39 +11003631 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003632 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003633 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003634 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003635 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003636 return rv;
3637}
3638
3639static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003640__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003641
NeilBrownd4dbd022006-06-26 00:27:59 -07003642
3643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003644layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003645{
3646 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003647 if (mddev->reshape_position != MaxSector &&
3648 mddev->layout != mddev->new_layout)
3649 return sprintf(page, "%d (%d)\n",
3650 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003651 return sprintf(page, "%d\n", mddev->layout);
3652}
3653
3654static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003655layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003656{
3657 char *e;
3658 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003659
3660 if (!*buf || (*e && *e != '\n'))
3661 return -EINVAL;
3662
NeilBrownb3546032009-03-31 14:56:41 +11003663 if (mddev->pers) {
3664 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003665 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003666 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003667 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003668 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003669 if (err) {
3670 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003671 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003672 }
NeilBrownb3546032009-03-31 14:56:41 +11003673 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003674 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003675 if (mddev->reshape_position == MaxSector)
3676 mddev->layout = n;
3677 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003678 return len;
3679}
3680static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003681__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003682
3683
NeilBrowneae17012005-11-08 21:39:23 -08003684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003685raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003686{
NeilBrownbb636542005-11-08 21:39:45 -08003687 if (mddev->raid_disks == 0)
3688 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003689 if (mddev->reshape_position != MaxSector &&
3690 mddev->delta_disks != 0)
3691 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3692 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003693 return sprintf(page, "%d\n", mddev->raid_disks);
3694}
3695
NeilBrownfd01b882011-10-11 16:47:53 +11003696static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003697
3698static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003699raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003700{
NeilBrownda943b992006-01-06 00:20:54 -08003701 char *e;
3702 int rv = 0;
3703 unsigned long n = simple_strtoul(buf, &e, 10);
3704
3705 if (!*buf || (*e && *e != '\n'))
3706 return -EINVAL;
3707
3708 if (mddev->pers)
3709 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003710 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003711 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003712 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003713
3714 rdev_for_each(rdev, mddev) {
3715 if (olddisks < n &&
3716 rdev->data_offset < rdev->new_data_offset)
3717 return -EINVAL;
3718 if (olddisks > n &&
3719 rdev->data_offset > rdev->new_data_offset)
3720 return -EINVAL;
3721 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 mddev->delta_disks = n - olddisks;
3723 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003724 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003725 } else
NeilBrownda943b992006-01-06 00:20:54 -08003726 mddev->raid_disks = n;
3727 return rv ? rv : len;
3728}
3729static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003730__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003731
NeilBrown24dd4692005-11-08 21:39:26 -08003732static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003733chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003734{
NeilBrown08a02ec2007-05-09 02:35:38 -07003735 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003736 mddev->chunk_sectors != mddev->new_chunk_sectors)
3737 return sprintf(page, "%d (%d)\n",
3738 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003739 mddev->chunk_sectors << 9);
3740 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003741}
3742
3743static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003744chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003745{
NeilBrown3b343802006-01-06 00:20:47 -08003746 char *e;
3747 unsigned long n = simple_strtoul(buf, &e, 10);
3748
NeilBrown3b343802006-01-06 00:20:47 -08003749 if (!*buf || (*e && *e != '\n'))
3750 return -EINVAL;
3751
NeilBrownb3546032009-03-31 14:56:41 +11003752 if (mddev->pers) {
3753 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003754 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003755 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003756 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003757 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003758 if (err) {
3759 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003760 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003761 }
NeilBrownb3546032009-03-31 14:56:41 +11003762 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003763 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003764 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003765 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003766 }
NeilBrown3b343802006-01-06 00:20:47 -08003767 return len;
3768}
3769static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003770__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003771
NeilBrowna94213b2006-06-26 00:28:00 -07003772static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003773resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003774{
NeilBrownd1a7c502009-03-31 15:24:32 +11003775 if (mddev->recovery_cp == MaxSector)
3776 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003777 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3778}
3779
3780static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003781resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003782{
NeilBrowna94213b2006-06-26 00:28:00 -07003783 char *e;
3784 unsigned long long n = simple_strtoull(buf, &e, 10);
3785
NeilBrownb0986362011-05-11 15:52:21 +10003786 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003787 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003788 if (cmd_match(buf, "none"))
3789 n = MaxSector;
3790 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003791 return -EINVAL;
3792
3793 mddev->recovery_cp = n;
3794 return len;
3795}
3796static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003797__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003798
NeilBrown9e653b62006-06-26 00:27:58 -07003799/*
3800 * The array state can be:
3801 *
3802 * clear
3803 * No devices, no size, no level
3804 * Equivalent to STOP_ARRAY ioctl
3805 * inactive
3806 * May have some settings, but array is not active
3807 * all IO results in error
3808 * When written, doesn't tear down array, but just stops it
3809 * suspended (not supported yet)
3810 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003811 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003812 * readonly
3813 * no resync can happen. no superblocks get written.
3814 * write requests fail
3815 * read-auto
3816 * like readonly, but behaves like 'clean' on a write request.
3817 *
3818 * clean - no pending writes, but otherwise active.
3819 * When written to inactive array, starts without resync
3820 * If a write request arrives then
3821 * if metadata is known, mark 'dirty' and switch to 'active'.
3822 * if not known, block and switch to write-pending
3823 * If written to an active array that has pending writes, then fails.
3824 * active
3825 * fully active: IO and resync can be happening.
3826 * When written to inactive array, starts with resync
3827 *
3828 * write-pending
3829 * clean, but writes are blocked waiting for 'active' to be written.
3830 *
3831 * active-idle
3832 * like active, but no writes have been seen for a while (100msec).
3833 *
3834 */
3835enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3836 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003837static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003838 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3839 "write-pending", "active-idle", NULL };
3840
3841static int match_word(const char *word, char **list)
3842{
3843 int n;
3844 for (n=0; list[n]; n++)
3845 if (cmd_match(word, list[n]))
3846 break;
3847 return n;
3848}
3849
3850static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003851array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003852{
3853 enum array_state st = inactive;
3854
3855 if (mddev->pers)
3856 switch(mddev->ro) {
3857 case 1:
3858 st = readonly;
3859 break;
3860 case 2:
3861 st = read_auto;
3862 break;
3863 case 0:
3864 if (mddev->in_sync)
3865 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003866 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003867 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003868 else if (mddev->safemode)
3869 st = active_idle;
3870 else
3871 st = active;
3872 }
3873 else {
3874 if (list_empty(&mddev->disks) &&
3875 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003876 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003877 st = clear;
3878 else
3879 st = inactive;
3880 }
3881 return sprintf(page, "%s\n", array_states[st]);
3882}
3883
NeilBrowna05b7ea02012-07-19 15:59:18 +10003884static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3885static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003886static int do_md_run(struct mddev * mddev);
3887static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003888
3889static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003890array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003891{
3892 int err = -EINVAL;
3893 enum array_state st = match_word(buf, array_states);
3894 switch(st) {
3895 case bad_word:
3896 break;
3897 case clear:
3898 /* stopping an active array */
NeilBrowna05b7ea02012-07-19 15:59:18 +10003899 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003900 break;
3901 case inactive:
3902 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10003903 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003904 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10003905 else
NeilBrowne6910632008-02-06 01:39:51 -08003906 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003907 break;
3908 case suspended:
3909 break; /* not supported yet */
3910 case readonly:
3911 if (mddev->pers)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003912 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003913 else {
3914 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003915 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003916 err = do_md_run(mddev);
3917 }
3918 break;
3919 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003920 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003921 if (mddev->ro == 0)
NeilBrowna05b7ea02012-07-19 15:59:18 +10003922 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003923 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003924 err = restart_array(mddev);
3925 if (err == 0) {
3926 mddev->ro = 2;
3927 set_disk_ro(mddev->gendisk, 0);
3928 }
NeilBrown9e653b62006-06-26 00:27:58 -07003929 } else {
3930 mddev->ro = 2;
3931 err = do_md_run(mddev);
3932 }
3933 break;
3934 case clean:
3935 if (mddev->pers) {
3936 restart_array(mddev);
3937 spin_lock_irq(&mddev->write_lock);
3938 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003939 if (mddev->in_sync == 0) {
3940 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003941 if (mddev->safemode == 1)
3942 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003943 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003944 }
3945 err = 0;
3946 } else
3947 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003948 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003949 } else
3950 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003951 break;
3952 case active:
3953 if (mddev->pers) {
3954 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003955 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003956 wake_up(&mddev->sb_wait);
3957 err = 0;
3958 } else {
3959 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003960 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003961 err = do_md_run(mddev);
3962 }
3963 break;
3964 case write_pending:
3965 case active_idle:
3966 /* these cannot be set */
3967 break;
3968 }
3969 if (err)
3970 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003971 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003972 if (mddev->hold_active == UNTIL_IOCTL)
3973 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003974 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003975 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003976 }
NeilBrown9e653b62006-06-26 00:27:58 -07003977}
NeilBrown80ca3a42006-07-10 04:44:18 -07003978static struct md_sysfs_entry md_array_state =
3979__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003980
NeilBrown6d7ff732006-01-06 00:21:16 -08003981static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003982max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003983 return sprintf(page, "%d\n",
3984 atomic_read(&mddev->max_corr_read_errors));
3985}
3986
3987static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003988max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003989{
3990 char *e;
3991 unsigned long n = simple_strtoul(buf, &e, 10);
3992
3993 if (*buf && (*e == 0 || *e == '\n')) {
3994 atomic_set(&mddev->max_corr_read_errors, n);
3995 return len;
3996 }
3997 return -EINVAL;
3998}
3999
4000static struct md_sysfs_entry max_corr_read_errors =
4001__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4002 max_corrected_read_errors_store);
4003
4004static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004005null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08004006{
4007 return -EINVAL;
4008}
4009
4010static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004011new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08004012{
4013 /* buf must be %d:%d\n? giving major and minor numbers */
4014 /* The new device is added to the array.
4015 * If the array has a persistent superblock, we read the
4016 * superblock to initialise info and check validity.
4017 * Otherwise, only checking done is that in bind_rdev_to_array,
4018 * which mainly checks size.
4019 */
4020 char *e;
4021 int major = simple_strtoul(buf, &e, 10);
4022 int minor;
4023 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004024 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08004025 int err;
4026
4027 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4028 return -EINVAL;
4029 minor = simple_strtoul(e+1, &e, 10);
4030 if (*e && *e != '\n')
4031 return -EINVAL;
4032 dev = MKDEV(major, minor);
4033 if (major != MAJOR(dev) ||
4034 minor != MINOR(dev))
4035 return -EOVERFLOW;
4036
4037
4038 if (mddev->persistent) {
4039 rdev = md_import_device(dev, mddev->major_version,
4040 mddev->minor_version);
4041 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004042 struct md_rdev *rdev0
4043 = list_entry(mddev->disks.next,
4044 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08004045 err = super_types[mddev->major_version]
4046 .load_super(rdev, rdev0, mddev->minor_version);
4047 if (err < 0)
4048 goto out;
4049 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004050 } else if (mddev->external)
4051 rdev = md_import_device(dev, -2, -1);
4052 else
NeilBrown6d7ff732006-01-06 00:21:16 -08004053 rdev = md_import_device(dev, -1, -1);
4054
4055 if (IS_ERR(rdev))
4056 return PTR_ERR(rdev);
4057 err = bind_rdev_to_array(rdev, mddev);
4058 out:
4059 if (err)
4060 export_rdev(rdev);
4061 return err ? err : len;
4062}
4063
4064static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004065__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004066
4067static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004068bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004069{
4070 char *end;
4071 unsigned long chunk, end_chunk;
4072
4073 if (!mddev->bitmap)
4074 goto out;
4075 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4076 while (*buf) {
4077 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4078 if (buf == end) break;
4079 if (*end == '-') { /* range */
4080 buf = end + 1;
4081 end_chunk = simple_strtoul(buf, &end, 0);
4082 if (buf == end) break;
4083 }
4084 if (*end && !isspace(*end)) break;
4085 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004086 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004087 }
4088 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4089out:
4090 return len;
4091}
4092
4093static struct md_sysfs_entry md_bitmap =
4094__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4095
4096static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004097size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004098{
Andre Noll58c0fed2009-03-31 14:33:13 +11004099 return sprintf(page, "%llu\n",
4100 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004101}
4102
NeilBrownfd01b882011-10-11 16:47:53 +11004103static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004104
4105static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004106size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004107{
4108 /* If array is inactive, we can reduce the component size, but
4109 * not increase it (except from 0).
4110 * If array is active, we can try an on-line resize
4111 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004112 sector_t sectors;
4113 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004114
Andre Noll58c0fed2009-03-31 14:33:13 +11004115 if (err < 0)
4116 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004117 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004118 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07004119 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004120 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004121 if (mddev->dev_sectors == 0 ||
4122 mddev->dev_sectors > sectors)
4123 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004124 else
4125 err = -ENOSPC;
4126 }
4127 return err ? err : len;
4128}
4129
4130static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004131__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004132
NeilBrown8bb93aa2006-01-06 00:20:50 -08004133
4134/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004135 * This is one of
4136 * 'none' for arrays with no metadata (good luck...)
4137 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004138 * or N.M for internally known formats
4139 */
4140static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004141metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004142{
4143 if (mddev->persistent)
4144 return sprintf(page, "%d.%d\n",
4145 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004146 else if (mddev->external)
4147 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004148 else
4149 return sprintf(page, "none\n");
4150}
4151
4152static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004153metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004154{
4155 int major, minor;
4156 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004157 /* Changing the details of 'external' metadata is
4158 * always permitted. Otherwise there must be
4159 * no devices attached to the array.
4160 */
4161 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4162 ;
4163 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004164 return -EBUSY;
4165
4166 if (cmd_match(buf, "none")) {
4167 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004168 mddev->external = 0;
4169 mddev->major_version = 0;
4170 mddev->minor_version = 90;
4171 return len;
4172 }
4173 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004174 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004175 if (namelen >= sizeof(mddev->metadata_type))
4176 namelen = sizeof(mddev->metadata_type)-1;
4177 strncpy(mddev->metadata_type, buf+9, namelen);
4178 mddev->metadata_type[namelen] = 0;
4179 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4180 mddev->metadata_type[--namelen] = 0;
4181 mddev->persistent = 0;
4182 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004183 mddev->major_version = 0;
4184 mddev->minor_version = 90;
4185 return len;
4186 }
4187 major = simple_strtoul(buf, &e, 10);
4188 if (e==buf || *e != '.')
4189 return -EINVAL;
4190 buf = e+1;
4191 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004192 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004193 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004194 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004195 return -ENOENT;
4196 mddev->major_version = major;
4197 mddev->minor_version = minor;
4198 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004199 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004200 return len;
4201}
4202
4203static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004204__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004205
NeilBrowna35b0d62006-01-06 00:20:49 -08004206static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004207action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004208{
NeilBrown7eec3142005-11-08 21:39:44 -08004209 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004210 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4211 type = "frozen";
4212 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004213 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004214 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4215 type = "reshape";
4216 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004217 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4218 type = "resync";
4219 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4220 type = "check";
4221 else
4222 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004223 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004224 type = "recover";
4225 }
4226 return sprintf(page, "%s\n", type);
4227}
4228
NeilBrownfd01b882011-10-11 16:47:53 +11004229static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004230
NeilBrown24dd4692005-11-08 21:39:26 -08004231static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004232action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004233{
NeilBrown7eec3142005-11-08 21:39:44 -08004234 if (!mddev->pers || !mddev->pers->sync_request)
4235 return -EINVAL;
4236
NeilBrownb6a9ce62009-05-26 09:41:17 +10004237 if (cmd_match(page, "frozen"))
4238 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4239 else
4240 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4241
4242 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004243 if (mddev->sync_thread) {
4244 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004245 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004246 }
NeilBrown03c902e2006-01-06 00:20:46 -08004247 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4248 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004249 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004250 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004251 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004252 else if (cmd_match(page, "recover")) {
4253 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4254 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4255 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004256 int err;
4257 if (mddev->pers->start_reshape == NULL)
4258 return -EINVAL;
4259 err = mddev->pers->start_reshape(mddev);
4260 if (err)
4261 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004262 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004263 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004264 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004265 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004266 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004267 return -EINVAL;
4268 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4269 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004270 }
NeilBrown03c902e2006-01-06 00:20:46 -08004271 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004272 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004273 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004274 return len;
4275}
4276
NeilBrown9d888832005-11-08 21:39:26 -08004277static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004278mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004279{
4280 return sprintf(page, "%llu\n",
4281 (unsigned long long) mddev->resync_mismatches);
4282}
4283
NeilBrown80ca3a42006-07-10 04:44:18 -07004284static struct md_sysfs_entry md_scan_mode =
4285__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004286
NeilBrown96de1e62005-11-08 21:39:39 -08004287
NeilBrown80ca3a42006-07-10 04:44:18 -07004288static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004289
NeilBrown88202a02006-01-06 00:21:36 -08004290static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004291sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004292{
4293 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4294 mddev->sync_speed_min ? "local": "system");
4295}
4296
4297static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004298sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004299{
4300 int min;
4301 char *e;
4302 if (strncmp(buf, "system", 6)==0) {
4303 mddev->sync_speed_min = 0;
4304 return len;
4305 }
4306 min = simple_strtoul(buf, &e, 10);
4307 if (buf == e || (*e && *e != '\n') || min <= 0)
4308 return -EINVAL;
4309 mddev->sync_speed_min = min;
4310 return len;
4311}
4312
4313static struct md_sysfs_entry md_sync_min =
4314__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4315
4316static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004317sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004318{
4319 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4320 mddev->sync_speed_max ? "local": "system");
4321}
4322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004325{
4326 int max;
4327 char *e;
4328 if (strncmp(buf, "system", 6)==0) {
4329 mddev->sync_speed_max = 0;
4330 return len;
4331 }
4332 max = simple_strtoul(buf, &e, 10);
4333 if (buf == e || (*e && *e != '\n') || max <= 0)
4334 return -EINVAL;
4335 mddev->sync_speed_max = max;
4336 return len;
4337}
4338
4339static struct md_sysfs_entry md_sync_max =
4340__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4341
Iustin Popd7f3d292007-10-16 23:30:54 -07004342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004344{
4345 return sprintf(page, "%d\n", mddev->degraded);
4346}
4347static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004348
4349static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004350sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004351{
4352 return sprintf(page, "%d\n", mddev->parallel_resync);
4353}
4354
4355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004357{
4358 long n;
4359
4360 if (strict_strtol(buf, 10, &n))
4361 return -EINVAL;
4362
4363 if (n != 0 && n != 1)
4364 return -EINVAL;
4365
4366 mddev->parallel_resync = n;
4367
4368 if (mddev->sync_thread)
4369 wake_up(&resync_wait);
4370
4371 return len;
4372}
4373
4374/* force parallel resync, even with shared block devices */
4375static struct md_sysfs_entry md_sync_force_parallel =
4376__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4377 sync_force_parallel_show, sync_force_parallel_store);
4378
4379static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004380sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004381{
4382 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004383 if (mddev->curr_resync == 0)
4384 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004385 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4386 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004387 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004388 db = resync - mddev->resync_mark_cnt;
4389 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004390}
4391
NeilBrown80ca3a42006-07-10 04:44:18 -07004392static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004393
4394static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004395sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004396{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004397 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004398
NeilBrownacb180b2009-04-14 16:28:34 +10004399 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4400 return sprintf(page, "none\n");
4401
NeilBrownc804cde2012-05-21 09:28:33 +10004402 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4403 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004404 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004405 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004406 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004407
NeilBrownacb180b2009-04-14 16:28:34 +10004408 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004409 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004410}
4411
NeilBrown80ca3a42006-07-10 04:44:18 -07004412static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004413
NeilBrowne464eaf2006-03-27 01:18:14 -08004414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004416{
4417 return sprintf(page, "%llu\n",
4418 (unsigned long long)mddev->resync_min);
4419}
4420static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004421min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004422{
4423 unsigned long long min;
4424 if (strict_strtoull(buf, 10, &min))
4425 return -EINVAL;
4426 if (min > mddev->resync_max)
4427 return -EINVAL;
4428 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4429 return -EBUSY;
4430
4431 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004432 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004433 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004434 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004435 return -EINVAL;
4436 }
4437 mddev->resync_min = min;
4438
4439 return len;
4440}
4441
4442static struct md_sysfs_entry md_min_sync =
4443__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4444
4445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004446max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004447{
4448 if (mddev->resync_max == MaxSector)
4449 return sprintf(page, "max\n");
4450 else
4451 return sprintf(page, "%llu\n",
4452 (unsigned long long)mddev->resync_max);
4453}
4454static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004455max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004456{
4457 if (strncmp(buf, "max", 3) == 0)
4458 mddev->resync_max = MaxSector;
4459 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004460 unsigned long long max;
4461 if (strict_strtoull(buf, 10, &max))
4462 return -EINVAL;
4463 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004464 return -EINVAL;
4465 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004466 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004467 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4468 return -EBUSY;
4469
4470 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004471 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004472 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004473 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004474 return -EINVAL;
4475 }
4476 mddev->resync_max = max;
4477 }
4478 wake_up(&mddev->recovery_wait);
4479 return len;
4480}
4481
4482static struct md_sysfs_entry md_max_sync =
4483__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4484
4485static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004486suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004487{
4488 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4489}
4490
4491static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004492suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004493{
4494 char *e;
4495 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004496 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004497
NeilBrownb8d966e2009-07-01 11:14:04 +10004498 if (mddev->pers == NULL ||
4499 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004500 return -EINVAL;
4501 if (buf == e || (*e && *e != '\n'))
4502 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004503
4504 mddev->suspend_lo = new;
4505 if (new >= old)
4506 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004507 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004508 else {
4509 /* Expanding suspended region - need to wait */
4510 mddev->pers->quiesce(mddev, 1);
4511 mddev->pers->quiesce(mddev, 0);
4512 }
4513 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004514}
4515static struct md_sysfs_entry md_suspend_lo =
4516__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4517
4518
4519static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004520suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004521{
4522 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4523}
4524
4525static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004526suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004527{
4528 char *e;
4529 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004530 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004531
NeilBrownb8d966e2009-07-01 11:14:04 +10004532 if (mddev->pers == NULL ||
4533 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004534 return -EINVAL;
4535 if (buf == e || (*e && *e != '\n'))
4536 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004537
4538 mddev->suspend_hi = new;
4539 if (new <= old)
4540 /* Shrinking suspended region */
4541 mddev->pers->quiesce(mddev, 2);
4542 else {
4543 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004544 mddev->pers->quiesce(mddev, 1);
4545 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004546 }
4547 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004548}
4549static struct md_sysfs_entry md_suspend_hi =
4550__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4551
NeilBrown08a02ec2007-05-09 02:35:38 -07004552static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004553reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004554{
4555 if (mddev->reshape_position != MaxSector)
4556 return sprintf(page, "%llu\n",
4557 (unsigned long long)mddev->reshape_position);
4558 strcpy(page, "none\n");
4559 return 5;
4560}
4561
4562static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004563reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004564{
NeilBrownc6563a82012-05-21 09:27:00 +10004565 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004566 char *e;
4567 unsigned long long new = simple_strtoull(buf, &e, 10);
4568 if (mddev->pers)
4569 return -EBUSY;
4570 if (buf == e || (*e && *e != '\n'))
4571 return -EINVAL;
4572 mddev->reshape_position = new;
4573 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004574 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004575 mddev->new_level = mddev->level;
4576 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004577 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004578 rdev_for_each(rdev, mddev)
4579 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004580 return len;
4581}
4582
4583static struct md_sysfs_entry md_reshape_position =
4584__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4585 reshape_position_store);
4586
Dan Williamsb522adc2009-03-31 15:00:31 +11004587static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004588reshape_direction_show(struct mddev *mddev, char *page)
4589{
4590 return sprintf(page, "%s\n",
4591 mddev->reshape_backwards ? "backwards" : "forwards");
4592}
4593
4594static ssize_t
4595reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4596{
4597 int backwards = 0;
4598 if (cmd_match(buf, "forwards"))
4599 backwards = 0;
4600 else if (cmd_match(buf, "backwards"))
4601 backwards = 1;
4602 else
4603 return -EINVAL;
4604 if (mddev->reshape_backwards == backwards)
4605 return len;
4606
4607 /* check if we are allowed to change */
4608 if (mddev->delta_disks)
4609 return -EBUSY;
4610
4611 if (mddev->persistent &&
4612 mddev->major_version == 0)
4613 return -EINVAL;
4614
4615 mddev->reshape_backwards = backwards;
4616 return len;
4617}
4618
4619static struct md_sysfs_entry md_reshape_direction =
4620__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4621 reshape_direction_store);
4622
4623static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004624array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004625{
4626 if (mddev->external_size)
4627 return sprintf(page, "%llu\n",
4628 (unsigned long long)mddev->array_sectors/2);
4629 else
4630 return sprintf(page, "default\n");
4631}
4632
4633static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004634array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004635{
4636 sector_t sectors;
4637
4638 if (strncmp(buf, "default", 7) == 0) {
4639 if (mddev->pers)
4640 sectors = mddev->pers->size(mddev, 0, 0);
4641 else
4642 sectors = mddev->array_sectors;
4643
4644 mddev->external_size = 0;
4645 } else {
4646 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4647 return -EINVAL;
4648 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004649 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004650
4651 mddev->external_size = 1;
4652 }
4653
4654 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004655 if (mddev->pers) {
4656 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004657 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004658 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004659 return len;
4660}
4661
4662static struct md_sysfs_entry md_array_size =
4663__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4664 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004665
NeilBrowneae17012005-11-08 21:39:23 -08004666static struct attribute *md_default_attrs[] = {
4667 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004668 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004669 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004670 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004671 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004672 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004673 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004674 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004675 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004676 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004677 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004678 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004679 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004680 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004681 NULL,
4682};
4683
4684static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004685 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004686 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004687 &md_sync_min.attr,
4688 &md_sync_max.attr,
4689 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004690 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004691 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004692 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004693 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004694 &md_suspend_lo.attr,
4695 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004696 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004697 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004698 NULL,
4699};
NeilBrown411036f2005-11-08 21:39:40 -08004700static struct attribute_group md_redundancy_group = {
4701 .name = NULL,
4702 .attrs = md_redundancy_attrs,
4703};
4704
NeilBrowneae17012005-11-08 21:39:23 -08004705
4706static ssize_t
4707md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4708{
4709 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004710 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004711 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004712
4713 if (!entry->show)
4714 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004715 spin_lock(&all_mddevs_lock);
4716 if (list_empty(&mddev->all_mddevs)) {
4717 spin_unlock(&all_mddevs_lock);
4718 return -EBUSY;
4719 }
4720 mddev_get(mddev);
4721 spin_unlock(&all_mddevs_lock);
4722
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004723 rv = mddev_lock(mddev);
4724 if (!rv) {
4725 rv = entry->show(mddev, page);
4726 mddev_unlock(mddev);
4727 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004728 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004729 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004730}
4731
4732static ssize_t
4733md_attr_store(struct kobject *kobj, struct attribute *attr,
4734 const char *page, size_t length)
4735{
4736 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004737 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004738 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004739
4740 if (!entry->store)
4741 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004742 if (!capable(CAP_SYS_ADMIN))
4743 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004744 spin_lock(&all_mddevs_lock);
4745 if (list_empty(&mddev->all_mddevs)) {
4746 spin_unlock(&all_mddevs_lock);
4747 return -EBUSY;
4748 }
4749 mddev_get(mddev);
4750 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004751 rv = mddev_lock(mddev);
4752 if (!rv) {
4753 rv = entry->store(mddev, page, length);
4754 mddev_unlock(mddev);
4755 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004756 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004757 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004758}
4759
4760static void md_free(struct kobject *ko)
4761{
NeilBrownfd01b882011-10-11 16:47:53 +11004762 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004763
4764 if (mddev->sysfs_state)
4765 sysfs_put(mddev->sysfs_state);
4766
4767 if (mddev->gendisk) {
4768 del_gendisk(mddev->gendisk);
4769 put_disk(mddev->gendisk);
4770 }
4771 if (mddev->queue)
4772 blk_cleanup_queue(mddev->queue);
4773
NeilBrowneae17012005-11-08 21:39:23 -08004774 kfree(mddev);
4775}
4776
Emese Revfy52cf25d2010-01-19 02:58:23 +01004777static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004778 .show = md_attr_show,
4779 .store = md_attr_store,
4780};
4781static struct kobj_type md_ktype = {
4782 .release = md_free,
4783 .sysfs_ops = &md_sysfs_ops,
4784 .default_attrs = md_default_attrs,
4785};
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787int mdp_major = 0;
4788
Dan Williams5fd3a172009-03-04 00:57:25 -07004789static void mddev_delayed_delete(struct work_struct *ws)
4790{
NeilBrownfd01b882011-10-11 16:47:53 +11004791 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004792
NeilBrown43a70502009-12-14 12:49:55 +11004793 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004794 kobject_del(&mddev->kobj);
4795 kobject_put(&mddev->kobj);
4796}
4797
NeilBrownefeb53c2009-01-09 08:31:10 +11004798static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004800 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004801 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004803 int partitioned;
4804 int shift;
4805 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004806 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004809 return -ENODEV;
4810
4811 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4812 shift = partitioned ? MdpMinorShift : 0;
4813 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Tejun Heoe804ac72010-10-15 15:36:08 +02004815 /* wait for any previous instance of this device to be
4816 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004817 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004818 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004819
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004820 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004821 error = -EEXIST;
4822 if (mddev->gendisk)
4823 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004824
4825 if (name) {
4826 /* Need to ensure that 'name' is not a duplicate.
4827 */
NeilBrownfd01b882011-10-11 16:47:53 +11004828 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004829 spin_lock(&all_mddevs_lock);
4830
4831 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4832 if (mddev2->gendisk &&
4833 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4834 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004835 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004836 }
4837 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 }
NeilBrown8b765392009-01-09 08:31:08 +11004839
NeilBrown0909dc42009-07-01 12:27:21 +10004840 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004841 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004842 if (!mddev->queue)
4843 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004844 mddev->queue->queuedata = mddev;
4845
NeilBrown409c57f2009-03-31 14:39:39 +11004846 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004847 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 disk = alloc_disk(1 << shift);
4850 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004851 blk_cleanup_queue(mddev->queue);
4852 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004853 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004855 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004857 if (name)
4858 strcpy(disk->disk_name, name);
4859 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 disk->fops = &md_fops;
4864 disk->private_data = mddev;
4865 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004866 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004867 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004868 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004869 * remove it now.
4870 */
4871 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004873 /* As soon as we call add_disk(), another thread could get
4874 * through to md_open, so make sure it doesn't get too far
4875 */
4876 mutex_lock(&mddev->open_mutex);
4877 add_disk(disk);
4878
Tejun Heoed9e1982008-08-25 19:56:05 +09004879 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4880 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004881 if (error) {
4882 /* This isn't possible, but as kobject_init_and_add is marked
4883 * __must_check, we must do something with the result
4884 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004885 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4886 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004887 error = 0;
4888 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004889 if (mddev->kobj.sd &&
4890 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004891 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004892 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004893 abort:
4894 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004895 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004896 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004897 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004898 }
NeilBrownd3374822009-01-09 08:31:10 +11004899 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004900 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004901}
4902
4903static struct kobject *md_probe(dev_t dev, int *part, void *data)
4904{
4905 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return NULL;
4907}
4908
NeilBrownefeb53c2009-01-09 08:31:10 +11004909static int add_named_array(const char *val, struct kernel_param *kp)
4910{
4911 /* val must be "md_*" where * is not all digits.
4912 * We allocate an array with a large free minor number, and
4913 * set the name to val. val must not already be an active name.
4914 */
4915 int len = strlen(val);
4916 char buf[DISK_NAME_LEN];
4917
4918 while (len && val[len-1] == '\n')
4919 len--;
4920 if (len >= DISK_NAME_LEN)
4921 return -E2BIG;
4922 strlcpy(buf, val, len+1);
4923 if (strncmp(buf, "md_", 3) != 0)
4924 return -EINVAL;
4925 return md_alloc(0, buf);
4926}
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928static void md_safemode_timeout(unsigned long data)
4929{
NeilBrownfd01b882011-10-11 16:47:53 +11004930 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Neil Brown0fd62b82008-06-28 08:31:36 +10004932 if (!atomic_read(&mddev->writes_pending)) {
4933 mddev->safemode = 1;
4934 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004935 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 md_wakeup_thread(mddev->thread);
4938}
4939
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004940static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
NeilBrownfd01b882011-10-11 16:47:53 +11004942int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
NeilBrown2604b702006-01-06 00:20:36 -08004944 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004945 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004946 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
NeilBrowna757e642005-04-16 15:26:42 -07004948 if (list_empty(&mddev->disks))
4949 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 if (mddev->pers)
4953 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004954 /* Cannot run until previous stop completes properly */
4955 if (mddev->sysfs_active)
4956 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 /*
4959 * Analyze all RAID superblock(s)
4960 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004961 if (!mddev->raid_disks) {
4962 if (!mddev->persistent)
4963 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004964 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
NeilBrownd9d166c2006-01-06 00:20:51 -08004967 if (mddev->level != LEVEL_NONE)
4968 request_module("md-level-%d", mddev->level);
4969 else if (mddev->clevel[0])
4970 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 /*
4973 * Drop all container device buffers, from now on
4974 * the only valid external interface is through the md
4975 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004977 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004978 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 continue;
4980 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004981 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004982
4983 /* perform some consistency tests on the device.
4984 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004985 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004986 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004987 if (rdev->meta_bdev) {
4988 /* Nothing to check */;
4989 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004990 if (mddev->dev_sectors &&
4991 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004992 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004993 printk("md: %s: data overlaps metadata\n",
4994 mdname(mddev));
4995 return -EINVAL;
4996 }
4997 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004998 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004999 > rdev->data_offset) {
5000 printk("md: %s: metadata overlaps data\n",
5001 mdname(mddev));
5002 return -EINVAL;
5003 }
5004 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005005 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 }
5007
NeilBrowna167f662010-10-26 18:31:13 +11005008 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10005009 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11005010 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11005011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005013 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005014 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005016 if (mddev->level != LEVEL_NONE)
5017 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5018 mddev->level);
5019 else
5020 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5021 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return -EINVAL;
5023 }
NeilBrown2604b702006-01-06 00:20:36 -08005024 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005026 if (mddev->level != pers->level) {
5027 mddev->level = pers->level;
5028 mddev->new_level = pers->level;
5029 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005030 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
NeilBrownf6705572006-03-27 01:18:11 -08005032 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005033 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005034 /* This personality cannot handle reshaping... */
5035 mddev->pers = NULL;
5036 module_put(pers->owner);
5037 return -EINVAL;
5038 }
5039
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005040 if (pers->sync_request) {
5041 /* Warn if this is a potentially silly
5042 * configuration.
5043 */
5044 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005045 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005046 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005047
NeilBrowndafb20f2012-03-19 12:46:39 +11005048 rdev_for_each(rdev, mddev)
5049 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005050 if (rdev < rdev2 &&
5051 rdev->bdev->bd_contains ==
5052 rdev2->bdev->bd_contains) {
5053 printk(KERN_WARNING
5054 "%s: WARNING: %s appears to be"
5055 " on the same physical disk as"
5056 " %s.\n",
5057 mdname(mddev),
5058 bdevname(rdev->bdev,b),
5059 bdevname(rdev2->bdev,b2));
5060 warned = 1;
5061 }
5062 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005063
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005064 if (warned)
5065 printk(KERN_WARNING
5066 "True protection against single-disk"
5067 " failure might be compromised.\n");
5068 }
5069
NeilBrown657390d2005-08-26 18:34:16 -07005070 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005071 /* may be over-ridden by personality */
5072 mddev->resync_max_sectors = mddev->dev_sectors;
5073
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005074 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
NeilBrown0f9552b2009-12-30 12:08:50 +11005076 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005077 mddev->ro = 2; /* read-only, but switch on first write */
5078
NeilBrownb15c2e52006-01-06 00:20:16 -08005079 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005080 if (err)
5081 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005082 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5083 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5084 " but 'external_size' not in effect?\n", __func__);
5085 printk(KERN_ERR
5086 "md: invalid array_size %llu > default size %llu\n",
5087 (unsigned long long)mddev->array_sectors / 2,
5088 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5089 err = -EINVAL;
5090 mddev->pers->stop(mddev);
5091 }
NeilBrownef99bf42012-05-22 13:55:08 +10005092 if (err == 0 && mddev->pers->sync_request &&
5093 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005094 err = bitmap_create(mddev);
5095 if (err) {
5096 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5097 mdname(mddev), err);
5098 mddev->pers->stop(mddev);
5099 }
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 module_put(mddev->pers->owner);
5103 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005104 bitmap_destroy(mddev);
5105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005107 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005108 if (mddev->kobj.sd &&
5109 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005110 printk(KERN_WARNING
5111 "md: cannot register extra attributes for %s\n",
5112 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005113 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005114 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005115 mddev->ro = 0;
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005118 atomic_set(&mddev->max_corr_read_errors,
5119 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 mddev->safemode = 0;
5121 mddev->safemode_timer.function = md_safemode_timeout;
5122 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005123 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005125 smp_wmb();
5126 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005127 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005128 if (rdev->raid_disk >= 0)
5129 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005130 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5133
NeilBrown850b2b42006-10-03 01:15:46 -07005134 if (mddev->flags)
5135 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
NeilBrownd7603b72006-01-06 00:20:30 -08005137 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005138 sysfs_notify_dirent_safe(mddev->sysfs_state);
5139 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005140 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 return 0;
5142}
NeilBrown390ee602010-06-01 19:37:27 +10005143EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
NeilBrownfd01b882011-10-11 16:47:53 +11005145static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005146{
5147 int err;
5148
5149 err = md_run(mddev);
5150 if (err)
5151 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005152 err = bitmap_load(mddev);
5153 if (err) {
5154 bitmap_destroy(mddev);
5155 goto out;
5156 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005157
5158 md_wakeup_thread(mddev->thread);
5159 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5160
NeilBrownfe60b012010-03-29 11:10:42 +11005161 set_capacity(mddev->gendisk, mddev->array_sectors);
5162 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005163 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005164 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5165out:
5166 return err;
5167}
5168
NeilBrownfd01b882011-10-11 16:47:53 +11005169static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
5171 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Andre Noll80fab1d2008-07-11 22:02:21 +10005173 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005175 return -ENXIO;
5176 if (!mddev->pers)
5177 return -EINVAL;
5178 if (!mddev->ro)
5179 return -EBUSY;
5180 mddev->safemode = 0;
5181 mddev->ro = 0;
5182 set_disk_ro(disk, 0);
5183 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5184 mdname(mddev));
5185 /* Kick recovery or resync if necessary */
5186 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5187 md_wakeup_thread(mddev->thread);
5188 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005189 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
NeilBrownacc55e22006-06-26 00:27:47 -07005193/* similar to deny_write_access, but accounts for our holding a reference
5194 * to the file ourselves */
5195static int deny_bitmap_write_access(struct file * file)
5196{
5197 struct inode *inode = file->f_mapping->host;
5198
5199 spin_lock(&inode->i_lock);
5200 if (atomic_read(&inode->i_writecount) > 1) {
5201 spin_unlock(&inode->i_lock);
5202 return -ETXTBSY;
5203 }
5204 atomic_set(&inode->i_writecount, -1);
5205 spin_unlock(&inode->i_lock);
5206
5207 return 0;
5208}
5209
NeilBrown43a70502009-12-14 12:49:55 +11005210void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005211{
5212 struct inode *inode = file->f_mapping->host;
5213
5214 spin_lock(&inode->i_lock);
5215 atomic_set(&inode->i_writecount, 1);
5216 spin_unlock(&inode->i_lock);
5217}
5218
NeilBrownfd01b882011-10-11 16:47:53 +11005219static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005220{
5221 mddev->array_sectors = 0;
5222 mddev->external_size = 0;
5223 mddev->dev_sectors = 0;
5224 mddev->raid_disks = 0;
5225 mddev->recovery_cp = 0;
5226 mddev->resync_min = 0;
5227 mddev->resync_max = MaxSector;
5228 mddev->reshape_position = MaxSector;
5229 mddev->external = 0;
5230 mddev->persistent = 0;
5231 mddev->level = LEVEL_NONE;
5232 mddev->clevel[0] = 0;
5233 mddev->flags = 0;
5234 mddev->ro = 0;
5235 mddev->metadata_type[0] = 0;
5236 mddev->chunk_sectors = 0;
5237 mddev->ctime = mddev->utime = 0;
5238 mddev->layout = 0;
5239 mddev->max_disks = 0;
5240 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005241 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005242 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005243 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005244 mddev->new_level = LEVEL_NONE;
5245 mddev->new_layout = 0;
5246 mddev->new_chunk_sectors = 0;
5247 mddev->curr_resync = 0;
5248 mddev->resync_mismatches = 0;
5249 mddev->suspend_lo = mddev->suspend_hi = 0;
5250 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5251 mddev->recovery = 0;
5252 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005253 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005254 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005255 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005256 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005257 mddev->bitmap_info.offset = 0;
5258 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005259 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005260 mddev->bitmap_info.chunksize = 0;
5261 mddev->bitmap_info.daemon_sleep = 0;
5262 mddev->bitmap_info.max_write_behind = 0;
5263}
5264
NeilBrownfd01b882011-10-11 16:47:53 +11005265static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005266{
5267 if (mddev->sync_thread) {
5268 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5269 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005270 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005271 }
5272
5273 del_timer_sync(&mddev->safemode_timer);
5274
5275 bitmap_flush(mddev);
5276 md_super_wait(mddev);
5277
5278 if (!mddev->in_sync || mddev->flags) {
5279 /* mark array as shutdown cleanly */
5280 mddev->in_sync = 1;
5281 md_update_sb(mddev, 1);
5282 }
5283}
NeilBrowndefad612011-01-14 09:14:33 +11005284
NeilBrownfd01b882011-10-11 16:47:53 +11005285void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005286{
5287 mddev_lock(mddev);
5288 __md_stop_writes(mddev);
5289 mddev_unlock(mddev);
5290}
NeilBrown390ee602010-06-01 19:37:27 +10005291EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005292
NeilBrownfd01b882011-10-11 16:47:53 +11005293void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005294{
NeilBrown0ca69882011-01-14 09:14:33 +11005295 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005296 mddev->pers->stop(mddev);
5297 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5298 mddev->to_remove = &md_redundancy_group;
5299 module_put(mddev->pers->owner);
5300 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005301 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005302}
NeilBrown390ee602010-06-01 19:37:27 +10005303EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005304
NeilBrowna05b7ea02012-07-19 15:59:18 +10005305static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005306{
5307 int err = 0;
5308 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005309 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005310 printk("md: %s still in use.\n",mdname(mddev));
5311 err = -EBUSY;
5312 goto out;
5313 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005314 if (bdev)
5315 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005316 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005317 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005318
5319 err = -ENXIO;
5320 if (mddev->ro==1)
5321 goto out;
5322 mddev->ro = 1;
5323 set_disk_ro(mddev->gendisk, 1);
5324 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005325 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005326 err = 0;
5327 }
5328out:
5329 mutex_unlock(&mddev->open_mutex);
5330 return err;
5331}
5332
NeilBrown9e653b62006-06-26 00:27:58 -07005333/* mode:
5334 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005335 * 2 - stop but do not disassemble array
5336 */
NeilBrowna05b7ea02012-07-19 15:59:18 +10005337static int do_md_stop(struct mddev * mddev, int mode,
5338 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005341 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
NeilBrownc8c00a62009-08-10 12:50:52 +10005343 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005344 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005345 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005346 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005347 mutex_unlock(&mddev->open_mutex);
5348 return -EBUSY;
5349 }
NeilBrowna05b7ea02012-07-19 15:59:18 +10005350 if (bdev)
5351 /* It is possible IO was issued on some other
5352 * open file which was closed before we took ->open_mutex.
5353 * As that was not the last close __blkdev_put will not
5354 * have called sync_blockdev, so we must.
5355 */
5356 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
NeilBrown6e17b022010-08-07 21:41:19 +10005358 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005359 if (mddev->ro)
5360 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005361
NeilBrowndefad612011-01-14 09:14:33 +11005362 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005363 md_stop(mddev);
5364 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005365 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005366
NeilBrowna4bd82d2010-03-29 13:23:10 +11005367 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005368 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005369
NeilBrowndafb20f2012-03-19 12:46:39 +11005370 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005371 if (rdev->raid_disk >= 0)
5372 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005373
NeilBrowna4bd82d2010-03-29 13:23:10 +11005374 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005375 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005376 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005377 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005378
NeilBrowna4bd82d2010-03-29 13:23:10 +11005379 if (mddev->ro)
5380 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005381 } else
5382 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 /*
5384 * Free resources if final stop
5385 */
NeilBrown9e653b62006-06-26 00:27:58 -07005386 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5388
NeilBrown978f9462006-02-02 14:28:05 -08005389 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005390 if (mddev->bitmap_info.file) {
5391 restore_bitmap_write_access(mddev->bitmap_info.file);
5392 fput(mddev->bitmap_info.file);
5393 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005394 }
NeilBrownc3d97142009-12-14 12:49:52 +11005395 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 export_array(mddev);
5398
NeilBrown6177b472010-03-29 11:37:13 +11005399 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005400 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005401 if (mddev->hold_active == UNTIL_STOP)
5402 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005403 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005404 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005405 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005406 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005410#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005411static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
NeilBrown3cb03002011-10-11 16:45:26 +11005413 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 int err;
5415
NeilBrowna757e642005-04-16 15:26:42 -07005416 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 printk(KERN_INFO "md: running: ");
5420
NeilBrowndafb20f2012-03-19 12:46:39 +11005421 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 char b[BDEVNAME_SIZE];
5423 printk("<%s>", bdevname(rdev->bdev,b));
5424 }
5425 printk("\n");
5426
NeilBrownd710e132008-10-13 11:55:12 +11005427 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 if (err) {
5429 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea02012-07-19 15:59:18 +10005430 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432}
5433
5434/*
5435 * lets try to run arrays based on all disks that have arrived
5436 * until now. (those are in pending_raid_disks)
5437 *
5438 * the method: pick the first pending disk, collect all disks with
5439 * the same UUID, remove all from the pending list and put them into
5440 * the 'same_array' list. Then order this list based on superblock
5441 * update time (freshest comes first), kick out 'old' disks and
5442 * compare superblocks. If everything's fine then run it.
5443 *
5444 * If "unit" is allocated, then bump its reference count
5445 */
5446static void autorun_devices(int part)
5447{
NeilBrown3cb03002011-10-11 16:45:26 +11005448 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005449 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 char b[BDEVNAME_SIZE];
5451
5452 printk(KERN_INFO "md: autorun ...\n");
5453 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005454 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005456 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005458 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 printk(KERN_INFO "md: considering %s ...\n",
5461 bdevname(rdev0->bdev,b));
5462 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005463 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (super_90_load(rdev, rdev0, 0) >= 0) {
5465 printk(KERN_INFO "md: adding %s ...\n",
5466 bdevname(rdev->bdev,b));
5467 list_move(&rdev->same_set, &candidates);
5468 }
5469 /*
5470 * now we have a set of devices, with all of them having
5471 * mostly sane superblocks. It's time to allocate the
5472 * mddev.
5473 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005474 if (part) {
5475 dev = MKDEV(mdp_major,
5476 rdev0->preferred_minor << MdpMinorShift);
5477 unit = MINOR(dev) >> MdpMinorShift;
5478 } else {
5479 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5480 unit = MINOR(dev);
5481 }
5482 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5484 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5485 break;
5486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 md_probe(dev, NULL, NULL);
5489 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005490 if (!mddev || !mddev->gendisk) {
5491 if (mddev)
5492 mddev_put(mddev);
5493 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 "md: cannot allocate memory for md drive.\n");
5495 break;
5496 }
5497 if (mddev_lock(mddev))
5498 printk(KERN_WARNING "md: %s locked, cannot run\n",
5499 mdname(mddev));
5500 else if (mddev->raid_disks || mddev->major_version
5501 || !list_empty(&mddev->disks)) {
5502 printk(KERN_WARNING
5503 "md: %s already running, cannot run %s\n",
5504 mdname(mddev), bdevname(rdev0->bdev,b));
5505 mddev_unlock(mddev);
5506 } else {
5507 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005508 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005509 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 list_del_init(&rdev->same_set);
5511 if (bind_rdev_to_array(rdev, mddev))
5512 export_rdev(rdev);
5513 }
5514 autorun_array(mddev);
5515 mddev_unlock(mddev);
5516 }
5517 /* on success, candidates will be empty, on error
5518 * it won't...
5519 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005520 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005521 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 mddev_put(mddev);
5525 }
5526 printk(KERN_INFO "md: ... autorun DONE.\n");
5527}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005528#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530static int get_version(void __user * arg)
5531{
5532 mdu_version_t ver;
5533
5534 ver.major = MD_MAJOR_VERSION;
5535 ver.minor = MD_MINOR_VERSION;
5536 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5537
5538 if (copy_to_user(arg, &ver, sizeof(ver)))
5539 return -EFAULT;
5540
5541 return 0;
5542}
5543
NeilBrownfd01b882011-10-11 16:47:53 +11005544static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
5546 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005547 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005548 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
NeilBrowna9f326e2009-09-23 18:06:41 +10005550 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005551 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005553 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 failed++;
5555 else {
5556 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005557 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005558 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 else
5560 spare++;
5561 }
5562 }
5563
5564 info.major_version = mddev->major_version;
5565 info.minor_version = mddev->minor_version;
5566 info.patch_version = MD_PATCHLEVEL_VERSION;
5567 info.ctime = mddev->ctime;
5568 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005569 info.size = mddev->dev_sectors / 2;
5570 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005571 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 info.nr_disks = nr;
5573 info.raid_disks = mddev->raid_disks;
5574 info.md_minor = mddev->md_minor;
5575 info.not_persistent= !mddev->persistent;
5576
5577 info.utime = mddev->utime;
5578 info.state = 0;
5579 if (mddev->in_sync)
5580 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005581 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005582 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005583 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 info.working_disks = working;
5585 info.failed_disks = failed;
5586 info.spare_disks = spare;
5587
5588 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005589 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 if (copy_to_user(arg, &info, sizeof(info)))
5592 return -EFAULT;
5593
5594 return 0;
5595}
5596
NeilBrownfd01b882011-10-11 16:47:53 +11005597static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005598{
5599 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5600 char *ptr, *buf = NULL;
5601 int err = -ENOMEM;
5602
Dan Williamsb5470dc2008-06-27 21:44:04 -07005603 if (md_allow_write(mddev))
5604 file = kmalloc(sizeof(*file), GFP_NOIO);
5605 else
5606 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005607
NeilBrown32a76272005-06-21 17:17:14 -07005608 if (!file)
5609 goto out;
5610
5611 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005612 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005613 file->pathname[0] = '\0';
5614 goto copy_out;
5615 }
5616
5617 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5618 if (!buf)
5619 goto out;
5620
NeilBrown1ec885c2012-05-22 13:55:10 +10005621 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5622 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005623 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005624 goto out;
5625
5626 strcpy(file->pathname, ptr);
5627
5628copy_out:
5629 err = 0;
5630 if (copy_to_user(arg, file, sizeof(*file)))
5631 err = -EFAULT;
5632out:
5633 kfree(buf);
5634 kfree(file);
5635 return err;
5636}
5637
NeilBrownfd01b882011-10-11 16:47:53 +11005638static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
5640 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005641 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 if (copy_from_user(&info, arg, sizeof(info)))
5644 return -EFAULT;
5645
Andre Noll26ef3792008-07-11 22:02:21 +10005646 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 if (rdev) {
5648 info.major = MAJOR(rdev->bdev->bd_dev);
5649 info.minor = MINOR(rdev->bdev->bd_dev);
5650 info.raid_disk = rdev->raid_disk;
5651 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005652 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005654 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 info.state |= (1<<MD_DISK_ACTIVE);
5656 info.state |= (1<<MD_DISK_SYNC);
5657 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005658 if (test_bit(WriteMostly, &rdev->flags))
5659 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 } else {
5661 info.major = info.minor = 0;
5662 info.raid_disk = -1;
5663 info.state = (1<<MD_DISK_REMOVED);
5664 }
5665
5666 if (copy_to_user(arg, &info, sizeof(info)))
5667 return -EFAULT;
5668
5669 return 0;
5670}
5671
NeilBrownfd01b882011-10-11 16:47:53 +11005672static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
5674 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005675 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 dev_t dev = MKDEV(info->major,info->minor);
5677
5678 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5679 return -EOVERFLOW;
5680
5681 if (!mddev->raid_disks) {
5682 int err;
5683 /* expecting a device which has a superblock */
5684 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5685 if (IS_ERR(rdev)) {
5686 printk(KERN_WARNING
5687 "md: md_import_device returned %ld\n",
5688 PTR_ERR(rdev));
5689 return PTR_ERR(rdev);
5690 }
5691 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005692 struct md_rdev *rdev0
5693 = list_entry(mddev->disks.next,
5694 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005695 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 .load_super(rdev, rdev0, mddev->minor_version);
5697 if (err < 0) {
5698 printk(KERN_WARNING
5699 "md: %s has different UUID to %s\n",
5700 bdevname(rdev->bdev,b),
5701 bdevname(rdev0->bdev,b2));
5702 export_rdev(rdev);
5703 return -EINVAL;
5704 }
5705 }
5706 err = bind_rdev_to_array(rdev, mddev);
5707 if (err)
5708 export_rdev(rdev);
5709 return err;
5710 }
5711
5712 /*
5713 * add_new_disk can be used once the array is assembled
5714 * to add "hot spares". They must already have a superblock
5715 * written
5716 */
5717 if (mddev->pers) {
5718 int err;
5719 if (!mddev->pers->hot_add_disk) {
5720 printk(KERN_WARNING
5721 "%s: personality does not support diskops!\n",
5722 mdname(mddev));
5723 return -EINVAL;
5724 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005725 if (mddev->persistent)
5726 rdev = md_import_device(dev, mddev->major_version,
5727 mddev->minor_version);
5728 else
5729 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 if (IS_ERR(rdev)) {
5731 printk(KERN_WARNING
5732 "md: md_import_device returned %ld\n",
5733 PTR_ERR(rdev));
5734 return PTR_ERR(rdev);
5735 }
NeilBrown1a855a02010-12-09 16:36:28 +11005736 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005737 if (!mddev->persistent) {
5738 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005739 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005740 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005741 set_bit(In_sync, &rdev->flags);
5742 } else
NeilBrown41158c72005-06-21 17:17:25 -07005743 rdev->raid_disk = -1;
5744 } else
5745 super_types[mddev->major_version].
5746 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005747 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005748 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005749 /* This was a hot-add request, but events doesn't
5750 * match, so reject it.
5751 */
5752 export_rdev(rdev);
5753 return -EINVAL;
5754 }
5755
NeilBrown1a855a02010-12-09 16:36:28 +11005756 if (test_bit(In_sync, &rdev->flags))
5757 rdev->saved_raid_disk = rdev->raid_disk;
5758 else
5759 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005760
NeilBrownb2d444d2005-11-08 21:39:31 -08005761 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005762 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5763 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005764 else
5765 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 rdev->raid_disk = -1;
5768 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005769 if (!err && !mddev->pers->hot_remove_disk) {
5770 /* If there is hot_add_disk but no hot_remove_disk
5771 * then added disks for geometry changes,
5772 * and should be added immediately.
5773 */
5774 super_types[mddev->major_version].
5775 validate_super(mddev, rdev);
5776 err = mddev->pers->hot_add_disk(mddev, rdev);
5777 if (err)
5778 unbind_rdev_from_array(rdev);
5779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 if (err)
5781 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005782 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005783 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005784
NeilBrown17571282006-12-10 02:20:52 -08005785 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005786 if (mddev->degraded)
5787 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005788 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005789 if (!err)
5790 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005791 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 return err;
5793 }
5794
5795 /* otherwise, add_new_disk is only allowed
5796 * for major_version==0 superblocks
5797 */
5798 if (mddev->major_version != 0) {
5799 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5800 mdname(mddev));
5801 return -EINVAL;
5802 }
5803
5804 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5805 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005806 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 if (IS_ERR(rdev)) {
5808 printk(KERN_WARNING
5809 "md: error, md_import_device() returned %ld\n",
5810 PTR_ERR(rdev));
5811 return PTR_ERR(rdev);
5812 }
5813 rdev->desc_nr = info->number;
5814 if (info->raid_disk < mddev->raid_disks)
5815 rdev->raid_disk = info->raid_disk;
5816 else
5817 rdev->raid_disk = -1;
5818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005820 if (info->state & (1<<MD_DISK_SYNC))
5821 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005823 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5824 set_bit(WriteMostly, &rdev->flags);
5825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (!mddev->persistent) {
5827 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005828 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5829 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005830 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005831 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
NeilBrown2bf071b2006-01-06 00:20:55 -08005833 err = bind_rdev_to_array(rdev, mddev);
5834 if (err) {
5835 export_rdev(rdev);
5836 return err;
5837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
5839
5840 return 0;
5841}
5842
NeilBrownfd01b882011-10-11 16:47:53 +11005843static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
5845 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005846 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 rdev = find_rdev(mddev, dev);
5849 if (!rdev)
5850 return -ENXIO;
5851
5852 if (rdev->raid_disk >= 0)
5853 goto busy;
5854
5855 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005856 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005857 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
5859 return 0;
5860busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005861 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 bdevname(rdev->bdev,b), mdname(mddev));
5863 return -EBUSY;
5864}
5865
NeilBrownfd01b882011-10-11 16:47:53 +11005866static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
5868 char b[BDEVNAME_SIZE];
5869 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005870 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872 if (!mddev->pers)
5873 return -ENODEV;
5874
5875 if (mddev->major_version != 0) {
5876 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5877 " version-0 superblocks.\n",
5878 mdname(mddev));
5879 return -EINVAL;
5880 }
5881 if (!mddev->pers->hot_add_disk) {
5882 printk(KERN_WARNING
5883 "%s: personality does not support diskops!\n",
5884 mdname(mddev));
5885 return -EINVAL;
5886 }
5887
NeilBrownd710e132008-10-13 11:55:12 +11005888 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (IS_ERR(rdev)) {
5890 printk(KERN_WARNING
5891 "md: error, md_import_device() returned %ld\n",
5892 PTR_ERR(rdev));
5893 return -EINVAL;
5894 }
5895
5896 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005897 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005899 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
NeilBrown8190e752009-06-18 08:48:58 +10005901 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
NeilBrownb2d444d2005-11-08 21:39:31 -08005903 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 printk(KERN_WARNING
5905 "md: can not hot-add faulty %s disk to %s!\n",
5906 bdevname(rdev->bdev,b), mdname(mddev));
5907 err = -EINVAL;
5908 goto abort_export;
5909 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005910 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005912 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005913 err = bind_rdev_to_array(rdev, mddev);
5914 if (err)
5915 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 /*
5918 * The rest should better be atomic, we can have disk failures
5919 * noticed in interrupt contexts ...
5920 */
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 rdev->raid_disk = -1;
5923
NeilBrown850b2b42006-10-03 01:15:46 -07005924 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 /*
5927 * Kick recovery, maybe this spare has to be added to the
5928 * array immediately.
5929 */
5930 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5931 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005932 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 return 0;
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935abort_export:
5936 export_rdev(rdev);
5937 return err;
5938}
5939
NeilBrownfd01b882011-10-11 16:47:53 +11005940static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005941{
5942 int err;
5943
NeilBrown36fa3062005-09-09 16:23:45 -07005944 if (mddev->pers) {
5945 if (!mddev->pers->quiesce)
5946 return -EBUSY;
5947 if (mddev->recovery || mddev->sync_thread)
5948 return -EBUSY;
5949 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005950 }
5951
NeilBrown36fa3062005-09-09 16:23:45 -07005952
5953 if (fd >= 0) {
5954 if (mddev->bitmap)
5955 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005956 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005957
NeilBrownc3d97142009-12-14 12:49:52 +11005958 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005959 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5960 mdname(mddev));
5961 return -EBADF;
5962 }
5963
NeilBrownc3d97142009-12-14 12:49:52 +11005964 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005965 if (err) {
5966 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5967 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005968 fput(mddev->bitmap_info.file);
5969 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005970 return err;
5971 }
NeilBrownc3d97142009-12-14 12:49:52 +11005972 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005973 } else if (mddev->bitmap == NULL)
5974 return -ENOENT; /* cannot remove what isn't there */
5975 err = 0;
5976 if (mddev->pers) {
5977 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005978 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005979 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005980 if (!err)
5981 err = bitmap_load(mddev);
5982 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005983 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005984 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005985 fd = -1; /* make sure to put the file */
5986 }
NeilBrown36fa3062005-09-09 16:23:45 -07005987 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005988 }
5989 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005990 if (mddev->bitmap_info.file) {
5991 restore_bitmap_write_access(mddev->bitmap_info.file);
5992 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005993 }
NeilBrownc3d97142009-12-14 12:49:52 +11005994 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005995 }
5996
NeilBrown32a76272005-06-21 17:17:14 -07005997 return err;
5998}
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000/*
6001 * set_array_info is used two different ways
6002 * The original usage is when creating a new array.
6003 * In this usage, raid_disks is > 0 and it together with
6004 * level, size, not_persistent,layout,chunksize determine the
6005 * shape of the array.
6006 * This will always create an array with a type-0.90.0 superblock.
6007 * The newer usage is when assembling an array.
6008 * In this case raid_disks will be 0, and the major_version field is
6009 * use to determine which style super-blocks are to be found on the devices.
6010 * The minor and patch _version numbers are also kept incase the
6011 * super_block handler wishes to interpret them.
6012 */
NeilBrownfd01b882011-10-11 16:47:53 +11006013static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
6015
6016 if (info->raid_disks == 0) {
6017 /* just setting version number for superblock loading */
6018 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006019 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 super_types[info->major_version].name == NULL) {
6021 /* maybe try to auto-load a module? */
6022 printk(KERN_INFO
6023 "md: superblock version %d not known\n",
6024 info->major_version);
6025 return -EINVAL;
6026 }
6027 mddev->major_version = info->major_version;
6028 mddev->minor_version = info->minor_version;
6029 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006030 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006031 /* ensure mddev_put doesn't delete this now that there
6032 * is some minimal configuration.
6033 */
6034 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 return 0;
6036 }
6037 mddev->major_version = MD_MAJOR_VERSION;
6038 mddev->minor_version = MD_MINOR_VERSION;
6039 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6040 mddev->ctime = get_seconds();
6041
6042 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006043 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006044 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 mddev->raid_disks = info->raid_disks;
6046 /* don't set md_minor, it is determined by which /dev/md* was
6047 * openned
6048 */
6049 if (info->state & (1<<MD_SB_CLEAN))
6050 mddev->recovery_cp = MaxSector;
6051 else
6052 mddev->recovery_cp = 0;
6053 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006054 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
6056 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006057 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 mddev->max_disks = MD_SB_DISKS;
6060
NeilBrowne6910632008-02-06 01:39:51 -08006061 if (mddev->persistent)
6062 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006063 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
NeilBrownc3d97142009-12-14 12:49:52 +11006065 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006066 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006067 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006068
NeilBrownf6705572006-03-27 01:18:11 -08006069 mddev->reshape_position = MaxSector;
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 /*
6072 * Generate a 128 bit UUID
6073 */
6074 get_random_bytes(mddev->uuid, 16);
6075
NeilBrownf6705572006-03-27 01:18:11 -08006076 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006077 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006078 mddev->new_layout = mddev->layout;
6079 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006080 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return 0;
6083}
6084
NeilBrownfd01b882011-10-11 16:47:53 +11006085void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006086{
Dan Williamsb522adc2009-03-31 15:00:31 +11006087 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6088
6089 if (mddev->external_size)
6090 return;
6091
Dan Williams1f403622009-03-31 14:59:03 +11006092 mddev->array_sectors = array_sectors;
6093}
6094EXPORT_SYMBOL(md_set_array_sectors);
6095
NeilBrownfd01b882011-10-11 16:47:53 +11006096static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006097{
NeilBrown3cb03002011-10-11 16:45:26 +11006098 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006099 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006100 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006101
6102 if (mddev->pers->resize == NULL)
6103 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006104 /* The "num_sectors" is the number of sectors of each device that
6105 * is used. This can only make sense for arrays with redundancy.
6106 * linear and raid0 always use whatever space is available. We can only
6107 * consider changing this number if no resync or reconstruction is
6108 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006109 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006110 * of each device. If num_sectors is zero, we find the largest size
6111 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006112 */
6113 if (mddev->sync_thread)
6114 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006115
NeilBrowndafb20f2012-03-19 12:46:39 +11006116 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006117 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006118
Andre Nolld71f9f82008-07-11 22:02:22 +10006119 if (fit && (num_sectors == 0 || num_sectors > avail))
6120 num_sectors = avail;
6121 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006122 return -ENOSPC;
6123 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006124 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006125 if (!rv)
6126 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006127 return rv;
6128}
6129
NeilBrownfd01b882011-10-11 16:47:53 +11006130static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006131{
6132 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006133 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006134 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006135 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006136 return -EINVAL;
6137 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006138 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006139 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006140 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006141 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006142
6143 rdev_for_each(rdev, mddev) {
6144 if (mddev->raid_disks < raid_disks &&
6145 rdev->data_offset < rdev->new_data_offset)
6146 return -EINVAL;
6147 if (mddev->raid_disks > raid_disks &&
6148 rdev->data_offset > rdev->new_data_offset)
6149 return -EINVAL;
6150 }
6151
NeilBrown63c70c42006-03-27 01:18:13 -08006152 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006153 if (mddev->delta_disks < 0)
6154 mddev->reshape_backwards = 1;
6155 else if (mddev->delta_disks > 0)
6156 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006157
6158 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006159 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006160 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006161 mddev->reshape_backwards = 0;
6162 }
NeilBrownda943b992006-01-06 00:20:54 -08006163 return rv;
6164}
6165
6166
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167/*
6168 * update_array_info is used to change the configuration of an
6169 * on-line array.
6170 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6171 * fields in the info are checked against the array.
6172 * Any differences that cannot be handled will cause an error.
6173 * Normally, only one change can be managed at a time.
6174 */
NeilBrownfd01b882011-10-11 16:47:53 +11006175static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176{
6177 int rv = 0;
6178 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006179 int state = 0;
6180
6181 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006182 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006183 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 if (mddev->major_version != info->major_version ||
6186 mddev->minor_version != info->minor_version ||
6187/* mddev->patch_version != info->patch_version || */
6188 mddev->ctime != info->ctime ||
6189 mddev->level != info->level ||
6190/* mddev->layout != info->layout || */
6191 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006192 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006193 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6194 ((state^info->state) & 0xfffffe00)
6195 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return -EINVAL;
6197 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006198 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6199 cnt++;
6200 if (mddev->raid_disks != info->raid_disks)
6201 cnt++;
6202 if (mddev->layout != info->layout)
6203 cnt++;
6204 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6205 cnt++;
6206 if (cnt == 0)
6207 return 0;
6208 if (cnt > 1)
6209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 if (mddev->layout != info->layout) {
6212 /* Change layout
6213 * we don't need to do anything at the md level, the
6214 * personality will take care of it all.
6215 */
NeilBrown50ac1682009-06-18 08:47:55 +10006216 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006218 else {
6219 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006220 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006221 if (rv)
6222 mddev->new_layout = mddev->layout;
6223 return rv;
6224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006226 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006227 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
NeilBrownda943b992006-01-06 00:20:54 -08006229 if (mddev->raid_disks != info->raid_disks)
6230 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
NeilBrown36fa3062005-09-09 16:23:45 -07006232 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6233 if (mddev->pers->quiesce == NULL)
6234 return -EINVAL;
6235 if (mddev->recovery || mddev->sync_thread)
6236 return -EBUSY;
6237 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6238 /* add the bitmap */
6239 if (mddev->bitmap)
6240 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006241 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006242 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006243 mddev->bitmap_info.offset =
6244 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006245 mddev->bitmap_info.space =
6246 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006247 mddev->pers->quiesce(mddev, 1);
6248 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006249 if (!rv)
6250 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006251 if (rv)
6252 bitmap_destroy(mddev);
6253 mddev->pers->quiesce(mddev, 0);
6254 } else {
6255 /* remove the bitmap */
6256 if (!mddev->bitmap)
6257 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006258 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006259 return -EINVAL;
6260 mddev->pers->quiesce(mddev, 1);
6261 bitmap_destroy(mddev);
6262 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006263 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006264 }
6265 }
NeilBrown850b2b42006-10-03 01:15:46 -07006266 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 return rv;
6268}
6269
NeilBrownfd01b882011-10-11 16:47:53 +11006270static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
NeilBrown3cb03002011-10-11 16:45:26 +11006272 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
6274 if (mddev->pers == NULL)
6275 return -ENODEV;
6276
6277 rdev = find_rdev(mddev, dev);
6278 if (!rdev)
6279 return -ENODEV;
6280
6281 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006282 if (!test_bit(Faulty, &rdev->flags))
6283 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 return 0;
6285}
6286
Andre Noll2f9618c2008-04-25 18:57:58 +02006287/*
6288 * We have a problem here : there is no easy way to give a CHS
6289 * virtual geometry. We currently pretend that we have a 2 heads
6290 * 4 sectors (with a BIG number of cylinders...). This drives
6291 * dosfs just mad... ;-)
6292 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006293static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6294{
NeilBrownfd01b882011-10-11 16:47:53 +11006295 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006296
6297 geo->heads = 2;
6298 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006299 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006300 return 0;
6301}
6302
Al Viroa39907f2008-03-02 10:31:15 -05006303static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 unsigned int cmd, unsigned long arg)
6305{
6306 int err = 0;
6307 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006308 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006309 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310
NeilBrown506c9e42011-12-23 10:17:26 +11006311 switch (cmd) {
6312 case RAID_VERSION:
6313 case GET_ARRAY_INFO:
6314 case GET_DISK_INFO:
6315 break;
6316 default:
6317 if (!capable(CAP_SYS_ADMIN))
6318 return -EACCES;
6319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
6321 /*
6322 * Commands dealing with the RAID driver but not any
6323 * particular array:
6324 */
6325 switch (cmd)
6326 {
6327 case RAID_VERSION:
6328 err = get_version(argp);
6329 goto done;
6330
6331 case PRINT_RAID_DEBUG:
6332 err = 0;
6333 md_print_devices();
6334 goto done;
6335
6336#ifndef MODULE
6337 case RAID_AUTORUN:
6338 err = 0;
6339 autostart_arrays(arg);
6340 goto done;
6341#endif
6342 default:;
6343 }
6344
6345 /*
6346 * Commands creating/starting a new array:
6347 */
6348
Al Viroa39907f2008-03-02 10:31:15 -05006349 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
6351 if (!mddev) {
6352 BUG();
6353 goto abort;
6354 }
6355
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 err = mddev_lock(mddev);
6357 if (err) {
6358 printk(KERN_INFO
6359 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6360 err, cmd);
6361 goto abort;
6362 }
6363
6364 switch (cmd)
6365 {
6366 case SET_ARRAY_INFO:
6367 {
6368 mdu_array_info_t info;
6369 if (!arg)
6370 memset(&info, 0, sizeof(info));
6371 else if (copy_from_user(&info, argp, sizeof(info))) {
6372 err = -EFAULT;
6373 goto abort_unlock;
6374 }
6375 if (mddev->pers) {
6376 err = update_array_info(mddev, &info);
6377 if (err) {
6378 printk(KERN_WARNING "md: couldn't update"
6379 " array info. %d\n", err);
6380 goto abort_unlock;
6381 }
6382 goto done_unlock;
6383 }
6384 if (!list_empty(&mddev->disks)) {
6385 printk(KERN_WARNING
6386 "md: array %s already has disks!\n",
6387 mdname(mddev));
6388 err = -EBUSY;
6389 goto abort_unlock;
6390 }
6391 if (mddev->raid_disks) {
6392 printk(KERN_WARNING
6393 "md: array %s already initialised!\n",
6394 mdname(mddev));
6395 err = -EBUSY;
6396 goto abort_unlock;
6397 }
6398 err = set_array_info(mddev, &info);
6399 if (err) {
6400 printk(KERN_WARNING "md: couldn't set"
6401 " array info. %d\n", err);
6402 goto abort_unlock;
6403 }
6404 }
6405 goto done_unlock;
6406
6407 default:;
6408 }
6409
6410 /*
6411 * Commands querying/configuring an existing array:
6412 */
NeilBrown32a76272005-06-21 17:17:14 -07006413 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006414 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006415 if ((!mddev->raid_disks && !mddev->external)
6416 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6417 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6418 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 err = -ENODEV;
6420 goto abort_unlock;
6421 }
6422
6423 /*
6424 * Commands even a read-only array can execute:
6425 */
6426 switch (cmd)
6427 {
6428 case GET_ARRAY_INFO:
6429 err = get_array_info(mddev, argp);
6430 goto done_unlock;
6431
NeilBrown32a76272005-06-21 17:17:14 -07006432 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006433 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006434 goto done_unlock;
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 case GET_DISK_INFO:
6437 err = get_disk_info(mddev, argp);
6438 goto done_unlock;
6439
6440 case RESTART_ARRAY_RW:
6441 err = restart_array(mddev);
6442 goto done_unlock;
6443
6444 case STOP_ARRAY:
NeilBrowna05b7ea02012-07-19 15:59:18 +10006445 err = do_md_stop(mddev, 0, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 goto done_unlock;
6447
6448 case STOP_ARRAY_RO:
NeilBrowna05b7ea02012-07-19 15:59:18 +10006449 err = md_set_readonly(mddev, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 goto done_unlock;
6451
Dan Williamse2218352010-05-12 08:25:37 +10006452 case BLKROSET:
6453 if (get_user(ro, (int __user *)(arg))) {
6454 err = -EFAULT;
6455 goto done_unlock;
6456 }
6457 err = -EINVAL;
6458
6459 /* if the bdev is going readonly the value of mddev->ro
6460 * does not matter, no writes are coming
6461 */
6462 if (ro)
6463 goto done_unlock;
6464
6465 /* are we are already prepared for writes? */
6466 if (mddev->ro != 1)
6467 goto done_unlock;
6468
6469 /* transitioning to readauto need only happen for
6470 * arrays that call md_write_start
6471 */
6472 if (mddev->pers) {
6473 err = restart_array(mddev);
6474 if (err == 0) {
6475 mddev->ro = 2;
6476 set_disk_ro(mddev->gendisk, 0);
6477 }
6478 }
6479 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 }
6481
6482 /*
6483 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006484 * superblock, so we do not allow them on read-only arrays.
6485 * However non-MD ioctls (e.g. get-size) will still come through
6486 * here and hit the 'default' below, so only disallow
6487 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006489 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006490 if (mddev->ro == 2) {
6491 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006492 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006493 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6494 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006495 } else {
6496 err = -EROFS;
6497 goto abort_unlock;
6498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
6500
6501 switch (cmd)
6502 {
6503 case ADD_NEW_DISK:
6504 {
6505 mdu_disk_info_t info;
6506 if (copy_from_user(&info, argp, sizeof(info)))
6507 err = -EFAULT;
6508 else
6509 err = add_new_disk(mddev, &info);
6510 goto done_unlock;
6511 }
6512
6513 case HOT_REMOVE_DISK:
6514 err = hot_remove_disk(mddev, new_decode_dev(arg));
6515 goto done_unlock;
6516
6517 case HOT_ADD_DISK:
6518 err = hot_add_disk(mddev, new_decode_dev(arg));
6519 goto done_unlock;
6520
6521 case SET_DISK_FAULTY:
6522 err = set_disk_faulty(mddev, new_decode_dev(arg));
6523 goto done_unlock;
6524
6525 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006526 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 goto done_unlock;
6528
NeilBrown32a76272005-06-21 17:17:14 -07006529 case SET_BITMAP_FILE:
6530 err = set_bitmap_file(mddev, (int)arg);
6531 goto done_unlock;
6532
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 err = -EINVAL;
6535 goto abort_unlock;
6536 }
6537
6538done_unlock:
6539abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006540 if (mddev->hold_active == UNTIL_IOCTL &&
6541 err != -EINVAL)
6542 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 mddev_unlock(mddev);
6544
6545 return err;
6546done:
6547 if (err)
6548 MD_BUG();
6549abort:
6550 return err;
6551}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006552#ifdef CONFIG_COMPAT
6553static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6554 unsigned int cmd, unsigned long arg)
6555{
6556 switch (cmd) {
6557 case HOT_REMOVE_DISK:
6558 case HOT_ADD_DISK:
6559 case SET_DISK_FAULTY:
6560 case SET_BITMAP_FILE:
6561 /* These take in integer arg, do not convert */
6562 break;
6563 default:
6564 arg = (unsigned long)compat_ptr(arg);
6565 break;
6566 }
6567
6568 return md_ioctl(bdev, mode, cmd, arg);
6569}
6570#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
Al Viroa39907f2008-03-02 10:31:15 -05006572static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573{
6574 /*
6575 * Succeed if we can lock the mddev, which confirms that
6576 * it isn't being stopped right now.
6577 */
NeilBrownfd01b882011-10-11 16:47:53 +11006578 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 int err;
6580
Yuanhan Liu0c098222012-05-22 13:55:32 +10006581 if (!mddev)
6582 return -ENODEV;
6583
NeilBrownd3374822009-01-09 08:31:10 +11006584 if (mddev->gendisk != bdev->bd_disk) {
6585 /* we are racing with mddev_put which is discarding this
6586 * bd_disk.
6587 */
6588 mddev_put(mddev);
6589 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006590 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006591 /* Then retry the open from the top */
6592 return -ERESTARTSYS;
6593 }
6594 BUG_ON(mddev != bdev->bd_disk->private_data);
6595
NeilBrownc8c00a62009-08-10 12:50:52 +10006596 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 goto out;
6598
6599 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006600 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006601 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006603 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 out:
6605 return err;
6606}
6607
Al Viroa39907f2008-03-02 10:31:15 -05006608static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609{
NeilBrownfd01b882011-10-11 16:47:53 +11006610 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006612 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006613 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 mddev_put(mddev);
6615
6616 return 0;
6617}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006618
6619static int md_media_changed(struct gendisk *disk)
6620{
NeilBrownfd01b882011-10-11 16:47:53 +11006621 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006622
6623 return mddev->changed;
6624}
6625
6626static int md_revalidate(struct gendisk *disk)
6627{
NeilBrownfd01b882011-10-11 16:47:53 +11006628 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006629
6630 mddev->changed = 0;
6631 return 0;
6632}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006633static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634{
6635 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006636 .open = md_open,
6637 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006638 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006639#ifdef CONFIG_COMPAT
6640 .compat_ioctl = md_compat_ioctl,
6641#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006642 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006643 .media_changed = md_media_changed,
6644 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645};
6646
Adrian Bunk75c96f82005-05-05 16:16:09 -07006647static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648{
NeilBrown2b8bf342011-10-11 16:48:23 +11006649 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 /*
6652 * md_thread is a 'system-thread', it's priority should be very
6653 * high. We avoid resource deadlocks individually in each
6654 * raid personality. (RAID5 does preallocation) We also use RR and
6655 * the very same RT priority as kswapd, thus we will never get
6656 * into a priority inversion deadlock.
6657 *
6658 * we definitely have to have equal or higher priority than
6659 * bdflush, otherwise bdflush will deadlock if there are too
6660 * many dirty RAID5 blocks.
6661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
NeilBrown6985c432005-10-19 21:23:47 -07006663 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006664 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
NeilBrown93588e22005-11-15 00:09:12 -08006666 /* We need to wait INTERRUPTIBLE so that
6667 * we don't add to the load-average.
6668 * That means we need to be sure no signals are
6669 * pending
6670 */
6671 if (signal_pending(current))
6672 flush_signals(current);
6673
6674 wait_event_interruptible_timeout
6675 (thread->wqueue,
6676 test_bit(THREAD_WAKEUP, &thread->flags)
6677 || kthread_should_stop(),
6678 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
NeilBrown6c987912011-01-14 09:13:53 +11006680 clear_bit(THREAD_WAKEUP, &thread->flags);
6681 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006682 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 return 0;
6686}
6687
NeilBrown2b8bf342011-10-11 16:48:23 +11006688void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689{
6690 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006691 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 set_bit(THREAD_WAKEUP, &thread->flags);
6693 wake_up(&thread->wqueue);
6694 }
6695}
6696
NeilBrown2b8bf342011-10-11 16:48:23 +11006697struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 const char *name)
6699{
NeilBrown2b8bf342011-10-11 16:48:23 +11006700 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
NeilBrown2b8bf342011-10-11 16:48:23 +11006702 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 if (!thread)
6704 return NULL;
6705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 init_waitqueue_head(&thread->wqueue);
6707
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 thread->run = run;
6709 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006710 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006711 thread->tsk = kthread_run(md_thread, thread,
6712 "%s_%s",
6713 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006714 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006715 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 kfree(thread);
6717 return NULL;
6718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 return thread;
6720}
6721
NeilBrown2b8bf342011-10-11 16:48:23 +11006722void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723{
NeilBrown2b8bf342011-10-11 16:48:23 +11006724 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006725 if (!thread)
6726 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006727 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006728 /* Locking ensures that mddev_unlock does not wake_up a
6729 * non-existent thread
6730 */
6731 spin_lock(&pers_lock);
6732 *threadp = NULL;
6733 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006734
6735 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 kfree(thread);
6737}
6738
NeilBrownfd01b882011-10-11 16:47:53 +11006739void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740{
6741 if (!mddev) {
6742 MD_BUG();
6743 return;
6744 }
6745
NeilBrownb2d444d2005-11-08 21:39:31 -08006746 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006748
NeilBrownde393cd2011-07-28 11:31:48 +10006749 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 return;
6751 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006752 if (mddev->degraded)
6753 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006754 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6756 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6757 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006758 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006759 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006760 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761}
6762
6763/* seq_file implementation /proc/mdstat */
6764
6765static void status_unused(struct seq_file *seq)
6766{
6767 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006768 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
6770 seq_printf(seq, "unused devices: ");
6771
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006772 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 char b[BDEVNAME_SIZE];
6774 i++;
6775 seq_printf(seq, "%s ",
6776 bdevname(rdev->bdev,b));
6777 }
6778 if (!i)
6779 seq_printf(seq, "<none>");
6780
6781 seq_printf(seq, "\n");
6782}
6783
6784
NeilBrownfd01b882011-10-11 16:47:53 +11006785static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786{
NeilBrowndd71cf62009-05-07 12:49:35 +10006787 sector_t max_sectors, resync, res;
6788 unsigned long dt, db;
6789 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006790 int scale;
6791 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
NeilBrowndd71cf62009-05-07 12:49:35 +10006793 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
NeilBrownc804cde2012-05-21 09:28:33 +10006795 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6796 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006797 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006799 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 /*
6802 * Should not happen.
6803 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006804 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 MD_BUG();
6806 return;
6807 }
NeilBrown4588b422006-03-27 01:18:04 -08006808 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006809 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006810 * u32, as those are the requirements for sector_div.
6811 * Thus 'scale' must be at least 10
6812 */
6813 scale = 10;
6814 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006815 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006816 scale++;
6817 }
6818 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006819 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006820
6821 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 {
NeilBrown4588b422006-03-27 01:18:04 -08006823 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 seq_printf(seq, "[");
6825 for (i = 0; i < x; i++)
6826 seq_printf(seq, "=");
6827 seq_printf(seq, ">");
6828 for (i = 0; i < y; i++)
6829 seq_printf(seq, ".");
6830 seq_printf(seq, "] ");
6831 }
NeilBrown4588b422006-03-27 01:18:04 -08006832 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006833 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6834 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006835 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6836 "check" :
6837 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6838 "resync" : "recovery"))),
6839 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006840 (unsigned long long) resync/2,
6841 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
6843 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 * dt: time from mark until now
6845 * db: blocks written from mark until now
6846 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006847 *
6848 * rt is a sector_t, so could be 32bit or 64bit.
6849 * So we divide before multiply in case it is 32bit and close
6850 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006851 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006852 * near the end of resync when the number of remaining sectors
6853 * is close to 'db'.
6854 * We then divide rt by 32 after multiplying by db to compensate.
6855 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 */
6857 dt = ((jiffies - mddev->resync_mark) / HZ);
6858 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006859 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6860 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
NeilBrowndd71cf62009-05-07 12:49:35 +10006862 rt = max_sectors - resync; /* number of remaining sectors */
6863 sector_div(rt, db/32+1);
6864 rt *= dt;
6865 rt >>= 5;
6866
6867 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6868 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
NeilBrownff4e8d92006-07-10 04:44:16 -07006870 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871}
6872
6873static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6874{
6875 struct list_head *tmp;
6876 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006877 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
6879 if (l >= 0x10000)
6880 return NULL;
6881 if (!l--)
6882 /* header */
6883 return (void*)1;
6884
6885 spin_lock(&all_mddevs_lock);
6886 list_for_each(tmp,&all_mddevs)
6887 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006888 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 mddev_get(mddev);
6890 spin_unlock(&all_mddevs_lock);
6891 return mddev;
6892 }
6893 spin_unlock(&all_mddevs_lock);
6894 if (!l--)
6895 return (void*)2;/* tail */
6896 return NULL;
6897}
6898
6899static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6900{
6901 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006902 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
6904 ++*pos;
6905 if (v == (void*)2)
6906 return NULL;
6907
6908 spin_lock(&all_mddevs_lock);
6909 if (v == (void*)1)
6910 tmp = all_mddevs.next;
6911 else
6912 tmp = mddev->all_mddevs.next;
6913 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006914 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 else {
6916 next_mddev = (void*)2;
6917 *pos = 0x10000;
6918 }
6919 spin_unlock(&all_mddevs_lock);
6920
6921 if (v != (void*)1)
6922 mddev_put(mddev);
6923 return next_mddev;
6924
6925}
6926
6927static void md_seq_stop(struct seq_file *seq, void *v)
6928{
NeilBrownfd01b882011-10-11 16:47:53 +11006929 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930
6931 if (mddev && v != (void*)1 && v != (void*)2)
6932 mddev_put(mddev);
6933}
6934
6935static int md_seq_show(struct seq_file *seq, void *v)
6936{
NeilBrownfd01b882011-10-11 16:47:53 +11006937 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006938 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006939 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
6941 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006942 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 seq_printf(seq, "Personalities : ");
6944 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006945 list_for_each_entry(pers, &pers_list, list)
6946 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
6948 spin_unlock(&pers_lock);
6949 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006950 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 return 0;
6952 }
6953 if (v == (void*)2) {
6954 status_unused(seq);
6955 return 0;
6956 }
6957
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006958 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6962 seq_printf(seq, "%s : %sactive", mdname(mddev),
6963 mddev->pers ? "" : "in");
6964 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006965 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006967 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006968 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 seq_printf(seq, " %s", mddev->pers->name);
6970 }
6971
Andre Nolldd8ac332009-03-31 14:33:13 +11006972 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006973 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 char b[BDEVNAME_SIZE];
6975 seq_printf(seq, " %s[%d]",
6976 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006977 if (test_bit(WriteMostly, &rdev->flags))
6978 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006979 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 seq_printf(seq, "(F)");
6981 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006982 }
6983 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006984 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006985 if (test_bit(Replacement, &rdev->flags))
6986 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006987 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 }
6989
6990 if (!list_empty(&mddev->disks)) {
6991 if (mddev->pers)
6992 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006993 (unsigned long long)
6994 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 else
6996 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006997 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006999 if (mddev->persistent) {
7000 if (mddev->major_version != 0 ||
7001 mddev->minor_version != 90) {
7002 seq_printf(seq," super %d.%d",
7003 mddev->major_version,
7004 mddev->minor_version);
7005 }
NeilBrowne6910632008-02-06 01:39:51 -08007006 } else if (mddev->external)
7007 seq_printf(seq, " super external:%s",
7008 mddev->metadata_type);
7009 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007010 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
7012 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007013 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007015 if (mddev->pers->sync_request) {
7016 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007017 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007018 seq_printf(seq, "\n ");
7019 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
7020 seq_printf(seq, "\tresync=DELAYED\n ");
7021 else if (mddev->recovery_cp < MaxSector)
7022 seq_printf(seq, "\tresync=PENDING\n ");
7023 }
NeilBrown32a76272005-06-21 17:17:14 -07007024 } else
7025 seq_printf(seq, "\n ");
7026
NeilBrown57148962012-03-19 12:46:40 +11007027 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
7029 seq_printf(seq, "\n");
7030 }
7031 mddev_unlock(mddev);
7032
7033 return 0;
7034}
7035
Jan Engelhardt110518b2009-05-07 12:49:37 +10007036static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 .start = md_seq_start,
7038 .next = md_seq_next,
7039 .stop = md_seq_stop,
7040 .show = md_seq_show,
7041};
7042
7043static int md_seq_open(struct inode *inode, struct file *file)
7044{
Kay Sieversf1514632011-07-12 20:48:39 +02007045 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 int error;
7047
7048 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007049 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007050 return error;
7051
7052 seq = file->private_data;
7053 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 return error;
7055}
7056
NeilBrownd7603b72006-01-06 00:20:30 -08007057static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7058{
Kay Sieversf1514632011-07-12 20:48:39 +02007059 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007060 int mask;
7061
7062 poll_wait(filp, &md_event_waiters, wait);
7063
7064 /* always allow read */
7065 mask = POLLIN | POLLRDNORM;
7066
Kay Sieversf1514632011-07-12 20:48:39 +02007067 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007068 mask |= POLLERR | POLLPRI;
7069 return mask;
7070}
7071
Arjan van de Venfa027c22007-02-12 00:55:33 -08007072static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007073 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 .open = md_seq_open,
7075 .read = seq_read,
7076 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007077 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007078 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079};
7080
NeilBrown84fc4b52011-10-11 16:49:58 +11007081int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007084 list_add_tail(&p->list, &pers_list);
7085 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 spin_unlock(&pers_lock);
7087 return 0;
7088}
7089
NeilBrown84fc4b52011-10-11 16:49:58 +11007090int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091{
NeilBrown2604b702006-01-06 00:20:36 -08007092 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007094 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 spin_unlock(&pers_lock);
7096 return 0;
7097}
7098
NeilBrownfd01b882011-10-11 16:47:53 +11007099static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100{
NeilBrown3cb03002011-10-11 16:45:26 +11007101 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007103 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
7105 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007106 rcu_read_lock();
7107 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007109 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7110 (int)part_stat_read(&disk->part0, sectors[1]) -
7111 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007112 /* sync IO will cause sync_io to increase before the disk_stats
7113 * as sync_io is counted when a request starts, and
7114 * disk_stats is counted when it completes.
7115 * So resync activity will cause curr_events to be smaller than
7116 * when there was no such activity.
7117 * non-sync IO will cause disk_stat to increase without
7118 * increasing sync_io so curr_events will (eventually)
7119 * be larger than it was before. Once it becomes
7120 * substantially larger, the test below will cause
7121 * the array to appear non-idle, and resync will slow
7122 * down.
7123 * If there is a lot of outstanding resync activity when
7124 * we set last_event to curr_events, then all that activity
7125 * completing might cause the array to appear non-idle
7126 * and resync will be slowed down even though there might
7127 * not have been non-resync activity. This will only
7128 * happen once though. 'last_events' will soon reflect
7129 * the state where there is little or no outstanding
7130 * resync requests, and further resync activity will
7131 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007134 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 rdev->last_events = curr_events;
7136 idle = 0;
7137 }
7138 }
NeilBrown4b809912008-07-21 17:05:25 +10007139 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 return idle;
7141}
7142
NeilBrownfd01b882011-10-11 16:47:53 +11007143void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
7145 /* another "blocks" (512byte) blocks have been synced */
7146 atomic_sub(blocks, &mddev->recovery_active);
7147 wake_up(&mddev->recovery_wait);
7148 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007149 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 md_wakeup_thread(mddev->thread);
7151 // stop recovery, signal do_sync ....
7152 }
7153}
7154
7155
NeilBrown06d91a52005-06-21 17:17:12 -07007156/* md_write_start(mddev, bi)
7157 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007158 * in superblock) before writing, schedule a superblock update
7159 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007160 */
NeilBrownfd01b882011-10-11 16:47:53 +11007161void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162{
Neil Brown0fd62b82008-06-28 08:31:36 +10007163 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007164 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007165 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007166
NeilBrownf91de922005-11-08 21:39:36 -08007167 BUG_ON(mddev->ro == 1);
7168 if (mddev->ro == 2) {
7169 /* need to switch to read/write */
7170 mddev->ro = 0;
7171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7172 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007173 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007174 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007175 }
NeilBrown06d91a52005-06-21 17:17:12 -07007176 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007177 if (mddev->safemode == 1)
7178 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007179 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007180 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007181 if (mddev->in_sync) {
7182 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07007183 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007184 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007185 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007186 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007187 }
NeilBrowna9701a32005-11-08 21:39:34 -08007188 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007189 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007190 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007191 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007192 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007193 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194}
7195
NeilBrownfd01b882011-10-11 16:47:53 +11007196void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197{
7198 if (atomic_dec_and_test(&mddev->writes_pending)) {
7199 if (mddev->safemode == 2)
7200 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007201 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7203 }
7204}
7205
NeilBrown2a2275d2007-01-26 00:57:11 -08007206/* md_allow_write(mddev)
7207 * Calling this ensures that the array is marked 'active' so that writes
7208 * may proceed without blocking. It is important to call this before
7209 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7210 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007211 *
7212 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7213 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007214 */
NeilBrownfd01b882011-10-11 16:47:53 +11007215int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007216{
7217 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007218 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007219 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007220 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007221 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007222 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007223
7224 spin_lock_irq(&mddev->write_lock);
7225 if (mddev->in_sync) {
7226 mddev->in_sync = 0;
7227 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007228 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007229 if (mddev->safemode_delay &&
7230 mddev->safemode == 0)
7231 mddev->safemode = 1;
7232 spin_unlock_irq(&mddev->write_lock);
7233 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007234 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007235 } else
7236 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007237
NeilBrown070dc6d2010-08-30 17:33:34 +10007238 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007239 return -EAGAIN;
7240 else
7241 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007242}
7243EXPORT_SYMBOL_GPL(md_allow_write);
7244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245#define SYNC_MARKS 10
7246#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007247void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248{
NeilBrownfd01b882011-10-11 16:47:53 +11007249 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 unsigned int currspeed = 0,
7251 window;
NeilBrown57afd892005-06-21 17:17:13 -07007252 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 unsigned long mark[SYNC_MARKS];
7254 sector_t mark_cnt[SYNC_MARKS];
7255 int last_mark,m;
7256 struct list_head *tmp;
7257 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007258 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007259 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007260 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007261 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
7263 /* just incase thread restarts... */
7264 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7265 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007266 if (mddev->ro) /* never try to sync a read-only array */
7267 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268
NeilBrown61df9d92006-10-03 01:15:57 -07007269 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7270 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7271 desc = "data-check";
7272 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7273 desc = "requested-resync";
7274 else
7275 desc = "resync";
7276 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7277 desc = "reshape";
7278 else
7279 desc = "recovery";
7280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 /* we overload curr_resync somewhat here.
7282 * 0 == not engaged in resync at all
7283 * 2 == checking that there is no conflict with another sync
7284 * 1 == like 2, but have yielded to allow conflicting resync to
7285 * commense
7286 * other == active in resync - this many blocks
7287 *
7288 * Before starting a resync we must have set curr_resync to
7289 * 2, and then checked that every "conflicting" array has curr_resync
7290 * less than ours. When we find one that is the same or higher
7291 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7292 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7293 * This will mean we have to start checking from the beginning again.
7294 *
7295 */
7296
7297 do {
7298 mddev->curr_resync = 2;
7299
7300 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007301 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007302 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007303
7304 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007306 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 if (mddev2 == mddev)
7308 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007309 if (!mddev->parallel_resync
7310 && mddev2->curr_resync
7311 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 DEFINE_WAIT(wq);
7313 if (mddev < mddev2 && mddev->curr_resync == 2) {
7314 /* arbitrarily yield */
7315 mddev->curr_resync = 1;
7316 wake_up(&resync_wait);
7317 }
7318 if (mddev > mddev2 && mddev->curr_resync == 1)
7319 /* no need to wait here, we can wait the next
7320 * time 'round when curr_resync == 2
7321 */
7322 continue;
NeilBrown97441972008-09-19 11:49:54 +10007323 /* We need to wait 'interruptible' so as not to
7324 * contribute to the load average, and not to
7325 * be caught by 'softlockup'
7326 */
7327 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007328 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007329 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007330 printk(KERN_INFO "md: delaying %s of %s"
7331 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007333 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007335 if (signal_pending(current))
7336 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 schedule();
7338 finish_wait(&resync_wait, &wq);
7339 goto try_again;
7340 }
7341 finish_wait(&resync_wait, &wq);
7342 }
7343 }
7344 } while (mddev->curr_resync < 2);
7345
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007346 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007347 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007349 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 */
7351 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007352 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007353 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007354 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7355 j = mddev->resync_min;
7356 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007357 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007358
NeilBrownccfcc3c2006-03-27 01:18:09 -08007359 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007360 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007361 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007363 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007364 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007365 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007366 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007367 if (rdev->raid_disk >= 0 &&
7368 !test_bit(Faulty, &rdev->flags) &&
7369 !test_bit(In_sync, &rdev->flags) &&
7370 rdev->recovery_offset < j)
7371 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007372 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
NeilBrown61df9d92006-10-03 01:15:57 -07007375 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7376 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7377 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007378 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007379 "(but not more than %d KB/sec) for %s.\n",
7380 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
NeilBrowneea1bf32009-03-31 14:27:02 +11007382 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007383
NeilBrown57afd892005-06-21 17:17:13 -07007384 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 for (m = 0; m < SYNC_MARKS; m++) {
7386 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007387 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 }
7389 last_mark = 0;
7390 mddev->resync_mark = mark[last_mark];
7391 mddev->resync_mark_cnt = mark_cnt[last_mark];
7392
7393 /*
7394 * Tune reconstruction:
7395 */
7396 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007397 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7398 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
7400 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 last_check = 0;
7402
7403 if (j>2) {
7404 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007405 "md: resuming %s of %s from checkpoint.\n",
7406 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 mddev->curr_resync = j;
7408 }
NeilBrown75d3da42011-01-14 09:14:34 +11007409 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410
majianpeng7c2c57c2012-07-03 12:12:26 +10007411 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007413 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414
NeilBrown57afd892005-06-21 17:17:13 -07007415 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007416
NeilBrown7a91ee12009-05-26 12:57:21 +10007417 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7418 ((mddev->curr_resync > mddev->curr_resync_completed &&
7419 (mddev->curr_resync - mddev->curr_resync_completed)
7420 > (max_sectors >> 4)) ||
7421 (j - mddev->curr_resync_completed)*2
7422 >= mddev->resync_max - mddev->curr_resync_completed
7423 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007424 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007425 wait_event(mddev->recovery_wait,
7426 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007427 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007428 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007429 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007430 }
NeilBrownacb180b2009-04-14 16:28:34 +10007431
NeilBrowne62e58a2009-07-01 13:15:35 +10007432 while (j >= mddev->resync_max && !kthread_should_stop()) {
7433 /* As this condition is controlled by user-space,
7434 * we can block indefinitely, so use '_interruptible'
7435 * to avoid triggering warnings.
7436 */
7437 flush_signals(current); /* just in case */
7438 wait_event_interruptible(mddev->recovery_wait,
7439 mddev->resync_max > j
7440 || kthread_should_stop());
7441 }
NeilBrownacb180b2009-04-14 16:28:34 +10007442
7443 if (kthread_should_stop())
7444 goto interrupted;
7445
NeilBrown57afd892005-06-21 17:17:13 -07007446 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007447 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007448 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007449 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 goto out;
7451 }
NeilBrown57afd892005-06-21 17:17:13 -07007452
7453 if (!skipped) { /* actual IO requested */
7454 io_sectors += sectors;
7455 atomic_add(sectors, &mddev->recovery_active);
7456 }
7457
NeilBrowne875ece2011-07-28 11:39:24 +10007458 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7459 break;
7460
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 j += sectors;
7462 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007463 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007464 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007465 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007466 * visible in /proc/mdstat
7467 */
7468 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007469
7470 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 continue;
7472
NeilBrown57afd892005-06-21 17:17:13 -07007473 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 repeat:
7475 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7476 /* step marks */
7477 int next = (last_mark+1) % SYNC_MARKS;
7478
7479 mddev->resync_mark = mark[next];
7480 mddev->resync_mark_cnt = mark_cnt[next];
7481 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007482 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 last_mark = next;
7484 }
7485
7486
NeilBrownc6207272008-02-06 01:39:52 -08007487 if (kthread_should_stop())
7488 goto interrupted;
7489
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
7491 /*
7492 * this loop exits only if either when we are slower than
7493 * the 'hard' speed limit, or the system was IO-idle for
7494 * a jiffy.
7495 * the system might be non-idle CPU-wise, but we only care
7496 * about not overloading the IO subsystem. (things like an
7497 * e2fsck being done on the RAID array should execute fast)
7498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 cond_resched();
7500
NeilBrown57afd892005-06-21 17:17:13 -07007501 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7502 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503
NeilBrown88202a02006-01-06 00:21:36 -08007504 if (currspeed > speed_min(mddev)) {
7505 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007506 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007507 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 goto repeat;
7509 }
7510 }
7511 }
NeilBrown61df9d92006-10-03 01:15:57 -07007512 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 /*
7514 * this also signals 'finished resyncing' to md_stop
7515 */
7516 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007517 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7519
7520 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007521 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
NeilBrowndfc70642008-05-23 13:04:39 -07007523 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007524 mddev->curr_resync > 2) {
7525 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7526 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7527 if (mddev->curr_resync >= mddev->recovery_cp) {
7528 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007529 "md: checkpointing %s of %s.\n",
7530 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007531 mddev->recovery_cp =
7532 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007533 }
7534 } else
7535 mddev->recovery_cp = MaxSector;
7536 } else {
7537 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7538 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007539 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007540 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007541 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007542 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007543 !test_bit(Faulty, &rdev->flags) &&
7544 !test_bit(In_sync, &rdev->flags) &&
7545 rdev->recovery_offset < mddev->curr_resync)
7546 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007547 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007550 skip:
NeilBrown17571282006-12-10 02:20:52 -08007551 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
NeilBrownc07b70a2009-12-14 12:49:48 +11007553 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7554 /* We completed so min/max setting can be forgotten if used. */
7555 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7556 mddev->resync_min = 0;
7557 mddev->resync_max = MaxSector;
7558 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7559 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 mddev->curr_resync = 0;
7561 wake_up(&resync_wait);
7562 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7563 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007564 return;
7565
7566 interrupted:
7567 /*
7568 * got a signal, exit.
7569 */
7570 printk(KERN_INFO
7571 "md: md_do_sync() got signal ... exiting\n");
7572 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7573 goto out;
7574
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575}
NeilBrown29269552006-03-27 01:18:10 -08007576EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577
NeilBrownfd01b882011-10-11 16:47:53 +11007578static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007579{
NeilBrown3cb03002011-10-11 16:45:26 +11007580 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007581 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007582 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007583
NeilBrown97e4f422009-03-31 14:33:13 +11007584 mddev->curr_resync_completed = 0;
7585
NeilBrowndafb20f2012-03-19 12:46:39 +11007586 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007587 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007588 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007589 (test_bit(Faulty, &rdev->flags) ||
7590 ! test_bit(In_sync, &rdev->flags)) &&
7591 atomic_read(&rdev->nr_pending)==0) {
7592 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007593 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007594 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007595 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007596 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007597 }
7598 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007599 if (removed)
7600 sysfs_notify(&mddev->kobj, NULL,
7601 "degraded");
7602
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007603
NeilBrowndafb20f2012-03-19 12:46:39 +11007604 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007605 if (rdev->raid_disk >= 0 &&
7606 !test_bit(In_sync, &rdev->flags) &&
7607 !test_bit(Faulty, &rdev->flags))
7608 spares++;
7609 if (rdev->raid_disk < 0
7610 && !test_bit(Faulty, &rdev->flags)) {
7611 rdev->recovery_offset = 0;
7612 if (mddev->pers->
7613 hot_add_disk(mddev, rdev) == 0) {
7614 if (sysfs_link_rdev(mddev, rdev))
7615 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007616 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007617 md_new_event(mddev);
7618 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007619 }
NeilBrowndfc70642008-05-23 13:04:39 -07007620 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007621 }
NeilBrown6dafab62012-09-19 12:54:22 +10007622 if (removed)
7623 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007624 return spares;
7625}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007626
NeilBrownfd01b882011-10-11 16:47:53 +11007627static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007628{
NeilBrown3cb03002011-10-11 16:45:26 +11007629 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007630
7631 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007632 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007633 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7634 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7635 /* success...*/
7636 /* activate any spares */
NeilBrown6dafab62012-09-19 12:54:22 +10007637 if (mddev->pers->spare_active(mddev)) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007638 sysfs_notify(&mddev->kobj, NULL,
7639 "degraded");
NeilBrown6dafab62012-09-19 12:54:22 +10007640 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7641 }
NeilBrown7ebc0be2011-01-14 09:14:33 +11007642 }
7643 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7644 mddev->pers->finish_reshape)
7645 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007646
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007647 /* If array is no-longer degraded, then any saved_raid_disk
7648 * information must be scrapped. Also if any device is now
7649 * In_sync we must scrape the saved_raid_disk for that device
7650 * do the superblock for an incrementally recovered device
7651 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007652 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007653 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007654 if (!mddev->degraded ||
7655 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007656 rdev->saved_raid_disk = -1;
7657
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007658 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007659 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7660 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7661 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7662 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7663 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7664 /* flag recovery needed just to double check */
7665 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7666 sysfs_notify_dirent_safe(mddev->sysfs_action);
7667 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007668 if (mddev->event_work.func)
7669 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007670}
7671
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672/*
7673 * This routine is regularly called by all per-raid-array threads to
7674 * deal with generic issues like resync and super-block update.
7675 * Raid personalities that don't have a thread (linear/raid0) do not
7676 * need this as they never do any recovery or update the superblock.
7677 *
7678 * It does not do any resync itself, but rather "forks" off other threads
7679 * to do that as needed.
7680 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7681 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007682 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 * and wakeups up this thread which will reap the thread and finish up.
7684 * This thread also removes any faulty devices (with nr_pending == 0).
7685 *
7686 * The overall approach is:
7687 * 1/ if the superblock needs updating, update it.
7688 * 2/ If a recovery thread is running, don't do anything else.
7689 * 3/ If recovery has finished, clean up, possibly marking spares active.
7690 * 4/ If there are any faulty devices, remove them.
7691 * 5/ If array is degraded, try to add spares devices
7692 * 6/ If array has spares or is not in-sync, start a resync thread.
7693 */
NeilBrownfd01b882011-10-11 16:47:53 +11007694void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007695{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007696 if (mddev->suspended)
7697 return;
7698
NeilBrown5f404022005-06-21 17:17:16 -07007699 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007700 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
NeilBrownfca4d842005-06-21 17:17:11 -07007702 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007703 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007704 printk(KERN_INFO "md: %s in immediate safe mode\n",
7705 mdname(mddev));
7706 mddev->safemode = 2;
7707 }
7708 flush_signals(current);
7709 }
7710
NeilBrownc89a8ee2008-08-05 15:54:13 +10007711 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007714 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007716 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007717 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007718 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7719 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 ))
7721 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007722
NeilBrowndf5b89b2006-03-27 01:18:20 -08007723 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007724 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007725
NeilBrownc89a8ee2008-08-05 15:54:13 +10007726 if (mddev->ro) {
7727 /* Only thing we do on a ro array is remove
7728 * failed devices.
7729 */
NeilBrown3cb03002011-10-11 16:45:26 +11007730 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007731 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007732 if (rdev->raid_disk >= 0 &&
7733 !test_bit(Blocked, &rdev->flags) &&
7734 test_bit(Faulty, &rdev->flags) &&
7735 atomic_read(&rdev->nr_pending)==0) {
7736 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007737 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007738 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007739 rdev->raid_disk = -1;
7740 }
7741 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007742 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7743 goto unlock;
7744 }
7745
NeilBrown31a59e32008-04-30 00:52:30 -07007746 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007747 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007748 spin_lock_irq(&mddev->write_lock);
7749 if (mddev->safemode &&
7750 !atomic_read(&mddev->writes_pending) &&
7751 !mddev->in_sync &&
7752 mddev->recovery_cp == MaxSector) {
7753 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007754 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007755 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007756 }
7757 if (mddev->safemode == 1)
7758 mddev->safemode = 0;
7759 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007760 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007761 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007762 }
NeilBrownfca4d842005-06-21 17:17:11 -07007763
NeilBrown850b2b42006-10-03 01:15:46 -07007764 if (mddev->flags)
7765 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007766
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7768 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7769 /* resync/recovery still happening */
7770 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7771 goto unlock;
7772 }
7773 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007774 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 goto unlock;
7776 }
Neil Brown72a23c22008-06-28 08:31:41 +10007777 /* Set RUNNING before clearing NEEDED to avoid
7778 * any transients in the value of "sync_action".
7779 */
7780 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007781 /* Clear some bits that don't mean anything, but
7782 * might be left set
7783 */
NeilBrown24dd4692005-11-08 21:39:26 -08007784 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7785 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786
NeilBrowned209582012-04-24 10:23:14 +10007787 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7788 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007789 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 /* no recovery is running.
7791 * remove any failed drives, then
7792 * add spares if possible.
7793 * Spare are also removed and re-added, to allow
7794 * the personality to fail the re-add.
7795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007797 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007798 if (mddev->pers->check_reshape == NULL ||
7799 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007800 /* Cannot proceed */
7801 goto unlock;
7802 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007803 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007804 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007805 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7806 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007807 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007808 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007809 } else if (mddev->recovery_cp < MaxSector) {
7810 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007811 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007812 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7813 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007815
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007817 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007818 /* We are adding a device or devices to an array
7819 * which has the bitmap stored on all devices.
7820 * So make sure all bitmap pages get written
7821 */
7822 bitmap_write_all(mddev->bitmap);
7823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 mddev->sync_thread = md_register_thread(md_do_sync,
7825 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007826 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827 if (!mddev->sync_thread) {
7828 printk(KERN_ERR "%s: could not start resync"
7829 " thread...\n",
7830 mdname(mddev));
7831 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007832 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7833 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7834 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7835 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7836 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007837 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007839 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007840 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 }
7842 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007843 if (!mddev->sync_thread) {
7844 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7845 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7846 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007847 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007848 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 mddev_unlock(mddev);
7851 }
7852}
7853
NeilBrownfd01b882011-10-11 16:47:53 +11007854void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007855{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007856 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007857 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007858 !test_bit(Blocked, &rdev->flags) &&
7859 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007860 msecs_to_jiffies(5000));
7861 rdev_dec_pending(rdev, mddev);
7862}
7863EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7864
NeilBrownc6563a82012-05-21 09:27:00 +10007865void md_finish_reshape(struct mddev *mddev)
7866{
7867 /* called be personality module when reshape completes. */
7868 struct md_rdev *rdev;
7869
7870 rdev_for_each(rdev, mddev) {
7871 if (rdev->data_offset > rdev->new_data_offset)
7872 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7873 else
7874 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7875 rdev->data_offset = rdev->new_data_offset;
7876 }
7877}
7878EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007879
7880/* Bad block management.
7881 * We can record which blocks on each device are 'bad' and so just
7882 * fail those blocks, or that stripe, rather than the whole device.
7883 * Entries in the bad-block table are 64bits wide. This comprises:
7884 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7885 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7886 * A 'shift' can be set so that larger blocks are tracked and
7887 * consequently larger devices can be covered.
7888 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7889 *
7890 * Locking of the bad-block table uses a seqlock so md_is_badblock
7891 * might need to retry if it is very unlucky.
7892 * We will sometimes want to check for bad blocks in a bi_end_io function,
7893 * so we use the write_seqlock_irq variant.
7894 *
7895 * When looking for a bad block we specify a range and want to
7896 * know if any block in the range is bad. So we binary-search
7897 * to the last range that starts at-or-before the given endpoint,
7898 * (or "before the sector after the target range")
7899 * then see if it ends after the given start.
7900 * We return
7901 * 0 if there are no known bad blocks in the range
7902 * 1 if there are known bad block which are all acknowledged
7903 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7904 * plus the start/length of the first bad section we overlap.
7905 */
7906int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7907 sector_t *first_bad, int *bad_sectors)
7908{
7909 int hi;
7910 int lo = 0;
7911 u64 *p = bb->page;
7912 int rv = 0;
7913 sector_t target = s + sectors;
7914 unsigned seq;
7915
7916 if (bb->shift > 0) {
7917 /* round the start down, and the end up */
7918 s >>= bb->shift;
7919 target += (1<<bb->shift) - 1;
7920 target >>= bb->shift;
7921 sectors = target - s;
7922 }
7923 /* 'target' is now the first block after the bad range */
7924
7925retry:
7926 seq = read_seqbegin(&bb->lock);
7927
7928 hi = bb->count;
7929
7930 /* Binary search between lo and hi for 'target'
7931 * i.e. for the last range that starts before 'target'
7932 */
7933 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7934 * are known not to be the last range before target.
7935 * VARIANT: hi-lo is the number of possible
7936 * ranges, and decreases until it reaches 1
7937 */
7938 while (hi - lo > 1) {
7939 int mid = (lo + hi) / 2;
7940 sector_t a = BB_OFFSET(p[mid]);
7941 if (a < target)
7942 /* This could still be the one, earlier ranges
7943 * could not. */
7944 lo = mid;
7945 else
7946 /* This and later ranges are definitely out. */
7947 hi = mid;
7948 }
7949 /* 'lo' might be the last that started before target, but 'hi' isn't */
7950 if (hi > lo) {
7951 /* need to check all range that end after 's' to see if
7952 * any are unacknowledged.
7953 */
7954 while (lo >= 0 &&
7955 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7956 if (BB_OFFSET(p[lo]) < target) {
7957 /* starts before the end, and finishes after
7958 * the start, so they must overlap
7959 */
7960 if (rv != -1 && BB_ACK(p[lo]))
7961 rv = 1;
7962 else
7963 rv = -1;
7964 *first_bad = BB_OFFSET(p[lo]);
7965 *bad_sectors = BB_LEN(p[lo]);
7966 }
7967 lo--;
7968 }
7969 }
7970
7971 if (read_seqretry(&bb->lock, seq))
7972 goto retry;
7973
7974 return rv;
7975}
7976EXPORT_SYMBOL_GPL(md_is_badblock);
7977
7978/*
7979 * Add a range of bad blocks to the table.
7980 * This might extend the table, or might contract it
7981 * if two adjacent ranges can be merged.
7982 * We binary-search to find the 'insertion' point, then
7983 * decide how best to handle it.
7984 */
7985static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7986 int acknowledged)
7987{
7988 u64 *p;
7989 int lo, hi;
7990 int rv = 1;
7991
7992 if (bb->shift < 0)
7993 /* badblocks are disabled */
7994 return 0;
7995
7996 if (bb->shift) {
7997 /* round the start down, and the end up */
7998 sector_t next = s + sectors;
7999 s >>= bb->shift;
8000 next += (1<<bb->shift) - 1;
8001 next >>= bb->shift;
8002 sectors = next - s;
8003 }
8004
8005 write_seqlock_irq(&bb->lock);
8006
8007 p = bb->page;
8008 lo = 0;
8009 hi = bb->count;
8010 /* Find the last range that starts at-or-before 's' */
8011 while (hi - lo > 1) {
8012 int mid = (lo + hi) / 2;
8013 sector_t a = BB_OFFSET(p[mid]);
8014 if (a <= s)
8015 lo = mid;
8016 else
8017 hi = mid;
8018 }
8019 if (hi > lo && BB_OFFSET(p[lo]) > s)
8020 hi = lo;
8021
8022 if (hi > lo) {
8023 /* we found a range that might merge with the start
8024 * of our new range
8025 */
8026 sector_t a = BB_OFFSET(p[lo]);
8027 sector_t e = a + BB_LEN(p[lo]);
8028 int ack = BB_ACK(p[lo]);
8029 if (e >= s) {
8030 /* Yes, we can merge with a previous range */
8031 if (s == a && s + sectors >= e)
8032 /* new range covers old */
8033 ack = acknowledged;
8034 else
8035 ack = ack && acknowledged;
8036
8037 if (e < s + sectors)
8038 e = s + sectors;
8039 if (e - a <= BB_MAX_LEN) {
8040 p[lo] = BB_MAKE(a, e-a, ack);
8041 s = e;
8042 } else {
8043 /* does not all fit in one range,
8044 * make p[lo] maximal
8045 */
8046 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8047 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8048 s = a + BB_MAX_LEN;
8049 }
8050 sectors = e - s;
8051 }
8052 }
8053 if (sectors && hi < bb->count) {
8054 /* 'hi' points to the first range that starts after 's'.
8055 * Maybe we can merge with the start of that range */
8056 sector_t a = BB_OFFSET(p[hi]);
8057 sector_t e = a + BB_LEN(p[hi]);
8058 int ack = BB_ACK(p[hi]);
8059 if (a <= s + sectors) {
8060 /* merging is possible */
8061 if (e <= s + sectors) {
8062 /* full overlap */
8063 e = s + sectors;
8064 ack = acknowledged;
8065 } else
8066 ack = ack && acknowledged;
8067
8068 a = s;
8069 if (e - a <= BB_MAX_LEN) {
8070 p[hi] = BB_MAKE(a, e-a, ack);
8071 s = e;
8072 } else {
8073 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8074 s = a + BB_MAX_LEN;
8075 }
8076 sectors = e - s;
8077 lo = hi;
8078 hi++;
8079 }
8080 }
8081 if (sectors == 0 && hi < bb->count) {
8082 /* we might be able to combine lo and hi */
8083 /* Note: 's' is at the end of 'lo' */
8084 sector_t a = BB_OFFSET(p[hi]);
8085 int lolen = BB_LEN(p[lo]);
8086 int hilen = BB_LEN(p[hi]);
8087 int newlen = lolen + hilen - (s - a);
8088 if (s >= a && newlen < BB_MAX_LEN) {
8089 /* yes, we can combine them */
8090 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8091 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8092 memmove(p + hi, p + hi + 1,
8093 (bb->count - hi - 1) * 8);
8094 bb->count--;
8095 }
8096 }
8097 while (sectors) {
8098 /* didn't merge (it all).
8099 * Need to add a range just before 'hi' */
8100 if (bb->count >= MD_MAX_BADBLOCKS) {
8101 /* No room for more */
8102 rv = 0;
8103 break;
8104 } else {
8105 int this_sectors = sectors;
8106 memmove(p + hi + 1, p + hi,
8107 (bb->count - hi) * 8);
8108 bb->count++;
8109
8110 if (this_sectors > BB_MAX_LEN)
8111 this_sectors = BB_MAX_LEN;
8112 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8113 sectors -= this_sectors;
8114 s += this_sectors;
8115 }
8116 }
8117
8118 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008119 if (!acknowledged)
8120 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008121 write_sequnlock_irq(&bb->lock);
8122
8123 return rv;
8124}
8125
NeilBrown3cb03002011-10-11 16:45:26 +11008126int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008127 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008128{
NeilBrownc6563a82012-05-21 09:27:00 +10008129 int rv;
8130 if (is_new)
8131 s += rdev->new_data_offset;
8132 else
8133 s += rdev->data_offset;
8134 rv = md_set_badblocks(&rdev->badblocks,
8135 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008136 if (rv) {
8137 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008138 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008139 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8140 md_wakeup_thread(rdev->mddev->thread);
8141 }
8142 return rv;
8143}
8144EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8145
8146/*
8147 * Remove a range of bad blocks from the table.
8148 * This may involve extending the table if we spilt a region,
8149 * but it must not fail. So if the table becomes full, we just
8150 * drop the remove request.
8151 */
8152static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8153{
8154 u64 *p;
8155 int lo, hi;
8156 sector_t target = s + sectors;
8157 int rv = 0;
8158
8159 if (bb->shift > 0) {
8160 /* When clearing we round the start up and the end down.
8161 * This should not matter as the shift should align with
8162 * the block size and no rounding should ever be needed.
8163 * However it is better the think a block is bad when it
8164 * isn't than to think a block is not bad when it is.
8165 */
8166 s += (1<<bb->shift) - 1;
8167 s >>= bb->shift;
8168 target >>= bb->shift;
8169 sectors = target - s;
8170 }
8171
8172 write_seqlock_irq(&bb->lock);
8173
8174 p = bb->page;
8175 lo = 0;
8176 hi = bb->count;
8177 /* Find the last range that starts before 'target' */
8178 while (hi - lo > 1) {
8179 int mid = (lo + hi) / 2;
8180 sector_t a = BB_OFFSET(p[mid]);
8181 if (a < target)
8182 lo = mid;
8183 else
8184 hi = mid;
8185 }
8186 if (hi > lo) {
8187 /* p[lo] is the last range that could overlap the
8188 * current range. Earlier ranges could also overlap,
8189 * but only this one can overlap the end of the range.
8190 */
8191 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8192 /* Partial overlap, leave the tail of this range */
8193 int ack = BB_ACK(p[lo]);
8194 sector_t a = BB_OFFSET(p[lo]);
8195 sector_t end = a + BB_LEN(p[lo]);
8196
8197 if (a < s) {
8198 /* we need to split this range */
8199 if (bb->count >= MD_MAX_BADBLOCKS) {
8200 rv = 0;
8201 goto out;
8202 }
8203 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8204 bb->count++;
8205 p[lo] = BB_MAKE(a, s-a, ack);
8206 lo++;
8207 }
8208 p[lo] = BB_MAKE(target, end - target, ack);
8209 /* there is no longer an overlap */
8210 hi = lo;
8211 lo--;
8212 }
8213 while (lo >= 0 &&
8214 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8215 /* This range does overlap */
8216 if (BB_OFFSET(p[lo]) < s) {
8217 /* Keep the early parts of this range. */
8218 int ack = BB_ACK(p[lo]);
8219 sector_t start = BB_OFFSET(p[lo]);
8220 p[lo] = BB_MAKE(start, s - start, ack);
8221 /* now low doesn't overlap, so.. */
8222 break;
8223 }
8224 lo--;
8225 }
8226 /* 'lo' is strictly before, 'hi' is strictly after,
8227 * anything between needs to be discarded
8228 */
8229 if (hi - lo > 1) {
8230 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8231 bb->count -= (hi - lo - 1);
8232 }
8233 }
8234
8235 bb->changed = 1;
8236out:
8237 write_sequnlock_irq(&bb->lock);
8238 return rv;
8239}
8240
NeilBrownc6563a82012-05-21 09:27:00 +10008241int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8242 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008243{
NeilBrownc6563a82012-05-21 09:27:00 +10008244 if (is_new)
8245 s += rdev->new_data_offset;
8246 else
8247 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008248 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008249 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008250}
8251EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8252
8253/*
8254 * Acknowledge all bad blocks in a list.
8255 * This only succeeds if ->changed is clear. It is used by
8256 * in-kernel metadata updates
8257 */
8258void md_ack_all_badblocks(struct badblocks *bb)
8259{
8260 if (bb->page == NULL || bb->changed)
8261 /* no point even trying */
8262 return;
8263 write_seqlock_irq(&bb->lock);
8264
majianpengecb178b2012-03-19 12:46:42 +11008265 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008266 u64 *p = bb->page;
8267 int i;
8268 for (i = 0; i < bb->count ; i++) {
8269 if (!BB_ACK(p[i])) {
8270 sector_t start = BB_OFFSET(p[i]);
8271 int len = BB_LEN(p[i]);
8272 p[i] = BB_MAKE(start, len, 1);
8273 }
8274 }
NeilBrownde393cd2011-07-28 11:31:48 +10008275 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008276 }
8277 write_sequnlock_irq(&bb->lock);
8278}
8279EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8280
NeilBrown16c791a2011-07-28 11:31:47 +10008281/* sysfs access to bad-blocks list.
8282 * We present two files.
8283 * 'bad-blocks' lists sector numbers and lengths of ranges that
8284 * are recorded as bad. The list is truncated to fit within
8285 * the one-page limit of sysfs.
8286 * Writing "sector length" to this file adds an acknowledged
8287 * bad block list.
8288 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8289 * been acknowledged. Writing to this file adds bad blocks
8290 * without acknowledging them. This is largely for testing.
8291 */
8292
8293static ssize_t
8294badblocks_show(struct badblocks *bb, char *page, int unack)
8295{
8296 size_t len;
8297 int i;
8298 u64 *p = bb->page;
8299 unsigned seq;
8300
8301 if (bb->shift < 0)
8302 return 0;
8303
8304retry:
8305 seq = read_seqbegin(&bb->lock);
8306
8307 len = 0;
8308 i = 0;
8309
8310 while (len < PAGE_SIZE && i < bb->count) {
8311 sector_t s = BB_OFFSET(p[i]);
8312 unsigned int length = BB_LEN(p[i]);
8313 int ack = BB_ACK(p[i]);
8314 i++;
8315
8316 if (unack && ack)
8317 continue;
8318
8319 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8320 (unsigned long long)s << bb->shift,
8321 length << bb->shift);
8322 }
NeilBrownde393cd2011-07-28 11:31:48 +10008323 if (unack && len == 0)
8324 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008325
8326 if (read_seqretry(&bb->lock, seq))
8327 goto retry;
8328
8329 return len;
8330}
8331
8332#define DO_DEBUG 1
8333
8334static ssize_t
8335badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8336{
8337 unsigned long long sector;
8338 int length;
8339 char newline;
8340#ifdef DO_DEBUG
8341 /* Allow clearing via sysfs *only* for testing/debugging.
8342 * Normally only a successful write may clear a badblock
8343 */
8344 int clear = 0;
8345 if (page[0] == '-') {
8346 clear = 1;
8347 page++;
8348 }
8349#endif /* DO_DEBUG */
8350
8351 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8352 case 3:
8353 if (newline != '\n')
8354 return -EINVAL;
8355 case 2:
8356 if (length <= 0)
8357 return -EINVAL;
8358 break;
8359 default:
8360 return -EINVAL;
8361 }
8362
8363#ifdef DO_DEBUG
8364 if (clear) {
8365 md_clear_badblocks(bb, sector, length);
8366 return len;
8367 }
8368#endif /* DO_DEBUG */
8369 if (md_set_badblocks(bb, sector, length, !unack))
8370 return len;
8371 else
8372 return -ENOSPC;
8373}
8374
Adrian Bunk75c96f82005-05-05 16:16:09 -07008375static int md_notify_reboot(struct notifier_block *this,
8376 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377{
8378 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008379 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008380 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
NeilBrownc744a652012-03-19 12:46:37 +11008382 for_each_mddev(mddev, tmp) {
8383 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008384 if (mddev->pers)
8385 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008386 mddev->safemode = 2;
8387 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008388 }
NeilBrownc744a652012-03-19 12:46:37 +11008389 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 }
NeilBrownc744a652012-03-19 12:46:37 +11008391 /*
8392 * certain more exotic SCSI devices are known to be
8393 * volatile wrt too early system reboots. While the
8394 * right place to handle this issue is the given
8395 * driver, we do want to have a safe RAID driver ...
8396 */
8397 if (need_delay)
8398 mdelay(1000*1);
8399
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 return NOTIFY_DONE;
8401}
8402
Adrian Bunk75c96f82005-05-05 16:16:09 -07008403static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 .notifier_call = md_notify_reboot,
8405 .next = NULL,
8406 .priority = INT_MAX, /* before any real devices */
8407};
8408
8409static void md_geninit(void)
8410{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008411 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008413 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414}
8415
Adrian Bunk75c96f82005-05-05 16:16:09 -07008416static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417{
Tejun Heoe804ac72010-10-15 15:36:08 +02008418 int ret = -ENOMEM;
8419
Tejun Heoada609e2011-01-25 14:35:54 +01008420 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008421 if (!md_wq)
8422 goto err_wq;
8423
8424 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8425 if (!md_misc_wq)
8426 goto err_misc_wq;
8427
8428 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8429 goto err_md;
8430
8431 if ((ret = register_blkdev(0, "mdp")) < 0)
8432 goto err_mdp;
8433 mdp_major = ret;
8434
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008435 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008436 md_probe, NULL, NULL);
8437 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 md_probe, NULL, NULL);
8439
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008441 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
8443 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445
Tejun Heoe804ac72010-10-15 15:36:08 +02008446err_mdp:
8447 unregister_blkdev(MD_MAJOR, "md");
8448err_md:
8449 destroy_workqueue(md_misc_wq);
8450err_misc_wq:
8451 destroy_workqueue(md_wq);
8452err_wq:
8453 return ret;
8454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455
8456#ifndef MODULE
8457
8458/*
8459 * Searches all registered partitions for autorun RAID arrays
8460 * at boot time.
8461 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008462
8463static LIST_HEAD(all_detected_devices);
8464struct detected_devices_node {
8465 struct list_head list;
8466 dev_t dev;
8467};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468
8469void md_autodetect_dev(dev_t dev)
8470{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008471 struct detected_devices_node *node_detected_dev;
8472
8473 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8474 if (node_detected_dev) {
8475 node_detected_dev->dev = dev;
8476 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8477 } else {
8478 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8479 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481}
8482
8483
8484static void autostart_arrays(int part)
8485{
NeilBrown3cb03002011-10-11 16:45:26 +11008486 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008487 struct detected_devices_node *node_detected_dev;
8488 dev_t dev;
8489 int i_scanned, i_passed;
8490
8491 i_scanned = 0;
8492 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493
8494 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8495
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008496 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8497 i_scanned++;
8498 node_detected_dev = list_entry(all_detected_devices.next,
8499 struct detected_devices_node, list);
8500 list_del(&node_detected_dev->list);
8501 dev = node_detected_dev->dev;
8502 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008503 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 if (IS_ERR(rdev))
8505 continue;
8506
NeilBrownb2d444d2005-11-08 21:39:31 -08008507 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 MD_BUG();
8509 continue;
8510 }
NeilBrownd0fae182008-03-04 14:29:31 -08008511 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008512 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008513 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008515
8516 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8517 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008518
8519 autorun_devices(part);
8520}
8521
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008522#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523
8524static __exit void md_exit(void)
8525{
NeilBrownfd01b882011-10-11 16:47:53 +11008526 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008528
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008529 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008530 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008532 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 unregister_blkdev(mdp_major, "mdp");
8534 unregister_reboot_notifier(&md_notifier);
8535 unregister_sysctl_table(raid_table_header);
8536 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008537 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008539 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008541 destroy_workqueue(md_misc_wq);
8542 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543}
8544
Dan Williams685784a2007-07-09 11:56:42 -07008545subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546module_exit(md_exit)
8547
NeilBrownf91de922005-11-08 21:39:36 -08008548static int get_ro(char *buffer, struct kernel_param *kp)
8549{
8550 return sprintf(buffer, "%d", start_readonly);
8551}
8552static int set_ro(const char *val, struct kernel_param *kp)
8553{
8554 char *e;
8555 int num = simple_strtoul(val, &e, 10);
8556 if (*val && (*e == '\0' || *e == '\n')) {
8557 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008558 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008559 }
8560 return -EINVAL;
8561}
8562
NeilBrown80ca3a42006-07-10 04:44:18 -07008563module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8564module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008565
NeilBrownefeb53c2009-01-09 08:31:10 +11008566module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008567
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568EXPORT_SYMBOL(register_md_personality);
8569EXPORT_SYMBOL(unregister_md_personality);
8570EXPORT_SYMBOL(md_error);
8571EXPORT_SYMBOL(md_done_sync);
8572EXPORT_SYMBOL(md_write_start);
8573EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574EXPORT_SYMBOL(md_register_thread);
8575EXPORT_SYMBOL(md_unregister_thread);
8576EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577EXPORT_SYMBOL(md_check_recovery);
8578MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008579MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008580MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008581MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);