blob: d557e557ff8f9f57be938764d32cb240f2852804 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Al Viroff01bb42011-09-16 02:31:11 -040039#include <linux/fs.h>
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040046#include <linux/module.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110047#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
158static void mddev_bio_destructor(struct bio *bio)
159{
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 mddevp = (void*)bio;
163 mddev = mddevp[-1];
164
165 bio_free(bio, mddev->bio_set);
166}
167
168struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100169 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100170{
171 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100172 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100173
174 if (!mddev || !mddev->bio_set)
175 return bio_alloc(gfp_mask, nr_iovecs);
176
177 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
178 mddev->bio_set);
179 if (!b)
180 return NULL;
181 mddevp = (void*)b;
182 mddevp[-1] = mddev;
183 b->bi_destructor = mddev_bio_destructor;
184 return b;
185}
186EXPORT_SYMBOL_GPL(bio_alloc_mddev);
187
188struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100189 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100190{
191 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100193
194 if (!mddev || !mddev->bio_set)
195 return bio_clone(bio, gfp_mask);
196
197 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
198 mddev->bio_set);
199 if (!b)
200 return NULL;
201 mddevp = (void*)b;
202 mddevp[-1] = mddev;
203 b->bi_destructor = mddev_bio_destructor;
204 __bio_clone(b, bio);
205 if (bio_integrity(bio)) {
206 int ret;
207
208 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
209
210 if (ret < 0) {
211 bio_put(b);
212 return NULL;
213 }
214 }
215
216 return b;
217}
218EXPORT_SYMBOL_GPL(bio_clone_mddev);
219
NeilBrownd2eb35a2011-07-28 11:31:48 +1000220void md_trim_bio(struct bio *bio, int offset, int size)
221{
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 */
226 int i;
227 struct bio_vec *bvec;
228 int sofar = 0;
229
230 size <<= 9;
231 if (offset == 0 && size == bio->bi_size)
232 return;
233
234 bio->bi_sector += offset;
235 bio->bi_size = size;
236 offset <<= 9;
237 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
238
239 while (bio->bi_idx < bio->bi_vcnt &&
240 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
241 /* remove this whole bio_vec */
242 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
243 bio->bi_idx++;
244 }
245 if (bio->bi_idx < bio->bi_vcnt) {
246 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
247 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
248 }
249 /* avoid any complications with bi_idx being non-zero*/
250 if (bio->bi_idx) {
251 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
252 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
253 bio->bi_vcnt -= bio->bi_idx;
254 bio->bi_idx = 0;
255 }
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec, bio, i) {
258 if (sofar + bvec->bv_len > size)
259 bvec->bv_len = size - sofar;
260 if (bvec->bv_len == 0) {
261 bio->bi_vcnt = i;
262 break;
263 }
264 sofar += bvec->bv_len;
265 }
266}
267EXPORT_SYMBOL_GPL(md_trim_bio);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
NeilBrownd7603b72006-01-06 00:20:30 -0800270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
273 * count increases.
274 *
275 * Events are:
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
278 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800279static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800280static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100281void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800282{
283 atomic_inc(&md_event_count);
284 wake_up(&md_event_waiters);
285}
NeilBrown29269552006-03-27 01:18:10 -0800286EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800287
NeilBrownc331eb02006-05-30 21:27:13 -0700288/* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
290 */
NeilBrownfd01b882011-10-11 16:47:53 +1100291static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700292{
293 atomic_inc(&md_event_count);
294 wake_up(&md_event_waiters);
295}
296
NeilBrownd7603b72006-01-06 00:20:30 -0800297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
300 */
301static LIST_HEAD(all_mddevs);
302static DEFINE_SPINLOCK(all_mddevs_lock);
303
304
305/*
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 \
314 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100315 _tmp = all_mddevs.next; \
316 _mddev = NULL;}); \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100324 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 )
326
327
NeilBrown409c57f2009-03-31 14:39:39 +1100328/* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200335static void md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
NeilBrown49077322010-03-25 16:20:56 +1100337 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100338 struct mddev *mddev = q->queuedata;
NeilBrown49077322010-03-25 16:20:56 +1100339 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500340 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100341
NeilBrown0ca69882011-01-14 09:14:33 +1100342 if (mddev == NULL || mddev->pers == NULL
343 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100344 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200345 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100346 }
NeilBrown0ca69882011-01-14 09:14:33 +1100347 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100348 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200349 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100350 DEFINE_WAIT(__wait);
351 for (;;) {
352 prepare_to_wait(&mddev->sb_wait, &__wait,
353 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200354 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100355 break;
356 rcu_read_unlock();
357 schedule();
358 rcu_read_lock();
359 }
360 finish_wait(&mddev->sb_wait, &__wait);
361 }
362 atomic_inc(&mddev->active_io);
363 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100364
Chris Masone91ece52011-02-07 19:21:48 -0500365 /*
366 * save the sectors now since our bio can
367 * go away inside make_request
368 */
369 sectors = bio_sectors(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200370 mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100371
372 cpu = part_stat_lock();
373 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500374 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100375 part_stat_unlock();
376
NeilBrown409c57f2009-03-31 14:39:39 +1100377 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
378 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
NeilBrown9e35b992010-04-06 14:23:02 +1000381/* mddev_suspend makes sure no new requests are submitted
382 * to the device, and that any requests that have been submitted
383 * are completely handled.
384 * Once ->stop is called and completes, the module will be completely
385 * unused.
386 */
NeilBrownfd01b882011-10-11 16:47:53 +1100387void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100388{
389 BUG_ON(mddev->suspended);
390 mddev->suspended = 1;
391 synchronize_rcu();
392 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
393 mddev->pers->quiesce(mddev, 1);
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500394
395 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100396}
NeilBrown390ee602010-06-01 19:37:27 +1000397EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100398
NeilBrownfd01b882011-10-11 16:47:53 +1100399void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100400{
401 mddev->suspended = 0;
402 wake_up(&mddev->sb_wait);
403 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500404
405 md_wakeup_thread(mddev->thread);
406 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100407}
NeilBrown390ee602010-06-01 19:37:27 +1000408EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100409
NeilBrownfd01b882011-10-11 16:47:53 +1100410int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000411{
412 return mddev->suspended;
413}
414EXPORT_SYMBOL(mddev_congested);
415
NeilBrowna2826aa2009-12-14 12:49:49 +1100416/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200417 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100418 */
419
Tejun Heoe9c74692010-09-03 11:56:18 +0200420static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100421{
NeilBrown3cb03002011-10-11 16:45:26 +1100422 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100423 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100424
425 rdev_dec_pending(rdev, mddev);
426
427 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200429 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 }
431 bio_put(bio);
432}
433
NeilBrowna7a07e62010-12-09 16:04:25 +1100434static void md_submit_flush_data(struct work_struct *ws);
435
NeilBrowna035fc32010-12-09 16:17:51 +1100436static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100437{
NeilBrownfd01b882011-10-11 16:47:53 +1100438 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100439 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100440
NeilBrowna7a07e62010-12-09 16:04:25 +1100441 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
442 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100443 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100444 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100445 if (rdev->raid_disk >= 0 &&
446 !test_bit(Faulty, &rdev->flags)) {
447 /* Take two references, one is dropped
448 * when request finishes, one after
449 * we reclaim rcu_read_lock
450 */
451 struct bio *bi;
452 atomic_inc(&rdev->nr_pending);
453 atomic_inc(&rdev->nr_pending);
454 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000455 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200456 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100457 bi->bi_private = rdev;
458 bi->bi_bdev = rdev->bdev;
459 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200460 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100461 rcu_read_lock();
462 rdev_dec_pending(rdev, mddev);
463 }
464 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100465 if (atomic_dec_and_test(&mddev->flush_pending))
466 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100467}
468
Tejun Heoe9c74692010-09-03 11:56:18 +0200469static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100470{
NeilBrownfd01b882011-10-11 16:47:53 +1100471 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200472 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100473
Tejun Heoe9c74692010-09-03 11:56:18 +0200474 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100475 /* an empty barrier - all done */
476 bio_endio(bio, 0);
477 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200478 bio->bi_rw &= ~REQ_FLUSH;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200479 mddev->pers->make_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100480 }
NeilBrown2b74e122010-12-09 15:59:01 +1100481
482 mddev->flush_bio = NULL;
483 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484}
485
NeilBrownfd01b882011-10-11 16:47:53 +1100486void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100487{
488 spin_lock_irq(&mddev->write_lock);
489 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200490 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100491 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200492 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100493 spin_unlock_irq(&mddev->write_lock);
494
NeilBrowna035fc32010-12-09 16:17:51 +1100495 INIT_WORK(&mddev->flush_work, submit_flushes);
496 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497}
Tejun Heoe9c74692010-09-03 11:56:18 +0200498EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100499
NeilBrown97658cd2011-04-18 18:25:42 +1000500/* Support for plugging.
501 * This mirrors the plugging support in request_queue, but does not
502 * require having a whole queue or request structures.
503 * We allocate an md_plug_cb for each md device and each thread it gets
504 * plugged on. This links tot the private plug_handle structure in the
505 * personality data where we keep a count of the number of outstanding
506 * plugs so other code can see if a plug is active.
507 */
508struct md_plug_cb {
509 struct blk_plug_cb cb;
NeilBrownfd01b882011-10-11 16:47:53 +1100510 struct mddev *mddev;
NeilBrown97658cd2011-04-18 18:25:42 +1000511};
512
513static void plugger_unplug(struct blk_plug_cb *cb)
514{
515 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
516 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
517 md_wakeup_thread(mdcb->mddev->thread);
518 kfree(mdcb);
519}
520
521/* Check that an unplug wakeup will come shortly.
522 * If not, wakeup the md thread immediately
523 */
NeilBrownfd01b882011-10-11 16:47:53 +1100524int mddev_check_plugged(struct mddev *mddev)
NeilBrown97658cd2011-04-18 18:25:42 +1000525{
526 struct blk_plug *plug = current->plug;
527 struct md_plug_cb *mdcb;
528
529 if (!plug)
530 return 0;
531
532 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
533 if (mdcb->cb.callback == plugger_unplug &&
534 mdcb->mddev == mddev) {
535 /* Already on the list, move to top */
536 if (mdcb != list_first_entry(&plug->cb_list,
537 struct md_plug_cb,
538 cb.list))
539 list_move(&mdcb->cb.list, &plug->cb_list);
540 return 1;
541 }
542 }
543 /* Not currently on the callback list */
544 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
545 if (!mdcb)
546 return 0;
547
548 mdcb->mddev = mddev;
549 mdcb->cb.callback = plugger_unplug;
550 atomic_inc(&mddev->plug_cnt);
551 list_add(&mdcb->cb.list, &plug->cb_list);
552 return 1;
553}
554EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000555
NeilBrownfd01b882011-10-11 16:47:53 +1100556static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 atomic_inc(&mddev->active);
559 return mddev;
560}
561
Dan Williams5fd3a172009-03-04 00:57:25 -0700562static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100563
NeilBrownfd01b882011-10-11 16:47:53 +1100564static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
NeilBrowna167f662010-10-26 18:31:13 +1100566 struct bio_set *bs = NULL;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
569 return;
NeilBrownd3374822009-01-09 08:31:10 +1100570 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100571 mddev->ctime == 0 && !mddev->hold_active) {
572 /* Array is not configured at all, and not held active,
573 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100574 list_del_init(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100575 bs = mddev->bio_set;
576 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100577 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200578 /* We did a probe so need to clean up. Call
579 * queue_work inside the spinlock so that
580 * flush_workqueue() after mddev_find will
581 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100582 */
583 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200584 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100585 } else
586 kfree(mddev);
587 }
588 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100589 if (bs)
590 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
NeilBrownfd01b882011-10-11 16:47:53 +1100593void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100594{
595 mutex_init(&mddev->open_mutex);
596 mutex_init(&mddev->reconfig_mutex);
597 mutex_init(&mddev->bitmap_info.mutex);
598 INIT_LIST_HEAD(&mddev->disks);
599 INIT_LIST_HEAD(&mddev->all_mddevs);
600 init_timer(&mddev->safemode_timer);
601 atomic_set(&mddev->active, 1);
602 atomic_set(&mddev->openers, 0);
603 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000604 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100605 spin_lock_init(&mddev->write_lock);
606 atomic_set(&mddev->flush_pending, 0);
607 init_waitqueue_head(&mddev->sb_wait);
608 init_waitqueue_head(&mddev->recovery_wait);
609 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000610 mddev->reshape_backwards = 0;
NeilBrownfafd7fb2010-04-01 15:55:30 +1100611 mddev->resync_min = 0;
612 mddev->resync_max = MaxSector;
613 mddev->level = LEVEL_NONE;
614}
NeilBrown390ee602010-06-01 19:37:27 +1000615EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100616
NeilBrownfd01b882011-10-11 16:47:53 +1100617static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
NeilBrownfd01b882011-10-11 16:47:53 +1100619 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
NeilBrown8f5f02c2011-02-16 13:58:51 +1100621 if (unit && MAJOR(unit) != MD_MAJOR)
622 unit &= ~((1<<MdpMinorShift)-1);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 retry:
625 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
NeilBrownefeb53c2009-01-09 08:31:10 +1100627 if (unit) {
628 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
629 if (mddev->unit == unit) {
630 mddev_get(mddev);
631 spin_unlock(&all_mddevs_lock);
632 kfree(new);
633 return mddev;
634 }
635
636 if (new) {
637 list_add(&new->all_mddevs, &all_mddevs);
638 spin_unlock(&all_mddevs_lock);
639 new->hold_active = UNTIL_IOCTL;
640 return new;
641 }
642 } else if (new) {
643 /* find an unused unit number */
644 static int next_minor = 512;
645 int start = next_minor;
646 int is_free = 0;
647 int dev = 0;
648 while (!is_free) {
649 dev = MKDEV(MD_MAJOR, next_minor);
650 next_minor++;
651 if (next_minor > MINORMASK)
652 next_minor = 0;
653 if (next_minor == start) {
654 /* Oh dear, all in use. */
655 spin_unlock(&all_mddevs_lock);
656 kfree(new);
657 return NULL;
658 }
659
660 is_free = 1;
661 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
662 if (mddev->unit == dev) {
663 is_free = 0;
664 break;
665 }
666 }
667 new->unit = dev;
668 new->md_minor = MINOR(dev);
669 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 list_add(&new->all_mddevs, &all_mddevs);
671 spin_unlock(&all_mddevs_lock);
672 return new;
673 }
674 spin_unlock(&all_mddevs_lock);
675
NeilBrown9ffae0c2006-01-06 00:20:32 -0800676 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!new)
678 return NULL;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 new->unit = unit;
681 if (MAJOR(unit) == MD_MAJOR)
682 new->md_minor = MINOR(unit);
683 else
684 new->md_minor = MINOR(unit) >> MdpMinorShift;
685
NeilBrownfafd7fb2010-04-01 15:55:30 +1100686 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto retry;
689}
690
NeilBrownfd01b882011-10-11 16:47:53 +1100691static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800693 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrownfd01b882011-10-11 16:47:53 +1100696static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100697{
698 return mutex_is_locked(&mddev->reconfig_mutex);
699}
700
NeilBrownfd01b882011-10-11 16:47:53 +1100701static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800703 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
NeilBrownb6eb1272010-04-15 10:13:47 +1000706static struct attribute_group md_redundancy_group;
707
NeilBrownfd01b882011-10-11 16:47:53 +1100708static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
NeilBrowna64c8762010-04-14 17:15:37 +1000710 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000711 /* These cannot be removed under reconfig_mutex as
712 * an access to the files will try to take reconfig_mutex
713 * while holding the file unremovable, which leads to
714 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000715 * So hold set sysfs_active while the remove in happeing,
716 * and anything else which might set ->to_remove or my
717 * otherwise change the sysfs namespace will fail with
718 * -EBUSY if sysfs_active is still set.
719 * We set sysfs_active under reconfig_mutex and elsewhere
720 * test it under the same mutex to ensure its correct value
721 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000722 */
NeilBrowna64c8762010-04-14 17:15:37 +1000723 struct attribute_group *to_remove = mddev->to_remove;
724 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000725 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000726 mutex_unlock(&mddev->reconfig_mutex);
727
NeilBrown00bcb4a2010-06-01 19:37:23 +1000728 if (mddev->kobj.sd) {
729 if (to_remove != &md_redundancy_group)
730 sysfs_remove_group(&mddev->kobj, to_remove);
731 if (mddev->pers == NULL ||
732 mddev->pers->sync_request == NULL) {
733 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
734 if (mddev->sysfs_action)
735 sysfs_put(mddev->sysfs_action);
736 mddev->sysfs_action = NULL;
737 }
NeilBrowna64c8762010-04-14 17:15:37 +1000738 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000739 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000740 } else
741 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Chris Dunlop751e67c2011-10-19 16:48:26 +1100743 /* As we've dropped the mutex we need a spinlock to
744 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000745 */
746 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700747 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000748 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
NeilBrownfd01b882011-10-11 16:47:53 +1100751static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
NeilBrown3cb03002011-10-11 16:45:26 +1100753 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrowndafb20f2012-03-19 12:46:39 +1100755 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (rdev->desc_nr == nr)
757 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return NULL;
760}
761
NeilBrownfd01b882011-10-11 16:47:53 +1100762static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
NeilBrown3cb03002011-10-11 16:45:26 +1100764 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
NeilBrowndafb20f2012-03-19 12:46:39 +1100766 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (rdev->bdev->bd_dev == dev)
768 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return NULL;
771}
772
NeilBrown84fc4b52011-10-11 16:49:58 +1100773static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800774{
NeilBrown84fc4b52011-10-11 16:49:58 +1100775 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800776 list_for_each_entry(pers, &pers_list, list) {
777 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800778 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800779 if (strcmp(pers->name, clevel)==0)
780 return pers;
781 }
NeilBrown2604b702006-01-06 00:20:36 -0800782 return NULL;
783}
784
Andre Nollb73df2d2008-07-11 22:02:23 +1000785/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100786static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100788 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000789 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
NeilBrown3cb03002011-10-11 16:45:26 +1100792static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 if (rdev->sb_page)
795 MD_BUG();
796
797 rdev->sb_page = alloc_page(GFP_KERNEL);
798 if (!rdev->sb_page) {
799 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000800 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
803 return 0;
804}
805
NeilBrown545c8792012-05-22 13:54:30 +1000806void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800809 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 rdev->sb_loaded = 0;
811 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000812 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100813 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
NeilBrown2699b672011-07-28 11:31:47 +1000815 if (rdev->bb_page) {
816 put_page(rdev->bb_page);
817 rdev->bb_page = NULL;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
NeilBrown545c8792012-05-22 13:54:30 +1000820EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
NeilBrown6712ecf2007-09-27 12:47:43 +0200822static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700823{
NeilBrown3cb03002011-10-11 16:45:26 +1100824 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100825 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700826
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700827 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
828 printk("md: super_written gets error=%d, uptodate=%d\n",
829 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
830 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800831 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700832 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700833
NeilBrowna9701a32005-11-08 21:39:34 -0800834 if (atomic_dec_and_test(&mddev->pending_writes))
835 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700836 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700837}
838
NeilBrownfd01b882011-10-11 16:47:53 +1100839void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700840 sector_t sector, int size, struct page *page)
841{
842 /* write first size bytes of page to sector of rdev
843 * Increment mddev->pending_writes before returning
844 * and decrement it on completion, waking up sb_wait
845 * if zero is reached.
846 * If an error occurred, call md_error
847 */
NeilBrowna167f662010-10-26 18:31:13 +1100848 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700849
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100850 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700851 bio->bi_sector = sector;
852 bio_add_page(bio, page, size, 0);
853 bio->bi_private = rdev;
854 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800855
NeilBrown7bfa19f2005-06-21 17:17:28 -0700856 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000857 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800858}
859
NeilBrownfd01b882011-10-11 16:47:53 +1100860void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800861{
Tejun Heoe9c74692010-09-03 11:56:18 +0200862 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800863 DEFINE_WAIT(wq);
864 for(;;) {
865 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
866 if (atomic_read(&mddev->pending_writes)==0)
867 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800868 schedule();
869 }
870 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700871}
872
NeilBrown6712ecf2007-09-27 12:47:43 +0200873static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
NeilBrown3cb03002011-10-11 16:45:26 +1100878int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100879 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
NeilBrowna167f662010-10-26 18:31:13 +1100881 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct completion event;
883 int ret;
884
Jens Axboe721a9602011-03-09 11:56:30 +0100885 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100887 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
888 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100889 if (metadata_op)
890 bio->bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000891 else if (rdev->mddev->reshape_position != MaxSector &&
892 (rdev->mddev->reshape_backwards ==
893 (sector >= rdev->mddev->reshape_position)))
894 bio->bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100895 else
896 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 bio_add_page(bio, page, size, 0);
898 init_completion(&event);
899 bio->bi_private = &event;
900 bio->bi_end_io = bi_complete;
901 submit_bio(rw, bio);
902 wait_for_completion(&event);
903
904 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
905 bio_put(bio);
906 return ret;
907}
NeilBrowna8745db2006-01-06 00:20:34 -0800908EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
NeilBrown3cb03002011-10-11 16:45:26 +1100910static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 char b[BDEVNAME_SIZE];
913 if (!rdev->sb_page) {
914 MD_BUG();
915 return -EINVAL;
916 }
917 if (rdev->sb_loaded)
918 return 0;
919
920
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100921 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 goto fail;
923 rdev->sb_loaded = 1;
924 return 0;
925
926fail:
927 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
928 bdevname(rdev->bdev,b));
929 return -EINVAL;
930}
931
932static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
933{
Andre Noll05710462008-07-11 22:02:20 +1000934 return sb1->set_uuid0 == sb2->set_uuid0 &&
935 sb1->set_uuid1 == sb2->set_uuid1 &&
936 sb1->set_uuid2 == sb2->set_uuid2 &&
937 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
941{
942 int ret;
943 mdp_super_t *tmp1, *tmp2;
944
945 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
946 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
947
948 if (!tmp1 || !tmp2) {
949 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100950 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 goto abort;
952 }
953
954 *tmp1 = *sb1;
955 *tmp2 = *sb2;
956
957 /*
958 * nr_disks is not constant
959 */
960 tmp1->nr_disks = 0;
961 tmp2->nr_disks = 0;
962
Andre Nollce0c8e02008-07-11 22:02:20 +1000963 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700965 kfree(tmp1);
966 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return ret;
968}
969
NeilBrown4d167f02007-05-09 02:35:37 -0700970
971static u32 md_csum_fold(u32 csum)
972{
973 csum = (csum & 0xffff) + (csum >> 16);
974 return (csum & 0xffff) + (csum >> 16);
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static unsigned int calc_sb_csum(mdp_super_t * sb)
978{
NeilBrown4d167f02007-05-09 02:35:37 -0700979 u64 newcsum = 0;
980 u32 *sb32 = (u32*)sb;
981 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned int disk_csum, csum;
983
984 disk_csum = sb->sb_csum;
985 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700986
987 for (i = 0; i < MD_SB_BYTES/4 ; i++)
988 newcsum += sb32[i];
989 csum = (newcsum & 0xffffffff) + (newcsum>>32);
990
991
992#ifdef CONFIG_ALPHA
993 /* This used to use csum_partial, which was wrong for several
994 * reasons including that different results are returned on
995 * different architectures. It isn't critical that we get exactly
996 * the same return value as before (we always csum_fold before
997 * testing, and that removes any differences). However as we
998 * know that csum_partial always returned a 16bit value on
999 * alphas, do a fold to maximise conformity to previous behaviour.
1000 */
1001 sb->sb_csum = md_csum_fold(disk_csum);
1002#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return csum;
1006}
1007
1008
1009/*
1010 * Handle superblock details.
1011 * We want to be able to handle multiple superblock formats
1012 * so we have a common interface to them all, and an array of
1013 * different handlers.
1014 * We rely on user-space to write the initial superblock, and support
1015 * reading and updating of superblocks.
1016 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001017 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * loads and validates a superblock on dev.
1019 * if refdev != NULL, compare superblocks on both devices
1020 * Return:
1021 * 0 - dev has a superblock that is compatible with refdev
1022 * 1 - dev has a superblock that is compatible and newer than refdev
1023 * so dev should be used as the refdev in future
1024 * -EINVAL superblock incompatible or invalid
1025 * -othererror e.g. -EIO
1026 *
NeilBrownfd01b882011-10-11 16:47:53 +11001027 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * Verify that dev is acceptable into mddev.
1029 * The first time, mddev->raid_disks will be 0, and data from
1030 * dev should be merged in. Subsequent calls check that dev
1031 * is new enough. Return 0 or -EINVAL
1032 *
NeilBrownfd01b882011-10-11 16:47:53 +11001033 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 * Update the superblock for rdev with data in mddev
1035 * This does not write to disc.
1036 *
1037 */
1038
1039struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001040 char *name;
1041 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001042 int (*load_super)(struct md_rdev *rdev,
1043 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001044 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001045 int (*validate_super)(struct mddev *mddev,
1046 struct md_rdev *rdev);
1047 void (*sync_super)(struct mddev *mddev,
1048 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001049 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001050 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001051 int (*allow_new_offset)(struct md_rdev *rdev,
1052 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053};
1054
1055/*
Andre Noll0894cc32009-06-18 08:49:23 +10001056 * Check that the given mddev has no bitmap.
1057 *
1058 * This function is called from the run method of all personalities that do not
1059 * support bitmaps. It prints an error message and returns non-zero if mddev
1060 * has a bitmap. Otherwise, it returns 0.
1061 *
1062 */
NeilBrownfd01b882011-10-11 16:47:53 +11001063int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001064{
NeilBrownc3d97142009-12-14 12:49:52 +11001065 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001066 return 0;
1067 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1068 mdname(mddev), mddev->pers->name);
1069 return 1;
1070}
1071EXPORT_SYMBOL(md_check_no_bitmap);
1072
1073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * load_super for 0.90.0
1075 */
NeilBrown3cb03002011-10-11 16:45:26 +11001076static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1079 mdp_super_t *sb;
1080 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
Andre Noll0f420352008-07-11 22:02:23 +10001083 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * it's at the end of the disk.
1085 *
1086 * It also happens to be a multiple of 4Kb.
1087 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001088 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
NeilBrown0002b272005-09-09 16:23:53 -07001090 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (ret) return ret;
1092
1093 ret = -EINVAL;
1094
1095 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001096 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (sb->md_magic != MD_SB_MAGIC) {
1099 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1100 b);
1101 goto abort;
1102 }
1103
1104 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001105 sb->minor_version < 90 ||
1106 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1108 sb->major_version, sb->minor_version,
1109 b);
1110 goto abort;
1111 }
1112
1113 if (sb->raid_disks <= 0)
1114 goto abort;
1115
NeilBrown4d167f02007-05-09 02:35:37 -07001116 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1118 b);
1119 goto abort;
1120 }
1121
1122 rdev->preferred_minor = sb->md_minor;
1123 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001124 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001125 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001126 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (sb->level == LEVEL_MULTIPATH)
1129 rdev->desc_nr = -1;
1130 else
1131 rdev->desc_nr = sb->this_disk.number;
1132
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001133 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001135 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001137 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!uuid_equal(refsb, sb)) {
1139 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1140 b, bdevname(refdev->bdev,b2));
1141 goto abort;
1142 }
1143 if (!sb_equal(refsb, sb)) {
1144 printk(KERN_WARNING "md: %s has same UUID"
1145 " but different superblock to %s\n",
1146 b, bdevname(refdev->bdev, b2));
1147 goto abort;
1148 }
1149 ev1 = md_event(sb);
1150 ev2 = md_event(refsb);
1151 if (ev1 > ev2)
1152 ret = 1;
1153 else
1154 ret = 0;
1155 }
NeilBrown8190e752009-06-18 08:48:58 +10001156 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001157 /* Limit to 4TB as metadata cannot record more than that */
1158 if (rdev->sectors >= (2ULL << 32))
1159 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
NeilBrown27a7b262011-09-10 17:21:28 +10001161 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001162 /* "this cannot possibly happen" ... */
1163 ret = -EINVAL;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 abort:
1166 return ret;
1167}
1168
1169/*
1170 * validate_super for 0.90.0
1171 */
NeilBrownfd01b882011-10-11 16:47:53 +11001172static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001175 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001176 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
NeilBrown41158c72005-06-21 17:17:25 -07001178 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001179 clear_bit(Faulty, &rdev->flags);
1180 clear_bit(In_sync, &rdev->flags);
1181 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (mddev->raid_disks == 0) {
1184 mddev->major_version = 0;
1185 mddev->minor_version = sb->minor_version;
1186 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001187 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001188 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 mddev->ctime = sb->ctime;
1190 mddev->utime = sb->utime;
1191 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001192 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 mddev->layout = sb->layout;
1194 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001195 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001196 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001197 mddev->bitmap_info.offset = 0;
1198 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001199 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
NeilBrownf6705572006-03-27 01:18:11 -08001201 if (mddev->minor_version >= 91) {
1202 mddev->reshape_position = sb->reshape_position;
1203 mddev->delta_disks = sb->delta_disks;
1204 mddev->new_level = sb->new_level;
1205 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001206 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001207 if (mddev->delta_disks < 0)
1208 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001209 } else {
1210 mddev->reshape_position = MaxSector;
1211 mddev->delta_disks = 0;
1212 mddev->new_level = mddev->level;
1213 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001214 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001215 }
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (sb->state & (1<<MD_SB_CLEAN))
1218 mddev->recovery_cp = MaxSector;
1219 else {
1220 if (sb->events_hi == sb->cp_events_hi &&
1221 sb->events_lo == sb->cp_events_lo) {
1222 mddev->recovery_cp = sb->recovery_cp;
1223 } else
1224 mddev->recovery_cp = 0;
1225 }
1226
1227 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1228 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1229 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1230 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1231
1232 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001233
1234 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001235 mddev->bitmap_info.file == NULL)
1236 mddev->bitmap_info.offset =
1237 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001238
NeilBrown41158c72005-06-21 17:17:25 -07001239 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001240 /* Insist on good event counter while assembling, except
1241 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001243 if (sb->disks[rdev->desc_nr].state & (
1244 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1245 if (ev1 < mddev->events)
1246 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001247 } else if (mddev->bitmap) {
1248 /* if adding to array with a bitmap, then we can accept an
1249 * older device ... but not too old.
1250 */
NeilBrown41158c72005-06-21 17:17:25 -07001251 if (ev1 < mddev->bitmap->events_cleared)
1252 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001253 } else {
1254 if (ev1 < mddev->events)
1255 /* just a hot-add of a new device, leave raid_disk at -1 */
1256 return 0;
1257 }
NeilBrown41158c72005-06-21 17:17:25 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 desc = sb->disks + rdev->desc_nr;
1261
1262 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001263 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001264 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1265 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001266 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001268 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1269 /* active but not in sync implies recovery up to
1270 * reshape position. We don't know exactly where
1271 * that is, so set to zero for now */
1272 if (mddev->minor_version >= 91) {
1273 rdev->recovery_offset = 0;
1274 rdev->raid_disk = desc->raid_disk;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001277 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1278 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001279 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001280 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282}
1283
1284/*
1285 * sync_super for 0.90.0
1286 */
NeilBrownfd01b882011-10-11 16:47:53 +11001287static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001290 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* make rdev->sb match mddev data..
1295 *
1296 * 1/ zero out disks
1297 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1298 * 3/ any empty disks < next_spare become removed
1299 *
1300 * disks[0] gets initialised to REMOVED because
1301 * we cannot be sure from other fields if it has
1302 * been initialised or not.
1303 */
1304 int i;
1305 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1306
NeilBrown61181562005-09-09 16:24:02 -07001307 rdev->sb_size = MD_SB_BYTES;
1308
Namhyung Kim65a06f062011-07-27 11:00:36 +10001309 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 memset(sb, 0, sizeof(*sb));
1312
1313 sb->md_magic = MD_SB_MAGIC;
1314 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sb->patch_version = mddev->patch_version;
1316 sb->gvalid_words = 0; /* ignored */
1317 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1318 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1319 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1320 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1321
1322 sb->ctime = mddev->ctime;
1323 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001324 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 sb->raid_disks = mddev->raid_disks;
1326 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001327 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 sb->utime = mddev->utime;
1329 sb->state = 0;
1330 sb->events_hi = (mddev->events>>32);
1331 sb->events_lo = (u32)mddev->events;
1332
NeilBrownf6705572006-03-27 01:18:11 -08001333 if (mddev->reshape_position == MaxSector)
1334 sb->minor_version = 90;
1335 else {
1336 sb->minor_version = 91;
1337 sb->reshape_position = mddev->reshape_position;
1338 sb->new_level = mddev->new_level;
1339 sb->delta_disks = mddev->delta_disks;
1340 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001341 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001342 }
1343 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (mddev->in_sync)
1345 {
1346 sb->recovery_cp = mddev->recovery_cp;
1347 sb->cp_events_hi = (mddev->events>>32);
1348 sb->cp_events_lo = (u32)mddev->events;
1349 if (mddev->recovery_cp == MaxSector)
1350 sb->state = (1<< MD_SB_CLEAN);
1351 } else
1352 sb->recovery_cp = 0;
1353
1354 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001355 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
NeilBrownc3d97142009-12-14 12:49:52 +11001357 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001358 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001361 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001363 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001364 int is_active = test_bit(In_sync, &rdev2->flags);
1365
1366 if (rdev2->raid_disk >= 0 &&
1367 sb->minor_version >= 91)
1368 /* we have nowhere to store the recovery_offset,
1369 * but if it is not below the reshape_position,
1370 * we can piggy-back on that.
1371 */
1372 is_active = 1;
1373 if (rdev2->raid_disk < 0 ||
1374 test_bit(Faulty, &rdev2->flags))
1375 is_active = 0;
1376 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001377 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001379 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001380 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 d = &sb->disks[rdev2->desc_nr];
1382 nr_disks++;
1383 d->number = rdev2->desc_nr;
1384 d->major = MAJOR(rdev2->bdev->bd_dev);
1385 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001386 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 d->raid_disk = rdev2->raid_disk;
1388 else
1389 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001390 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001392 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001394 if (test_bit(In_sync, &rdev2->flags))
1395 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 active++;
1397 working++;
1398 } else {
1399 d->state = 0;
1400 spare++;
1401 working++;
1402 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001403 if (test_bit(WriteMostly, &rdev2->flags))
1404 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* now set the "removed" and "faulty" bits on any missing devices */
1407 for (i=0 ; i < mddev->raid_disks ; i++) {
1408 mdp_disk_t *d = &sb->disks[i];
1409 if (d->state == 0 && d->number == 0) {
1410 d->number = i;
1411 d->raid_disk = i;
1412 d->state = (1<<MD_DISK_REMOVED);
1413 d->state |= (1<<MD_DISK_FAULTY);
1414 failed++;
1415 }
1416 }
1417 sb->nr_disks = nr_disks;
1418 sb->active_disks = active;
1419 sb->working_disks = working;
1420 sb->failed_disks = failed;
1421 sb->spare_disks = spare;
1422
1423 sb->this_disk = sb->disks[rdev->desc_nr];
1424 sb->sb_csum = calc_sb_csum(sb);
1425}
1426
1427/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001428 * rdev_size_change for 0.90.0
1429 */
1430static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001431super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001432{
Andre Noll58c0fed2009-03-31 14:33:13 +11001433 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001434 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001435 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001436 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001437 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001438 if (!num_sectors || num_sectors > rdev->sb_start)
1439 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001440 /* Limit to 4TB as metadata cannot record more than that.
1441 * 4TB == 2^32 KB, or 2*2^32 sectors.
1442 */
1443 if (num_sectors >= (2ULL << 32))
1444 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001445 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001446 rdev->sb_page);
1447 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001448 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001449}
1450
NeilBrownc6563a82012-05-21 09:27:00 +10001451static int
1452super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1453{
1454 /* non-zero offset changes not possible with v0.90 */
1455 return new_offset == 0;
1456}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001457
1458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * version 1 superblock
1460 */
1461
NeilBrown1c05b4b2006-10-21 10:24:08 -07001462static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001464 __le32 disk_csum;
1465 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 unsigned long long newcsum;
1467 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001468 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int i;
1470
1471 disk_csum = sb->sb_csum;
1472 sb->sb_csum = 0;
1473 newcsum = 0;
1474 for (i=0; size>=4; size -= 4 )
1475 newcsum += le32_to_cpu(*isuper++);
1476
1477 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001478 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1481 sb->sb_csum = disk_csum;
1482 return cpu_to_le32(csum);
1483}
1484
NeilBrown2699b672011-07-28 11:31:47 +10001485static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1486 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001487static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct mdp_superblock_1 *sb;
1490 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001491 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001492 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001494 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /*
Andre Noll0f420352008-07-11 22:02:23 +10001497 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 * It is always aligned to a 4K boundary and
1499 * depeding on minor_version, it can be:
1500 * 0: At least 8K, but less than 12K, from end of device
1501 * 1: At start of device
1502 * 2: 4K from start of device.
1503 */
1504 switch(minor_version) {
1505 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001506 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001507 sb_start -= 8*2;
1508 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001511 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 break;
1513 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001514 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
1516 default:
1517 return -EINVAL;
1518 }
Andre Noll0f420352008-07-11 22:02:23 +10001519 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
NeilBrown0002b272005-09-09 16:23:53 -07001521 /* superblock is rarely larger than 1K, but it can be larger,
1522 * and it is safe to read 4k, so we do that
1523 */
1524 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (ret) return ret;
1526
1527
Namhyung Kim65a06f062011-07-27 11:00:36 +10001528 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1531 sb->major_version != cpu_to_le32(1) ||
1532 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001533 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001534 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EINVAL;
1536
1537 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1538 printk("md: invalid superblock checksum on %s\n",
1539 bdevname(rdev->bdev,b));
1540 return -EINVAL;
1541 }
1542 if (le64_to_cpu(sb->data_size) < 10) {
1543 printk("md: data_size too small on %s\n",
1544 bdevname(rdev->bdev,b));
1545 return -EINVAL;
1546 }
NeilBrownc6563a82012-05-21 09:27:00 +10001547 if (sb->pad0 ||
1548 sb->pad3[0] ||
1549 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1550 /* Some padding is non-zero, might be a new feature */
1551 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 rdev->preferred_minor = 0xffff;
1554 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001555 rdev->new_data_offset = rdev->data_offset;
1556 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1557 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1558 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001559 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
NeilBrown0002b272005-09-09 16:23:53 -07001561 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001562 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001563 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001564 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1565
1566 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001567 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001568 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001569 if (minor_version
1570 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1571 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001572
NeilBrown31b65a02006-07-10 04:44:14 -07001573 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1574 rdev->desc_nr = -1;
1575 else
1576 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1577
NeilBrown2699b672011-07-28 11:31:47 +10001578 if (!rdev->bb_page) {
1579 rdev->bb_page = alloc_page(GFP_KERNEL);
1580 if (!rdev->bb_page)
1581 return -ENOMEM;
1582 }
1583 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1584 rdev->badblocks.count == 0) {
1585 /* need to load the bad block list.
1586 * Currently we limit it to one page.
1587 */
1588 s32 offset;
1589 sector_t bb_sector;
1590 u64 *bbp;
1591 int i;
1592 int sectors = le16_to_cpu(sb->bblog_size);
1593 if (sectors > (PAGE_SIZE / 512))
1594 return -EINVAL;
1595 offset = le32_to_cpu(sb->bblog_offset);
1596 if (offset == 0)
1597 return -EINVAL;
1598 bb_sector = (long long)offset;
1599 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1600 rdev->bb_page, READ, true))
1601 return -EIO;
1602 bbp = (u64 *)page_address(rdev->bb_page);
1603 rdev->badblocks.shift = sb->bblog_shift;
1604 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1605 u64 bb = le64_to_cpu(*bbp);
1606 int count = bb & (0x3ff);
1607 u64 sector = bb >> 10;
1608 sector <<= sb->bblog_shift;
1609 count <<= sb->bblog_shift;
1610 if (bb + 1 == 0)
1611 break;
1612 if (md_set_badblocks(&rdev->badblocks,
1613 sector, count, 1) == 0)
1614 return -EINVAL;
1615 }
1616 } else if (sb->bblog_offset == 0)
1617 rdev->badblocks.shift = -1;
1618
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001619 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001620 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001621 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001623 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1626 sb->level != refsb->level ||
1627 sb->layout != refsb->layout ||
1628 sb->chunksize != refsb->chunksize) {
1629 printk(KERN_WARNING "md: %s has strangely different"
1630 " superblock to %s\n",
1631 bdevname(rdev->bdev,b),
1632 bdevname(refdev->bdev,b2));
1633 return -EINVAL;
1634 }
1635 ev1 = le64_to_cpu(sb->events);
1636 ev2 = le64_to_cpu(refsb->events);
1637
1638 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001639 ret = 1;
1640 else
1641 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
NeilBrownc6563a82012-05-21 09:27:00 +10001643 if (minor_version) {
1644 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1645 sectors -= rdev->data_offset;
1646 } else
1647 sectors = rdev->sb_start;
1648 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001650 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
NeilBrownfd01b882011-10-11 16:47:53 +11001654static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001656 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001657 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
NeilBrown41158c72005-06-21 17:17:25 -07001659 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001660 clear_bit(Faulty, &rdev->flags);
1661 clear_bit(In_sync, &rdev->flags);
1662 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (mddev->raid_disks == 0) {
1665 mddev->major_version = 1;
1666 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001667 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001668 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1670 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1671 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001672 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 mddev->layout = le32_to_cpu(sb->layout);
1674 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001675 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001676 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001677 mddev->bitmap_info.offset = 0;
1678 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001679 mddev->reshape_backwards = 0;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1682 memcpy(mddev->uuid, sb->set_uuid, 16);
1683
1684 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001685
NeilBrown71c08052005-09-09 16:23:51 -07001686 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001687 mddev->bitmap_info.file == NULL )
1688 mddev->bitmap_info.offset =
1689 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001690
NeilBrownf6705572006-03-27 01:18:11 -08001691 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1692 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1693 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1694 mddev->new_level = le32_to_cpu(sb->new_level);
1695 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001696 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001697 if (mddev->delta_disks < 0 ||
1698 (mddev->delta_disks == 0 &&
1699 (le32_to_cpu(sb->feature_map)
1700 & MD_FEATURE_RESHAPE_BACKWARDS)))
1701 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001702 } else {
1703 mddev->reshape_position = MaxSector;
1704 mddev->delta_disks = 0;
1705 mddev->new_level = mddev->level;
1706 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001707 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001708 }
1709
NeilBrown41158c72005-06-21 17:17:25 -07001710 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001711 /* Insist of good event counter while assembling, except for
1712 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001714 if (rdev->desc_nr >= 0 &&
1715 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1716 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1717 if (ev1 < mddev->events)
1718 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001719 } else if (mddev->bitmap) {
1720 /* If adding to array with a bitmap, then we can accept an
1721 * older device, but not too old.
1722 */
NeilBrown41158c72005-06-21 17:17:25 -07001723 if (ev1 < mddev->bitmap->events_cleared)
1724 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001725 } else {
1726 if (ev1 < mddev->events)
1727 /* just a hot-add of a new device, leave raid_disk at -1 */
1728 return 0;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (mddev->level != LEVEL_MULTIPATH) {
1731 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001732 if (rdev->desc_nr < 0 ||
1733 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1734 role = 0xffff;
1735 rdev->desc_nr = -1;
1736 } else
1737 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 switch(role) {
1739 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001742 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 break;
1744 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001745 if ((le32_to_cpu(sb->feature_map) &
1746 MD_FEATURE_RECOVERY_OFFSET))
1747 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1748 else
1749 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 rdev->raid_disk = role;
1751 break;
1752 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001753 if (sb->devflags & WriteMostly1)
1754 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001755 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1756 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001757 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001758 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761}
1762
NeilBrownfd01b882011-10-11 16:47:53 +11001763static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001766 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int max_dev, i;
1768 /* make rdev->sb match mddev and rdev data. */
1769
Namhyung Kim65a06f062011-07-27 11:00:36 +10001770 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 sb->feature_map = 0;
1773 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001774 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 memset(sb->pad3, 0, sizeof(sb->pad3));
1776
1777 sb->utime = cpu_to_le64((__u64)mddev->utime);
1778 sb->events = cpu_to_le64(mddev->events);
1779 if (mddev->in_sync)
1780 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1781 else
1782 sb->resync_offset = cpu_to_le64(0);
1783
NeilBrown1c05b4b2006-10-21 10:24:08 -07001784 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001785
NeilBrownf0ca3402006-02-02 14:28:04 -08001786 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001787 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001788 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001789 sb->level = cpu_to_le32(mddev->level);
1790 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001791
NeilBrownaeb9b2112011-08-25 14:43:08 +10001792 if (test_bit(WriteMostly, &rdev->flags))
1793 sb->devflags |= WriteMostly1;
1794 else
1795 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001796 sb->data_offset = cpu_to_le64(rdev->data_offset);
1797 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001798
NeilBrownc3d97142009-12-14 12:49:52 +11001799 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1800 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001801 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001802 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001803
1804 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001805 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001806 sb->feature_map |=
1807 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1808 sb->recovery_offset =
1809 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001810 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001811 if (test_bit(Replacement, &rdev->flags))
1812 sb->feature_map |=
1813 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001814
NeilBrownf6705572006-03-27 01:18:11 -08001815 if (mddev->reshape_position != MaxSector) {
1816 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1817 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1818 sb->new_layout = cpu_to_le32(mddev->new_layout);
1819 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1820 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001821 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001822 if (mddev->delta_disks == 0 &&
1823 mddev->reshape_backwards)
1824 sb->feature_map
1825 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001826 if (rdev->new_data_offset != rdev->data_offset) {
1827 sb->feature_map
1828 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1829 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1830 - rdev->data_offset));
1831 }
NeilBrownf6705572006-03-27 01:18:11 -08001832 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001833
NeilBrown2699b672011-07-28 11:31:47 +10001834 if (rdev->badblocks.count == 0)
1835 /* Nothing to do for bad blocks*/ ;
1836 else if (sb->bblog_offset == 0)
1837 /* Cannot record bad blocks on this device */
1838 md_error(mddev, rdev);
1839 else {
1840 struct badblocks *bb = &rdev->badblocks;
1841 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1842 u64 *p = bb->page;
1843 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1844 if (bb->changed) {
1845 unsigned seq;
1846
1847retry:
1848 seq = read_seqbegin(&bb->lock);
1849
1850 memset(bbp, 0xff, PAGE_SIZE);
1851
1852 for (i = 0 ; i < bb->count ; i++) {
1853 u64 internal_bb = *p++;
1854 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1855 | BB_LEN(internal_bb));
1856 *bbp++ = cpu_to_le64(store_bb);
1857 }
NeilBrownd0962932012-03-19 12:46:41 +11001858 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001859 if (read_seqretry(&bb->lock, seq))
1860 goto retry;
1861
1862 bb->sector = (rdev->sb_start +
1863 (int)le32_to_cpu(sb->bblog_offset));
1864 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001865 }
1866 }
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001869 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (rdev2->desc_nr+1 > max_dev)
1871 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001872
NeilBrown70471da2009-08-03 10:59:57 +10001873 if (max_dev > le32_to_cpu(sb->max_dev)) {
1874 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001875 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001876 rdev->sb_size = max_dev * 2 + 256;
1877 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1878 if (rdev->sb_size & bmask)
1879 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001880 } else
1881 max_dev = le32_to_cpu(sb->max_dev);
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 for (i=0; i<max_dev;i++)
1884 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1885
NeilBrowndafb20f2012-03-19 12:46:39 +11001886 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001888 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001890 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001892 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001893 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 else
1895 sb->dev_roles[i] = cpu_to_le16(0xffff);
1896 }
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 sb->sb_csum = calc_sb_1_csum(sb);
1899}
1900
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001902super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001903{
1904 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001905 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001906 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001907 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10001908 if (rdev->data_offset != rdev->new_data_offset)
1909 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10001910 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001911 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001912 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001913 max_sectors -= rdev->data_offset;
1914 if (!num_sectors || num_sectors > max_sectors)
1915 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001916 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001917 /* minor version 0 with bitmap we can't move */
1918 return 0;
1919 } else {
1920 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001921 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001922 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001923 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001924 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001925 if (!num_sectors || num_sectors > max_sectors)
1926 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001927 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001928 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001929 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001930 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001931 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001932 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001933 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001934 rdev->sb_page);
1935 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001936 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10001937
1938}
1939
1940static int
1941super_1_allow_new_offset(struct md_rdev *rdev,
1942 unsigned long long new_offset)
1943{
1944 /* All necessary checks on new >= old have been done */
1945 struct bitmap *bitmap;
1946 if (new_offset >= rdev->data_offset)
1947 return 1;
1948
1949 /* with 1.0 metadata, there is no metadata to tread on
1950 * so we can always move back */
1951 if (rdev->mddev->minor_version == 0)
1952 return 1;
1953
1954 /* otherwise we must be sure not to step on
1955 * any metadata, so stay:
1956 * 36K beyond start of superblock
1957 * beyond end of badblocks
1958 * beyond write-intent bitmap
1959 */
1960 if (rdev->sb_start + (32+4)*2 > new_offset)
1961 return 0;
1962 bitmap = rdev->mddev->bitmap;
1963 if (bitmap && !rdev->mddev->bitmap_info.file &&
1964 rdev->sb_start + rdev->mddev->bitmap_info.offset +
1965 bitmap->file_pages * (PAGE_SIZE>>9) > new_offset)
1966 return 0;
1967 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1968 return 0;
1969
1970 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Adrian Bunk75c96f82005-05-05 16:16:09 -07001973static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 [0] = {
1975 .name = "0.90.0",
1976 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001977 .load_super = super_90_load,
1978 .validate_super = super_90_validate,
1979 .sync_super = super_90_sync,
1980 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001981 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 },
1983 [1] = {
1984 .name = "md-1",
1985 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001986 .load_super = super_1_load,
1987 .validate_super = super_1_validate,
1988 .sync_super = super_1_sync,
1989 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10001990 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 },
1992};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
NeilBrownfd01b882011-10-11 16:47:53 +11001994static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001995{
1996 if (mddev->sync_super) {
1997 mddev->sync_super(mddev, rdev);
1998 return;
1999 }
2000
2001 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2002
2003 super_types[mddev->major_version].sync_super(mddev, rdev);
2004}
2005
NeilBrownfd01b882011-10-11 16:47:53 +11002006static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
NeilBrown3cb03002011-10-11 16:45:26 +11002008 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
NeilBrown4b809912008-07-21 17:05:25 +10002010 rcu_read_lock();
2011 rdev_for_each_rcu(rdev, mddev1)
2012 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002013 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002014 rdev2->bdev->bd_contains) {
2015 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002016 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002017 }
2018 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020}
2021
2022static LIST_HEAD(pending_raid_disks);
2023
Andre Nollac5e7112009-08-03 10:59:47 +10002024/*
2025 * Try to register data integrity profile for an mddev
2026 *
2027 * This is called when an array is started and after a disk has been kicked
2028 * from the array. It only succeeds if all working and active component devices
2029 * are integrity capable with matching profiles.
2030 */
NeilBrownfd01b882011-10-11 16:47:53 +11002031int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002032{
NeilBrown3cb03002011-10-11 16:45:26 +11002033 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002034
Andre Nollac5e7112009-08-03 10:59:47 +10002035 if (list_empty(&mddev->disks))
2036 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002037 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2038 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002039 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002040 /* skip spares and non-functional disks */
2041 if (test_bit(Faulty, &rdev->flags))
2042 continue;
2043 if (rdev->raid_disk < 0)
2044 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002045 if (!reference) {
2046 /* Use the first rdev as the reference */
2047 reference = rdev;
2048 continue;
2049 }
2050 /* does this rdev's profile match the reference profile? */
2051 if (blk_integrity_compare(reference->bdev->bd_disk,
2052 rdev->bdev->bd_disk) < 0)
2053 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002054 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002055 if (!reference || !bdev_get_integrity(reference->bdev))
2056 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002057 /*
2058 * All component devices are integrity capable and have matching
2059 * profiles, register the common profile for the md device.
2060 */
2061 if (blk_integrity_register(mddev->gendisk,
2062 bdev_get_integrity(reference->bdev)) != 0) {
2063 printk(KERN_ERR "md: failed to register integrity for %s\n",
2064 mdname(mddev));
2065 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002066 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01002067 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2068 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2069 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2070 mdname(mddev));
2071 return -EINVAL;
2072 }
Andre Nollac5e7112009-08-03 10:59:47 +10002073 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002074}
Andre Nollac5e7112009-08-03 10:59:47 +10002075EXPORT_SYMBOL(md_integrity_register);
2076
2077/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11002078void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002079{
2080 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
2081 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
2082
2083 if (!bi_mddev) /* nothing to do */
2084 return;
2085 if (rdev->raid_disk < 0) /* skip spares */
2086 return;
2087 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2088 rdev->bdev->bd_disk) >= 0)
2089 return;
2090 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2091 blk_integrity_unregister(mddev->gendisk);
2092}
2093EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002094
NeilBrownfd01b882011-10-11 16:47:53 +11002095static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002097 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002098 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002099 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 if (rdev->mddev) {
2103 MD_BUG();
2104 return -EINVAL;
2105 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002106
2107 /* prevent duplicates */
2108 if (find_rdev(mddev, rdev->bdev->bd_dev))
2109 return -EEXIST;
2110
Andre Nolldd8ac332009-03-31 14:33:13 +11002111 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2112 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2113 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002114 if (mddev->pers) {
2115 /* Cannot change size, so fail
2116 * If mddev->level <= 0, then we don't care
2117 * about aligning sizes (e.g. linear)
2118 */
2119 if (mddev->level > 0)
2120 return -ENOSPC;
2121 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002122 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Verify rdev->desc_nr is unique.
2126 * If it is -1, assign a free number, else
2127 * check number is not in use
2128 */
2129 if (rdev->desc_nr < 0) {
2130 int choice = 0;
2131 if (mddev->pers) choice = mddev->raid_disks;
2132 while (find_rdev_nr(mddev, choice))
2133 choice++;
2134 rdev->desc_nr = choice;
2135 } else {
2136 if (find_rdev_nr(mddev, rdev->desc_nr))
2137 return -EBUSY;
2138 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002139 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2140 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2141 mdname(mddev), mddev->max_disks);
2142 return -EBUSY;
2143 }
NeilBrown19133a42005-11-08 21:39:35 -08002144 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002145 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002146 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002149 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002150
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002151 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002152 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002153
Tejun Heo0762b8b2008-08-25 19:56:12 +09002154 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002155 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2156 /* failure here is OK */;
2157 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002158
NeilBrown4b809912008-07-21 17:05:25 +10002159 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002160 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002161
2162 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002163 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002166
2167 fail:
2168 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2169 b, mdname(mddev));
2170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
NeilBrown177a99b2008-02-06 01:39:56 -08002173static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002174{
NeilBrown3cb03002011-10-11 16:45:26 +11002175 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002176 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002177 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002178}
2179
NeilBrown3cb03002011-10-11 16:45:26 +11002180static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 char b[BDEVNAME_SIZE];
2183 if (!rdev->mddev) {
2184 MD_BUG();
2185 return;
2186 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002187 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002188 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2190 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002191 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002192 sysfs_put(rdev->sysfs_state);
2193 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002194 kfree(rdev->badblocks.page);
2195 rdev->badblocks.count = 0;
2196 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002197 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002198 * writing to 'remove' to "dev/state". We also need
2199 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002200 */
NeilBrown4b809912008-07-21 17:05:25 +10002201 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002202 INIT_WORK(&rdev->del_work, md_delayed_delete);
2203 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002204 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207/*
2208 * prevent the device from being mounted, repartitioned or
2209 * otherwise reused by a RAID array (or any other kernel
2210 * subsystem), by bd_claiming the device.
2211 */
NeilBrown3cb03002011-10-11 16:45:26 +11002212static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int err = 0;
2215 struct block_device *bdev;
2216 char b[BDEVNAME_SIZE];
2217
Tejun Heod4d77622010-11-13 11:55:18 +01002218 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002219 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (IS_ERR(bdev)) {
2221 printk(KERN_ERR "md: could not open %s.\n",
2222 __bdevname(dev, b));
2223 return PTR_ERR(bdev);
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 rdev->bdev = bdev;
2226 return err;
2227}
2228
NeilBrown3cb03002011-10-11 16:45:26 +11002229static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct block_device *bdev = rdev->bdev;
2232 rdev->bdev = NULL;
2233 if (!bdev)
2234 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002235 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238void md_autodetect_dev(dev_t dev);
2239
NeilBrown3cb03002011-10-11 16:45:26 +11002240static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 char b[BDEVNAME_SIZE];
2243 printk(KERN_INFO "md: export_rdev(%s)\n",
2244 bdevname(rdev->bdev,b));
2245 if (rdev->mddev)
2246 MD_BUG();
NeilBrown545c8792012-05-22 13:54:30 +10002247 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002249 if (test_bit(AutoDetected, &rdev->flags))
2250 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251#endif
2252 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002253 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
NeilBrown3cb03002011-10-11 16:45:26 +11002256static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 unbind_rdev_from_array(rdev);
2259 export_rdev(rdev);
2260}
2261
NeilBrownfd01b882011-10-11 16:47:53 +11002262static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
NeilBrown3cb03002011-10-11 16:45:26 +11002264 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
NeilBrowndafb20f2012-03-19 12:46:39 +11002266 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!rdev->mddev) {
2268 MD_BUG();
2269 continue;
2270 }
2271 kick_rdev_from_array(rdev);
2272 }
2273 if (!list_empty(&mddev->disks))
2274 MD_BUG();
2275 mddev->raid_disks = 0;
2276 mddev->major_version = 0;
2277}
2278
2279static void print_desc(mdp_disk_t *desc)
2280{
2281 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2282 desc->major,desc->minor,desc->raid_disk,desc->state);
2283}
2284
Cheng Renquancd2ac932009-01-09 08:31:08 +11002285static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 int i;
2288
2289 printk(KERN_INFO
2290 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2291 sb->major_version, sb->minor_version, sb->patch_version,
2292 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2293 sb->ctime);
2294 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2295 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2296 sb->md_minor, sb->layout, sb->chunk_size);
2297 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2298 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2299 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2300 sb->failed_disks, sb->spare_disks,
2301 sb->sb_csum, (unsigned long)sb->events_lo);
2302
2303 printk(KERN_INFO);
2304 for (i = 0; i < MD_SB_DISKS; i++) {
2305 mdp_disk_t *desc;
2306
2307 desc = sb->disks + i;
2308 if (desc->number || desc->major || desc->minor ||
2309 desc->raid_disk || (desc->state && (desc->state != 4))) {
2310 printk(" D %2d: ", i);
2311 print_desc(desc);
2312 }
2313 }
2314 printk(KERN_INFO "md: THIS: ");
2315 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Cheng Renquancd2ac932009-01-09 08:31:08 +11002318static void print_sb_1(struct mdp_superblock_1 *sb)
2319{
2320 __u8 *uuid;
2321
2322 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002323 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002324 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002325 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002326 le32_to_cpu(sb->major_version),
2327 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002328 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002329 sb->set_name,
2330 (unsigned long long)le64_to_cpu(sb->ctime)
2331 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2332
2333 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002334 printk(KERN_INFO
2335 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002336 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002337 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002338 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2339 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002340 le32_to_cpu(sb->level),
2341 (unsigned long long)le64_to_cpu(sb->size),
2342 le32_to_cpu(sb->raid_disks),
2343 le32_to_cpu(sb->layout),
2344 le32_to_cpu(sb->chunksize),
2345 (unsigned long long)le64_to_cpu(sb->data_offset),
2346 (unsigned long long)le64_to_cpu(sb->data_size),
2347 (unsigned long long)le64_to_cpu(sb->super_offset),
2348 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2349 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002350 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002351 sb->devflags,
2352 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2353 (unsigned long long)le64_to_cpu(sb->events),
2354 (unsigned long long)le64_to_cpu(sb->resync_offset),
2355 le32_to_cpu(sb->sb_csum),
2356 le32_to_cpu(sb->max_dev)
2357 );
2358}
2359
NeilBrown3cb03002011-10-11 16:45:26 +11002360static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002363 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2364 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002365 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2366 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002368 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2369 switch (major_version) {
2370 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002371 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002372 break;
2373 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002374 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002375 break;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 } else
2378 printk(KERN_INFO "md: no rdev superblock!\n");
2379}
2380
Adrian Bunk5e563412006-06-26 00:27:42 -07002381static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002383 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002384 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002385 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 char b[BDEVNAME_SIZE];
2387
2388 printk("\n");
2389 printk("md: **********************************\n");
2390 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2391 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002392 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
NeilBrown32a76272005-06-21 17:17:14 -07002394 if (mddev->bitmap)
2395 bitmap_print_sb(mddev->bitmap);
2396 else
2397 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002398 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 printk("<%s>", bdevname(rdev->bdev,b));
2400 printk("\n");
2401
NeilBrowndafb20f2012-03-19 12:46:39 +11002402 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002403 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405 printk("md: **********************************\n");
2406 printk("\n");
2407}
2408
2409
NeilBrownfd01b882011-10-11 16:47:53 +11002410static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
NeilBrown42543762006-06-26 00:27:57 -07002412 /* Update each superblock (in-memory image), but
2413 * if we are allowed to, skip spares which already
2414 * have the right event counter, or have one earlier
2415 * (which would mean they aren't being marked as dirty
2416 * with the rest of the array)
2417 */
NeilBrown3cb03002011-10-11 16:45:26 +11002418 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002419 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002420 if (rdev->sb_events == mddev->events ||
2421 (nospares &&
2422 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002423 rdev->sb_events+1 == mddev->events)) {
2424 /* Don't update this superblock */
2425 rdev->sb_loaded = 2;
2426 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002427 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002428 rdev->sb_loaded = 1;
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431}
2432
NeilBrownfd01b882011-10-11 16:47:53 +11002433static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
NeilBrown3cb03002011-10-11 16:45:26 +11002435 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002436 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002437 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002438 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002441 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002442 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002443 if (rdev->raid_disk >= 0 &&
2444 mddev->delta_disks >= 0 &&
2445 !test_bit(In_sync, &rdev->flags) &&
2446 mddev->curr_resync_completed > rdev->recovery_offset)
2447 rdev->recovery_offset = mddev->curr_resync_completed;
2448
2449 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002450 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002451 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002452 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002453 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002454 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002455 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002456 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002457 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002458 md_ack_all_badblocks(&rdev->badblocks);
2459 md_error(mddev, rdev);
2460 }
2461 clear_bit(Blocked, &rdev->flags);
2462 clear_bit(BlockedBadBlocks, &rdev->flags);
2463 wake_up(&rdev->blocked_wait);
2464 }
2465 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002466 wake_up(&mddev->sb_wait);
2467 return;
2468 }
2469
NeilBrowna9701a32005-11-08 21:39:34 -08002470 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002471
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002472 mddev->utime = get_seconds();
2473
NeilBrown850b2b422006-10-03 01:15:46 -07002474 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2475 force_change = 1;
2476 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2477 /* just a clean<-> dirty transition, possibly leave spares alone,
2478 * though if events isn't the right even/odd, we will have to do
2479 * spares after all
2480 */
2481 nospares = 1;
2482 if (force_change)
2483 nospares = 0;
2484 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002485 /* If the array is degraded, then skipping spares is both
2486 * dangerous and fairly pointless.
2487 * Dangerous because a device that was removed from the array
2488 * might have a event_count that still looks up-to-date,
2489 * so it can be re-added without a resync.
2490 * Pointless because if there are any spares to skip,
2491 * then a recovery will happen and soon that array won't
2492 * be degraded any more and the spare can go back to sleep then.
2493 */
NeilBrown850b2b422006-10-03 01:15:46 -07002494 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002495
NeilBrown06d91a52005-06-21 17:17:12 -07002496 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002497
2498 /* If this is just a dirty<->clean transition, and the array is clean
2499 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002500 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002501 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002502 && mddev->can_decrease_events
2503 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002504 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002505 mddev->can_decrease_events = 0;
2506 } else {
NeilBrown42543762006-06-26 00:27:57 -07002507 /* otherwise we have to go forward and ... */
2508 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002509 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if (!mddev->events) {
2513 /*
2514 * oops, this 64-bit counter should never wrap.
2515 * Either we are in around ~1 trillion A.C., assuming
2516 * 1 reboot per second, or we have a bug:
2517 */
2518 MD_BUG();
2519 mddev->events --;
2520 }
NeilBrown2699b672011-07-28 11:31:47 +10002521
NeilBrowndafb20f2012-03-19 12:46:39 +11002522 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002523 if (rdev->badblocks.changed)
2524 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002525 if (test_bit(Faulty, &rdev->flags))
2526 set_bit(FaultRecorded, &rdev->flags);
2527 }
NeilBrown2699b672011-07-28 11:31:47 +10002528
NeilBrowne6910632008-02-06 01:39:51 -08002529 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002530 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
NeilBrown36a4e1f2011-10-07 14:23:17 +11002532 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2533 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
NeilBrown4ad13662007-07-17 04:06:13 -07002535 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002536 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002538
NeilBrown42543762006-06-26 00:27:57 -07002539 if (rdev->sb_loaded != 1)
2540 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002542 if (!test_bit(Faulty, &rdev->flags) &&
2543 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002544 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002545 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002546 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002547 pr_debug("md: (write) %s's sb offset: %llu\n",
2548 bdevname(rdev->bdev, b),
2549 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002550 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002551 if (rdev->badblocks.size) {
2552 md_super_write(mddev, rdev,
2553 rdev->badblocks.sector,
2554 rdev->badblocks.size << 9,
2555 rdev->bb_page);
2556 rdev->badblocks.size = 0;
2557 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002558
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002559 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002560 pr_debug("md: %s (skipping faulty)\n",
2561 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002562 else
2563 pr_debug("(skipping incremental s/r ");
2564
NeilBrown7bfa19f2005-06-21 17:17:28 -07002565 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /* only need to write one superblock... */
2567 break;
2568 }
NeilBrowna9701a32005-11-08 21:39:34 -08002569 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002570 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002571
NeilBrowna9701a32005-11-08 21:39:34 -08002572 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002573 if (mddev->in_sync != sync_req ||
2574 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002575 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002576 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002577 goto repeat;
2578 }
NeilBrown850b2b422006-10-03 01:15:46 -07002579 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002580 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002581 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002582 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2583 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002584
NeilBrowndafb20f2012-03-19 12:46:39 +11002585 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002586 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2587 clear_bit(Blocked, &rdev->flags);
2588
2589 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002590 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002591 clear_bit(BlockedBadBlocks, &rdev->flags);
2592 wake_up(&rdev->blocked_wait);
2593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Andre Noll7f6ce762008-03-23 18:34:54 +01002596/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002597 * We want to accept with case. For this we use cmd_match.
2598 */
2599static int cmd_match(const char *cmd, const char *str)
2600{
2601 /* See if cmd, written into a sysfs file, matches
2602 * str. They must either be the same, or cmd can
2603 * have a trailing newline
2604 */
2605 while (*cmd && *str && *cmd == *str) {
2606 cmd++;
2607 str++;
2608 }
2609 if (*cmd == '\n')
2610 cmd++;
2611 if (*str || *cmd)
2612 return 0;
2613 return 1;
2614}
2615
NeilBrown86e6ffd2005-11-08 21:39:24 -08002616struct rdev_sysfs_entry {
2617 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002618 ssize_t (*show)(struct md_rdev *, char *);
2619 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002620};
2621
2622static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002623state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002624{
2625 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002626 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002627
NeilBrownde393cd2011-07-28 11:31:48 +10002628 if (test_bit(Faulty, &rdev->flags) ||
2629 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002630 len+= sprintf(page+len, "%sfaulty",sep);
2631 sep = ",";
2632 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002633 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002634 len += sprintf(page+len, "%sin_sync",sep);
2635 sep = ",";
2636 }
NeilBrownf6556752006-06-26 00:28:01 -07002637 if (test_bit(WriteMostly, &rdev->flags)) {
2638 len += sprintf(page+len, "%swrite_mostly",sep);
2639 sep = ",";
2640 }
NeilBrownde393cd2011-07-28 11:31:48 +10002641 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002642 (rdev->badblocks.unacked_exist
2643 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002644 len += sprintf(page+len, "%sblocked", sep);
2645 sep = ",";
2646 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002647 if (!test_bit(Faulty, &rdev->flags) &&
2648 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002649 len += sprintf(page+len, "%sspare", sep);
2650 sep = ",";
2651 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002652 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2653 len += sprintf(page+len, "%swrite_error", sep);
2654 sep = ",";
2655 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002656 if (test_bit(WantReplacement, &rdev->flags)) {
2657 len += sprintf(page+len, "%swant_replacement", sep);
2658 sep = ",";
2659 }
2660 if (test_bit(Replacement, &rdev->flags)) {
2661 len += sprintf(page+len, "%sreplacement", sep);
2662 sep = ",";
2663 }
2664
NeilBrown86e6ffd2005-11-08 21:39:24 -08002665 return len+sprintf(page+len, "\n");
2666}
2667
NeilBrown45dc2de2006-06-26 00:27:58 -07002668static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002669state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002670{
2671 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002672 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002673 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002674 * writemostly - sets write_mostly
2675 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002676 * blocked - sets the Blocked flags
2677 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002678 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002679 * write_error - sets WriteErrorSeen
2680 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002681 */
2682 int err = -EINVAL;
2683 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2684 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002685 if (test_bit(Faulty, &rdev->flags))
2686 err = 0;
2687 else
2688 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002689 } else if (cmd_match(buf, "remove")) {
2690 if (rdev->raid_disk >= 0)
2691 err = -EBUSY;
2692 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002693 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002694 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002695 if (mddev->pers)
2696 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002697 md_new_event(mddev);
2698 err = 0;
2699 }
NeilBrownf6556752006-06-26 00:28:01 -07002700 } else if (cmd_match(buf, "writemostly")) {
2701 set_bit(WriteMostly, &rdev->flags);
2702 err = 0;
2703 } else if (cmd_match(buf, "-writemostly")) {
2704 clear_bit(WriteMostly, &rdev->flags);
2705 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002706 } else if (cmd_match(buf, "blocked")) {
2707 set_bit(Blocked, &rdev->flags);
2708 err = 0;
2709 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002710 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002711 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002712 /* metadata handler doesn't understand badblocks,
2713 * so we need to fail the device
2714 */
2715 md_error(rdev->mddev, rdev);
2716 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002717 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002718 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002719 wake_up(&rdev->blocked_wait);
2720 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2721 md_wakeup_thread(rdev->mddev->thread);
2722
2723 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002724 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2725 set_bit(In_sync, &rdev->flags);
2726 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002727 } else if (cmd_match(buf, "write_error")) {
2728 set_bit(WriteErrorSeen, &rdev->flags);
2729 err = 0;
2730 } else if (cmd_match(buf, "-write_error")) {
2731 clear_bit(WriteErrorSeen, &rdev->flags);
2732 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002733 } else if (cmd_match(buf, "want_replacement")) {
2734 /* Any non-spare device that is not a replacement can
2735 * become want_replacement at any time, but we then need to
2736 * check if recovery is needed.
2737 */
2738 if (rdev->raid_disk >= 0 &&
2739 !test_bit(Replacement, &rdev->flags))
2740 set_bit(WantReplacement, &rdev->flags);
2741 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2742 md_wakeup_thread(rdev->mddev->thread);
2743 err = 0;
2744 } else if (cmd_match(buf, "-want_replacement")) {
2745 /* Clearing 'want_replacement' is always allowed.
2746 * Once replacements starts it is too late though.
2747 */
2748 err = 0;
2749 clear_bit(WantReplacement, &rdev->flags);
2750 } else if (cmd_match(buf, "replacement")) {
2751 /* Can only set a device as a replacement when array has not
2752 * yet been started. Once running, replacement is automatic
2753 * from spares, or by assigning 'slot'.
2754 */
2755 if (rdev->mddev->pers)
2756 err = -EBUSY;
2757 else {
2758 set_bit(Replacement, &rdev->flags);
2759 err = 0;
2760 }
2761 } else if (cmd_match(buf, "-replacement")) {
2762 /* Similarly, can only clear Replacement before start */
2763 if (rdev->mddev->pers)
2764 err = -EBUSY;
2765 else {
2766 clear_bit(Replacement, &rdev->flags);
2767 err = 0;
2768 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002769 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002770 if (!err)
2771 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002772 return err ? err : len;
2773}
NeilBrown80ca3a42006-07-10 04:44:18 -07002774static struct rdev_sysfs_entry rdev_state =
2775__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002776
2777static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002778errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002779{
2780 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2781}
2782
2783static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002784errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002785{
2786 char *e;
2787 unsigned long n = simple_strtoul(buf, &e, 10);
2788 if (*buf && (*e == 0 || *e == '\n')) {
2789 atomic_set(&rdev->corrected_errors, n);
2790 return len;
2791 }
2792 return -EINVAL;
2793}
2794static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002795__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002796
NeilBrown014236d2006-01-06 00:20:55 -08002797static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002798slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002799{
2800 if (rdev->raid_disk < 0)
2801 return sprintf(page, "none\n");
2802 else
2803 return sprintf(page, "%d\n", rdev->raid_disk);
2804}
2805
2806static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002807slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002808{
2809 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002810 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002811 int slot = simple_strtoul(buf, &e, 10);
2812 if (strncmp(buf, "none", 4)==0)
2813 slot = -1;
2814 else if (e==buf || (*e && *e!= '\n'))
2815 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002816 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002817 /* Setting 'slot' on an active array requires also
2818 * updating the 'rd%d' link, and communicating
2819 * with the personality with ->hot_*_disk.
2820 * For now we only support removing
2821 * failed/spare devices. This normally happens automatically,
2822 * but not when the metadata is externally managed.
2823 */
NeilBrownc303da62008-02-06 01:39:51 -08002824 if (rdev->raid_disk == -1)
2825 return -EEXIST;
2826 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002827 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002828 return -EINVAL;
2829 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002830 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002831 if (err)
2832 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002833 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002834 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002835 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2836 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002837 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002838 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002839 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002840 */
2841
2842 if (rdev->raid_disk != -1)
2843 return -EBUSY;
2844
NeilBrownc6751b22011-02-02 11:57:13 +11002845 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2846 return -EBUSY;
2847
Neil Brown6c2fce22008-06-28 08:31:31 +10002848 if (rdev->mddev->pers->hot_add_disk == NULL)
2849 return -EINVAL;
2850
NeilBrownba1b41b2011-01-14 09:14:34 +11002851 if (slot >= rdev->mddev->raid_disks &&
2852 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2853 return -ENOSPC;
2854
Neil Brown6c2fce22008-06-28 08:31:31 +10002855 rdev->raid_disk = slot;
2856 if (test_bit(In_sync, &rdev->flags))
2857 rdev->saved_raid_disk = slot;
2858 else
2859 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002860 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002861 err = rdev->mddev->pers->
2862 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002863 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002864 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002865 return err;
Neil Brown52664732008-06-28 08:31:44 +10002866 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002867 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002868 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002869 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002870 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002871 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002872 if (slot >= rdev->mddev->raid_disks &&
2873 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002874 return -ENOSPC;
2875 rdev->raid_disk = slot;
2876 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002877 clear_bit(Faulty, &rdev->flags);
2878 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002879 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002880 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002881 }
NeilBrown014236d2006-01-06 00:20:55 -08002882 return len;
2883}
2884
2885
2886static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002887__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002888
NeilBrown93c8cad2006-01-06 00:20:56 -08002889static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002890offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002891{
Andrew Morton6961ece2006-01-06 00:20:59 -08002892 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002893}
2894
2895static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002896offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002897{
NeilBrownc6563a82012-05-21 09:27:00 +10002898 unsigned long long offset;
2899 if (strict_strtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002900 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002901 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002902 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002903 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002904 /* Must set offset before size, so overlap checks
2905 * can be sane */
2906 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002907 rdev->data_offset = offset;
2908 return len;
2909}
2910
2911static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002912__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002913
NeilBrownc6563a82012-05-21 09:27:00 +10002914static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2915{
2916 return sprintf(page, "%llu\n",
2917 (unsigned long long)rdev->new_data_offset);
2918}
2919
2920static ssize_t new_offset_store(struct md_rdev *rdev,
2921 const char *buf, size_t len)
2922{
2923 unsigned long long new_offset;
2924 struct mddev *mddev = rdev->mddev;
2925
2926 if (strict_strtoull(buf, 10, &new_offset) < 0)
2927 return -EINVAL;
2928
2929 if (mddev->sync_thread)
2930 return -EBUSY;
2931 if (new_offset == rdev->data_offset)
2932 /* reset is always permitted */
2933 ;
2934 else if (new_offset > rdev->data_offset) {
2935 /* must not push array size beyond rdev_sectors */
2936 if (new_offset - rdev->data_offset
2937 + mddev->dev_sectors > rdev->sectors)
2938 return -E2BIG;
2939 }
2940 /* Metadata worries about other space details. */
2941
2942 /* decreasing the offset is inconsistent with a backwards
2943 * reshape.
2944 */
2945 if (new_offset < rdev->data_offset &&
2946 mddev->reshape_backwards)
2947 return -EINVAL;
2948 /* Increasing offset is inconsistent with forwards
2949 * reshape. reshape_direction should be set to
2950 * 'backwards' first.
2951 */
2952 if (new_offset > rdev->data_offset &&
2953 !mddev->reshape_backwards)
2954 return -EINVAL;
2955
2956 if (mddev->pers && mddev->persistent &&
2957 !super_types[mddev->major_version]
2958 .allow_new_offset(rdev, new_offset))
2959 return -E2BIG;
2960 rdev->new_data_offset = new_offset;
2961 if (new_offset > rdev->data_offset)
2962 mddev->reshape_backwards = 1;
2963 else if (new_offset < rdev->data_offset)
2964 mddev->reshape_backwards = 0;
2965
2966 return len;
2967}
2968static struct rdev_sysfs_entry rdev_new_offset =
2969__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2970
NeilBrown83303b62006-01-06 00:21:06 -08002971static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002972rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002973{
Andre Nolldd8ac332009-03-31 14:33:13 +11002974 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002975}
2976
NeilBrownc5d79ad2008-02-06 01:39:54 -08002977static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2978{
2979 /* check if two start/length pairs overlap */
2980 if (s1+l1 <= s2)
2981 return 0;
2982 if (s2+l2 <= s1)
2983 return 0;
2984 return 1;
2985}
2986
Dan Williamsb522adc2009-03-31 15:00:31 +11002987static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2988{
2989 unsigned long long blocks;
2990 sector_t new;
2991
2992 if (strict_strtoull(buf, 10, &blocks) < 0)
2993 return -EINVAL;
2994
2995 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2996 return -EINVAL; /* sector conversion overflow */
2997
2998 new = blocks * 2;
2999 if (new != blocks * 2)
3000 return -EINVAL; /* unsigned long long to sector_t overflow */
3001
3002 *sectors = new;
3003 return 0;
3004}
3005
NeilBrown83303b62006-01-06 00:21:06 -08003006static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003007rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003008{
NeilBrownfd01b882011-10-11 16:47:53 +11003009 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003010 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003011 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003012
Dan Williamsb522adc2009-03-31 15:00:31 +11003013 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003014 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003015 if (rdev->data_offset != rdev->new_data_offset)
3016 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003017 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003018 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003019 sectors = super_types[my_mddev->major_version].
3020 rdev_size_change(rdev, sectors);
3021 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003022 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003023 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003024 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003025 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003026 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003027 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003028 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003029
Andre Nolldd8ac332009-03-31 14:33:13 +11003030 rdev->sectors = sectors;
3031 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003032 /* need to check that all other rdevs with the same ->bdev
3033 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11003034 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08003035 * we have to change it back, we will have the lock again.
3036 */
NeilBrownfd01b882011-10-11 16:47:53 +11003037 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003038 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003039 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003040
NeilBrown27c529b2008-03-04 14:29:33 -08003041 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08003042 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003043 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003044
3045 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11003046 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003047 if (rdev->bdev == rdev2->bdev &&
3048 rdev != rdev2 &&
3049 overlaps(rdev->data_offset, rdev->sectors,
3050 rdev2->data_offset,
3051 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003052 overlap = 1;
3053 break;
3054 }
3055 mddev_unlock(mddev);
3056 if (overlap) {
3057 mddev_put(mddev);
3058 break;
3059 }
3060 }
NeilBrown27c529b2008-03-04 14:29:33 -08003061 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08003062 if (overlap) {
3063 /* Someone else could have slipped in a size
3064 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003065 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003066 * safe, and trust userspace not to race with
3067 * itself
3068 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003069 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003070 return -EBUSY;
3071 }
3072 }
NeilBrown83303b62006-01-06 00:21:06 -08003073 return len;
3074}
3075
3076static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003077__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003078
Dan Williams06e3c812009-12-12 21:17:12 -07003079
NeilBrown3cb03002011-10-11 16:45:26 +11003080static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003081{
3082 unsigned long long recovery_start = rdev->recovery_offset;
3083
3084 if (test_bit(In_sync, &rdev->flags) ||
3085 recovery_start == MaxSector)
3086 return sprintf(page, "none\n");
3087
3088 return sprintf(page, "%llu\n", recovery_start);
3089}
3090
NeilBrown3cb03002011-10-11 16:45:26 +11003091static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003092{
3093 unsigned long long recovery_start;
3094
3095 if (cmd_match(buf, "none"))
3096 recovery_start = MaxSector;
3097 else if (strict_strtoull(buf, 10, &recovery_start))
3098 return -EINVAL;
3099
3100 if (rdev->mddev->pers &&
3101 rdev->raid_disk >= 0)
3102 return -EBUSY;
3103
3104 rdev->recovery_offset = recovery_start;
3105 if (recovery_start == MaxSector)
3106 set_bit(In_sync, &rdev->flags);
3107 else
3108 clear_bit(In_sync, &rdev->flags);
3109 return len;
3110}
3111
3112static struct rdev_sysfs_entry rdev_recovery_start =
3113__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3114
NeilBrown16c791a2011-07-28 11:31:47 +10003115
3116static ssize_t
3117badblocks_show(struct badblocks *bb, char *page, int unack);
3118static ssize_t
3119badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3120
NeilBrown3cb03002011-10-11 16:45:26 +11003121static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003122{
3123 return badblocks_show(&rdev->badblocks, page, 0);
3124}
NeilBrown3cb03002011-10-11 16:45:26 +11003125static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003126{
NeilBrownde393cd2011-07-28 11:31:48 +10003127 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3128 /* Maybe that ack was all we needed */
3129 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3130 wake_up(&rdev->blocked_wait);
3131 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003132}
3133static struct rdev_sysfs_entry rdev_bad_blocks =
3134__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3135
3136
NeilBrown3cb03002011-10-11 16:45:26 +11003137static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003138{
3139 return badblocks_show(&rdev->badblocks, page, 1);
3140}
NeilBrown3cb03002011-10-11 16:45:26 +11003141static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003142{
3143 return badblocks_store(&rdev->badblocks, page, len, 1);
3144}
3145static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3146__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3147
NeilBrown86e6ffd2005-11-08 21:39:24 -08003148static struct attribute *rdev_default_attrs[] = {
3149 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003150 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003151 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003152 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003153 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003154 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003155 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003156 &rdev_bad_blocks.attr,
3157 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003158 NULL,
3159};
3160static ssize_t
3161rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3162{
3163 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003164 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003165 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003166 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003167
3168 if (!entry->show)
3169 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003170
3171 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3172 if (!rv) {
3173 if (rdev->mddev == NULL)
3174 rv = -EBUSY;
3175 else
3176 rv = entry->show(rdev, page);
3177 mddev_unlock(mddev);
3178 }
3179 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003180}
3181
3182static ssize_t
3183rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3184 const char *page, size_t length)
3185{
3186 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003187 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003188 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003189 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003190
3191 if (!entry->store)
3192 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003193 if (!capable(CAP_SYS_ADMIN))
3194 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003195 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003196 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003197 if (rdev->mddev == NULL)
3198 rv = -EBUSY;
3199 else
3200 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003201 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003202 }
3203 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003204}
3205
3206static void rdev_free(struct kobject *ko)
3207{
NeilBrown3cb03002011-10-11 16:45:26 +11003208 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003209 kfree(rdev);
3210}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003211static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003212 .show = rdev_attr_show,
3213 .store = rdev_attr_store,
3214};
3215static struct kobj_type rdev_ktype = {
3216 .release = rdev_free,
3217 .sysfs_ops = &rdev_sysfs_ops,
3218 .default_attrs = rdev_default_attrs,
3219};
3220
NeilBrown3cb03002011-10-11 16:45:26 +11003221int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003222{
3223 rdev->desc_nr = -1;
3224 rdev->saved_raid_disk = -1;
3225 rdev->raid_disk = -1;
3226 rdev->flags = 0;
3227 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003228 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003229 rdev->sb_events = 0;
3230 rdev->last_read_error.tv_sec = 0;
3231 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003232 rdev->sb_loaded = 0;
3233 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003234 atomic_set(&rdev->nr_pending, 0);
3235 atomic_set(&rdev->read_errors, 0);
3236 atomic_set(&rdev->corrected_errors, 0);
3237
3238 INIT_LIST_HEAD(&rdev->same_set);
3239 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003240
3241 /* Add space to store bad block list.
3242 * This reserves the space even on arrays where it cannot
3243 * be used - I wonder if that matters
3244 */
3245 rdev->badblocks.count = 0;
3246 rdev->badblocks.shift = 0;
3247 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3248 seqlock_init(&rdev->badblocks.lock);
3249 if (rdev->badblocks.page == NULL)
3250 return -ENOMEM;
3251
3252 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003253}
3254EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255/*
3256 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3257 *
3258 * mark the device faulty if:
3259 *
3260 * - the device is nonexistent (zero size)
3261 * - the device has no valid superblock
3262 *
3263 * a faulty rdev _never_ has rdev->sb set.
3264 */
NeilBrown3cb03002011-10-11 16:45:26 +11003265static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 char b[BDEVNAME_SIZE];
3268 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003269 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 sector_t size;
3271
NeilBrown9ffae0c2006-01-06 00:20:32 -08003272 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (!rdev) {
3274 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3275 return ERR_PTR(-ENOMEM);
3276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
NeilBrown2230dfe2011-07-28 11:31:46 +10003278 err = md_rdev_init(rdev);
3279 if (err)
3280 goto abort_free;
3281 err = alloc_disk_sb(rdev);
3282 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 goto abort_free;
3284
NeilBrownc5d79ad2008-02-06 01:39:54 -08003285 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if (err)
3287 goto abort_free;
3288
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003289 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003290
Mike Snitzer77304d22010-11-08 14:39:12 +01003291 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (!size) {
3293 printk(KERN_WARNING
3294 "md: %s has zero or unknown size, marking faulty!\n",
3295 bdevname(rdev->bdev,b));
3296 err = -EINVAL;
3297 goto abort_free;
3298 }
3299
3300 if (super_format >= 0) {
3301 err = super_types[super_format].
3302 load_super(rdev, NULL, super_minor);
3303 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003304 printk(KERN_WARNING
3305 "md: %s does not have a valid v%d.%d "
3306 "superblock, not importing!\n",
3307 bdevname(rdev->bdev,b),
3308 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto abort_free;
3310 }
3311 if (err < 0) {
3312 printk(KERN_WARNING
3313 "md: could not read %s's sb, not importing!\n",
3314 bdevname(rdev->bdev,b));
3315 goto abort_free;
3316 }
3317 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003318 if (super_format == -1)
3319 /* hot-add for 0.90, or non-persistent: so no badblocks */
3320 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return rdev;
3323
3324abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003325 if (rdev->bdev)
3326 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003327 md_rdev_clear(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003328 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 kfree(rdev);
3330 return ERR_PTR(err);
3331}
3332
3333/*
3334 * Check a full RAID array for plausibility
3335 */
3336
3337
NeilBrownfd01b882011-10-11 16:47:53 +11003338static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003341 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 char b[BDEVNAME_SIZE];
3343
3344 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003345 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 switch (super_types[mddev->major_version].
3347 load_super(rdev, freshest, mddev->minor_version)) {
3348 case 1:
3349 freshest = rdev;
3350 break;
3351 case 0:
3352 break;
3353 default:
3354 printk( KERN_ERR \
3355 "md: fatal superblock inconsistency in %s"
3356 " -- removing from array\n",
3357 bdevname(rdev->bdev,b));
3358 kick_rdev_from_array(rdev);
3359 }
3360
3361
3362 super_types[mddev->major_version].
3363 validate_super(mddev, freshest);
3364
3365 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003366 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003367 if (mddev->max_disks &&
3368 (rdev->desc_nr >= mddev->max_disks ||
3369 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003370 printk(KERN_WARNING
3371 "md: %s: %s: only %d devices permitted\n",
3372 mdname(mddev), bdevname(rdev->bdev, b),
3373 mddev->max_disks);
3374 kick_rdev_from_array(rdev);
3375 continue;
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (rdev != freshest)
3378 if (super_types[mddev->major_version].
3379 validate_super(mddev, rdev)) {
3380 printk(KERN_WARNING "md: kicking non-fresh %s"
3381 " from array!\n",
3382 bdevname(rdev->bdev,b));
3383 kick_rdev_from_array(rdev);
3384 continue;
3385 }
3386 if (mddev->level == LEVEL_MULTIPATH) {
3387 rdev->desc_nr = i++;
3388 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003389 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003390 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003391 rdev->raid_disk = -1;
3392 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 }
3394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
NeilBrown72e02072009-12-14 12:49:55 +11003397/* Read a fixed-point number.
3398 * Numbers in sysfs attributes should be in "standard" units where
3399 * possible, so time should be in seconds.
3400 * However we internally use a a much smaller unit such as
3401 * milliseconds or jiffies.
3402 * This function takes a decimal number with a possible fractional
3403 * component, and produces an integer which is the result of
3404 * multiplying that number by 10^'scale'.
3405 * all without any floating-point arithmetic.
3406 */
3407int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3408{
3409 unsigned long result = 0;
3410 long decimals = -1;
3411 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3412 if (*cp == '.')
3413 decimals = 0;
3414 else if (decimals < scale) {
3415 unsigned int value;
3416 value = *cp - '0';
3417 result = result * 10 + value;
3418 if (decimals >= 0)
3419 decimals++;
3420 }
3421 cp++;
3422 }
3423 if (*cp == '\n')
3424 cp++;
3425 if (*cp)
3426 return -EINVAL;
3427 if (decimals < 0)
3428 decimals = 0;
3429 while (decimals < scale) {
3430 result *= 10;
3431 decimals ++;
3432 }
3433 *res = result;
3434 return 0;
3435}
3436
3437
NeilBrown19052c02008-08-05 15:54:13 +10003438static void md_safemode_timeout(unsigned long data);
3439
NeilBrowneae17012005-11-08 21:39:23 -08003440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003441safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003442{
3443 int msec = (mddev->safemode_delay*1000)/HZ;
3444 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3445}
3446static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003447safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003448{
NeilBrown16f17b32006-06-26 00:27:37 -07003449 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003450
NeilBrown72e02072009-12-14 12:49:55 +11003451 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003452 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003453 if (msec == 0)
3454 mddev->safemode_delay = 0;
3455 else {
NeilBrown19052c02008-08-05 15:54:13 +10003456 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003457 mddev->safemode_delay = (msec*HZ)/1000;
3458 if (mddev->safemode_delay == 0)
3459 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003460 if (mddev->safemode_delay < old_delay)
3461 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003462 }
3463 return len;
3464}
3465static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003466__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003467
3468static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003469level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003470{
NeilBrown84fc4b52011-10-11 16:49:58 +11003471 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003472 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003473 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003474 else if (mddev->clevel[0])
3475 return sprintf(page, "%s\n", mddev->clevel);
3476 else if (mddev->level != LEVEL_NONE)
3477 return sprintf(page, "%d\n", mddev->level);
3478 else
3479 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003480}
3481
NeilBrownd9d166c2006-01-06 00:20:51 -08003482static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003483level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003484{
Dan Williamsf2859af2010-05-02 10:04:16 -07003485 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003486 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003487 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003488 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003489 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003490 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003491
3492 if (mddev->pers == NULL) {
3493 if (len == 0)
3494 return 0;
3495 if (len >= sizeof(mddev->clevel))
3496 return -ENOSPC;
3497 strncpy(mddev->clevel, buf, len);
3498 if (mddev->clevel[len-1] == '\n')
3499 len--;
3500 mddev->clevel[len] = 0;
3501 mddev->level = LEVEL_NONE;
3502 return rv;
3503 }
3504
3505 /* request to change the personality. Need to ensure:
3506 * - array is not engaged in resync/recovery/reshape
3507 * - old personality can be suspended
3508 * - new personality will access other array.
3509 */
3510
NeilBrownbb4f1e92010-08-08 21:18:03 +10003511 if (mddev->sync_thread ||
3512 mddev->reshape_position != MaxSector ||
3513 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003514 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003515
3516 if (!mddev->pers->quiesce) {
3517 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3518 mdname(mddev), mddev->pers->name);
3519 return -EINVAL;
3520 }
3521
3522 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003523 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003524 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003525 strncpy(clevel, buf, len);
3526 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003527 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003528 clevel[len] = 0;
3529 if (strict_strtol(clevel, 10, &level))
3530 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003531
Dan Williamsf2859af2010-05-02 10:04:16 -07003532 if (request_module("md-%s", clevel) != 0)
3533 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003534 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003535 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003536 if (!pers || !try_module_get(pers->owner)) {
3537 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003538 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003539 return -EINVAL;
3540 }
3541 spin_unlock(&pers_lock);
3542
3543 if (pers == mddev->pers) {
3544 /* Nothing to do! */
3545 module_put(pers->owner);
3546 return rv;
3547 }
3548 if (!pers->takeover) {
3549 module_put(pers->owner);
3550 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003551 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003552 return -EINVAL;
3553 }
3554
NeilBrowndafb20f2012-03-19 12:46:39 +11003555 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003556 rdev->new_raid_disk = rdev->raid_disk;
3557
NeilBrown245f46c2009-03-31 14:39:39 +11003558 /* ->takeover must set new_* and/or delta_disks
3559 * if it succeeds, and may set them when it fails.
3560 */
3561 priv = pers->takeover(mddev);
3562 if (IS_ERR(priv)) {
3563 mddev->new_level = mddev->level;
3564 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003565 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003566 mddev->raid_disks -= mddev->delta_disks;
3567 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003568 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003569 module_put(pers->owner);
3570 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003571 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003572 return PTR_ERR(priv);
3573 }
3574
3575 /* Looks like we have a winner */
3576 mddev_suspend(mddev);
3577 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003578
3579 if (mddev->pers->sync_request == NULL &&
3580 pers->sync_request != NULL) {
3581 /* need to add the md_redundancy_group */
3582 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3583 printk(KERN_WARNING
3584 "md: cannot register extra attributes for %s\n",
3585 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003586 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003587 }
3588 if (mddev->pers->sync_request != NULL &&
3589 pers->sync_request == NULL) {
3590 /* need to remove the md_redundancy_group */
3591 if (mddev->to_remove == NULL)
3592 mddev->to_remove = &md_redundancy_group;
3593 }
3594
Trela Maciej54071b32010-03-08 16:02:42 +11003595 if (mddev->pers->sync_request == NULL &&
3596 mddev->external) {
3597 /* We are converting from a no-redundancy array
3598 * to a redundancy array and metadata is managed
3599 * externally so we need to be sure that writes
3600 * won't block due to a need to transition
3601 * clean->dirty
3602 * until external management is started.
3603 */
3604 mddev->in_sync = 0;
3605 mddev->safemode_delay = 0;
3606 mddev->safemode = 0;
3607 }
3608
NeilBrowndafb20f2012-03-19 12:46:39 +11003609 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003610 if (rdev->raid_disk < 0)
3611 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003612 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003613 rdev->new_raid_disk = -1;
3614 if (rdev->new_raid_disk == rdev->raid_disk)
3615 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003616 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003617 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003618 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003619 if (rdev->raid_disk < 0)
3620 continue;
3621 if (rdev->new_raid_disk == rdev->raid_disk)
3622 continue;
3623 rdev->raid_disk = rdev->new_raid_disk;
3624 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003625 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003626 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003627 if (sysfs_link_rdev(mddev, rdev))
3628 printk(KERN_WARNING "md: cannot register rd%d"
3629 " for %s after level change\n",
3630 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003631 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003632 }
3633
3634 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003635 mddev->pers = pers;
3636 mddev->private = priv;
3637 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3638 mddev->level = mddev->new_level;
3639 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003640 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003641 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003642 mddev->reshape_backwards = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003643 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003644 if (mddev->pers->sync_request == NULL) {
3645 /* this is now an array without redundancy, so
3646 * it must always be in_sync
3647 */
3648 mddev->in_sync = 1;
3649 del_timer_sync(&mddev->safemode_timer);
3650 }
NeilBrown245f46c2009-03-31 14:39:39 +11003651 pers->run(mddev);
3652 mddev_resume(mddev);
3653 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3654 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3655 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003656 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003657 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003658 return rv;
3659}
3660
3661static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003662__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003663
NeilBrownd4dbd022006-06-26 00:27:59 -07003664
3665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003666layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003667{
3668 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003669 if (mddev->reshape_position != MaxSector &&
3670 mddev->layout != mddev->new_layout)
3671 return sprintf(page, "%d (%d)\n",
3672 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003673 return sprintf(page, "%d\n", mddev->layout);
3674}
3675
3676static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003677layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003678{
3679 char *e;
3680 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003681
3682 if (!*buf || (*e && *e != '\n'))
3683 return -EINVAL;
3684
NeilBrownb3546032009-03-31 14:56:41 +11003685 if (mddev->pers) {
3686 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003687 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003688 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003689 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003690 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003691 if (err) {
3692 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003693 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003694 }
NeilBrownb3546032009-03-31 14:56:41 +11003695 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003696 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003697 if (mddev->reshape_position == MaxSector)
3698 mddev->layout = n;
3699 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003700 return len;
3701}
3702static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003703__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003704
3705
NeilBrowneae17012005-11-08 21:39:23 -08003706static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003707raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003708{
NeilBrownbb636542005-11-08 21:39:45 -08003709 if (mddev->raid_disks == 0)
3710 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003711 if (mddev->reshape_position != MaxSector &&
3712 mddev->delta_disks != 0)
3713 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3714 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003715 return sprintf(page, "%d\n", mddev->raid_disks);
3716}
3717
NeilBrownfd01b882011-10-11 16:47:53 +11003718static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003719
3720static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003721raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003722{
NeilBrownda943b992006-01-06 00:20:54 -08003723 char *e;
3724 int rv = 0;
3725 unsigned long n = simple_strtoul(buf, &e, 10);
3726
3727 if (!*buf || (*e && *e != '\n'))
3728 return -EINVAL;
3729
3730 if (mddev->pers)
3731 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003732 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10003733 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07003734 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10003735
3736 rdev_for_each(rdev, mddev) {
3737 if (olddisks < n &&
3738 rdev->data_offset < rdev->new_data_offset)
3739 return -EINVAL;
3740 if (olddisks > n &&
3741 rdev->data_offset > rdev->new_data_offset)
3742 return -EINVAL;
3743 }
NeilBrown08a02ec2007-05-09 02:35:38 -07003744 mddev->delta_disks = n - olddisks;
3745 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10003746 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07003747 } else
NeilBrownda943b992006-01-06 00:20:54 -08003748 mddev->raid_disks = n;
3749 return rv ? rv : len;
3750}
3751static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003752__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003753
NeilBrown24dd4692005-11-08 21:39:26 -08003754static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003755chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003756{
NeilBrown08a02ec2007-05-09 02:35:38 -07003757 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003758 mddev->chunk_sectors != mddev->new_chunk_sectors)
3759 return sprintf(page, "%d (%d)\n",
3760 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003761 mddev->chunk_sectors << 9);
3762 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003763}
3764
3765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003766chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003767{
NeilBrown3b343802006-01-06 00:20:47 -08003768 char *e;
3769 unsigned long n = simple_strtoul(buf, &e, 10);
3770
NeilBrown3b343802006-01-06 00:20:47 -08003771 if (!*buf || (*e && *e != '\n'))
3772 return -EINVAL;
3773
NeilBrownb3546032009-03-31 14:56:41 +11003774 if (mddev->pers) {
3775 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003776 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003777 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003778 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003779 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003780 if (err) {
3781 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003782 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003783 }
NeilBrownb3546032009-03-31 14:56:41 +11003784 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003785 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003786 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003787 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003788 }
NeilBrown3b343802006-01-06 00:20:47 -08003789 return len;
3790}
3791static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003792__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003793
NeilBrowna94213b2006-06-26 00:28:00 -07003794static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003795resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003796{
NeilBrownd1a7c502009-03-31 15:24:32 +11003797 if (mddev->recovery_cp == MaxSector)
3798 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003799 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3800}
3801
3802static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003803resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003804{
NeilBrowna94213b2006-06-26 00:28:00 -07003805 char *e;
3806 unsigned long long n = simple_strtoull(buf, &e, 10);
3807
NeilBrownb0986362011-05-11 15:52:21 +10003808 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003809 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003810 if (cmd_match(buf, "none"))
3811 n = MaxSector;
3812 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003813 return -EINVAL;
3814
3815 mddev->recovery_cp = n;
3816 return len;
3817}
3818static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003819__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003820
NeilBrown9e653b62006-06-26 00:27:58 -07003821/*
3822 * The array state can be:
3823 *
3824 * clear
3825 * No devices, no size, no level
3826 * Equivalent to STOP_ARRAY ioctl
3827 * inactive
3828 * May have some settings, but array is not active
3829 * all IO results in error
3830 * When written, doesn't tear down array, but just stops it
3831 * suspended (not supported yet)
3832 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003833 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003834 * readonly
3835 * no resync can happen. no superblocks get written.
3836 * write requests fail
3837 * read-auto
3838 * like readonly, but behaves like 'clean' on a write request.
3839 *
3840 * clean - no pending writes, but otherwise active.
3841 * When written to inactive array, starts without resync
3842 * If a write request arrives then
3843 * if metadata is known, mark 'dirty' and switch to 'active'.
3844 * if not known, block and switch to write-pending
3845 * If written to an active array that has pending writes, then fails.
3846 * active
3847 * fully active: IO and resync can be happening.
3848 * When written to inactive array, starts with resync
3849 *
3850 * write-pending
3851 * clean, but writes are blocked waiting for 'active' to be written.
3852 *
3853 * active-idle
3854 * like active, but no writes have been seen for a while (100msec).
3855 *
3856 */
3857enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3858 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003859static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003860 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3861 "write-pending", "active-idle", NULL };
3862
3863static int match_word(const char *word, char **list)
3864{
3865 int n;
3866 for (n=0; list[n]; n++)
3867 if (cmd_match(word, list[n]))
3868 break;
3869 return n;
3870}
3871
3872static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003873array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003874{
3875 enum array_state st = inactive;
3876
3877 if (mddev->pers)
3878 switch(mddev->ro) {
3879 case 1:
3880 st = readonly;
3881 break;
3882 case 2:
3883 st = read_auto;
3884 break;
3885 case 0:
3886 if (mddev->in_sync)
3887 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003888 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003889 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003890 else if (mddev->safemode)
3891 st = active_idle;
3892 else
3893 st = active;
3894 }
3895 else {
3896 if (list_empty(&mddev->disks) &&
3897 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003898 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003899 st = clear;
3900 else
3901 st = inactive;
3902 }
3903 return sprintf(page, "%s\n", array_states[st]);
3904}
3905
NeilBrownfd01b882011-10-11 16:47:53 +11003906static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3907static int md_set_readonly(struct mddev * mddev, int is_open);
3908static int do_md_run(struct mddev * mddev);
3909static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003910
3911static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003912array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003913{
3914 int err = -EINVAL;
3915 enum array_state st = match_word(buf, array_states);
3916 switch(st) {
3917 case bad_word:
3918 break;
3919 case clear:
3920 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003921 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003922 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003923 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003924 break;
3925 case inactive:
3926 /* stopping an active array */
3927 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003928 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003929 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003930 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003931 } else
3932 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003933 break;
3934 case suspended:
3935 break; /* not supported yet */
3936 case readonly:
3937 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003938 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003939 else {
3940 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003941 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003942 err = do_md_run(mddev);
3943 }
3944 break;
3945 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003946 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003947 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003948 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003949 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003950 err = restart_array(mddev);
3951 if (err == 0) {
3952 mddev->ro = 2;
3953 set_disk_ro(mddev->gendisk, 0);
3954 }
NeilBrown9e653b62006-06-26 00:27:58 -07003955 } else {
3956 mddev->ro = 2;
3957 err = do_md_run(mddev);
3958 }
3959 break;
3960 case clean:
3961 if (mddev->pers) {
3962 restart_array(mddev);
3963 spin_lock_irq(&mddev->write_lock);
3964 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003965 if (mddev->in_sync == 0) {
3966 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003967 if (mddev->safemode == 1)
3968 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003969 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003970 }
3971 err = 0;
3972 } else
3973 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003974 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003975 } else
3976 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003977 break;
3978 case active:
3979 if (mddev->pers) {
3980 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003981 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003982 wake_up(&mddev->sb_wait);
3983 err = 0;
3984 } else {
3985 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003986 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003987 err = do_md_run(mddev);
3988 }
3989 break;
3990 case write_pending:
3991 case active_idle:
3992 /* these cannot be set */
3993 break;
3994 }
3995 if (err)
3996 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003997 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003998 if (mddev->hold_active == UNTIL_IOCTL)
3999 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004000 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07004001 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10004002 }
NeilBrown9e653b62006-06-26 00:27:58 -07004003}
NeilBrown80ca3a42006-07-10 04:44:18 -07004004static struct md_sysfs_entry md_array_state =
4005__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004006
NeilBrown6d7ff7382006-01-06 00:21:16 -08004007static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004008max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004009 return sprintf(page, "%d\n",
4010 atomic_read(&mddev->max_corr_read_errors));
4011}
4012
4013static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004014max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004015{
4016 char *e;
4017 unsigned long n = simple_strtoul(buf, &e, 10);
4018
4019 if (*buf && (*e == 0 || *e == '\n')) {
4020 atomic_set(&mddev->max_corr_read_errors, n);
4021 return len;
4022 }
4023 return -EINVAL;
4024}
4025
4026static struct md_sysfs_entry max_corr_read_errors =
4027__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4028 max_corrected_read_errors_store);
4029
4030static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004031null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004032{
4033 return -EINVAL;
4034}
4035
4036static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004037new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004038{
4039 /* buf must be %d:%d\n? giving major and minor numbers */
4040 /* The new device is added to the array.
4041 * If the array has a persistent superblock, we read the
4042 * superblock to initialise info and check validity.
4043 * Otherwise, only checking done is that in bind_rdev_to_array,
4044 * which mainly checks size.
4045 */
4046 char *e;
4047 int major = simple_strtoul(buf, &e, 10);
4048 int minor;
4049 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004050 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004051 int err;
4052
4053 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4054 return -EINVAL;
4055 minor = simple_strtoul(e+1, &e, 10);
4056 if (*e && *e != '\n')
4057 return -EINVAL;
4058 dev = MKDEV(major, minor);
4059 if (major != MAJOR(dev) ||
4060 minor != MINOR(dev))
4061 return -EOVERFLOW;
4062
4063
4064 if (mddev->persistent) {
4065 rdev = md_import_device(dev, mddev->major_version,
4066 mddev->minor_version);
4067 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004068 struct md_rdev *rdev0
4069 = list_entry(mddev->disks.next,
4070 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004071 err = super_types[mddev->major_version]
4072 .load_super(rdev, rdev0, mddev->minor_version);
4073 if (err < 0)
4074 goto out;
4075 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004076 } else if (mddev->external)
4077 rdev = md_import_device(dev, -2, -1);
4078 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004079 rdev = md_import_device(dev, -1, -1);
4080
4081 if (IS_ERR(rdev))
4082 return PTR_ERR(rdev);
4083 err = bind_rdev_to_array(rdev, mddev);
4084 out:
4085 if (err)
4086 export_rdev(rdev);
4087 return err ? err : len;
4088}
4089
4090static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004091__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004092
4093static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004094bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004095{
4096 char *end;
4097 unsigned long chunk, end_chunk;
4098
4099 if (!mddev->bitmap)
4100 goto out;
4101 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4102 while (*buf) {
4103 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4104 if (buf == end) break;
4105 if (*end == '-') { /* range */
4106 buf = end + 1;
4107 end_chunk = simple_strtoul(buf, &end, 0);
4108 if (buf == end) break;
4109 }
4110 if (*end && !isspace(*end)) break;
4111 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004112 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004113 }
4114 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4115out:
4116 return len;
4117}
4118
4119static struct md_sysfs_entry md_bitmap =
4120__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4121
4122static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004123size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004124{
Andre Noll58c0fed2009-03-31 14:33:13 +11004125 return sprintf(page, "%llu\n",
4126 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004127}
4128
NeilBrownfd01b882011-10-11 16:47:53 +11004129static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004130
4131static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004132size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004133{
4134 /* If array is inactive, we can reduce the component size, but
4135 * not increase it (except from 0).
4136 * If array is active, we can try an on-line resize
4137 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004138 sector_t sectors;
4139 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004140
Andre Noll58c0fed2009-03-31 14:33:13 +11004141 if (err < 0)
4142 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004143 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004144 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07004145 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004146 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004147 if (mddev->dev_sectors == 0 ||
4148 mddev->dev_sectors > sectors)
4149 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004150 else
4151 err = -ENOSPC;
4152 }
4153 return err ? err : len;
4154}
4155
4156static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004157__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004158
NeilBrown8bb93aa2006-01-06 00:20:50 -08004159
4160/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004161 * This is one of
4162 * 'none' for arrays with no metadata (good luck...)
4163 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004164 * or N.M for internally known formats
4165 */
4166static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004167metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004168{
4169 if (mddev->persistent)
4170 return sprintf(page, "%d.%d\n",
4171 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004172 else if (mddev->external)
4173 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004174 else
4175 return sprintf(page, "none\n");
4176}
4177
4178static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004179metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004180{
4181 int major, minor;
4182 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004183 /* Changing the details of 'external' metadata is
4184 * always permitted. Otherwise there must be
4185 * no devices attached to the array.
4186 */
4187 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4188 ;
4189 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004190 return -EBUSY;
4191
4192 if (cmd_match(buf, "none")) {
4193 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004194 mddev->external = 0;
4195 mddev->major_version = 0;
4196 mddev->minor_version = 90;
4197 return len;
4198 }
4199 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004200 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004201 if (namelen >= sizeof(mddev->metadata_type))
4202 namelen = sizeof(mddev->metadata_type)-1;
4203 strncpy(mddev->metadata_type, buf+9, namelen);
4204 mddev->metadata_type[namelen] = 0;
4205 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4206 mddev->metadata_type[--namelen] = 0;
4207 mddev->persistent = 0;
4208 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004209 mddev->major_version = 0;
4210 mddev->minor_version = 90;
4211 return len;
4212 }
4213 major = simple_strtoul(buf, &e, 10);
4214 if (e==buf || *e != '.')
4215 return -EINVAL;
4216 buf = e+1;
4217 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004218 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004219 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004220 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004221 return -ENOENT;
4222 mddev->major_version = major;
4223 mddev->minor_version = minor;
4224 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004225 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004226 return len;
4227}
4228
4229static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004230__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004231
NeilBrowna35b0d62006-01-06 00:20:49 -08004232static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004233action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004234{
NeilBrown7eec3142005-11-08 21:39:44 -08004235 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004236 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4237 type = "frozen";
4238 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004239 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004240 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4241 type = "reshape";
4242 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004243 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4244 type = "resync";
4245 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4246 type = "check";
4247 else
4248 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004249 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004250 type = "recover";
4251 }
4252 return sprintf(page, "%s\n", type);
4253}
4254
NeilBrownfd01b882011-10-11 16:47:53 +11004255static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004256
NeilBrown24dd4692005-11-08 21:39:26 -08004257static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004258action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004259{
NeilBrown7eec3142005-11-08 21:39:44 -08004260 if (!mddev->pers || !mddev->pers->sync_request)
4261 return -EINVAL;
4262
NeilBrownb6a9ce62009-05-26 09:41:17 +10004263 if (cmd_match(page, "frozen"))
4264 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4265 else
4266 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4267
4268 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004269 if (mddev->sync_thread) {
4270 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004271 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004272 }
NeilBrown03c902e2006-01-06 00:20:46 -08004273 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4274 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004275 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004276 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004277 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004278 else if (cmd_match(page, "recover")) {
4279 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4280 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4281 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004282 int err;
4283 if (mddev->pers->start_reshape == NULL)
4284 return -EINVAL;
4285 err = mddev->pers->start_reshape(mddev);
4286 if (err)
4287 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004288 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004289 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004290 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004291 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004292 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004293 return -EINVAL;
4294 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4295 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004296 }
NeilBrown03c902e2006-01-06 00:20:46 -08004297 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004298 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004299 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004300 return len;
4301}
4302
NeilBrown9d888832005-11-08 21:39:26 -08004303static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004304mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004305{
4306 return sprintf(page, "%llu\n",
4307 (unsigned long long) mddev->resync_mismatches);
4308}
4309
NeilBrown80ca3a42006-07-10 04:44:18 -07004310static struct md_sysfs_entry md_scan_mode =
4311__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004312
NeilBrown96de1e62005-11-08 21:39:39 -08004313
NeilBrown80ca3a42006-07-10 04:44:18 -07004314static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004315
NeilBrown88202a02006-01-06 00:21:36 -08004316static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004317sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004318{
4319 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4320 mddev->sync_speed_min ? "local": "system");
4321}
4322
4323static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004324sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004325{
4326 int min;
4327 char *e;
4328 if (strncmp(buf, "system", 6)==0) {
4329 mddev->sync_speed_min = 0;
4330 return len;
4331 }
4332 min = simple_strtoul(buf, &e, 10);
4333 if (buf == e || (*e && *e != '\n') || min <= 0)
4334 return -EINVAL;
4335 mddev->sync_speed_min = min;
4336 return len;
4337}
4338
4339static struct md_sysfs_entry md_sync_min =
4340__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4341
4342static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004343sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004344{
4345 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4346 mddev->sync_speed_max ? "local": "system");
4347}
4348
4349static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004350sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004351{
4352 int max;
4353 char *e;
4354 if (strncmp(buf, "system", 6)==0) {
4355 mddev->sync_speed_max = 0;
4356 return len;
4357 }
4358 max = simple_strtoul(buf, &e, 10);
4359 if (buf == e || (*e && *e != '\n') || max <= 0)
4360 return -EINVAL;
4361 mddev->sync_speed_max = max;
4362 return len;
4363}
4364
4365static struct md_sysfs_entry md_sync_max =
4366__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4367
Iustin Popd7f3d292007-10-16 23:30:54 -07004368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004369degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004370{
4371 return sprintf(page, "%d\n", mddev->degraded);
4372}
4373static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004374
4375static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004376sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004377{
4378 return sprintf(page, "%d\n", mddev->parallel_resync);
4379}
4380
4381static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004382sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004383{
4384 long n;
4385
4386 if (strict_strtol(buf, 10, &n))
4387 return -EINVAL;
4388
4389 if (n != 0 && n != 1)
4390 return -EINVAL;
4391
4392 mddev->parallel_resync = n;
4393
4394 if (mddev->sync_thread)
4395 wake_up(&resync_wait);
4396
4397 return len;
4398}
4399
4400/* force parallel resync, even with shared block devices */
4401static struct md_sysfs_entry md_sync_force_parallel =
4402__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4403 sync_force_parallel_show, sync_force_parallel_store);
4404
4405static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004406sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004407{
4408 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004409 if (mddev->curr_resync == 0)
4410 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004411 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4412 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004413 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004414 db = resync - mddev->resync_mark_cnt;
4415 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004416}
4417
NeilBrown80ca3a42006-07-10 04:44:18 -07004418static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004419
4420static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004421sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004422{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004423 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004424
NeilBrownacb180b2009-04-14 16:28:34 +10004425 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4426 return sprintf(page, "none\n");
4427
NeilBrownc804cde2012-05-21 09:28:33 +10004428 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4429 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004430 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004431 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004432 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004433
NeilBrownacb180b2009-04-14 16:28:34 +10004434 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004435 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004436}
4437
NeilBrown80ca3a42006-07-10 04:44:18 -07004438static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004439
NeilBrowne464eaf2006-03-27 01:18:14 -08004440static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004441min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004442{
4443 return sprintf(page, "%llu\n",
4444 (unsigned long long)mddev->resync_min);
4445}
4446static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004447min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004448{
4449 unsigned long long min;
4450 if (strict_strtoull(buf, 10, &min))
4451 return -EINVAL;
4452 if (min > mddev->resync_max)
4453 return -EINVAL;
4454 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4455 return -EBUSY;
4456
4457 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004458 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004459 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004460 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004461 return -EINVAL;
4462 }
4463 mddev->resync_min = min;
4464
4465 return len;
4466}
4467
4468static struct md_sysfs_entry md_min_sync =
4469__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4470
4471static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004472max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004473{
4474 if (mddev->resync_max == MaxSector)
4475 return sprintf(page, "max\n");
4476 else
4477 return sprintf(page, "%llu\n",
4478 (unsigned long long)mddev->resync_max);
4479}
4480static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004481max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004482{
4483 if (strncmp(buf, "max", 3) == 0)
4484 mddev->resync_max = MaxSector;
4485 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004486 unsigned long long max;
4487 if (strict_strtoull(buf, 10, &max))
4488 return -EINVAL;
4489 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004490 return -EINVAL;
4491 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004492 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004493 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4494 return -EBUSY;
4495
4496 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004497 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004498 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004499 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004500 return -EINVAL;
4501 }
4502 mddev->resync_max = max;
4503 }
4504 wake_up(&mddev->recovery_wait);
4505 return len;
4506}
4507
4508static struct md_sysfs_entry md_max_sync =
4509__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4510
4511static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004512suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004513{
4514 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4515}
4516
4517static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004518suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004519{
4520 char *e;
4521 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004522 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004523
NeilBrownb8d966e2009-07-01 11:14:04 +10004524 if (mddev->pers == NULL ||
4525 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004526 return -EINVAL;
4527 if (buf == e || (*e && *e != '\n'))
4528 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004529
4530 mddev->suspend_lo = new;
4531 if (new >= old)
4532 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004533 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004534 else {
4535 /* Expanding suspended region - need to wait */
4536 mddev->pers->quiesce(mddev, 1);
4537 mddev->pers->quiesce(mddev, 0);
4538 }
4539 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004540}
4541static struct md_sysfs_entry md_suspend_lo =
4542__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4543
4544
4545static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004546suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004547{
4548 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4549}
4550
4551static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004552suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004553{
4554 char *e;
4555 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004556 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004557
NeilBrownb8d966e2009-07-01 11:14:04 +10004558 if (mddev->pers == NULL ||
4559 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004560 return -EINVAL;
4561 if (buf == e || (*e && *e != '\n'))
4562 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004563
4564 mddev->suspend_hi = new;
4565 if (new <= old)
4566 /* Shrinking suspended region */
4567 mddev->pers->quiesce(mddev, 2);
4568 else {
4569 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004570 mddev->pers->quiesce(mddev, 1);
4571 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004572 }
4573 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004574}
4575static struct md_sysfs_entry md_suspend_hi =
4576__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4577
NeilBrown08a02ec2007-05-09 02:35:38 -07004578static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004579reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004580{
4581 if (mddev->reshape_position != MaxSector)
4582 return sprintf(page, "%llu\n",
4583 (unsigned long long)mddev->reshape_position);
4584 strcpy(page, "none\n");
4585 return 5;
4586}
4587
4588static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004589reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004590{
NeilBrownc6563a82012-05-21 09:27:00 +10004591 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004592 char *e;
4593 unsigned long long new = simple_strtoull(buf, &e, 10);
4594 if (mddev->pers)
4595 return -EBUSY;
4596 if (buf == e || (*e && *e != '\n'))
4597 return -EINVAL;
4598 mddev->reshape_position = new;
4599 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10004600 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004601 mddev->new_level = mddev->level;
4602 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004603 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10004604 rdev_for_each(rdev, mddev)
4605 rdev->new_data_offset = rdev->data_offset;
NeilBrown08a02ec2007-05-09 02:35:38 -07004606 return len;
4607}
4608
4609static struct md_sysfs_entry md_reshape_position =
4610__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4611 reshape_position_store);
4612
Dan Williamsb522adc2009-03-31 15:00:31 +11004613static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10004614reshape_direction_show(struct mddev *mddev, char *page)
4615{
4616 return sprintf(page, "%s\n",
4617 mddev->reshape_backwards ? "backwards" : "forwards");
4618}
4619
4620static ssize_t
4621reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4622{
4623 int backwards = 0;
4624 if (cmd_match(buf, "forwards"))
4625 backwards = 0;
4626 else if (cmd_match(buf, "backwards"))
4627 backwards = 1;
4628 else
4629 return -EINVAL;
4630 if (mddev->reshape_backwards == backwards)
4631 return len;
4632
4633 /* check if we are allowed to change */
4634 if (mddev->delta_disks)
4635 return -EBUSY;
4636
4637 if (mddev->persistent &&
4638 mddev->major_version == 0)
4639 return -EINVAL;
4640
4641 mddev->reshape_backwards = backwards;
4642 return len;
4643}
4644
4645static struct md_sysfs_entry md_reshape_direction =
4646__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4647 reshape_direction_store);
4648
4649static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004650array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004651{
4652 if (mddev->external_size)
4653 return sprintf(page, "%llu\n",
4654 (unsigned long long)mddev->array_sectors/2);
4655 else
4656 return sprintf(page, "default\n");
4657}
4658
4659static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004661{
4662 sector_t sectors;
4663
4664 if (strncmp(buf, "default", 7) == 0) {
4665 if (mddev->pers)
4666 sectors = mddev->pers->size(mddev, 0, 0);
4667 else
4668 sectors = mddev->array_sectors;
4669
4670 mddev->external_size = 0;
4671 } else {
4672 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4673 return -EINVAL;
4674 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004675 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004676
4677 mddev->external_size = 1;
4678 }
4679
4680 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004681 if (mddev->pers) {
4682 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004683 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004684 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004685 return len;
4686}
4687
4688static struct md_sysfs_entry md_array_size =
4689__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4690 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004691
NeilBrowneae17012005-11-08 21:39:23 -08004692static struct attribute *md_default_attrs[] = {
4693 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004694 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004695 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004696 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004697 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004698 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004699 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004700 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004701 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004702 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004703 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10004704 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004705 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004706 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004707 NULL,
4708};
4709
4710static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004711 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004712 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004713 &md_sync_min.attr,
4714 &md_sync_max.attr,
4715 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004716 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004717 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004718 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004719 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004720 &md_suspend_lo.attr,
4721 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004722 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004723 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004724 NULL,
4725};
NeilBrown411036f2005-11-08 21:39:40 -08004726static struct attribute_group md_redundancy_group = {
4727 .name = NULL,
4728 .attrs = md_redundancy_attrs,
4729};
4730
NeilBrowneae17012005-11-08 21:39:23 -08004731
4732static ssize_t
4733md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4734{
4735 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004736 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004737 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004738
4739 if (!entry->show)
4740 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004741 spin_lock(&all_mddevs_lock);
4742 if (list_empty(&mddev->all_mddevs)) {
4743 spin_unlock(&all_mddevs_lock);
4744 return -EBUSY;
4745 }
4746 mddev_get(mddev);
4747 spin_unlock(&all_mddevs_lock);
4748
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004749 rv = mddev_lock(mddev);
4750 if (!rv) {
4751 rv = entry->show(mddev, page);
4752 mddev_unlock(mddev);
4753 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004754 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004755 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004756}
4757
4758static ssize_t
4759md_attr_store(struct kobject *kobj, struct attribute *attr,
4760 const char *page, size_t length)
4761{
4762 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004763 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004764 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004765
4766 if (!entry->store)
4767 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004768 if (!capable(CAP_SYS_ADMIN))
4769 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004770 spin_lock(&all_mddevs_lock);
4771 if (list_empty(&mddev->all_mddevs)) {
4772 spin_unlock(&all_mddevs_lock);
4773 return -EBUSY;
4774 }
4775 mddev_get(mddev);
4776 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004777 rv = mddev_lock(mddev);
4778 if (!rv) {
4779 rv = entry->store(mddev, page, length);
4780 mddev_unlock(mddev);
4781 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004782 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004783 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004784}
4785
4786static void md_free(struct kobject *ko)
4787{
NeilBrownfd01b882011-10-11 16:47:53 +11004788 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004789
4790 if (mddev->sysfs_state)
4791 sysfs_put(mddev->sysfs_state);
4792
4793 if (mddev->gendisk) {
4794 del_gendisk(mddev->gendisk);
4795 put_disk(mddev->gendisk);
4796 }
4797 if (mddev->queue)
4798 blk_cleanup_queue(mddev->queue);
4799
NeilBrowneae17012005-11-08 21:39:23 -08004800 kfree(mddev);
4801}
4802
Emese Revfy52cf25d2010-01-19 02:58:23 +01004803static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004804 .show = md_attr_show,
4805 .store = md_attr_store,
4806};
4807static struct kobj_type md_ktype = {
4808 .release = md_free,
4809 .sysfs_ops = &md_sysfs_ops,
4810 .default_attrs = md_default_attrs,
4811};
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813int mdp_major = 0;
4814
Dan Williams5fd3a172009-03-04 00:57:25 -07004815static void mddev_delayed_delete(struct work_struct *ws)
4816{
NeilBrownfd01b882011-10-11 16:47:53 +11004817 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004818
NeilBrown43a70502009-12-14 12:49:55 +11004819 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004820 kobject_del(&mddev->kobj);
4821 kobject_put(&mddev->kobj);
4822}
4823
NeilBrownefeb53c2009-01-09 08:31:10 +11004824static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004826 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004827 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004829 int partitioned;
4830 int shift;
4831 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004832 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004835 return -ENODEV;
4836
4837 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4838 shift = partitioned ? MdpMinorShift : 0;
4839 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Tejun Heoe804ac72010-10-15 15:36:08 +02004841 /* wait for any previous instance of this device to be
4842 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004843 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004844 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004845
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004846 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004847 error = -EEXIST;
4848 if (mddev->gendisk)
4849 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004850
4851 if (name) {
4852 /* Need to ensure that 'name' is not a duplicate.
4853 */
NeilBrownfd01b882011-10-11 16:47:53 +11004854 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004855 spin_lock(&all_mddevs_lock);
4856
4857 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4858 if (mddev2->gendisk &&
4859 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4860 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004861 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004862 }
4863 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
NeilBrown8b765392009-01-09 08:31:08 +11004865
NeilBrown0909dc42009-07-01 12:27:21 +10004866 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004867 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004868 if (!mddev->queue)
4869 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004870 mddev->queue->queuedata = mddev;
4871
NeilBrown409c57f2009-03-31 14:39:39 +11004872 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004873 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 disk = alloc_disk(1 << shift);
4876 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004877 blk_cleanup_queue(mddev->queue);
4878 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004879 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004881 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004883 if (name)
4884 strcpy(disk->disk_name, name);
4885 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004887 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 disk->fops = &md_fops;
4890 disk->private_data = mddev;
4891 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004892 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004893 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004894 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004895 * remove it now.
4896 */
4897 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004899 /* As soon as we call add_disk(), another thread could get
4900 * through to md_open, so make sure it doesn't get too far
4901 */
4902 mutex_lock(&mddev->open_mutex);
4903 add_disk(disk);
4904
Tejun Heoed9e1982008-08-25 19:56:05 +09004905 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4906 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004907 if (error) {
4908 /* This isn't possible, but as kobject_init_and_add is marked
4909 * __must_check, we must do something with the result
4910 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004911 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4912 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004913 error = 0;
4914 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004915 if (mddev->kobj.sd &&
4916 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004917 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004918 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004919 abort:
4920 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004921 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004922 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004923 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004924 }
NeilBrownd3374822009-01-09 08:31:10 +11004925 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004926 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004927}
4928
4929static struct kobject *md_probe(dev_t dev, int *part, void *data)
4930{
4931 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return NULL;
4933}
4934
NeilBrownefeb53c2009-01-09 08:31:10 +11004935static int add_named_array(const char *val, struct kernel_param *kp)
4936{
4937 /* val must be "md_*" where * is not all digits.
4938 * We allocate an array with a large free minor number, and
4939 * set the name to val. val must not already be an active name.
4940 */
4941 int len = strlen(val);
4942 char buf[DISK_NAME_LEN];
4943
4944 while (len && val[len-1] == '\n')
4945 len--;
4946 if (len >= DISK_NAME_LEN)
4947 return -E2BIG;
4948 strlcpy(buf, val, len+1);
4949 if (strncmp(buf, "md_", 3) != 0)
4950 return -EINVAL;
4951 return md_alloc(0, buf);
4952}
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954static void md_safemode_timeout(unsigned long data)
4955{
NeilBrownfd01b882011-10-11 16:47:53 +11004956 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Neil Brown0fd62b82008-06-28 08:31:36 +10004958 if (!atomic_read(&mddev->writes_pending)) {
4959 mddev->safemode = 1;
4960 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004961 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 md_wakeup_thread(mddev->thread);
4964}
4965
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004966static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
NeilBrownfd01b882011-10-11 16:47:53 +11004968int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
NeilBrown2604b702006-01-06 00:20:36 -08004970 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004971 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004972 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
NeilBrowna757e642005-04-16 15:26:42 -07004974 if (list_empty(&mddev->disks))
4975 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 if (mddev->pers)
4979 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004980 /* Cannot run until previous stop completes properly */
4981 if (mddev->sysfs_active)
4982 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 /*
4985 * Analyze all RAID superblock(s)
4986 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004987 if (!mddev->raid_disks) {
4988 if (!mddev->persistent)
4989 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004990 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
NeilBrownd9d166c2006-01-06 00:20:51 -08004993 if (mddev->level != LEVEL_NONE)
4994 request_module("md-level-%d", mddev->level);
4995 else if (mddev->clevel[0])
4996 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
4998 /*
4999 * Drop all container device buffers, from now on
5000 * the only valid external interface is through the md
5001 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 */
NeilBrowndafb20f2012-03-19 12:46:39 +11005003 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005004 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 continue;
5006 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005007 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07005008
5009 /* perform some consistency tests on the device.
5010 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005011 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005012 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005013 if (rdev->meta_bdev) {
5014 /* Nothing to check */;
5015 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005016 if (mddev->dev_sectors &&
5017 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005018 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07005019 printk("md: %s: data overlaps metadata\n",
5020 mdname(mddev));
5021 return -EINVAL;
5022 }
5023 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005024 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005025 > rdev->data_offset) {
5026 printk("md: %s: metadata overlaps data\n",
5027 mdname(mddev));
5028 return -EINVAL;
5029 }
5030 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005031 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033
NeilBrowna167f662010-10-26 18:31:13 +11005034 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10005035 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11005036 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11005037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005039 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005040 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005042 if (mddev->level != LEVEL_NONE)
5043 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5044 mddev->level);
5045 else
5046 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5047 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 return -EINVAL;
5049 }
NeilBrown2604b702006-01-06 00:20:36 -08005050 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005052 if (mddev->level != pers->level) {
5053 mddev->level = pers->level;
5054 mddev->new_level = pers->level;
5055 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005056 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
NeilBrownf6705572006-03-27 01:18:11 -08005058 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005059 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005060 /* This personality cannot handle reshaping... */
5061 mddev->pers = NULL;
5062 module_put(pers->owner);
5063 return -EINVAL;
5064 }
5065
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005066 if (pers->sync_request) {
5067 /* Warn if this is a potentially silly
5068 * configuration.
5069 */
5070 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005071 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005072 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005073
NeilBrowndafb20f2012-03-19 12:46:39 +11005074 rdev_for_each(rdev, mddev)
5075 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005076 if (rdev < rdev2 &&
5077 rdev->bdev->bd_contains ==
5078 rdev2->bdev->bd_contains) {
5079 printk(KERN_WARNING
5080 "%s: WARNING: %s appears to be"
5081 " on the same physical disk as"
5082 " %s.\n",
5083 mdname(mddev),
5084 bdevname(rdev->bdev,b),
5085 bdevname(rdev2->bdev,b2));
5086 warned = 1;
5087 }
5088 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005089
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005090 if (warned)
5091 printk(KERN_WARNING
5092 "True protection against single-disk"
5093 " failure might be compromised.\n");
5094 }
5095
NeilBrown657390d2005-08-26 18:34:16 -07005096 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005097 /* may be over-ridden by personality */
5098 mddev->resync_max_sectors = mddev->dev_sectors;
5099
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005100 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
NeilBrown0f9552b52009-12-30 12:08:50 +11005102 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005103 mddev->ro = 2; /* read-only, but switch on first write */
5104
NeilBrownb15c2e52006-01-06 00:20:16 -08005105 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005106 if (err)
5107 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11005108 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5109 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5110 " but 'external_size' not in effect?\n", __func__);
5111 printk(KERN_ERR
5112 "md: invalid array_size %llu > default size %llu\n",
5113 (unsigned long long)mddev->array_sectors / 2,
5114 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5115 err = -EINVAL;
5116 mddev->pers->stop(mddev);
5117 }
5118 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08005119 err = bitmap_create(mddev);
5120 if (err) {
5121 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5122 mdname(mddev), err);
5123 mddev->pers->stop(mddev);
5124 }
5125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 module_put(mddev->pers->owner);
5128 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07005129 bitmap_destroy(mddev);
5130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08005132 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005133 if (mddev->kobj.sd &&
5134 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005135 printk(KERN_WARNING
5136 "md: cannot register extra attributes for %s\n",
5137 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005138 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005139 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005140 mddev->ro = 0;
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11005143 atomic_set(&mddev->max_corr_read_errors,
5144 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 mddev->safemode = 0;
5146 mddev->safemode_timer.function = md_safemode_timeout;
5147 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07005148 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005150 smp_wmb();
5151 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11005152 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005153 if (rdev->raid_disk >= 0)
5154 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10005155 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5158
NeilBrown850b2b422006-10-03 01:15:46 -07005159 if (mddev->flags)
5160 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
NeilBrownd7603b72006-01-06 00:20:30 -08005162 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005163 sysfs_notify_dirent_safe(mddev->sysfs_state);
5164 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005165 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 return 0;
5167}
NeilBrown390ee602010-06-01 19:37:27 +10005168EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
NeilBrownfd01b882011-10-11 16:47:53 +11005170static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005171{
5172 int err;
5173
5174 err = md_run(mddev);
5175 if (err)
5176 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10005177 err = bitmap_load(mddev);
5178 if (err) {
5179 bitmap_destroy(mddev);
5180 goto out;
5181 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005182
5183 md_wakeup_thread(mddev->thread);
5184 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5185
NeilBrownfe60b012010-03-29 11:10:42 +11005186 set_capacity(mddev->gendisk, mddev->array_sectors);
5187 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005188 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005189 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5190out:
5191 return err;
5192}
5193
NeilBrownfd01b882011-10-11 16:47:53 +11005194static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195{
5196 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Andre Noll80fab1d2008-07-11 22:02:21 +10005198 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005200 return -ENXIO;
5201 if (!mddev->pers)
5202 return -EINVAL;
5203 if (!mddev->ro)
5204 return -EBUSY;
5205 mddev->safemode = 0;
5206 mddev->ro = 0;
5207 set_disk_ro(disk, 0);
5208 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5209 mdname(mddev));
5210 /* Kick recovery or resync if necessary */
5211 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5212 md_wakeup_thread(mddev->thread);
5213 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005214 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216}
5217
NeilBrownacc55e22006-06-26 00:27:47 -07005218/* similar to deny_write_access, but accounts for our holding a reference
5219 * to the file ourselves */
5220static int deny_bitmap_write_access(struct file * file)
5221{
5222 struct inode *inode = file->f_mapping->host;
5223
5224 spin_lock(&inode->i_lock);
5225 if (atomic_read(&inode->i_writecount) > 1) {
5226 spin_unlock(&inode->i_lock);
5227 return -ETXTBSY;
5228 }
5229 atomic_set(&inode->i_writecount, -1);
5230 spin_unlock(&inode->i_lock);
5231
5232 return 0;
5233}
5234
NeilBrown43a70502009-12-14 12:49:55 +11005235void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005236{
5237 struct inode *inode = file->f_mapping->host;
5238
5239 spin_lock(&inode->i_lock);
5240 atomic_set(&inode->i_writecount, 1);
5241 spin_unlock(&inode->i_lock);
5242}
5243
NeilBrownfd01b882011-10-11 16:47:53 +11005244static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005245{
5246 mddev->array_sectors = 0;
5247 mddev->external_size = 0;
5248 mddev->dev_sectors = 0;
5249 mddev->raid_disks = 0;
5250 mddev->recovery_cp = 0;
5251 mddev->resync_min = 0;
5252 mddev->resync_max = MaxSector;
5253 mddev->reshape_position = MaxSector;
5254 mddev->external = 0;
5255 mddev->persistent = 0;
5256 mddev->level = LEVEL_NONE;
5257 mddev->clevel[0] = 0;
5258 mddev->flags = 0;
5259 mddev->ro = 0;
5260 mddev->metadata_type[0] = 0;
5261 mddev->chunk_sectors = 0;
5262 mddev->ctime = mddev->utime = 0;
5263 mddev->layout = 0;
5264 mddev->max_disks = 0;
5265 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005266 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005267 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005268 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005269 mddev->new_level = LEVEL_NONE;
5270 mddev->new_layout = 0;
5271 mddev->new_chunk_sectors = 0;
5272 mddev->curr_resync = 0;
5273 mddev->resync_mismatches = 0;
5274 mddev->suspend_lo = mddev->suspend_hi = 0;
5275 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5276 mddev->recovery = 0;
5277 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005278 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005279 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005280 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005281 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005282 mddev->bitmap_info.offset = 0;
5283 mddev->bitmap_info.default_offset = 0;
5284 mddev->bitmap_info.chunksize = 0;
5285 mddev->bitmap_info.daemon_sleep = 0;
5286 mddev->bitmap_info.max_write_behind = 0;
5287}
5288
NeilBrownfd01b882011-10-11 16:47:53 +11005289static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005290{
5291 if (mddev->sync_thread) {
5292 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5293 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005294 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005295 }
5296
5297 del_timer_sync(&mddev->safemode_timer);
5298
5299 bitmap_flush(mddev);
5300 md_super_wait(mddev);
5301
5302 if (!mddev->in_sync || mddev->flags) {
5303 /* mark array as shutdown cleanly */
5304 mddev->in_sync = 1;
5305 md_update_sb(mddev, 1);
5306 }
5307}
NeilBrowndefad612011-01-14 09:14:33 +11005308
NeilBrownfd01b882011-10-11 16:47:53 +11005309void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005310{
5311 mddev_lock(mddev);
5312 __md_stop_writes(mddev);
5313 mddev_unlock(mddev);
5314}
NeilBrown390ee602010-06-01 19:37:27 +10005315EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005316
NeilBrownfd01b882011-10-11 16:47:53 +11005317void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005318{
NeilBrown0ca69882011-01-14 09:14:33 +11005319 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005320 mddev->pers->stop(mddev);
5321 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5322 mddev->to_remove = &md_redundancy_group;
5323 module_put(mddev->pers->owner);
5324 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005325 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005326}
NeilBrown390ee602010-06-01 19:37:27 +10005327EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005328
NeilBrownfd01b882011-10-11 16:47:53 +11005329static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005330{
5331 int err = 0;
5332 mutex_lock(&mddev->open_mutex);
5333 if (atomic_read(&mddev->openers) > is_open) {
5334 printk("md: %s still in use.\n",mdname(mddev));
5335 err = -EBUSY;
5336 goto out;
5337 }
5338 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005339 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005340
5341 err = -ENXIO;
5342 if (mddev->ro==1)
5343 goto out;
5344 mddev->ro = 1;
5345 set_disk_ro(mddev->gendisk, 1);
5346 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005347 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005348 err = 0;
5349 }
5350out:
5351 mutex_unlock(&mddev->open_mutex);
5352 return err;
5353}
5354
NeilBrown9e653b62006-06-26 00:27:58 -07005355/* mode:
5356 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005357 * 2 - stop but do not disassemble array
5358 */
NeilBrownfd01b882011-10-11 16:47:53 +11005359static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005362 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
NeilBrownc8c00a62009-08-10 12:50:52 +10005364 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005365 if (atomic_read(&mddev->openers) > is_open ||
5366 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005367 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005368 mutex_unlock(&mddev->open_mutex);
5369 return -EBUSY;
5370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
NeilBrown6e17b022010-08-07 21:41:19 +10005372 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005373 if (mddev->ro)
5374 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005375
NeilBrowndefad612011-01-14 09:14:33 +11005376 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005377 md_stop(mddev);
5378 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005379 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005380
NeilBrowna4bd82d2010-03-29 13:23:10 +11005381 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005382 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005383
NeilBrowndafb20f2012-03-19 12:46:39 +11005384 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005385 if (rdev->raid_disk >= 0)
5386 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005387
NeilBrowna4bd82d2010-03-29 13:23:10 +11005388 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005389 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005390 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005391 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005392
NeilBrowna4bd82d2010-03-29 13:23:10 +11005393 if (mddev->ro)
5394 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005395 } else
5396 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 /*
5398 * Free resources if final stop
5399 */
NeilBrown9e653b62006-06-26 00:27:58 -07005400 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5402
NeilBrown978f9462006-02-02 14:28:05 -08005403 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005404 if (mddev->bitmap_info.file) {
5405 restore_bitmap_write_access(mddev->bitmap_info.file);
5406 fput(mddev->bitmap_info.file);
5407 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005408 }
NeilBrownc3d97142009-12-14 12:49:52 +11005409 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 export_array(mddev);
5412
NeilBrown6177b472010-03-29 11:37:13 +11005413 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005414 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005415 if (mddev->hold_active == UNTIL_STOP)
5416 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005417 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005418 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005419 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005420 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005424#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005425static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426{
NeilBrown3cb03002011-10-11 16:45:26 +11005427 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 int err;
5429
NeilBrowna757e642005-04-16 15:26:42 -07005430 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 printk(KERN_INFO "md: running: ");
5434
NeilBrowndafb20f2012-03-19 12:46:39 +11005435 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 char b[BDEVNAME_SIZE];
5437 printk("<%s>", bdevname(rdev->bdev,b));
5438 }
5439 printk("\n");
5440
NeilBrownd710e132008-10-13 11:55:12 +11005441 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 if (err) {
5443 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005444 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446}
5447
5448/*
5449 * lets try to run arrays based on all disks that have arrived
5450 * until now. (those are in pending_raid_disks)
5451 *
5452 * the method: pick the first pending disk, collect all disks with
5453 * the same UUID, remove all from the pending list and put them into
5454 * the 'same_array' list. Then order this list based on superblock
5455 * update time (freshest comes first), kick out 'old' disks and
5456 * compare superblocks. If everything's fine then run it.
5457 *
5458 * If "unit" is allocated, then bump its reference count
5459 */
5460static void autorun_devices(int part)
5461{
NeilBrown3cb03002011-10-11 16:45:26 +11005462 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005463 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 char b[BDEVNAME_SIZE];
5465
5466 printk(KERN_INFO "md: autorun ...\n");
5467 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005468 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005470 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005472 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
5474 printk(KERN_INFO "md: considering %s ...\n",
5475 bdevname(rdev0->bdev,b));
5476 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005477 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 if (super_90_load(rdev, rdev0, 0) >= 0) {
5479 printk(KERN_INFO "md: adding %s ...\n",
5480 bdevname(rdev->bdev,b));
5481 list_move(&rdev->same_set, &candidates);
5482 }
5483 /*
5484 * now we have a set of devices, with all of them having
5485 * mostly sane superblocks. It's time to allocate the
5486 * mddev.
5487 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005488 if (part) {
5489 dev = MKDEV(mdp_major,
5490 rdev0->preferred_minor << MdpMinorShift);
5491 unit = MINOR(dev) >> MdpMinorShift;
5492 } else {
5493 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5494 unit = MINOR(dev);
5495 }
5496 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5498 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5499 break;
5500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 md_probe(dev, NULL, NULL);
5503 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005504 if (!mddev || !mddev->gendisk) {
5505 if (mddev)
5506 mddev_put(mddev);
5507 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 "md: cannot allocate memory for md drive.\n");
5509 break;
5510 }
5511 if (mddev_lock(mddev))
5512 printk(KERN_WARNING "md: %s locked, cannot run\n",
5513 mdname(mddev));
5514 else if (mddev->raid_disks || mddev->major_version
5515 || !list_empty(&mddev->disks)) {
5516 printk(KERN_WARNING
5517 "md: %s already running, cannot run %s\n",
5518 mdname(mddev), bdevname(rdev0->bdev,b));
5519 mddev_unlock(mddev);
5520 } else {
5521 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005522 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005523 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 list_del_init(&rdev->same_set);
5525 if (bind_rdev_to_array(rdev, mddev))
5526 export_rdev(rdev);
5527 }
5528 autorun_array(mddev);
5529 mddev_unlock(mddev);
5530 }
5531 /* on success, candidates will be empty, on error
5532 * it won't...
5533 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005534 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005535 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 mddev_put(mddev);
5539 }
5540 printk(KERN_INFO "md: ... autorun DONE.\n");
5541}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005542#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544static int get_version(void __user * arg)
5545{
5546 mdu_version_t ver;
5547
5548 ver.major = MD_MAJOR_VERSION;
5549 ver.minor = MD_MINOR_VERSION;
5550 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5551
5552 if (copy_to_user(arg, &ver, sizeof(ver)))
5553 return -EFAULT;
5554
5555 return 0;
5556}
5557
NeilBrownfd01b882011-10-11 16:47:53 +11005558static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
5560 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005561 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005562 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
NeilBrowna9f326e2009-09-23 18:06:41 +10005564 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005565 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005567 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 failed++;
5569 else {
5570 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005571 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005572 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 else
5574 spare++;
5575 }
5576 }
5577
5578 info.major_version = mddev->major_version;
5579 info.minor_version = mddev->minor_version;
5580 info.patch_version = MD_PATCHLEVEL_VERSION;
5581 info.ctime = mddev->ctime;
5582 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005583 info.size = mddev->dev_sectors / 2;
5584 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005585 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 info.nr_disks = nr;
5587 info.raid_disks = mddev->raid_disks;
5588 info.md_minor = mddev->md_minor;
5589 info.not_persistent= !mddev->persistent;
5590
5591 info.utime = mddev->utime;
5592 info.state = 0;
5593 if (mddev->in_sync)
5594 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005595 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005596 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005597 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 info.working_disks = working;
5599 info.failed_disks = failed;
5600 info.spare_disks = spare;
5601
5602 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005603 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 if (copy_to_user(arg, &info, sizeof(info)))
5606 return -EFAULT;
5607
5608 return 0;
5609}
5610
NeilBrownfd01b882011-10-11 16:47:53 +11005611static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005612{
5613 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5614 char *ptr, *buf = NULL;
5615 int err = -ENOMEM;
5616
Dan Williamsb5470dc2008-06-27 21:44:04 -07005617 if (md_allow_write(mddev))
5618 file = kmalloc(sizeof(*file), GFP_NOIO);
5619 else
5620 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005621
NeilBrown32a76272005-06-21 17:17:14 -07005622 if (!file)
5623 goto out;
5624
5625 /* bitmap disabled, zero the first byte and copy out */
5626 if (!mddev->bitmap || !mddev->bitmap->file) {
5627 file->pathname[0] = '\0';
5628 goto copy_out;
5629 }
5630
5631 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5632 if (!buf)
5633 goto out;
5634
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005635 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5636 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005637 goto out;
5638
5639 strcpy(file->pathname, ptr);
5640
5641copy_out:
5642 err = 0;
5643 if (copy_to_user(arg, file, sizeof(*file)))
5644 err = -EFAULT;
5645out:
5646 kfree(buf);
5647 kfree(file);
5648 return err;
5649}
5650
NeilBrownfd01b882011-10-11 16:47:53 +11005651static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
5653 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005654 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
5656 if (copy_from_user(&info, arg, sizeof(info)))
5657 return -EFAULT;
5658
Andre Noll26ef3792008-07-11 22:02:21 +10005659 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 if (rdev) {
5661 info.major = MAJOR(rdev->bdev->bd_dev);
5662 info.minor = MINOR(rdev->bdev->bd_dev);
5663 info.raid_disk = rdev->raid_disk;
5664 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005665 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005667 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 info.state |= (1<<MD_DISK_ACTIVE);
5669 info.state |= (1<<MD_DISK_SYNC);
5670 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005671 if (test_bit(WriteMostly, &rdev->flags))
5672 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 } else {
5674 info.major = info.minor = 0;
5675 info.raid_disk = -1;
5676 info.state = (1<<MD_DISK_REMOVED);
5677 }
5678
5679 if (copy_to_user(arg, &info, sizeof(info)))
5680 return -EFAULT;
5681
5682 return 0;
5683}
5684
NeilBrownfd01b882011-10-11 16:47:53 +11005685static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686{
5687 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005688 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 dev_t dev = MKDEV(info->major,info->minor);
5690
5691 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5692 return -EOVERFLOW;
5693
5694 if (!mddev->raid_disks) {
5695 int err;
5696 /* expecting a device which has a superblock */
5697 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5698 if (IS_ERR(rdev)) {
5699 printk(KERN_WARNING
5700 "md: md_import_device returned %ld\n",
5701 PTR_ERR(rdev));
5702 return PTR_ERR(rdev);
5703 }
5704 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005705 struct md_rdev *rdev0
5706 = list_entry(mddev->disks.next,
5707 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005708 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 .load_super(rdev, rdev0, mddev->minor_version);
5710 if (err < 0) {
5711 printk(KERN_WARNING
5712 "md: %s has different UUID to %s\n",
5713 bdevname(rdev->bdev,b),
5714 bdevname(rdev0->bdev,b2));
5715 export_rdev(rdev);
5716 return -EINVAL;
5717 }
5718 }
5719 err = bind_rdev_to_array(rdev, mddev);
5720 if (err)
5721 export_rdev(rdev);
5722 return err;
5723 }
5724
5725 /*
5726 * add_new_disk can be used once the array is assembled
5727 * to add "hot spares". They must already have a superblock
5728 * written
5729 */
5730 if (mddev->pers) {
5731 int err;
5732 if (!mddev->pers->hot_add_disk) {
5733 printk(KERN_WARNING
5734 "%s: personality does not support diskops!\n",
5735 mdname(mddev));
5736 return -EINVAL;
5737 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005738 if (mddev->persistent)
5739 rdev = md_import_device(dev, mddev->major_version,
5740 mddev->minor_version);
5741 else
5742 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 if (IS_ERR(rdev)) {
5744 printk(KERN_WARNING
5745 "md: md_import_device returned %ld\n",
5746 PTR_ERR(rdev));
5747 return PTR_ERR(rdev);
5748 }
NeilBrown1a855a02010-12-09 16:36:28 +11005749 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005750 if (!mddev->persistent) {
5751 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005752 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005753 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005754 set_bit(In_sync, &rdev->flags);
5755 } else
NeilBrown41158c72005-06-21 17:17:25 -07005756 rdev->raid_disk = -1;
5757 } else
5758 super_types[mddev->major_version].
5759 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005760 if ((info->state & (1<<MD_DISK_SYNC)) &&
5761 (!test_bit(In_sync, &rdev->flags) ||
5762 rdev->raid_disk != info->raid_disk)) {
5763 /* This was a hot-add request, but events doesn't
5764 * match, so reject it.
5765 */
5766 export_rdev(rdev);
5767 return -EINVAL;
5768 }
5769
NeilBrown1a855a02010-12-09 16:36:28 +11005770 if (test_bit(In_sync, &rdev->flags))
5771 rdev->saved_raid_disk = rdev->raid_disk;
5772 else
5773 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005774
NeilBrownb2d444d2005-11-08 21:39:31 -08005775 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005776 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5777 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005778 else
5779 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 rdev->raid_disk = -1;
5782 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005783 if (!err && !mddev->pers->hot_remove_disk) {
5784 /* If there is hot_add_disk but no hot_remove_disk
5785 * then added disks for geometry changes,
5786 * and should be added immediately.
5787 */
5788 super_types[mddev->major_version].
5789 validate_super(mddev, rdev);
5790 err = mddev->pers->hot_add_disk(mddev, rdev);
5791 if (err)
5792 unbind_rdev_from_array(rdev);
5793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 if (err)
5795 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005796 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005797 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005798
NeilBrown17571282006-12-10 02:20:52 -08005799 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005800 if (mddev->degraded)
5801 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005802 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005803 if (!err)
5804 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005805 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 return err;
5807 }
5808
5809 /* otherwise, add_new_disk is only allowed
5810 * for major_version==0 superblocks
5811 */
5812 if (mddev->major_version != 0) {
5813 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5814 mdname(mddev));
5815 return -EINVAL;
5816 }
5817
5818 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5819 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005820 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 if (IS_ERR(rdev)) {
5822 printk(KERN_WARNING
5823 "md: error, md_import_device() returned %ld\n",
5824 PTR_ERR(rdev));
5825 return PTR_ERR(rdev);
5826 }
5827 rdev->desc_nr = info->number;
5828 if (info->raid_disk < mddev->raid_disks)
5829 rdev->raid_disk = info->raid_disk;
5830 else
5831 rdev->raid_disk = -1;
5832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005834 if (info->state & (1<<MD_DISK_SYNC))
5835 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005837 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5838 set_bit(WriteMostly, &rdev->flags);
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (!mddev->persistent) {
5841 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005842 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5843 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005844 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005845 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
NeilBrown2bf071b2006-01-06 00:20:55 -08005847 err = bind_rdev_to_array(rdev, mddev);
5848 if (err) {
5849 export_rdev(rdev);
5850 return err;
5851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 }
5853
5854 return 0;
5855}
5856
NeilBrownfd01b882011-10-11 16:47:53 +11005857static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858{
5859 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005860 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 rdev = find_rdev(mddev, dev);
5863 if (!rdev)
5864 return -ENXIO;
5865
5866 if (rdev->raid_disk >= 0)
5867 goto busy;
5868
5869 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005870 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005871 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 return 0;
5874busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005875 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 bdevname(rdev->bdev,b), mdname(mddev));
5877 return -EBUSY;
5878}
5879
NeilBrownfd01b882011-10-11 16:47:53 +11005880static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
5882 char b[BDEVNAME_SIZE];
5883 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005884 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
5886 if (!mddev->pers)
5887 return -ENODEV;
5888
5889 if (mddev->major_version != 0) {
5890 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5891 " version-0 superblocks.\n",
5892 mdname(mddev));
5893 return -EINVAL;
5894 }
5895 if (!mddev->pers->hot_add_disk) {
5896 printk(KERN_WARNING
5897 "%s: personality does not support diskops!\n",
5898 mdname(mddev));
5899 return -EINVAL;
5900 }
5901
NeilBrownd710e132008-10-13 11:55:12 +11005902 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (IS_ERR(rdev)) {
5904 printk(KERN_WARNING
5905 "md: error, md_import_device() returned %ld\n",
5906 PTR_ERR(rdev));
5907 return -EINVAL;
5908 }
5909
5910 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005911 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005913 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
NeilBrown8190e752009-06-18 08:48:58 +10005915 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
NeilBrownb2d444d2005-11-08 21:39:31 -08005917 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 printk(KERN_WARNING
5919 "md: can not hot-add faulty %s disk to %s!\n",
5920 bdevname(rdev->bdev,b), mdname(mddev));
5921 err = -EINVAL;
5922 goto abort_export;
5923 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005924 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005926 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005927 err = bind_rdev_to_array(rdev, mddev);
5928 if (err)
5929 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
5931 /*
5932 * The rest should better be atomic, we can have disk failures
5933 * noticed in interrupt contexts ...
5934 */
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 rdev->raid_disk = -1;
5937
NeilBrown850b2b422006-10-03 01:15:46 -07005938 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 /*
5941 * Kick recovery, maybe this spare has to be added to the
5942 * array immediately.
5943 */
5944 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5945 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005946 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 return 0;
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949abort_export:
5950 export_rdev(rdev);
5951 return err;
5952}
5953
NeilBrownfd01b882011-10-11 16:47:53 +11005954static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005955{
5956 int err;
5957
NeilBrown36fa3062005-09-09 16:23:45 -07005958 if (mddev->pers) {
5959 if (!mddev->pers->quiesce)
5960 return -EBUSY;
5961 if (mddev->recovery || mddev->sync_thread)
5962 return -EBUSY;
5963 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005964 }
5965
NeilBrown36fa3062005-09-09 16:23:45 -07005966
5967 if (fd >= 0) {
5968 if (mddev->bitmap)
5969 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005970 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005971
NeilBrownc3d97142009-12-14 12:49:52 +11005972 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005973 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5974 mdname(mddev));
5975 return -EBADF;
5976 }
5977
NeilBrownc3d97142009-12-14 12:49:52 +11005978 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005979 if (err) {
5980 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5981 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005982 fput(mddev->bitmap_info.file);
5983 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005984 return err;
5985 }
NeilBrownc3d97142009-12-14 12:49:52 +11005986 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005987 } else if (mddev->bitmap == NULL)
5988 return -ENOENT; /* cannot remove what isn't there */
5989 err = 0;
5990 if (mddev->pers) {
5991 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005992 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005993 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005994 if (!err)
5995 err = bitmap_load(mddev);
5996 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005997 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005998 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005999 fd = -1; /* make sure to put the file */
6000 }
NeilBrown36fa3062005-09-09 16:23:45 -07006001 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07006002 }
6003 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11006004 if (mddev->bitmap_info.file) {
6005 restore_bitmap_write_access(mddev->bitmap_info.file);
6006 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07006007 }
NeilBrownc3d97142009-12-14 12:49:52 +11006008 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07006009 }
6010
NeilBrown32a76272005-06-21 17:17:14 -07006011 return err;
6012}
6013
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014/*
6015 * set_array_info is used two different ways
6016 * The original usage is when creating a new array.
6017 * In this usage, raid_disks is > 0 and it together with
6018 * level, size, not_persistent,layout,chunksize determine the
6019 * shape of the array.
6020 * This will always create an array with a type-0.90.0 superblock.
6021 * The newer usage is when assembling an array.
6022 * In this case raid_disks will be 0, and the major_version field is
6023 * use to determine which style super-blocks are to be found on the devices.
6024 * The minor and patch _version numbers are also kept incase the
6025 * super_block handler wishes to interpret them.
6026 */
NeilBrownfd01b882011-10-11 16:47:53 +11006027static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
6029
6030 if (info->raid_disks == 0) {
6031 /* just setting version number for superblock loading */
6032 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006033 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 super_types[info->major_version].name == NULL) {
6035 /* maybe try to auto-load a module? */
6036 printk(KERN_INFO
6037 "md: superblock version %d not known\n",
6038 info->major_version);
6039 return -EINVAL;
6040 }
6041 mddev->major_version = info->major_version;
6042 mddev->minor_version = info->minor_version;
6043 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006044 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006045 /* ensure mddev_put doesn't delete this now that there
6046 * is some minimal configuration.
6047 */
6048 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return 0;
6050 }
6051 mddev->major_version = MD_MAJOR_VERSION;
6052 mddev->minor_version = MD_MINOR_VERSION;
6053 mddev->patch_version = MD_PATCHLEVEL_VERSION;
6054 mddev->ctime = get_seconds();
6055
6056 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006057 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006058 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 mddev->raid_disks = info->raid_disks;
6060 /* don't set md_minor, it is determined by which /dev/md* was
6061 * openned
6062 */
6063 if (info->state & (1<<MD_SB_CLEAN))
6064 mddev->recovery_cp = MaxSector;
6065 else
6066 mddev->recovery_cp = 0;
6067 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006068 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
6070 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006071 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 mddev->max_disks = MD_SB_DISKS;
6074
NeilBrowne6910632008-02-06 01:39:51 -08006075 if (mddev->persistent)
6076 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006077 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
NeilBrownc3d97142009-12-14 12:49:52 +11006079 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6080 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006081
NeilBrownf6705572006-03-27 01:18:11 -08006082 mddev->reshape_position = MaxSector;
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 /*
6085 * Generate a 128 bit UUID
6086 */
6087 get_random_bytes(mddev->uuid, 16);
6088
NeilBrownf6705572006-03-27 01:18:11 -08006089 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006090 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006091 mddev->new_layout = mddev->layout;
6092 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006093 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 return 0;
6096}
6097
NeilBrownfd01b882011-10-11 16:47:53 +11006098void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006099{
Dan Williamsb522adc2009-03-31 15:00:31 +11006100 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6101
6102 if (mddev->external_size)
6103 return;
6104
Dan Williams1f403622009-03-31 14:59:03 +11006105 mddev->array_sectors = array_sectors;
6106}
6107EXPORT_SYMBOL(md_set_array_sectors);
6108
NeilBrownfd01b882011-10-11 16:47:53 +11006109static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006110{
NeilBrown3cb03002011-10-11 16:45:26 +11006111 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006112 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006113 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08006114
6115 if (mddev->pers->resize == NULL)
6116 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006117 /* The "num_sectors" is the number of sectors of each device that
6118 * is used. This can only make sense for arrays with redundancy.
6119 * linear and raid0 always use whatever space is available. We can only
6120 * consider changing this number if no resync or reconstruction is
6121 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006122 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006123 * of each device. If num_sectors is zero, we find the largest size
6124 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006125 */
6126 if (mddev->sync_thread)
6127 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10006128 if (mddev->bitmap)
6129 /* Sorry, cannot grow a bitmap yet, just remove it,
6130 * grow, and re-add.
6131 */
6132 return -EBUSY;
NeilBrowndafb20f2012-03-19 12:46:39 +11006133 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006134 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006135
Andre Nolld71f9f82008-07-11 22:02:22 +10006136 if (fit && (num_sectors == 0 || num_sectors > avail))
6137 num_sectors = avail;
6138 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006139 return -ENOSPC;
6140 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006141 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006142 if (!rv)
6143 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08006144 return rv;
6145}
6146
NeilBrownfd01b882011-10-11 16:47:53 +11006147static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006148{
6149 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006150 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006151 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006152 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006153 return -EINVAL;
6154 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006155 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006156 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08006157 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006158 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006159
6160 rdev_for_each(rdev, mddev) {
6161 if (mddev->raid_disks < raid_disks &&
6162 rdev->data_offset < rdev->new_data_offset)
6163 return -EINVAL;
6164 if (mddev->raid_disks > raid_disks &&
6165 rdev->data_offset > rdev->new_data_offset)
6166 return -EINVAL;
6167 }
6168
NeilBrown63c70c42006-03-27 01:18:13 -08006169 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006170 if (mddev->delta_disks < 0)
6171 mddev->reshape_backwards = 1;
6172 else if (mddev->delta_disks > 0)
6173 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006174
6175 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10006176 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11006177 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006178 mddev->reshape_backwards = 0;
6179 }
NeilBrownda943b992006-01-06 00:20:54 -08006180 return rv;
6181}
6182
6183
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184/*
6185 * update_array_info is used to change the configuration of an
6186 * on-line array.
6187 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6188 * fields in the info are checked against the array.
6189 * Any differences that cannot be handled will cause an error.
6190 * Normally, only one change can be managed at a time.
6191 */
NeilBrownfd01b882011-10-11 16:47:53 +11006192static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
6194 int rv = 0;
6195 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006196 int state = 0;
6197
6198 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11006199 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07006200 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 if (mddev->major_version != info->major_version ||
6203 mddev->minor_version != info->minor_version ||
6204/* mddev->patch_version != info->patch_version || */
6205 mddev->ctime != info->ctime ||
6206 mddev->level != info->level ||
6207/* mddev->layout != info->layout || */
6208 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10006209 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07006210 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6211 ((state^info->state) & 0xfffffe00)
6212 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 return -EINVAL;
6214 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11006215 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6216 cnt++;
6217 if (mddev->raid_disks != info->raid_disks)
6218 cnt++;
6219 if (mddev->layout != info->layout)
6220 cnt++;
6221 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6222 cnt++;
6223 if (cnt == 0)
6224 return 0;
6225 if (cnt > 1)
6226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
6228 if (mddev->layout != info->layout) {
6229 /* Change layout
6230 * we don't need to do anything at the md level, the
6231 * personality will take care of it all.
6232 */
NeilBrown50ac1682009-06-18 08:47:55 +10006233 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006235 else {
6236 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006237 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006238 if (rv)
6239 mddev->new_layout = mddev->layout;
6240 return rv;
6241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006243 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006244 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
NeilBrownda943b992006-01-06 00:20:54 -08006246 if (mddev->raid_disks != info->raid_disks)
6247 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
NeilBrown36fa3062005-09-09 16:23:45 -07006249 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6250 if (mddev->pers->quiesce == NULL)
6251 return -EINVAL;
6252 if (mddev->recovery || mddev->sync_thread)
6253 return -EBUSY;
6254 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6255 /* add the bitmap */
6256 if (mddev->bitmap)
6257 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006258 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006259 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006260 mddev->bitmap_info.offset =
6261 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07006262 mddev->pers->quiesce(mddev, 1);
6263 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006264 if (!rv)
6265 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006266 if (rv)
6267 bitmap_destroy(mddev);
6268 mddev->pers->quiesce(mddev, 0);
6269 } else {
6270 /* remove the bitmap */
6271 if (!mddev->bitmap)
6272 return -ENOENT;
6273 if (mddev->bitmap->file)
6274 return -EINVAL;
6275 mddev->pers->quiesce(mddev, 1);
6276 bitmap_destroy(mddev);
6277 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006278 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006279 }
6280 }
NeilBrown850b2b422006-10-03 01:15:46 -07006281 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 return rv;
6283}
6284
NeilBrownfd01b882011-10-11 16:47:53 +11006285static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
NeilBrown3cb03002011-10-11 16:45:26 +11006287 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 if (mddev->pers == NULL)
6290 return -ENODEV;
6291
6292 rdev = find_rdev(mddev, dev);
6293 if (!rdev)
6294 return -ENODEV;
6295
6296 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006297 if (!test_bit(Faulty, &rdev->flags))
6298 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 return 0;
6300}
6301
Andre Noll2f9618c2008-04-25 18:57:58 +02006302/*
6303 * We have a problem here : there is no easy way to give a CHS
6304 * virtual geometry. We currently pretend that we have a 2 heads
6305 * 4 sectors (with a BIG number of cylinders...). This drives
6306 * dosfs just mad... ;-)
6307 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006308static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6309{
NeilBrownfd01b882011-10-11 16:47:53 +11006310 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006311
6312 geo->heads = 2;
6313 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006314 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006315 return 0;
6316}
6317
Al Viroa39907f2008-03-02 10:31:15 -05006318static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 unsigned int cmd, unsigned long arg)
6320{
6321 int err = 0;
6322 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006323 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006324 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325
NeilBrown506c9e42011-12-23 10:17:26 +11006326 switch (cmd) {
6327 case RAID_VERSION:
6328 case GET_ARRAY_INFO:
6329 case GET_DISK_INFO:
6330 break;
6331 default:
6332 if (!capable(CAP_SYS_ADMIN))
6333 return -EACCES;
6334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
6336 /*
6337 * Commands dealing with the RAID driver but not any
6338 * particular array:
6339 */
6340 switch (cmd)
6341 {
6342 case RAID_VERSION:
6343 err = get_version(argp);
6344 goto done;
6345
6346 case PRINT_RAID_DEBUG:
6347 err = 0;
6348 md_print_devices();
6349 goto done;
6350
6351#ifndef MODULE
6352 case RAID_AUTORUN:
6353 err = 0;
6354 autostart_arrays(arg);
6355 goto done;
6356#endif
6357 default:;
6358 }
6359
6360 /*
6361 * Commands creating/starting a new array:
6362 */
6363
Al Viroa39907f2008-03-02 10:31:15 -05006364 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 if (!mddev) {
6367 BUG();
6368 goto abort;
6369 }
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 err = mddev_lock(mddev);
6372 if (err) {
6373 printk(KERN_INFO
6374 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6375 err, cmd);
6376 goto abort;
6377 }
6378
6379 switch (cmd)
6380 {
6381 case SET_ARRAY_INFO:
6382 {
6383 mdu_array_info_t info;
6384 if (!arg)
6385 memset(&info, 0, sizeof(info));
6386 else if (copy_from_user(&info, argp, sizeof(info))) {
6387 err = -EFAULT;
6388 goto abort_unlock;
6389 }
6390 if (mddev->pers) {
6391 err = update_array_info(mddev, &info);
6392 if (err) {
6393 printk(KERN_WARNING "md: couldn't update"
6394 " array info. %d\n", err);
6395 goto abort_unlock;
6396 }
6397 goto done_unlock;
6398 }
6399 if (!list_empty(&mddev->disks)) {
6400 printk(KERN_WARNING
6401 "md: array %s already has disks!\n",
6402 mdname(mddev));
6403 err = -EBUSY;
6404 goto abort_unlock;
6405 }
6406 if (mddev->raid_disks) {
6407 printk(KERN_WARNING
6408 "md: array %s already initialised!\n",
6409 mdname(mddev));
6410 err = -EBUSY;
6411 goto abort_unlock;
6412 }
6413 err = set_array_info(mddev, &info);
6414 if (err) {
6415 printk(KERN_WARNING "md: couldn't set"
6416 " array info. %d\n", err);
6417 goto abort_unlock;
6418 }
6419 }
6420 goto done_unlock;
6421
6422 default:;
6423 }
6424
6425 /*
6426 * Commands querying/configuring an existing array:
6427 */
NeilBrown32a76272005-06-21 17:17:14 -07006428 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006429 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006430 if ((!mddev->raid_disks && !mddev->external)
6431 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6432 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6433 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 err = -ENODEV;
6435 goto abort_unlock;
6436 }
6437
6438 /*
6439 * Commands even a read-only array can execute:
6440 */
6441 switch (cmd)
6442 {
6443 case GET_ARRAY_INFO:
6444 err = get_array_info(mddev, argp);
6445 goto done_unlock;
6446
NeilBrown32a76272005-06-21 17:17:14 -07006447 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006448 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006449 goto done_unlock;
6450
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 case GET_DISK_INFO:
6452 err = get_disk_info(mddev, argp);
6453 goto done_unlock;
6454
6455 case RESTART_ARRAY_RW:
6456 err = restart_array(mddev);
6457 goto done_unlock;
6458
6459 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006460 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 goto done_unlock;
6462
6463 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006464 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 goto done_unlock;
6466
Dan Williamse2218352010-05-12 08:25:37 +10006467 case BLKROSET:
6468 if (get_user(ro, (int __user *)(arg))) {
6469 err = -EFAULT;
6470 goto done_unlock;
6471 }
6472 err = -EINVAL;
6473
6474 /* if the bdev is going readonly the value of mddev->ro
6475 * does not matter, no writes are coming
6476 */
6477 if (ro)
6478 goto done_unlock;
6479
6480 /* are we are already prepared for writes? */
6481 if (mddev->ro != 1)
6482 goto done_unlock;
6483
6484 /* transitioning to readauto need only happen for
6485 * arrays that call md_write_start
6486 */
6487 if (mddev->pers) {
6488 err = restart_array(mddev);
6489 if (err == 0) {
6490 mddev->ro = 2;
6491 set_disk_ro(mddev->gendisk, 0);
6492 }
6493 }
6494 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
6497 /*
6498 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006499 * superblock, so we do not allow them on read-only arrays.
6500 * However non-MD ioctls (e.g. get-size) will still come through
6501 * here and hit the 'default' below, so only disallow
6502 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006504 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006505 if (mddev->ro == 2) {
6506 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006507 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006508 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6509 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006510 } else {
6511 err = -EROFS;
6512 goto abort_unlock;
6513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
6515
6516 switch (cmd)
6517 {
6518 case ADD_NEW_DISK:
6519 {
6520 mdu_disk_info_t info;
6521 if (copy_from_user(&info, argp, sizeof(info)))
6522 err = -EFAULT;
6523 else
6524 err = add_new_disk(mddev, &info);
6525 goto done_unlock;
6526 }
6527
6528 case HOT_REMOVE_DISK:
6529 err = hot_remove_disk(mddev, new_decode_dev(arg));
6530 goto done_unlock;
6531
6532 case HOT_ADD_DISK:
6533 err = hot_add_disk(mddev, new_decode_dev(arg));
6534 goto done_unlock;
6535
6536 case SET_DISK_FAULTY:
6537 err = set_disk_faulty(mddev, new_decode_dev(arg));
6538 goto done_unlock;
6539
6540 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006541 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 goto done_unlock;
6543
NeilBrown32a76272005-06-21 17:17:14 -07006544 case SET_BITMAP_FILE:
6545 err = set_bitmap_file(mddev, (int)arg);
6546 goto done_unlock;
6547
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 err = -EINVAL;
6550 goto abort_unlock;
6551 }
6552
6553done_unlock:
6554abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006555 if (mddev->hold_active == UNTIL_IOCTL &&
6556 err != -EINVAL)
6557 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 mddev_unlock(mddev);
6559
6560 return err;
6561done:
6562 if (err)
6563 MD_BUG();
6564abort:
6565 return err;
6566}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006567#ifdef CONFIG_COMPAT
6568static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6569 unsigned int cmd, unsigned long arg)
6570{
6571 switch (cmd) {
6572 case HOT_REMOVE_DISK:
6573 case HOT_ADD_DISK:
6574 case SET_DISK_FAULTY:
6575 case SET_BITMAP_FILE:
6576 /* These take in integer arg, do not convert */
6577 break;
6578 default:
6579 arg = (unsigned long)compat_ptr(arg);
6580 break;
6581 }
6582
6583 return md_ioctl(bdev, mode, cmd, arg);
6584}
6585#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
Al Viroa39907f2008-03-02 10:31:15 -05006587static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588{
6589 /*
6590 * Succeed if we can lock the mddev, which confirms that
6591 * it isn't being stopped right now.
6592 */
NeilBrownfd01b882011-10-11 16:47:53 +11006593 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 int err;
6595
NeilBrownd3374822009-01-09 08:31:10 +11006596 if (mddev->gendisk != bdev->bd_disk) {
6597 /* we are racing with mddev_put which is discarding this
6598 * bd_disk.
6599 */
6600 mddev_put(mddev);
6601 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006602 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006603 /* Then retry the open from the top */
6604 return -ERESTARTSYS;
6605 }
6606 BUG_ON(mddev != bdev->bd_disk->private_data);
6607
NeilBrownc8c00a62009-08-10 12:50:52 +10006608 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 goto out;
6610
6611 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006612 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006613 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006615 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 out:
6617 return err;
6618}
6619
Al Viroa39907f2008-03-02 10:31:15 -05006620static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621{
NeilBrownfd01b882011-10-11 16:47:53 +11006622 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006624 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006625 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 mddev_put(mddev);
6627
6628 return 0;
6629}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006630
6631static int md_media_changed(struct gendisk *disk)
6632{
NeilBrownfd01b882011-10-11 16:47:53 +11006633 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006634
6635 return mddev->changed;
6636}
6637
6638static int md_revalidate(struct gendisk *disk)
6639{
NeilBrownfd01b882011-10-11 16:47:53 +11006640 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006641
6642 mddev->changed = 0;
6643 return 0;
6644}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006645static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646{
6647 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006648 .open = md_open,
6649 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006650 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006651#ifdef CONFIG_COMPAT
6652 .compat_ioctl = md_compat_ioctl,
6653#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006654 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006655 .media_changed = md_media_changed,
6656 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657};
6658
Adrian Bunk75c96f82005-05-05 16:16:09 -07006659static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660{
NeilBrown2b8bf342011-10-11 16:48:23 +11006661 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 /*
6664 * md_thread is a 'system-thread', it's priority should be very
6665 * high. We avoid resource deadlocks individually in each
6666 * raid personality. (RAID5 does preallocation) We also use RR and
6667 * the very same RT priority as kswapd, thus we will never get
6668 * into a priority inversion deadlock.
6669 *
6670 * we definitely have to have equal or higher priority than
6671 * bdflush, otherwise bdflush will deadlock if there are too
6672 * many dirty RAID5 blocks.
6673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
NeilBrown6985c432005-10-19 21:23:47 -07006675 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006676 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
NeilBrown93588e22005-11-15 00:09:12 -08006678 /* We need to wait INTERRUPTIBLE so that
6679 * we don't add to the load-average.
6680 * That means we need to be sure no signals are
6681 * pending
6682 */
6683 if (signal_pending(current))
6684 flush_signals(current);
6685
6686 wait_event_interruptible_timeout
6687 (thread->wqueue,
6688 test_bit(THREAD_WAKEUP, &thread->flags)
6689 || kthread_should_stop(),
6690 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691
NeilBrown6c987912011-01-14 09:13:53 +11006692 clear_bit(THREAD_WAKEUP, &thread->flags);
6693 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006694 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 return 0;
6698}
6699
NeilBrown2b8bf342011-10-11 16:48:23 +11006700void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701{
6702 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006703 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 set_bit(THREAD_WAKEUP, &thread->flags);
6705 wake_up(&thread->wqueue);
6706 }
6707}
6708
NeilBrown2b8bf342011-10-11 16:48:23 +11006709struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 const char *name)
6711{
NeilBrown2b8bf342011-10-11 16:48:23 +11006712 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
NeilBrown2b8bf342011-10-11 16:48:23 +11006714 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 if (!thread)
6716 return NULL;
6717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 init_waitqueue_head(&thread->wqueue);
6719
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 thread->run = run;
6721 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006722 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006723 thread->tsk = kthread_run(md_thread, thread,
6724 "%s_%s",
6725 mdname(thread->mddev),
6726 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006727 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 kfree(thread);
6729 return NULL;
6730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 return thread;
6732}
6733
NeilBrown2b8bf342011-10-11 16:48:23 +11006734void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735{
NeilBrown2b8bf342011-10-11 16:48:23 +11006736 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006737 if (!thread)
6738 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006739 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006740 /* Locking ensures that mddev_unlock does not wake_up a
6741 * non-existent thread
6742 */
6743 spin_lock(&pers_lock);
6744 *threadp = NULL;
6745 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006746
6747 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 kfree(thread);
6749}
6750
NeilBrownfd01b882011-10-11 16:47:53 +11006751void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752{
6753 if (!mddev) {
6754 MD_BUG();
6755 return;
6756 }
6757
NeilBrownb2d444d2005-11-08 21:39:31 -08006758 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006760
NeilBrownde393cd2011-07-28 11:31:48 +10006761 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 return;
6763 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006764 if (mddev->degraded)
6765 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006766 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6768 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6769 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006770 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006771 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006772 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773}
6774
6775/* seq_file implementation /proc/mdstat */
6776
6777static void status_unused(struct seq_file *seq)
6778{
6779 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006780 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
6782 seq_printf(seq, "unused devices: ");
6783
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006784 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 char b[BDEVNAME_SIZE];
6786 i++;
6787 seq_printf(seq, "%s ",
6788 bdevname(rdev->bdev,b));
6789 }
6790 if (!i)
6791 seq_printf(seq, "<none>");
6792
6793 seq_printf(seq, "\n");
6794}
6795
6796
NeilBrownfd01b882011-10-11 16:47:53 +11006797static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798{
NeilBrowndd71cf62009-05-07 12:49:35 +10006799 sector_t max_sectors, resync, res;
6800 unsigned long dt, db;
6801 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006802 int scale;
6803 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
NeilBrowndd71cf62009-05-07 12:49:35 +10006805 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
NeilBrownc804cde2012-05-21 09:28:33 +10006807 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6808 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006809 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006811 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
6813 /*
6814 * Should not happen.
6815 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006816 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 MD_BUG();
6818 return;
6819 }
NeilBrown4588b422006-03-27 01:18:04 -08006820 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006821 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006822 * u32, as those are the requirements for sector_div.
6823 * Thus 'scale' must be at least 10
6824 */
6825 scale = 10;
6826 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006827 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006828 scale++;
6829 }
6830 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006831 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006832
6833 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 {
NeilBrown4588b422006-03-27 01:18:04 -08006835 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 seq_printf(seq, "[");
6837 for (i = 0; i < x; i++)
6838 seq_printf(seq, "=");
6839 seq_printf(seq, ">");
6840 for (i = 0; i < y; i++)
6841 seq_printf(seq, ".");
6842 seq_printf(seq, "] ");
6843 }
NeilBrown4588b422006-03-27 01:18:04 -08006844 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006845 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6846 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006847 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6848 "check" :
6849 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6850 "resync" : "recovery"))),
6851 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006852 (unsigned long long) resync/2,
6853 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
6855 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 * dt: time from mark until now
6857 * db: blocks written from mark until now
6858 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006859 *
6860 * rt is a sector_t, so could be 32bit or 64bit.
6861 * So we divide before multiply in case it is 32bit and close
6862 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006863 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006864 * near the end of resync when the number of remaining sectors
6865 * is close to 'db'.
6866 * We then divide rt by 32 after multiplying by db to compensate.
6867 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 */
6869 dt = ((jiffies - mddev->resync_mark) / HZ);
6870 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006871 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6872 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
NeilBrowndd71cf62009-05-07 12:49:35 +10006874 rt = max_sectors - resync; /* number of remaining sectors */
6875 sector_div(rt, db/32+1);
6876 rt *= dt;
6877 rt >>= 5;
6878
6879 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6880 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
NeilBrownff4e8d92006-07-10 04:44:16 -07006882 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
6885static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6886{
6887 struct list_head *tmp;
6888 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006889 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
6891 if (l >= 0x10000)
6892 return NULL;
6893 if (!l--)
6894 /* header */
6895 return (void*)1;
6896
6897 spin_lock(&all_mddevs_lock);
6898 list_for_each(tmp,&all_mddevs)
6899 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006900 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 mddev_get(mddev);
6902 spin_unlock(&all_mddevs_lock);
6903 return mddev;
6904 }
6905 spin_unlock(&all_mddevs_lock);
6906 if (!l--)
6907 return (void*)2;/* tail */
6908 return NULL;
6909}
6910
6911static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6912{
6913 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006914 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916 ++*pos;
6917 if (v == (void*)2)
6918 return NULL;
6919
6920 spin_lock(&all_mddevs_lock);
6921 if (v == (void*)1)
6922 tmp = all_mddevs.next;
6923 else
6924 tmp = mddev->all_mddevs.next;
6925 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006926 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 else {
6928 next_mddev = (void*)2;
6929 *pos = 0x10000;
6930 }
6931 spin_unlock(&all_mddevs_lock);
6932
6933 if (v != (void*)1)
6934 mddev_put(mddev);
6935 return next_mddev;
6936
6937}
6938
6939static void md_seq_stop(struct seq_file *seq, void *v)
6940{
NeilBrownfd01b882011-10-11 16:47:53 +11006941 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
6943 if (mddev && v != (void*)1 && v != (void*)2)
6944 mddev_put(mddev);
6945}
6946
6947static int md_seq_show(struct seq_file *seq, void *v)
6948{
NeilBrownfd01b882011-10-11 16:47:53 +11006949 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006950 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006951 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
6953 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006954 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 seq_printf(seq, "Personalities : ");
6956 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006957 list_for_each_entry(pers, &pers_list, list)
6958 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
6960 spin_unlock(&pers_lock);
6961 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006962 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 return 0;
6964 }
6965 if (v == (void*)2) {
6966 status_unused(seq);
6967 return 0;
6968 }
6969
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006970 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6974 seq_printf(seq, "%s : %sactive", mdname(mddev),
6975 mddev->pers ? "" : "in");
6976 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006977 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006979 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006980 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 seq_printf(seq, " %s", mddev->pers->name);
6982 }
6983
Andre Nolldd8ac332009-03-31 14:33:13 +11006984 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006985 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 char b[BDEVNAME_SIZE];
6987 seq_printf(seq, " %s[%d]",
6988 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006989 if (test_bit(WriteMostly, &rdev->flags))
6990 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006991 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 seq_printf(seq, "(F)");
6993 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006994 }
6995 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006996 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006997 if (test_bit(Replacement, &rdev->flags))
6998 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006999 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 }
7001
7002 if (!list_empty(&mddev->disks)) {
7003 if (mddev->pers)
7004 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007005 (unsigned long long)
7006 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 else
7008 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007009 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007011 if (mddev->persistent) {
7012 if (mddev->major_version != 0 ||
7013 mddev->minor_version != 90) {
7014 seq_printf(seq," super %d.%d",
7015 mddev->major_version,
7016 mddev->minor_version);
7017 }
NeilBrowne6910632008-02-06 01:39:51 -08007018 } else if (mddev->external)
7019 seq_printf(seq, " super external:%s",
7020 mddev->metadata_type);
7021 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07007022 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023
7024 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11007025 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08007027 if (mddev->pers->sync_request) {
7028 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11007029 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08007030 seq_printf(seq, "\n ");
7031 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
7032 seq_printf(seq, "\tresync=DELAYED\n ");
7033 else if (mddev->recovery_cp < MaxSector)
7034 seq_printf(seq, "\tresync=PENDING\n ");
7035 }
NeilBrown32a76272005-06-21 17:17:14 -07007036 } else
7037 seq_printf(seq, "\n ");
7038
NeilBrown57148962012-03-19 12:46:40 +11007039 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
7041 seq_printf(seq, "\n");
7042 }
7043 mddev_unlock(mddev);
7044
7045 return 0;
7046}
7047
Jan Engelhardt110518b2009-05-07 12:49:37 +10007048static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 .start = md_seq_start,
7050 .next = md_seq_next,
7051 .stop = md_seq_stop,
7052 .show = md_seq_show,
7053};
7054
7055static int md_seq_open(struct inode *inode, struct file *file)
7056{
Kay Sieversf1514632011-07-12 20:48:39 +02007057 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 int error;
7059
7060 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08007061 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02007062 return error;
7063
7064 seq = file->private_data;
7065 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 return error;
7067}
7068
NeilBrownd7603b72006-01-06 00:20:30 -08007069static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7070{
Kay Sieversf1514632011-07-12 20:48:39 +02007071 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08007072 int mask;
7073
7074 poll_wait(filp, &md_event_waiters, wait);
7075
7076 /* always allow read */
7077 mask = POLLIN | POLLRDNORM;
7078
Kay Sieversf1514632011-07-12 20:48:39 +02007079 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08007080 mask |= POLLERR | POLLPRI;
7081 return mask;
7082}
7083
Arjan van de Venfa027c22007-02-12 00:55:33 -08007084static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07007085 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 .open = md_seq_open,
7087 .read = seq_read,
7088 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07007089 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08007090 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091};
7092
NeilBrown84fc4b52011-10-11 16:49:58 +11007093int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007096 list_add_tail(&p->list, &pers_list);
7097 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 spin_unlock(&pers_lock);
7099 return 0;
7100}
7101
NeilBrown84fc4b52011-10-11 16:49:58 +11007102int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103{
NeilBrown2604b702006-01-06 00:20:36 -08007104 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007106 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 spin_unlock(&pers_lock);
7108 return 0;
7109}
7110
NeilBrownfd01b882011-10-11 16:47:53 +11007111static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112{
NeilBrown3cb03002011-10-11 16:45:26 +11007113 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11007115 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
7117 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10007118 rcu_read_lock();
7119 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11007121 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7122 (int)part_stat_read(&disk->part0, sectors[1]) -
7123 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07007124 /* sync IO will cause sync_io to increase before the disk_stats
7125 * as sync_io is counted when a request starts, and
7126 * disk_stats is counted when it completes.
7127 * So resync activity will cause curr_events to be smaller than
7128 * when there was no such activity.
7129 * non-sync IO will cause disk_stat to increase without
7130 * increasing sync_io so curr_events will (eventually)
7131 * be larger than it was before. Once it becomes
7132 * substantially larger, the test below will cause
7133 * the array to appear non-idle, and resync will slow
7134 * down.
7135 * If there is a lot of outstanding resync activity when
7136 * we set last_event to curr_events, then all that activity
7137 * completing might cause the array to appear non-idle
7138 * and resync will be slowed down even though there might
7139 * not have been non-resync activity. This will only
7140 * happen once though. 'last_events' will soon reflect
7141 * the state where there is little or no outstanding
7142 * resync requests, and further resync activity will
7143 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08007144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 */
NeilBrowneea1bf32009-03-31 14:27:02 +11007146 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 rdev->last_events = curr_events;
7148 idle = 0;
7149 }
7150 }
NeilBrown4b809912008-07-21 17:05:25 +10007151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 return idle;
7153}
7154
NeilBrownfd01b882011-10-11 16:47:53 +11007155void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156{
7157 /* another "blocks" (512byte) blocks have been synced */
7158 atomic_sub(blocks, &mddev->recovery_active);
7159 wake_up(&mddev->recovery_wait);
7160 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07007161 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 md_wakeup_thread(mddev->thread);
7163 // stop recovery, signal do_sync ....
7164 }
7165}
7166
7167
NeilBrown06d91a52005-06-21 17:17:12 -07007168/* md_write_start(mddev, bi)
7169 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07007170 * in superblock) before writing, schedule a superblock update
7171 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07007172 */
NeilBrownfd01b882011-10-11 16:47:53 +11007173void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174{
Neil Brown0fd62b82008-06-28 08:31:36 +10007175 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007176 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07007177 return;
NeilBrown06d91a52005-06-21 17:17:12 -07007178
NeilBrownf91de922005-11-08 21:39:36 -08007179 BUG_ON(mddev->ro == 1);
7180 if (mddev->ro == 2) {
7181 /* need to switch to read/write */
7182 mddev->ro = 0;
7183 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7184 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08007185 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007186 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08007187 }
NeilBrown06d91a52005-06-21 17:17:12 -07007188 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07007189 if (mddev->safemode == 1)
7190 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07007191 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08007192 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07007193 if (mddev->in_sync) {
7194 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07007195 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007196 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07007197 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10007198 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07007199 }
NeilBrowna9701a32005-11-08 21:39:34 -08007200 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07007201 }
Neil Brown0fd62b82008-06-28 08:31:36 +10007202 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007203 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07007204 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07007205 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206}
7207
NeilBrownfd01b882011-10-11 16:47:53 +11007208void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209{
7210 if (atomic_dec_and_test(&mddev->writes_pending)) {
7211 if (mddev->safemode == 2)
7212 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07007213 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7215 }
7216}
7217
NeilBrown2a2275d2007-01-26 00:57:11 -08007218/* md_allow_write(mddev)
7219 * Calling this ensures that the array is marked 'active' so that writes
7220 * may proceed without blocking. It is important to call this before
7221 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7222 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007223 *
7224 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7225 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007226 */
NeilBrownfd01b882011-10-11 16:47:53 +11007227int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007228{
7229 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007230 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007231 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007232 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007233 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007234 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007235
7236 spin_lock_irq(&mddev->write_lock);
7237 if (mddev->in_sync) {
7238 mddev->in_sync = 0;
7239 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007240 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007241 if (mddev->safemode_delay &&
7242 mddev->safemode == 0)
7243 mddev->safemode = 1;
7244 spin_unlock_irq(&mddev->write_lock);
7245 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007246 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007247 } else
7248 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007249
NeilBrown070dc6d2010-08-30 17:33:34 +10007250 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007251 return -EAGAIN;
7252 else
7253 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007254}
7255EXPORT_SYMBOL_GPL(md_allow_write);
7256
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257#define SYNC_MARKS 10
7258#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007259void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260{
NeilBrownfd01b882011-10-11 16:47:53 +11007261 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 unsigned int currspeed = 0,
7263 window;
NeilBrown57afd892005-06-21 17:17:13 -07007264 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 unsigned long mark[SYNC_MARKS];
7266 sector_t mark_cnt[SYNC_MARKS];
7267 int last_mark,m;
7268 struct list_head *tmp;
7269 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007270 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007271 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007272 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274 /* just incase thread restarts... */
7275 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7276 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007277 if (mddev->ro) /* never try to sync a read-only array */
7278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
NeilBrown61df9d92006-10-03 01:15:57 -07007280 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7281 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7282 desc = "data-check";
7283 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7284 desc = "requested-resync";
7285 else
7286 desc = "resync";
7287 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7288 desc = "reshape";
7289 else
7290 desc = "recovery";
7291
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 /* we overload curr_resync somewhat here.
7293 * 0 == not engaged in resync at all
7294 * 2 == checking that there is no conflict with another sync
7295 * 1 == like 2, but have yielded to allow conflicting resync to
7296 * commense
7297 * other == active in resync - this many blocks
7298 *
7299 * Before starting a resync we must have set curr_resync to
7300 * 2, and then checked that every "conflicting" array has curr_resync
7301 * less than ours. When we find one that is the same or higher
7302 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7303 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7304 * This will mean we have to start checking from the beginning again.
7305 *
7306 */
7307
7308 do {
7309 mddev->curr_resync = 2;
7310
7311 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007312 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007314
7315 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007317 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 if (mddev2 == mddev)
7319 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007320 if (!mddev->parallel_resync
7321 && mddev2->curr_resync
7322 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 DEFINE_WAIT(wq);
7324 if (mddev < mddev2 && mddev->curr_resync == 2) {
7325 /* arbitrarily yield */
7326 mddev->curr_resync = 1;
7327 wake_up(&resync_wait);
7328 }
7329 if (mddev > mddev2 && mddev->curr_resync == 1)
7330 /* no need to wait here, we can wait the next
7331 * time 'round when curr_resync == 2
7332 */
7333 continue;
NeilBrown97441972008-09-19 11:49:54 +10007334 /* We need to wait 'interruptible' so as not to
7335 * contribute to the load average, and not to
7336 * be caught by 'softlockup'
7337 */
7338 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007339 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007340 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007341 printk(KERN_INFO "md: delaying %s of %s"
7342 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007344 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007346 if (signal_pending(current))
7347 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 schedule();
7349 finish_wait(&resync_wait, &wq);
7350 goto try_again;
7351 }
7352 finish_wait(&resync_wait, &wq);
7353 }
7354 }
7355 } while (mddev->curr_resync < 2);
7356
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007357 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007358 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007360 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 */
7362 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007363 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007364 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007365 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7366 j = mddev->resync_min;
7367 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007368 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007369
NeilBrownccfcc3c2006-03-27 01:18:09 -08007370 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrownc804cde2012-05-21 09:28:33 +10007371 max_sectors = mddev->resync_max_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007372 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007374 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007375 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007376 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007377 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007378 if (rdev->raid_disk >= 0 &&
7379 !test_bit(Faulty, &rdev->flags) &&
7380 !test_bit(In_sync, &rdev->flags) &&
7381 rdev->recovery_offset < j)
7382 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007383 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
NeilBrown61df9d92006-10-03 01:15:57 -07007386 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7387 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7388 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007389 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007390 "(but not more than %d KB/sec) for %s.\n",
7391 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
NeilBrowneea1bf32009-03-31 14:27:02 +11007393 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007394
NeilBrown57afd892005-06-21 17:17:13 -07007395 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 for (m = 0; m < SYNC_MARKS; m++) {
7397 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007398 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 }
7400 last_mark = 0;
7401 mddev->resync_mark = mark[last_mark];
7402 mddev->resync_mark_cnt = mark_cnt[last_mark];
7403
7404 /*
7405 * Tune reconstruction:
7406 */
7407 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac42450c2011-06-07 17:48:35 -05007408 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7409 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410
7411 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 last_check = 0;
7413
7414 if (j>2) {
7415 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007416 "md: resuming %s of %s from checkpoint.\n",
7417 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 mddev->curr_resync = j;
7419 }
NeilBrown75d3da42011-01-14 09:14:34 +11007420 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421
7422 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007423 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
NeilBrown57afd892005-06-21 17:17:13 -07007425 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007426
NeilBrown7a91ee12009-05-26 12:57:21 +10007427 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7428 ((mddev->curr_resync > mddev->curr_resync_completed &&
7429 (mddev->curr_resync - mddev->curr_resync_completed)
7430 > (max_sectors >> 4)) ||
7431 (j - mddev->curr_resync_completed)*2
7432 >= mddev->resync_max - mddev->curr_resync_completed
7433 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007434 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007435 wait_event(mddev->recovery_wait,
7436 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007437 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007438 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007439 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007440 }
NeilBrownacb180b2009-04-14 16:28:34 +10007441
NeilBrowne62e58a2009-07-01 13:15:35 +10007442 while (j >= mddev->resync_max && !kthread_should_stop()) {
7443 /* As this condition is controlled by user-space,
7444 * we can block indefinitely, so use '_interruptible'
7445 * to avoid triggering warnings.
7446 */
7447 flush_signals(current); /* just in case */
7448 wait_event_interruptible(mddev->recovery_wait,
7449 mddev->resync_max > j
7450 || kthread_should_stop());
7451 }
NeilBrownacb180b2009-04-14 16:28:34 +10007452
7453 if (kthread_should_stop())
7454 goto interrupted;
7455
NeilBrown57afd892005-06-21 17:17:13 -07007456 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007457 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007458 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007459 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460 goto out;
7461 }
NeilBrown57afd892005-06-21 17:17:13 -07007462
7463 if (!skipped) { /* actual IO requested */
7464 io_sectors += sectors;
7465 atomic_add(sectors, &mddev->recovery_active);
7466 }
7467
NeilBrowne875ece2011-07-28 11:39:24 +10007468 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7469 break;
7470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 j += sectors;
7472 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007473 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007474 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007475 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007476 * visible in /proc/mdstat
7477 */
7478 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007479
7480 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481 continue;
7482
NeilBrown57afd892005-06-21 17:17:13 -07007483 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 repeat:
7485 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7486 /* step marks */
7487 int next = (last_mark+1) % SYNC_MARKS;
7488
7489 mddev->resync_mark = mark[next];
7490 mddev->resync_mark_cnt = mark_cnt[next];
7491 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007492 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 last_mark = next;
7494 }
7495
7496
NeilBrownc6207272008-02-06 01:39:52 -08007497 if (kthread_should_stop())
7498 goto interrupted;
7499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
7501 /*
7502 * this loop exits only if either when we are slower than
7503 * the 'hard' speed limit, or the system was IO-idle for
7504 * a jiffy.
7505 * the system might be non-idle CPU-wise, but we only care
7506 * about not overloading the IO subsystem. (things like an
7507 * e2fsck being done on the RAID array should execute fast)
7508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 cond_resched();
7510
NeilBrown57afd892005-06-21 17:17:13 -07007511 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7512 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513
NeilBrown88202a02006-01-06 00:21:36 -08007514 if (currspeed > speed_min(mddev)) {
7515 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007516 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007517 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 goto repeat;
7519 }
7520 }
7521 }
NeilBrown61df9d92006-10-03 01:15:57 -07007522 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 /*
7524 * this also signals 'finished resyncing' to md_stop
7525 */
7526 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7528
7529 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007530 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
NeilBrowndfc70642008-05-23 13:04:39 -07007532 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007533 mddev->curr_resync > 2) {
7534 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7535 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7536 if (mddev->curr_resync >= mddev->recovery_cp) {
7537 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007538 "md: checkpointing %s of %s.\n",
7539 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007540 mddev->recovery_cp =
7541 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007542 }
7543 } else
7544 mddev->recovery_cp = MaxSector;
7545 } else {
7546 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7547 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007548 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007549 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007550 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007551 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007552 !test_bit(Faulty, &rdev->flags) &&
7553 !test_bit(In_sync, &rdev->flags) &&
7554 rdev->recovery_offset < mddev->curr_resync)
7555 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007556 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007559 skip:
NeilBrown17571282006-12-10 02:20:52 -08007560 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561
NeilBrownc07b70a2009-12-14 12:49:48 +11007562 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7563 /* We completed so min/max setting can be forgotten if used. */
7564 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7565 mddev->resync_min = 0;
7566 mddev->resync_max = MaxSector;
7567 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7568 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 mddev->curr_resync = 0;
7570 wake_up(&resync_wait);
7571 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7572 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007573 return;
7574
7575 interrupted:
7576 /*
7577 * got a signal, exit.
7578 */
7579 printk(KERN_INFO
7580 "md: md_do_sync() got signal ... exiting\n");
7581 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7582 goto out;
7583
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584}
NeilBrown29269552006-03-27 01:18:10 -08007585EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586
NeilBrownfd01b882011-10-11 16:47:53 +11007587static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007588{
NeilBrown3cb03002011-10-11 16:45:26 +11007589 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007590 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007591 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007592
NeilBrown97e4f422009-03-31 14:33:13 +11007593 mddev->curr_resync_completed = 0;
7594
NeilBrowndafb20f2012-03-19 12:46:39 +11007595 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007596 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007597 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007598 (test_bit(Faulty, &rdev->flags) ||
7599 ! test_bit(In_sync, &rdev->flags)) &&
7600 atomic_read(&rdev->nr_pending)==0) {
7601 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007602 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007603 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007604 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007605 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007606 }
7607 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007608 if (removed)
7609 sysfs_notify(&mddev->kobj, NULL,
7610 "degraded");
7611
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007612
NeilBrowndafb20f2012-03-19 12:46:39 +11007613 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007614 if (rdev->raid_disk >= 0 &&
7615 !test_bit(In_sync, &rdev->flags) &&
7616 !test_bit(Faulty, &rdev->flags))
7617 spares++;
7618 if (rdev->raid_disk < 0
7619 && !test_bit(Faulty, &rdev->flags)) {
7620 rdev->recovery_offset = 0;
7621 if (mddev->pers->
7622 hot_add_disk(mddev, rdev) == 0) {
7623 if (sysfs_link_rdev(mddev, rdev))
7624 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007625 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007626 md_new_event(mddev);
7627 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007628 }
NeilBrowndfc70642008-05-23 13:04:39 -07007629 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007630 }
7631 return spares;
7632}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007633
NeilBrownfd01b882011-10-11 16:47:53 +11007634static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007635{
NeilBrown3cb03002011-10-11 16:45:26 +11007636 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007637
7638 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007639 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007640 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7641 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7642 /* success...*/
7643 /* activate any spares */
7644 if (mddev->pers->spare_active(mddev))
7645 sysfs_notify(&mddev->kobj, NULL,
7646 "degraded");
7647 }
7648 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7649 mddev->pers->finish_reshape)
7650 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007651
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007652 /* If array is no-longer degraded, then any saved_raid_disk
7653 * information must be scrapped. Also if any device is now
7654 * In_sync we must scrape the saved_raid_disk for that device
7655 * do the superblock for an incrementally recovered device
7656 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007657 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007658 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007659 if (!mddev->degraded ||
7660 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007661 rdev->saved_raid_disk = -1;
7662
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007663 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007664 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7665 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7666 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7667 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7668 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7669 /* flag recovery needed just to double check */
7670 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7671 sysfs_notify_dirent_safe(mddev->sysfs_action);
7672 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007673 if (mddev->event_work.func)
7674 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007675}
7676
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677/*
7678 * This routine is regularly called by all per-raid-array threads to
7679 * deal with generic issues like resync and super-block update.
7680 * Raid personalities that don't have a thread (linear/raid0) do not
7681 * need this as they never do any recovery or update the superblock.
7682 *
7683 * It does not do any resync itself, but rather "forks" off other threads
7684 * to do that as needed.
7685 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7686 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007687 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 * and wakeups up this thread which will reap the thread and finish up.
7689 * This thread also removes any faulty devices (with nr_pending == 0).
7690 *
7691 * The overall approach is:
7692 * 1/ if the superblock needs updating, update it.
7693 * 2/ If a recovery thread is running, don't do anything else.
7694 * 3/ If recovery has finished, clean up, possibly marking spares active.
7695 * 4/ If there are any faulty devices, remove them.
7696 * 5/ If array is degraded, try to add spares devices
7697 * 6/ If array has spares or is not in-sync, start a resync thread.
7698 */
NeilBrownfd01b882011-10-11 16:47:53 +11007699void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007701 if (mddev->suspended)
7702 return;
7703
NeilBrown5f404022005-06-21 17:17:16 -07007704 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007705 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
NeilBrownfca4d842005-06-21 17:17:11 -07007707 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007708 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007709 printk(KERN_INFO "md: %s in immediate safe mode\n",
7710 mdname(mddev));
7711 mddev->safemode = 2;
7712 }
7713 flush_signals(current);
7714 }
7715
NeilBrownc89a8ee2008-08-05 15:54:13 +10007716 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007719 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007721 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007722 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007723 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7724 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 ))
7726 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007727
NeilBrowndf5b89b2006-03-27 01:18:20 -08007728 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007729 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007730
NeilBrownc89a8ee2008-08-05 15:54:13 +10007731 if (mddev->ro) {
7732 /* Only thing we do on a ro array is remove
7733 * failed devices.
7734 */
NeilBrown3cb03002011-10-11 16:45:26 +11007735 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007736 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007737 if (rdev->raid_disk >= 0 &&
7738 !test_bit(Blocked, &rdev->flags) &&
7739 test_bit(Faulty, &rdev->flags) &&
7740 atomic_read(&rdev->nr_pending)==0) {
7741 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007742 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007743 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007744 rdev->raid_disk = -1;
7745 }
7746 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007747 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7748 goto unlock;
7749 }
7750
NeilBrown31a59e32008-04-30 00:52:30 -07007751 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007752 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007753 spin_lock_irq(&mddev->write_lock);
7754 if (mddev->safemode &&
7755 !atomic_read(&mddev->writes_pending) &&
7756 !mddev->in_sync &&
7757 mddev->recovery_cp == MaxSector) {
7758 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007759 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007761 }
7762 if (mddev->safemode == 1)
7763 mddev->safemode = 0;
7764 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007765 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007766 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007767 }
NeilBrownfca4d842005-06-21 17:17:11 -07007768
NeilBrown850b2b422006-10-03 01:15:46 -07007769 if (mddev->flags)
7770 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007771
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7773 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7774 /* resync/recovery still happening */
7775 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7776 goto unlock;
7777 }
7778 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007779 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780 goto unlock;
7781 }
Neil Brown72a23c22008-06-28 08:31:41 +10007782 /* Set RUNNING before clearing NEEDED to avoid
7783 * any transients in the value of "sync_action".
7784 */
7785 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007786 /* Clear some bits that don't mean anything, but
7787 * might be left set
7788 */
NeilBrown24dd4692005-11-08 21:39:26 -08007789 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7790 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
NeilBrowned209582012-04-24 10:23:14 +10007792 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7793 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007794 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 /* no recovery is running.
7796 * remove any failed drives, then
7797 * add spares if possible.
7798 * Spare are also removed and re-added, to allow
7799 * the personality to fail the re-add.
7800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007802 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007803 if (mddev->pers->check_reshape == NULL ||
7804 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007805 /* Cannot proceed */
7806 goto unlock;
7807 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007808 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007809 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007810 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7811 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007812 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007813 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007814 } else if (mddev->recovery_cp < MaxSector) {
7815 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007816 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007817 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7818 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007819 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007820
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007822 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7823 /* We are adding a device or devices to an array
7824 * which has the bitmap stored on all devices.
7825 * So make sure all bitmap pages get written
7826 */
7827 bitmap_write_all(mddev->bitmap);
7828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 mddev->sync_thread = md_register_thread(md_do_sync,
7830 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007831 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 if (!mddev->sync_thread) {
7833 printk(KERN_ERR "%s: could not start resync"
7834 " thread...\n",
7835 mdname(mddev));
7836 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007837 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7838 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7839 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7840 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7841 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007842 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007844 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007845 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846 }
7847 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007848 if (!mddev->sync_thread) {
7849 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7850 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7851 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007852 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007853 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855 mddev_unlock(mddev);
7856 }
7857}
7858
NeilBrownfd01b882011-10-11 16:47:53 +11007859void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007860{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007861 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007862 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007863 !test_bit(Blocked, &rdev->flags) &&
7864 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007865 msecs_to_jiffies(5000));
7866 rdev_dec_pending(rdev, mddev);
7867}
7868EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7869
NeilBrownc6563a82012-05-21 09:27:00 +10007870void md_finish_reshape(struct mddev *mddev)
7871{
7872 /* called be personality module when reshape completes. */
7873 struct md_rdev *rdev;
7874
7875 rdev_for_each(rdev, mddev) {
7876 if (rdev->data_offset > rdev->new_data_offset)
7877 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7878 else
7879 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7880 rdev->data_offset = rdev->new_data_offset;
7881 }
7882}
7883EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10007884
7885/* Bad block management.
7886 * We can record which blocks on each device are 'bad' and so just
7887 * fail those blocks, or that stripe, rather than the whole device.
7888 * Entries in the bad-block table are 64bits wide. This comprises:
7889 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7890 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7891 * A 'shift' can be set so that larger blocks are tracked and
7892 * consequently larger devices can be covered.
7893 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7894 *
7895 * Locking of the bad-block table uses a seqlock so md_is_badblock
7896 * might need to retry if it is very unlucky.
7897 * We will sometimes want to check for bad blocks in a bi_end_io function,
7898 * so we use the write_seqlock_irq variant.
7899 *
7900 * When looking for a bad block we specify a range and want to
7901 * know if any block in the range is bad. So we binary-search
7902 * to the last range that starts at-or-before the given endpoint,
7903 * (or "before the sector after the target range")
7904 * then see if it ends after the given start.
7905 * We return
7906 * 0 if there are no known bad blocks in the range
7907 * 1 if there are known bad block which are all acknowledged
7908 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7909 * plus the start/length of the first bad section we overlap.
7910 */
7911int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7912 sector_t *first_bad, int *bad_sectors)
7913{
7914 int hi;
7915 int lo = 0;
7916 u64 *p = bb->page;
7917 int rv = 0;
7918 sector_t target = s + sectors;
7919 unsigned seq;
7920
7921 if (bb->shift > 0) {
7922 /* round the start down, and the end up */
7923 s >>= bb->shift;
7924 target += (1<<bb->shift) - 1;
7925 target >>= bb->shift;
7926 sectors = target - s;
7927 }
7928 /* 'target' is now the first block after the bad range */
7929
7930retry:
7931 seq = read_seqbegin(&bb->lock);
7932
7933 hi = bb->count;
7934
7935 /* Binary search between lo and hi for 'target'
7936 * i.e. for the last range that starts before 'target'
7937 */
7938 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7939 * are known not to be the last range before target.
7940 * VARIANT: hi-lo is the number of possible
7941 * ranges, and decreases until it reaches 1
7942 */
7943 while (hi - lo > 1) {
7944 int mid = (lo + hi) / 2;
7945 sector_t a = BB_OFFSET(p[mid]);
7946 if (a < target)
7947 /* This could still be the one, earlier ranges
7948 * could not. */
7949 lo = mid;
7950 else
7951 /* This and later ranges are definitely out. */
7952 hi = mid;
7953 }
7954 /* 'lo' might be the last that started before target, but 'hi' isn't */
7955 if (hi > lo) {
7956 /* need to check all range that end after 's' to see if
7957 * any are unacknowledged.
7958 */
7959 while (lo >= 0 &&
7960 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7961 if (BB_OFFSET(p[lo]) < target) {
7962 /* starts before the end, and finishes after
7963 * the start, so they must overlap
7964 */
7965 if (rv != -1 && BB_ACK(p[lo]))
7966 rv = 1;
7967 else
7968 rv = -1;
7969 *first_bad = BB_OFFSET(p[lo]);
7970 *bad_sectors = BB_LEN(p[lo]);
7971 }
7972 lo--;
7973 }
7974 }
7975
7976 if (read_seqretry(&bb->lock, seq))
7977 goto retry;
7978
7979 return rv;
7980}
7981EXPORT_SYMBOL_GPL(md_is_badblock);
7982
7983/*
7984 * Add a range of bad blocks to the table.
7985 * This might extend the table, or might contract it
7986 * if two adjacent ranges can be merged.
7987 * We binary-search to find the 'insertion' point, then
7988 * decide how best to handle it.
7989 */
7990static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7991 int acknowledged)
7992{
7993 u64 *p;
7994 int lo, hi;
7995 int rv = 1;
7996
7997 if (bb->shift < 0)
7998 /* badblocks are disabled */
7999 return 0;
8000
8001 if (bb->shift) {
8002 /* round the start down, and the end up */
8003 sector_t next = s + sectors;
8004 s >>= bb->shift;
8005 next += (1<<bb->shift) - 1;
8006 next >>= bb->shift;
8007 sectors = next - s;
8008 }
8009
8010 write_seqlock_irq(&bb->lock);
8011
8012 p = bb->page;
8013 lo = 0;
8014 hi = bb->count;
8015 /* Find the last range that starts at-or-before 's' */
8016 while (hi - lo > 1) {
8017 int mid = (lo + hi) / 2;
8018 sector_t a = BB_OFFSET(p[mid]);
8019 if (a <= s)
8020 lo = mid;
8021 else
8022 hi = mid;
8023 }
8024 if (hi > lo && BB_OFFSET(p[lo]) > s)
8025 hi = lo;
8026
8027 if (hi > lo) {
8028 /* we found a range that might merge with the start
8029 * of our new range
8030 */
8031 sector_t a = BB_OFFSET(p[lo]);
8032 sector_t e = a + BB_LEN(p[lo]);
8033 int ack = BB_ACK(p[lo]);
8034 if (e >= s) {
8035 /* Yes, we can merge with a previous range */
8036 if (s == a && s + sectors >= e)
8037 /* new range covers old */
8038 ack = acknowledged;
8039 else
8040 ack = ack && acknowledged;
8041
8042 if (e < s + sectors)
8043 e = s + sectors;
8044 if (e - a <= BB_MAX_LEN) {
8045 p[lo] = BB_MAKE(a, e-a, ack);
8046 s = e;
8047 } else {
8048 /* does not all fit in one range,
8049 * make p[lo] maximal
8050 */
8051 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8052 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8053 s = a + BB_MAX_LEN;
8054 }
8055 sectors = e - s;
8056 }
8057 }
8058 if (sectors && hi < bb->count) {
8059 /* 'hi' points to the first range that starts after 's'.
8060 * Maybe we can merge with the start of that range */
8061 sector_t a = BB_OFFSET(p[hi]);
8062 sector_t e = a + BB_LEN(p[hi]);
8063 int ack = BB_ACK(p[hi]);
8064 if (a <= s + sectors) {
8065 /* merging is possible */
8066 if (e <= s + sectors) {
8067 /* full overlap */
8068 e = s + sectors;
8069 ack = acknowledged;
8070 } else
8071 ack = ack && acknowledged;
8072
8073 a = s;
8074 if (e - a <= BB_MAX_LEN) {
8075 p[hi] = BB_MAKE(a, e-a, ack);
8076 s = e;
8077 } else {
8078 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8079 s = a + BB_MAX_LEN;
8080 }
8081 sectors = e - s;
8082 lo = hi;
8083 hi++;
8084 }
8085 }
8086 if (sectors == 0 && hi < bb->count) {
8087 /* we might be able to combine lo and hi */
8088 /* Note: 's' is at the end of 'lo' */
8089 sector_t a = BB_OFFSET(p[hi]);
8090 int lolen = BB_LEN(p[lo]);
8091 int hilen = BB_LEN(p[hi]);
8092 int newlen = lolen + hilen - (s - a);
8093 if (s >= a && newlen < BB_MAX_LEN) {
8094 /* yes, we can combine them */
8095 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8096 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8097 memmove(p + hi, p + hi + 1,
8098 (bb->count - hi - 1) * 8);
8099 bb->count--;
8100 }
8101 }
8102 while (sectors) {
8103 /* didn't merge (it all).
8104 * Need to add a range just before 'hi' */
8105 if (bb->count >= MD_MAX_BADBLOCKS) {
8106 /* No room for more */
8107 rv = 0;
8108 break;
8109 } else {
8110 int this_sectors = sectors;
8111 memmove(p + hi + 1, p + hi,
8112 (bb->count - hi) * 8);
8113 bb->count++;
8114
8115 if (this_sectors > BB_MAX_LEN)
8116 this_sectors = BB_MAX_LEN;
8117 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8118 sectors -= this_sectors;
8119 s += this_sectors;
8120 }
8121 }
8122
8123 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10008124 if (!acknowledged)
8125 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10008126 write_sequnlock_irq(&bb->lock);
8127
8128 return rv;
8129}
8130
NeilBrown3cb03002011-10-11 16:45:26 +11008131int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10008132 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008133{
NeilBrownc6563a82012-05-21 09:27:00 +10008134 int rv;
8135 if (is_new)
8136 s += rdev->new_data_offset;
8137 else
8138 s += rdev->data_offset;
8139 rv = md_set_badblocks(&rdev->badblocks,
8140 s, sectors, 0);
NeilBrown2230dfe2011-07-28 11:31:46 +10008141 if (rv) {
8142 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11008143 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10008144 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8145 md_wakeup_thread(rdev->mddev->thread);
8146 }
8147 return rv;
8148}
8149EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8150
8151/*
8152 * Remove a range of bad blocks from the table.
8153 * This may involve extending the table if we spilt a region,
8154 * but it must not fail. So if the table becomes full, we just
8155 * drop the remove request.
8156 */
8157static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8158{
8159 u64 *p;
8160 int lo, hi;
8161 sector_t target = s + sectors;
8162 int rv = 0;
8163
8164 if (bb->shift > 0) {
8165 /* When clearing we round the start up and the end down.
8166 * This should not matter as the shift should align with
8167 * the block size and no rounding should ever be needed.
8168 * However it is better the think a block is bad when it
8169 * isn't than to think a block is not bad when it is.
8170 */
8171 s += (1<<bb->shift) - 1;
8172 s >>= bb->shift;
8173 target >>= bb->shift;
8174 sectors = target - s;
8175 }
8176
8177 write_seqlock_irq(&bb->lock);
8178
8179 p = bb->page;
8180 lo = 0;
8181 hi = bb->count;
8182 /* Find the last range that starts before 'target' */
8183 while (hi - lo > 1) {
8184 int mid = (lo + hi) / 2;
8185 sector_t a = BB_OFFSET(p[mid]);
8186 if (a < target)
8187 lo = mid;
8188 else
8189 hi = mid;
8190 }
8191 if (hi > lo) {
8192 /* p[lo] is the last range that could overlap the
8193 * current range. Earlier ranges could also overlap,
8194 * but only this one can overlap the end of the range.
8195 */
8196 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8197 /* Partial overlap, leave the tail of this range */
8198 int ack = BB_ACK(p[lo]);
8199 sector_t a = BB_OFFSET(p[lo]);
8200 sector_t end = a + BB_LEN(p[lo]);
8201
8202 if (a < s) {
8203 /* we need to split this range */
8204 if (bb->count >= MD_MAX_BADBLOCKS) {
8205 rv = 0;
8206 goto out;
8207 }
8208 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8209 bb->count++;
8210 p[lo] = BB_MAKE(a, s-a, ack);
8211 lo++;
8212 }
8213 p[lo] = BB_MAKE(target, end - target, ack);
8214 /* there is no longer an overlap */
8215 hi = lo;
8216 lo--;
8217 }
8218 while (lo >= 0 &&
8219 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8220 /* This range does overlap */
8221 if (BB_OFFSET(p[lo]) < s) {
8222 /* Keep the early parts of this range. */
8223 int ack = BB_ACK(p[lo]);
8224 sector_t start = BB_OFFSET(p[lo]);
8225 p[lo] = BB_MAKE(start, s - start, ack);
8226 /* now low doesn't overlap, so.. */
8227 break;
8228 }
8229 lo--;
8230 }
8231 /* 'lo' is strictly before, 'hi' is strictly after,
8232 * anything between needs to be discarded
8233 */
8234 if (hi - lo > 1) {
8235 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8236 bb->count -= (hi - lo - 1);
8237 }
8238 }
8239
8240 bb->changed = 1;
8241out:
8242 write_sequnlock_irq(&bb->lock);
8243 return rv;
8244}
8245
NeilBrownc6563a82012-05-21 09:27:00 +10008246int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8247 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10008248{
NeilBrownc6563a82012-05-21 09:27:00 +10008249 if (is_new)
8250 s += rdev->new_data_offset;
8251 else
8252 s += rdev->data_offset;
NeilBrown2230dfe2011-07-28 11:31:46 +10008253 return md_clear_badblocks(&rdev->badblocks,
NeilBrownc6563a82012-05-21 09:27:00 +10008254 s, sectors);
NeilBrown2230dfe2011-07-28 11:31:46 +10008255}
8256EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8257
8258/*
8259 * Acknowledge all bad blocks in a list.
8260 * This only succeeds if ->changed is clear. It is used by
8261 * in-kernel metadata updates
8262 */
8263void md_ack_all_badblocks(struct badblocks *bb)
8264{
8265 if (bb->page == NULL || bb->changed)
8266 /* no point even trying */
8267 return;
8268 write_seqlock_irq(&bb->lock);
8269
majianpengecb178b2012-03-19 12:46:42 +11008270 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008271 u64 *p = bb->page;
8272 int i;
8273 for (i = 0; i < bb->count ; i++) {
8274 if (!BB_ACK(p[i])) {
8275 sector_t start = BB_OFFSET(p[i]);
8276 int len = BB_LEN(p[i]);
8277 p[i] = BB_MAKE(start, len, 1);
8278 }
8279 }
NeilBrownde393cd2011-07-28 11:31:48 +10008280 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008281 }
8282 write_sequnlock_irq(&bb->lock);
8283}
8284EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8285
NeilBrown16c791a2011-07-28 11:31:47 +10008286/* sysfs access to bad-blocks list.
8287 * We present two files.
8288 * 'bad-blocks' lists sector numbers and lengths of ranges that
8289 * are recorded as bad. The list is truncated to fit within
8290 * the one-page limit of sysfs.
8291 * Writing "sector length" to this file adds an acknowledged
8292 * bad block list.
8293 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8294 * been acknowledged. Writing to this file adds bad blocks
8295 * without acknowledging them. This is largely for testing.
8296 */
8297
8298static ssize_t
8299badblocks_show(struct badblocks *bb, char *page, int unack)
8300{
8301 size_t len;
8302 int i;
8303 u64 *p = bb->page;
8304 unsigned seq;
8305
8306 if (bb->shift < 0)
8307 return 0;
8308
8309retry:
8310 seq = read_seqbegin(&bb->lock);
8311
8312 len = 0;
8313 i = 0;
8314
8315 while (len < PAGE_SIZE && i < bb->count) {
8316 sector_t s = BB_OFFSET(p[i]);
8317 unsigned int length = BB_LEN(p[i]);
8318 int ack = BB_ACK(p[i]);
8319 i++;
8320
8321 if (unack && ack)
8322 continue;
8323
8324 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8325 (unsigned long long)s << bb->shift,
8326 length << bb->shift);
8327 }
NeilBrownde393cd2011-07-28 11:31:48 +10008328 if (unack && len == 0)
8329 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008330
8331 if (read_seqretry(&bb->lock, seq))
8332 goto retry;
8333
8334 return len;
8335}
8336
8337#define DO_DEBUG 1
8338
8339static ssize_t
8340badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8341{
8342 unsigned long long sector;
8343 int length;
8344 char newline;
8345#ifdef DO_DEBUG
8346 /* Allow clearing via sysfs *only* for testing/debugging.
8347 * Normally only a successful write may clear a badblock
8348 */
8349 int clear = 0;
8350 if (page[0] == '-') {
8351 clear = 1;
8352 page++;
8353 }
8354#endif /* DO_DEBUG */
8355
8356 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8357 case 3:
8358 if (newline != '\n')
8359 return -EINVAL;
8360 case 2:
8361 if (length <= 0)
8362 return -EINVAL;
8363 break;
8364 default:
8365 return -EINVAL;
8366 }
8367
8368#ifdef DO_DEBUG
8369 if (clear) {
8370 md_clear_badblocks(bb, sector, length);
8371 return len;
8372 }
8373#endif /* DO_DEBUG */
8374 if (md_set_badblocks(bb, sector, length, !unack))
8375 return len;
8376 else
8377 return -ENOSPC;
8378}
8379
Adrian Bunk75c96f82005-05-05 16:16:09 -07008380static int md_notify_reboot(struct notifier_block *this,
8381 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382{
8383 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008384 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008385 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386
NeilBrownc744a652012-03-19 12:46:37 +11008387 for_each_mddev(mddev, tmp) {
8388 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008389 if (mddev->pers)
8390 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008391 mddev->safemode = 2;
8392 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008393 }
NeilBrownc744a652012-03-19 12:46:37 +11008394 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395 }
NeilBrownc744a652012-03-19 12:46:37 +11008396 /*
8397 * certain more exotic SCSI devices are known to be
8398 * volatile wrt too early system reboots. While the
8399 * right place to handle this issue is the given
8400 * driver, we do want to have a safe RAID driver ...
8401 */
8402 if (need_delay)
8403 mdelay(1000*1);
8404
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 return NOTIFY_DONE;
8406}
8407
Adrian Bunk75c96f82005-05-05 16:16:09 -07008408static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409 .notifier_call = md_notify_reboot,
8410 .next = NULL,
8411 .priority = INT_MAX, /* before any real devices */
8412};
8413
8414static void md_geninit(void)
8415{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008416 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008418 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419}
8420
Adrian Bunk75c96f82005-05-05 16:16:09 -07008421static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422{
Tejun Heoe804ac72010-10-15 15:36:08 +02008423 int ret = -ENOMEM;
8424
Tejun Heoada609e2011-01-25 14:35:54 +01008425 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008426 if (!md_wq)
8427 goto err_wq;
8428
8429 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8430 if (!md_misc_wq)
8431 goto err_misc_wq;
8432
8433 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8434 goto err_md;
8435
8436 if ((ret = register_blkdev(0, "mdp")) < 0)
8437 goto err_mdp;
8438 mdp_major = ret;
8439
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008440 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008441 md_probe, NULL, NULL);
8442 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 md_probe, NULL, NULL);
8444
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008446 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008447
8448 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450
Tejun Heoe804ac72010-10-15 15:36:08 +02008451err_mdp:
8452 unregister_blkdev(MD_MAJOR, "md");
8453err_md:
8454 destroy_workqueue(md_misc_wq);
8455err_misc_wq:
8456 destroy_workqueue(md_wq);
8457err_wq:
8458 return ret;
8459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460
8461#ifndef MODULE
8462
8463/*
8464 * Searches all registered partitions for autorun RAID arrays
8465 * at boot time.
8466 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008467
8468static LIST_HEAD(all_detected_devices);
8469struct detected_devices_node {
8470 struct list_head list;
8471 dev_t dev;
8472};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473
8474void md_autodetect_dev(dev_t dev)
8475{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008476 struct detected_devices_node *node_detected_dev;
8477
8478 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8479 if (node_detected_dev) {
8480 node_detected_dev->dev = dev;
8481 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8482 } else {
8483 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8484 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486}
8487
8488
8489static void autostart_arrays(int part)
8490{
NeilBrown3cb03002011-10-11 16:45:26 +11008491 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008492 struct detected_devices_node *node_detected_dev;
8493 dev_t dev;
8494 int i_scanned, i_passed;
8495
8496 i_scanned = 0;
8497 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498
8499 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8500
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008501 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8502 i_scanned++;
8503 node_detected_dev = list_entry(all_detected_devices.next,
8504 struct detected_devices_node, list);
8505 list_del(&node_detected_dev->list);
8506 dev = node_detected_dev->dev;
8507 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008508 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 if (IS_ERR(rdev))
8510 continue;
8511
NeilBrownb2d444d2005-11-08 21:39:31 -08008512 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 MD_BUG();
8514 continue;
8515 }
NeilBrownd0fae182008-03-04 14:29:31 -08008516 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008518 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008520
8521 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8522 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523
8524 autorun_devices(part);
8525}
8526
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008527#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528
8529static __exit void md_exit(void)
8530{
NeilBrownfd01b882011-10-11 16:47:53 +11008531 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008533
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008534 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008535 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008537 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 unregister_blkdev(mdp_major, "mdp");
8539 unregister_reboot_notifier(&md_notifier);
8540 unregister_sysctl_table(raid_table_header);
8541 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008542 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008544 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008546 destroy_workqueue(md_misc_wq);
8547 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548}
8549
Dan Williams685784a2007-07-09 11:56:42 -07008550subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551module_exit(md_exit)
8552
NeilBrownf91de922005-11-08 21:39:36 -08008553static int get_ro(char *buffer, struct kernel_param *kp)
8554{
8555 return sprintf(buffer, "%d", start_readonly);
8556}
8557static int set_ro(const char *val, struct kernel_param *kp)
8558{
8559 char *e;
8560 int num = simple_strtoul(val, &e, 10);
8561 if (*val && (*e == '\0' || *e == '\n')) {
8562 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008563 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008564 }
8565 return -EINVAL;
8566}
8567
NeilBrown80ca3a42006-07-10 04:44:18 -07008568module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8569module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008570
NeilBrownefeb53c2009-01-09 08:31:10 +11008571module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008572
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573EXPORT_SYMBOL(register_md_personality);
8574EXPORT_SYMBOL(unregister_md_personality);
8575EXPORT_SYMBOL(md_error);
8576EXPORT_SYMBOL(md_done_sync);
8577EXPORT_SYMBOL(md_write_start);
8578EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579EXPORT_SYMBOL(md_register_thread);
8580EXPORT_SYMBOL(md_unregister_thread);
8581EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582EXPORT_SYMBOL(md_check_recovery);
8583MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008584MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008585MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008586MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);