blob: b493fa4173875e667d218fcd7b32eec7baf83913 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
158static void mddev_bio_destructor(struct bio *bio)
159{
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 mddevp = (void*)bio;
163 mddev = mddevp[-1];
164
165 bio_free(bio, mddev->bio_set);
166}
167
168struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100169 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100170{
171 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100172 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100173
174 if (!mddev || !mddev->bio_set)
175 return bio_alloc(gfp_mask, nr_iovecs);
176
177 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
178 mddev->bio_set);
179 if (!b)
180 return NULL;
181 mddevp = (void*)b;
182 mddevp[-1] = mddev;
183 b->bi_destructor = mddev_bio_destructor;
184 return b;
185}
186EXPORT_SYMBOL_GPL(bio_alloc_mddev);
187
188struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100189 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100190{
191 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100193
194 if (!mddev || !mddev->bio_set)
195 return bio_clone(bio, gfp_mask);
196
197 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
198 mddev->bio_set);
199 if (!b)
200 return NULL;
201 mddevp = (void*)b;
202 mddevp[-1] = mddev;
203 b->bi_destructor = mddev_bio_destructor;
204 __bio_clone(b, bio);
205 if (bio_integrity(bio)) {
206 int ret;
207
208 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
209
210 if (ret < 0) {
211 bio_put(b);
212 return NULL;
213 }
214 }
215
216 return b;
217}
218EXPORT_SYMBOL_GPL(bio_clone_mddev);
219
NeilBrownd2eb35a2011-07-28 11:31:48 +1000220void md_trim_bio(struct bio *bio, int offset, int size)
221{
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 */
226 int i;
227 struct bio_vec *bvec;
228 int sofar = 0;
229
230 size <<= 9;
231 if (offset == 0 && size == bio->bi_size)
232 return;
233
234 bio->bi_sector += offset;
235 bio->bi_size = size;
236 offset <<= 9;
237 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
238
239 while (bio->bi_idx < bio->bi_vcnt &&
240 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
241 /* remove this whole bio_vec */
242 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
243 bio->bi_idx++;
244 }
245 if (bio->bi_idx < bio->bi_vcnt) {
246 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
247 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
248 }
249 /* avoid any complications with bi_idx being non-zero*/
250 if (bio->bi_idx) {
251 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
252 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
253 bio->bi_vcnt -= bio->bi_idx;
254 bio->bi_idx = 0;
255 }
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec, bio, i) {
258 if (sofar + bvec->bv_len > size)
259 bvec->bv_len = size - sofar;
260 if (bvec->bv_len == 0) {
261 bio->bi_vcnt = i;
262 break;
263 }
264 sofar += bvec->bv_len;
265 }
266}
267EXPORT_SYMBOL_GPL(md_trim_bio);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
NeilBrownd7603b72006-01-06 00:20:30 -0800270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
273 * count increases.
274 *
275 * Events are:
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
278 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800279static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800280static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100281void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800282{
283 atomic_inc(&md_event_count);
284 wake_up(&md_event_waiters);
285}
NeilBrown29269552006-03-27 01:18:10 -0800286EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800287
NeilBrownc331eb02006-05-30 21:27:13 -0700288/* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
290 */
NeilBrownfd01b882011-10-11 16:47:53 +1100291static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700292{
293 atomic_inc(&md_event_count);
294 wake_up(&md_event_waiters);
295}
296
NeilBrownd7603b72006-01-06 00:20:30 -0800297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
300 */
301static LIST_HEAD(all_mddevs);
302static DEFINE_SPINLOCK(all_mddevs_lock);
303
304
305/*
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 \
314 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100315 _tmp = all_mddevs.next; \
316 _mddev = NULL;}); \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100324 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 )
326
327
NeilBrown409c57f2009-03-31 14:39:39 +1100328/* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200335static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
NeilBrown49077322010-03-25 16:20:56 +1100337 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100338 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100339 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500340 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100341
NeilBrown0ca69882011-01-14 09:14:33 +1100342 if (mddev == NULL || mddev->pers == NULL
343 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100344 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200345 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100346 }
NeilBrown0ca69882011-01-14 09:14:33 +1100347 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100348 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200349 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100350 DEFINE_WAIT(__wait);
351 for (;;) {
352 prepare_to_wait(&mddev->sb_wait, &__wait,
353 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200354 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100355 break;
356 rcu_read_unlock();
357 schedule();
358 rcu_read_lock();
359 }
360 finish_wait(&mddev->sb_wait, &__wait);
361 }
362 atomic_inc(&mddev->active_io);
363 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100364
Chris Masone91ece52011-02-07 19:21:48 -0500365 /*
366 * save the sectors now since our bio can
367 * go away inside make_request
368 */
369 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200370 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100371
372 cpu = part_stat_lock();
373 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500374 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100375 part_stat_unlock();
376
NeilBrown409c57f2009-03-31 14:39:39 +1100377 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
378 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
NeilBrown9e35b992010-04-06 14:23:02 +1000381/* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
385 * unused.
386 */
NeilBrownfd01b882011-10-11 16:47:53 +1100387void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100388{
389 BUG_ON(mddev->suspended);
390 mddev->suspended = 1;
391 synchronize_rcu();
392 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
393 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500394
395 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100396}
NeilBrown390ee602010-06-01 19:37:27 +1000397EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100398
NeilBrownfd01b882011-10-11 16:47:53 +1100399void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100400{
401 mddev->suspended = 0;
402 wake_up(&mddev->sb_wait);
403 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500404
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
NeilBrown9cbb1752012-07-31 09:08:14 +0200501void md_unplug(struct blk_plug_cb *cb)
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;
NeilBrown27a7b262011-09-10 17:21:28 +10001111 /* Limit to 4TB as metadata cannot record more than that */
1112 if (rdev->sectors >= (2ULL << 32))
1113 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
NeilBrown27a7b262011-09-10 17:21:28 +10001115 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001116 /* "this cannot possibly happen" ... */
1117 ret = -EINVAL;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 abort:
1120 return ret;
1121}
1122
1123/*
1124 * validate_super for 0.90.0
1125 */
NeilBrownfd01b882011-10-11 16:47:53 +11001126static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001129 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001130 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
NeilBrown41158c72005-06-21 17:17:25 -07001132 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001133 clear_bit(Faulty, &rdev->flags);
1134 clear_bit(In_sync, &rdev->flags);
1135 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (mddev->raid_disks == 0) {
1138 mddev->major_version = 0;
1139 mddev->minor_version = sb->minor_version;
1140 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001141 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001142 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 mddev->ctime = sb->ctime;
1144 mddev->utime = sb->utime;
1145 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001146 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 mddev->layout = sb->layout;
1148 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001149 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001150 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001151 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001152 mddev->bitmap_info.space = 0;
1153 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001154 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001155 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001156 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
NeilBrownf6705572006-03-27 01:18:11 -08001158 if (mddev->minor_version >= 91) {
1159 mddev->reshape_position = sb->reshape_position;
1160 mddev->delta_disks = sb->delta_disks;
1161 mddev->new_level = sb->new_level;
1162 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001163 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001164 if (mddev->delta_disks < 0)
1165 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001166 } else {
1167 mddev->reshape_position = MaxSector;
1168 mddev->delta_disks = 0;
1169 mddev->new_level = mddev->level;
1170 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001171 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001172 }
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (sb->state & (1<<MD_SB_CLEAN))
1175 mddev->recovery_cp = MaxSector;
1176 else {
1177 if (sb->events_hi == sb->cp_events_hi &&
1178 sb->events_lo == sb->cp_events_lo) {
1179 mddev->recovery_cp = sb->recovery_cp;
1180 } else
1181 mddev->recovery_cp = 0;
1182 }
1183
1184 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1185 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1186 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1187 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1188
1189 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001190
1191 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001192 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001193 mddev->bitmap_info.offset =
1194 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001195 mddev->bitmap_info.space =
1196 mddev->bitmap_info.space;
1197 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001198
NeilBrown41158c72005-06-21 17:17:25 -07001199 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001200 /* Insist on good event counter while assembling, except
1201 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001203 if (sb->disks[rdev->desc_nr].state & (
1204 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1205 if (ev1 < mddev->events)
1206 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001207 } else if (mddev->bitmap) {
1208 /* if adding to array with a bitmap, then we can accept an
1209 * older device ... but not too old.
1210 */
NeilBrown41158c72005-06-21 17:17:25 -07001211 if (ev1 < mddev->bitmap->events_cleared)
1212 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001213 } else {
1214 if (ev1 < mddev->events)
1215 /* just a hot-add of a new device, leave raid_disk at -1 */
1216 return 0;
1217 }
NeilBrown41158c72005-06-21 17:17:25 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 desc = sb->disks + rdev->desc_nr;
1221
1222 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001223 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001224 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1225 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001226 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001228 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1229 /* active but not in sync implies recovery up to
1230 * reshape position. We don't know exactly where
1231 * that is, so set to zero for now */
1232 if (mddev->minor_version >= 91) {
1233 rdev->recovery_offset = 0;
1234 rdev->raid_disk = desc->raid_disk;
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001237 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1238 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001239 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001240 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
1244/*
1245 * sync_super for 0.90.0
1246 */
NeilBrownfd01b882011-10-11 16:47:53 +11001247static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001250 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* make rdev->sb match mddev data..
1255 *
1256 * 1/ zero out disks
1257 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1258 * 3/ any empty disks < next_spare become removed
1259 *
1260 * disks[0] gets initialised to REMOVED because
1261 * we cannot be sure from other fields if it has
1262 * been initialised or not.
1263 */
1264 int i;
1265 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1266
NeilBrown61181562005-09-09 16:24:02 -07001267 rdev->sb_size = MD_SB_BYTES;
1268
Namhyung Kim65a06f062011-07-27 11:00:36 +10001269 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 memset(sb, 0, sizeof(*sb));
1272
1273 sb->md_magic = MD_SB_MAGIC;
1274 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sb->patch_version = mddev->patch_version;
1276 sb->gvalid_words = 0; /* ignored */
1277 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1278 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1279 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1280 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1281
1282 sb->ctime = mddev->ctime;
1283 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001284 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 sb->raid_disks = mddev->raid_disks;
1286 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001287 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sb->utime = mddev->utime;
1289 sb->state = 0;
1290 sb->events_hi = (mddev->events>>32);
1291 sb->events_lo = (u32)mddev->events;
1292
NeilBrownf6705572006-03-27 01:18:11 -08001293 if (mddev->reshape_position == MaxSector)
1294 sb->minor_version = 90;
1295 else {
1296 sb->minor_version = 91;
1297 sb->reshape_position = mddev->reshape_position;
1298 sb->new_level = mddev->new_level;
1299 sb->delta_disks = mddev->delta_disks;
1300 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001301 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001302 }
1303 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (mddev->in_sync)
1305 {
1306 sb->recovery_cp = mddev->recovery_cp;
1307 sb->cp_events_hi = (mddev->events>>32);
1308 sb->cp_events_lo = (u32)mddev->events;
1309 if (mddev->recovery_cp == MaxSector)
1310 sb->state = (1<< MD_SB_CLEAN);
1311 } else
1312 sb->recovery_cp = 0;
1313
1314 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001315 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
NeilBrownc3d97142009-12-14 12:49:52 +11001317 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001318 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001321 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001323 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001324 int is_active = test_bit(In_sync, &rdev2->flags);
1325
1326 if (rdev2->raid_disk >= 0 &&
1327 sb->minor_version >= 91)
1328 /* we have nowhere to store the recovery_offset,
1329 * but if it is not below the reshape_position,
1330 * we can piggy-back on that.
1331 */
1332 is_active = 1;
1333 if (rdev2->raid_disk < 0 ||
1334 test_bit(Faulty, &rdev2->flags))
1335 is_active = 0;
1336 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001337 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001339 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001340 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 d = &sb->disks[rdev2->desc_nr];
1342 nr_disks++;
1343 d->number = rdev2->desc_nr;
1344 d->major = MAJOR(rdev2->bdev->bd_dev);
1345 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001346 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 d->raid_disk = rdev2->raid_disk;
1348 else
1349 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001350 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001352 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001354 if (test_bit(In_sync, &rdev2->flags))
1355 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 active++;
1357 working++;
1358 } else {
1359 d->state = 0;
1360 spare++;
1361 working++;
1362 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001363 if (test_bit(WriteMostly, &rdev2->flags))
1364 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* now set the "removed" and "faulty" bits on any missing devices */
1367 for (i=0 ; i < mddev->raid_disks ; i++) {
1368 mdp_disk_t *d = &sb->disks[i];
1369 if (d->state == 0 && d->number == 0) {
1370 d->number = i;
1371 d->raid_disk = i;
1372 d->state = (1<<MD_DISK_REMOVED);
1373 d->state |= (1<<MD_DISK_FAULTY);
1374 failed++;
1375 }
1376 }
1377 sb->nr_disks = nr_disks;
1378 sb->active_disks = active;
1379 sb->working_disks = working;
1380 sb->failed_disks = failed;
1381 sb->spare_disks = spare;
1382
1383 sb->this_disk = sb->disks[rdev->desc_nr];
1384 sb->sb_csum = calc_sb_csum(sb);
1385}
1386
1387/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001388 * rdev_size_change for 0.90.0
1389 */
1390static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001391super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001392{
Andre Noll58c0fed2009-03-31 14:33:13 +11001393 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001394 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001395 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001396 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001397 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001398 if (!num_sectors || num_sectors > rdev->sb_start)
1399 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001400 /* Limit to 4TB as metadata cannot record more than that.
1401 * 4TB == 2^32 KB, or 2*2^32 sectors.
1402 */
1403 if (num_sectors >= (2ULL << 32))
1404 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001405 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406 rdev->sb_page);
1407 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001408 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001409}
1410
NeilBrownc6563a82012-05-21 09:27:00 +10001411static int
1412super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1413{
1414 /* non-zero offset changes not possible with v0.90 */
1415 return new_offset == 0;
1416}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001417
1418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * version 1 superblock
1420 */
1421
NeilBrown1c05b4b2006-10-21 10:24:08 -07001422static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001424 __le32 disk_csum;
1425 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 unsigned long long newcsum;
1427 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001428 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int i;
1430
1431 disk_csum = sb->sb_csum;
1432 sb->sb_csum = 0;
1433 newcsum = 0;
1434 for (i=0; size>=4; size -= 4 )
1435 newcsum += le32_to_cpu(*isuper++);
1436
1437 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001438 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1441 sb->sb_csum = disk_csum;
1442 return cpu_to_le32(csum);
1443}
1444
NeilBrown2699b672011-07-28 11:31:47 +10001445static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1446 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001447static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct mdp_superblock_1 *sb;
1450 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001451 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001452 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001454 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /*
Andre Noll0f420352008-07-11 22:02:23 +10001457 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * It is always aligned to a 4K boundary and
1459 * depeding on minor_version, it can be:
1460 * 0: At least 8K, but less than 12K, from end of device
1461 * 1: At start of device
1462 * 2: 4K from start of device.
1463 */
1464 switch(minor_version) {
1465 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001466 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001467 sb_start -= 8*2;
1468 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001471 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001474 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 default:
1477 return -EINVAL;
1478 }
Andre Noll0f420352008-07-11 22:02:23 +10001479 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
NeilBrown0002b272005-09-09 16:23:53 -07001481 /* superblock is rarely larger than 1K, but it can be larger,
1482 * and it is safe to read 4k, so we do that
1483 */
1484 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (ret) return ret;
1486
1487
Namhyung Kim65a06f062011-07-27 11:00:36 +10001488 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1491 sb->major_version != cpu_to_le32(1) ||
1492 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001493 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001494 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EINVAL;
1496
1497 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1498 printk("md: invalid superblock checksum on %s\n",
1499 bdevname(rdev->bdev,b));
1500 return -EINVAL;
1501 }
1502 if (le64_to_cpu(sb->data_size) < 10) {
1503 printk("md: data_size too small on %s\n",
1504 bdevname(rdev->bdev,b));
1505 return -EINVAL;
1506 }
NeilBrownc6563a82012-05-21 09:27:00 +10001507 if (sb->pad0 ||
1508 sb->pad3[0] ||
1509 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1510 /* Some padding is non-zero, might be a new feature */
1511 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 rdev->preferred_minor = 0xffff;
1514 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001515 rdev->new_data_offset = rdev->data_offset;
1516 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1517 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1518 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001519 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
NeilBrown0002b272005-09-09 16:23:53 -07001521 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001522 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001523 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001524 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1525
1526 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001527 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001528 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001529 if (minor_version
1530 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1531 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001532
NeilBrown31b65a02006-07-10 04:44:14 -07001533 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1534 rdev->desc_nr = -1;
1535 else
1536 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1537
NeilBrown2699b672011-07-28 11:31:47 +10001538 if (!rdev->bb_page) {
1539 rdev->bb_page = alloc_page(GFP_KERNEL);
1540 if (!rdev->bb_page)
1541 return -ENOMEM;
1542 }
1543 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1544 rdev->badblocks.count == 0) {
1545 /* need to load the bad block list.
1546 * Currently we limit it to one page.
1547 */
1548 s32 offset;
1549 sector_t bb_sector;
1550 u64 *bbp;
1551 int i;
1552 int sectors = le16_to_cpu(sb->bblog_size);
1553 if (sectors > (PAGE_SIZE / 512))
1554 return -EINVAL;
1555 offset = le32_to_cpu(sb->bblog_offset);
1556 if (offset == 0)
1557 return -EINVAL;
1558 bb_sector = (long long)offset;
1559 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1560 rdev->bb_page, READ, true))
1561 return -EIO;
1562 bbp = (u64 *)page_address(rdev->bb_page);
1563 rdev->badblocks.shift = sb->bblog_shift;
1564 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1565 u64 bb = le64_to_cpu(*bbp);
1566 int count = bb & (0x3ff);
1567 u64 sector = bb >> 10;
1568 sector <<= sb->bblog_shift;
1569 count <<= sb->bblog_shift;
1570 if (bb + 1 == 0)
1571 break;
1572 if (md_set_badblocks(&rdev->badblocks,
1573 sector, count, 1) == 0)
1574 return -EINVAL;
1575 }
1576 } else if (sb->bblog_offset == 0)
1577 rdev->badblocks.shift = -1;
1578
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001579 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001580 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001581 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001583 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1586 sb->level != refsb->level ||
1587 sb->layout != refsb->layout ||
1588 sb->chunksize != refsb->chunksize) {
1589 printk(KERN_WARNING "md: %s has strangely different"
1590 " superblock to %s\n",
1591 bdevname(rdev->bdev,b),
1592 bdevname(refdev->bdev,b2));
1593 return -EINVAL;
1594 }
1595 ev1 = le64_to_cpu(sb->events);
1596 ev2 = le64_to_cpu(refsb->events);
1597
1598 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001599 ret = 1;
1600 else
1601 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
NeilBrownc6563a82012-05-21 09:27:00 +10001603 if (minor_version) {
1604 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1605 sectors -= rdev->data_offset;
1606 } else
1607 sectors = rdev->sb_start;
1608 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001610 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001611 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
NeilBrownfd01b882011-10-11 16:47:53 +11001614static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001616 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001617 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
NeilBrown41158c72005-06-21 17:17:25 -07001619 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001620 clear_bit(Faulty, &rdev->flags);
1621 clear_bit(In_sync, &rdev->flags);
1622 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (mddev->raid_disks == 0) {
1625 mddev->major_version = 1;
1626 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001627 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001628 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1630 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1631 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001632 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 mddev->layout = le32_to_cpu(sb->layout);
1634 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001635 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001636 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001637 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001638 mddev->bitmap_info.space = 0;
1639 /* Default location for bitmap is 1K after superblock
1640 * using 3K - total of 4K
1641 */
NeilBrownc3d97142009-12-14 12:49:52 +11001642 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001643 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001644 mddev->reshape_backwards = 0;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1647 memcpy(mddev->uuid, sb->set_uuid, 16);
1648
1649 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001650
NeilBrown71c08052005-09-09 16:23:51 -07001651 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001652 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001653 mddev->bitmap_info.offset =
1654 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001655 /* Metadata doesn't record how much space is available.
1656 * For 1.0, we assume we can use up to the superblock
1657 * if before, else to 4K beyond superblock.
1658 * For others, assume no change is possible.
1659 */
1660 if (mddev->minor_version > 0)
1661 mddev->bitmap_info.space = 0;
1662 else if (mddev->bitmap_info.offset > 0)
1663 mddev->bitmap_info.space =
1664 8 - mddev->bitmap_info.offset;
1665 else
1666 mddev->bitmap_info.space =
1667 -mddev->bitmap_info.offset;
1668 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001669
NeilBrownf6705572006-03-27 01:18:11 -08001670 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1671 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1672 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1673 mddev->new_level = le32_to_cpu(sb->new_level);
1674 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001675 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001676 if (mddev->delta_disks < 0 ||
1677 (mddev->delta_disks == 0 &&
1678 (le32_to_cpu(sb->feature_map)
1679 & MD_FEATURE_RESHAPE_BACKWARDS)))
1680 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001681 } else {
1682 mddev->reshape_position = MaxSector;
1683 mddev->delta_disks = 0;
1684 mddev->new_level = mddev->level;
1685 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001686 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001687 }
1688
NeilBrown41158c72005-06-21 17:17:25 -07001689 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001690 /* Insist of good event counter while assembling, except for
1691 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001693 if (rdev->desc_nr >= 0 &&
1694 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1695 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1696 if (ev1 < mddev->events)
1697 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001698 } else if (mddev->bitmap) {
1699 /* If adding to array with a bitmap, then we can accept an
1700 * older device, but not too old.
1701 */
NeilBrown41158c72005-06-21 17:17:25 -07001702 if (ev1 < mddev->bitmap->events_cleared)
1703 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001704 } else {
1705 if (ev1 < mddev->events)
1706 /* just a hot-add of a new device, leave raid_disk at -1 */
1707 return 0;
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (mddev->level != LEVEL_MULTIPATH) {
1710 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001711 if (rdev->desc_nr < 0 ||
1712 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1713 role = 0xffff;
1714 rdev->desc_nr = -1;
1715 } else
1716 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 switch(role) {
1718 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 break;
1720 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001721 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001724 if ((le32_to_cpu(sb->feature_map) &
1725 MD_FEATURE_RECOVERY_OFFSET))
1726 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1727 else
1728 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rdev->raid_disk = role;
1730 break;
1731 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001732 if (sb->devflags & WriteMostly1)
1733 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001734 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1735 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001736 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001737 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
1740}
1741
NeilBrownfd01b882011-10-11 16:47:53 +11001742static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001745 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int max_dev, i;
1747 /* make rdev->sb match mddev and rdev data. */
1748
Namhyung Kim65a06f062011-07-27 11:00:36 +10001749 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 sb->feature_map = 0;
1752 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001753 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 memset(sb->pad3, 0, sizeof(sb->pad3));
1755
1756 sb->utime = cpu_to_le64((__u64)mddev->utime);
1757 sb->events = cpu_to_le64(mddev->events);
1758 if (mddev->in_sync)
1759 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1760 else
1761 sb->resync_offset = cpu_to_le64(0);
1762
NeilBrown1c05b4b2006-10-21 10:24:08 -07001763 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001764
NeilBrownf0ca3402006-02-02 14:28:04 -08001765 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001766 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001767 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001768 sb->level = cpu_to_le32(mddev->level);
1769 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001770
NeilBrownaeb9b2112011-08-25 14:43:08 +10001771 if (test_bit(WriteMostly, &rdev->flags))
1772 sb->devflags |= WriteMostly1;
1773 else
1774 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001775 sb->data_offset = cpu_to_le64(rdev->data_offset);
1776 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001777
NeilBrownc3d97142009-12-14 12:49:52 +11001778 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1779 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001780 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001781 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001782
1783 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001784 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001785 sb->feature_map |=
1786 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1787 sb->recovery_offset =
1788 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001789 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001790 if (test_bit(Replacement, &rdev->flags))
1791 sb->feature_map |=
1792 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001793
NeilBrownf6705572006-03-27 01:18:11 -08001794 if (mddev->reshape_position != MaxSector) {
1795 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1796 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1797 sb->new_layout = cpu_to_le32(mddev->new_layout);
1798 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1799 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001800 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001801 if (mddev->delta_disks == 0 &&
1802 mddev->reshape_backwards)
1803 sb->feature_map
1804 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001805 if (rdev->new_data_offset != rdev->data_offset) {
1806 sb->feature_map
1807 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1808 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1809 - rdev->data_offset));
1810 }
NeilBrownf6705572006-03-27 01:18:11 -08001811 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001812
NeilBrown2699b672011-07-28 11:31:47 +10001813 if (rdev->badblocks.count == 0)
1814 /* Nothing to do for bad blocks*/ ;
1815 else if (sb->bblog_offset == 0)
1816 /* Cannot record bad blocks on this device */
1817 md_error(mddev, rdev);
1818 else {
1819 struct badblocks *bb = &rdev->badblocks;
1820 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1821 u64 *p = bb->page;
1822 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1823 if (bb->changed) {
1824 unsigned seq;
1825
1826retry:
1827 seq = read_seqbegin(&bb->lock);
1828
1829 memset(bbp, 0xff, PAGE_SIZE);
1830
1831 for (i = 0 ; i < bb->count ; i++) {
1832 u64 internal_bb = *p++;
1833 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1834 | BB_LEN(internal_bb));
1835 *bbp++ = cpu_to_le64(store_bb);
1836 }
NeilBrownd0962932012-03-19 12:46:41 +11001837 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001838 if (read_seqretry(&bb->lock, seq))
1839 goto retry;
1840
1841 bb->sector = (rdev->sb_start +
1842 (int)le32_to_cpu(sb->bblog_offset));
1843 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001844 }
1845 }
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001848 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (rdev2->desc_nr+1 > max_dev)
1850 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001851
NeilBrown70471da2009-08-03 10:59:57 +10001852 if (max_dev > le32_to_cpu(sb->max_dev)) {
1853 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001854 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001855 rdev->sb_size = max_dev * 2 + 256;
1856 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1857 if (rdev->sb_size & bmask)
1858 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001859 } else
1860 max_dev = le32_to_cpu(sb->max_dev);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 for (i=0; i<max_dev;i++)
1863 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1864
NeilBrowndafb20f2012-03-19 12:46:39 +11001865 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001867 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001869 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001871 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001872 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 else
1874 sb->dev_roles[i] = cpu_to_le16(0xffff);
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 sb->sb_csum = calc_sb_1_csum(sb);
1878}
1879
Chris Webb0cd17fe2008-06-28 08:31:46 +10001880static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001881super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001882{
1883 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001884 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001885 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001886 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001887 if (rdev->data_offset != rdev->new_data_offset)
1888 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001889 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001890 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001891 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001892 max_sectors -= rdev->data_offset;
1893 if (!num_sectors || num_sectors > max_sectors)
1894 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001895 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001896 /* minor version 0 with bitmap we can't move */
1897 return 0;
1898 } else {
1899 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001900 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001901 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001902 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001903 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001904 if (!num_sectors || num_sectors > max_sectors)
1905 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001906 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001908 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001909 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001910 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001911 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001912 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001913 rdev->sb_page);
1914 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001915 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001916
1917}
1918
1919static int
1920super_1_allow_new_offset(struct md_rdev *rdev,
1921 unsigned long long new_offset)
1922{
1923 /* All necessary checks on new >= old have been done */
1924 struct bitmap *bitmap;
1925 if (new_offset >= rdev->data_offset)
1926 return 1;
1927
1928 /* with 1.0 metadata, there is no metadata to tread on
1929 * so we can always move back */
1930 if (rdev->mddev->minor_version == 0)
1931 return 1;
1932
1933 /* otherwise we must be sure not to step on
1934 * any metadata, so stay:
1935 * 36K beyond start of superblock
1936 * beyond end of badblocks
1937 * beyond write-intent bitmap
1938 */
1939 if (rdev->sb_start + (32+4)*2 > new_offset)
1940 return 0;
1941 bitmap = rdev->mddev->bitmap;
1942 if (bitmap && !rdev->mddev->bitmap_info.file &&
1943 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10001944 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10001945 return 0;
1946 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1947 return 0;
1948
1949 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Adrian Bunk75c96f82005-05-05 16:16:09 -07001952static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 [0] = {
1954 .name = "0.90.0",
1955 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001956 .load_super = super_90_load,
1957 .validate_super = super_90_validate,
1958 .sync_super = super_90_sync,
1959 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001960 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 },
1962 [1] = {
1963 .name = "md-1",
1964 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001965 .load_super = super_1_load,
1966 .validate_super = super_1_validate,
1967 .sync_super = super_1_sync,
1968 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001969 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 },
1971};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
NeilBrownfd01b882011-10-11 16:47:53 +11001973static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001974{
1975 if (mddev->sync_super) {
1976 mddev->sync_super(mddev, rdev);
1977 return;
1978 }
1979
1980 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1981
1982 super_types[mddev->major_version].sync_super(mddev, rdev);
1983}
1984
NeilBrownfd01b882011-10-11 16:47:53 +11001985static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
NeilBrown3cb03002011-10-11 16:45:26 +11001987 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
NeilBrown4b809912008-07-21 17:05:25 +10001989 rcu_read_lock();
1990 rdev_for_each_rcu(rdev, mddev1)
1991 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001992 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001993 rdev2->bdev->bd_contains) {
1994 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001995 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001996 }
1997 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
2000
2001static LIST_HEAD(pending_raid_disks);
2002
Andre Nollac5e7112009-08-03 10:59:47 +10002003/*
2004 * Try to register data integrity profile for an mddev
2005 *
2006 * This is called when an array is started and after a disk has been kicked
2007 * from the array. It only succeeds if all working and active component devices
2008 * are integrity capable with matching profiles.
2009 */
NeilBrownfd01b882011-10-11 16:47:53 +11002010int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002011{
NeilBrown3cb03002011-10-11 16:45:26 +11002012 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002013
Andre Nollac5e7112009-08-03 10:59:47 +10002014 if (list_empty(&mddev->disks))
2015 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002016 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2017 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002018 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002019 /* skip spares and non-functional disks */
2020 if (test_bit(Faulty, &rdev->flags))
2021 continue;
2022 if (rdev->raid_disk < 0)
2023 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002024 if (!reference) {
2025 /* Use the first rdev as the reference */
2026 reference = rdev;
2027 continue;
2028 }
2029 /* does this rdev's profile match the reference profile? */
2030 if (blk_integrity_compare(reference->bdev->bd_disk,
2031 rdev->bdev->bd_disk) < 0)
2032 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002033 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002034 if (!reference || !bdev_get_integrity(reference->bdev))
2035 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002036 /*
2037 * All component devices are integrity capable and have matching
2038 * profiles, register the common profile for the md device.
2039 */
2040 if (blk_integrity_register(mddev->gendisk,
2041 bdev_get_integrity(reference->bdev)) != 0) {
2042 printk(KERN_ERR "md: failed to register integrity for %s\n",
2043 mdname(mddev));
2044 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002045 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002046 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2047 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2048 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2049 mdname(mddev));
2050 return -EINVAL;
2051 }
Andre Nollac5e7112009-08-03 10:59:47 +10002052 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002053}
Andre Nollac5e7112009-08-03 10:59:47 +10002054EXPORT_SYMBOL(md_integrity_register);
2055
2056/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002057void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002058{
2059 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
2060 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
2061
2062 if (!bi_mddev) /* nothing to do */
2063 return;
2064 if (rdev->raid_disk < 0) /* skip spares */
2065 return;
2066 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2067 rdev->bdev->bd_disk) >= 0)
2068 return;
2069 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2070 blk_integrity_unregister(mddev->gendisk);
2071}
2072EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002073
NeilBrownfd01b882011-10-11 16:47:53 +11002074static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002076 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002077 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002078 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002079 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 if (rdev->mddev) {
2082 MD_BUG();
2083 return -EINVAL;
2084 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002085
2086 /* prevent duplicates */
2087 if (find_rdev(mddev, rdev->bdev->bd_dev))
2088 return -EEXIST;
2089
Andre Nolldd8ac332009-03-31 14:33:13 +11002090 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2091 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2092 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002093 if (mddev->pers) {
2094 /* Cannot change size, so fail
2095 * If mddev->level <= 0, then we don't care
2096 * about aligning sizes (e.g. linear)
2097 */
2098 if (mddev->level > 0)
2099 return -ENOSPC;
2100 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002101 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /* Verify rdev->desc_nr is unique.
2105 * If it is -1, assign a free number, else
2106 * check number is not in use
2107 */
2108 if (rdev->desc_nr < 0) {
2109 int choice = 0;
2110 if (mddev->pers) choice = mddev->raid_disks;
2111 while (find_rdev_nr(mddev, choice))
2112 choice++;
2113 rdev->desc_nr = choice;
2114 } else {
2115 if (find_rdev_nr(mddev, rdev->desc_nr))
2116 return -EBUSY;
2117 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002118 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2119 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2120 mdname(mddev), mddev->max_disks);
2121 return -EBUSY;
2122 }
NeilBrown19133a42005-11-08 21:39:35 -08002123 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002124 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002125 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002128 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002129
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002130 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002131 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002132
Tejun Heo0762b8b2008-08-25 19:56:12 +09002133 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002134 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2135 /* failure here is OK */;
2136 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002137
NeilBrown4b809912008-07-21 17:05:25 +10002138 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002139 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002140
2141 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002142 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002145
2146 fail:
2147 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2148 b, mdname(mddev));
2149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
NeilBrown177a99b2008-02-06 01:39:56 -08002152static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002153{
NeilBrown3cb03002011-10-11 16:45:26 +11002154 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002155 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002156 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002157}
2158
NeilBrown3cb03002011-10-11 16:45:26 +11002159static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 char b[BDEVNAME_SIZE];
2162 if (!rdev->mddev) {
2163 MD_BUG();
2164 return;
2165 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002166 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002167 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2169 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002170 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002171 sysfs_put(rdev->sysfs_state);
2172 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002173 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002174 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002175 * writing to 'remove' to "dev/state". We also need
2176 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002177 */
NeilBrown4b809912008-07-21 17:05:25 +10002178 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002179 INIT_WORK(&rdev->del_work, md_delayed_delete);
2180 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002181 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184/*
2185 * prevent the device from being mounted, repartitioned or
2186 * otherwise reused by a RAID array (or any other kernel
2187 * subsystem), by bd_claiming the device.
2188 */
NeilBrown3cb03002011-10-11 16:45:26 +11002189static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int err = 0;
2192 struct block_device *bdev;
2193 char b[BDEVNAME_SIZE];
2194
Tejun Heod4d77622010-11-13 11:55:18 +01002195 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002196 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (IS_ERR(bdev)) {
2198 printk(KERN_ERR "md: could not open %s.\n",
2199 __bdevname(dev, b));
2200 return PTR_ERR(bdev);
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rdev->bdev = bdev;
2203 return err;
2204}
2205
NeilBrown3cb03002011-10-11 16:45:26 +11002206static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct block_device *bdev = rdev->bdev;
2209 rdev->bdev = NULL;
2210 if (!bdev)
2211 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002212 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215void md_autodetect_dev(dev_t dev);
2216
NeilBrown3cb03002011-10-11 16:45:26 +11002217static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 char b[BDEVNAME_SIZE];
2220 printk(KERN_INFO "md: export_rdev(%s)\n",
2221 bdevname(rdev->bdev,b));
2222 if (rdev->mddev)
2223 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002224 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002226 if (test_bit(AutoDetected, &rdev->flags))
2227 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#endif
2229 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002230 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
NeilBrown3cb03002011-10-11 16:45:26 +11002233static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 unbind_rdev_from_array(rdev);
2236 export_rdev(rdev);
2237}
2238
NeilBrownfd01b882011-10-11 16:47:53 +11002239static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
NeilBrown3cb03002011-10-11 16:45:26 +11002241 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
NeilBrowndafb20f2012-03-19 12:46:39 +11002243 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (!rdev->mddev) {
2245 MD_BUG();
2246 continue;
2247 }
2248 kick_rdev_from_array(rdev);
2249 }
2250 if (!list_empty(&mddev->disks))
2251 MD_BUG();
2252 mddev->raid_disks = 0;
2253 mddev->major_version = 0;
2254}
2255
2256static void print_desc(mdp_disk_t *desc)
2257{
2258 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2259 desc->major,desc->minor,desc->raid_disk,desc->state);
2260}
2261
Cheng Renquancd2ac932009-01-09 08:31:08 +11002262static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 int i;
2265
2266 printk(KERN_INFO
2267 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2268 sb->major_version, sb->minor_version, sb->patch_version,
2269 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2270 sb->ctime);
2271 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2272 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2273 sb->md_minor, sb->layout, sb->chunk_size);
2274 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2275 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2276 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2277 sb->failed_disks, sb->spare_disks,
2278 sb->sb_csum, (unsigned long)sb->events_lo);
2279
2280 printk(KERN_INFO);
2281 for (i = 0; i < MD_SB_DISKS; i++) {
2282 mdp_disk_t *desc;
2283
2284 desc = sb->disks + i;
2285 if (desc->number || desc->major || desc->minor ||
2286 desc->raid_disk || (desc->state && (desc->state != 4))) {
2287 printk(" D %2d: ", i);
2288 print_desc(desc);
2289 }
2290 }
2291 printk(KERN_INFO "md: THIS: ");
2292 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Cheng Renquancd2ac932009-01-09 08:31:08 +11002295static void print_sb_1(struct mdp_superblock_1 *sb)
2296{
2297 __u8 *uuid;
2298
2299 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002300 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002301 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002302 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002303 le32_to_cpu(sb->major_version),
2304 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002305 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002306 sb->set_name,
2307 (unsigned long long)le64_to_cpu(sb->ctime)
2308 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2309
2310 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002311 printk(KERN_INFO
2312 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002313 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002314 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002315 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2316 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002317 le32_to_cpu(sb->level),
2318 (unsigned long long)le64_to_cpu(sb->size),
2319 le32_to_cpu(sb->raid_disks),
2320 le32_to_cpu(sb->layout),
2321 le32_to_cpu(sb->chunksize),
2322 (unsigned long long)le64_to_cpu(sb->data_offset),
2323 (unsigned long long)le64_to_cpu(sb->data_size),
2324 (unsigned long long)le64_to_cpu(sb->super_offset),
2325 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2326 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002327 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002328 sb->devflags,
2329 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2330 (unsigned long long)le64_to_cpu(sb->events),
2331 (unsigned long long)le64_to_cpu(sb->resync_offset),
2332 le32_to_cpu(sb->sb_csum),
2333 le32_to_cpu(sb->max_dev)
2334 );
2335}
2336
NeilBrown3cb03002011-10-11 16:45:26 +11002337static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002340 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2341 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002342 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2343 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002345 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2346 switch (major_version) {
2347 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002348 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002349 break;
2350 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002351 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002352 break;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 } else
2355 printk(KERN_INFO "md: no rdev superblock!\n");
2356}
2357
Adrian Bunk5e563412006-06-26 00:27:42 -07002358static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002360 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002361 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002362 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 char b[BDEVNAME_SIZE];
2364
2365 printk("\n");
2366 printk("md: **********************************\n");
2367 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2368 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002369 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
NeilBrown32a76272005-06-21 17:17:14 -07002371 if (mddev->bitmap)
2372 bitmap_print_sb(mddev->bitmap);
2373 else
2374 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002375 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 printk("<%s>", bdevname(rdev->bdev,b));
2377 printk("\n");
2378
NeilBrowndafb20f2012-03-19 12:46:39 +11002379 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002380 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 printk("md: **********************************\n");
2383 printk("\n");
2384}
2385
2386
NeilBrownfd01b882011-10-11 16:47:53 +11002387static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
NeilBrown42543762006-06-26 00:27:57 -07002389 /* Update each superblock (in-memory image), but
2390 * if we are allowed to, skip spares which already
2391 * have the right event counter, or have one earlier
2392 * (which would mean they aren't being marked as dirty
2393 * with the rest of the array)
2394 */
NeilBrown3cb03002011-10-11 16:45:26 +11002395 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002396 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002397 if (rdev->sb_events == mddev->events ||
2398 (nospares &&
2399 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002400 rdev->sb_events+1 == mddev->events)) {
2401 /* Don't update this superblock */
2402 rdev->sb_loaded = 2;
2403 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002404 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002405 rdev->sb_loaded = 1;
2406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408}
2409
NeilBrownfd01b882011-10-11 16:47:53 +11002410static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
NeilBrown3cb03002011-10-11 16:45:26 +11002412 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002413 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002414 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002415 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002418 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002419 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002420 if (rdev->raid_disk >= 0 &&
2421 mddev->delta_disks >= 0 &&
2422 !test_bit(In_sync, &rdev->flags) &&
2423 mddev->curr_resync_completed > rdev->recovery_offset)
2424 rdev->recovery_offset = mddev->curr_resync_completed;
2425
2426 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002427 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002428 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002429 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002430 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002431 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002432 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002433 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002434 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002435 md_ack_all_badblocks(&rdev->badblocks);
2436 md_error(mddev, rdev);
2437 }
2438 clear_bit(Blocked, &rdev->flags);
2439 clear_bit(BlockedBadBlocks, &rdev->flags);
2440 wake_up(&rdev->blocked_wait);
2441 }
2442 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002443 wake_up(&mddev->sb_wait);
2444 return;
2445 }
2446
NeilBrowna9701a32005-11-08 21:39:34 -08002447 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002448
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002449 mddev->utime = get_seconds();
2450
NeilBrown850b2b422006-10-03 01:15:46 -07002451 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2452 force_change = 1;
2453 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2454 /* just a clean<-> dirty transition, possibly leave spares alone,
2455 * though if events isn't the right even/odd, we will have to do
2456 * spares after all
2457 */
2458 nospares = 1;
2459 if (force_change)
2460 nospares = 0;
2461 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002462 /* If the array is degraded, then skipping spares is both
2463 * dangerous and fairly pointless.
2464 * Dangerous because a device that was removed from the array
2465 * might have a event_count that still looks up-to-date,
2466 * so it can be re-added without a resync.
2467 * Pointless because if there are any spares to skip,
2468 * then a recovery will happen and soon that array won't
2469 * be degraded any more and the spare can go back to sleep then.
2470 */
NeilBrown850b2b422006-10-03 01:15:46 -07002471 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002472
NeilBrown06d91a52005-06-21 17:17:12 -07002473 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002474
2475 /* If this is just a dirty<->clean transition, and the array is clean
2476 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002477 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002478 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002479 && mddev->can_decrease_events
2480 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002481 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002482 mddev->can_decrease_events = 0;
2483 } else {
NeilBrown42543762006-06-26 00:27:57 -07002484 /* otherwise we have to go forward and ... */
2485 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002486 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 if (!mddev->events) {
2490 /*
2491 * oops, this 64-bit counter should never wrap.
2492 * Either we are in around ~1 trillion A.C., assuming
2493 * 1 reboot per second, or we have a bug:
2494 */
2495 MD_BUG();
2496 mddev->events --;
2497 }
NeilBrown2699b672011-07-28 11:31:47 +10002498
NeilBrowndafb20f2012-03-19 12:46:39 +11002499 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002500 if (rdev->badblocks.changed)
2501 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002502 if (test_bit(Faulty, &rdev->flags))
2503 set_bit(FaultRecorded, &rdev->flags);
2504 }
NeilBrown2699b672011-07-28 11:31:47 +10002505
NeilBrowne6910632008-02-06 01:39:51 -08002506 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002507 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
NeilBrown36a4e1f2011-10-07 14:23:17 +11002509 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2510 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
NeilBrown4ad13662007-07-17 04:06:13 -07002512 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002513 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002515
NeilBrown42543762006-06-26 00:27:57 -07002516 if (rdev->sb_loaded != 1)
2517 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002519 if (!test_bit(Faulty, &rdev->flags) &&
2520 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002521 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002522 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002523 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002524 pr_debug("md: (write) %s's sb offset: %llu\n",
2525 bdevname(rdev->bdev, b),
2526 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002527 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002528 if (rdev->badblocks.size) {
2529 md_super_write(mddev, rdev,
2530 rdev->badblocks.sector,
2531 rdev->badblocks.size << 9,
2532 rdev->bb_page);
2533 rdev->badblocks.size = 0;
2534 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002535
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002536 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002537 pr_debug("md: %s (skipping faulty)\n",
2538 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002539 else
2540 pr_debug("(skipping incremental s/r ");
2541
NeilBrown7bfa19f2005-06-21 17:17:28 -07002542 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* only need to write one superblock... */
2544 break;
2545 }
NeilBrowna9701a32005-11-08 21:39:34 -08002546 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002547 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002548
NeilBrowna9701a32005-11-08 21:39:34 -08002549 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002550 if (mddev->in_sync != sync_req ||
2551 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002552 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002553 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002554 goto repeat;
2555 }
NeilBrown850b2b422006-10-03 01:15:46 -07002556 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002557 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002558 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002559 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2560 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002561
NeilBrowndafb20f2012-03-19 12:46:39 +11002562 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002563 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2564 clear_bit(Blocked, &rdev->flags);
2565
2566 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002567 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002568 clear_bit(BlockedBadBlocks, &rdev->flags);
2569 wake_up(&rdev->blocked_wait);
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Andre Noll7f6ce762008-03-23 18:34:54 +01002573/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002574 * We want to accept with case. For this we use cmd_match.
2575 */
2576static int cmd_match(const char *cmd, const char *str)
2577{
2578 /* See if cmd, written into a sysfs file, matches
2579 * str. They must either be the same, or cmd can
2580 * have a trailing newline
2581 */
2582 while (*cmd && *str && *cmd == *str) {
2583 cmd++;
2584 str++;
2585 }
2586 if (*cmd == '\n')
2587 cmd++;
2588 if (*str || *cmd)
2589 return 0;
2590 return 1;
2591}
2592
NeilBrown86e6ffd2005-11-08 21:39:24 -08002593struct rdev_sysfs_entry {
2594 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002595 ssize_t (*show)(struct md_rdev *, char *);
2596 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002597};
2598
2599static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002600state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601{
2602 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002603 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604
NeilBrownde393cd2011-07-28 11:31:48 +10002605 if (test_bit(Faulty, &rdev->flags) ||
2606 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002607 len+= sprintf(page+len, "%sfaulty",sep);
2608 sep = ",";
2609 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002610 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002611 len += sprintf(page+len, "%sin_sync",sep);
2612 sep = ",";
2613 }
NeilBrownf6556752006-06-26 00:28:01 -07002614 if (test_bit(WriteMostly, &rdev->flags)) {
2615 len += sprintf(page+len, "%swrite_mostly",sep);
2616 sep = ",";
2617 }
NeilBrownde393cd2011-07-28 11:31:48 +10002618 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002619 (rdev->badblocks.unacked_exist
2620 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002621 len += sprintf(page+len, "%sblocked", sep);
2622 sep = ",";
2623 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002624 if (!test_bit(Faulty, &rdev->flags) &&
2625 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002626 len += sprintf(page+len, "%sspare", sep);
2627 sep = ",";
2628 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002629 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2630 len += sprintf(page+len, "%swrite_error", sep);
2631 sep = ",";
2632 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002633 if (test_bit(WantReplacement, &rdev->flags)) {
2634 len += sprintf(page+len, "%swant_replacement", sep);
2635 sep = ",";
2636 }
2637 if (test_bit(Replacement, &rdev->flags)) {
2638 len += sprintf(page+len, "%sreplacement", sep);
2639 sep = ",";
2640 }
2641
NeilBrown86e6ffd2005-11-08 21:39:24 -08002642 return len+sprintf(page+len, "\n");
2643}
2644
NeilBrown45dc2de2006-06-26 00:27:58 -07002645static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002646state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002647{
2648 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002649 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002650 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002651 * writemostly - sets write_mostly
2652 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002653 * blocked - sets the Blocked flags
2654 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002655 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002656 * write_error - sets WriteErrorSeen
2657 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002658 */
2659 int err = -EINVAL;
2660 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2661 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002662 if (test_bit(Faulty, &rdev->flags))
2663 err = 0;
2664 else
2665 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002666 } else if (cmd_match(buf, "remove")) {
2667 if (rdev->raid_disk >= 0)
2668 err = -EBUSY;
2669 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002670 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002671 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002672 if (mddev->pers)
2673 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002674 md_new_event(mddev);
2675 err = 0;
2676 }
NeilBrownf6556752006-06-26 00:28:01 -07002677 } else if (cmd_match(buf, "writemostly")) {
2678 set_bit(WriteMostly, &rdev->flags);
2679 err = 0;
2680 } else if (cmd_match(buf, "-writemostly")) {
2681 clear_bit(WriteMostly, &rdev->flags);
2682 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002683 } else if (cmd_match(buf, "blocked")) {
2684 set_bit(Blocked, &rdev->flags);
2685 err = 0;
2686 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002687 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002688 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002689 /* metadata handler doesn't understand badblocks,
2690 * so we need to fail the device
2691 */
2692 md_error(rdev->mddev, rdev);
2693 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002694 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002695 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002696 wake_up(&rdev->blocked_wait);
2697 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2698 md_wakeup_thread(rdev->mddev->thread);
2699
2700 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002701 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2702 set_bit(In_sync, &rdev->flags);
2703 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002704 } else if (cmd_match(buf, "write_error")) {
2705 set_bit(WriteErrorSeen, &rdev->flags);
2706 err = 0;
2707 } else if (cmd_match(buf, "-write_error")) {
2708 clear_bit(WriteErrorSeen, &rdev->flags);
2709 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002710 } else if (cmd_match(buf, "want_replacement")) {
2711 /* Any non-spare device that is not a replacement can
2712 * become want_replacement at any time, but we then need to
2713 * check if recovery is needed.
2714 */
2715 if (rdev->raid_disk >= 0 &&
2716 !test_bit(Replacement, &rdev->flags))
2717 set_bit(WantReplacement, &rdev->flags);
2718 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2719 md_wakeup_thread(rdev->mddev->thread);
2720 err = 0;
2721 } else if (cmd_match(buf, "-want_replacement")) {
2722 /* Clearing 'want_replacement' is always allowed.
2723 * Once replacements starts it is too late though.
2724 */
2725 err = 0;
2726 clear_bit(WantReplacement, &rdev->flags);
2727 } else if (cmd_match(buf, "replacement")) {
2728 /* Can only set a device as a replacement when array has not
2729 * yet been started. Once running, replacement is automatic
2730 * from spares, or by assigning 'slot'.
2731 */
2732 if (rdev->mddev->pers)
2733 err = -EBUSY;
2734 else {
2735 set_bit(Replacement, &rdev->flags);
2736 err = 0;
2737 }
2738 } else if (cmd_match(buf, "-replacement")) {
2739 /* Similarly, can only clear Replacement before start */
2740 if (rdev->mddev->pers)
2741 err = -EBUSY;
2742 else {
2743 clear_bit(Replacement, &rdev->flags);
2744 err = 0;
2745 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002746 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002747 if (!err)
2748 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002749 return err ? err : len;
2750}
NeilBrown80ca3a42006-07-10 04:44:18 -07002751static struct rdev_sysfs_entry rdev_state =
2752__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002753
2754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002756{
2757 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2758}
2759
2760static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002761errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002762{
2763 char *e;
2764 unsigned long n = simple_strtoul(buf, &e, 10);
2765 if (*buf && (*e == 0 || *e == '\n')) {
2766 atomic_set(&rdev->corrected_errors, n);
2767 return len;
2768 }
2769 return -EINVAL;
2770}
2771static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002772__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002773
NeilBrown014236d2006-01-06 00:20:55 -08002774static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002775slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002776{
2777 if (rdev->raid_disk < 0)
2778 return sprintf(page, "none\n");
2779 else
2780 return sprintf(page, "%d\n", rdev->raid_disk);
2781}
2782
2783static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002784slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002785{
2786 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002787 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002788 int slot = simple_strtoul(buf, &e, 10);
2789 if (strncmp(buf, "none", 4)==0)
2790 slot = -1;
2791 else if (e==buf || (*e && *e!= '\n'))
2792 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002793 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002794 /* Setting 'slot' on an active array requires also
2795 * updating the 'rd%d' link, and communicating
2796 * with the personality with ->hot_*_disk.
2797 * For now we only support removing
2798 * failed/spare devices. This normally happens automatically,
2799 * but not when the metadata is externally managed.
2800 */
NeilBrownc303da62008-02-06 01:39:51 -08002801 if (rdev->raid_disk == -1)
2802 return -EEXIST;
2803 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002804 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002805 return -EINVAL;
2806 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002807 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002808 if (err)
2809 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002810 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002811 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002812 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2813 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002814 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002815 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002816 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002817 */
2818
2819 if (rdev->raid_disk != -1)
2820 return -EBUSY;
2821
NeilBrownc6751b22011-02-02 11:57:13 +11002822 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2823 return -EBUSY;
2824
Neil Brown6c2fce22008-06-28 08:31:31 +10002825 if (rdev->mddev->pers->hot_add_disk == NULL)
2826 return -EINVAL;
2827
NeilBrownba1b41b2011-01-14 09:14:34 +11002828 if (slot >= rdev->mddev->raid_disks &&
2829 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2830 return -ENOSPC;
2831
Neil Brown6c2fce22008-06-28 08:31:31 +10002832 rdev->raid_disk = slot;
2833 if (test_bit(In_sync, &rdev->flags))
2834 rdev->saved_raid_disk = slot;
2835 else
2836 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002837 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002838 err = rdev->mddev->pers->
2839 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002840 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002841 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002842 return err;
Neil Brown52664732008-06-28 08:31:44 +10002843 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002844 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002845 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002846 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002847 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002848 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002849 if (slot >= rdev->mddev->raid_disks &&
2850 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002851 return -ENOSPC;
2852 rdev->raid_disk = slot;
2853 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002854 clear_bit(Faulty, &rdev->flags);
2855 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002856 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002857 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002858 }
NeilBrown014236d2006-01-06 00:20:55 -08002859 return len;
2860}
2861
2862
2863static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002864__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002865
NeilBrown93c8cad2006-01-06 00:20:56 -08002866static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002867offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002868{
Andrew Morton6961ece2006-01-06 00:20:59 -08002869 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002870}
2871
2872static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002873offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002874{
NeilBrownc6563a82012-05-21 09:27:00 +10002875 unsigned long long offset;
2876 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002877 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002878 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002879 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002880 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002881 /* Must set offset before size, so overlap checks
2882 * can be sane */
2883 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002884 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10002885 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08002886 return len;
2887}
2888
2889static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002890__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002891
NeilBrownc6563a82012-05-21 09:27:00 +10002892static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2893{
2894 return sprintf(page, "%llu\n",
2895 (unsigned long long)rdev->new_data_offset);
2896}
2897
2898static ssize_t new_offset_store(struct md_rdev *rdev,
2899 const char *buf, size_t len)
2900{
2901 unsigned long long new_offset;
2902 struct mddev *mddev = rdev->mddev;
2903
2904 if (strict_strtoull(buf, 10, &new_offset) < 0)
2905 return -EINVAL;
2906
2907 if (mddev->sync_thread)
2908 return -EBUSY;
2909 if (new_offset == rdev->data_offset)
2910 /* reset is always permitted */
2911 ;
2912 else if (new_offset > rdev->data_offset) {
2913 /* must not push array size beyond rdev_sectors */
2914 if (new_offset - rdev->data_offset
2915 + mddev->dev_sectors > rdev->sectors)
2916 return -E2BIG;
2917 }
2918 /* Metadata worries about other space details. */
2919
2920 /* decreasing the offset is inconsistent with a backwards
2921 * reshape.
2922 */
2923 if (new_offset < rdev->data_offset &&
2924 mddev->reshape_backwards)
2925 return -EINVAL;
2926 /* Increasing offset is inconsistent with forwards
2927 * reshape. reshape_direction should be set to
2928 * 'backwards' first.
2929 */
2930 if (new_offset > rdev->data_offset &&
2931 !mddev->reshape_backwards)
2932 return -EINVAL;
2933
2934 if (mddev->pers && mddev->persistent &&
2935 !super_types[mddev->major_version]
2936 .allow_new_offset(rdev, new_offset))
2937 return -E2BIG;
2938 rdev->new_data_offset = new_offset;
2939 if (new_offset > rdev->data_offset)
2940 mddev->reshape_backwards = 1;
2941 else if (new_offset < rdev->data_offset)
2942 mddev->reshape_backwards = 0;
2943
2944 return len;
2945}
2946static struct rdev_sysfs_entry rdev_new_offset =
2947__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2948
NeilBrown83303b62006-01-06 00:21:06 -08002949static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002950rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002951{
Andre Nolldd8ac332009-03-31 14:33:13 +11002952 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002953}
2954
NeilBrownc5d79ad2008-02-06 01:39:54 -08002955static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2956{
2957 /* check if two start/length pairs overlap */
2958 if (s1+l1 <= s2)
2959 return 0;
2960 if (s2+l2 <= s1)
2961 return 0;
2962 return 1;
2963}
2964
Dan Williamsb522adc2009-03-31 15:00:31 +11002965static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2966{
2967 unsigned long long blocks;
2968 sector_t new;
2969
2970 if (strict_strtoull(buf, 10, &blocks) < 0)
2971 return -EINVAL;
2972
2973 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2974 return -EINVAL; /* sector conversion overflow */
2975
2976 new = blocks * 2;
2977 if (new != blocks * 2)
2978 return -EINVAL; /* unsigned long long to sector_t overflow */
2979
2980 *sectors = new;
2981 return 0;
2982}
2983
NeilBrown83303b62006-01-06 00:21:06 -08002984static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002985rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002986{
NeilBrownfd01b882011-10-11 16:47:53 +11002987 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002988 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002989 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002990
Dan Williamsb522adc2009-03-31 15:00:31 +11002991 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002992 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10002993 if (rdev->data_offset != rdev->new_data_offset)
2994 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002995 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002996 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002997 sectors = super_types[my_mddev->major_version].
2998 rdev_size_change(rdev, sectors);
2999 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003000 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003001 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003002 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003003 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003004 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003005 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003006 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003007
Andre Nolldd8ac332009-03-31 14:33:13 +11003008 rdev->sectors = sectors;
3009 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003010 /* need to check that all other rdevs with the same ->bdev
3011 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003012 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003013 * we have to change it back, we will have the lock again.
3014 */
NeilBrownfd01b882011-10-11 16:47:53 +11003015 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003016 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003017 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003018
NeilBrown27c529b2008-03-04 14:29:33 -08003019 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003020 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003021 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003022
3023 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003024 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003025 if (rdev->bdev == rdev2->bdev &&
3026 rdev != rdev2 &&
3027 overlaps(rdev->data_offset, rdev->sectors,
3028 rdev2->data_offset,
3029 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003030 overlap = 1;
3031 break;
3032 }
3033 mddev_unlock(mddev);
3034 if (overlap) {
3035 mddev_put(mddev);
3036 break;
3037 }
3038 }
NeilBrown27c529b2008-03-04 14:29:33 -08003039 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003040 if (overlap) {
3041 /* Someone else could have slipped in a size
3042 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003043 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003044 * safe, and trust userspace not to race with
3045 * itself
3046 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003047 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003048 return -EBUSY;
3049 }
3050 }
NeilBrown83303b62006-01-06 00:21:06 -08003051 return len;
3052}
3053
3054static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003055__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003056
Dan Williams06e3c812009-12-12 21:17:12 -07003057
NeilBrown3cb03002011-10-11 16:45:26 +11003058static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003059{
3060 unsigned long long recovery_start = rdev->recovery_offset;
3061
3062 if (test_bit(In_sync, &rdev->flags) ||
3063 recovery_start == MaxSector)
3064 return sprintf(page, "none\n");
3065
3066 return sprintf(page, "%llu\n", recovery_start);
3067}
3068
NeilBrown3cb03002011-10-11 16:45:26 +11003069static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003070{
3071 unsigned long long recovery_start;
3072
3073 if (cmd_match(buf, "none"))
3074 recovery_start = MaxSector;
3075 else if (strict_strtoull(buf, 10, &recovery_start))
3076 return -EINVAL;
3077
3078 if (rdev->mddev->pers &&
3079 rdev->raid_disk >= 0)
3080 return -EBUSY;
3081
3082 rdev->recovery_offset = recovery_start;
3083 if (recovery_start == MaxSector)
3084 set_bit(In_sync, &rdev->flags);
3085 else
3086 clear_bit(In_sync, &rdev->flags);
3087 return len;
3088}
3089
3090static struct rdev_sysfs_entry rdev_recovery_start =
3091__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3092
NeilBrown16c791a2011-07-28 11:31:47 +10003093
3094static ssize_t
3095badblocks_show(struct badblocks *bb, char *page, int unack);
3096static ssize_t
3097badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3098
NeilBrown3cb03002011-10-11 16:45:26 +11003099static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003100{
3101 return badblocks_show(&rdev->badblocks, page, 0);
3102}
NeilBrown3cb03002011-10-11 16:45:26 +11003103static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003104{
NeilBrownde393cd2011-07-28 11:31:48 +10003105 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3106 /* Maybe that ack was all we needed */
3107 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3108 wake_up(&rdev->blocked_wait);
3109 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003110}
3111static struct rdev_sysfs_entry rdev_bad_blocks =
3112__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3113
3114
NeilBrown3cb03002011-10-11 16:45:26 +11003115static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003116{
3117 return badblocks_show(&rdev->badblocks, page, 1);
3118}
NeilBrown3cb03002011-10-11 16:45:26 +11003119static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003120{
3121 return badblocks_store(&rdev->badblocks, page, len, 1);
3122}
3123static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3124__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3125
NeilBrown86e6ffd2005-11-08 21:39:24 -08003126static struct attribute *rdev_default_attrs[] = {
3127 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003128 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003129 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003130 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003131 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003132 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003133 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003134 &rdev_bad_blocks.attr,
3135 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003136 NULL,
3137};
3138static ssize_t
3139rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3140{
3141 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003142 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003143 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003144 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003145
3146 if (!entry->show)
3147 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003148
3149 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3150 if (!rv) {
3151 if (rdev->mddev == NULL)
3152 rv = -EBUSY;
3153 else
3154 rv = entry->show(rdev, page);
3155 mddev_unlock(mddev);
3156 }
3157 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158}
3159
3160static ssize_t
3161rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3162 const char *page, size_t length)
3163{
3164 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003165 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003166 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003167 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003168
3169 if (!entry->store)
3170 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003171 if (!capable(CAP_SYS_ADMIN))
3172 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003173 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003174 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003175 if (rdev->mddev == NULL)
3176 rv = -EBUSY;
3177 else
3178 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003179 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003180 }
3181 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003182}
3183
3184static void rdev_free(struct kobject *ko)
3185{
NeilBrown3cb03002011-10-11 16:45:26 +11003186 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003187 kfree(rdev);
3188}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003189static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003190 .show = rdev_attr_show,
3191 .store = rdev_attr_store,
3192};
3193static struct kobj_type rdev_ktype = {
3194 .release = rdev_free,
3195 .sysfs_ops = &rdev_sysfs_ops,
3196 .default_attrs = rdev_default_attrs,
3197};
3198
NeilBrown3cb03002011-10-11 16:45:26 +11003199int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003200{
3201 rdev->desc_nr = -1;
3202 rdev->saved_raid_disk = -1;
3203 rdev->raid_disk = -1;
3204 rdev->flags = 0;
3205 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003206 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003207 rdev->sb_events = 0;
3208 rdev->last_read_error.tv_sec = 0;
3209 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003210 rdev->sb_loaded = 0;
3211 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003212 atomic_set(&rdev->nr_pending, 0);
3213 atomic_set(&rdev->read_errors, 0);
3214 atomic_set(&rdev->corrected_errors, 0);
3215
3216 INIT_LIST_HEAD(&rdev->same_set);
3217 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003218
3219 /* Add space to store bad block list.
3220 * This reserves the space even on arrays where it cannot
3221 * be used - I wonder if that matters
3222 */
3223 rdev->badblocks.count = 0;
3224 rdev->badblocks.shift = 0;
3225 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3226 seqlock_init(&rdev->badblocks.lock);
3227 if (rdev->badblocks.page == NULL)
3228 return -ENOMEM;
3229
3230 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003231}
3232EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
3234 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3235 *
3236 * mark the device faulty if:
3237 *
3238 * - the device is nonexistent (zero size)
3239 * - the device has no valid superblock
3240 *
3241 * a faulty rdev _never_ has rdev->sb set.
3242 */
NeilBrown3cb03002011-10-11 16:45:26 +11003243static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
3245 char b[BDEVNAME_SIZE];
3246 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003247 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 sector_t size;
3249
NeilBrown9ffae0c2006-01-06 00:20:32 -08003250 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (!rdev) {
3252 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3253 return ERR_PTR(-ENOMEM);
3254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
NeilBrown2230dfe2011-07-28 11:31:46 +10003256 err = md_rdev_init(rdev);
3257 if (err)
3258 goto abort_free;
3259 err = alloc_disk_sb(rdev);
3260 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 goto abort_free;
3262
NeilBrownc5d79ad2008-02-06 01:39:54 -08003263 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (err)
3265 goto abort_free;
3266
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003267 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003268
Mike Snitzer77304d22010-11-08 14:39:12 +01003269 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (!size) {
3271 printk(KERN_WARNING
3272 "md: %s has zero or unknown size, marking faulty!\n",
3273 bdevname(rdev->bdev,b));
3274 err = -EINVAL;
3275 goto abort_free;
3276 }
3277
3278 if (super_format >= 0) {
3279 err = super_types[super_format].
3280 load_super(rdev, NULL, super_minor);
3281 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003282 printk(KERN_WARNING
3283 "md: %s does not have a valid v%d.%d "
3284 "superblock, not importing!\n",
3285 bdevname(rdev->bdev,b),
3286 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 goto abort_free;
3288 }
3289 if (err < 0) {
3290 printk(KERN_WARNING
3291 "md: could not read %s's sb, not importing!\n",
3292 bdevname(rdev->bdev,b));
3293 goto abort_free;
3294 }
3295 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003296 if (super_format == -1)
3297 /* hot-add for 0.90, or non-persistent: so no badblocks */
3298 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return rdev;
3301
3302abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003303 if (rdev->bdev)
3304 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003305 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 kfree(rdev);
3307 return ERR_PTR(err);
3308}
3309
3310/*
3311 * Check a full RAID array for plausibility
3312 */
3313
3314
NeilBrownfd01b882011-10-11 16:47:53 +11003315static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003318 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 char b[BDEVNAME_SIZE];
3320
3321 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003322 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 switch (super_types[mddev->major_version].
3324 load_super(rdev, freshest, mddev->minor_version)) {
3325 case 1:
3326 freshest = rdev;
3327 break;
3328 case 0:
3329 break;
3330 default:
3331 printk( KERN_ERR \
3332 "md: fatal superblock inconsistency in %s"
3333 " -- removing from array\n",
3334 bdevname(rdev->bdev,b));
3335 kick_rdev_from_array(rdev);
3336 }
3337
3338
3339 super_types[mddev->major_version].
3340 validate_super(mddev, freshest);
3341
3342 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003343 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003344 if (mddev->max_disks &&
3345 (rdev->desc_nr >= mddev->max_disks ||
3346 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003347 printk(KERN_WARNING
3348 "md: %s: %s: only %d devices permitted\n",
3349 mdname(mddev), bdevname(rdev->bdev, b),
3350 mddev->max_disks);
3351 kick_rdev_from_array(rdev);
3352 continue;
3353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (rdev != freshest)
3355 if (super_types[mddev->major_version].
3356 validate_super(mddev, rdev)) {
3357 printk(KERN_WARNING "md: kicking non-fresh %s"
3358 " from array!\n",
3359 bdevname(rdev->bdev,b));
3360 kick_rdev_from_array(rdev);
3361 continue;
3362 }
3363 if (mddev->level == LEVEL_MULTIPATH) {
3364 rdev->desc_nr = i++;
3365 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003366 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003367 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003368 rdev->raid_disk = -1;
3369 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
NeilBrown72e02072009-12-14 12:49:55 +11003374/* Read a fixed-point number.
3375 * Numbers in sysfs attributes should be in "standard" units where
3376 * possible, so time should be in seconds.
3377 * However we internally use a a much smaller unit such as
3378 * milliseconds or jiffies.
3379 * This function takes a decimal number with a possible fractional
3380 * component, and produces an integer which is the result of
3381 * multiplying that number by 10^'scale'.
3382 * all without any floating-point arithmetic.
3383 */
3384int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3385{
3386 unsigned long result = 0;
3387 long decimals = -1;
3388 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3389 if (*cp == '.')
3390 decimals = 0;
3391 else if (decimals < scale) {
3392 unsigned int value;
3393 value = *cp - '0';
3394 result = result * 10 + value;
3395 if (decimals >= 0)
3396 decimals++;
3397 }
3398 cp++;
3399 }
3400 if (*cp == '\n')
3401 cp++;
3402 if (*cp)
3403 return -EINVAL;
3404 if (decimals < 0)
3405 decimals = 0;
3406 while (decimals < scale) {
3407 result *= 10;
3408 decimals ++;
3409 }
3410 *res = result;
3411 return 0;
3412}
3413
3414
NeilBrown19052c02008-08-05 15:54:13 +10003415static void md_safemode_timeout(unsigned long data);
3416
NeilBrowneae17012005-11-08 21:39:23 -08003417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003418safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003419{
3420 int msec = (mddev->safemode_delay*1000)/HZ;
3421 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3422}
3423static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003424safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003425{
NeilBrown16f17b32006-06-26 00:27:37 -07003426 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003427
NeilBrown72e02072009-12-14 12:49:55 +11003428 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003429 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003430 if (msec == 0)
3431 mddev->safemode_delay = 0;
3432 else {
NeilBrown19052c02008-08-05 15:54:13 +10003433 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003434 mddev->safemode_delay = (msec*HZ)/1000;
3435 if (mddev->safemode_delay == 0)
3436 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003437 if (mddev->safemode_delay < old_delay)
3438 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003439 }
3440 return len;
3441}
3442static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003443__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003444
3445static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003446level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003447{
NeilBrown84fc4b52011-10-11 16:49:58 +11003448 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003449 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003450 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003451 else if (mddev->clevel[0])
3452 return sprintf(page, "%s\n", mddev->clevel);
3453 else if (mddev->level != LEVEL_NONE)
3454 return sprintf(page, "%d\n", mddev->level);
3455 else
3456 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003457}
3458
NeilBrownd9d166c2006-01-06 00:20:51 -08003459static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003460level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003461{
Dan Williamsf2859af2010-05-02 10:04:16 -07003462 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003463 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003464 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003465 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003466 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003467 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003468
3469 if (mddev->pers == NULL) {
3470 if (len == 0)
3471 return 0;
3472 if (len >= sizeof(mddev->clevel))
3473 return -ENOSPC;
3474 strncpy(mddev->clevel, buf, len);
3475 if (mddev->clevel[len-1] == '\n')
3476 len--;
3477 mddev->clevel[len] = 0;
3478 mddev->level = LEVEL_NONE;
3479 return rv;
3480 }
3481
3482 /* request to change the personality. Need to ensure:
3483 * - array is not engaged in resync/recovery/reshape
3484 * - old personality can be suspended
3485 * - new personality will access other array.
3486 */
3487
NeilBrownbb4f1e92010-08-08 21:18:03 +10003488 if (mddev->sync_thread ||
3489 mddev->reshape_position != MaxSector ||
3490 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003491 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003492
3493 if (!mddev->pers->quiesce) {
3494 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3495 mdname(mddev), mddev->pers->name);
3496 return -EINVAL;
3497 }
3498
3499 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003500 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003501 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003502 strncpy(clevel, buf, len);
3503 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003504 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003505 clevel[len] = 0;
3506 if (strict_strtol(clevel, 10, &level))
3507 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003508
Dan Williamsf2859af2010-05-02 10:04:16 -07003509 if (request_module("md-%s", clevel) != 0)
3510 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003511 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003512 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003513 if (!pers || !try_module_get(pers->owner)) {
3514 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003515 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003516 return -EINVAL;
3517 }
3518 spin_unlock(&pers_lock);
3519
3520 if (pers == mddev->pers) {
3521 /* Nothing to do! */
3522 module_put(pers->owner);
3523 return rv;
3524 }
3525 if (!pers->takeover) {
3526 module_put(pers->owner);
3527 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003528 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003529 return -EINVAL;
3530 }
3531
NeilBrowndafb20f2012-03-19 12:46:39 +11003532 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003533 rdev->new_raid_disk = rdev->raid_disk;
3534
NeilBrown245f46c2009-03-31 14:39:39 +11003535 /* ->takeover must set new_* and/or delta_disks
3536 * if it succeeds, and may set them when it fails.
3537 */
3538 priv = pers->takeover(mddev);
3539 if (IS_ERR(priv)) {
3540 mddev->new_level = mddev->level;
3541 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003542 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003543 mddev->raid_disks -= mddev->delta_disks;
3544 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003545 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003546 module_put(pers->owner);
3547 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003548 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003549 return PTR_ERR(priv);
3550 }
3551
3552 /* Looks like we have a winner */
3553 mddev_suspend(mddev);
3554 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003555
3556 if (mddev->pers->sync_request == NULL &&
3557 pers->sync_request != NULL) {
3558 /* need to add the md_redundancy_group */
3559 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3560 printk(KERN_WARNING
3561 "md: cannot register extra attributes for %s\n",
3562 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003563 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003564 }
3565 if (mddev->pers->sync_request != NULL &&
3566 pers->sync_request == NULL) {
3567 /* need to remove the md_redundancy_group */
3568 if (mddev->to_remove == NULL)
3569 mddev->to_remove = &md_redundancy_group;
3570 }
3571
Trela Maciej54071b32010-03-08 16:02:42 +11003572 if (mddev->pers->sync_request == NULL &&
3573 mddev->external) {
3574 /* We are converting from a no-redundancy array
3575 * to a redundancy array and metadata is managed
3576 * externally so we need to be sure that writes
3577 * won't block due to a need to transition
3578 * clean->dirty
3579 * until external management is started.
3580 */
3581 mddev->in_sync = 0;
3582 mddev->safemode_delay = 0;
3583 mddev->safemode = 0;
3584 }
3585
NeilBrowndafb20f2012-03-19 12:46:39 +11003586 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003587 if (rdev->raid_disk < 0)
3588 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003589 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003590 rdev->new_raid_disk = -1;
3591 if (rdev->new_raid_disk == rdev->raid_disk)
3592 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003593 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003594 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003595 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003596 if (rdev->raid_disk < 0)
3597 continue;
3598 if (rdev->new_raid_disk == rdev->raid_disk)
3599 continue;
3600 rdev->raid_disk = rdev->new_raid_disk;
3601 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003602 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003603 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003604 if (sysfs_link_rdev(mddev, rdev))
3605 printk(KERN_WARNING "md: cannot register rd%d"
3606 " for %s after level change\n",
3607 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003608 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003609 }
3610
3611 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003612 mddev->pers = pers;
3613 mddev->private = priv;
3614 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3615 mddev->level = mddev->new_level;
3616 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003617 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003618 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003619 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003620 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003621 if (mddev->pers->sync_request == NULL) {
3622 /* this is now an array without redundancy, so
3623 * it must always be in_sync
3624 */
3625 mddev->in_sync = 1;
3626 del_timer_sync(&mddev->safemode_timer);
3627 }
NeilBrown245f46c2009-03-31 14:39:39 +11003628 pers->run(mddev);
NeilBrown245f46c2009-03-31 14:39:39 +11003629 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003630 mddev_resume(mddev);
Maciej Trela5cac78612010-04-14 17:17:39 +10003631 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003632 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003633 return rv;
3634}
3635
3636static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003637__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003638
NeilBrownd4dbd022006-06-26 00:27:59 -07003639
3640static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003641layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003642{
3643 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003644 if (mddev->reshape_position != MaxSector &&
3645 mddev->layout != mddev->new_layout)
3646 return sprintf(page, "%d (%d)\n",
3647 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003648 return sprintf(page, "%d\n", mddev->layout);
3649}
3650
3651static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003652layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003653{
3654 char *e;
3655 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003656
3657 if (!*buf || (*e && *e != '\n'))
3658 return -EINVAL;
3659
NeilBrownb3546032009-03-31 14:56:41 +11003660 if (mddev->pers) {
3661 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003662 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003663 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003664 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003665 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003666 if (err) {
3667 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003668 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003669 }
NeilBrownb3546032009-03-31 14:56:41 +11003670 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003671 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003672 if (mddev->reshape_position == MaxSector)
3673 mddev->layout = n;
3674 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003675 return len;
3676}
3677static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003678__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003679
3680
NeilBrowneae17012005-11-08 21:39:23 -08003681static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003682raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003683{
NeilBrownbb636542005-11-08 21:39:45 -08003684 if (mddev->raid_disks == 0)
3685 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003686 if (mddev->reshape_position != MaxSector &&
3687 mddev->delta_disks != 0)
3688 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3689 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003690 return sprintf(page, "%d\n", mddev->raid_disks);
3691}
3692
NeilBrownfd01b882011-10-11 16:47:53 +11003693static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003694
3695static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003696raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003697{
NeilBrownda943b992006-01-06 00:20:54 -08003698 char *e;
3699 int rv = 0;
3700 unsigned long n = simple_strtoul(buf, &e, 10);
3701
3702 if (!*buf || (*e && *e != '\n'))
3703 return -EINVAL;
3704
3705 if (mddev->pers)
3706 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003707 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003708 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003709 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003710
3711 rdev_for_each(rdev, mddev) {
3712 if (olddisks < n &&
3713 rdev->data_offset < rdev->new_data_offset)
3714 return -EINVAL;
3715 if (olddisks > n &&
3716 rdev->data_offset > rdev->new_data_offset)
3717 return -EINVAL;
3718 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003719 mddev->delta_disks = n - olddisks;
3720 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003721 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003722 } else
NeilBrownda943b992006-01-06 00:20:54 -08003723 mddev->raid_disks = n;
3724 return rv ? rv : len;
3725}
3726static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003727__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003728
NeilBrown24dd4692005-11-08 21:39:26 -08003729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003730chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003731{
NeilBrown08a02ec2007-05-09 02:35:38 -07003732 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003733 mddev->chunk_sectors != mddev->new_chunk_sectors)
3734 return sprintf(page, "%d (%d)\n",
3735 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003736 mddev->chunk_sectors << 9);
3737 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003738}
3739
3740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003741chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003742{
NeilBrown3b343802006-01-06 00:20:47 -08003743 char *e;
3744 unsigned long n = simple_strtoul(buf, &e, 10);
3745
NeilBrown3b343802006-01-06 00:20:47 -08003746 if (!*buf || (*e && *e != '\n'))
3747 return -EINVAL;
3748
NeilBrownb3546032009-03-31 14:56:41 +11003749 if (mddev->pers) {
3750 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003751 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003752 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003753 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003754 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003755 if (err) {
3756 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003757 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003758 }
NeilBrownb3546032009-03-31 14:56:41 +11003759 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003760 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003761 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003762 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003763 }
NeilBrown3b343802006-01-06 00:20:47 -08003764 return len;
3765}
3766static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003767__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003768
NeilBrowna94213b2006-06-26 00:28:00 -07003769static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003770resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003771{
NeilBrownd1a7c502009-03-31 15:24:32 +11003772 if (mddev->recovery_cp == MaxSector)
3773 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003774 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3775}
3776
3777static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003778resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003779{
NeilBrowna94213b2006-06-26 00:28:00 -07003780 char *e;
3781 unsigned long long n = simple_strtoull(buf, &e, 10);
3782
NeilBrownb0986362011-05-11 15:52:21 +10003783 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003784 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003785 if (cmd_match(buf, "none"))
3786 n = MaxSector;
3787 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003788 return -EINVAL;
3789
3790 mddev->recovery_cp = n;
3791 return len;
3792}
3793static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003794__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003795
NeilBrown9e653b62006-06-26 00:27:58 -07003796/*
3797 * The array state can be:
3798 *
3799 * clear
3800 * No devices, no size, no level
3801 * Equivalent to STOP_ARRAY ioctl
3802 * inactive
3803 * May have some settings, but array is not active
3804 * all IO results in error
3805 * When written, doesn't tear down array, but just stops it
3806 * suspended (not supported yet)
3807 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003808 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003809 * readonly
3810 * no resync can happen. no superblocks get written.
3811 * write requests fail
3812 * read-auto
3813 * like readonly, but behaves like 'clean' on a write request.
3814 *
3815 * clean - no pending writes, but otherwise active.
3816 * When written to inactive array, starts without resync
3817 * If a write request arrives then
3818 * if metadata is known, mark 'dirty' and switch to 'active'.
3819 * if not known, block and switch to write-pending
3820 * If written to an active array that has pending writes, then fails.
3821 * active
3822 * fully active: IO and resync can be happening.
3823 * When written to inactive array, starts with resync
3824 *
3825 * write-pending
3826 * clean, but writes are blocked waiting for 'active' to be written.
3827 *
3828 * active-idle
3829 * like active, but no writes have been seen for a while (100msec).
3830 *
3831 */
3832enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3833 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003834static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003835 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3836 "write-pending", "active-idle", NULL };
3837
3838static int match_word(const char *word, char **list)
3839{
3840 int n;
3841 for (n=0; list[n]; n++)
3842 if (cmd_match(word, list[n]))
3843 break;
3844 return n;
3845}
3846
3847static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003848array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003849{
3850 enum array_state st = inactive;
3851
3852 if (mddev->pers)
3853 switch(mddev->ro) {
3854 case 1:
3855 st = readonly;
3856 break;
3857 case 2:
3858 st = read_auto;
3859 break;
3860 case 0:
3861 if (mddev->in_sync)
3862 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003863 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003864 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003865 else if (mddev->safemode)
3866 st = active_idle;
3867 else
3868 st = active;
3869 }
3870 else {
3871 if (list_empty(&mddev->disks) &&
3872 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003873 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003874 st = clear;
3875 else
3876 st = inactive;
3877 }
3878 return sprintf(page, "%s\n", array_states[st]);
3879}
3880
NeilBrowna05b7ea2012-07-19 15:59:18 +10003881static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3882static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
NeilBrownfd01b882011-10-11 16:47:53 +11003883static int do_md_run(struct mddev * mddev);
3884static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003885
3886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003887array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003888{
3889 int err = -EINVAL;
3890 enum array_state st = match_word(buf, array_states);
3891 switch(st) {
3892 case bad_word:
3893 break;
3894 case clear:
3895 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003896 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003897 return -EBUSY;
NeilBrowna05b7ea2012-07-19 15:59:18 +10003898 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003899 break;
3900 case inactive:
3901 /* stopping an active array */
3902 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003903 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003904 return -EBUSY;
NeilBrowna05b7ea2012-07-19 15:59:18 +10003905 err = do_md_stop(mddev, 2, NULL);
NeilBrowne6910632008-02-06 01:39:51 -08003906 } else
3907 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003908 break;
3909 case suspended:
3910 break; /* not supported yet */
3911 case readonly:
3912 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003913 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07003914 else {
3915 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003916 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003917 err = do_md_run(mddev);
3918 }
3919 break;
3920 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003921 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003922 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10003923 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11003924 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003925 err = restart_array(mddev);
3926 if (err == 0) {
3927 mddev->ro = 2;
3928 set_disk_ro(mddev->gendisk, 0);
3929 }
NeilBrown9e653b62006-06-26 00:27:58 -07003930 } else {
3931 mddev->ro = 2;
3932 err = do_md_run(mddev);
3933 }
3934 break;
3935 case clean:
3936 if (mddev->pers) {
3937 restart_array(mddev);
3938 spin_lock_irq(&mddev->write_lock);
3939 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003940 if (mddev->in_sync == 0) {
3941 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003942 if (mddev->safemode == 1)
3943 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003944 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003945 }
3946 err = 0;
3947 } else
3948 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003949 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003950 } else
3951 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003952 break;
3953 case active:
3954 if (mddev->pers) {
3955 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003956 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003957 wake_up(&mddev->sb_wait);
3958 err = 0;
3959 } else {
3960 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003961 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003962 err = do_md_run(mddev);
3963 }
3964 break;
3965 case write_pending:
3966 case active_idle:
3967 /* these cannot be set */
3968 break;
3969 }
3970 if (err)
3971 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003972 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003973 if (mddev->hold_active == UNTIL_IOCTL)
3974 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003975 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003976 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003977 }
NeilBrown9e653b62006-06-26 00:27:58 -07003978}
NeilBrown80ca3a42006-07-10 04:44:18 -07003979static struct md_sysfs_entry md_array_state =
3980__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003981
NeilBrown6d7ff7382006-01-06 00:21:16 -08003982static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003983max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003984 return sprintf(page, "%d\n",
3985 atomic_read(&mddev->max_corr_read_errors));
3986}
3987
3988static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003989max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003990{
3991 char *e;
3992 unsigned long n = simple_strtoul(buf, &e, 10);
3993
3994 if (*buf && (*e == 0 || *e == '\n')) {
3995 atomic_set(&mddev->max_corr_read_errors, n);
3996 return len;
3997 }
3998 return -EINVAL;
3999}
4000
4001static struct md_sysfs_entry max_corr_read_errors =
4002__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4003 max_corrected_read_errors_store);
4004
4005static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004006null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004007{
4008 return -EINVAL;
4009}
4010
4011static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004012new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004013{
4014 /* buf must be %d:%d\n? giving major and minor numbers */
4015 /* The new device is added to the array.
4016 * If the array has a persistent superblock, we read the
4017 * superblock to initialise info and check validity.
4018 * Otherwise, only checking done is that in bind_rdev_to_array,
4019 * which mainly checks size.
4020 */
4021 char *e;
4022 int major = simple_strtoul(buf, &e, 10);
4023 int minor;
4024 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004025 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004026 int err;
4027
4028 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4029 return -EINVAL;
4030 minor = simple_strtoul(e+1, &e, 10);
4031 if (*e && *e != '\n')
4032 return -EINVAL;
4033 dev = MKDEV(major, minor);
4034 if (major != MAJOR(dev) ||
4035 minor != MINOR(dev))
4036 return -EOVERFLOW;
4037
4038
4039 if (mddev->persistent) {
4040 rdev = md_import_device(dev, mddev->major_version,
4041 mddev->minor_version);
4042 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004043 struct md_rdev *rdev0
4044 = list_entry(mddev->disks.next,
4045 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004046 err = super_types[mddev->major_version]
4047 .load_super(rdev, rdev0, mddev->minor_version);
4048 if (err < 0)
4049 goto out;
4050 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004051 } else if (mddev->external)
4052 rdev = md_import_device(dev, -2, -1);
4053 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004054 rdev = md_import_device(dev, -1, -1);
4055
4056 if (IS_ERR(rdev))
4057 return PTR_ERR(rdev);
4058 err = bind_rdev_to_array(rdev, mddev);
4059 out:
4060 if (err)
4061 export_rdev(rdev);
4062 return err ? err : len;
4063}
4064
4065static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004066__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004067
4068static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004069bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004070{
4071 char *end;
4072 unsigned long chunk, end_chunk;
4073
4074 if (!mddev->bitmap)
4075 goto out;
4076 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4077 while (*buf) {
4078 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4079 if (buf == end) break;
4080 if (*end == '-') { /* range */
4081 buf = end + 1;
4082 end_chunk = simple_strtoul(buf, &end, 0);
4083 if (buf == end) break;
4084 }
4085 if (*end && !isspace(*end)) break;
4086 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004087 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004088 }
4089 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4090out:
4091 return len;
4092}
4093
4094static struct md_sysfs_entry md_bitmap =
4095__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4096
4097static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004098size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004099{
Andre Noll58c0fed2009-03-31 14:33:13 +11004100 return sprintf(page, "%llu\n",
4101 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004102}
4103
NeilBrownfd01b882011-10-11 16:47:53 +11004104static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004105
4106static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004107size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004108{
4109 /* If array is inactive, we can reduce the component size, but
4110 * not increase it (except from 0).
4111 * If array is active, we can try an on-line resize
4112 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004113 sector_t sectors;
4114 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004115
Andre Noll58c0fed2009-03-31 14:33:13 +11004116 if (err < 0)
4117 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004118 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004119 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004120 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004121 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004122 if (mddev->dev_sectors == 0 ||
4123 mddev->dev_sectors > sectors)
4124 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004125 else
4126 err = -ENOSPC;
4127 }
4128 return err ? err : len;
4129}
4130
4131static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004132__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004133
NeilBrown8bb93aa2006-01-06 00:20:50 -08004134
4135/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004136 * This is one of
4137 * 'none' for arrays with no metadata (good luck...)
4138 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004139 * or N.M for internally known formats
4140 */
4141static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004142metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004143{
4144 if (mddev->persistent)
4145 return sprintf(page, "%d.%d\n",
4146 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004147 else if (mddev->external)
4148 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004149 else
4150 return sprintf(page, "none\n");
4151}
4152
4153static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004154metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004155{
4156 int major, minor;
4157 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004158 /* Changing the details of 'external' metadata is
4159 * always permitted. Otherwise there must be
4160 * no devices attached to the array.
4161 */
4162 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4163 ;
4164 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004165 return -EBUSY;
4166
4167 if (cmd_match(buf, "none")) {
4168 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004169 mddev->external = 0;
4170 mddev->major_version = 0;
4171 mddev->minor_version = 90;
4172 return len;
4173 }
4174 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004175 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004176 if (namelen >= sizeof(mddev->metadata_type))
4177 namelen = sizeof(mddev->metadata_type)-1;
4178 strncpy(mddev->metadata_type, buf+9, namelen);
4179 mddev->metadata_type[namelen] = 0;
4180 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4181 mddev->metadata_type[--namelen] = 0;
4182 mddev->persistent = 0;
4183 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004184 mddev->major_version = 0;
4185 mddev->minor_version = 90;
4186 return len;
4187 }
4188 major = simple_strtoul(buf, &e, 10);
4189 if (e==buf || *e != '.')
4190 return -EINVAL;
4191 buf = e+1;
4192 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004193 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004194 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004195 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004196 return -ENOENT;
4197 mddev->major_version = major;
4198 mddev->minor_version = minor;
4199 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004200 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004201 return len;
4202}
4203
4204static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004205__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004206
NeilBrowna35b0d62006-01-06 00:20:49 -08004207static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004208action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004209{
NeilBrown7eec3142005-11-08 21:39:44 -08004210 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004211 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4212 type = "frozen";
4213 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004214 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004215 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4216 type = "reshape";
4217 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004218 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4219 type = "resync";
4220 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4221 type = "check";
4222 else
4223 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004224 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004225 type = "recover";
4226 }
4227 return sprintf(page, "%s\n", type);
4228}
4229
NeilBrownfd01b882011-10-11 16:47:53 +11004230static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004231
NeilBrown24dd4692005-11-08 21:39:26 -08004232static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004233action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004234{
NeilBrown7eec3142005-11-08 21:39:44 -08004235 if (!mddev->pers || !mddev->pers->sync_request)
4236 return -EINVAL;
4237
NeilBrownb6a9ce62009-05-26 09:41:17 +10004238 if (cmd_match(page, "frozen"))
4239 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4240 else
4241 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4242
4243 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004244 if (mddev->sync_thread) {
4245 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004246 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004247 }
NeilBrown03c902e2006-01-06 00:20:46 -08004248 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4249 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004250 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004251 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004252 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004253 else if (cmd_match(page, "recover")) {
4254 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4256 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004257 int err;
4258 if (mddev->pers->start_reshape == NULL)
4259 return -EINVAL;
4260 err = mddev->pers->start_reshape(mddev);
4261 if (err)
4262 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004263 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004264 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004265 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004266 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004267 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004268 return -EINVAL;
4269 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4270 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004271 }
NeilBrown03c902e2006-01-06 00:20:46 -08004272 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004273 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004274 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004275 return len;
4276}
4277
NeilBrown9d888832005-11-08 21:39:26 -08004278static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004279mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004280{
4281 return sprintf(page, "%llu\n",
4282 (unsigned long long) mddev->resync_mismatches);
4283}
4284
NeilBrown80ca3a42006-07-10 04:44:18 -07004285static struct md_sysfs_entry md_scan_mode =
4286__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004287
NeilBrown96de1e62005-11-08 21:39:39 -08004288
NeilBrown80ca3a42006-07-10 04:44:18 -07004289static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004290
NeilBrown88202a02006-01-06 00:21:36 -08004291static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004292sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004293{
4294 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4295 mddev->sync_speed_min ? "local": "system");
4296}
4297
4298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004299sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004300{
4301 int min;
4302 char *e;
4303 if (strncmp(buf, "system", 6)==0) {
4304 mddev->sync_speed_min = 0;
4305 return len;
4306 }
4307 min = simple_strtoul(buf, &e, 10);
4308 if (buf == e || (*e && *e != '\n') || min <= 0)
4309 return -EINVAL;
4310 mddev->sync_speed_min = min;
4311 return len;
4312}
4313
4314static struct md_sysfs_entry md_sync_min =
4315__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4316
4317static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004318sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004319{
4320 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4321 mddev->sync_speed_max ? "local": "system");
4322}
4323
4324static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004325sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004326{
4327 int max;
4328 char *e;
4329 if (strncmp(buf, "system", 6)==0) {
4330 mddev->sync_speed_max = 0;
4331 return len;
4332 }
4333 max = simple_strtoul(buf, &e, 10);
4334 if (buf == e || (*e && *e != '\n') || max <= 0)
4335 return -EINVAL;
4336 mddev->sync_speed_max = max;
4337 return len;
4338}
4339
4340static struct md_sysfs_entry md_sync_max =
4341__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4342
Iustin Popd7f3d292007-10-16 23:30:54 -07004343static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004344degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004345{
4346 return sprintf(page, "%d\n", mddev->degraded);
4347}
4348static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004349
4350static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004351sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004352{
4353 return sprintf(page, "%d\n", mddev->parallel_resync);
4354}
4355
4356static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004357sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004358{
4359 long n;
4360
4361 if (strict_strtol(buf, 10, &n))
4362 return -EINVAL;
4363
4364 if (n != 0 && n != 1)
4365 return -EINVAL;
4366
4367 mddev->parallel_resync = n;
4368
4369 if (mddev->sync_thread)
4370 wake_up(&resync_wait);
4371
4372 return len;
4373}
4374
4375/* force parallel resync, even with shared block devices */
4376static struct md_sysfs_entry md_sync_force_parallel =
4377__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4378 sync_force_parallel_show, sync_force_parallel_store);
4379
4380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004381sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004382{
4383 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004384 if (mddev->curr_resync == 0)
4385 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004386 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4387 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004388 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004389 db = resync - mddev->resync_mark_cnt;
4390 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004391}
4392
NeilBrown80ca3a42006-07-10 04:44:18 -07004393static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004394
4395static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004396sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004397{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004398 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004399
NeilBrownacb180b2009-04-14 16:28:34 +10004400 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4401 return sprintf(page, "none\n");
4402
NeilBrownc804cde2012-05-21 09:28:33 +10004403 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4404 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004405 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004406 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004407 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004408
NeilBrownacb180b2009-04-14 16:28:34 +10004409 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004410 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004411}
4412
NeilBrown80ca3a42006-07-10 04:44:18 -07004413static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004414
NeilBrowne464eaf2006-03-27 01:18:14 -08004415static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004416min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004417{
4418 return sprintf(page, "%llu\n",
4419 (unsigned long long)mddev->resync_min);
4420}
4421static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004422min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004423{
4424 unsigned long long min;
4425 if (strict_strtoull(buf, 10, &min))
4426 return -EINVAL;
4427 if (min > mddev->resync_max)
4428 return -EINVAL;
4429 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4430 return -EBUSY;
4431
4432 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004433 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004434 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004435 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004436 return -EINVAL;
4437 }
4438 mddev->resync_min = min;
4439
4440 return len;
4441}
4442
4443static struct md_sysfs_entry md_min_sync =
4444__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4445
4446static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004447max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004448{
4449 if (mddev->resync_max == MaxSector)
4450 return sprintf(page, "max\n");
4451 else
4452 return sprintf(page, "%llu\n",
4453 (unsigned long long)mddev->resync_max);
4454}
4455static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004456max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004457{
4458 if (strncmp(buf, "max", 3) == 0)
4459 mddev->resync_max = MaxSector;
4460 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004461 unsigned long long max;
4462 if (strict_strtoull(buf, 10, &max))
4463 return -EINVAL;
4464 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004465 return -EINVAL;
4466 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004467 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004468 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4469 return -EBUSY;
4470
4471 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004472 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004473 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004474 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004475 return -EINVAL;
4476 }
4477 mddev->resync_max = max;
4478 }
4479 wake_up(&mddev->recovery_wait);
4480 return len;
4481}
4482
4483static struct md_sysfs_entry md_max_sync =
4484__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4485
4486static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004487suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004488{
4489 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4490}
4491
4492static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004493suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004494{
4495 char *e;
4496 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004497 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004498
NeilBrownb8d966e2009-07-01 11:14:04 +10004499 if (mddev->pers == NULL ||
4500 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004501 return -EINVAL;
4502 if (buf == e || (*e && *e != '\n'))
4503 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004504
4505 mddev->suspend_lo = new;
4506 if (new >= old)
4507 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004508 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004509 else {
4510 /* Expanding suspended region - need to wait */
4511 mddev->pers->quiesce(mddev, 1);
4512 mddev->pers->quiesce(mddev, 0);
4513 }
4514 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004515}
4516static struct md_sysfs_entry md_suspend_lo =
4517__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4518
4519
4520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004521suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004522{
4523 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4524}
4525
4526static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004527suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004528{
4529 char *e;
4530 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004531 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004532
NeilBrownb8d966e2009-07-01 11:14:04 +10004533 if (mddev->pers == NULL ||
4534 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004535 return -EINVAL;
4536 if (buf == e || (*e && *e != '\n'))
4537 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004538
4539 mddev->suspend_hi = new;
4540 if (new <= old)
4541 /* Shrinking suspended region */
4542 mddev->pers->quiesce(mddev, 2);
4543 else {
4544 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004545 mddev->pers->quiesce(mddev, 1);
4546 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004547 }
4548 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004549}
4550static struct md_sysfs_entry md_suspend_hi =
4551__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4552
NeilBrown08a02ec2007-05-09 02:35:38 -07004553static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004554reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004555{
4556 if (mddev->reshape_position != MaxSector)
4557 return sprintf(page, "%llu\n",
4558 (unsigned long long)mddev->reshape_position);
4559 strcpy(page, "none\n");
4560 return 5;
4561}
4562
4563static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004564reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004565{
NeilBrownc6563a82012-05-21 09:27:00 +10004566 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004567 char *e;
4568 unsigned long long new = simple_strtoull(buf, &e, 10);
4569 if (mddev->pers)
4570 return -EBUSY;
4571 if (buf == e || (*e && *e != '\n'))
4572 return -EINVAL;
4573 mddev->reshape_position = new;
4574 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004575 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004576 mddev->new_level = mddev->level;
4577 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004578 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004579 rdev_for_each(rdev, mddev)
4580 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004581 return len;
4582}
4583
4584static struct md_sysfs_entry md_reshape_position =
4585__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4586 reshape_position_store);
4587
Dan Williamsb522adc2009-03-31 15:00:31 +11004588static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004589reshape_direction_show(struct mddev *mddev, char *page)
4590{
4591 return sprintf(page, "%s\n",
4592 mddev->reshape_backwards ? "backwards" : "forwards");
4593}
4594
4595static ssize_t
4596reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4597{
4598 int backwards = 0;
4599 if (cmd_match(buf, "forwards"))
4600 backwards = 0;
4601 else if (cmd_match(buf, "backwards"))
4602 backwards = 1;
4603 else
4604 return -EINVAL;
4605 if (mddev->reshape_backwards == backwards)
4606 return len;
4607
4608 /* check if we are allowed to change */
4609 if (mddev->delta_disks)
4610 return -EBUSY;
4611
4612 if (mddev->persistent &&
4613 mddev->major_version == 0)
4614 return -EINVAL;
4615
4616 mddev->reshape_backwards = backwards;
4617 return len;
4618}
4619
4620static struct md_sysfs_entry md_reshape_direction =
4621__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4622 reshape_direction_store);
4623
4624static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004625array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004626{
4627 if (mddev->external_size)
4628 return sprintf(page, "%llu\n",
4629 (unsigned long long)mddev->array_sectors/2);
4630 else
4631 return sprintf(page, "default\n");
4632}
4633
4634static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004635array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004636{
4637 sector_t sectors;
4638
4639 if (strncmp(buf, "default", 7) == 0) {
4640 if (mddev->pers)
4641 sectors = mddev->pers->size(mddev, 0, 0);
4642 else
4643 sectors = mddev->array_sectors;
4644
4645 mddev->external_size = 0;
4646 } else {
4647 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4648 return -EINVAL;
4649 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004650 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004651
4652 mddev->external_size = 1;
4653 }
4654
4655 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004656 if (mddev->pers) {
4657 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004658 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004659 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004660 return len;
4661}
4662
4663static struct md_sysfs_entry md_array_size =
4664__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4665 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004666
NeilBrowneae17012005-11-08 21:39:23 -08004667static struct attribute *md_default_attrs[] = {
4668 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004669 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004670 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004671 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004672 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004673 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004674 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004675 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004676 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004677 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004678 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004679 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004680 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004681 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004682 NULL,
4683};
4684
4685static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004686 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004687 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004688 &md_sync_min.attr,
4689 &md_sync_max.attr,
4690 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004691 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004692 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004693 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004694 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004695 &md_suspend_lo.attr,
4696 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004697 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004698 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004699 NULL,
4700};
NeilBrown411036f2005-11-08 21:39:40 -08004701static struct attribute_group md_redundancy_group = {
4702 .name = NULL,
4703 .attrs = md_redundancy_attrs,
4704};
4705
NeilBrowneae17012005-11-08 21:39:23 -08004706
4707static ssize_t
4708md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4709{
4710 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004711 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004712 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004713
4714 if (!entry->show)
4715 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004716 spin_lock(&all_mddevs_lock);
4717 if (list_empty(&mddev->all_mddevs)) {
4718 spin_unlock(&all_mddevs_lock);
4719 return -EBUSY;
4720 }
4721 mddev_get(mddev);
4722 spin_unlock(&all_mddevs_lock);
4723
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004724 rv = mddev_lock(mddev);
4725 if (!rv) {
4726 rv = entry->show(mddev, page);
4727 mddev_unlock(mddev);
4728 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004729 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004730 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004731}
4732
4733static ssize_t
4734md_attr_store(struct kobject *kobj, struct attribute *attr,
4735 const char *page, size_t length)
4736{
4737 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004738 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004739 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004740
4741 if (!entry->store)
4742 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004743 if (!capable(CAP_SYS_ADMIN))
4744 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004745 spin_lock(&all_mddevs_lock);
4746 if (list_empty(&mddev->all_mddevs)) {
4747 spin_unlock(&all_mddevs_lock);
4748 return -EBUSY;
4749 }
4750 mddev_get(mddev);
4751 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004752 rv = mddev_lock(mddev);
4753 if (!rv) {
4754 rv = entry->store(mddev, page, length);
4755 mddev_unlock(mddev);
4756 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004757 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004758 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004759}
4760
4761static void md_free(struct kobject *ko)
4762{
NeilBrownfd01b882011-10-11 16:47:53 +11004763 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004764
4765 if (mddev->sysfs_state)
4766 sysfs_put(mddev->sysfs_state);
4767
4768 if (mddev->gendisk) {
4769 del_gendisk(mddev->gendisk);
4770 put_disk(mddev->gendisk);
4771 }
4772 if (mddev->queue)
4773 blk_cleanup_queue(mddev->queue);
4774
NeilBrowneae17012005-11-08 21:39:23 -08004775 kfree(mddev);
4776}
4777
Emese Revfy52cf25d2010-01-19 02:58:23 +01004778static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004779 .show = md_attr_show,
4780 .store = md_attr_store,
4781};
4782static struct kobj_type md_ktype = {
4783 .release = md_free,
4784 .sysfs_ops = &md_sysfs_ops,
4785 .default_attrs = md_default_attrs,
4786};
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788int mdp_major = 0;
4789
Dan Williams5fd3a172009-03-04 00:57:25 -07004790static void mddev_delayed_delete(struct work_struct *ws)
4791{
NeilBrownfd01b882011-10-11 16:47:53 +11004792 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004793
NeilBrown43a70502009-12-14 12:49:55 +11004794 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004795 kobject_del(&mddev->kobj);
4796 kobject_put(&mddev->kobj);
4797}
4798
NeilBrownefeb53c2009-01-09 08:31:10 +11004799static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004801 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004802 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004804 int partitioned;
4805 int shift;
4806 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004807 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004810 return -ENODEV;
4811
4812 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4813 shift = partitioned ? MdpMinorShift : 0;
4814 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Tejun Heoe804ac72010-10-15 15:36:08 +02004816 /* wait for any previous instance of this device to be
4817 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004818 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004819 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004820
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004821 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004822 error = -EEXIST;
4823 if (mddev->gendisk)
4824 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004825
4826 if (name) {
4827 /* Need to ensure that 'name' is not a duplicate.
4828 */
NeilBrownfd01b882011-10-11 16:47:53 +11004829 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004830 spin_lock(&all_mddevs_lock);
4831
4832 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4833 if (mddev2->gendisk &&
4834 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4835 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004836 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004837 }
4838 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 }
NeilBrown8b765392009-01-09 08:31:08 +11004840
NeilBrown0909dc42009-07-01 12:27:21 +10004841 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004842 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004843 if (!mddev->queue)
4844 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004845 mddev->queue->queuedata = mddev;
4846
NeilBrown409c57f2009-03-31 14:39:39 +11004847 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004848 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 disk = alloc_disk(1 << shift);
4851 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004852 blk_cleanup_queue(mddev->queue);
4853 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004854 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004856 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004858 if (name)
4859 strcpy(disk->disk_name, name);
4860 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004862 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 disk->fops = &md_fops;
4865 disk->private_data = mddev;
4866 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004867 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004868 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004869 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004870 * remove it now.
4871 */
4872 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004874 /* As soon as we call add_disk(), another thread could get
4875 * through to md_open, so make sure it doesn't get too far
4876 */
4877 mutex_lock(&mddev->open_mutex);
4878 add_disk(disk);
4879
Tejun Heoed9e1982008-08-25 19:56:05 +09004880 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4881 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004882 if (error) {
4883 /* This isn't possible, but as kobject_init_and_add is marked
4884 * __must_check, we must do something with the result
4885 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004886 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4887 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004888 error = 0;
4889 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004890 if (mddev->kobj.sd &&
4891 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004892 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004893 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004894 abort:
4895 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004896 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004897 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004898 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004899 }
NeilBrownd3374822009-01-09 08:31:10 +11004900 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004901 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004902}
4903
4904static struct kobject *md_probe(dev_t dev, int *part, void *data)
4905{
4906 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return NULL;
4908}
4909
NeilBrownefeb53c2009-01-09 08:31:10 +11004910static int add_named_array(const char *val, struct kernel_param *kp)
4911{
4912 /* val must be "md_*" where * is not all digits.
4913 * We allocate an array with a large free minor number, and
4914 * set the name to val. val must not already be an active name.
4915 */
4916 int len = strlen(val);
4917 char buf[DISK_NAME_LEN];
4918
4919 while (len && val[len-1] == '\n')
4920 len--;
4921 if (len >= DISK_NAME_LEN)
4922 return -E2BIG;
4923 strlcpy(buf, val, len+1);
4924 if (strncmp(buf, "md_", 3) != 0)
4925 return -EINVAL;
4926 return md_alloc(0, buf);
4927}
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929static void md_safemode_timeout(unsigned long data)
4930{
NeilBrownfd01b882011-10-11 16:47:53 +11004931 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Neil Brown0fd62b82008-06-28 08:31:36 +10004933 if (!atomic_read(&mddev->writes_pending)) {
4934 mddev->safemode = 1;
4935 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004936 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 md_wakeup_thread(mddev->thread);
4939}
4940
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004941static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
NeilBrownfd01b882011-10-11 16:47:53 +11004943int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
NeilBrown2604b702006-01-06 00:20:36 -08004945 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004946 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004947 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
NeilBrowna757e642005-04-16 15:26:42 -07004949 if (list_empty(&mddev->disks))
4950 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 if (mddev->pers)
4954 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004955 /* Cannot run until previous stop completes properly */
4956 if (mddev->sysfs_active)
4957 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 /*
4960 * Analyze all RAID superblock(s)
4961 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004962 if (!mddev->raid_disks) {
4963 if (!mddev->persistent)
4964 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004965 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
NeilBrownd9d166c2006-01-06 00:20:51 -08004968 if (mddev->level != LEVEL_NONE)
4969 request_module("md-level-%d", mddev->level);
4970 else if (mddev->clevel[0])
4971 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 /*
4974 * Drop all container device buffers, from now on
4975 * the only valid external interface is through the md
4976 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004978 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004979 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 continue;
4981 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004982 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004983
4984 /* perform some consistency tests on the device.
4985 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004986 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004987 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004988 if (rdev->meta_bdev) {
4989 /* Nothing to check */;
4990 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004991 if (mddev->dev_sectors &&
4992 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004993 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004994 printk("md: %s: data overlaps metadata\n",
4995 mdname(mddev));
4996 return -EINVAL;
4997 }
4998 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004999 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005000 > rdev->data_offset) {
5001 printk("md: %s: metadata overlaps data\n",
5002 mdname(mddev));
5003 return -EINVAL;
5004 }
5005 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005006 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 }
5008
NeilBrowna167f662010-10-26 18:31:13 +11005009 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10005010 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11005011 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11005012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005014 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005015 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005017 if (mddev->level != LEVEL_NONE)
5018 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5019 mddev->level);
5020 else
5021 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5022 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return -EINVAL;
5024 }
NeilBrown2604b702006-01-06 00:20:36 -08005025 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005027 if (mddev->level != pers->level) {
5028 mddev->level = pers->level;
5029 mddev->new_level = pers->level;
5030 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005031 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
NeilBrownf6705572006-03-27 01:18:11 -08005033 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005034 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005035 /* This personality cannot handle reshaping... */
5036 mddev->pers = NULL;
5037 module_put(pers->owner);
5038 return -EINVAL;
5039 }
5040
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005041 if (pers->sync_request) {
5042 /* Warn if this is a potentially silly
5043 * configuration.
5044 */
5045 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005046 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005047 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005048
NeilBrowndafb20f2012-03-19 12:46:39 +11005049 rdev_for_each(rdev, mddev)
5050 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005051 if (rdev < rdev2 &&
5052 rdev->bdev->bd_contains ==
5053 rdev2->bdev->bd_contains) {
5054 printk(KERN_WARNING
5055 "%s: WARNING: %s appears to be"
5056 " on the same physical disk as"
5057 " %s.\n",
5058 mdname(mddev),
5059 bdevname(rdev->bdev,b),
5060 bdevname(rdev2->bdev,b2));
5061 warned = 1;
5062 }
5063 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005064
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005065 if (warned)
5066 printk(KERN_WARNING
5067 "True protection against single-disk"
5068 " failure might be compromised.\n");
5069 }
5070
NeilBrown657390d2005-08-26 18:34:16 -07005071 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005072 /* may be over-ridden by personality */
5073 mddev->resync_max_sectors = mddev->dev_sectors;
5074
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005075 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
NeilBrown0f9552b52009-12-30 12:08:50 +11005077 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005078 mddev->ro = 2; /* read-only, but switch on first write */
5079
NeilBrownb15c2e52006-01-06 00:20:16 -08005080 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005081 if (err)
5082 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005083 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5084 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5085 " but 'external_size' not in effect?\n", __func__);
5086 printk(KERN_ERR
5087 "md: invalid array_size %llu > default size %llu\n",
5088 (unsigned long long)mddev->array_sectors / 2,
5089 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5090 err = -EINVAL;
5091 mddev->pers->stop(mddev);
5092 }
NeilBrownef99bf42012-05-22 13:55:08 +10005093 if (err == 0 && mddev->pers->sync_request &&
5094 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005095 err = bitmap_create(mddev);
5096 if (err) {
5097 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5098 mdname(mddev), err);
5099 mddev->pers->stop(mddev);
5100 }
5101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 module_put(mddev->pers->owner);
5104 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005105 bitmap_destroy(mddev);
5106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005108 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005109 if (mddev->kobj.sd &&
5110 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005111 printk(KERN_WARNING
5112 "md: cannot register extra attributes for %s\n",
5113 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005114 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005115 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005116 mddev->ro = 0;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005119 atomic_set(&mddev->max_corr_read_errors,
5120 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mddev->safemode = 0;
5122 mddev->safemode_timer.function = md_safemode_timeout;
5123 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005124 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005126 smp_wmb();
5127 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005128 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005129 if (rdev->raid_disk >= 0)
5130 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005131 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5134
NeilBrown850b2b422006-10-03 01:15:46 -07005135 if (mddev->flags)
5136 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
NeilBrownd7603b72006-01-06 00:20:30 -08005138 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005139 sysfs_notify_dirent_safe(mddev->sysfs_state);
5140 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005141 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return 0;
5143}
NeilBrown390ee602010-06-01 19:37:27 +10005144EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
NeilBrownfd01b882011-10-11 16:47:53 +11005146static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005147{
5148 int err;
5149
5150 err = md_run(mddev);
5151 if (err)
5152 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005153 err = bitmap_load(mddev);
5154 if (err) {
5155 bitmap_destroy(mddev);
5156 goto out;
5157 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005158
5159 md_wakeup_thread(mddev->thread);
5160 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5161
NeilBrownfe60b012010-03-29 11:10:42 +11005162 set_capacity(mddev->gendisk, mddev->array_sectors);
5163 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005164 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005165 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5166out:
5167 return err;
5168}
5169
NeilBrownfd01b882011-10-11 16:47:53 +11005170static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171{
5172 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
Andre Noll80fab1d2008-07-11 22:02:21 +10005174 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005176 return -ENXIO;
5177 if (!mddev->pers)
5178 return -EINVAL;
5179 if (!mddev->ro)
5180 return -EBUSY;
5181 mddev->safemode = 0;
5182 mddev->ro = 0;
5183 set_disk_ro(disk, 0);
5184 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5185 mdname(mddev));
5186 /* Kick recovery or resync if necessary */
5187 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5188 md_wakeup_thread(mddev->thread);
5189 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005190 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
NeilBrownacc55e22006-06-26 00:27:47 -07005194/* similar to deny_write_access, but accounts for our holding a reference
5195 * to the file ourselves */
5196static int deny_bitmap_write_access(struct file * file)
5197{
5198 struct inode *inode = file->f_mapping->host;
5199
5200 spin_lock(&inode->i_lock);
5201 if (atomic_read(&inode->i_writecount) > 1) {
5202 spin_unlock(&inode->i_lock);
5203 return -ETXTBSY;
5204 }
5205 atomic_set(&inode->i_writecount, -1);
5206 spin_unlock(&inode->i_lock);
5207
5208 return 0;
5209}
5210
NeilBrown43a70502009-12-14 12:49:55 +11005211void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005212{
5213 struct inode *inode = file->f_mapping->host;
5214
5215 spin_lock(&inode->i_lock);
5216 atomic_set(&inode->i_writecount, 1);
5217 spin_unlock(&inode->i_lock);
5218}
5219
NeilBrownfd01b882011-10-11 16:47:53 +11005220static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005221{
5222 mddev->array_sectors = 0;
5223 mddev->external_size = 0;
5224 mddev->dev_sectors = 0;
5225 mddev->raid_disks = 0;
5226 mddev->recovery_cp = 0;
5227 mddev->resync_min = 0;
5228 mddev->resync_max = MaxSector;
5229 mddev->reshape_position = MaxSector;
5230 mddev->external = 0;
5231 mddev->persistent = 0;
5232 mddev->level = LEVEL_NONE;
5233 mddev->clevel[0] = 0;
5234 mddev->flags = 0;
5235 mddev->ro = 0;
5236 mddev->metadata_type[0] = 0;
5237 mddev->chunk_sectors = 0;
5238 mddev->ctime = mddev->utime = 0;
5239 mddev->layout = 0;
5240 mddev->max_disks = 0;
5241 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005242 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005243 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005244 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005245 mddev->new_level = LEVEL_NONE;
5246 mddev->new_layout = 0;
5247 mddev->new_chunk_sectors = 0;
5248 mddev->curr_resync = 0;
5249 mddev->resync_mismatches = 0;
5250 mddev->suspend_lo = mddev->suspend_hi = 0;
5251 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5252 mddev->recovery = 0;
5253 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005254 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005255 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005256 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005257 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005258 mddev->bitmap_info.offset = 0;
5259 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005260 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005261 mddev->bitmap_info.chunksize = 0;
5262 mddev->bitmap_info.daemon_sleep = 0;
5263 mddev->bitmap_info.max_write_behind = 0;
5264}
5265
NeilBrownfd01b882011-10-11 16:47:53 +11005266static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005267{
5268 if (mddev->sync_thread) {
5269 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5270 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005271 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005272 }
5273
5274 del_timer_sync(&mddev->safemode_timer);
5275
5276 bitmap_flush(mddev);
5277 md_super_wait(mddev);
5278
5279 if (!mddev->in_sync || mddev->flags) {
5280 /* mark array as shutdown cleanly */
5281 mddev->in_sync = 1;
5282 md_update_sb(mddev, 1);
5283 }
5284}
NeilBrowndefad612011-01-14 09:14:33 +11005285
NeilBrownfd01b882011-10-11 16:47:53 +11005286void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005287{
5288 mddev_lock(mddev);
5289 __md_stop_writes(mddev);
5290 mddev_unlock(mddev);
5291}
NeilBrown390ee602010-06-01 19:37:27 +10005292EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005293
NeilBrownfd01b882011-10-11 16:47:53 +11005294void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005295{
NeilBrown0ca69882011-01-14 09:14:33 +11005296 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005297 mddev->pers->stop(mddev);
5298 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5299 mddev->to_remove = &md_redundancy_group;
5300 module_put(mddev->pers->owner);
5301 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005302 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005303}
NeilBrown390ee602010-06-01 19:37:27 +10005304EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005305
NeilBrowna05b7ea2012-07-19 15:59:18 +10005306static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005307{
5308 int err = 0;
5309 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005310 if (atomic_read(&mddev->openers) > !!bdev) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005311 printk("md: %s still in use.\n",mdname(mddev));
5312 err = -EBUSY;
5313 goto out;
5314 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005315 if (bdev)
5316 sync_blockdev(bdev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005317 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005318 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005319
5320 err = -ENXIO;
5321 if (mddev->ro==1)
5322 goto out;
5323 mddev->ro = 1;
5324 set_disk_ro(mddev->gendisk, 1);
5325 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005326 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005327 err = 0;
5328 }
5329out:
5330 mutex_unlock(&mddev->open_mutex);
5331 return err;
5332}
5333
NeilBrown9e653b62006-06-26 00:27:58 -07005334/* mode:
5335 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005336 * 2 - stop but do not disassemble array
5337 */
NeilBrowna05b7ea2012-07-19 15:59:18 +10005338static int do_md_stop(struct mddev * mddev, int mode,
5339 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005342 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
NeilBrownc8c00a62009-08-10 12:50:52 +10005344 mutex_lock(&mddev->open_mutex);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005345 if (atomic_read(&mddev->openers) > !!bdev ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10005346 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005347 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005348 mutex_unlock(&mddev->open_mutex);
5349 return -EBUSY;
5350 }
NeilBrowna05b7ea2012-07-19 15:59:18 +10005351 if (bdev)
5352 /* It is possible IO was issued on some other
5353 * open file which was closed before we took ->open_mutex.
5354 * As that was not the last close __blkdev_put will not
5355 * have called sync_blockdev, so we must.
5356 */
5357 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
NeilBrown6e17b022010-08-07 21:41:19 +10005359 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005360 if (mddev->ro)
5361 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005362
NeilBrowndefad612011-01-14 09:14:33 +11005363 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005364 md_stop(mddev);
5365 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005366 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005367
NeilBrowna4bd82d2010-03-29 13:23:10 +11005368 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005369 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005370
NeilBrowndafb20f2012-03-19 12:46:39 +11005371 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005372 if (rdev->raid_disk >= 0)
5373 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005374
NeilBrowna4bd82d2010-03-29 13:23:10 +11005375 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005376 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005377 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005378 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005379
NeilBrowna4bd82d2010-03-29 13:23:10 +11005380 if (mddev->ro)
5381 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005382 } else
5383 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 /*
5385 * Free resources if final stop
5386 */
NeilBrown9e653b62006-06-26 00:27:58 -07005387 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5389
NeilBrown978f9462006-02-02 14:28:05 -08005390 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005391 if (mddev->bitmap_info.file) {
5392 restore_bitmap_write_access(mddev->bitmap_info.file);
5393 fput(mddev->bitmap_info.file);
5394 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005395 }
NeilBrownc3d97142009-12-14 12:49:52 +11005396 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 export_array(mddev);
5399
NeilBrown6177b472010-03-29 11:37:13 +11005400 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005401 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005402 if (mddev->hold_active == UNTIL_STOP)
5403 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005404 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005405 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005406 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005407 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005411#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005412static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
NeilBrown3cb03002011-10-11 16:45:26 +11005414 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 int err;
5416
NeilBrowna757e642005-04-16 15:26:42 -07005417 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
5420 printk(KERN_INFO "md: running: ");
5421
NeilBrowndafb20f2012-03-19 12:46:39 +11005422 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 char b[BDEVNAME_SIZE];
5424 printk("<%s>", bdevname(rdev->bdev,b));
5425 }
5426 printk("\n");
5427
NeilBrownd710e132008-10-13 11:55:12 +11005428 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 if (err) {
5430 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10005431 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433}
5434
5435/*
5436 * lets try to run arrays based on all disks that have arrived
5437 * until now. (those are in pending_raid_disks)
5438 *
5439 * the method: pick the first pending disk, collect all disks with
5440 * the same UUID, remove all from the pending list and put them into
5441 * the 'same_array' list. Then order this list based on superblock
5442 * update time (freshest comes first), kick out 'old' disks and
5443 * compare superblocks. If everything's fine then run it.
5444 *
5445 * If "unit" is allocated, then bump its reference count
5446 */
5447static void autorun_devices(int part)
5448{
NeilBrown3cb03002011-10-11 16:45:26 +11005449 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005450 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 char b[BDEVNAME_SIZE];
5452
5453 printk(KERN_INFO "md: autorun ...\n");
5454 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005455 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005457 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005459 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
5461 printk(KERN_INFO "md: considering %s ...\n",
5462 bdevname(rdev0->bdev,b));
5463 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005464 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 if (super_90_load(rdev, rdev0, 0) >= 0) {
5466 printk(KERN_INFO "md: adding %s ...\n",
5467 bdevname(rdev->bdev,b));
5468 list_move(&rdev->same_set, &candidates);
5469 }
5470 /*
5471 * now we have a set of devices, with all of them having
5472 * mostly sane superblocks. It's time to allocate the
5473 * mddev.
5474 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005475 if (part) {
5476 dev = MKDEV(mdp_major,
5477 rdev0->preferred_minor << MdpMinorShift);
5478 unit = MINOR(dev) >> MdpMinorShift;
5479 } else {
5480 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5481 unit = MINOR(dev);
5482 }
5483 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5485 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5486 break;
5487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 md_probe(dev, NULL, NULL);
5490 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005491 if (!mddev || !mddev->gendisk) {
5492 if (mddev)
5493 mddev_put(mddev);
5494 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 "md: cannot allocate memory for md drive.\n");
5496 break;
5497 }
5498 if (mddev_lock(mddev))
5499 printk(KERN_WARNING "md: %s locked, cannot run\n",
5500 mdname(mddev));
5501 else if (mddev->raid_disks || mddev->major_version
5502 || !list_empty(&mddev->disks)) {
5503 printk(KERN_WARNING
5504 "md: %s already running, cannot run %s\n",
5505 mdname(mddev), bdevname(rdev0->bdev,b));
5506 mddev_unlock(mddev);
5507 } else {
5508 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005509 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005510 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 list_del_init(&rdev->same_set);
5512 if (bind_rdev_to_array(rdev, mddev))
5513 export_rdev(rdev);
5514 }
5515 autorun_array(mddev);
5516 mddev_unlock(mddev);
5517 }
5518 /* on success, candidates will be empty, on error
5519 * it won't...
5520 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005521 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005522 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 mddev_put(mddev);
5526 }
5527 printk(KERN_INFO "md: ... autorun DONE.\n");
5528}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005529#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531static int get_version(void __user * arg)
5532{
5533 mdu_version_t ver;
5534
5535 ver.major = MD_MAJOR_VERSION;
5536 ver.minor = MD_MINOR_VERSION;
5537 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5538
5539 if (copy_to_user(arg, &ver, sizeof(ver)))
5540 return -EFAULT;
5541
5542 return 0;
5543}
5544
NeilBrownfd01b882011-10-11 16:47:53 +11005545static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
5547 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005548 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005549 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
NeilBrowna9f326e2009-09-23 18:06:41 +10005551 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005552 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005554 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 failed++;
5556 else {
5557 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005558 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005559 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 else
5561 spare++;
5562 }
5563 }
5564
5565 info.major_version = mddev->major_version;
5566 info.minor_version = mddev->minor_version;
5567 info.patch_version = MD_PATCHLEVEL_VERSION;
5568 info.ctime = mddev->ctime;
5569 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005570 info.size = mddev->dev_sectors / 2;
5571 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005572 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 info.nr_disks = nr;
5574 info.raid_disks = mddev->raid_disks;
5575 info.md_minor = mddev->md_minor;
5576 info.not_persistent= !mddev->persistent;
5577
5578 info.utime = mddev->utime;
5579 info.state = 0;
5580 if (mddev->in_sync)
5581 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005582 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005583 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005584 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 info.working_disks = working;
5586 info.failed_disks = failed;
5587 info.spare_disks = spare;
5588
5589 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005590 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 if (copy_to_user(arg, &info, sizeof(info)))
5593 return -EFAULT;
5594
5595 return 0;
5596}
5597
NeilBrownfd01b882011-10-11 16:47:53 +11005598static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005599{
5600 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5601 char *ptr, *buf = NULL;
5602 int err = -ENOMEM;
5603
Dan Williamsb5470dc2008-06-27 21:44:04 -07005604 if (md_allow_write(mddev))
5605 file = kmalloc(sizeof(*file), GFP_NOIO);
5606 else
5607 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005608
NeilBrown32a76272005-06-21 17:17:14 -07005609 if (!file)
5610 goto out;
5611
5612 /* bitmap disabled, zero the first byte and copy out */
NeilBrown1ec885c2012-05-22 13:55:10 +10005613 if (!mddev->bitmap || !mddev->bitmap->storage.file) {
NeilBrown32a76272005-06-21 17:17:14 -07005614 file->pathname[0] = '\0';
5615 goto copy_out;
5616 }
5617
5618 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5619 if (!buf)
5620 goto out;
5621
NeilBrown1ec885c2012-05-22 13:55:10 +10005622 ptr = d_path(&mddev->bitmap->storage.file->f_path,
5623 buf, sizeof(file->pathname));
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005624 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005625 goto out;
5626
5627 strcpy(file->pathname, ptr);
5628
5629copy_out:
5630 err = 0;
5631 if (copy_to_user(arg, file, sizeof(*file)))
5632 err = -EFAULT;
5633out:
5634 kfree(buf);
5635 kfree(file);
5636 return err;
5637}
5638
NeilBrownfd01b882011-10-11 16:47:53 +11005639static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
5641 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005642 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 if (copy_from_user(&info, arg, sizeof(info)))
5645 return -EFAULT;
5646
Andre Noll26ef3792008-07-11 22:02:21 +10005647 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (rdev) {
5649 info.major = MAJOR(rdev->bdev->bd_dev);
5650 info.minor = MINOR(rdev->bdev->bd_dev);
5651 info.raid_disk = rdev->raid_disk;
5652 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005653 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005655 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 info.state |= (1<<MD_DISK_ACTIVE);
5657 info.state |= (1<<MD_DISK_SYNC);
5658 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005659 if (test_bit(WriteMostly, &rdev->flags))
5660 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 } else {
5662 info.major = info.minor = 0;
5663 info.raid_disk = -1;
5664 info.state = (1<<MD_DISK_REMOVED);
5665 }
5666
5667 if (copy_to_user(arg, &info, sizeof(info)))
5668 return -EFAULT;
5669
5670 return 0;
5671}
5672
NeilBrownfd01b882011-10-11 16:47:53 +11005673static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674{
5675 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005676 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 dev_t dev = MKDEV(info->major,info->minor);
5678
5679 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5680 return -EOVERFLOW;
5681
5682 if (!mddev->raid_disks) {
5683 int err;
5684 /* expecting a device which has a superblock */
5685 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5686 if (IS_ERR(rdev)) {
5687 printk(KERN_WARNING
5688 "md: md_import_device returned %ld\n",
5689 PTR_ERR(rdev));
5690 return PTR_ERR(rdev);
5691 }
5692 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005693 struct md_rdev *rdev0
5694 = list_entry(mddev->disks.next,
5695 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005696 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 .load_super(rdev, rdev0, mddev->minor_version);
5698 if (err < 0) {
5699 printk(KERN_WARNING
5700 "md: %s has different UUID to %s\n",
5701 bdevname(rdev->bdev,b),
5702 bdevname(rdev0->bdev,b2));
5703 export_rdev(rdev);
5704 return -EINVAL;
5705 }
5706 }
5707 err = bind_rdev_to_array(rdev, mddev);
5708 if (err)
5709 export_rdev(rdev);
5710 return err;
5711 }
5712
5713 /*
5714 * add_new_disk can be used once the array is assembled
5715 * to add "hot spares". They must already have a superblock
5716 * written
5717 */
5718 if (mddev->pers) {
5719 int err;
5720 if (!mddev->pers->hot_add_disk) {
5721 printk(KERN_WARNING
5722 "%s: personality does not support diskops!\n",
5723 mdname(mddev));
5724 return -EINVAL;
5725 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005726 if (mddev->persistent)
5727 rdev = md_import_device(dev, mddev->major_version,
5728 mddev->minor_version);
5729 else
5730 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (IS_ERR(rdev)) {
5732 printk(KERN_WARNING
5733 "md: md_import_device returned %ld\n",
5734 PTR_ERR(rdev));
5735 return PTR_ERR(rdev);
5736 }
NeilBrown1a855a02010-12-09 16:36:28 +11005737 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005738 if (!mddev->persistent) {
5739 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005740 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005741 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005742 set_bit(In_sync, &rdev->flags);
5743 } else
NeilBrown41158c72005-06-21 17:17:25 -07005744 rdev->raid_disk = -1;
5745 } else
5746 super_types[mddev->major_version].
5747 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005748 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10005749 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10005750 /* This was a hot-add request, but events doesn't
5751 * match, so reject it.
5752 */
5753 export_rdev(rdev);
5754 return -EINVAL;
5755 }
5756
NeilBrown1a855a02010-12-09 16:36:28 +11005757 if (test_bit(In_sync, &rdev->flags))
5758 rdev->saved_raid_disk = rdev->raid_disk;
5759 else
5760 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005761
NeilBrownb2d444d2005-11-08 21:39:31 -08005762 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005763 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5764 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005765 else
5766 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 rdev->raid_disk = -1;
5769 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005770 if (!err && !mddev->pers->hot_remove_disk) {
5771 /* If there is hot_add_disk but no hot_remove_disk
5772 * then added disks for geometry changes,
5773 * and should be added immediately.
5774 */
5775 super_types[mddev->major_version].
5776 validate_super(mddev, rdev);
5777 err = mddev->pers->hot_add_disk(mddev, rdev);
5778 if (err)
5779 unbind_rdev_from_array(rdev);
5780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 if (err)
5782 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005783 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005784 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005785
NeilBrown17571282006-12-10 02:20:52 -08005786 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005787 if (mddev->degraded)
5788 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005789 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005790 if (!err)
5791 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005792 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 return err;
5794 }
5795
5796 /* otherwise, add_new_disk is only allowed
5797 * for major_version==0 superblocks
5798 */
5799 if (mddev->major_version != 0) {
5800 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5801 mdname(mddev));
5802 return -EINVAL;
5803 }
5804
5805 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5806 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005807 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 if (IS_ERR(rdev)) {
5809 printk(KERN_WARNING
5810 "md: error, md_import_device() returned %ld\n",
5811 PTR_ERR(rdev));
5812 return PTR_ERR(rdev);
5813 }
5814 rdev->desc_nr = info->number;
5815 if (info->raid_disk < mddev->raid_disks)
5816 rdev->raid_disk = info->raid_disk;
5817 else
5818 rdev->raid_disk = -1;
5819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005821 if (info->state & (1<<MD_DISK_SYNC))
5822 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005824 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5825 set_bit(WriteMostly, &rdev->flags);
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (!mddev->persistent) {
5828 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005829 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5830 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005831 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005832 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
NeilBrown2bf071b2006-01-06 00:20:55 -08005834 err = bind_rdev_to_array(rdev, mddev);
5835 if (err) {
5836 export_rdev(rdev);
5837 return err;
5838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 }
5840
5841 return 0;
5842}
5843
NeilBrownfd01b882011-10-11 16:47:53 +11005844static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845{
5846 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005847 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 rdev = find_rdev(mddev, dev);
5850 if (!rdev)
5851 return -ENXIO;
5852
5853 if (rdev->raid_disk >= 0)
5854 goto busy;
5855
5856 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005857 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005858 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 return 0;
5861busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005862 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 bdevname(rdev->bdev,b), mdname(mddev));
5864 return -EBUSY;
5865}
5866
NeilBrownfd01b882011-10-11 16:47:53 +11005867static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868{
5869 char b[BDEVNAME_SIZE];
5870 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005871 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 if (!mddev->pers)
5874 return -ENODEV;
5875
5876 if (mddev->major_version != 0) {
5877 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5878 " version-0 superblocks.\n",
5879 mdname(mddev));
5880 return -EINVAL;
5881 }
5882 if (!mddev->pers->hot_add_disk) {
5883 printk(KERN_WARNING
5884 "%s: personality does not support diskops!\n",
5885 mdname(mddev));
5886 return -EINVAL;
5887 }
5888
NeilBrownd710e132008-10-13 11:55:12 +11005889 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 if (IS_ERR(rdev)) {
5891 printk(KERN_WARNING
5892 "md: error, md_import_device() returned %ld\n",
5893 PTR_ERR(rdev));
5894 return -EINVAL;
5895 }
5896
5897 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005898 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005900 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrown8190e752009-06-18 08:48:58 +10005902 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
NeilBrownb2d444d2005-11-08 21:39:31 -08005904 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 printk(KERN_WARNING
5906 "md: can not hot-add faulty %s disk to %s!\n",
5907 bdevname(rdev->bdev,b), mdname(mddev));
5908 err = -EINVAL;
5909 goto abort_export;
5910 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005911 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005913 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005914 err = bind_rdev_to_array(rdev, mddev);
5915 if (err)
5916 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 /*
5919 * The rest should better be atomic, we can have disk failures
5920 * noticed in interrupt contexts ...
5921 */
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 rdev->raid_disk = -1;
5924
NeilBrown850b2b422006-10-03 01:15:46 -07005925 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /*
5928 * Kick recovery, maybe this spare has to be added to the
5929 * array immediately.
5930 */
5931 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5932 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005933 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return 0;
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936abort_export:
5937 export_rdev(rdev);
5938 return err;
5939}
5940
NeilBrownfd01b882011-10-11 16:47:53 +11005941static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005942{
5943 int err;
5944
NeilBrown36fa3062005-09-09 16:23:45 -07005945 if (mddev->pers) {
5946 if (!mddev->pers->quiesce)
5947 return -EBUSY;
5948 if (mddev->recovery || mddev->sync_thread)
5949 return -EBUSY;
5950 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005951 }
5952
NeilBrown36fa3062005-09-09 16:23:45 -07005953
5954 if (fd >= 0) {
5955 if (mddev->bitmap)
5956 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005957 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005958
NeilBrownc3d97142009-12-14 12:49:52 +11005959 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005960 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5961 mdname(mddev));
5962 return -EBADF;
5963 }
5964
NeilBrownc3d97142009-12-14 12:49:52 +11005965 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005966 if (err) {
5967 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5968 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005969 fput(mddev->bitmap_info.file);
5970 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005971 return err;
5972 }
NeilBrownc3d97142009-12-14 12:49:52 +11005973 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005974 } else if (mddev->bitmap == NULL)
5975 return -ENOENT; /* cannot remove what isn't there */
5976 err = 0;
5977 if (mddev->pers) {
5978 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005979 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005980 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005981 if (!err)
5982 err = bitmap_load(mddev);
5983 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005984 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005985 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005986 fd = -1; /* make sure to put the file */
5987 }
NeilBrown36fa3062005-09-09 16:23:45 -07005988 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005989 }
5990 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005991 if (mddev->bitmap_info.file) {
5992 restore_bitmap_write_access(mddev->bitmap_info.file);
5993 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005994 }
NeilBrownc3d97142009-12-14 12:49:52 +11005995 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005996 }
5997
NeilBrown32a76272005-06-21 17:17:14 -07005998 return err;
5999}
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001/*
6002 * set_array_info is used two different ways
6003 * The original usage is when creating a new array.
6004 * In this usage, raid_disks is > 0 and it together with
6005 * level, size, not_persistent,layout,chunksize determine the
6006 * shape of the array.
6007 * This will always create an array with a type-0.90.0 superblock.
6008 * The newer usage is when assembling an array.
6009 * In this case raid_disks will be 0, and the major_version field is
6010 * use to determine which style super-blocks are to be found on the devices.
6011 * The minor and patch _version numbers are also kept incase the
6012 * super_block handler wishes to interpret them.
6013 */
NeilBrownfd01b882011-10-11 16:47:53 +11006014static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
6016
6017 if (info->raid_disks == 0) {
6018 /* just setting version number for superblock loading */
6019 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006020 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 super_types[info->major_version].name == NULL) {
6022 /* maybe try to auto-load a module? */
6023 printk(KERN_INFO
6024 "md: superblock version %d not known\n",
6025 info->major_version);
6026 return -EINVAL;
6027 }
6028 mddev->major_version = info->major_version;
6029 mddev->minor_version = info->minor_version;
6030 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006031 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006032 /* ensure mddev_put doesn't delete this now that there
6033 * is some minimal configuration.
6034 */
6035 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 return 0;
6037 }
6038 mddev->major_version = MD_MAJOR_VERSION;
6039 mddev->minor_version = MD_MINOR_VERSION;
6040 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6041 mddev->ctime = get_seconds();
6042
6043 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006044 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006045 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 mddev->raid_disks = info->raid_disks;
6047 /* don't set md_minor, it is determined by which /dev/md* was
6048 * openned
6049 */
6050 if (info->state & (1<<MD_SB_CLEAN))
6051 mddev->recovery_cp = MaxSector;
6052 else
6053 mddev->recovery_cp = 0;
6054 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006055 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
6057 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006058 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
6060 mddev->max_disks = MD_SB_DISKS;
6061
NeilBrowne6910632008-02-06 01:39:51 -08006062 if (mddev->persistent)
6063 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006064 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
NeilBrownc3d97142009-12-14 12:49:52 +11006066 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006067 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006068 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006069
NeilBrownf6705572006-03-27 01:18:11 -08006070 mddev->reshape_position = MaxSector;
6071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 /*
6073 * Generate a 128 bit UUID
6074 */
6075 get_random_bytes(mddev->uuid, 16);
6076
NeilBrownf6705572006-03-27 01:18:11 -08006077 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006078 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006079 mddev->new_layout = mddev->layout;
6080 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006081 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006082
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 return 0;
6084}
6085
NeilBrownfd01b882011-10-11 16:47:53 +11006086void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006087{
Dan Williamsb522adc2009-03-31 15:00:31 +11006088 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6089
6090 if (mddev->external_size)
6091 return;
6092
Dan Williams1f403622009-03-31 14:59:03 +11006093 mddev->array_sectors = array_sectors;
6094}
6095EXPORT_SYMBOL(md_set_array_sectors);
6096
NeilBrownfd01b882011-10-11 16:47:53 +11006097static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006098{
NeilBrown3cb03002011-10-11 16:45:26 +11006099 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006100 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006101 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006102
6103 if (mddev->pers->resize == NULL)
6104 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006105 /* The "num_sectors" is the number of sectors of each device that
6106 * is used. This can only make sense for arrays with redundancy.
6107 * linear and raid0 always use whatever space is available. We can only
6108 * consider changing this number if no resync or reconstruction is
6109 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006110 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006111 * of each device. If num_sectors is zero, we find the largest size
6112 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006113 */
6114 if (mddev->sync_thread)
6115 return -EBUSY;
NeilBrowna4a61252012-05-22 13:55:27 +10006116
NeilBrowndafb20f2012-03-19 12:46:39 +11006117 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006118 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006119
Andre Nolld71f9f82008-07-11 22:02:22 +10006120 if (fit && (num_sectors == 0 || num_sectors > avail))
6121 num_sectors = avail;
6122 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006123 return -ENOSPC;
6124 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006125 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006126 if (!rv)
6127 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006128 return rv;
6129}
6130
NeilBrownfd01b882011-10-11 16:47:53 +11006131static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006132{
6133 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006134 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006135 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006136 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006137 return -EINVAL;
6138 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006139 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006140 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006141 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006142 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006143
6144 rdev_for_each(rdev, mddev) {
6145 if (mddev->raid_disks < raid_disks &&
6146 rdev->data_offset < rdev->new_data_offset)
6147 return -EINVAL;
6148 if (mddev->raid_disks > raid_disks &&
6149 rdev->data_offset > rdev->new_data_offset)
6150 return -EINVAL;
6151 }
6152
NeilBrown63c70c42006-03-27 01:18:13 -08006153 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006154 if (mddev->delta_disks < 0)
6155 mddev->reshape_backwards = 1;
6156 else if (mddev->delta_disks > 0)
6157 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006158
6159 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006160 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006161 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006162 mddev->reshape_backwards = 0;
6163 }
NeilBrownda943b992006-01-06 00:20:54 -08006164 return rv;
6165}
6166
6167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168/*
6169 * update_array_info is used to change the configuration of an
6170 * on-line array.
6171 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6172 * fields in the info are checked against the array.
6173 * Any differences that cannot be handled will cause an error.
6174 * Normally, only one change can be managed at a time.
6175 */
NeilBrownfd01b882011-10-11 16:47:53 +11006176static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
6178 int rv = 0;
6179 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006180 int state = 0;
6181
6182 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006183 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006184 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186 if (mddev->major_version != info->major_version ||
6187 mddev->minor_version != info->minor_version ||
6188/* mddev->patch_version != info->patch_version || */
6189 mddev->ctime != info->ctime ||
6190 mddev->level != info->level ||
6191/* mddev->layout != info->layout || */
6192 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006193 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006194 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6195 ((state^info->state) & 0xfffffe00)
6196 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return -EINVAL;
6198 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006199 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6200 cnt++;
6201 if (mddev->raid_disks != info->raid_disks)
6202 cnt++;
6203 if (mddev->layout != info->layout)
6204 cnt++;
6205 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6206 cnt++;
6207 if (cnt == 0)
6208 return 0;
6209 if (cnt > 1)
6210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 if (mddev->layout != info->layout) {
6213 /* Change layout
6214 * we don't need to do anything at the md level, the
6215 * personality will take care of it all.
6216 */
NeilBrown50ac1682009-06-18 08:47:55 +10006217 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006219 else {
6220 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006221 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006222 if (rv)
6223 mddev->new_layout = mddev->layout;
6224 return rv;
6225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006227 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006228 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
NeilBrownda943b992006-01-06 00:20:54 -08006230 if (mddev->raid_disks != info->raid_disks)
6231 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
NeilBrown36fa3062005-09-09 16:23:45 -07006233 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6234 if (mddev->pers->quiesce == NULL)
6235 return -EINVAL;
6236 if (mddev->recovery || mddev->sync_thread)
6237 return -EBUSY;
6238 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6239 /* add the bitmap */
6240 if (mddev->bitmap)
6241 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006242 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006243 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006244 mddev->bitmap_info.offset =
6245 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10006246 mddev->bitmap_info.space =
6247 mddev->bitmap_info.default_space;
NeilBrown36fa3062005-09-09 16:23:45 -07006248 mddev->pers->quiesce(mddev, 1);
6249 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006250 if (!rv)
6251 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006252 if (rv)
6253 bitmap_destroy(mddev);
6254 mddev->pers->quiesce(mddev, 0);
6255 } else {
6256 /* remove the bitmap */
6257 if (!mddev->bitmap)
6258 return -ENOENT;
NeilBrown1ec885c2012-05-22 13:55:10 +10006259 if (mddev->bitmap->storage.file)
NeilBrown36fa3062005-09-09 16:23:45 -07006260 return -EINVAL;
6261 mddev->pers->quiesce(mddev, 1);
6262 bitmap_destroy(mddev);
6263 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006264 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006265 }
6266 }
NeilBrown850b2b422006-10-03 01:15:46 -07006267 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 return rv;
6269}
6270
NeilBrownfd01b882011-10-11 16:47:53 +11006271static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272{
NeilBrown3cb03002011-10-11 16:45:26 +11006273 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
6275 if (mddev->pers == NULL)
6276 return -ENODEV;
6277
6278 rdev = find_rdev(mddev, dev);
6279 if (!rdev)
6280 return -ENODEV;
6281
6282 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006283 if (!test_bit(Faulty, &rdev->flags))
6284 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 return 0;
6286}
6287
Andre Noll2f9618c2008-04-25 18:57:58 +02006288/*
6289 * We have a problem here : there is no easy way to give a CHS
6290 * virtual geometry. We currently pretend that we have a 2 heads
6291 * 4 sectors (with a BIG number of cylinders...). This drives
6292 * dosfs just mad... ;-)
6293 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006294static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6295{
NeilBrownfd01b882011-10-11 16:47:53 +11006296 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006297
6298 geo->heads = 2;
6299 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006300 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006301 return 0;
6302}
6303
Al Viroa39907f2008-03-02 10:31:15 -05006304static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 unsigned int cmd, unsigned long arg)
6306{
6307 int err = 0;
6308 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006309 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006310 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
NeilBrown506c9e42011-12-23 10:17:26 +11006312 switch (cmd) {
6313 case RAID_VERSION:
6314 case GET_ARRAY_INFO:
6315 case GET_DISK_INFO:
6316 break;
6317 default:
6318 if (!capable(CAP_SYS_ADMIN))
6319 return -EACCES;
6320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 /*
6323 * Commands dealing with the RAID driver but not any
6324 * particular array:
6325 */
6326 switch (cmd)
6327 {
6328 case RAID_VERSION:
6329 err = get_version(argp);
6330 goto done;
6331
6332 case PRINT_RAID_DEBUG:
6333 err = 0;
6334 md_print_devices();
6335 goto done;
6336
6337#ifndef MODULE
6338 case RAID_AUTORUN:
6339 err = 0;
6340 autostart_arrays(arg);
6341 goto done;
6342#endif
6343 default:;
6344 }
6345
6346 /*
6347 * Commands creating/starting a new array:
6348 */
6349
Al Viroa39907f2008-03-02 10:31:15 -05006350 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
6352 if (!mddev) {
6353 BUG();
6354 goto abort;
6355 }
6356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 err = mddev_lock(mddev);
6358 if (err) {
6359 printk(KERN_INFO
6360 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6361 err, cmd);
6362 goto abort;
6363 }
6364
6365 switch (cmd)
6366 {
6367 case SET_ARRAY_INFO:
6368 {
6369 mdu_array_info_t info;
6370 if (!arg)
6371 memset(&info, 0, sizeof(info));
6372 else if (copy_from_user(&info, argp, sizeof(info))) {
6373 err = -EFAULT;
6374 goto abort_unlock;
6375 }
6376 if (mddev->pers) {
6377 err = update_array_info(mddev, &info);
6378 if (err) {
6379 printk(KERN_WARNING "md: couldn't update"
6380 " array info. %d\n", err);
6381 goto abort_unlock;
6382 }
6383 goto done_unlock;
6384 }
6385 if (!list_empty(&mddev->disks)) {
6386 printk(KERN_WARNING
6387 "md: array %s already has disks!\n",
6388 mdname(mddev));
6389 err = -EBUSY;
6390 goto abort_unlock;
6391 }
6392 if (mddev->raid_disks) {
6393 printk(KERN_WARNING
6394 "md: array %s already initialised!\n",
6395 mdname(mddev));
6396 err = -EBUSY;
6397 goto abort_unlock;
6398 }
6399 err = set_array_info(mddev, &info);
6400 if (err) {
6401 printk(KERN_WARNING "md: couldn't set"
6402 " array info. %d\n", err);
6403 goto abort_unlock;
6404 }
6405 }
6406 goto done_unlock;
6407
6408 default:;
6409 }
6410
6411 /*
6412 * Commands querying/configuring an existing array:
6413 */
NeilBrown32a76272005-06-21 17:17:14 -07006414 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006415 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006416 if ((!mddev->raid_disks && !mddev->external)
6417 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6418 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6419 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 err = -ENODEV;
6421 goto abort_unlock;
6422 }
6423
6424 /*
6425 * Commands even a read-only array can execute:
6426 */
6427 switch (cmd)
6428 {
6429 case GET_ARRAY_INFO:
6430 err = get_array_info(mddev, argp);
6431 goto done_unlock;
6432
NeilBrown32a76272005-06-21 17:17:14 -07006433 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006434 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006435 goto done_unlock;
6436
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 case GET_DISK_INFO:
6438 err = get_disk_info(mddev, argp);
6439 goto done_unlock;
6440
6441 case RESTART_ARRAY_RW:
6442 err = restart_array(mddev);
6443 goto done_unlock;
6444
6445 case STOP_ARRAY:
NeilBrowna05b7ea2012-07-19 15:59:18 +10006446 err = do_md_stop(mddev, 0, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 goto done_unlock;
6448
6449 case STOP_ARRAY_RO:
NeilBrowna05b7ea2012-07-19 15:59:18 +10006450 err = md_set_readonly(mddev, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 goto done_unlock;
6452
Dan Williamse2218352010-05-12 08:25:37 +10006453 case BLKROSET:
6454 if (get_user(ro, (int __user *)(arg))) {
6455 err = -EFAULT;
6456 goto done_unlock;
6457 }
6458 err = -EINVAL;
6459
6460 /* if the bdev is going readonly the value of mddev->ro
6461 * does not matter, no writes are coming
6462 */
6463 if (ro)
6464 goto done_unlock;
6465
6466 /* are we are already prepared for writes? */
6467 if (mddev->ro != 1)
6468 goto done_unlock;
6469
6470 /* transitioning to readauto need only happen for
6471 * arrays that call md_write_start
6472 */
6473 if (mddev->pers) {
6474 err = restart_array(mddev);
6475 if (err == 0) {
6476 mddev->ro = 2;
6477 set_disk_ro(mddev->gendisk, 0);
6478 }
6479 }
6480 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
6482
6483 /*
6484 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006485 * superblock, so we do not allow them on read-only arrays.
6486 * However non-MD ioctls (e.g. get-size) will still come through
6487 * here and hit the 'default' below, so only disallow
6488 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006490 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006491 if (mddev->ro == 2) {
6492 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006493 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006494 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6495 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006496 } else {
6497 err = -EROFS;
6498 goto abort_unlock;
6499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 }
6501
6502 switch (cmd)
6503 {
6504 case ADD_NEW_DISK:
6505 {
6506 mdu_disk_info_t info;
6507 if (copy_from_user(&info, argp, sizeof(info)))
6508 err = -EFAULT;
6509 else
6510 err = add_new_disk(mddev, &info);
6511 goto done_unlock;
6512 }
6513
6514 case HOT_REMOVE_DISK:
6515 err = hot_remove_disk(mddev, new_decode_dev(arg));
6516 goto done_unlock;
6517
6518 case HOT_ADD_DISK:
6519 err = hot_add_disk(mddev, new_decode_dev(arg));
6520 goto done_unlock;
6521
6522 case SET_DISK_FAULTY:
6523 err = set_disk_faulty(mddev, new_decode_dev(arg));
6524 goto done_unlock;
6525
6526 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006527 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 goto done_unlock;
6529
NeilBrown32a76272005-06-21 17:17:14 -07006530 case SET_BITMAP_FILE:
6531 err = set_bitmap_file(mddev, (int)arg);
6532 goto done_unlock;
6533
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 err = -EINVAL;
6536 goto abort_unlock;
6537 }
6538
6539done_unlock:
6540abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006541 if (mddev->hold_active == UNTIL_IOCTL &&
6542 err != -EINVAL)
6543 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 mddev_unlock(mddev);
6545
6546 return err;
6547done:
6548 if (err)
6549 MD_BUG();
6550abort:
6551 return err;
6552}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006553#ifdef CONFIG_COMPAT
6554static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6555 unsigned int cmd, unsigned long arg)
6556{
6557 switch (cmd) {
6558 case HOT_REMOVE_DISK:
6559 case HOT_ADD_DISK:
6560 case SET_DISK_FAULTY:
6561 case SET_BITMAP_FILE:
6562 /* These take in integer arg, do not convert */
6563 break;
6564 default:
6565 arg = (unsigned long)compat_ptr(arg);
6566 break;
6567 }
6568
6569 return md_ioctl(bdev, mode, cmd, arg);
6570}
6571#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Al Viroa39907f2008-03-02 10:31:15 -05006573static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574{
6575 /*
6576 * Succeed if we can lock the mddev, which confirms that
6577 * it isn't being stopped right now.
6578 */
NeilBrownfd01b882011-10-11 16:47:53 +11006579 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 int err;
6581
Yuanhan Liu0c098222012-05-22 13:55:32 +10006582 if (!mddev)
6583 return -ENODEV;
6584
NeilBrownd3374822009-01-09 08:31:10 +11006585 if (mddev->gendisk != bdev->bd_disk) {
6586 /* we are racing with mddev_put which is discarding this
6587 * bd_disk.
6588 */
6589 mddev_put(mddev);
6590 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006591 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006592 /* Then retry the open from the top */
6593 return -ERESTARTSYS;
6594 }
6595 BUG_ON(mddev != bdev->bd_disk->private_data);
6596
NeilBrownc8c00a62009-08-10 12:50:52 +10006597 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 goto out;
6599
6600 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006601 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006602 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006604 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 out:
6606 return err;
6607}
6608
Al Viroa39907f2008-03-02 10:31:15 -05006609static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610{
NeilBrownfd01b882011-10-11 16:47:53 +11006611 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006613 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006614 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 mddev_put(mddev);
6616
6617 return 0;
6618}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006619
6620static int md_media_changed(struct gendisk *disk)
6621{
NeilBrownfd01b882011-10-11 16:47:53 +11006622 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006623
6624 return mddev->changed;
6625}
6626
6627static int md_revalidate(struct gendisk *disk)
6628{
NeilBrownfd01b882011-10-11 16:47:53 +11006629 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006630
6631 mddev->changed = 0;
6632 return 0;
6633}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006634static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635{
6636 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006637 .open = md_open,
6638 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006639 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006640#ifdef CONFIG_COMPAT
6641 .compat_ioctl = md_compat_ioctl,
6642#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006643 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006644 .media_changed = md_media_changed,
6645 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646};
6647
Adrian Bunk75c96f82005-05-05 16:16:09 -07006648static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649{
NeilBrown2b8bf342011-10-11 16:48:23 +11006650 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 /*
6653 * md_thread is a 'system-thread', it's priority should be very
6654 * high. We avoid resource deadlocks individually in each
6655 * raid personality. (RAID5 does preallocation) We also use RR and
6656 * the very same RT priority as kswapd, thus we will never get
6657 * into a priority inversion deadlock.
6658 *
6659 * we definitely have to have equal or higher priority than
6660 * bdflush, otherwise bdflush will deadlock if there are too
6661 * many dirty RAID5 blocks.
6662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
NeilBrown6985c432005-10-19 21:23:47 -07006664 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006665 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
NeilBrown93588e22005-11-15 00:09:12 -08006667 /* We need to wait INTERRUPTIBLE so that
6668 * we don't add to the load-average.
6669 * That means we need to be sure no signals are
6670 * pending
6671 */
6672 if (signal_pending(current))
6673 flush_signals(current);
6674
6675 wait_event_interruptible_timeout
6676 (thread->wqueue,
6677 test_bit(THREAD_WAKEUP, &thread->flags)
6678 || kthread_should_stop(),
6679 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
NeilBrown6c987912011-01-14 09:13:53 +11006681 clear_bit(THREAD_WAKEUP, &thread->flags);
6682 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006683 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 return 0;
6687}
6688
NeilBrown2b8bf342011-10-11 16:48:23 +11006689void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690{
6691 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006692 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 set_bit(THREAD_WAKEUP, &thread->flags);
6694 wake_up(&thread->wqueue);
6695 }
6696}
6697
NeilBrown2b8bf342011-10-11 16:48:23 +11006698struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 const char *name)
6700{
NeilBrown2b8bf342011-10-11 16:48:23 +11006701 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702
NeilBrown2b8bf342011-10-11 16:48:23 +11006703 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 if (!thread)
6705 return NULL;
6706
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 init_waitqueue_head(&thread->wqueue);
6708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 thread->run = run;
6710 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006711 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006712 thread->tsk = kthread_run(md_thread, thread,
6713 "%s_%s",
6714 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10006715 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006716 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 kfree(thread);
6718 return NULL;
6719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 return thread;
6721}
6722
NeilBrown2b8bf342011-10-11 16:48:23 +11006723void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724{
NeilBrown2b8bf342011-10-11 16:48:23 +11006725 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006726 if (!thread)
6727 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006728 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006729 /* Locking ensures that mddev_unlock does not wake_up a
6730 * non-existent thread
6731 */
6732 spin_lock(&pers_lock);
6733 *threadp = NULL;
6734 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006735
6736 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 kfree(thread);
6738}
6739
NeilBrownfd01b882011-10-11 16:47:53 +11006740void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741{
6742 if (!mddev) {
6743 MD_BUG();
6744 return;
6745 }
6746
NeilBrownb2d444d2005-11-08 21:39:31 -08006747 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006749
NeilBrownde393cd2011-07-28 11:31:48 +10006750 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 return;
6752 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006753 if (mddev->degraded)
6754 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006755 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6757 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6758 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006759 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006760 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006761 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762}
6763
6764/* seq_file implementation /proc/mdstat */
6765
6766static void status_unused(struct seq_file *seq)
6767{
6768 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006769 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
6771 seq_printf(seq, "unused devices: ");
6772
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006773 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 char b[BDEVNAME_SIZE];
6775 i++;
6776 seq_printf(seq, "%s ",
6777 bdevname(rdev->bdev,b));
6778 }
6779 if (!i)
6780 seq_printf(seq, "<none>");
6781
6782 seq_printf(seq, "\n");
6783}
6784
6785
NeilBrownfd01b882011-10-11 16:47:53 +11006786static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787{
NeilBrowndd71cf62009-05-07 12:49:35 +10006788 sector_t max_sectors, resync, res;
6789 unsigned long dt, db;
6790 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006791 int scale;
6792 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793
NeilBrowndd71cf62009-05-07 12:49:35 +10006794 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
NeilBrownc804cde2012-05-21 09:28:33 +10006796 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6797 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006798 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006800 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
6802 /*
6803 * Should not happen.
6804 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006805 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 MD_BUG();
6807 return;
6808 }
NeilBrown4588b422006-03-27 01:18:04 -08006809 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006810 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006811 * u32, as those are the requirements for sector_div.
6812 * Thus 'scale' must be at least 10
6813 */
6814 scale = 10;
6815 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006816 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006817 scale++;
6818 }
6819 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006820 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006821
6822 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 {
NeilBrown4588b422006-03-27 01:18:04 -08006824 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 seq_printf(seq, "[");
6826 for (i = 0; i < x; i++)
6827 seq_printf(seq, "=");
6828 seq_printf(seq, ">");
6829 for (i = 0; i < y; i++)
6830 seq_printf(seq, ".");
6831 seq_printf(seq, "] ");
6832 }
NeilBrown4588b422006-03-27 01:18:04 -08006833 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006834 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6835 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006836 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6837 "check" :
6838 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6839 "resync" : "recovery"))),
6840 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006841 (unsigned long long) resync/2,
6842 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
6844 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 * dt: time from mark until now
6846 * db: blocks written from mark until now
6847 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006848 *
6849 * rt is a sector_t, so could be 32bit or 64bit.
6850 * So we divide before multiply in case it is 32bit and close
6851 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006852 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006853 * near the end of resync when the number of remaining sectors
6854 * is close to 'db'.
6855 * We then divide rt by 32 after multiplying by db to compensate.
6856 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 */
6858 dt = ((jiffies - mddev->resync_mark) / HZ);
6859 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006860 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6861 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
NeilBrowndd71cf62009-05-07 12:49:35 +10006863 rt = max_sectors - resync; /* number of remaining sectors */
6864 sector_div(rt, db/32+1);
6865 rt *= dt;
6866 rt >>= 5;
6867
6868 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6869 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870
NeilBrownff4e8d92006-07-10 04:44:16 -07006871 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872}
6873
6874static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6875{
6876 struct list_head *tmp;
6877 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006878 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
6880 if (l >= 0x10000)
6881 return NULL;
6882 if (!l--)
6883 /* header */
6884 return (void*)1;
6885
6886 spin_lock(&all_mddevs_lock);
6887 list_for_each(tmp,&all_mddevs)
6888 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006889 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 mddev_get(mddev);
6891 spin_unlock(&all_mddevs_lock);
6892 return mddev;
6893 }
6894 spin_unlock(&all_mddevs_lock);
6895 if (!l--)
6896 return (void*)2;/* tail */
6897 return NULL;
6898}
6899
6900static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6901{
6902 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006903 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904
6905 ++*pos;
6906 if (v == (void*)2)
6907 return NULL;
6908
6909 spin_lock(&all_mddevs_lock);
6910 if (v == (void*)1)
6911 tmp = all_mddevs.next;
6912 else
6913 tmp = mddev->all_mddevs.next;
6914 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006915 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 else {
6917 next_mddev = (void*)2;
6918 *pos = 0x10000;
6919 }
6920 spin_unlock(&all_mddevs_lock);
6921
6922 if (v != (void*)1)
6923 mddev_put(mddev);
6924 return next_mddev;
6925
6926}
6927
6928static void md_seq_stop(struct seq_file *seq, void *v)
6929{
NeilBrownfd01b882011-10-11 16:47:53 +11006930 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
6932 if (mddev && v != (void*)1 && v != (void*)2)
6933 mddev_put(mddev);
6934}
6935
6936static int md_seq_show(struct seq_file *seq, void *v)
6937{
NeilBrownfd01b882011-10-11 16:47:53 +11006938 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006939 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006940 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006943 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 seq_printf(seq, "Personalities : ");
6945 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006946 list_for_each_entry(pers, &pers_list, list)
6947 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
6949 spin_unlock(&pers_lock);
6950 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006951 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 return 0;
6953 }
6954 if (v == (void*)2) {
6955 status_unused(seq);
6956 return 0;
6957 }
6958
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006959 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006961
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6963 seq_printf(seq, "%s : %sactive", mdname(mddev),
6964 mddev->pers ? "" : "in");
6965 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006966 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006968 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006969 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 seq_printf(seq, " %s", mddev->pers->name);
6971 }
6972
Andre Nolldd8ac332009-03-31 14:33:13 +11006973 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006974 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 char b[BDEVNAME_SIZE];
6976 seq_printf(seq, " %s[%d]",
6977 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006978 if (test_bit(WriteMostly, &rdev->flags))
6979 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006980 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 seq_printf(seq, "(F)");
6982 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006983 }
6984 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006985 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006986 if (test_bit(Replacement, &rdev->flags))
6987 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006988 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 }
6990
6991 if (!list_empty(&mddev->disks)) {
6992 if (mddev->pers)
6993 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006994 (unsigned long long)
6995 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 else
6997 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006998 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007000 if (mddev->persistent) {
7001 if (mddev->major_version != 0 ||
7002 mddev->minor_version != 90) {
7003 seq_printf(seq," super %d.%d",
7004 mddev->major_version,
7005 mddev->minor_version);
7006 }
NeilBrowne6910632008-02-06 01:39:51 -08007007 } else if (mddev->external)
7008 seq_printf(seq, " super external:%s",
7009 mddev->metadata_type);
7010 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007011 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
7013 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007014 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007016 if (mddev->pers->sync_request) {
7017 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007018 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007019 seq_printf(seq, "\n ");
7020 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
7021 seq_printf(seq, "\tresync=DELAYED\n ");
7022 else if (mddev->recovery_cp < MaxSector)
7023 seq_printf(seq, "\tresync=PENDING\n ");
7024 }
NeilBrown32a76272005-06-21 17:17:14 -07007025 } else
7026 seq_printf(seq, "\n ");
7027
NeilBrown57148962012-03-19 12:46:40 +11007028 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
7030 seq_printf(seq, "\n");
7031 }
7032 mddev_unlock(mddev);
7033
7034 return 0;
7035}
7036
Jan Engelhardt110518b2009-05-07 12:49:37 +10007037static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 .start = md_seq_start,
7039 .next = md_seq_next,
7040 .stop = md_seq_stop,
7041 .show = md_seq_show,
7042};
7043
7044static int md_seq_open(struct inode *inode, struct file *file)
7045{
Kay Sieversf1514632011-07-12 20:48:39 +02007046 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 int error;
7048
7049 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007050 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007051 return error;
7052
7053 seq = file->private_data;
7054 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 return error;
7056}
7057
NeilBrownd7603b72006-01-06 00:20:30 -08007058static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7059{
Kay Sieversf1514632011-07-12 20:48:39 +02007060 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007061 int mask;
7062
7063 poll_wait(filp, &md_event_waiters, wait);
7064
7065 /* always allow read */
7066 mask = POLLIN | POLLRDNORM;
7067
Kay Sieversf1514632011-07-12 20:48:39 +02007068 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007069 mask |= POLLERR | POLLPRI;
7070 return mask;
7071}
7072
Arjan van de Venfa027c22007-02-12 00:55:33 -08007073static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007074 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 .open = md_seq_open,
7076 .read = seq_read,
7077 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007078 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007079 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080};
7081
NeilBrown84fc4b52011-10-11 16:49:58 +11007082int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007085 list_add_tail(&p->list, &pers_list);
7086 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 spin_unlock(&pers_lock);
7088 return 0;
7089}
7090
NeilBrown84fc4b52011-10-11 16:49:58 +11007091int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092{
NeilBrown2604b702006-01-06 00:20:36 -08007093 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007095 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 spin_unlock(&pers_lock);
7097 return 0;
7098}
7099
NeilBrownfd01b882011-10-11 16:47:53 +11007100static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
NeilBrown3cb03002011-10-11 16:45:26 +11007102 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007104 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105
7106 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007107 rcu_read_lock();
7108 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007110 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7111 (int)part_stat_read(&disk->part0, sectors[1]) -
7112 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007113 /* sync IO will cause sync_io to increase before the disk_stats
7114 * as sync_io is counted when a request starts, and
7115 * disk_stats is counted when it completes.
7116 * So resync activity will cause curr_events to be smaller than
7117 * when there was no such activity.
7118 * non-sync IO will cause disk_stat to increase without
7119 * increasing sync_io so curr_events will (eventually)
7120 * be larger than it was before. Once it becomes
7121 * substantially larger, the test below will cause
7122 * the array to appear non-idle, and resync will slow
7123 * down.
7124 * If there is a lot of outstanding resync activity when
7125 * we set last_event to curr_events, then all that activity
7126 * completing might cause the array to appear non-idle
7127 * and resync will be slowed down even though there might
7128 * not have been non-resync activity. This will only
7129 * happen once though. 'last_events' will soon reflect
7130 * the state where there is little or no outstanding
7131 * resync requests, and further resync activity will
7132 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007135 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 rdev->last_events = curr_events;
7137 idle = 0;
7138 }
7139 }
NeilBrown4b809912008-07-21 17:05:25 +10007140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 return idle;
7142}
7143
NeilBrownfd01b882011-10-11 16:47:53 +11007144void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145{
7146 /* another "blocks" (512byte) blocks have been synced */
7147 atomic_sub(blocks, &mddev->recovery_active);
7148 wake_up(&mddev->recovery_wait);
7149 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007150 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 md_wakeup_thread(mddev->thread);
7152 // stop recovery, signal do_sync ....
7153 }
7154}
7155
7156
NeilBrown06d91a52005-06-21 17:17:12 -07007157/* md_write_start(mddev, bi)
7158 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007159 * in superblock) before writing, schedule a superblock update
7160 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007161 */
NeilBrownfd01b882011-10-11 16:47:53 +11007162void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163{
Neil Brown0fd62b82008-06-28 08:31:36 +10007164 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007165 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007166 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007167
NeilBrownf91de922005-11-08 21:39:36 -08007168 BUG_ON(mddev->ro == 1);
7169 if (mddev->ro == 2) {
7170 /* need to switch to read/write */
7171 mddev->ro = 0;
7172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7173 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007174 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007175 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007176 }
NeilBrown06d91a52005-06-21 17:17:12 -07007177 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007178 if (mddev->safemode == 1)
7179 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007180 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007181 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007182 if (mddev->in_sync) {
7183 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007184 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007185 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007186 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007187 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007188 }
NeilBrowna9701a32005-11-08 21:39:34 -08007189 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007190 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007191 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007192 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007193 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007194 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195}
7196
NeilBrownfd01b882011-10-11 16:47:53 +11007197void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198{
7199 if (atomic_dec_and_test(&mddev->writes_pending)) {
7200 if (mddev->safemode == 2)
7201 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007202 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7204 }
7205}
7206
NeilBrown2a2275d2007-01-26 00:57:11 -08007207/* md_allow_write(mddev)
7208 * Calling this ensures that the array is marked 'active' so that writes
7209 * may proceed without blocking. It is important to call this before
7210 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7211 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007212 *
7213 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7214 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007215 */
NeilBrownfd01b882011-10-11 16:47:53 +11007216int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007217{
7218 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007219 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007220 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007221 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007222 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007223 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007224
7225 spin_lock_irq(&mddev->write_lock);
7226 if (mddev->in_sync) {
7227 mddev->in_sync = 0;
7228 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007229 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007230 if (mddev->safemode_delay &&
7231 mddev->safemode == 0)
7232 mddev->safemode = 1;
7233 spin_unlock_irq(&mddev->write_lock);
7234 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007235 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007236 } else
7237 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007238
NeilBrown070dc6d2010-08-30 17:33:34 +10007239 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007240 return -EAGAIN;
7241 else
7242 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007243}
7244EXPORT_SYMBOL_GPL(md_allow_write);
7245
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246#define SYNC_MARKS 10
7247#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007248void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249{
NeilBrownfd01b882011-10-11 16:47:53 +11007250 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 unsigned int currspeed = 0,
7252 window;
NeilBrown57afd892005-06-21 17:17:13 -07007253 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 unsigned long mark[SYNC_MARKS];
7255 sector_t mark_cnt[SYNC_MARKS];
7256 int last_mark,m;
7257 struct list_head *tmp;
7258 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007259 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007260 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007261 char *desc;
majianpeng7c2c57c2012-07-03 12:12:26 +10007262 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263
7264 /* just incase thread restarts... */
7265 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7266 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007267 if (mddev->ro) /* never try to sync a read-only array */
7268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
NeilBrown61df9d92006-10-03 01:15:57 -07007270 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7271 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7272 desc = "data-check";
7273 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7274 desc = "requested-resync";
7275 else
7276 desc = "resync";
7277 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7278 desc = "reshape";
7279 else
7280 desc = "recovery";
7281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 /* we overload curr_resync somewhat here.
7283 * 0 == not engaged in resync at all
7284 * 2 == checking that there is no conflict with another sync
7285 * 1 == like 2, but have yielded to allow conflicting resync to
7286 * commense
7287 * other == active in resync - this many blocks
7288 *
7289 * Before starting a resync we must have set curr_resync to
7290 * 2, and then checked that every "conflicting" array has curr_resync
7291 * less than ours. When we find one that is the same or higher
7292 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7293 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7294 * This will mean we have to start checking from the beginning again.
7295 *
7296 */
7297
7298 do {
7299 mddev->curr_resync = 2;
7300
7301 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007302 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007303 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007304
7305 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007307 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 if (mddev2 == mddev)
7309 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007310 if (!mddev->parallel_resync
7311 && mddev2->curr_resync
7312 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 DEFINE_WAIT(wq);
7314 if (mddev < mddev2 && mddev->curr_resync == 2) {
7315 /* arbitrarily yield */
7316 mddev->curr_resync = 1;
7317 wake_up(&resync_wait);
7318 }
7319 if (mddev > mddev2 && mddev->curr_resync == 1)
7320 /* no need to wait here, we can wait the next
7321 * time 'round when curr_resync == 2
7322 */
7323 continue;
NeilBrown97441972008-09-19 11:49:54 +10007324 /* We need to wait 'interruptible' so as not to
7325 * contribute to the load average, and not to
7326 * be caught by 'softlockup'
7327 */
7328 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007329 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007330 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007331 printk(KERN_INFO "md: delaying %s of %s"
7332 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007334 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007336 if (signal_pending(current))
7337 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 schedule();
7339 finish_wait(&resync_wait, &wq);
7340 goto try_again;
7341 }
7342 finish_wait(&resync_wait, &wq);
7343 }
7344 }
7345 } while (mddev->curr_resync < 2);
7346
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007347 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007348 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007350 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 */
7352 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007353 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007354 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007355 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7356 j = mddev->resync_min;
7357 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007358 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007359
NeilBrownccfcc3c2006-03-27 01:18:09 -08007360 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007361 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007362 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007364 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007365 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007366 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007367 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007368 if (rdev->raid_disk >= 0 &&
7369 !test_bit(Faulty, &rdev->flags) &&
7370 !test_bit(In_sync, &rdev->flags) &&
7371 rdev->recovery_offset < j)
7372 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007373 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
NeilBrown61df9d92006-10-03 01:15:57 -07007376 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7377 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7378 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007379 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007380 "(but not more than %d KB/sec) for %s.\n",
7381 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
NeilBrowneea1bf32009-03-31 14:27:02 +11007383 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007384
NeilBrown57afd892005-06-21 17:17:13 -07007385 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386 for (m = 0; m < SYNC_MARKS; m++) {
7387 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007388 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 }
7390 last_mark = 0;
7391 mddev->resync_mark = mark[last_mark];
7392 mddev->resync_mark_cnt = mark_cnt[last_mark];
7393
7394 /*
7395 * Tune reconstruction:
7396 */
7397 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007398 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7399 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
7401 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 last_check = 0;
7403
7404 if (j>2) {
7405 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007406 "md: resuming %s of %s from checkpoint.\n",
7407 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 mddev->curr_resync = j;
7409 }
NeilBrown75d3da42011-01-14 09:14:34 +11007410 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411
majianpeng7c2c57c2012-07-03 12:12:26 +10007412 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007414 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415
NeilBrown57afd892005-06-21 17:17:13 -07007416 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007417
NeilBrown7a91ee12009-05-26 12:57:21 +10007418 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7419 ((mddev->curr_resync > mddev->curr_resync_completed &&
7420 (mddev->curr_resync - mddev->curr_resync_completed)
7421 > (max_sectors >> 4)) ||
7422 (j - mddev->curr_resync_completed)*2
7423 >= mddev->resync_max - mddev->curr_resync_completed
7424 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007425 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007426 wait_event(mddev->recovery_wait,
7427 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007428 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007429 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007430 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007431 }
NeilBrownacb180b2009-04-14 16:28:34 +10007432
NeilBrowne62e58a2009-07-01 13:15:35 +10007433 while (j >= mddev->resync_max && !kthread_should_stop()) {
7434 /* As this condition is controlled by user-space,
7435 * we can block indefinitely, so use '_interruptible'
7436 * to avoid triggering warnings.
7437 */
7438 flush_signals(current); /* just in case */
7439 wait_event_interruptible(mddev->recovery_wait,
7440 mddev->resync_max > j
7441 || kthread_should_stop());
7442 }
NeilBrownacb180b2009-04-14 16:28:34 +10007443
7444 if (kthread_should_stop())
7445 goto interrupted;
7446
NeilBrown57afd892005-06-21 17:17:13 -07007447 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007448 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007449 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007450 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 goto out;
7452 }
NeilBrown57afd892005-06-21 17:17:13 -07007453
7454 if (!skipped) { /* actual IO requested */
7455 io_sectors += sectors;
7456 atomic_add(sectors, &mddev->recovery_active);
7457 }
7458
NeilBrowne875ece2011-07-28 11:39:24 +10007459 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7460 break;
7461
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 j += sectors;
7463 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007464 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007465 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007466 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007467 * visible in /proc/mdstat
7468 */
7469 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007470
7471 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 continue;
7473
NeilBrown57afd892005-06-21 17:17:13 -07007474 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 repeat:
7476 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7477 /* step marks */
7478 int next = (last_mark+1) % SYNC_MARKS;
7479
7480 mddev->resync_mark = mark[next];
7481 mddev->resync_mark_cnt = mark_cnt[next];
7482 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007483 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 last_mark = next;
7485 }
7486
7487
NeilBrownc6207272008-02-06 01:39:52 -08007488 if (kthread_should_stop())
7489 goto interrupted;
7490
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
7492 /*
7493 * this loop exits only if either when we are slower than
7494 * the 'hard' speed limit, or the system was IO-idle for
7495 * a jiffy.
7496 * the system might be non-idle CPU-wise, but we only care
7497 * about not overloading the IO subsystem. (things like an
7498 * e2fsck being done on the RAID array should execute fast)
7499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 cond_resched();
7501
NeilBrown57afd892005-06-21 17:17:13 -07007502 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7503 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504
NeilBrown88202a02006-01-06 00:21:36 -08007505 if (currspeed > speed_min(mddev)) {
7506 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007507 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007508 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 goto repeat;
7510 }
7511 }
7512 }
NeilBrown61df9d92006-10-03 01:15:57 -07007513 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 /*
7515 * this also signals 'finished resyncing' to md_stop
7516 */
7517 out:
majianpeng7c2c57c2012-07-03 12:12:26 +10007518 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7520
7521 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007522 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523
NeilBrowndfc70642008-05-23 13:04:39 -07007524 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007525 mddev->curr_resync > 2) {
7526 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7527 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7528 if (mddev->curr_resync >= mddev->recovery_cp) {
7529 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007530 "md: checkpointing %s of %s.\n",
7531 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007532 mddev->recovery_cp =
7533 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007534 }
7535 } else
7536 mddev->recovery_cp = MaxSector;
7537 } else {
7538 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7539 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007540 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007541 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007542 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007543 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007544 !test_bit(Faulty, &rdev->flags) &&
7545 !test_bit(In_sync, &rdev->flags) &&
7546 rdev->recovery_offset < mddev->curr_resync)
7547 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007548 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007551 skip:
NeilBrown17571282006-12-10 02:20:52 -08007552 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
NeilBrownc07b70a2009-12-14 12:49:48 +11007554 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7555 /* We completed so min/max setting can be forgotten if used. */
7556 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7557 mddev->resync_min = 0;
7558 mddev->resync_max = MaxSector;
7559 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7560 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 mddev->curr_resync = 0;
7562 wake_up(&resync_wait);
7563 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7564 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007565 return;
7566
7567 interrupted:
7568 /*
7569 * got a signal, exit.
7570 */
7571 printk(KERN_INFO
7572 "md: md_do_sync() got signal ... exiting\n");
7573 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7574 goto out;
7575
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576}
NeilBrown29269552006-03-27 01:18:10 -08007577EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
NeilBrownfd01b882011-10-11 16:47:53 +11007579static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007580{
NeilBrown3cb03002011-10-11 16:45:26 +11007581 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007582 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007583 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007584
NeilBrown97e4f422009-03-31 14:33:13 +11007585 mddev->curr_resync_completed = 0;
7586
NeilBrowndafb20f2012-03-19 12:46:39 +11007587 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007588 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007589 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007590 (test_bit(Faulty, &rdev->flags) ||
7591 ! test_bit(In_sync, &rdev->flags)) &&
7592 atomic_read(&rdev->nr_pending)==0) {
7593 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007594 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007595 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007596 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007597 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007598 }
7599 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007600 if (removed)
7601 sysfs_notify(&mddev->kobj, NULL,
7602 "degraded");
7603
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007604
NeilBrowndafb20f2012-03-19 12:46:39 +11007605 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007606 if (rdev->raid_disk >= 0 &&
7607 !test_bit(In_sync, &rdev->flags) &&
7608 !test_bit(Faulty, &rdev->flags))
7609 spares++;
7610 if (rdev->raid_disk < 0
7611 && !test_bit(Faulty, &rdev->flags)) {
7612 rdev->recovery_offset = 0;
7613 if (mddev->pers->
7614 hot_add_disk(mddev, rdev) == 0) {
7615 if (sysfs_link_rdev(mddev, rdev))
7616 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007617 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007618 md_new_event(mddev);
7619 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007620 }
NeilBrowndfc70642008-05-23 13:04:39 -07007621 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007622 }
7623 return spares;
7624}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007625
NeilBrownfd01b882011-10-11 16:47:53 +11007626static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007627{
NeilBrown3cb03002011-10-11 16:45:26 +11007628 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007629
7630 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007631 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007632 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7633 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7634 /* success...*/
7635 /* activate any spares */
7636 if (mddev->pers->spare_active(mddev))
7637 sysfs_notify(&mddev->kobj, NULL,
7638 "degraded");
7639 }
7640 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7641 mddev->pers->finish_reshape)
7642 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007643
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007644 /* If array is no-longer degraded, then any saved_raid_disk
7645 * information must be scrapped. Also if any device is now
7646 * In_sync we must scrape the saved_raid_disk for that device
7647 * do the superblock for an incrementally recovered device
7648 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007649 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007650 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007651 if (!mddev->degraded ||
7652 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007653 rdev->saved_raid_disk = -1;
7654
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007655 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007656 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7657 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7658 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7659 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7660 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7661 /* flag recovery needed just to double check */
7662 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7663 sysfs_notify_dirent_safe(mddev->sysfs_action);
7664 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007665 if (mddev->event_work.func)
7666 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007667}
7668
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669/*
7670 * This routine is regularly called by all per-raid-array threads to
7671 * deal with generic issues like resync and super-block update.
7672 * Raid personalities that don't have a thread (linear/raid0) do not
7673 * need this as they never do any recovery or update the superblock.
7674 *
7675 * It does not do any resync itself, but rather "forks" off other threads
7676 * to do that as needed.
7677 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7678 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007679 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 * and wakeups up this thread which will reap the thread and finish up.
7681 * This thread also removes any faulty devices (with nr_pending == 0).
7682 *
7683 * The overall approach is:
7684 * 1/ if the superblock needs updating, update it.
7685 * 2/ If a recovery thread is running, don't do anything else.
7686 * 3/ If recovery has finished, clean up, possibly marking spares active.
7687 * 4/ If there are any faulty devices, remove them.
7688 * 5/ If array is degraded, try to add spares devices
7689 * 6/ If array has spares or is not in-sync, start a resync thread.
7690 */
NeilBrownfd01b882011-10-11 16:47:53 +11007691void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007693 if (mddev->suspended)
7694 return;
7695
NeilBrown5f404022005-06-21 17:17:16 -07007696 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007697 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
NeilBrownfca4d842005-06-21 17:17:11 -07007699 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007700 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007701 printk(KERN_INFO "md: %s in immediate safe mode\n",
7702 mdname(mddev));
7703 mddev->safemode = 2;
7704 }
7705 flush_signals(current);
7706 }
7707
NeilBrownc89a8ee2008-08-05 15:54:13 +10007708 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007711 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007713 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007714 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007715 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7716 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 ))
7718 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007719
NeilBrowndf5b89b2006-03-27 01:18:20 -08007720 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007721 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007722
NeilBrownc89a8ee2008-08-05 15:54:13 +10007723 if (mddev->ro) {
7724 /* Only thing we do on a ro array is remove
7725 * failed devices.
7726 */
NeilBrown3cb03002011-10-11 16:45:26 +11007727 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007728 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007729 if (rdev->raid_disk >= 0 &&
7730 !test_bit(Blocked, &rdev->flags) &&
7731 test_bit(Faulty, &rdev->flags) &&
7732 atomic_read(&rdev->nr_pending)==0) {
7733 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007734 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007735 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007736 rdev->raid_disk = -1;
7737 }
7738 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007739 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7740 goto unlock;
7741 }
7742
NeilBrown31a59e32008-04-30 00:52:30 -07007743 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007744 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007745 spin_lock_irq(&mddev->write_lock);
7746 if (mddev->safemode &&
7747 !atomic_read(&mddev->writes_pending) &&
7748 !mddev->in_sync &&
7749 mddev->recovery_cp == MaxSector) {
7750 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007751 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007752 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007753 }
7754 if (mddev->safemode == 1)
7755 mddev->safemode = 0;
7756 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007757 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007758 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007759 }
NeilBrownfca4d842005-06-21 17:17:11 -07007760
NeilBrown850b2b422006-10-03 01:15:46 -07007761 if (mddev->flags)
7762 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007763
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7765 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7766 /* resync/recovery still happening */
7767 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7768 goto unlock;
7769 }
7770 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007771 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 goto unlock;
7773 }
Neil Brown72a23c22008-06-28 08:31:41 +10007774 /* Set RUNNING before clearing NEEDED to avoid
7775 * any transients in the value of "sync_action".
7776 */
7777 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007778 /* Clear some bits that don't mean anything, but
7779 * might be left set
7780 */
NeilBrown24dd4692005-11-08 21:39:26 -08007781 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7782 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
NeilBrowned209582012-04-24 10:23:14 +10007784 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7785 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007786 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007787 /* no recovery is running.
7788 * remove any failed drives, then
7789 * add spares if possible.
7790 * Spare are also removed and re-added, to allow
7791 * the personality to fail the re-add.
7792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007794 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007795 if (mddev->pers->check_reshape == NULL ||
7796 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007797 /* Cannot proceed */
7798 goto unlock;
7799 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007800 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007801 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007802 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7803 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007804 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007805 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007806 } else if (mddev->recovery_cp < MaxSector) {
7807 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007808 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007809 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7810 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10007814 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007815 /* We are adding a device or devices to an array
7816 * which has the bitmap stored on all devices.
7817 * So make sure all bitmap pages get written
7818 */
7819 bitmap_write_all(mddev->bitmap);
7820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 mddev->sync_thread = md_register_thread(md_do_sync,
7822 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007823 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 if (!mddev->sync_thread) {
7825 printk(KERN_ERR "%s: could not start resync"
7826 " thread...\n",
7827 mdname(mddev));
7828 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007829 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7830 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7831 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7832 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7833 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007834 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007836 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007837 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 }
7839 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007840 if (!mddev->sync_thread) {
7841 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7842 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7843 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007844 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007845 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847 mddev_unlock(mddev);
7848 }
7849}
7850
NeilBrownfd01b882011-10-11 16:47:53 +11007851void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007852{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007853 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007854 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007855 !test_bit(Blocked, &rdev->flags) &&
7856 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007857 msecs_to_jiffies(5000));
7858 rdev_dec_pending(rdev, mddev);
7859}
7860EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7861
NeilBrownc6563a82012-05-21 09:27:00 +10007862void md_finish_reshape(struct mddev *mddev)
7863{
7864 /* called be personality module when reshape completes. */
7865 struct md_rdev *rdev;
7866
7867 rdev_for_each(rdev, mddev) {
7868 if (rdev->data_offset > rdev->new_data_offset)
7869 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7870 else
7871 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7872 rdev->data_offset = rdev->new_data_offset;
7873 }
7874}
7875EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007876
7877/* Bad block management.
7878 * We can record which blocks on each device are 'bad' and so just
7879 * fail those blocks, or that stripe, rather than the whole device.
7880 * Entries in the bad-block table are 64bits wide. This comprises:
7881 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7882 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7883 * A 'shift' can be set so that larger blocks are tracked and
7884 * consequently larger devices can be covered.
7885 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7886 *
7887 * Locking of the bad-block table uses a seqlock so md_is_badblock
7888 * might need to retry if it is very unlucky.
7889 * We will sometimes want to check for bad blocks in a bi_end_io function,
7890 * so we use the write_seqlock_irq variant.
7891 *
7892 * When looking for a bad block we specify a range and want to
7893 * know if any block in the range is bad. So we binary-search
7894 * to the last range that starts at-or-before the given endpoint,
7895 * (or "before the sector after the target range")
7896 * then see if it ends after the given start.
7897 * We return
7898 * 0 if there are no known bad blocks in the range
7899 * 1 if there are known bad block which are all acknowledged
7900 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7901 * plus the start/length of the first bad section we overlap.
7902 */
7903int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7904 sector_t *first_bad, int *bad_sectors)
7905{
7906 int hi;
7907 int lo = 0;
7908 u64 *p = bb->page;
7909 int rv = 0;
7910 sector_t target = s + sectors;
7911 unsigned seq;
7912
7913 if (bb->shift > 0) {
7914 /* round the start down, and the end up */
7915 s >>= bb->shift;
7916 target += (1<<bb->shift) - 1;
7917 target >>= bb->shift;
7918 sectors = target - s;
7919 }
7920 /* 'target' is now the first block after the bad range */
7921
7922retry:
7923 seq = read_seqbegin(&bb->lock);
7924
7925 hi = bb->count;
7926
7927 /* Binary search between lo and hi for 'target'
7928 * i.e. for the last range that starts before 'target'
7929 */
7930 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7931 * are known not to be the last range before target.
7932 * VARIANT: hi-lo is the number of possible
7933 * ranges, and decreases until it reaches 1
7934 */
7935 while (hi - lo > 1) {
7936 int mid = (lo + hi) / 2;
7937 sector_t a = BB_OFFSET(p[mid]);
7938 if (a < target)
7939 /* This could still be the one, earlier ranges
7940 * could not. */
7941 lo = mid;
7942 else
7943 /* This and later ranges are definitely out. */
7944 hi = mid;
7945 }
7946 /* 'lo' might be the last that started before target, but 'hi' isn't */
7947 if (hi > lo) {
7948 /* need to check all range that end after 's' to see if
7949 * any are unacknowledged.
7950 */
7951 while (lo >= 0 &&
7952 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7953 if (BB_OFFSET(p[lo]) < target) {
7954 /* starts before the end, and finishes after
7955 * the start, so they must overlap
7956 */
7957 if (rv != -1 && BB_ACK(p[lo]))
7958 rv = 1;
7959 else
7960 rv = -1;
7961 *first_bad = BB_OFFSET(p[lo]);
7962 *bad_sectors = BB_LEN(p[lo]);
7963 }
7964 lo--;
7965 }
7966 }
7967
7968 if (read_seqretry(&bb->lock, seq))
7969 goto retry;
7970
7971 return rv;
7972}
7973EXPORT_SYMBOL_GPL(md_is_badblock);
7974
7975/*
7976 * Add a range of bad blocks to the table.
7977 * This might extend the table, or might contract it
7978 * if two adjacent ranges can be merged.
7979 * We binary-search to find the 'insertion' point, then
7980 * decide how best to handle it.
7981 */
7982static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7983 int acknowledged)
7984{
7985 u64 *p;
7986 int lo, hi;
7987 int rv = 1;
7988
7989 if (bb->shift < 0)
7990 /* badblocks are disabled */
7991 return 0;
7992
7993 if (bb->shift) {
7994 /* round the start down, and the end up */
7995 sector_t next = s + sectors;
7996 s >>= bb->shift;
7997 next += (1<<bb->shift) - 1;
7998 next >>= bb->shift;
7999 sectors = next - s;
8000 }
8001
8002 write_seqlock_irq(&bb->lock);
8003
8004 p = bb->page;
8005 lo = 0;
8006 hi = bb->count;
8007 /* Find the last range that starts at-or-before 's' */
8008 while (hi - lo > 1) {
8009 int mid = (lo + hi) / 2;
8010 sector_t a = BB_OFFSET(p[mid]);
8011 if (a <= s)
8012 lo = mid;
8013 else
8014 hi = mid;
8015 }
8016 if (hi > lo && BB_OFFSET(p[lo]) > s)
8017 hi = lo;
8018
8019 if (hi > lo) {
8020 /* we found a range that might merge with the start
8021 * of our new range
8022 */
8023 sector_t a = BB_OFFSET(p[lo]);
8024 sector_t e = a + BB_LEN(p[lo]);
8025 int ack = BB_ACK(p[lo]);
8026 if (e >= s) {
8027 /* Yes, we can merge with a previous range */
8028 if (s == a && s + sectors >= e)
8029 /* new range covers old */
8030 ack = acknowledged;
8031 else
8032 ack = ack && acknowledged;
8033
8034 if (e < s + sectors)
8035 e = s + sectors;
8036 if (e - a <= BB_MAX_LEN) {
8037 p[lo] = BB_MAKE(a, e-a, ack);
8038 s = e;
8039 } else {
8040 /* does not all fit in one range,
8041 * make p[lo] maximal
8042 */
8043 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8044 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8045 s = a + BB_MAX_LEN;
8046 }
8047 sectors = e - s;
8048 }
8049 }
8050 if (sectors && hi < bb->count) {
8051 /* 'hi' points to the first range that starts after 's'.
8052 * Maybe we can merge with the start of that range */
8053 sector_t a = BB_OFFSET(p[hi]);
8054 sector_t e = a + BB_LEN(p[hi]);
8055 int ack = BB_ACK(p[hi]);
8056 if (a <= s + sectors) {
8057 /* merging is possible */
8058 if (e <= s + sectors) {
8059 /* full overlap */
8060 e = s + sectors;
8061 ack = acknowledged;
8062 } else
8063 ack = ack && acknowledged;
8064
8065 a = s;
8066 if (e - a <= BB_MAX_LEN) {
8067 p[hi] = BB_MAKE(a, e-a, ack);
8068 s = e;
8069 } else {
8070 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8071 s = a + BB_MAX_LEN;
8072 }
8073 sectors = e - s;
8074 lo = hi;
8075 hi++;
8076 }
8077 }
8078 if (sectors == 0 && hi < bb->count) {
8079 /* we might be able to combine lo and hi */
8080 /* Note: 's' is at the end of 'lo' */
8081 sector_t a = BB_OFFSET(p[hi]);
8082 int lolen = BB_LEN(p[lo]);
8083 int hilen = BB_LEN(p[hi]);
8084 int newlen = lolen + hilen - (s - a);
8085 if (s >= a && newlen < BB_MAX_LEN) {
8086 /* yes, we can combine them */
8087 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8088 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8089 memmove(p + hi, p + hi + 1,
8090 (bb->count - hi - 1) * 8);
8091 bb->count--;
8092 }
8093 }
8094 while (sectors) {
8095 /* didn't merge (it all).
8096 * Need to add a range just before 'hi' */
8097 if (bb->count >= MD_MAX_BADBLOCKS) {
8098 /* No room for more */
8099 rv = 0;
8100 break;
8101 } else {
8102 int this_sectors = sectors;
8103 memmove(p + hi + 1, p + hi,
8104 (bb->count - hi) * 8);
8105 bb->count++;
8106
8107 if (this_sectors > BB_MAX_LEN)
8108 this_sectors = BB_MAX_LEN;
8109 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8110 sectors -= this_sectors;
8111 s += this_sectors;
8112 }
8113 }
8114
8115 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008116 if (!acknowledged)
8117 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008118 write_sequnlock_irq(&bb->lock);
8119
8120 return rv;
8121}
8122
NeilBrown3cb03002011-10-11 16:45:26 +11008123int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008124 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008125{
NeilBrownc6563a82012-05-21 09:27:00 +10008126 int rv;
8127 if (is_new)
8128 s += rdev->new_data_offset;
8129 else
8130 s += rdev->data_offset;
8131 rv = md_set_badblocks(&rdev->badblocks,
8132 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008133 if (rv) {
8134 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008135 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008136 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8137 md_wakeup_thread(rdev->mddev->thread);
8138 }
8139 return rv;
8140}
8141EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8142
8143/*
8144 * Remove a range of bad blocks from the table.
8145 * This may involve extending the table if we spilt a region,
8146 * but it must not fail. So if the table becomes full, we just
8147 * drop the remove request.
8148 */
8149static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8150{
8151 u64 *p;
8152 int lo, hi;
8153 sector_t target = s + sectors;
8154 int rv = 0;
8155
8156 if (bb->shift > 0) {
8157 /* When clearing we round the start up and the end down.
8158 * This should not matter as the shift should align with
8159 * the block size and no rounding should ever be needed.
8160 * However it is better the think a block is bad when it
8161 * isn't than to think a block is not bad when it is.
8162 */
8163 s += (1<<bb->shift) - 1;
8164 s >>= bb->shift;
8165 target >>= bb->shift;
8166 sectors = target - s;
8167 }
8168
8169 write_seqlock_irq(&bb->lock);
8170
8171 p = bb->page;
8172 lo = 0;
8173 hi = bb->count;
8174 /* Find the last range that starts before 'target' */
8175 while (hi - lo > 1) {
8176 int mid = (lo + hi) / 2;
8177 sector_t a = BB_OFFSET(p[mid]);
8178 if (a < target)
8179 lo = mid;
8180 else
8181 hi = mid;
8182 }
8183 if (hi > lo) {
8184 /* p[lo] is the last range that could overlap the
8185 * current range. Earlier ranges could also overlap,
8186 * but only this one can overlap the end of the range.
8187 */
8188 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8189 /* Partial overlap, leave the tail of this range */
8190 int ack = BB_ACK(p[lo]);
8191 sector_t a = BB_OFFSET(p[lo]);
8192 sector_t end = a + BB_LEN(p[lo]);
8193
8194 if (a < s) {
8195 /* we need to split this range */
8196 if (bb->count >= MD_MAX_BADBLOCKS) {
8197 rv = 0;
8198 goto out;
8199 }
8200 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8201 bb->count++;
8202 p[lo] = BB_MAKE(a, s-a, ack);
8203 lo++;
8204 }
8205 p[lo] = BB_MAKE(target, end - target, ack);
8206 /* there is no longer an overlap */
8207 hi = lo;
8208 lo--;
8209 }
8210 while (lo >= 0 &&
8211 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8212 /* This range does overlap */
8213 if (BB_OFFSET(p[lo]) < s) {
8214 /* Keep the early parts of this range. */
8215 int ack = BB_ACK(p[lo]);
8216 sector_t start = BB_OFFSET(p[lo]);
8217 p[lo] = BB_MAKE(start, s - start, ack);
8218 /* now low doesn't overlap, so.. */
8219 break;
8220 }
8221 lo--;
8222 }
8223 /* 'lo' is strictly before, 'hi' is strictly after,
8224 * anything between needs to be discarded
8225 */
8226 if (hi - lo > 1) {
8227 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8228 bb->count -= (hi - lo - 1);
8229 }
8230 }
8231
8232 bb->changed = 1;
8233out:
8234 write_sequnlock_irq(&bb->lock);
8235 return rv;
8236}
8237
NeilBrownc6563a82012-05-21 09:27:00 +10008238int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8239 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008240{
NeilBrownc6563a82012-05-21 09:27:00 +10008241 if (is_new)
8242 s += rdev->new_data_offset;
8243 else
8244 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008245 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008246 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008247}
8248EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8249
8250/*
8251 * Acknowledge all bad blocks in a list.
8252 * This only succeeds if ->changed is clear. It is used by
8253 * in-kernel metadata updates
8254 */
8255void md_ack_all_badblocks(struct badblocks *bb)
8256{
8257 if (bb->page == NULL || bb->changed)
8258 /* no point even trying */
8259 return;
8260 write_seqlock_irq(&bb->lock);
8261
majianpengecb178b2012-03-19 12:46:42 +11008262 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008263 u64 *p = bb->page;
8264 int i;
8265 for (i = 0; i < bb->count ; i++) {
8266 if (!BB_ACK(p[i])) {
8267 sector_t start = BB_OFFSET(p[i]);
8268 int len = BB_LEN(p[i]);
8269 p[i] = BB_MAKE(start, len, 1);
8270 }
8271 }
NeilBrownde393cd2011-07-28 11:31:48 +10008272 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008273 }
8274 write_sequnlock_irq(&bb->lock);
8275}
8276EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8277
NeilBrown16c791a2011-07-28 11:31:47 +10008278/* sysfs access to bad-blocks list.
8279 * We present two files.
8280 * 'bad-blocks' lists sector numbers and lengths of ranges that
8281 * are recorded as bad. The list is truncated to fit within
8282 * the one-page limit of sysfs.
8283 * Writing "sector length" to this file adds an acknowledged
8284 * bad block list.
8285 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8286 * been acknowledged. Writing to this file adds bad blocks
8287 * without acknowledging them. This is largely for testing.
8288 */
8289
8290static ssize_t
8291badblocks_show(struct badblocks *bb, char *page, int unack)
8292{
8293 size_t len;
8294 int i;
8295 u64 *p = bb->page;
8296 unsigned seq;
8297
8298 if (bb->shift < 0)
8299 return 0;
8300
8301retry:
8302 seq = read_seqbegin(&bb->lock);
8303
8304 len = 0;
8305 i = 0;
8306
8307 while (len < PAGE_SIZE && i < bb->count) {
8308 sector_t s = BB_OFFSET(p[i]);
8309 unsigned int length = BB_LEN(p[i]);
8310 int ack = BB_ACK(p[i]);
8311 i++;
8312
8313 if (unack && ack)
8314 continue;
8315
8316 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8317 (unsigned long long)s << bb->shift,
8318 length << bb->shift);
8319 }
NeilBrownde393cd2011-07-28 11:31:48 +10008320 if (unack && len == 0)
8321 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008322
8323 if (read_seqretry(&bb->lock, seq))
8324 goto retry;
8325
8326 return len;
8327}
8328
8329#define DO_DEBUG 1
8330
8331static ssize_t
8332badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8333{
8334 unsigned long long sector;
8335 int length;
8336 char newline;
8337#ifdef DO_DEBUG
8338 /* Allow clearing via sysfs *only* for testing/debugging.
8339 * Normally only a successful write may clear a badblock
8340 */
8341 int clear = 0;
8342 if (page[0] == '-') {
8343 clear = 1;
8344 page++;
8345 }
8346#endif /* DO_DEBUG */
8347
8348 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8349 case 3:
8350 if (newline != '\n')
8351 return -EINVAL;
8352 case 2:
8353 if (length <= 0)
8354 return -EINVAL;
8355 break;
8356 default:
8357 return -EINVAL;
8358 }
8359
8360#ifdef DO_DEBUG
8361 if (clear) {
8362 md_clear_badblocks(bb, sector, length);
8363 return len;
8364 }
8365#endif /* DO_DEBUG */
8366 if (md_set_badblocks(bb, sector, length, !unack))
8367 return len;
8368 else
8369 return -ENOSPC;
8370}
8371
Adrian Bunk75c96f82005-05-05 16:16:09 -07008372static int md_notify_reboot(struct notifier_block *this,
8373 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374{
8375 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008376 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008377 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378
NeilBrownc744a652012-03-19 12:46:37 +11008379 for_each_mddev(mddev, tmp) {
8380 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008381 if (mddev->pers)
8382 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008383 mddev->safemode = 2;
8384 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008385 }
NeilBrownc744a652012-03-19 12:46:37 +11008386 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 }
NeilBrownc744a652012-03-19 12:46:37 +11008388 /*
8389 * certain more exotic SCSI devices are known to be
8390 * volatile wrt too early system reboots. While the
8391 * right place to handle this issue is the given
8392 * driver, we do want to have a safe RAID driver ...
8393 */
8394 if (need_delay)
8395 mdelay(1000*1);
8396
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 return NOTIFY_DONE;
8398}
8399
Adrian Bunk75c96f82005-05-05 16:16:09 -07008400static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008401 .notifier_call = md_notify_reboot,
8402 .next = NULL,
8403 .priority = INT_MAX, /* before any real devices */
8404};
8405
8406static void md_geninit(void)
8407{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008408 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008410 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411}
8412
Adrian Bunk75c96f82005-05-05 16:16:09 -07008413static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414{
Tejun Heoe804ac72010-10-15 15:36:08 +02008415 int ret = -ENOMEM;
8416
Tejun Heoada609e2011-01-25 14:35:54 +01008417 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008418 if (!md_wq)
8419 goto err_wq;
8420
8421 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8422 if (!md_misc_wq)
8423 goto err_misc_wq;
8424
8425 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8426 goto err_md;
8427
8428 if ((ret = register_blkdev(0, "mdp")) < 0)
8429 goto err_mdp;
8430 mdp_major = ret;
8431
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008432 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008433 md_probe, NULL, NULL);
8434 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435 md_probe, NULL, NULL);
8436
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008438 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439
8440 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442
Tejun Heoe804ac72010-10-15 15:36:08 +02008443err_mdp:
8444 unregister_blkdev(MD_MAJOR, "md");
8445err_md:
8446 destroy_workqueue(md_misc_wq);
8447err_misc_wq:
8448 destroy_workqueue(md_wq);
8449err_wq:
8450 return ret;
8451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452
8453#ifndef MODULE
8454
8455/*
8456 * Searches all registered partitions for autorun RAID arrays
8457 * at boot time.
8458 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008459
8460static LIST_HEAD(all_detected_devices);
8461struct detected_devices_node {
8462 struct list_head list;
8463 dev_t dev;
8464};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465
8466void md_autodetect_dev(dev_t dev)
8467{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008468 struct detected_devices_node *node_detected_dev;
8469
8470 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8471 if (node_detected_dev) {
8472 node_detected_dev->dev = dev;
8473 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8474 } else {
8475 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8476 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478}
8479
8480
8481static void autostart_arrays(int part)
8482{
NeilBrown3cb03002011-10-11 16:45:26 +11008483 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008484 struct detected_devices_node *node_detected_dev;
8485 dev_t dev;
8486 int i_scanned, i_passed;
8487
8488 i_scanned = 0;
8489 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
8491 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8492
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008493 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8494 i_scanned++;
8495 node_detected_dev = list_entry(all_detected_devices.next,
8496 struct detected_devices_node, list);
8497 list_del(&node_detected_dev->list);
8498 dev = node_detected_dev->dev;
8499 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008500 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 if (IS_ERR(rdev))
8502 continue;
8503
NeilBrownb2d444d2005-11-08 21:39:31 -08008504 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 MD_BUG();
8506 continue;
8507 }
NeilBrownd0fae182008-03-04 14:29:31 -08008508 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008510 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008511 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008512
8513 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8514 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515
8516 autorun_devices(part);
8517}
8518
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008519#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520
8521static __exit void md_exit(void)
8522{
NeilBrownfd01b882011-10-11 16:47:53 +11008523 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008525
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008526 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008527 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008529 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 unregister_blkdev(mdp_major, "mdp");
8531 unregister_reboot_notifier(&md_notifier);
8532 unregister_sysctl_table(raid_table_header);
8533 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008534 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008536 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008538 destroy_workqueue(md_misc_wq);
8539 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540}
8541
Dan Williams685784a2007-07-09 11:56:42 -07008542subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543module_exit(md_exit)
8544
NeilBrownf91de922005-11-08 21:39:36 -08008545static int get_ro(char *buffer, struct kernel_param *kp)
8546{
8547 return sprintf(buffer, "%d", start_readonly);
8548}
8549static int set_ro(const char *val, struct kernel_param *kp)
8550{
8551 char *e;
8552 int num = simple_strtoul(val, &e, 10);
8553 if (*val && (*e == '\0' || *e == '\n')) {
8554 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008555 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008556 }
8557 return -EINVAL;
8558}
8559
NeilBrown80ca3a42006-07-10 04:44:18 -07008560module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8561module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008562
NeilBrownefeb53c2009-01-09 08:31:10 +11008563module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008564
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565EXPORT_SYMBOL(register_md_personality);
8566EXPORT_SYMBOL(unregister_md_personality);
8567EXPORT_SYMBOL(md_error);
8568EXPORT_SYMBOL(md_done_sync);
8569EXPORT_SYMBOL(md_write_start);
8570EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571EXPORT_SYMBOL(md_register_thread);
8572EXPORT_SYMBOL(md_unregister_thread);
8573EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574EXPORT_SYMBOL(md_check_recovery);
8575MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008576MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008577MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008578MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);