blob: 01233d855eb28b7d13c764879b2f3a4bf37f7df5 [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();
NeilBrowna167f662010-10-26 18:31:13 +1100455 bi = bio_alloc_mddev(GFP_KERNEL, 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;
610 mddev->resync_min = 0;
611 mddev->resync_max = MaxSector;
612 mddev->level = LEVEL_NONE;
613}
NeilBrown390ee602010-06-01 19:37:27 +1000614EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100615
NeilBrownfd01b882011-10-11 16:47:53 +1100616static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
NeilBrownfd01b882011-10-11 16:47:53 +1100618 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
NeilBrown8f5f02c2011-02-16 13:58:51 +1100620 if (unit && MAJOR(unit) != MD_MAJOR)
621 unit &= ~((1<<MdpMinorShift)-1);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 retry:
624 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
NeilBrownefeb53c2009-01-09 08:31:10 +1100626 if (unit) {
627 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
628 if (mddev->unit == unit) {
629 mddev_get(mddev);
630 spin_unlock(&all_mddevs_lock);
631 kfree(new);
632 return mddev;
633 }
634
635 if (new) {
636 list_add(&new->all_mddevs, &all_mddevs);
637 spin_unlock(&all_mddevs_lock);
638 new->hold_active = UNTIL_IOCTL;
639 return new;
640 }
641 } else if (new) {
642 /* find an unused unit number */
643 static int next_minor = 512;
644 int start = next_minor;
645 int is_free = 0;
646 int dev = 0;
647 while (!is_free) {
648 dev = MKDEV(MD_MAJOR, next_minor);
649 next_minor++;
650 if (next_minor > MINORMASK)
651 next_minor = 0;
652 if (next_minor == start) {
653 /* Oh dear, all in use. */
654 spin_unlock(&all_mddevs_lock);
655 kfree(new);
656 return NULL;
657 }
658
659 is_free = 1;
660 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
661 if (mddev->unit == dev) {
662 is_free = 0;
663 break;
664 }
665 }
666 new->unit = dev;
667 new->md_minor = MINOR(dev);
668 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 list_add(&new->all_mddevs, &all_mddevs);
670 spin_unlock(&all_mddevs_lock);
671 return new;
672 }
673 spin_unlock(&all_mddevs_lock);
674
NeilBrown9ffae0c2006-01-06 00:20:32 -0800675 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!new)
677 return NULL;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 new->unit = unit;
680 if (MAJOR(unit) == MD_MAJOR)
681 new->md_minor = MINOR(unit);
682 else
683 new->md_minor = MINOR(unit) >> MdpMinorShift;
684
NeilBrownfafd7fb2010-04-01 15:55:30 +1100685 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto retry;
688}
689
NeilBrownfd01b882011-10-11 16:47:53 +1100690static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800692 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
NeilBrownfd01b882011-10-11 16:47:53 +1100695static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100696{
697 return mutex_is_locked(&mddev->reconfig_mutex);
698}
699
NeilBrownfd01b882011-10-11 16:47:53 +1100700static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800702 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
NeilBrownb6eb1272010-04-15 10:13:47 +1000705static struct attribute_group md_redundancy_group;
706
NeilBrownfd01b882011-10-11 16:47:53 +1100707static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
NeilBrowna64c8762010-04-14 17:15:37 +1000709 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000710 /* These cannot be removed under reconfig_mutex as
711 * an access to the files will try to take reconfig_mutex
712 * while holding the file unremovable, which leads to
713 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000714 * So hold set sysfs_active while the remove in happeing,
715 * and anything else which might set ->to_remove or my
716 * otherwise change the sysfs namespace will fail with
717 * -EBUSY if sysfs_active is still set.
718 * We set sysfs_active under reconfig_mutex and elsewhere
719 * test it under the same mutex to ensure its correct value
720 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000721 */
NeilBrowna64c8762010-04-14 17:15:37 +1000722 struct attribute_group *to_remove = mddev->to_remove;
723 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000724 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000725 mutex_unlock(&mddev->reconfig_mutex);
726
NeilBrown00bcb4a2010-06-01 19:37:23 +1000727 if (mddev->kobj.sd) {
728 if (to_remove != &md_redundancy_group)
729 sysfs_remove_group(&mddev->kobj, to_remove);
730 if (mddev->pers == NULL ||
731 mddev->pers->sync_request == NULL) {
732 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
733 if (mddev->sysfs_action)
734 sysfs_put(mddev->sysfs_action);
735 mddev->sysfs_action = NULL;
736 }
NeilBrowna64c8762010-04-14 17:15:37 +1000737 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000738 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000739 } else
740 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Chris Dunlop751e67c2011-10-19 16:48:26 +1100742 /* As we've dropped the mutex we need a spinlock to
743 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000744 */
745 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700746 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000747 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
NeilBrownfd01b882011-10-11 16:47:53 +1100750static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
NeilBrown3cb03002011-10-11 16:45:26 +1100752 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
NeilBrowndafb20f2012-03-19 12:46:39 +1100754 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (rdev->desc_nr == nr)
756 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return NULL;
759}
760
NeilBrownfd01b882011-10-11 16:47:53 +1100761static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
NeilBrown3cb03002011-10-11 16:45:26 +1100763 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
NeilBrowndafb20f2012-03-19 12:46:39 +1100765 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (rdev->bdev->bd_dev == dev)
767 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return NULL;
770}
771
NeilBrown84fc4b52011-10-11 16:49:58 +1100772static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800773{
NeilBrown84fc4b52011-10-11 16:49:58 +1100774 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800775 list_for_each_entry(pers, &pers_list, list) {
776 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800777 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800778 if (strcmp(pers->name, clevel)==0)
779 return pers;
780 }
NeilBrown2604b702006-01-06 00:20:36 -0800781 return NULL;
782}
783
Andre Nollb73df2d2008-07-11 22:02:23 +1000784/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100785static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100787 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000788 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
NeilBrown3cb03002011-10-11 16:45:26 +1100791static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 if (rdev->sb_page)
794 MD_BUG();
795
796 rdev->sb_page = alloc_page(GFP_KERNEL);
797 if (!rdev->sb_page) {
798 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 return 0;
803}
804
NeilBrown3cb03002011-10-11 16:45:26 +1100805static void free_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800808 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rdev->sb_loaded = 0;
810 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000811 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100812 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
NeilBrown2699b672011-07-28 11:31:47 +1000814 if (rdev->bb_page) {
815 put_page(rdev->bb_page);
816 rdev->bb_page = NULL;
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820
NeilBrown6712ecf2007-09-27 12:47:43 +0200821static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700822{
NeilBrown3cb03002011-10-11 16:45:26 +1100823 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100824 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700825
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700826 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
827 printk("md: super_written gets error=%d, uptodate=%d\n",
828 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
829 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800830 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700831 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700832
NeilBrowna9701a32005-11-08 21:39:34 -0800833 if (atomic_dec_and_test(&mddev->pending_writes))
834 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700835 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700836}
837
NeilBrownfd01b882011-10-11 16:47:53 +1100838void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700839 sector_t sector, int size, struct page *page)
840{
841 /* write first size bytes of page to sector of rdev
842 * Increment mddev->pending_writes before returning
843 * and decrement it on completion, waking up sb_wait
844 * if zero is reached.
845 * If an error occurred, call md_error
846 */
NeilBrowna167f662010-10-26 18:31:13 +1100847 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700848
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100849 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700850 bio->bi_sector = sector;
851 bio_add_page(bio, page, size, 0);
852 bio->bi_private = rdev;
853 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800854
NeilBrown7bfa19f2005-06-21 17:17:28 -0700855 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000856 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800857}
858
NeilBrownfd01b882011-10-11 16:47:53 +1100859void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800860{
Tejun Heoe9c74692010-09-03 11:56:18 +0200861 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800862 DEFINE_WAIT(wq);
863 for(;;) {
864 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
865 if (atomic_read(&mddev->pending_writes)==0)
866 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800867 schedule();
868 }
869 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700870}
871
NeilBrown6712ecf2007-09-27 12:47:43 +0200872static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
NeilBrown3cb03002011-10-11 16:45:26 +1100877int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100878 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
NeilBrowna167f662010-10-26 18:31:13 +1100880 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct completion event;
882 int ret;
883
Jens Axboe721a9602011-03-09 11:56:30 +0100884 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100886 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
887 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100888 if (metadata_op)
889 bio->bi_sector = sector + rdev->sb_start;
890 else
891 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 bio_add_page(bio, page, size, 0);
893 init_completion(&event);
894 bio->bi_private = &event;
895 bio->bi_end_io = bi_complete;
896 submit_bio(rw, bio);
897 wait_for_completion(&event);
898
899 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
900 bio_put(bio);
901 return ret;
902}
NeilBrowna8745db2006-01-06 00:20:34 -0800903EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
NeilBrown3cb03002011-10-11 16:45:26 +1100905static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 char b[BDEVNAME_SIZE];
908 if (!rdev->sb_page) {
909 MD_BUG();
910 return -EINVAL;
911 }
912 if (rdev->sb_loaded)
913 return 0;
914
915
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100916 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto fail;
918 rdev->sb_loaded = 1;
919 return 0;
920
921fail:
922 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
923 bdevname(rdev->bdev,b));
924 return -EINVAL;
925}
926
927static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
928{
Andre Noll05710462008-07-11 22:02:20 +1000929 return sb1->set_uuid0 == sb2->set_uuid0 &&
930 sb1->set_uuid1 == sb2->set_uuid1 &&
931 sb1->set_uuid2 == sb2->set_uuid2 &&
932 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
936{
937 int ret;
938 mdp_super_t *tmp1, *tmp2;
939
940 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
941 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
942
943 if (!tmp1 || !tmp2) {
944 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100945 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 goto abort;
947 }
948
949 *tmp1 = *sb1;
950 *tmp2 = *sb2;
951
952 /*
953 * nr_disks is not constant
954 */
955 tmp1->nr_disks = 0;
956 tmp2->nr_disks = 0;
957
Andre Nollce0c8e02008-07-11 22:02:20 +1000958 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700960 kfree(tmp1);
961 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return ret;
963}
964
NeilBrown4d167f02007-05-09 02:35:37 -0700965
966static u32 md_csum_fold(u32 csum)
967{
968 csum = (csum & 0xffff) + (csum >> 16);
969 return (csum & 0xffff) + (csum >> 16);
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static unsigned int calc_sb_csum(mdp_super_t * sb)
973{
NeilBrown4d167f02007-05-09 02:35:37 -0700974 u64 newcsum = 0;
975 u32 *sb32 = (u32*)sb;
976 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned int disk_csum, csum;
978
979 disk_csum = sb->sb_csum;
980 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700981
982 for (i = 0; i < MD_SB_BYTES/4 ; i++)
983 newcsum += sb32[i];
984 csum = (newcsum & 0xffffffff) + (newcsum>>32);
985
986
987#ifdef CONFIG_ALPHA
988 /* This used to use csum_partial, which was wrong for several
989 * reasons including that different results are returned on
990 * different architectures. It isn't critical that we get exactly
991 * the same return value as before (we always csum_fold before
992 * testing, and that removes any differences). However as we
993 * know that csum_partial always returned a 16bit value on
994 * alphas, do a fold to maximise conformity to previous behaviour.
995 */
996 sb->sb_csum = md_csum_fold(disk_csum);
997#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return csum;
1001}
1002
1003
1004/*
1005 * Handle superblock details.
1006 * We want to be able to handle multiple superblock formats
1007 * so we have a common interface to them all, and an array of
1008 * different handlers.
1009 * We rely on user-space to write the initial superblock, and support
1010 * reading and updating of superblocks.
1011 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001012 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * loads and validates a superblock on dev.
1014 * if refdev != NULL, compare superblocks on both devices
1015 * Return:
1016 * 0 - dev has a superblock that is compatible with refdev
1017 * 1 - dev has a superblock that is compatible and newer than refdev
1018 * so dev should be used as the refdev in future
1019 * -EINVAL superblock incompatible or invalid
1020 * -othererror e.g. -EIO
1021 *
NeilBrownfd01b882011-10-11 16:47:53 +11001022 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 * Verify that dev is acceptable into mddev.
1024 * The first time, mddev->raid_disks will be 0, and data from
1025 * dev should be merged in. Subsequent calls check that dev
1026 * is new enough. Return 0 or -EINVAL
1027 *
NeilBrownfd01b882011-10-11 16:47:53 +11001028 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * Update the superblock for rdev with data in mddev
1030 * This does not write to disc.
1031 *
1032 */
1033
1034struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001035 char *name;
1036 struct module *owner;
NeilBrown3cb03002011-10-11 16:45:26 +11001037 int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001038 int minor_version);
NeilBrownfd01b882011-10-11 16:47:53 +11001039 int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev);
1040 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001041 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001042 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043};
1044
1045/*
Andre Noll0894cc32009-06-18 08:49:23 +10001046 * Check that the given mddev has no bitmap.
1047 *
1048 * This function is called from the run method of all personalities that do not
1049 * support bitmaps. It prints an error message and returns non-zero if mddev
1050 * has a bitmap. Otherwise, it returns 0.
1051 *
1052 */
NeilBrownfd01b882011-10-11 16:47:53 +11001053int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001054{
NeilBrownc3d97142009-12-14 12:49:52 +11001055 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001056 return 0;
1057 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1058 mdname(mddev), mddev->pers->name);
1059 return 1;
1060}
1061EXPORT_SYMBOL(md_check_no_bitmap);
1062
1063/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * load_super for 0.90.0
1065 */
NeilBrown3cb03002011-10-11 16:45:26 +11001066static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1069 mdp_super_t *sb;
1070 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
Andre Noll0f420352008-07-11 22:02:23 +10001073 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * it's at the end of the disk.
1075 *
1076 * It also happens to be a multiple of 4Kb.
1077 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001078 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
NeilBrown0002b272005-09-09 16:23:53 -07001080 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (ret) return ret;
1082
1083 ret = -EINVAL;
1084
1085 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +10001086 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (sb->md_magic != MD_SB_MAGIC) {
1089 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1090 b);
1091 goto abort;
1092 }
1093
1094 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001095 sb->minor_version < 90 ||
1096 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1098 sb->major_version, sb->minor_version,
1099 b);
1100 goto abort;
1101 }
1102
1103 if (sb->raid_disks <= 0)
1104 goto abort;
1105
NeilBrown4d167f02007-05-09 02:35:37 -07001106 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1108 b);
1109 goto abort;
1110 }
1111
1112 rdev->preferred_minor = sb->md_minor;
1113 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001114 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f383072011-07-28 11:31:47 +10001115 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (sb->level == LEVEL_MULTIPATH)
1118 rdev->desc_nr = -1;
1119 else
1120 rdev->desc_nr = sb->this_disk.number;
1121
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001122 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001124 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001126 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!uuid_equal(refsb, sb)) {
1128 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1129 b, bdevname(refdev->bdev,b2));
1130 goto abort;
1131 }
1132 if (!sb_equal(refsb, sb)) {
1133 printk(KERN_WARNING "md: %s has same UUID"
1134 " but different superblock to %s\n",
1135 b, bdevname(refdev->bdev, b2));
1136 goto abort;
1137 }
1138 ev1 = md_event(sb);
1139 ev2 = md_event(refsb);
1140 if (ev1 > ev2)
1141 ret = 1;
1142 else
1143 ret = 0;
1144 }
NeilBrown8190e752009-06-18 08:48:58 +10001145 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001146 /* Limit to 4TB as metadata cannot record more than that */
1147 if (rdev->sectors >= (2ULL << 32))
1148 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
NeilBrown27a7b262011-09-10 17:21:28 +10001150 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001151 /* "this cannot possibly happen" ... */
1152 ret = -EINVAL;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 abort:
1155 return ret;
1156}
1157
1158/*
1159 * validate_super for 0.90.0
1160 */
NeilBrownfd01b882011-10-11 16:47:53 +11001161static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001164 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001165 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
NeilBrown41158c72005-06-21 17:17:25 -07001167 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001168 clear_bit(Faulty, &rdev->flags);
1169 clear_bit(In_sync, &rdev->flags);
1170 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (mddev->raid_disks == 0) {
1173 mddev->major_version = 0;
1174 mddev->minor_version = sb->minor_version;
1175 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001176 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001177 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 mddev->ctime = sb->ctime;
1179 mddev->utime = sb->utime;
1180 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001181 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 mddev->layout = sb->layout;
1183 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001184 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001185 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001186 mddev->bitmap_info.offset = 0;
1187 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
NeilBrownf6705572006-03-27 01:18:11 -08001189 if (mddev->minor_version >= 91) {
1190 mddev->reshape_position = sb->reshape_position;
1191 mddev->delta_disks = sb->delta_disks;
1192 mddev->new_level = sb->new_level;
1193 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001194 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001195 } else {
1196 mddev->reshape_position = MaxSector;
1197 mddev->delta_disks = 0;
1198 mddev->new_level = mddev->level;
1199 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001200 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001201 }
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (sb->state & (1<<MD_SB_CLEAN))
1204 mddev->recovery_cp = MaxSector;
1205 else {
1206 if (sb->events_hi == sb->cp_events_hi &&
1207 sb->events_lo == sb->cp_events_lo) {
1208 mddev->recovery_cp = sb->recovery_cp;
1209 } else
1210 mddev->recovery_cp = 0;
1211 }
1212
1213 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1214 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1215 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1216 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1217
1218 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001219
1220 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001221 mddev->bitmap_info.file == NULL)
1222 mddev->bitmap_info.offset =
1223 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001224
NeilBrown41158c72005-06-21 17:17:25 -07001225 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001226 /* Insist on good event counter while assembling, except
1227 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001229 if (sb->disks[rdev->desc_nr].state & (
1230 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1231 if (ev1 < mddev->events)
1232 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001233 } else if (mddev->bitmap) {
1234 /* if adding to array with a bitmap, then we can accept an
1235 * older device ... but not too old.
1236 */
NeilBrown41158c72005-06-21 17:17:25 -07001237 if (ev1 < mddev->bitmap->events_cleared)
1238 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001239 } else {
1240 if (ev1 < mddev->events)
1241 /* just a hot-add of a new device, leave raid_disk at -1 */
1242 return 0;
1243 }
NeilBrown41158c72005-06-21 17:17:25 -07001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 desc = sb->disks + rdev->desc_nr;
1247
1248 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001249 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001250 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1251 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001252 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001254 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1255 /* active but not in sync implies recovery up to
1256 * reshape position. We don't know exactly where
1257 * that is, so set to zero for now */
1258 if (mddev->minor_version >= 91) {
1259 rdev->recovery_offset = 0;
1260 rdev->raid_disk = desc->raid_disk;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001263 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1264 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001265 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001266 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return 0;
1268}
1269
1270/*
1271 * sync_super for 0.90.0
1272 */
NeilBrownfd01b882011-10-11 16:47:53 +11001273static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001276 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /* make rdev->sb match mddev data..
1281 *
1282 * 1/ zero out disks
1283 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1284 * 3/ any empty disks < next_spare become removed
1285 *
1286 * disks[0] gets initialised to REMOVED because
1287 * we cannot be sure from other fields if it has
1288 * been initialised or not.
1289 */
1290 int i;
1291 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1292
NeilBrown61181562005-09-09 16:24:02 -07001293 rdev->sb_size = MD_SB_BYTES;
1294
Namhyung Kim65a06f02011-07-27 11:00:36 +10001295 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 memset(sb, 0, sizeof(*sb));
1298
1299 sb->md_magic = MD_SB_MAGIC;
1300 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 sb->patch_version = mddev->patch_version;
1302 sb->gvalid_words = 0; /* ignored */
1303 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1304 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1305 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1306 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1307
1308 sb->ctime = mddev->ctime;
1309 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001310 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sb->raid_disks = mddev->raid_disks;
1312 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001313 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 sb->utime = mddev->utime;
1315 sb->state = 0;
1316 sb->events_hi = (mddev->events>>32);
1317 sb->events_lo = (u32)mddev->events;
1318
NeilBrownf6705572006-03-27 01:18:11 -08001319 if (mddev->reshape_position == MaxSector)
1320 sb->minor_version = 90;
1321 else {
1322 sb->minor_version = 91;
1323 sb->reshape_position = mddev->reshape_position;
1324 sb->new_level = mddev->new_level;
1325 sb->delta_disks = mddev->delta_disks;
1326 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001327 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001328 }
1329 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (mddev->in_sync)
1331 {
1332 sb->recovery_cp = mddev->recovery_cp;
1333 sb->cp_events_hi = (mddev->events>>32);
1334 sb->cp_events_lo = (u32)mddev->events;
1335 if (mddev->recovery_cp == MaxSector)
1336 sb->state = (1<< MD_SB_CLEAN);
1337 } else
1338 sb->recovery_cp = 0;
1339
1340 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001341 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
NeilBrownc3d97142009-12-14 12:49:52 +11001343 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001344 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001347 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001349 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001350 int is_active = test_bit(In_sync, &rdev2->flags);
1351
1352 if (rdev2->raid_disk >= 0 &&
1353 sb->minor_version >= 91)
1354 /* we have nowhere to store the recovery_offset,
1355 * but if it is not below the reshape_position,
1356 * we can piggy-back on that.
1357 */
1358 is_active = 1;
1359 if (rdev2->raid_disk < 0 ||
1360 test_bit(Faulty, &rdev2->flags))
1361 is_active = 0;
1362 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001363 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001365 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001366 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 d = &sb->disks[rdev2->desc_nr];
1368 nr_disks++;
1369 d->number = rdev2->desc_nr;
1370 d->major = MAJOR(rdev2->bdev->bd_dev);
1371 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001372 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 d->raid_disk = rdev2->raid_disk;
1374 else
1375 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001376 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001378 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001380 if (test_bit(In_sync, &rdev2->flags))
1381 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 active++;
1383 working++;
1384 } else {
1385 d->state = 0;
1386 spare++;
1387 working++;
1388 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001389 if (test_bit(WriteMostly, &rdev2->flags))
1390 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* now set the "removed" and "faulty" bits on any missing devices */
1393 for (i=0 ; i < mddev->raid_disks ; i++) {
1394 mdp_disk_t *d = &sb->disks[i];
1395 if (d->state == 0 && d->number == 0) {
1396 d->number = i;
1397 d->raid_disk = i;
1398 d->state = (1<<MD_DISK_REMOVED);
1399 d->state |= (1<<MD_DISK_FAULTY);
1400 failed++;
1401 }
1402 }
1403 sb->nr_disks = nr_disks;
1404 sb->active_disks = active;
1405 sb->working_disks = working;
1406 sb->failed_disks = failed;
1407 sb->spare_disks = spare;
1408
1409 sb->this_disk = sb->disks[rdev->desc_nr];
1410 sb->sb_csum = calc_sb_csum(sb);
1411}
1412
1413/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001414 * rdev_size_change for 0.90.0
1415 */
1416static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001417super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001418{
Andre Noll58c0fed2009-03-31 14:33:13 +11001419 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001421 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001422 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001423 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001424 if (!num_sectors || num_sectors > rdev->sb_start)
1425 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001426 /* Limit to 4TB as metadata cannot record more than that.
1427 * 4TB == 2^32 KB, or 2*2^32 sectors.
1428 */
1429 if (num_sectors >= (2ULL << 32))
1430 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001431 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001432 rdev->sb_page);
1433 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001434 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001435}
1436
1437
1438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * version 1 superblock
1440 */
1441
NeilBrown1c05b4b2006-10-21 10:24:08 -07001442static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001444 __le32 disk_csum;
1445 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 unsigned long long newcsum;
1447 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001448 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int i;
1450
1451 disk_csum = sb->sb_csum;
1452 sb->sb_csum = 0;
1453 newcsum = 0;
1454 for (i=0; size>=4; size -= 4 )
1455 newcsum += le32_to_cpu(*isuper++);
1456
1457 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001458 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1461 sb->sb_csum = disk_csum;
1462 return cpu_to_le32(csum);
1463}
1464
NeilBrown2699b672011-07-28 11:31:47 +10001465static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1466 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001467static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct mdp_superblock_1 *sb;
1470 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001471 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001473 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
Andre Noll0f420352008-07-11 22:02:23 +10001476 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * It is always aligned to a 4K boundary and
1478 * depeding on minor_version, it can be:
1479 * 0: At least 8K, but less than 12K, from end of device
1480 * 1: At start of device
1481 * 2: 4K from start of device.
1482 */
1483 switch(minor_version) {
1484 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001485 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001486 sb_start -= 8*2;
1487 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
1489 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001490 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001493 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 default:
1496 return -EINVAL;
1497 }
Andre Noll0f420352008-07-11 22:02:23 +10001498 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
NeilBrown0002b272005-09-09 16:23:53 -07001500 /* superblock is rarely larger than 1K, but it can be larger,
1501 * and it is safe to read 4k, so we do that
1502 */
1503 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (ret) return ret;
1505
1506
Namhyung Kim65a06f02011-07-27 11:00:36 +10001507 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1510 sb->major_version != cpu_to_le32(1) ||
1511 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001512 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001513 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return -EINVAL;
1515
1516 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1517 printk("md: invalid superblock checksum on %s\n",
1518 bdevname(rdev->bdev,b));
1519 return -EINVAL;
1520 }
1521 if (le64_to_cpu(sb->data_size) < 10) {
1522 printk("md: data_size too small on %s\n",
1523 bdevname(rdev->bdev,b));
1524 return -EINVAL;
1525 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 rdev->preferred_minor = 0xffff;
1528 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001529 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
NeilBrown0002b272005-09-09 16:23:53 -07001531 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001532 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001533 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001534 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1535
1536 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001537 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001538 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001539
NeilBrown31b65a02006-07-10 04:44:14 -07001540 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1541 rdev->desc_nr = -1;
1542 else
1543 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1544
NeilBrown2699b672011-07-28 11:31:47 +10001545 if (!rdev->bb_page) {
1546 rdev->bb_page = alloc_page(GFP_KERNEL);
1547 if (!rdev->bb_page)
1548 return -ENOMEM;
1549 }
1550 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1551 rdev->badblocks.count == 0) {
1552 /* need to load the bad block list.
1553 * Currently we limit it to one page.
1554 */
1555 s32 offset;
1556 sector_t bb_sector;
1557 u64 *bbp;
1558 int i;
1559 int sectors = le16_to_cpu(sb->bblog_size);
1560 if (sectors > (PAGE_SIZE / 512))
1561 return -EINVAL;
1562 offset = le32_to_cpu(sb->bblog_offset);
1563 if (offset == 0)
1564 return -EINVAL;
1565 bb_sector = (long long)offset;
1566 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1567 rdev->bb_page, READ, true))
1568 return -EIO;
1569 bbp = (u64 *)page_address(rdev->bb_page);
1570 rdev->badblocks.shift = sb->bblog_shift;
1571 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1572 u64 bb = le64_to_cpu(*bbp);
1573 int count = bb & (0x3ff);
1574 u64 sector = bb >> 10;
1575 sector <<= sb->bblog_shift;
1576 count <<= sb->bblog_shift;
1577 if (bb + 1 == 0)
1578 break;
1579 if (md_set_badblocks(&rdev->badblocks,
1580 sector, count, 1) == 0)
1581 return -EINVAL;
1582 }
1583 } else if (sb->bblog_offset == 0)
1584 rdev->badblocks.shift = -1;
1585
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001586 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001587 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001588 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001590 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1593 sb->level != refsb->level ||
1594 sb->layout != refsb->layout ||
1595 sb->chunksize != refsb->chunksize) {
1596 printk(KERN_WARNING "md: %s has strangely different"
1597 " superblock to %s\n",
1598 bdevname(rdev->bdev,b),
1599 bdevname(refdev->bdev,b2));
1600 return -EINVAL;
1601 }
1602 ev1 = le64_to_cpu(sb->events);
1603 ev2 = le64_to_cpu(refsb->events);
1604
1605 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001606 ret = 1;
1607 else
1608 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
NeilBrowna1801f82008-03-04 14:29:31 -08001610 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001611 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001612 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001614 rdev->sectors = rdev->sb_start;
1615 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001617 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001618 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001619 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
NeilBrownfd01b882011-10-11 16:47:53 +11001623static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001625 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001626 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
NeilBrown41158c72005-06-21 17:17:25 -07001628 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001629 clear_bit(Faulty, &rdev->flags);
1630 clear_bit(In_sync, &rdev->flags);
1631 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (mddev->raid_disks == 0) {
1634 mddev->major_version = 1;
1635 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001636 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001637 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1639 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1640 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001641 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 mddev->layout = le32_to_cpu(sb->layout);
1643 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001644 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001645 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001646 mddev->bitmap_info.offset = 0;
1647 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1650 memcpy(mddev->uuid, sb->set_uuid, 16);
1651
1652 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001653
NeilBrown71c08052005-09-09 16:23:51 -07001654 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001655 mddev->bitmap_info.file == NULL )
1656 mddev->bitmap_info.offset =
1657 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001658
NeilBrownf6705572006-03-27 01:18:11 -08001659 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1660 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1661 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1662 mddev->new_level = le32_to_cpu(sb->new_level);
1663 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001664 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001665 } else {
1666 mddev->reshape_position = MaxSector;
1667 mddev->delta_disks = 0;
1668 mddev->new_level = mddev->level;
1669 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001670 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001671 }
1672
NeilBrown41158c72005-06-21 17:17:25 -07001673 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001674 /* Insist of good event counter while assembling, except for
1675 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001677 if (rdev->desc_nr >= 0 &&
1678 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1679 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1680 if (ev1 < mddev->events)
1681 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001682 } else if (mddev->bitmap) {
1683 /* If adding to array with a bitmap, then we can accept an
1684 * older device, but not too old.
1685 */
NeilBrown41158c72005-06-21 17:17:25 -07001686 if (ev1 < mddev->bitmap->events_cleared)
1687 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001688 } else {
1689 if (ev1 < mddev->events)
1690 /* just a hot-add of a new device, leave raid_disk at -1 */
1691 return 0;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (mddev->level != LEVEL_MULTIPATH) {
1694 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001695 if (rdev->desc_nr < 0 ||
1696 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1697 role = 0xffff;
1698 rdev->desc_nr = -1;
1699 } else
1700 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 switch(role) {
1702 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 break;
1704 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001705 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 break;
1707 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001708 if ((le32_to_cpu(sb->feature_map) &
1709 MD_FEATURE_RECOVERY_OFFSET))
1710 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1711 else
1712 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rdev->raid_disk = role;
1714 break;
1715 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001716 if (sb->devflags & WriteMostly1)
1717 set_bit(WriteMostly, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001718 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1719 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001720 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001721 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
NeilBrownfd01b882011-10-11 16:47:53 +11001726static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001729 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int max_dev, i;
1731 /* make rdev->sb match mddev and rdev data. */
1732
Namhyung Kim65a06f02011-07-27 11:00:36 +10001733 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 sb->feature_map = 0;
1736 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001737 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 memset(sb->pad3, 0, sizeof(sb->pad3));
1740
1741 sb->utime = cpu_to_le64((__u64)mddev->utime);
1742 sb->events = cpu_to_le64(mddev->events);
1743 if (mddev->in_sync)
1744 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1745 else
1746 sb->resync_offset = cpu_to_le64(0);
1747
NeilBrown1c05b4b2006-10-21 10:24:08 -07001748 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001749
NeilBrownf0ca3402006-02-02 14:28:04 -08001750 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001751 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001752 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001753 sb->level = cpu_to_le32(mddev->level);
1754 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001755
NeilBrownaeb9b212011-08-25 14:43:08 +10001756 if (test_bit(WriteMostly, &rdev->flags))
1757 sb->devflags |= WriteMostly1;
1758 else
1759 sb->devflags &= ~WriteMostly1;
1760
NeilBrownc3d97142009-12-14 12:49:52 +11001761 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1762 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001763 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001764 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001765
1766 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001767 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001768 sb->feature_map |=
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1770 sb->recovery_offset =
1771 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001772 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11001773 if (test_bit(Replacement, &rdev->flags))
1774 sb->feature_map |=
1775 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001776
NeilBrownf6705572006-03-27 01:18:11 -08001777 if (mddev->reshape_position != MaxSector) {
1778 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1779 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1780 sb->new_layout = cpu_to_le32(mddev->new_layout);
1781 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1782 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001783 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001784 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001785
NeilBrown2699b672011-07-28 11:31:47 +10001786 if (rdev->badblocks.count == 0)
1787 /* Nothing to do for bad blocks*/ ;
1788 else if (sb->bblog_offset == 0)
1789 /* Cannot record bad blocks on this device */
1790 md_error(mddev, rdev);
1791 else {
1792 struct badblocks *bb = &rdev->badblocks;
1793 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1794 u64 *p = bb->page;
1795 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1796 if (bb->changed) {
1797 unsigned seq;
1798
1799retry:
1800 seq = read_seqbegin(&bb->lock);
1801
1802 memset(bbp, 0xff, PAGE_SIZE);
1803
1804 for (i = 0 ; i < bb->count ; i++) {
1805 u64 internal_bb = *p++;
1806 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1807 | BB_LEN(internal_bb));
1808 *bbp++ = cpu_to_le64(store_bb);
1809 }
NeilBrownd0962932012-03-19 12:46:41 +11001810 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001811 if (read_seqretry(&bb->lock, seq))
1812 goto retry;
1813
1814 bb->sector = (rdev->sb_start +
1815 (int)le32_to_cpu(sb->bblog_offset));
1816 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001817 }
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001821 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rdev2->desc_nr+1 > max_dev)
1823 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001824
NeilBrown70471da2009-08-03 10:59:57 +10001825 if (max_dev > le32_to_cpu(sb->max_dev)) {
1826 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001827 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001828 rdev->sb_size = max_dev * 2 + 256;
1829 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1830 if (rdev->sb_size & bmask)
1831 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001832 } else
1833 max_dev = le32_to_cpu(sb->max_dev);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 for (i=0; i<max_dev;i++)
1836 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1837
NeilBrowndafb20f2012-03-19 12:46:39 +11001838 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001840 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001842 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001844 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001845 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 else
1847 sb->dev_roles[i] = cpu_to_le16(0xffff);
1848 }
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 sb->sb_csum = calc_sb_1_csum(sb);
1851}
1852
Chris Webb0cd17fe2008-06-28 08:31:46 +10001853static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001854super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855{
1856 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001857 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001858 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001859 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001860 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001862 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001863 max_sectors -= rdev->data_offset;
1864 if (!num_sectors || num_sectors > max_sectors)
1865 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001866 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001867 /* minor version 0 with bitmap we can't move */
1868 return 0;
1869 } else {
1870 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001871 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001872 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001873 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001874 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001875 if (!num_sectors || num_sectors > max_sectors)
1876 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001877 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001879 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001880 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001881 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001882 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001883 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884 rdev->sb_page);
1885 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001886 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Adrian Bunk75c96f82005-05-05 16:16:09 -07001889static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 [0] = {
1891 .name = "0.90.0",
1892 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001893 .load_super = super_90_load,
1894 .validate_super = super_90_validate,
1895 .sync_super = super_90_sync,
1896 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 },
1898 [1] = {
1899 .name = "md-1",
1900 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001901 .load_super = super_1_load,
1902 .validate_super = super_1_validate,
1903 .sync_super = super_1_sync,
1904 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 },
1906};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
NeilBrownfd01b882011-10-11 16:47:53 +11001908static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001909{
1910 if (mddev->sync_super) {
1911 mddev->sync_super(mddev, rdev);
1912 return;
1913 }
1914
1915 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1916
1917 super_types[mddev->major_version].sync_super(mddev, rdev);
1918}
1919
NeilBrownfd01b882011-10-11 16:47:53 +11001920static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
NeilBrown3cb03002011-10-11 16:45:26 +11001922 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown4b809912008-07-21 17:05:25 +10001924 rcu_read_lock();
1925 rdev_for_each_rcu(rdev, mddev1)
1926 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001927 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001928 rdev2->bdev->bd_contains) {
1929 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001930 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001931 }
1932 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
1936static LIST_HEAD(pending_raid_disks);
1937
Andre Nollac5e7112009-08-03 10:59:47 +10001938/*
1939 * Try to register data integrity profile for an mddev
1940 *
1941 * This is called when an array is started and after a disk has been kicked
1942 * from the array. It only succeeds if all working and active component devices
1943 * are integrity capable with matching profiles.
1944 */
NeilBrownfd01b882011-10-11 16:47:53 +11001945int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001946{
NeilBrown3cb03002011-10-11 16:45:26 +11001947 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001948
Andre Nollac5e7112009-08-03 10:59:47 +10001949 if (list_empty(&mddev->disks))
1950 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001951 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1952 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11001953 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10001954 /* skip spares and non-functional disks */
1955 if (test_bit(Faulty, &rdev->flags))
1956 continue;
1957 if (rdev->raid_disk < 0)
1958 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001959 if (!reference) {
1960 /* Use the first rdev as the reference */
1961 reference = rdev;
1962 continue;
1963 }
1964 /* does this rdev's profile match the reference profile? */
1965 if (blk_integrity_compare(reference->bdev->bd_disk,
1966 rdev->bdev->bd_disk) < 0)
1967 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001968 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001969 if (!reference || !bdev_get_integrity(reference->bdev))
1970 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001971 /*
1972 * All component devices are integrity capable and have matching
1973 * profiles, register the common profile for the md device.
1974 */
1975 if (blk_integrity_register(mddev->gendisk,
1976 bdev_get_integrity(reference->bdev)) != 0) {
1977 printk(KERN_ERR "md: failed to register integrity for %s\n",
1978 mdname(mddev));
1979 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001980 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001981 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1982 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1983 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1984 mdname(mddev));
1985 return -EINVAL;
1986 }
Andre Nollac5e7112009-08-03 10:59:47 +10001987 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001988}
Andre Nollac5e7112009-08-03 10:59:47 +10001989EXPORT_SYMBOL(md_integrity_register);
1990
1991/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001992void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001993{
1994 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1995 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1996
1997 if (!bi_mddev) /* nothing to do */
1998 return;
1999 if (rdev->raid_disk < 0) /* skip spares */
2000 return;
2001 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2002 rdev->bdev->bd_disk) >= 0)
2003 return;
2004 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2005 blk_integrity_unregister(mddev->gendisk);
2006}
2007EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002008
NeilBrownfd01b882011-10-11 16:47:53 +11002009static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08002011 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002012 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002013 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002014 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (rdev->mddev) {
2017 MD_BUG();
2018 return -EINVAL;
2019 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002020
2021 /* prevent duplicates */
2022 if (find_rdev(mddev, rdev->bdev->bd_dev))
2023 return -EEXIST;
2024
Andre Nolldd8ac332009-03-31 14:33:13 +11002025 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2026 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2027 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002028 if (mddev->pers) {
2029 /* Cannot change size, so fail
2030 * If mddev->level <= 0, then we don't care
2031 * about aligning sizes (e.g. linear)
2032 */
2033 if (mddev->level > 0)
2034 return -ENOSPC;
2035 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002036 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* Verify rdev->desc_nr is unique.
2040 * If it is -1, assign a free number, else
2041 * check number is not in use
2042 */
2043 if (rdev->desc_nr < 0) {
2044 int choice = 0;
2045 if (mddev->pers) choice = mddev->raid_disks;
2046 while (find_rdev_nr(mddev, choice))
2047 choice++;
2048 rdev->desc_nr = choice;
2049 } else {
2050 if (find_rdev_nr(mddev, rdev->desc_nr))
2051 return -EBUSY;
2052 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002053 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2054 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2055 mdname(mddev), mddev->max_disks);
2056 return -EBUSY;
2057 }
NeilBrown19133a42005-11-08 21:39:35 -08002058 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002059 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002060 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002063 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002064
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002065 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002066 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002067
Tejun Heo0762b8b2008-08-25 19:56:12 +09002068 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002069 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2070 /* failure here is OK */;
2071 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002072
NeilBrown4b809912008-07-21 17:05:25 +10002073 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002074 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002075
2076 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002077 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002080
2081 fail:
2082 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2083 b, mdname(mddev));
2084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
NeilBrown177a99b2008-02-06 01:39:56 -08002087static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002088{
NeilBrown3cb03002011-10-11 16:45:26 +11002089 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002090 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002091 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002092}
2093
NeilBrown3cb03002011-10-11 16:45:26 +11002094static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 char b[BDEVNAME_SIZE];
2097 if (!rdev->mddev) {
2098 MD_BUG();
2099 return;
2100 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002101 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002102 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2104 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002105 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002106 sysfs_put(rdev->sysfs_state);
2107 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002108 kfree(rdev->badblocks.page);
2109 rdev->badblocks.count = 0;
2110 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002111 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002112 * writing to 'remove' to "dev/state". We also need
2113 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002114 */
NeilBrown4b809912008-07-21 17:05:25 +10002115 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002116 INIT_WORK(&rdev->del_work, md_delayed_delete);
2117 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002118 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/*
2122 * prevent the device from being mounted, repartitioned or
2123 * otherwise reused by a RAID array (or any other kernel
2124 * subsystem), by bd_claiming the device.
2125 */
NeilBrown3cb03002011-10-11 16:45:26 +11002126static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int err = 0;
2129 struct block_device *bdev;
2130 char b[BDEVNAME_SIZE];
2131
Tejun Heod4d77622010-11-13 11:55:18 +01002132 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002133 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (IS_ERR(bdev)) {
2135 printk(KERN_ERR "md: could not open %s.\n",
2136 __bdevname(dev, b));
2137 return PTR_ERR(bdev);
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 rdev->bdev = bdev;
2140 return err;
2141}
2142
NeilBrown3cb03002011-10-11 16:45:26 +11002143static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct block_device *bdev = rdev->bdev;
2146 rdev->bdev = NULL;
2147 if (!bdev)
2148 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002149 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152void md_autodetect_dev(dev_t dev);
2153
NeilBrown3cb03002011-10-11 16:45:26 +11002154static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 char b[BDEVNAME_SIZE];
2157 printk(KERN_INFO "md: export_rdev(%s)\n",
2158 bdevname(rdev->bdev,b));
2159 if (rdev->mddev)
2160 MD_BUG();
2161 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002163 if (test_bit(AutoDetected, &rdev->flags))
2164 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165#endif
2166 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002167 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
NeilBrown3cb03002011-10-11 16:45:26 +11002170static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 unbind_rdev_from_array(rdev);
2173 export_rdev(rdev);
2174}
2175
NeilBrownfd01b882011-10-11 16:47:53 +11002176static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
NeilBrown3cb03002011-10-11 16:45:26 +11002178 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
NeilBrowndafb20f2012-03-19 12:46:39 +11002180 rdev_for_each_safe(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!rdev->mddev) {
2182 MD_BUG();
2183 continue;
2184 }
2185 kick_rdev_from_array(rdev);
2186 }
2187 if (!list_empty(&mddev->disks))
2188 MD_BUG();
2189 mddev->raid_disks = 0;
2190 mddev->major_version = 0;
2191}
2192
2193static void print_desc(mdp_disk_t *desc)
2194{
2195 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2196 desc->major,desc->minor,desc->raid_disk,desc->state);
2197}
2198
Cheng Renquancd2ac932009-01-09 08:31:08 +11002199static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 int i;
2202
2203 printk(KERN_INFO
2204 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2205 sb->major_version, sb->minor_version, sb->patch_version,
2206 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2207 sb->ctime);
2208 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2209 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2210 sb->md_minor, sb->layout, sb->chunk_size);
2211 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2212 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2213 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2214 sb->failed_disks, sb->spare_disks,
2215 sb->sb_csum, (unsigned long)sb->events_lo);
2216
2217 printk(KERN_INFO);
2218 for (i = 0; i < MD_SB_DISKS; i++) {
2219 mdp_disk_t *desc;
2220
2221 desc = sb->disks + i;
2222 if (desc->number || desc->major || desc->minor ||
2223 desc->raid_disk || (desc->state && (desc->state != 4))) {
2224 printk(" D %2d: ", i);
2225 print_desc(desc);
2226 }
2227 }
2228 printk(KERN_INFO "md: THIS: ");
2229 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Cheng Renquancd2ac932009-01-09 08:31:08 +11002232static void print_sb_1(struct mdp_superblock_1 *sb)
2233{
2234 __u8 *uuid;
2235
2236 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002237 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002238 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002239 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002240 le32_to_cpu(sb->major_version),
2241 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002242 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002243 sb->set_name,
2244 (unsigned long long)le64_to_cpu(sb->ctime)
2245 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2246
2247 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002248 printk(KERN_INFO
2249 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002250 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002251 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002252 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2253 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002254 le32_to_cpu(sb->level),
2255 (unsigned long long)le64_to_cpu(sb->size),
2256 le32_to_cpu(sb->raid_disks),
2257 le32_to_cpu(sb->layout),
2258 le32_to_cpu(sb->chunksize),
2259 (unsigned long long)le64_to_cpu(sb->data_offset),
2260 (unsigned long long)le64_to_cpu(sb->data_size),
2261 (unsigned long long)le64_to_cpu(sb->super_offset),
2262 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2263 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002264 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002265 sb->devflags,
2266 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2267 (unsigned long long)le64_to_cpu(sb->events),
2268 (unsigned long long)le64_to_cpu(sb->resync_offset),
2269 le32_to_cpu(sb->sb_csum),
2270 le32_to_cpu(sb->max_dev)
2271 );
2272}
2273
NeilBrown3cb03002011-10-11 16:45:26 +11002274static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002277 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2278 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002279 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2280 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002282 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2283 switch (major_version) {
2284 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002285 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002286 break;
2287 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002288 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002289 break;
2290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 } else
2292 printk(KERN_INFO "md: no rdev superblock!\n");
2293}
2294
Adrian Bunk5e563412006-06-26 00:27:42 -07002295static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002297 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002298 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002299 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 char b[BDEVNAME_SIZE];
2301
2302 printk("\n");
2303 printk("md: **********************************\n");
2304 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2305 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002306 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
NeilBrown32a76272005-06-21 17:17:14 -07002308 if (mddev->bitmap)
2309 bitmap_print_sb(mddev->bitmap);
2310 else
2311 printk("%s: ", mdname(mddev));
NeilBrowndafb20f2012-03-19 12:46:39 +11002312 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 printk("<%s>", bdevname(rdev->bdev,b));
2314 printk("\n");
2315
NeilBrowndafb20f2012-03-19 12:46:39 +11002316 rdev_for_each(rdev, mddev)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002317 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319 printk("md: **********************************\n");
2320 printk("\n");
2321}
2322
2323
NeilBrownfd01b882011-10-11 16:47:53 +11002324static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
NeilBrown42543762006-06-26 00:27:57 -07002326 /* Update each superblock (in-memory image), but
2327 * if we are allowed to, skip spares which already
2328 * have the right event counter, or have one earlier
2329 * (which would mean they aren't being marked as dirty
2330 * with the rest of the array)
2331 */
NeilBrown3cb03002011-10-11 16:45:26 +11002332 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002333 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002334 if (rdev->sb_events == mddev->events ||
2335 (nospares &&
2336 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002337 rdev->sb_events+1 == mddev->events)) {
2338 /* Don't update this superblock */
2339 rdev->sb_loaded = 2;
2340 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002341 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002342 rdev->sb_loaded = 1;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345}
2346
NeilBrownfd01b882011-10-11 16:47:53 +11002347static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
NeilBrown3cb03002011-10-11 16:45:26 +11002349 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002350 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002351 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002352 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002355 /* First make sure individual recovery_offsets are correct */
NeilBrowndafb20f2012-03-19 12:46:39 +11002356 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002357 if (rdev->raid_disk >= 0 &&
2358 mddev->delta_disks >= 0 &&
2359 !test_bit(In_sync, &rdev->flags) &&
2360 mddev->curr_resync_completed > rdev->recovery_offset)
2361 rdev->recovery_offset = mddev->curr_resync_completed;
2362
2363 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002364 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002365 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002366 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002367 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002368 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002369 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002370 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002371 rdev->badblocks.changed = 0;
NeilBrownde393cd2011-07-28 11:31:48 +10002372 md_ack_all_badblocks(&rdev->badblocks);
2373 md_error(mddev, rdev);
2374 }
2375 clear_bit(Blocked, &rdev->flags);
2376 clear_bit(BlockedBadBlocks, &rdev->flags);
2377 wake_up(&rdev->blocked_wait);
2378 }
2379 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002380 wake_up(&mddev->sb_wait);
2381 return;
2382 }
2383
NeilBrowna9701a32005-11-08 21:39:34 -08002384 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002385
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002386 mddev->utime = get_seconds();
2387
NeilBrown850b2b42006-10-03 01:15:46 -07002388 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2389 force_change = 1;
2390 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2391 /* just a clean<-> dirty transition, possibly leave spares alone,
2392 * though if events isn't the right even/odd, we will have to do
2393 * spares after all
2394 */
2395 nospares = 1;
2396 if (force_change)
2397 nospares = 0;
2398 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002399 /* If the array is degraded, then skipping spares is both
2400 * dangerous and fairly pointless.
2401 * Dangerous because a device that was removed from the array
2402 * might have a event_count that still looks up-to-date,
2403 * so it can be re-added without a resync.
2404 * Pointless because if there are any spares to skip,
2405 * then a recovery will happen and soon that array won't
2406 * be degraded any more and the spare can go back to sleep then.
2407 */
NeilBrown850b2b42006-10-03 01:15:46 -07002408 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002409
NeilBrown06d91a52005-06-21 17:17:12 -07002410 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002411
2412 /* If this is just a dirty<->clean transition, and the array is clean
2413 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002414 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002415 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002416 && mddev->can_decrease_events
2417 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002418 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002419 mddev->can_decrease_events = 0;
2420 } else {
NeilBrown42543762006-06-26 00:27:57 -07002421 /* otherwise we have to go forward and ... */
2422 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002423 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 if (!mddev->events) {
2427 /*
2428 * oops, this 64-bit counter should never wrap.
2429 * Either we are in around ~1 trillion A.C., assuming
2430 * 1 reboot per second, or we have a bug:
2431 */
2432 MD_BUG();
2433 mddev->events --;
2434 }
NeilBrown2699b672011-07-28 11:31:47 +10002435
NeilBrowndafb20f2012-03-19 12:46:39 +11002436 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002437 if (rdev->badblocks.changed)
2438 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002439 if (test_bit(Faulty, &rdev->flags))
2440 set_bit(FaultRecorded, &rdev->flags);
2441 }
NeilBrown2699b672011-07-28 11:31:47 +10002442
NeilBrowne6910632008-02-06 01:39:51 -08002443 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002444 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
NeilBrown36a4e1f2011-10-07 14:23:17 +11002446 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2447 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
NeilBrown4ad13662007-07-17 04:06:13 -07002449 bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002450 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002452
NeilBrown42543762006-06-26 00:27:57 -07002453 if (rdev->sb_loaded != 1)
2454 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002456 if (!test_bit(Faulty, &rdev->flags) &&
2457 rdev->saved_raid_disk == -1) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002458 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002459 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002460 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002461 pr_debug("md: (write) %s's sb offset: %llu\n",
2462 bdevname(rdev->bdev, b),
2463 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002464 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002465 if (rdev->badblocks.size) {
2466 md_super_write(mddev, rdev,
2467 rdev->badblocks.sector,
2468 rdev->badblocks.size << 9,
2469 rdev->bb_page);
2470 rdev->badblocks.size = 0;
2471 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002472
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002473 } else if (test_bit(Faulty, &rdev->flags))
NeilBrown36a4e1f2011-10-07 14:23:17 +11002474 pr_debug("md: %s (skipping faulty)\n",
2475 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002476 else
2477 pr_debug("(skipping incremental s/r ");
2478
NeilBrown7bfa19f2005-06-21 17:17:28 -07002479 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* only need to write one superblock... */
2481 break;
2482 }
NeilBrowna9701a32005-11-08 21:39:34 -08002483 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002484 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002485
NeilBrowna9701a32005-11-08 21:39:34 -08002486 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002487 if (mddev->in_sync != sync_req ||
2488 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002489 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002490 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002491 goto repeat;
2492 }
NeilBrown850b2b42006-10-03 01:15:46 -07002493 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002494 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002495 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002496 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2497 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002498
NeilBrowndafb20f2012-03-19 12:46:39 +11002499 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002500 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2501 clear_bit(Blocked, &rdev->flags);
2502
2503 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002504 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002505 clear_bit(BlockedBadBlocks, &rdev->flags);
2506 wake_up(&rdev->blocked_wait);
2507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Andre Noll7f6ce762008-03-23 18:34:54 +01002510/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002511 * We want to accept with case. For this we use cmd_match.
2512 */
2513static int cmd_match(const char *cmd, const char *str)
2514{
2515 /* See if cmd, written into a sysfs file, matches
2516 * str. They must either be the same, or cmd can
2517 * have a trailing newline
2518 */
2519 while (*cmd && *str && *cmd == *str) {
2520 cmd++;
2521 str++;
2522 }
2523 if (*cmd == '\n')
2524 cmd++;
2525 if (*str || *cmd)
2526 return 0;
2527 return 1;
2528}
2529
NeilBrown86e6ffd2005-11-08 21:39:24 -08002530struct rdev_sysfs_entry {
2531 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002532 ssize_t (*show)(struct md_rdev *, char *);
2533 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002534};
2535
2536static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002537state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002538{
2539 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002540 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002541
NeilBrownde393cd2011-07-28 11:31:48 +10002542 if (test_bit(Faulty, &rdev->flags) ||
2543 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002544 len+= sprintf(page+len, "%sfaulty",sep);
2545 sep = ",";
2546 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002547 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002548 len += sprintf(page+len, "%sin_sync",sep);
2549 sep = ",";
2550 }
NeilBrownf6556752006-06-26 00:28:01 -07002551 if (test_bit(WriteMostly, &rdev->flags)) {
2552 len += sprintf(page+len, "%swrite_mostly",sep);
2553 sep = ",";
2554 }
NeilBrownde393cd2011-07-28 11:31:48 +10002555 if (test_bit(Blocked, &rdev->flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002556 (rdev->badblocks.unacked_exist
2557 && !test_bit(Faulty, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002558 len += sprintf(page+len, "%sblocked", sep);
2559 sep = ",";
2560 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002561 if (!test_bit(Faulty, &rdev->flags) &&
2562 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002563 len += sprintf(page+len, "%sspare", sep);
2564 sep = ",";
2565 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002566 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2567 len += sprintf(page+len, "%swrite_error", sep);
2568 sep = ",";
2569 }
NeilBrown2d78f8c2011-12-23 10:17:51 +11002570 if (test_bit(WantReplacement, &rdev->flags)) {
2571 len += sprintf(page+len, "%swant_replacement", sep);
2572 sep = ",";
2573 }
2574 if (test_bit(Replacement, &rdev->flags)) {
2575 len += sprintf(page+len, "%sreplacement", sep);
2576 sep = ",";
2577 }
2578
NeilBrown86e6ffd2005-11-08 21:39:24 -08002579 return len+sprintf(page+len, "\n");
2580}
2581
NeilBrown45dc2de2006-06-26 00:27:58 -07002582static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002583state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002584{
2585 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002586 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002587 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002588 * writemostly - sets write_mostly
2589 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002590 * blocked - sets the Blocked flags
2591 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002592 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002593 * write_error - sets WriteErrorSeen
2594 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002595 */
2596 int err = -EINVAL;
2597 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2598 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002599 if (test_bit(Faulty, &rdev->flags))
2600 err = 0;
2601 else
2602 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002603 } else if (cmd_match(buf, "remove")) {
2604 if (rdev->raid_disk >= 0)
2605 err = -EBUSY;
2606 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002607 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002608 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002609 if (mddev->pers)
2610 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002611 md_new_event(mddev);
2612 err = 0;
2613 }
NeilBrownf6556752006-06-26 00:28:01 -07002614 } else if (cmd_match(buf, "writemostly")) {
2615 set_bit(WriteMostly, &rdev->flags);
2616 err = 0;
2617 } else if (cmd_match(buf, "-writemostly")) {
2618 clear_bit(WriteMostly, &rdev->flags);
2619 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002620 } else if (cmd_match(buf, "blocked")) {
2621 set_bit(Blocked, &rdev->flags);
2622 err = 0;
2623 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002624 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002625 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002626 /* metadata handler doesn't understand badblocks,
2627 * so we need to fail the device
2628 */
2629 md_error(rdev->mddev, rdev);
2630 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002631 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002632 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002633 wake_up(&rdev->blocked_wait);
2634 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2635 md_wakeup_thread(rdev->mddev->thread);
2636
2637 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002638 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2639 set_bit(In_sync, &rdev->flags);
2640 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002641 } else if (cmd_match(buf, "write_error")) {
2642 set_bit(WriteErrorSeen, &rdev->flags);
2643 err = 0;
2644 } else if (cmd_match(buf, "-write_error")) {
2645 clear_bit(WriteErrorSeen, &rdev->flags);
2646 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002647 } else if (cmd_match(buf, "want_replacement")) {
2648 /* Any non-spare device that is not a replacement can
2649 * become want_replacement at any time, but we then need to
2650 * check if recovery is needed.
2651 */
2652 if (rdev->raid_disk >= 0 &&
2653 !test_bit(Replacement, &rdev->flags))
2654 set_bit(WantReplacement, &rdev->flags);
2655 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2656 md_wakeup_thread(rdev->mddev->thread);
2657 err = 0;
2658 } else if (cmd_match(buf, "-want_replacement")) {
2659 /* Clearing 'want_replacement' is always allowed.
2660 * Once replacements starts it is too late though.
2661 */
2662 err = 0;
2663 clear_bit(WantReplacement, &rdev->flags);
2664 } else if (cmd_match(buf, "replacement")) {
2665 /* Can only set a device as a replacement when array has not
2666 * yet been started. Once running, replacement is automatic
2667 * from spares, or by assigning 'slot'.
2668 */
2669 if (rdev->mddev->pers)
2670 err = -EBUSY;
2671 else {
2672 set_bit(Replacement, &rdev->flags);
2673 err = 0;
2674 }
2675 } else if (cmd_match(buf, "-replacement")) {
2676 /* Similarly, can only clear Replacement before start */
2677 if (rdev->mddev->pers)
2678 err = -EBUSY;
2679 else {
2680 clear_bit(Replacement, &rdev->flags);
2681 err = 0;
2682 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002683 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002684 if (!err)
2685 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002686 return err ? err : len;
2687}
NeilBrown80ca3a42006-07-10 04:44:18 -07002688static struct rdev_sysfs_entry rdev_state =
2689__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002690
2691static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002692errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002693{
2694 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2695}
2696
2697static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002698errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002699{
2700 char *e;
2701 unsigned long n = simple_strtoul(buf, &e, 10);
2702 if (*buf && (*e == 0 || *e == '\n')) {
2703 atomic_set(&rdev->corrected_errors, n);
2704 return len;
2705 }
2706 return -EINVAL;
2707}
2708static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002709__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002710
NeilBrown014236d2006-01-06 00:20:55 -08002711static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002712slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002713{
2714 if (rdev->raid_disk < 0)
2715 return sprintf(page, "none\n");
2716 else
2717 return sprintf(page, "%d\n", rdev->raid_disk);
2718}
2719
2720static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002721slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002722{
2723 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002724 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002725 int slot = simple_strtoul(buf, &e, 10);
2726 if (strncmp(buf, "none", 4)==0)
2727 slot = -1;
2728 else if (e==buf || (*e && *e!= '\n'))
2729 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002730 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002731 /* Setting 'slot' on an active array requires also
2732 * updating the 'rd%d' link, and communicating
2733 * with the personality with ->hot_*_disk.
2734 * For now we only support removing
2735 * failed/spare devices. This normally happens automatically,
2736 * but not when the metadata is externally managed.
2737 */
NeilBrownc303da62008-02-06 01:39:51 -08002738 if (rdev->raid_disk == -1)
2739 return -EEXIST;
2740 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002741 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002742 return -EINVAL;
2743 err = rdev->mddev->pers->
NeilBrownb8321b62011-12-23 10:17:51 +11002744 hot_remove_disk(rdev->mddev, rdev);
NeilBrownc303da62008-02-06 01:39:51 -08002745 if (err)
2746 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002747 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002748 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002749 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2750 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002751 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002752 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002753 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002754 */
2755
2756 if (rdev->raid_disk != -1)
2757 return -EBUSY;
2758
NeilBrownc6751b22011-02-02 11:57:13 +11002759 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2760 return -EBUSY;
2761
Neil Brown6c2fce22008-06-28 08:31:31 +10002762 if (rdev->mddev->pers->hot_add_disk == NULL)
2763 return -EINVAL;
2764
NeilBrownba1b41b2011-01-14 09:14:34 +11002765 if (slot >= rdev->mddev->raid_disks &&
2766 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2767 return -ENOSPC;
2768
Neil Brown6c2fce22008-06-28 08:31:31 +10002769 rdev->raid_disk = slot;
2770 if (test_bit(In_sync, &rdev->flags))
2771 rdev->saved_raid_disk = slot;
2772 else
2773 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11002774 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002775 err = rdev->mddev->pers->
2776 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002777 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002778 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002779 return err;
Neil Brown52664732008-06-28 08:31:44 +10002780 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002781 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002782 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002783 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002784 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002785 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002786 if (slot >= rdev->mddev->raid_disks &&
2787 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002788 return -ENOSPC;
2789 rdev->raid_disk = slot;
2790 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002791 clear_bit(Faulty, &rdev->flags);
2792 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002793 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002794 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002795 }
NeilBrown014236d2006-01-06 00:20:55 -08002796 return len;
2797}
2798
2799
2800static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002801__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002802
NeilBrown93c8cad2006-01-06 00:20:56 -08002803static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002804offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002805{
Andrew Morton6961ece2006-01-06 00:20:59 -08002806 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002807}
2808
2809static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002810offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002811{
2812 char *e;
2813 unsigned long long offset = simple_strtoull(buf, &e, 10);
2814 if (e==buf || (*e && *e != '\n'))
2815 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002816 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002817 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002818 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002819 /* Must set offset before size, so overlap checks
2820 * can be sane */
2821 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002822 rdev->data_offset = offset;
2823 return len;
2824}
2825
2826static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002827__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002828
NeilBrown83303b62006-01-06 00:21:06 -08002829static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002830rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002831{
Andre Nolldd8ac332009-03-31 14:33:13 +11002832 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002833}
2834
NeilBrownc5d79ad2008-02-06 01:39:54 -08002835static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2836{
2837 /* check if two start/length pairs overlap */
2838 if (s1+l1 <= s2)
2839 return 0;
2840 if (s2+l2 <= s1)
2841 return 0;
2842 return 1;
2843}
2844
Dan Williamsb522adc2009-03-31 15:00:31 +11002845static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2846{
2847 unsigned long long blocks;
2848 sector_t new;
2849
2850 if (strict_strtoull(buf, 10, &blocks) < 0)
2851 return -EINVAL;
2852
2853 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2854 return -EINVAL; /* sector conversion overflow */
2855
2856 new = blocks * 2;
2857 if (new != blocks * 2)
2858 return -EINVAL; /* unsigned long long to sector_t overflow */
2859
2860 *sectors = new;
2861 return 0;
2862}
2863
NeilBrown83303b62006-01-06 00:21:06 -08002864static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002865rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002866{
NeilBrownfd01b882011-10-11 16:47:53 +11002867 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002868 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002869 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002870
Dan Williamsb522adc2009-03-31 15:00:31 +11002871 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002872 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002873 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002874 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002875 sectors = super_types[my_mddev->major_version].
2876 rdev_size_change(rdev, sectors);
2877 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002878 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002879 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002880 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002881 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002882 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002883 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002884 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002885
Andre Nolldd8ac332009-03-31 14:33:13 +11002886 rdev->sectors = sectors;
2887 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002888 /* need to check that all other rdevs with the same ->bdev
2889 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002890 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002891 * we have to change it back, we will have the lock again.
2892 */
NeilBrownfd01b882011-10-11 16:47:53 +11002893 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002894 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002895 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002896
NeilBrown27c529b2008-03-04 14:29:33 -08002897 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002898 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002899 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002900
2901 mddev_lock(mddev);
NeilBrowndafb20f2012-03-19 12:46:39 +11002902 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002903 if (rdev->bdev == rdev2->bdev &&
2904 rdev != rdev2 &&
2905 overlaps(rdev->data_offset, rdev->sectors,
2906 rdev2->data_offset,
2907 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002908 overlap = 1;
2909 break;
2910 }
2911 mddev_unlock(mddev);
2912 if (overlap) {
2913 mddev_put(mddev);
2914 break;
2915 }
2916 }
NeilBrown27c529b2008-03-04 14:29:33 -08002917 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002918 if (overlap) {
2919 /* Someone else could have slipped in a size
2920 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002921 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002922 * safe, and trust userspace not to race with
2923 * itself
2924 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002925 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002926 return -EBUSY;
2927 }
2928 }
NeilBrown83303b62006-01-06 00:21:06 -08002929 return len;
2930}
2931
2932static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002933__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002934
Dan Williams06e3c812009-12-12 21:17:12 -07002935
NeilBrown3cb03002011-10-11 16:45:26 +11002936static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002937{
2938 unsigned long long recovery_start = rdev->recovery_offset;
2939
2940 if (test_bit(In_sync, &rdev->flags) ||
2941 recovery_start == MaxSector)
2942 return sprintf(page, "none\n");
2943
2944 return sprintf(page, "%llu\n", recovery_start);
2945}
2946
NeilBrown3cb03002011-10-11 16:45:26 +11002947static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002948{
2949 unsigned long long recovery_start;
2950
2951 if (cmd_match(buf, "none"))
2952 recovery_start = MaxSector;
2953 else if (strict_strtoull(buf, 10, &recovery_start))
2954 return -EINVAL;
2955
2956 if (rdev->mddev->pers &&
2957 rdev->raid_disk >= 0)
2958 return -EBUSY;
2959
2960 rdev->recovery_offset = recovery_start;
2961 if (recovery_start == MaxSector)
2962 set_bit(In_sync, &rdev->flags);
2963 else
2964 clear_bit(In_sync, &rdev->flags);
2965 return len;
2966}
2967
2968static struct rdev_sysfs_entry rdev_recovery_start =
2969__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2970
NeilBrown16c791a2011-07-28 11:31:47 +10002971
2972static ssize_t
2973badblocks_show(struct badblocks *bb, char *page, int unack);
2974static ssize_t
2975badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2976
NeilBrown3cb03002011-10-11 16:45:26 +11002977static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002978{
2979 return badblocks_show(&rdev->badblocks, page, 0);
2980}
NeilBrown3cb03002011-10-11 16:45:26 +11002981static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002982{
NeilBrownde393cd2011-07-28 11:31:48 +10002983 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2984 /* Maybe that ack was all we needed */
2985 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2986 wake_up(&rdev->blocked_wait);
2987 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002988}
2989static struct rdev_sysfs_entry rdev_bad_blocks =
2990__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2991
2992
NeilBrown3cb03002011-10-11 16:45:26 +11002993static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002994{
2995 return badblocks_show(&rdev->badblocks, page, 1);
2996}
NeilBrown3cb03002011-10-11 16:45:26 +11002997static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002998{
2999 return badblocks_store(&rdev->badblocks, page, len, 1);
3000}
3001static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3002__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3003
NeilBrown86e6ffd2005-11-08 21:39:24 -08003004static struct attribute *rdev_default_attrs[] = {
3005 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003006 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003007 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003008 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003009 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003010 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003011 &rdev_bad_blocks.attr,
3012 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003013 NULL,
3014};
3015static ssize_t
3016rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3017{
3018 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003019 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11003020 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08003021 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003022
3023 if (!entry->show)
3024 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08003025
3026 rv = mddev ? mddev_lock(mddev) : -EBUSY;
3027 if (!rv) {
3028 if (rdev->mddev == NULL)
3029 rv = -EBUSY;
3030 else
3031 rv = entry->show(rdev, page);
3032 mddev_unlock(mddev);
3033 }
3034 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003035}
3036
3037static ssize_t
3038rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3039 const char *page, size_t length)
3040{
3041 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003042 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003043 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003044 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003045
3046 if (!entry->store)
3047 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003048 if (!capable(CAP_SYS_ADMIN))
3049 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003050 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003051 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003052 if (rdev->mddev == NULL)
3053 rv = -EBUSY;
3054 else
3055 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003056 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003057 }
3058 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003059}
3060
3061static void rdev_free(struct kobject *ko)
3062{
NeilBrown3cb03002011-10-11 16:45:26 +11003063 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003064 kfree(rdev);
3065}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003066static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003067 .show = rdev_attr_show,
3068 .store = rdev_attr_store,
3069};
3070static struct kobj_type rdev_ktype = {
3071 .release = rdev_free,
3072 .sysfs_ops = &rdev_sysfs_ops,
3073 .default_attrs = rdev_default_attrs,
3074};
3075
NeilBrown3cb03002011-10-11 16:45:26 +11003076int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003077{
3078 rdev->desc_nr = -1;
3079 rdev->saved_raid_disk = -1;
3080 rdev->raid_disk = -1;
3081 rdev->flags = 0;
3082 rdev->data_offset = 0;
3083 rdev->sb_events = 0;
3084 rdev->last_read_error.tv_sec = 0;
3085 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003086 rdev->sb_loaded = 0;
3087 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003088 atomic_set(&rdev->nr_pending, 0);
3089 atomic_set(&rdev->read_errors, 0);
3090 atomic_set(&rdev->corrected_errors, 0);
3091
3092 INIT_LIST_HEAD(&rdev->same_set);
3093 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003094
3095 /* Add space to store bad block list.
3096 * This reserves the space even on arrays where it cannot
3097 * be used - I wonder if that matters
3098 */
3099 rdev->badblocks.count = 0;
3100 rdev->badblocks.shift = 0;
3101 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3102 seqlock_init(&rdev->badblocks.lock);
3103 if (rdev->badblocks.page == NULL)
3104 return -ENOMEM;
3105
3106 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003107}
3108EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109/*
3110 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3111 *
3112 * mark the device faulty if:
3113 *
3114 * - the device is nonexistent (zero size)
3115 * - the device has no valid superblock
3116 *
3117 * a faulty rdev _never_ has rdev->sb set.
3118 */
NeilBrown3cb03002011-10-11 16:45:26 +11003119static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 char b[BDEVNAME_SIZE];
3122 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003123 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 sector_t size;
3125
NeilBrown9ffae0c2006-01-06 00:20:32 -08003126 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (!rdev) {
3128 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3129 return ERR_PTR(-ENOMEM);
3130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
NeilBrown2230dfe2011-07-28 11:31:46 +10003132 err = md_rdev_init(rdev);
3133 if (err)
3134 goto abort_free;
3135 err = alloc_disk_sb(rdev);
3136 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 goto abort_free;
3138
NeilBrownc5d79ad2008-02-06 01:39:54 -08003139 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (err)
3141 goto abort_free;
3142
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003143 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003144
Mike Snitzer77304d22010-11-08 14:39:12 +01003145 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (!size) {
3147 printk(KERN_WARNING
3148 "md: %s has zero or unknown size, marking faulty!\n",
3149 bdevname(rdev->bdev,b));
3150 err = -EINVAL;
3151 goto abort_free;
3152 }
3153
3154 if (super_format >= 0) {
3155 err = super_types[super_format].
3156 load_super(rdev, NULL, super_minor);
3157 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003158 printk(KERN_WARNING
3159 "md: %s does not have a valid v%d.%d "
3160 "superblock, not importing!\n",
3161 bdevname(rdev->bdev,b),
3162 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 goto abort_free;
3164 }
3165 if (err < 0) {
3166 printk(KERN_WARNING
3167 "md: could not read %s's sb, not importing!\n",
3168 bdevname(rdev->bdev,b));
3169 goto abort_free;
3170 }
3171 }
NeilBrown9f2f383072011-07-28 11:31:47 +10003172 if (super_format == -1)
3173 /* hot-add for 0.90, or non-persistent: so no badblocks */
3174 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return rdev;
3177
3178abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003179 if (rdev->bdev)
3180 unlock_rdev(rdev);
3181 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003182 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 kfree(rdev);
3184 return ERR_PTR(err);
3185}
3186
3187/*
3188 * Check a full RAID array for plausibility
3189 */
3190
3191
NeilBrownfd01b882011-10-11 16:47:53 +11003192static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003195 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 char b[BDEVNAME_SIZE];
3197
3198 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003199 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 switch (super_types[mddev->major_version].
3201 load_super(rdev, freshest, mddev->minor_version)) {
3202 case 1:
3203 freshest = rdev;
3204 break;
3205 case 0:
3206 break;
3207 default:
3208 printk( KERN_ERR \
3209 "md: fatal superblock inconsistency in %s"
3210 " -- removing from array\n",
3211 bdevname(rdev->bdev,b));
3212 kick_rdev_from_array(rdev);
3213 }
3214
3215
3216 super_types[mddev->major_version].
3217 validate_super(mddev, freshest);
3218
3219 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003220 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003221 if (mddev->max_disks &&
3222 (rdev->desc_nr >= mddev->max_disks ||
3223 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003224 printk(KERN_WARNING
3225 "md: %s: %s: only %d devices permitted\n",
3226 mdname(mddev), bdevname(rdev->bdev, b),
3227 mddev->max_disks);
3228 kick_rdev_from_array(rdev);
3229 continue;
3230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (rdev != freshest)
3232 if (super_types[mddev->major_version].
3233 validate_super(mddev, rdev)) {
3234 printk(KERN_WARNING "md: kicking non-fresh %s"
3235 " from array!\n",
3236 bdevname(rdev->bdev,b));
3237 kick_rdev_from_array(rdev);
3238 continue;
3239 }
3240 if (mddev->level == LEVEL_MULTIPATH) {
3241 rdev->desc_nr = i++;
3242 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003243 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003244 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003245 rdev->raid_disk = -1;
3246 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
3248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
NeilBrown72e02072009-12-14 12:49:55 +11003251/* Read a fixed-point number.
3252 * Numbers in sysfs attributes should be in "standard" units where
3253 * possible, so time should be in seconds.
3254 * However we internally use a a much smaller unit such as
3255 * milliseconds or jiffies.
3256 * This function takes a decimal number with a possible fractional
3257 * component, and produces an integer which is the result of
3258 * multiplying that number by 10^'scale'.
3259 * all without any floating-point arithmetic.
3260 */
3261int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3262{
3263 unsigned long result = 0;
3264 long decimals = -1;
3265 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3266 if (*cp == '.')
3267 decimals = 0;
3268 else if (decimals < scale) {
3269 unsigned int value;
3270 value = *cp - '0';
3271 result = result * 10 + value;
3272 if (decimals >= 0)
3273 decimals++;
3274 }
3275 cp++;
3276 }
3277 if (*cp == '\n')
3278 cp++;
3279 if (*cp)
3280 return -EINVAL;
3281 if (decimals < 0)
3282 decimals = 0;
3283 while (decimals < scale) {
3284 result *= 10;
3285 decimals ++;
3286 }
3287 *res = result;
3288 return 0;
3289}
3290
3291
NeilBrown19052c02008-08-05 15:54:13 +10003292static void md_safemode_timeout(unsigned long data);
3293
NeilBrowneae17012005-11-08 21:39:23 -08003294static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003295safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003296{
3297 int msec = (mddev->safemode_delay*1000)/HZ;
3298 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3299}
3300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003301safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003302{
NeilBrown16f17b32006-06-26 00:27:37 -07003303 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003304
NeilBrown72e02072009-12-14 12:49:55 +11003305 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003306 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003307 if (msec == 0)
3308 mddev->safemode_delay = 0;
3309 else {
NeilBrown19052c02008-08-05 15:54:13 +10003310 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003311 mddev->safemode_delay = (msec*HZ)/1000;
3312 if (mddev->safemode_delay == 0)
3313 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003314 if (mddev->safemode_delay < old_delay)
3315 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003316 }
3317 return len;
3318}
3319static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003320__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003321
3322static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003323level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003324{
NeilBrown84fc4b52011-10-11 16:49:58 +11003325 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003326 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003327 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003328 else if (mddev->clevel[0])
3329 return sprintf(page, "%s\n", mddev->clevel);
3330 else if (mddev->level != LEVEL_NONE)
3331 return sprintf(page, "%d\n", mddev->level);
3332 else
3333 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003334}
3335
NeilBrownd9d166c2006-01-06 00:20:51 -08003336static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003337level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003338{
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003340 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003341 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003342 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003343 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003344 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003345
3346 if (mddev->pers == NULL) {
3347 if (len == 0)
3348 return 0;
3349 if (len >= sizeof(mddev->clevel))
3350 return -ENOSPC;
3351 strncpy(mddev->clevel, buf, len);
3352 if (mddev->clevel[len-1] == '\n')
3353 len--;
3354 mddev->clevel[len] = 0;
3355 mddev->level = LEVEL_NONE;
3356 return rv;
3357 }
3358
3359 /* request to change the personality. Need to ensure:
3360 * - array is not engaged in resync/recovery/reshape
3361 * - old personality can be suspended
3362 * - new personality will access other array.
3363 */
3364
NeilBrownbb4f1e92010-08-08 21:18:03 +10003365 if (mddev->sync_thread ||
3366 mddev->reshape_position != MaxSector ||
3367 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003368 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003369
3370 if (!mddev->pers->quiesce) {
3371 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3372 mdname(mddev), mddev->pers->name);
3373 return -EINVAL;
3374 }
3375
3376 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003377 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003378 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003379 strncpy(clevel, buf, len);
3380 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003381 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003382 clevel[len] = 0;
3383 if (strict_strtol(clevel, 10, &level))
3384 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003385
Dan Williamsf2859af2010-05-02 10:04:16 -07003386 if (request_module("md-%s", clevel) != 0)
3387 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003388 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003389 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003390 if (!pers || !try_module_get(pers->owner)) {
3391 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003392 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003393 return -EINVAL;
3394 }
3395 spin_unlock(&pers_lock);
3396
3397 if (pers == mddev->pers) {
3398 /* Nothing to do! */
3399 module_put(pers->owner);
3400 return rv;
3401 }
3402 if (!pers->takeover) {
3403 module_put(pers->owner);
3404 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003405 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003406 return -EINVAL;
3407 }
3408
NeilBrowndafb20f2012-03-19 12:46:39 +11003409 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003410 rdev->new_raid_disk = rdev->raid_disk;
3411
NeilBrown245f46c2009-03-31 14:39:39 +11003412 /* ->takeover must set new_* and/or delta_disks
3413 * if it succeeds, and may set them when it fails.
3414 */
3415 priv = pers->takeover(mddev);
3416 if (IS_ERR(priv)) {
3417 mddev->new_level = mddev->level;
3418 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003419 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003420 mddev->raid_disks -= mddev->delta_disks;
3421 mddev->delta_disks = 0;
3422 module_put(pers->owner);
3423 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003424 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003425 return PTR_ERR(priv);
3426 }
3427
3428 /* Looks like we have a winner */
3429 mddev_suspend(mddev);
3430 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003431
3432 if (mddev->pers->sync_request == NULL &&
3433 pers->sync_request != NULL) {
3434 /* need to add the md_redundancy_group */
3435 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3436 printk(KERN_WARNING
3437 "md: cannot register extra attributes for %s\n",
3438 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003439 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003440 }
3441 if (mddev->pers->sync_request != NULL &&
3442 pers->sync_request == NULL) {
3443 /* need to remove the md_redundancy_group */
3444 if (mddev->to_remove == NULL)
3445 mddev->to_remove = &md_redundancy_group;
3446 }
3447
Trela Maciej54071b32010-03-08 16:02:42 +11003448 if (mddev->pers->sync_request == NULL &&
3449 mddev->external) {
3450 /* We are converting from a no-redundancy array
3451 * to a redundancy array and metadata is managed
3452 * externally so we need to be sure that writes
3453 * won't block due to a need to transition
3454 * clean->dirty
3455 * until external management is started.
3456 */
3457 mddev->in_sync = 0;
3458 mddev->safemode_delay = 0;
3459 mddev->safemode = 0;
3460 }
3461
NeilBrowndafb20f2012-03-19 12:46:39 +11003462 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003463 if (rdev->raid_disk < 0)
3464 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003465 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003466 rdev->new_raid_disk = -1;
3467 if (rdev->new_raid_disk == rdev->raid_disk)
3468 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003469 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003470 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003471 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003472 if (rdev->raid_disk < 0)
3473 continue;
3474 if (rdev->new_raid_disk == rdev->raid_disk)
3475 continue;
3476 rdev->raid_disk = rdev->new_raid_disk;
3477 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003478 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003479 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003480 if (sysfs_link_rdev(mddev, rdev))
3481 printk(KERN_WARNING "md: cannot register rd%d"
3482 " for %s after level change\n",
3483 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003484 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003485 }
3486
3487 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003488 mddev->pers = pers;
3489 mddev->private = priv;
3490 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3491 mddev->level = mddev->new_level;
3492 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003493 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003494 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003495 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003496 if (mddev->pers->sync_request == NULL) {
3497 /* this is now an array without redundancy, so
3498 * it must always be in_sync
3499 */
3500 mddev->in_sync = 1;
3501 del_timer_sync(&mddev->safemode_timer);
3502 }
NeilBrown245f46c2009-03-31 14:39:39 +11003503 pers->run(mddev);
3504 mddev_resume(mddev);
3505 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3506 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3507 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003508 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003509 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003510 return rv;
3511}
3512
3513static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003514__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003515
NeilBrownd4dbd022006-06-26 00:27:59 -07003516
3517static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003518layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003519{
3520 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003521 if (mddev->reshape_position != MaxSector &&
3522 mddev->layout != mddev->new_layout)
3523 return sprintf(page, "%d (%d)\n",
3524 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003525 return sprintf(page, "%d\n", mddev->layout);
3526}
3527
3528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003529layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003530{
3531 char *e;
3532 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003533
3534 if (!*buf || (*e && *e != '\n'))
3535 return -EINVAL;
3536
NeilBrownb3546032009-03-31 14:56:41 +11003537 if (mddev->pers) {
3538 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003539 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003540 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003541 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003542 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003543 if (err) {
3544 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003545 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003546 }
NeilBrownb3546032009-03-31 14:56:41 +11003547 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003548 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003549 if (mddev->reshape_position == MaxSector)
3550 mddev->layout = n;
3551 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003552 return len;
3553}
3554static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003555__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003556
3557
NeilBrowneae17012005-11-08 21:39:23 -08003558static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003559raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003560{
NeilBrownbb636542005-11-08 21:39:45 -08003561 if (mddev->raid_disks == 0)
3562 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003563 if (mddev->reshape_position != MaxSector &&
3564 mddev->delta_disks != 0)
3565 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3566 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003567 return sprintf(page, "%d\n", mddev->raid_disks);
3568}
3569
NeilBrownfd01b882011-10-11 16:47:53 +11003570static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b92006-01-06 00:20:54 -08003571
3572static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003573raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b92006-01-06 00:20:54 -08003574{
NeilBrownda943b92006-01-06 00:20:54 -08003575 char *e;
3576 int rv = 0;
3577 unsigned long n = simple_strtoul(buf, &e, 10);
3578
3579 if (!*buf || (*e && *e != '\n'))
3580 return -EINVAL;
3581
3582 if (mddev->pers)
3583 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003584 else if (mddev->reshape_position != MaxSector) {
3585 int olddisks = mddev->raid_disks - mddev->delta_disks;
3586 mddev->delta_disks = n - olddisks;
3587 mddev->raid_disks = n;
3588 } else
NeilBrownda943b92006-01-06 00:20:54 -08003589 mddev->raid_disks = n;
3590 return rv ? rv : len;
3591}
3592static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003593__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003594
NeilBrown24dd4692005-11-08 21:39:26 -08003595static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003596chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003597{
NeilBrown08a02ec2007-05-09 02:35:38 -07003598 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003599 mddev->chunk_sectors != mddev->new_chunk_sectors)
3600 return sprintf(page, "%d (%d)\n",
3601 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003602 mddev->chunk_sectors << 9);
3603 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003604}
3605
3606static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003607chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003608{
NeilBrown3b343802006-01-06 00:20:47 -08003609 char *e;
3610 unsigned long n = simple_strtoul(buf, &e, 10);
3611
NeilBrown3b343802006-01-06 00:20:47 -08003612 if (!*buf || (*e && *e != '\n'))
3613 return -EINVAL;
3614
NeilBrownb3546032009-03-31 14:56:41 +11003615 if (mddev->pers) {
3616 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003617 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003618 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003619 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003620 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003621 if (err) {
3622 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003623 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003624 }
NeilBrownb3546032009-03-31 14:56:41 +11003625 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003626 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003627 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003628 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003629 }
NeilBrown3b343802006-01-06 00:20:47 -08003630 return len;
3631}
3632static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003633__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003634
NeilBrowna94213b2006-06-26 00:28:00 -07003635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003636resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003637{
NeilBrownd1a7c502009-03-31 15:24:32 +11003638 if (mddev->recovery_cp == MaxSector)
3639 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003640 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3641}
3642
3643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003644resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003645{
NeilBrowna94213b2006-06-26 00:28:00 -07003646 char *e;
3647 unsigned long long n = simple_strtoull(buf, &e, 10);
3648
NeilBrownb0986362011-05-11 15:52:21 +10003649 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003650 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003651 if (cmd_match(buf, "none"))
3652 n = MaxSector;
3653 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003654 return -EINVAL;
3655
3656 mddev->recovery_cp = n;
3657 return len;
3658}
3659static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003660__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003661
NeilBrown9e653b62006-06-26 00:27:58 -07003662/*
3663 * The array state can be:
3664 *
3665 * clear
3666 * No devices, no size, no level
3667 * Equivalent to STOP_ARRAY ioctl
3668 * inactive
3669 * May have some settings, but array is not active
3670 * all IO results in error
3671 * When written, doesn't tear down array, but just stops it
3672 * suspended (not supported yet)
3673 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003674 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003675 * readonly
3676 * no resync can happen. no superblocks get written.
3677 * write requests fail
3678 * read-auto
3679 * like readonly, but behaves like 'clean' on a write request.
3680 *
3681 * clean - no pending writes, but otherwise active.
3682 * When written to inactive array, starts without resync
3683 * If a write request arrives then
3684 * if metadata is known, mark 'dirty' and switch to 'active'.
3685 * if not known, block and switch to write-pending
3686 * If written to an active array that has pending writes, then fails.
3687 * active
3688 * fully active: IO and resync can be happening.
3689 * When written to inactive array, starts with resync
3690 *
3691 * write-pending
3692 * clean, but writes are blocked waiting for 'active' to be written.
3693 *
3694 * active-idle
3695 * like active, but no writes have been seen for a while (100msec).
3696 *
3697 */
3698enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3699 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003700static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003701 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3702 "write-pending", "active-idle", NULL };
3703
3704static int match_word(const char *word, char **list)
3705{
3706 int n;
3707 for (n=0; list[n]; n++)
3708 if (cmd_match(word, list[n]))
3709 break;
3710 return n;
3711}
3712
3713static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003714array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003715{
3716 enum array_state st = inactive;
3717
3718 if (mddev->pers)
3719 switch(mddev->ro) {
3720 case 1:
3721 st = readonly;
3722 break;
3723 case 2:
3724 st = read_auto;
3725 break;
3726 case 0:
3727 if (mddev->in_sync)
3728 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003729 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003730 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003731 else if (mddev->safemode)
3732 st = active_idle;
3733 else
3734 st = active;
3735 }
3736 else {
3737 if (list_empty(&mddev->disks) &&
3738 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003739 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003740 st = clear;
3741 else
3742 st = inactive;
3743 }
3744 return sprintf(page, "%s\n", array_states[st]);
3745}
3746
NeilBrownfd01b882011-10-11 16:47:53 +11003747static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3748static int md_set_readonly(struct mddev * mddev, int is_open);
3749static int do_md_run(struct mddev * mddev);
3750static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003751
3752static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003753array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003754{
3755 int err = -EINVAL;
3756 enum array_state st = match_word(buf, array_states);
3757 switch(st) {
3758 case bad_word:
3759 break;
3760 case clear:
3761 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003762 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003763 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003764 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003765 break;
3766 case inactive:
3767 /* stopping an active array */
3768 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003769 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003770 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003771 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003772 } else
3773 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003774 break;
3775 case suspended:
3776 break; /* not supported yet */
3777 case readonly:
3778 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003779 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003780 else {
3781 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003782 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003783 err = do_md_run(mddev);
3784 }
3785 break;
3786 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003787 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003788 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003789 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003790 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003791 err = restart_array(mddev);
3792 if (err == 0) {
3793 mddev->ro = 2;
3794 set_disk_ro(mddev->gendisk, 0);
3795 }
NeilBrown9e653b62006-06-26 00:27:58 -07003796 } else {
3797 mddev->ro = 2;
3798 err = do_md_run(mddev);
3799 }
3800 break;
3801 case clean:
3802 if (mddev->pers) {
3803 restart_array(mddev);
3804 spin_lock_irq(&mddev->write_lock);
3805 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003806 if (mddev->in_sync == 0) {
3807 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003808 if (mddev->safemode == 1)
3809 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003810 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003811 }
3812 err = 0;
3813 } else
3814 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003815 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003816 } else
3817 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003818 break;
3819 case active:
3820 if (mddev->pers) {
3821 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003822 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003823 wake_up(&mddev->sb_wait);
3824 err = 0;
3825 } else {
3826 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003827 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003828 err = do_md_run(mddev);
3829 }
3830 break;
3831 case write_pending:
3832 case active_idle:
3833 /* these cannot be set */
3834 break;
3835 }
3836 if (err)
3837 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003838 else {
NeilBrown1d23f172011-12-08 15:49:12 +11003839 if (mddev->hold_active == UNTIL_IOCTL)
3840 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10003841 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003842 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003843 }
NeilBrown9e653b62006-06-26 00:27:58 -07003844}
NeilBrown80ca3a42006-07-10 04:44:18 -07003845static struct md_sysfs_entry md_array_state =
3846__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003847
NeilBrown6d7ff732006-01-06 00:21:16 -08003848static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003849max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003850 return sprintf(page, "%d\n",
3851 atomic_read(&mddev->max_corr_read_errors));
3852}
3853
3854static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003855max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003856{
3857 char *e;
3858 unsigned long n = simple_strtoul(buf, &e, 10);
3859
3860 if (*buf && (*e == 0 || *e == '\n')) {
3861 atomic_set(&mddev->max_corr_read_errors, n);
3862 return len;
3863 }
3864 return -EINVAL;
3865}
3866
3867static struct md_sysfs_entry max_corr_read_errors =
3868__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3869 max_corrected_read_errors_store);
3870
3871static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003872null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003873{
3874 return -EINVAL;
3875}
3876
3877static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003878new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003879{
3880 /* buf must be %d:%d\n? giving major and minor numbers */
3881 /* The new device is added to the array.
3882 * If the array has a persistent superblock, we read the
3883 * superblock to initialise info and check validity.
3884 * Otherwise, only checking done is that in bind_rdev_to_array,
3885 * which mainly checks size.
3886 */
3887 char *e;
3888 int major = simple_strtoul(buf, &e, 10);
3889 int minor;
3890 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003891 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003892 int err;
3893
3894 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3895 return -EINVAL;
3896 minor = simple_strtoul(e+1, &e, 10);
3897 if (*e && *e != '\n')
3898 return -EINVAL;
3899 dev = MKDEV(major, minor);
3900 if (major != MAJOR(dev) ||
3901 minor != MINOR(dev))
3902 return -EOVERFLOW;
3903
3904
3905 if (mddev->persistent) {
3906 rdev = md_import_device(dev, mddev->major_version,
3907 mddev->minor_version);
3908 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003909 struct md_rdev *rdev0
3910 = list_entry(mddev->disks.next,
3911 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003912 err = super_types[mddev->major_version]
3913 .load_super(rdev, rdev0, mddev->minor_version);
3914 if (err < 0)
3915 goto out;
3916 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003917 } else if (mddev->external)
3918 rdev = md_import_device(dev, -2, -1);
3919 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003920 rdev = md_import_device(dev, -1, -1);
3921
3922 if (IS_ERR(rdev))
3923 return PTR_ERR(rdev);
3924 err = bind_rdev_to_array(rdev, mddev);
3925 out:
3926 if (err)
3927 export_rdev(rdev);
3928 return err ? err : len;
3929}
3930
3931static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003932__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003933
3934static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003935bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003936{
3937 char *end;
3938 unsigned long chunk, end_chunk;
3939
3940 if (!mddev->bitmap)
3941 goto out;
3942 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3943 while (*buf) {
3944 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3945 if (buf == end) break;
3946 if (*end == '-') { /* range */
3947 buf = end + 1;
3948 end_chunk = simple_strtoul(buf, &end, 0);
3949 if (buf == end) break;
3950 }
3951 if (*end && !isspace(*end)) break;
3952 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003953 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003954 }
3955 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3956out:
3957 return len;
3958}
3959
3960static struct md_sysfs_entry md_bitmap =
3961__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3962
3963static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003964size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003965{
Andre Noll58c0fed2009-03-31 14:33:13 +11003966 return sprintf(page, "%llu\n",
3967 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003968}
3969
NeilBrownfd01b882011-10-11 16:47:53 +11003970static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003971
3972static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003973size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003974{
3975 /* If array is inactive, we can reduce the component size, but
3976 * not increase it (except from 0).
3977 * If array is active, we can try an on-line resize
3978 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003979 sector_t sectors;
3980 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003981
Andre Noll58c0fed2009-03-31 14:33:13 +11003982 if (err < 0)
3983 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003984 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003985 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003986 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003987 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003988 if (mddev->dev_sectors == 0 ||
3989 mddev->dev_sectors > sectors)
3990 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003991 else
3992 err = -ENOSPC;
3993 }
3994 return err ? err : len;
3995}
3996
3997static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003998__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003999
NeilBrown8bb93aa2006-01-06 00:20:50 -08004000
4001/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08004002 * This is one of
4003 * 'none' for arrays with no metadata (good luck...)
4004 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004005 * or N.M for internally known formats
4006 */
4007static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004008metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004009{
4010 if (mddev->persistent)
4011 return sprintf(page, "%d.%d\n",
4012 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004013 else if (mddev->external)
4014 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004015 else
4016 return sprintf(page, "none\n");
4017}
4018
4019static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004020metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004021{
4022 int major, minor;
4023 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11004024 /* Changing the details of 'external' metadata is
4025 * always permitted. Otherwise there must be
4026 * no devices attached to the array.
4027 */
4028 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4029 ;
4030 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08004031 return -EBUSY;
4032
4033 if (cmd_match(buf, "none")) {
4034 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004035 mddev->external = 0;
4036 mddev->major_version = 0;
4037 mddev->minor_version = 90;
4038 return len;
4039 }
4040 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004041 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004042 if (namelen >= sizeof(mddev->metadata_type))
4043 namelen = sizeof(mddev->metadata_type)-1;
4044 strncpy(mddev->metadata_type, buf+9, namelen);
4045 mddev->metadata_type[namelen] = 0;
4046 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4047 mddev->metadata_type[--namelen] = 0;
4048 mddev->persistent = 0;
4049 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004050 mddev->major_version = 0;
4051 mddev->minor_version = 90;
4052 return len;
4053 }
4054 major = simple_strtoul(buf, &e, 10);
4055 if (e==buf || *e != '.')
4056 return -EINVAL;
4057 buf = e+1;
4058 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004059 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004060 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004061 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004062 return -ENOENT;
4063 mddev->major_version = major;
4064 mddev->minor_version = minor;
4065 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004066 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004067 return len;
4068}
4069
4070static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004071__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004072
NeilBrowna35b0d62006-01-06 00:20:49 -08004073static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004074action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004075{
NeilBrown7eec3142005-11-08 21:39:44 -08004076 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004077 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4078 type = "frozen";
4079 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004080 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004081 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4082 type = "reshape";
4083 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004084 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4085 type = "resync";
4086 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4087 type = "check";
4088 else
4089 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004090 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004091 type = "recover";
4092 }
4093 return sprintf(page, "%s\n", type);
4094}
4095
NeilBrownfd01b882011-10-11 16:47:53 +11004096static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004097
NeilBrown24dd4692005-11-08 21:39:26 -08004098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004099action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004100{
NeilBrown7eec3142005-11-08 21:39:44 -08004101 if (!mddev->pers || !mddev->pers->sync_request)
4102 return -EINVAL;
4103
NeilBrownb6a9ce62009-05-26 09:41:17 +10004104 if (cmd_match(page, "frozen"))
4105 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4106 else
4107 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4108
4109 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004110 if (mddev->sync_thread) {
4111 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004112 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004113 }
NeilBrown03c902e2006-01-06 00:20:46 -08004114 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4115 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004116 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004117 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004119 else if (cmd_match(page, "recover")) {
4120 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4121 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4122 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004123 int err;
4124 if (mddev->pers->start_reshape == NULL)
4125 return -EINVAL;
4126 err = mddev->pers->start_reshape(mddev);
4127 if (err)
4128 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004129 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004130 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004131 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004132 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004133 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004134 return -EINVAL;
4135 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4136 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004137 }
NeilBrown03c902e2006-01-06 00:20:46 -08004138 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004139 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004140 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004141 return len;
4142}
4143
NeilBrown9d888832005-11-08 21:39:26 -08004144static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004145mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004146{
4147 return sprintf(page, "%llu\n",
4148 (unsigned long long) mddev->resync_mismatches);
4149}
4150
NeilBrown80ca3a42006-07-10 04:44:18 -07004151static struct md_sysfs_entry md_scan_mode =
4152__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004153
NeilBrown96de1e62005-11-08 21:39:39 -08004154
NeilBrown80ca3a42006-07-10 04:44:18 -07004155static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004156
NeilBrown88202a02006-01-06 00:21:36 -08004157static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004158sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004159{
4160 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4161 mddev->sync_speed_min ? "local": "system");
4162}
4163
4164static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004165sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004166{
4167 int min;
4168 char *e;
4169 if (strncmp(buf, "system", 6)==0) {
4170 mddev->sync_speed_min = 0;
4171 return len;
4172 }
4173 min = simple_strtoul(buf, &e, 10);
4174 if (buf == e || (*e && *e != '\n') || min <= 0)
4175 return -EINVAL;
4176 mddev->sync_speed_min = min;
4177 return len;
4178}
4179
4180static struct md_sysfs_entry md_sync_min =
4181__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4182
4183static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004184sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004185{
4186 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4187 mddev->sync_speed_max ? "local": "system");
4188}
4189
4190static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004191sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004192{
4193 int max;
4194 char *e;
4195 if (strncmp(buf, "system", 6)==0) {
4196 mddev->sync_speed_max = 0;
4197 return len;
4198 }
4199 max = simple_strtoul(buf, &e, 10);
4200 if (buf == e || (*e && *e != '\n') || max <= 0)
4201 return -EINVAL;
4202 mddev->sync_speed_max = max;
4203 return len;
4204}
4205
4206static struct md_sysfs_entry md_sync_max =
4207__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4208
Iustin Popd7f3d292007-10-16 23:30:54 -07004209static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004210degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004211{
4212 return sprintf(page, "%d\n", mddev->degraded);
4213}
4214static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004215
4216static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004217sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004218{
4219 return sprintf(page, "%d\n", mddev->parallel_resync);
4220}
4221
4222static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004223sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004224{
4225 long n;
4226
4227 if (strict_strtol(buf, 10, &n))
4228 return -EINVAL;
4229
4230 if (n != 0 && n != 1)
4231 return -EINVAL;
4232
4233 mddev->parallel_resync = n;
4234
4235 if (mddev->sync_thread)
4236 wake_up(&resync_wait);
4237
4238 return len;
4239}
4240
4241/* force parallel resync, even with shared block devices */
4242static struct md_sysfs_entry md_sync_force_parallel =
4243__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4244 sync_force_parallel_show, sync_force_parallel_store);
4245
4246static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004247sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004248{
4249 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004250 if (mddev->curr_resync == 0)
4251 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004252 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4253 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004254 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004255 db = resync - mddev->resync_mark_cnt;
4256 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004257}
4258
NeilBrown80ca3a42006-07-10 04:44:18 -07004259static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004260
4261static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004262sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004263{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004264 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004265
NeilBrownacb180b2009-04-14 16:28:34 +10004266 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4267 return sprintf(page, "none\n");
4268
NeilBrown88202a02006-01-06 00:21:36 -08004269 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004270 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004271 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004272 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004273
NeilBrownacb180b2009-04-14 16:28:34 +10004274 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004275 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004276}
4277
NeilBrown80ca3a42006-07-10 04:44:18 -07004278static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004279
NeilBrowne464eaf2006-03-27 01:18:14 -08004280static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004281min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004282{
4283 return sprintf(page, "%llu\n",
4284 (unsigned long long)mddev->resync_min);
4285}
4286static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004287min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004288{
4289 unsigned long long min;
4290 if (strict_strtoull(buf, 10, &min))
4291 return -EINVAL;
4292 if (min > mddev->resync_max)
4293 return -EINVAL;
4294 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4295 return -EBUSY;
4296
4297 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004298 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004299 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004300 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004301 return -EINVAL;
4302 }
4303 mddev->resync_min = min;
4304
4305 return len;
4306}
4307
4308static struct md_sysfs_entry md_min_sync =
4309__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4310
4311static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004312max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004313{
4314 if (mddev->resync_max == MaxSector)
4315 return sprintf(page, "max\n");
4316 else
4317 return sprintf(page, "%llu\n",
4318 (unsigned long long)mddev->resync_max);
4319}
4320static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004321max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004322{
4323 if (strncmp(buf, "max", 3) == 0)
4324 mddev->resync_max = MaxSector;
4325 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004326 unsigned long long max;
4327 if (strict_strtoull(buf, 10, &max))
4328 return -EINVAL;
4329 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004330 return -EINVAL;
4331 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004332 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004333 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4334 return -EBUSY;
4335
4336 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004337 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004338 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004339 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004340 return -EINVAL;
4341 }
4342 mddev->resync_max = max;
4343 }
4344 wake_up(&mddev->recovery_wait);
4345 return len;
4346}
4347
4348static struct md_sysfs_entry md_max_sync =
4349__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4350
4351static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004352suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004353{
4354 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4355}
4356
4357static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004358suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004359{
4360 char *e;
4361 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004362 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004363
NeilBrownb8d966e2009-07-01 11:14:04 +10004364 if (mddev->pers == NULL ||
4365 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004366 return -EINVAL;
4367 if (buf == e || (*e && *e != '\n'))
4368 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004369
4370 mddev->suspend_lo = new;
4371 if (new >= old)
4372 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004373 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004374 else {
4375 /* Expanding suspended region - need to wait */
4376 mddev->pers->quiesce(mddev, 1);
4377 mddev->pers->quiesce(mddev, 0);
4378 }
4379 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004380}
4381static struct md_sysfs_entry md_suspend_lo =
4382__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4383
4384
4385static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004386suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004387{
4388 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4389}
4390
4391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004392suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004393{
4394 char *e;
4395 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004396 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004397
NeilBrownb8d966e2009-07-01 11:14:04 +10004398 if (mddev->pers == NULL ||
4399 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004400 return -EINVAL;
4401 if (buf == e || (*e && *e != '\n'))
4402 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004403
4404 mddev->suspend_hi = new;
4405 if (new <= old)
4406 /* Shrinking suspended region */
4407 mddev->pers->quiesce(mddev, 2);
4408 else {
4409 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004410 mddev->pers->quiesce(mddev, 1);
4411 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004412 }
4413 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004414}
4415static struct md_sysfs_entry md_suspend_hi =
4416__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4417
NeilBrown08a02ec2007-05-09 02:35:38 -07004418static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004419reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004420{
4421 if (mddev->reshape_position != MaxSector)
4422 return sprintf(page, "%llu\n",
4423 (unsigned long long)mddev->reshape_position);
4424 strcpy(page, "none\n");
4425 return 5;
4426}
4427
4428static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004429reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004430{
4431 char *e;
4432 unsigned long long new = simple_strtoull(buf, &e, 10);
4433 if (mddev->pers)
4434 return -EBUSY;
4435 if (buf == e || (*e && *e != '\n'))
4436 return -EINVAL;
4437 mddev->reshape_position = new;
4438 mddev->delta_disks = 0;
4439 mddev->new_level = mddev->level;
4440 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004441 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004442 return len;
4443}
4444
4445static struct md_sysfs_entry md_reshape_position =
4446__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4447 reshape_position_store);
4448
Dan Williamsb522adc2009-03-31 15:00:31 +11004449static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004450array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004451{
4452 if (mddev->external_size)
4453 return sprintf(page, "%llu\n",
4454 (unsigned long long)mddev->array_sectors/2);
4455 else
4456 return sprintf(page, "default\n");
4457}
4458
4459static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004460array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004461{
4462 sector_t sectors;
4463
4464 if (strncmp(buf, "default", 7) == 0) {
4465 if (mddev->pers)
4466 sectors = mddev->pers->size(mddev, 0, 0);
4467 else
4468 sectors = mddev->array_sectors;
4469
4470 mddev->external_size = 0;
4471 } else {
4472 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4473 return -EINVAL;
4474 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004475 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004476
4477 mddev->external_size = 1;
4478 }
4479
4480 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004481 if (mddev->pers) {
4482 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004483 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004484 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004485 return len;
4486}
4487
4488static struct md_sysfs_entry md_array_size =
4489__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4490 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004491
NeilBrowneae17012005-11-08 21:39:23 -08004492static struct attribute *md_default_attrs[] = {
4493 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004494 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004495 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004496 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004497 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004498 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004499 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004500 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004501 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004502 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004503 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004504 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004505 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004506 NULL,
4507};
4508
4509static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004510 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004511 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004512 &md_sync_min.attr,
4513 &md_sync_max.attr,
4514 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004515 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004516 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004517 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004518 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004519 &md_suspend_lo.attr,
4520 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004521 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004522 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004523 NULL,
4524};
NeilBrown411036f2005-11-08 21:39:40 -08004525static struct attribute_group md_redundancy_group = {
4526 .name = NULL,
4527 .attrs = md_redundancy_attrs,
4528};
4529
NeilBrowneae17012005-11-08 21:39:23 -08004530
4531static ssize_t
4532md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4533{
4534 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004535 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004536 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004537
4538 if (!entry->show)
4539 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11004540 spin_lock(&all_mddevs_lock);
4541 if (list_empty(&mddev->all_mddevs)) {
4542 spin_unlock(&all_mddevs_lock);
4543 return -EBUSY;
4544 }
4545 mddev_get(mddev);
4546 spin_unlock(&all_mddevs_lock);
4547
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004548 rv = mddev_lock(mddev);
4549 if (!rv) {
4550 rv = entry->show(mddev, page);
4551 mddev_unlock(mddev);
4552 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004553 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004554 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004555}
4556
4557static ssize_t
4558md_attr_store(struct kobject *kobj, struct attribute *attr,
4559 const char *page, size_t length)
4560{
4561 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004562 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004563 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004564
4565 if (!entry->store)
4566 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004567 if (!capable(CAP_SYS_ADMIN))
4568 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11004569 spin_lock(&all_mddevs_lock);
4570 if (list_empty(&mddev->all_mddevs)) {
4571 spin_unlock(&all_mddevs_lock);
4572 return -EBUSY;
4573 }
4574 mddev_get(mddev);
4575 spin_unlock(&all_mddevs_lock);
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004576 rv = mddev_lock(mddev);
4577 if (!rv) {
4578 rv = entry->store(mddev, page, length);
4579 mddev_unlock(mddev);
4580 }
NeilBrownaf8a2432011-12-08 15:49:46 +11004581 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004582 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004583}
4584
4585static void md_free(struct kobject *ko)
4586{
NeilBrownfd01b882011-10-11 16:47:53 +11004587 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004588
4589 if (mddev->sysfs_state)
4590 sysfs_put(mddev->sysfs_state);
4591
4592 if (mddev->gendisk) {
4593 del_gendisk(mddev->gendisk);
4594 put_disk(mddev->gendisk);
4595 }
4596 if (mddev->queue)
4597 blk_cleanup_queue(mddev->queue);
4598
NeilBrowneae17012005-11-08 21:39:23 -08004599 kfree(mddev);
4600}
4601
Emese Revfy52cf25d2010-01-19 02:58:23 +01004602static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004603 .show = md_attr_show,
4604 .store = md_attr_store,
4605};
4606static struct kobj_type md_ktype = {
4607 .release = md_free,
4608 .sysfs_ops = &md_sysfs_ops,
4609 .default_attrs = md_default_attrs,
4610};
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612int mdp_major = 0;
4613
Dan Williams5fd3a172009-03-04 00:57:25 -07004614static void mddev_delayed_delete(struct work_struct *ws)
4615{
NeilBrownfd01b882011-10-11 16:47:53 +11004616 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004617
NeilBrown43a70502009-12-14 12:49:55 +11004618 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004619 kobject_del(&mddev->kobj);
4620 kobject_put(&mddev->kobj);
4621}
4622
NeilBrownefeb53c2009-01-09 08:31:10 +11004623static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004625 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004626 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004628 int partitioned;
4629 int shift;
4630 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004631 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004634 return -ENODEV;
4635
4636 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4637 shift = partitioned ? MdpMinorShift : 0;
4638 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Tejun Heoe804ac72010-10-15 15:36:08 +02004640 /* wait for any previous instance of this device to be
4641 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004642 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004643 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004644
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004645 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004646 error = -EEXIST;
4647 if (mddev->gendisk)
4648 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004649
4650 if (name) {
4651 /* Need to ensure that 'name' is not a duplicate.
4652 */
NeilBrownfd01b882011-10-11 16:47:53 +11004653 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004654 spin_lock(&all_mddevs_lock);
4655
4656 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4657 if (mddev2->gendisk &&
4658 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4659 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004660 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004661 }
4662 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
NeilBrown8b765392009-01-09 08:31:08 +11004664
NeilBrown0909dc42009-07-01 12:27:21 +10004665 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004666 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004667 if (!mddev->queue)
4668 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004669 mddev->queue->queuedata = mddev;
4670
NeilBrown409c57f2009-03-31 14:39:39 +11004671 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01004672 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11004673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 disk = alloc_disk(1 << shift);
4675 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004676 blk_cleanup_queue(mddev->queue);
4677 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004678 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004680 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004682 if (name)
4683 strcpy(disk->disk_name, name);
4684 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004686 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 disk->fops = &md_fops;
4689 disk->private_data = mddev;
4690 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004691 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004692 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004693 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004694 * remove it now.
4695 */
4696 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004698 /* As soon as we call add_disk(), another thread could get
4699 * through to md_open, so make sure it doesn't get too far
4700 */
4701 mutex_lock(&mddev->open_mutex);
4702 add_disk(disk);
4703
Tejun Heoed9e1982008-08-25 19:56:05 +09004704 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4705 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004706 if (error) {
4707 /* This isn't possible, but as kobject_init_and_add is marked
4708 * __must_check, we must do something with the result
4709 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004710 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4711 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004712 error = 0;
4713 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004714 if (mddev->kobj.sd &&
4715 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004716 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004717 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004718 abort:
4719 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004720 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004721 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004722 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004723 }
NeilBrownd3374822009-01-09 08:31:10 +11004724 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004725 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004726}
4727
4728static struct kobject *md_probe(dev_t dev, int *part, void *data)
4729{
4730 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 return NULL;
4732}
4733
NeilBrownefeb53c2009-01-09 08:31:10 +11004734static int add_named_array(const char *val, struct kernel_param *kp)
4735{
4736 /* val must be "md_*" where * is not all digits.
4737 * We allocate an array with a large free minor number, and
4738 * set the name to val. val must not already be an active name.
4739 */
4740 int len = strlen(val);
4741 char buf[DISK_NAME_LEN];
4742
4743 while (len && val[len-1] == '\n')
4744 len--;
4745 if (len >= DISK_NAME_LEN)
4746 return -E2BIG;
4747 strlcpy(buf, val, len+1);
4748 if (strncmp(buf, "md_", 3) != 0)
4749 return -EINVAL;
4750 return md_alloc(0, buf);
4751}
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753static void md_safemode_timeout(unsigned long data)
4754{
NeilBrownfd01b882011-10-11 16:47:53 +11004755 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Neil Brown0fd62b82008-06-28 08:31:36 +10004757 if (!atomic_read(&mddev->writes_pending)) {
4758 mddev->safemode = 1;
4759 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004760 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 md_wakeup_thread(mddev->thread);
4763}
4764
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004765static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
NeilBrownfd01b882011-10-11 16:47:53 +11004767int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
NeilBrown2604b702006-01-06 00:20:36 -08004769 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004770 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004771 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
NeilBrowna757e642005-04-16 15:26:42 -07004773 if (list_empty(&mddev->disks))
4774 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 if (mddev->pers)
4778 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004779 /* Cannot run until previous stop completes properly */
4780 if (mddev->sysfs_active)
4781 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 /*
4784 * Analyze all RAID superblock(s)
4785 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004786 if (!mddev->raid_disks) {
4787 if (!mddev->persistent)
4788 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004789 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
NeilBrownd9d166c2006-01-06 00:20:51 -08004792 if (mddev->level != LEVEL_NONE)
4793 request_module("md-level-%d", mddev->level);
4794 else if (mddev->clevel[0])
4795 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
4797 /*
4798 * Drop all container device buffers, from now on
4799 * the only valid external interface is through the md
4800 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 */
NeilBrowndafb20f2012-03-19 12:46:39 +11004802 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004803 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 continue;
4805 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004806 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004807
4808 /* perform some consistency tests on the device.
4809 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004810 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004811 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004812 if (rdev->meta_bdev) {
4813 /* Nothing to check */;
4814 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004815 if (mddev->dev_sectors &&
4816 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004817 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004818 printk("md: %s: data overlaps metadata\n",
4819 mdname(mddev));
4820 return -EINVAL;
4821 }
4822 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004823 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004824 > rdev->data_offset) {
4825 printk("md: %s: metadata overlaps data\n",
4826 mdname(mddev));
4827 return -EINVAL;
4828 }
4829 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004830 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
4832
NeilBrowna167f662010-10-26 18:31:13 +11004833 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004834 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004835 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004838 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004839 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004841 if (mddev->level != LEVEL_NONE)
4842 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4843 mddev->level);
4844 else
4845 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4846 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return -EINVAL;
4848 }
NeilBrown2604b702006-01-06 00:20:36 -08004849 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004851 if (mddev->level != pers->level) {
4852 mddev->level = pers->level;
4853 mddev->new_level = pers->level;
4854 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004855 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
NeilBrownf6705572006-03-27 01:18:11 -08004857 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004858 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004859 /* This personality cannot handle reshaping... */
4860 mddev->pers = NULL;
4861 module_put(pers->owner);
4862 return -EINVAL;
4863 }
4864
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004865 if (pers->sync_request) {
4866 /* Warn if this is a potentially silly
4867 * configuration.
4868 */
4869 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004870 struct md_rdev *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004871 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004872
NeilBrowndafb20f2012-03-19 12:46:39 +11004873 rdev_for_each(rdev, mddev)
4874 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004875 if (rdev < rdev2 &&
4876 rdev->bdev->bd_contains ==
4877 rdev2->bdev->bd_contains) {
4878 printk(KERN_WARNING
4879 "%s: WARNING: %s appears to be"
4880 " on the same physical disk as"
4881 " %s.\n",
4882 mdname(mddev),
4883 bdevname(rdev->bdev,b),
4884 bdevname(rdev2->bdev,b2));
4885 warned = 1;
4886 }
4887 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004888
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004889 if (warned)
4890 printk(KERN_WARNING
4891 "True protection against single-disk"
4892 " failure might be compromised.\n");
4893 }
4894
NeilBrown657390d2005-08-26 18:34:16 -07004895 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004896 /* may be over-ridden by personality */
4897 mddev->resync_max_sectors = mddev->dev_sectors;
4898
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004899 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
NeilBrown0f9552b2009-12-30 12:08:50 +11004901 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004902 mddev->ro = 2; /* read-only, but switch on first write */
4903
NeilBrownb15c2e52006-01-06 00:20:16 -08004904 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004905 if (err)
4906 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004907 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4908 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4909 " but 'external_size' not in effect?\n", __func__);
4910 printk(KERN_ERR
4911 "md: invalid array_size %llu > default size %llu\n",
4912 (unsigned long long)mddev->array_sectors / 2,
4913 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4914 err = -EINVAL;
4915 mddev->pers->stop(mddev);
4916 }
4917 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004918 err = bitmap_create(mddev);
4919 if (err) {
4920 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4921 mdname(mddev), err);
4922 mddev->pers->stop(mddev);
4923 }
4924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 module_put(mddev->pers->owner);
4927 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004928 bitmap_destroy(mddev);
4929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004931 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004932 if (mddev->kobj.sd &&
4933 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004934 printk(KERN_WARNING
4935 "md: cannot register extra attributes for %s\n",
4936 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004937 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004938 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004939 mddev->ro = 0;
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004942 atomic_set(&mddev->max_corr_read_errors,
4943 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 mddev->safemode = 0;
4945 mddev->safemode_timer.function = md_safemode_timeout;
4946 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004947 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004949 smp_wmb();
4950 mddev->ready = 1;
NeilBrowndafb20f2012-03-19 12:46:39 +11004951 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10004952 if (rdev->raid_disk >= 0)
4953 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004954 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4957
NeilBrown850b2b42006-10-03 01:15:46 -07004958 if (mddev->flags)
4959 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
NeilBrownd7603b72006-01-06 00:20:30 -08004961 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004962 sysfs_notify_dirent_safe(mddev->sysfs_state);
4963 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004964 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return 0;
4966}
NeilBrown390ee602010-06-01 19:37:27 +10004967EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
NeilBrownfd01b882011-10-11 16:47:53 +11004969static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004970{
4971 int err;
4972
4973 err = md_run(mddev);
4974 if (err)
4975 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004976 err = bitmap_load(mddev);
4977 if (err) {
4978 bitmap_destroy(mddev);
4979 goto out;
4980 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004981
4982 md_wakeup_thread(mddev->thread);
4983 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4984
NeilBrownfe60b012010-03-29 11:10:42 +11004985 set_capacity(mddev->gendisk, mddev->array_sectors);
4986 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004987 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004988 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4989out:
4990 return err;
4991}
4992
NeilBrownfd01b882011-10-11 16:47:53 +11004993static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
4995 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Andre Noll80fab1d2008-07-11 22:02:21 +10004997 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004999 return -ENXIO;
5000 if (!mddev->pers)
5001 return -EINVAL;
5002 if (!mddev->ro)
5003 return -EBUSY;
5004 mddev->safemode = 0;
5005 mddev->ro = 0;
5006 set_disk_ro(disk, 0);
5007 printk(KERN_INFO "md: %s switched to read-write mode.\n",
5008 mdname(mddev));
5009 /* Kick recovery or resync if necessary */
5010 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5011 md_wakeup_thread(mddev->thread);
5012 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005013 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
NeilBrownacc55e22006-06-26 00:27:47 -07005017/* similar to deny_write_access, but accounts for our holding a reference
5018 * to the file ourselves */
5019static int deny_bitmap_write_access(struct file * file)
5020{
5021 struct inode *inode = file->f_mapping->host;
5022
5023 spin_lock(&inode->i_lock);
5024 if (atomic_read(&inode->i_writecount) > 1) {
5025 spin_unlock(&inode->i_lock);
5026 return -ETXTBSY;
5027 }
5028 atomic_set(&inode->i_writecount, -1);
5029 spin_unlock(&inode->i_lock);
5030
5031 return 0;
5032}
5033
NeilBrown43a70502009-12-14 12:49:55 +11005034void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07005035{
5036 struct inode *inode = file->f_mapping->host;
5037
5038 spin_lock(&inode->i_lock);
5039 atomic_set(&inode->i_writecount, 1);
5040 spin_unlock(&inode->i_lock);
5041}
5042
NeilBrownfd01b882011-10-11 16:47:53 +11005043static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005044{
5045 mddev->array_sectors = 0;
5046 mddev->external_size = 0;
5047 mddev->dev_sectors = 0;
5048 mddev->raid_disks = 0;
5049 mddev->recovery_cp = 0;
5050 mddev->resync_min = 0;
5051 mddev->resync_max = MaxSector;
5052 mddev->reshape_position = MaxSector;
5053 mddev->external = 0;
5054 mddev->persistent = 0;
5055 mddev->level = LEVEL_NONE;
5056 mddev->clevel[0] = 0;
5057 mddev->flags = 0;
5058 mddev->ro = 0;
5059 mddev->metadata_type[0] = 0;
5060 mddev->chunk_sectors = 0;
5061 mddev->ctime = mddev->utime = 0;
5062 mddev->layout = 0;
5063 mddev->max_disks = 0;
5064 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005065 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005066 mddev->delta_disks = 0;
5067 mddev->new_level = LEVEL_NONE;
5068 mddev->new_layout = 0;
5069 mddev->new_chunk_sectors = 0;
5070 mddev->curr_resync = 0;
5071 mddev->resync_mismatches = 0;
5072 mddev->suspend_lo = mddev->suspend_hi = 0;
5073 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5074 mddev->recovery = 0;
5075 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005076 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005077 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005078 mddev->safemode = 0;
NeilBrown050b6612012-03-19 12:46:39 +11005079 mddev->merge_check_needed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005080 mddev->bitmap_info.offset = 0;
5081 mddev->bitmap_info.default_offset = 0;
5082 mddev->bitmap_info.chunksize = 0;
5083 mddev->bitmap_info.daemon_sleep = 0;
5084 mddev->bitmap_info.max_write_behind = 0;
5085}
5086
NeilBrownfd01b882011-10-11 16:47:53 +11005087static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005088{
5089 if (mddev->sync_thread) {
5090 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5091 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005092 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005093 }
5094
5095 del_timer_sync(&mddev->safemode_timer);
5096
5097 bitmap_flush(mddev);
5098 md_super_wait(mddev);
5099
5100 if (!mddev->in_sync || mddev->flags) {
5101 /* mark array as shutdown cleanly */
5102 mddev->in_sync = 1;
5103 md_update_sb(mddev, 1);
5104 }
5105}
NeilBrowndefad612011-01-14 09:14:33 +11005106
NeilBrownfd01b882011-10-11 16:47:53 +11005107void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005108{
5109 mddev_lock(mddev);
5110 __md_stop_writes(mddev);
5111 mddev_unlock(mddev);
5112}
NeilBrown390ee602010-06-01 19:37:27 +10005113EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005114
NeilBrownfd01b882011-10-11 16:47:53 +11005115void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005116{
NeilBrown0ca69882011-01-14 09:14:33 +11005117 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005118 mddev->pers->stop(mddev);
5119 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5120 mddev->to_remove = &md_redundancy_group;
5121 module_put(mddev->pers->owner);
5122 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005123 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005124}
NeilBrown390ee602010-06-01 19:37:27 +10005125EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005126
NeilBrownfd01b882011-10-11 16:47:53 +11005127static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005128{
5129 int err = 0;
5130 mutex_lock(&mddev->open_mutex);
5131 if (atomic_read(&mddev->openers) > is_open) {
5132 printk("md: %s still in use.\n",mdname(mddev));
5133 err = -EBUSY;
5134 goto out;
5135 }
5136 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005137 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005138
5139 err = -ENXIO;
5140 if (mddev->ro==1)
5141 goto out;
5142 mddev->ro = 1;
5143 set_disk_ro(mddev->gendisk, 1);
5144 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005145 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005146 err = 0;
5147 }
5148out:
5149 mutex_unlock(&mddev->open_mutex);
5150 return err;
5151}
5152
NeilBrown9e653b62006-06-26 00:27:58 -07005153/* mode:
5154 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005155 * 2 - stop but do not disassemble array
5156 */
NeilBrownfd01b882011-10-11 16:47:53 +11005157static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005160 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
NeilBrownc8c00a62009-08-10 12:50:52 +10005162 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005163 if (atomic_read(&mddev->openers) > is_open ||
5164 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005165 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005166 mutex_unlock(&mddev->open_mutex);
5167 return -EBUSY;
5168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169
NeilBrown6e17b022010-08-07 21:41:19 +10005170 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005171 if (mddev->ro)
5172 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005173
NeilBrowndefad612011-01-14 09:14:33 +11005174 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005175 md_stop(mddev);
5176 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005177 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005178
NeilBrowna4bd82d2010-03-29 13:23:10 +11005179 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005180 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005181
NeilBrowndafb20f2012-03-19 12:46:39 +11005182 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005183 if (rdev->raid_disk >= 0)
5184 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005185
NeilBrowna4bd82d2010-03-29 13:23:10 +11005186 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005187 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005188 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005189 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005190
NeilBrowna4bd82d2010-03-29 13:23:10 +11005191 if (mddev->ro)
5192 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005193 } else
5194 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 /*
5196 * Free resources if final stop
5197 */
NeilBrown9e653b62006-06-26 00:27:58 -07005198 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5200
NeilBrown978f9462006-02-02 14:28:05 -08005201 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005202 if (mddev->bitmap_info.file) {
5203 restore_bitmap_write_access(mddev->bitmap_info.file);
5204 fput(mddev->bitmap_info.file);
5205 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005206 }
NeilBrownc3d97142009-12-14 12:49:52 +11005207 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 export_array(mddev);
5210
NeilBrown6177b472010-03-29 11:37:13 +11005211 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005212 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005213 if (mddev->hold_active == UNTIL_STOP)
5214 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005215 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005216 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005217 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005218 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005222#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005223static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
NeilBrown3cb03002011-10-11 16:45:26 +11005225 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 int err;
5227
NeilBrowna757e642005-04-16 15:26:42 -07005228 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 printk(KERN_INFO "md: running: ");
5232
NeilBrowndafb20f2012-03-19 12:46:39 +11005233 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 char b[BDEVNAME_SIZE];
5235 printk("<%s>", bdevname(rdev->bdev,b));
5236 }
5237 printk("\n");
5238
NeilBrownd710e132008-10-13 11:55:12 +11005239 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (err) {
5241 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005242 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244}
5245
5246/*
5247 * lets try to run arrays based on all disks that have arrived
5248 * until now. (those are in pending_raid_disks)
5249 *
5250 * the method: pick the first pending disk, collect all disks with
5251 * the same UUID, remove all from the pending list and put them into
5252 * the 'same_array' list. Then order this list based on superblock
5253 * update time (freshest comes first), kick out 'old' disks and
5254 * compare superblocks. If everything's fine then run it.
5255 *
5256 * If "unit" is allocated, then bump its reference count
5257 */
5258static void autorun_devices(int part)
5259{
NeilBrown3cb03002011-10-11 16:45:26 +11005260 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005261 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 char b[BDEVNAME_SIZE];
5263
5264 printk(KERN_INFO "md: autorun ...\n");
5265 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005266 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005268 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005270 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
5272 printk(KERN_INFO "md: considering %s ...\n",
5273 bdevname(rdev0->bdev,b));
5274 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005275 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 if (super_90_load(rdev, rdev0, 0) >= 0) {
5277 printk(KERN_INFO "md: adding %s ...\n",
5278 bdevname(rdev->bdev,b));
5279 list_move(&rdev->same_set, &candidates);
5280 }
5281 /*
5282 * now we have a set of devices, with all of them having
5283 * mostly sane superblocks. It's time to allocate the
5284 * mddev.
5285 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005286 if (part) {
5287 dev = MKDEV(mdp_major,
5288 rdev0->preferred_minor << MdpMinorShift);
5289 unit = MINOR(dev) >> MdpMinorShift;
5290 } else {
5291 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5292 unit = MINOR(dev);
5293 }
5294 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5296 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5297 break;
5298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 md_probe(dev, NULL, NULL);
5301 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005302 if (!mddev || !mddev->gendisk) {
5303 if (mddev)
5304 mddev_put(mddev);
5305 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 "md: cannot allocate memory for md drive.\n");
5307 break;
5308 }
5309 if (mddev_lock(mddev))
5310 printk(KERN_WARNING "md: %s locked, cannot run\n",
5311 mdname(mddev));
5312 else if (mddev->raid_disks || mddev->major_version
5313 || !list_empty(&mddev->disks)) {
5314 printk(KERN_WARNING
5315 "md: %s already running, cannot run %s\n",
5316 mdname(mddev), bdevname(rdev0->bdev,b));
5317 mddev_unlock(mddev);
5318 } else {
5319 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005320 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005321 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 list_del_init(&rdev->same_set);
5323 if (bind_rdev_to_array(rdev, mddev))
5324 export_rdev(rdev);
5325 }
5326 autorun_array(mddev);
5327 mddev_unlock(mddev);
5328 }
5329 /* on success, candidates will be empty, on error
5330 * it won't...
5331 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005332 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005333 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 mddev_put(mddev);
5337 }
5338 printk(KERN_INFO "md: ... autorun DONE.\n");
5339}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005340#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342static int get_version(void __user * arg)
5343{
5344 mdu_version_t ver;
5345
5346 ver.major = MD_MAJOR_VERSION;
5347 ver.minor = MD_MINOR_VERSION;
5348 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5349
5350 if (copy_to_user(arg, &ver, sizeof(ver)))
5351 return -EFAULT;
5352
5353 return 0;
5354}
5355
NeilBrownfd01b882011-10-11 16:47:53 +11005356static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
5358 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005359 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005360 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
NeilBrowna9f326e2009-09-23 18:06:41 +10005362 nr=working=insync=failed=spare=0;
NeilBrowndafb20f2012-03-19 12:46:39 +11005363 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005365 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 failed++;
5367 else {
5368 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005369 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005370 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 else
5372 spare++;
5373 }
5374 }
5375
5376 info.major_version = mddev->major_version;
5377 info.minor_version = mddev->minor_version;
5378 info.patch_version = MD_PATCHLEVEL_VERSION;
5379 info.ctime = mddev->ctime;
5380 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005381 info.size = mddev->dev_sectors / 2;
5382 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005383 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 info.nr_disks = nr;
5385 info.raid_disks = mddev->raid_disks;
5386 info.md_minor = mddev->md_minor;
5387 info.not_persistent= !mddev->persistent;
5388
5389 info.utime = mddev->utime;
5390 info.state = 0;
5391 if (mddev->in_sync)
5392 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005393 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005394 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005395 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 info.working_disks = working;
5397 info.failed_disks = failed;
5398 info.spare_disks = spare;
5399
5400 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005401 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
5403 if (copy_to_user(arg, &info, sizeof(info)))
5404 return -EFAULT;
5405
5406 return 0;
5407}
5408
NeilBrownfd01b882011-10-11 16:47:53 +11005409static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005410{
5411 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5412 char *ptr, *buf = NULL;
5413 int err = -ENOMEM;
5414
Dan Williamsb5470dc2008-06-27 21:44:04 -07005415 if (md_allow_write(mddev))
5416 file = kmalloc(sizeof(*file), GFP_NOIO);
5417 else
5418 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005419
NeilBrown32a76272005-06-21 17:17:14 -07005420 if (!file)
5421 goto out;
5422
5423 /* bitmap disabled, zero the first byte and copy out */
5424 if (!mddev->bitmap || !mddev->bitmap->file) {
5425 file->pathname[0] = '\0';
5426 goto copy_out;
5427 }
5428
5429 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5430 if (!buf)
5431 goto out;
5432
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005433 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5434 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005435 goto out;
5436
5437 strcpy(file->pathname, ptr);
5438
5439copy_out:
5440 err = 0;
5441 if (copy_to_user(arg, file, sizeof(*file)))
5442 err = -EFAULT;
5443out:
5444 kfree(buf);
5445 kfree(file);
5446 return err;
5447}
5448
NeilBrownfd01b882011-10-11 16:47:53 +11005449static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
5451 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005452 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
5454 if (copy_from_user(&info, arg, sizeof(info)))
5455 return -EFAULT;
5456
Andre Noll26ef3792008-07-11 22:02:21 +10005457 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 if (rdev) {
5459 info.major = MAJOR(rdev->bdev->bd_dev);
5460 info.minor = MINOR(rdev->bdev->bd_dev);
5461 info.raid_disk = rdev->raid_disk;
5462 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005463 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005465 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 info.state |= (1<<MD_DISK_ACTIVE);
5467 info.state |= (1<<MD_DISK_SYNC);
5468 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005469 if (test_bit(WriteMostly, &rdev->flags))
5470 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 } else {
5472 info.major = info.minor = 0;
5473 info.raid_disk = -1;
5474 info.state = (1<<MD_DISK_REMOVED);
5475 }
5476
5477 if (copy_to_user(arg, &info, sizeof(info)))
5478 return -EFAULT;
5479
5480 return 0;
5481}
5482
NeilBrownfd01b882011-10-11 16:47:53 +11005483static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
5485 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005486 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 dev_t dev = MKDEV(info->major,info->minor);
5488
5489 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5490 return -EOVERFLOW;
5491
5492 if (!mddev->raid_disks) {
5493 int err;
5494 /* expecting a device which has a superblock */
5495 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5496 if (IS_ERR(rdev)) {
5497 printk(KERN_WARNING
5498 "md: md_import_device returned %ld\n",
5499 PTR_ERR(rdev));
5500 return PTR_ERR(rdev);
5501 }
5502 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005503 struct md_rdev *rdev0
5504 = list_entry(mddev->disks.next,
5505 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005506 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 .load_super(rdev, rdev0, mddev->minor_version);
5508 if (err < 0) {
5509 printk(KERN_WARNING
5510 "md: %s has different UUID to %s\n",
5511 bdevname(rdev->bdev,b),
5512 bdevname(rdev0->bdev,b2));
5513 export_rdev(rdev);
5514 return -EINVAL;
5515 }
5516 }
5517 err = bind_rdev_to_array(rdev, mddev);
5518 if (err)
5519 export_rdev(rdev);
5520 return err;
5521 }
5522
5523 /*
5524 * add_new_disk can be used once the array is assembled
5525 * to add "hot spares". They must already have a superblock
5526 * written
5527 */
5528 if (mddev->pers) {
5529 int err;
5530 if (!mddev->pers->hot_add_disk) {
5531 printk(KERN_WARNING
5532 "%s: personality does not support diskops!\n",
5533 mdname(mddev));
5534 return -EINVAL;
5535 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005536 if (mddev->persistent)
5537 rdev = md_import_device(dev, mddev->major_version,
5538 mddev->minor_version);
5539 else
5540 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (IS_ERR(rdev)) {
5542 printk(KERN_WARNING
5543 "md: md_import_device returned %ld\n",
5544 PTR_ERR(rdev));
5545 return PTR_ERR(rdev);
5546 }
NeilBrown1a855a02010-12-09 16:36:28 +11005547 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005548 if (!mddev->persistent) {
5549 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005550 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005551 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005552 set_bit(In_sync, &rdev->flags);
5553 } else
NeilBrown41158c72005-06-21 17:17:25 -07005554 rdev->raid_disk = -1;
5555 } else
5556 super_types[mddev->major_version].
5557 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005558 if ((info->state & (1<<MD_DISK_SYNC)) &&
5559 (!test_bit(In_sync, &rdev->flags) ||
5560 rdev->raid_disk != info->raid_disk)) {
5561 /* This was a hot-add request, but events doesn't
5562 * match, so reject it.
5563 */
5564 export_rdev(rdev);
5565 return -EINVAL;
5566 }
5567
NeilBrown1a855a02010-12-09 16:36:28 +11005568 if (test_bit(In_sync, &rdev->flags))
5569 rdev->saved_raid_disk = rdev->raid_disk;
5570 else
5571 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005572
NeilBrownb2d444d2005-11-08 21:39:31 -08005573 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005574 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5575 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005576 else
5577 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005578
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 rdev->raid_disk = -1;
5580 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005581 if (!err && !mddev->pers->hot_remove_disk) {
5582 /* If there is hot_add_disk but no hot_remove_disk
5583 * then added disks for geometry changes,
5584 * and should be added immediately.
5585 */
5586 super_types[mddev->major_version].
5587 validate_super(mddev, rdev);
5588 err = mddev->pers->hot_add_disk(mddev, rdev);
5589 if (err)
5590 unbind_rdev_from_array(rdev);
5591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (err)
5593 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005594 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005595 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005596
NeilBrown17571282006-12-10 02:20:52 -08005597 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005598 if (mddev->degraded)
5599 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005601 if (!err)
5602 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005603 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return err;
5605 }
5606
5607 /* otherwise, add_new_disk is only allowed
5608 * for major_version==0 superblocks
5609 */
5610 if (mddev->major_version != 0) {
5611 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5612 mdname(mddev));
5613 return -EINVAL;
5614 }
5615
5616 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5617 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005618 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (IS_ERR(rdev)) {
5620 printk(KERN_WARNING
5621 "md: error, md_import_device() returned %ld\n",
5622 PTR_ERR(rdev));
5623 return PTR_ERR(rdev);
5624 }
5625 rdev->desc_nr = info->number;
5626 if (info->raid_disk < mddev->raid_disks)
5627 rdev->raid_disk = info->raid_disk;
5628 else
5629 rdev->raid_disk = -1;
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005632 if (info->state & (1<<MD_DISK_SYNC))
5633 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005635 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5636 set_bit(WriteMostly, &rdev->flags);
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 if (!mddev->persistent) {
5639 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005640 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5641 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005642 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005643 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
NeilBrown2bf071b2006-01-06 00:20:55 -08005645 err = bind_rdev_to_array(rdev, mddev);
5646 if (err) {
5647 export_rdev(rdev);
5648 return err;
5649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
5651
5652 return 0;
5653}
5654
NeilBrownfd01b882011-10-11 16:47:53 +11005655static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
5657 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005658 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 rdev = find_rdev(mddev, dev);
5661 if (!rdev)
5662 return -ENXIO;
5663
5664 if (rdev->raid_disk >= 0)
5665 goto busy;
5666
5667 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005668 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005669 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 return 0;
5672busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005673 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 bdevname(rdev->bdev,b), mdname(mddev));
5675 return -EBUSY;
5676}
5677
NeilBrownfd01b882011-10-11 16:47:53 +11005678static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
5680 char b[BDEVNAME_SIZE];
5681 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005682 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 if (!mddev->pers)
5685 return -ENODEV;
5686
5687 if (mddev->major_version != 0) {
5688 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5689 " version-0 superblocks.\n",
5690 mdname(mddev));
5691 return -EINVAL;
5692 }
5693 if (!mddev->pers->hot_add_disk) {
5694 printk(KERN_WARNING
5695 "%s: personality does not support diskops!\n",
5696 mdname(mddev));
5697 return -EINVAL;
5698 }
5699
NeilBrownd710e132008-10-13 11:55:12 +11005700 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 if (IS_ERR(rdev)) {
5702 printk(KERN_WARNING
5703 "md: error, md_import_device() returned %ld\n",
5704 PTR_ERR(rdev));
5705 return -EINVAL;
5706 }
5707
5708 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005709 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005711 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
NeilBrown8190e752009-06-18 08:48:58 +10005713 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
NeilBrownb2d444d2005-11-08 21:39:31 -08005715 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 printk(KERN_WARNING
5717 "md: can not hot-add faulty %s disk to %s!\n",
5718 bdevname(rdev->bdev,b), mdname(mddev));
5719 err = -EINVAL;
5720 goto abort_export;
5721 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005722 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005724 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005725 err = bind_rdev_to_array(rdev, mddev);
5726 if (err)
5727 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 /*
5730 * The rest should better be atomic, we can have disk failures
5731 * noticed in interrupt contexts ...
5732 */
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 rdev->raid_disk = -1;
5735
NeilBrown850b2b42006-10-03 01:15:46 -07005736 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
5738 /*
5739 * Kick recovery, maybe this spare has to be added to the
5740 * array immediately.
5741 */
5742 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5743 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005744 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return 0;
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747abort_export:
5748 export_rdev(rdev);
5749 return err;
5750}
5751
NeilBrownfd01b882011-10-11 16:47:53 +11005752static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005753{
5754 int err;
5755
NeilBrown36fa3062005-09-09 16:23:45 -07005756 if (mddev->pers) {
5757 if (!mddev->pers->quiesce)
5758 return -EBUSY;
5759 if (mddev->recovery || mddev->sync_thread)
5760 return -EBUSY;
5761 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005762 }
5763
NeilBrown36fa3062005-09-09 16:23:45 -07005764
5765 if (fd >= 0) {
5766 if (mddev->bitmap)
5767 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005768 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005769
NeilBrownc3d97142009-12-14 12:49:52 +11005770 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005771 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5772 mdname(mddev));
5773 return -EBADF;
5774 }
5775
NeilBrownc3d97142009-12-14 12:49:52 +11005776 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005777 if (err) {
5778 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5779 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005780 fput(mddev->bitmap_info.file);
5781 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005782 return err;
5783 }
NeilBrownc3d97142009-12-14 12:49:52 +11005784 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005785 } else if (mddev->bitmap == NULL)
5786 return -ENOENT; /* cannot remove what isn't there */
5787 err = 0;
5788 if (mddev->pers) {
5789 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005790 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005791 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005792 if (!err)
5793 err = bitmap_load(mddev);
5794 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005795 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005796 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005797 fd = -1; /* make sure to put the file */
5798 }
NeilBrown36fa3062005-09-09 16:23:45 -07005799 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005800 }
5801 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005802 if (mddev->bitmap_info.file) {
5803 restore_bitmap_write_access(mddev->bitmap_info.file);
5804 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005805 }
NeilBrownc3d97142009-12-14 12:49:52 +11005806 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005807 }
5808
NeilBrown32a76272005-06-21 17:17:14 -07005809 return err;
5810}
5811
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812/*
5813 * set_array_info is used two different ways
5814 * The original usage is when creating a new array.
5815 * In this usage, raid_disks is > 0 and it together with
5816 * level, size, not_persistent,layout,chunksize determine the
5817 * shape of the array.
5818 * This will always create an array with a type-0.90.0 superblock.
5819 * The newer usage is when assembling an array.
5820 * In this case raid_disks will be 0, and the major_version field is
5821 * use to determine which style super-blocks are to be found on the devices.
5822 * The minor and patch _version numbers are also kept incase the
5823 * super_block handler wishes to interpret them.
5824 */
NeilBrownfd01b882011-10-11 16:47:53 +11005825static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826{
5827
5828 if (info->raid_disks == 0) {
5829 /* just setting version number for superblock loading */
5830 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005831 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 super_types[info->major_version].name == NULL) {
5833 /* maybe try to auto-load a module? */
5834 printk(KERN_INFO
5835 "md: superblock version %d not known\n",
5836 info->major_version);
5837 return -EINVAL;
5838 }
5839 mddev->major_version = info->major_version;
5840 mddev->minor_version = info->minor_version;
5841 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005842 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005843 /* ensure mddev_put doesn't delete this now that there
5844 * is some minimal configuration.
5845 */
5846 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 return 0;
5848 }
5849 mddev->major_version = MD_MAJOR_VERSION;
5850 mddev->minor_version = MD_MINOR_VERSION;
5851 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5852 mddev->ctime = get_seconds();
5853
5854 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005855 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005856 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 mddev->raid_disks = info->raid_disks;
5858 /* don't set md_minor, it is determined by which /dev/md* was
5859 * openned
5860 */
5861 if (info->state & (1<<MD_SB_CLEAN))
5862 mddev->recovery_cp = MaxSector;
5863 else
5864 mddev->recovery_cp = 0;
5865 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005866 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005869 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 mddev->max_disks = MD_SB_DISKS;
5872
NeilBrowne6910632008-02-06 01:39:51 -08005873 if (mddev->persistent)
5874 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005875 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
NeilBrownc3d97142009-12-14 12:49:52 +11005877 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5878 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005879
NeilBrownf6705572006-03-27 01:18:11 -08005880 mddev->reshape_position = MaxSector;
5881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 /*
5883 * Generate a 128 bit UUID
5884 */
5885 get_random_bytes(mddev->uuid, 16);
5886
NeilBrownf6705572006-03-27 01:18:11 -08005887 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005888 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005889 mddev->new_layout = mddev->layout;
5890 mddev->delta_disks = 0;
5891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 return 0;
5893}
5894
NeilBrownfd01b882011-10-11 16:47:53 +11005895void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005896{
Dan Williamsb522adc2009-03-31 15:00:31 +11005897 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5898
5899 if (mddev->external_size)
5900 return;
5901
Dan Williams1f403622009-03-31 14:59:03 +11005902 mddev->array_sectors = array_sectors;
5903}
5904EXPORT_SYMBOL(md_set_array_sectors);
5905
NeilBrownfd01b882011-10-11 16:47:53 +11005906static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005907{
NeilBrown3cb03002011-10-11 16:45:26 +11005908 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005909 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005910 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005911
5912 if (mddev->pers->resize == NULL)
5913 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005914 /* The "num_sectors" is the number of sectors of each device that
5915 * is used. This can only make sense for arrays with redundancy.
5916 * linear and raid0 always use whatever space is available. We can only
5917 * consider changing this number if no resync or reconstruction is
5918 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005919 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005920 * of each device. If num_sectors is zero, we find the largest size
5921 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005922 */
5923 if (mddev->sync_thread)
5924 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005925 if (mddev->bitmap)
5926 /* Sorry, cannot grow a bitmap yet, just remove it,
5927 * grow, and re-add.
5928 */
5929 return -EBUSY;
NeilBrowndafb20f2012-03-19 12:46:39 +11005930 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005931 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005932
Andre Nolld71f9f82008-07-11 22:02:22 +10005933 if (fit && (num_sectors == 0 || num_sectors > avail))
5934 num_sectors = avail;
5935 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005936 return -ENOSPC;
5937 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005938 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005939 if (!rv)
5940 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005941 return rv;
5942}
5943
NeilBrownfd01b882011-10-11 16:47:53 +11005944static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b92006-01-06 00:20:54 -08005945{
5946 int rv;
5947 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005948 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005949 return -EINVAL;
5950 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005951 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005952 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005953 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005954 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005955 mddev->delta_disks = raid_disks - mddev->raid_disks;
5956
5957 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005958 if (rv < 0)
5959 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005960 return rv;
5961}
5962
5963
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964/*
5965 * update_array_info is used to change the configuration of an
5966 * on-line array.
5967 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5968 * fields in the info are checked against the array.
5969 * Any differences that cannot be handled will cause an error.
5970 * Normally, only one change can be managed at a time.
5971 */
NeilBrownfd01b882011-10-11 16:47:53 +11005972static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
5974 int rv = 0;
5975 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005976 int state = 0;
5977
5978 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005979 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005980 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
5982 if (mddev->major_version != info->major_version ||
5983 mddev->minor_version != info->minor_version ||
5984/* mddev->patch_version != info->patch_version || */
5985 mddev->ctime != info->ctime ||
5986 mddev->level != info->level ||
5987/* mddev->layout != info->layout || */
5988 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005989 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005990 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5991 ((state^info->state) & 0xfffffe00)
5992 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return -EINVAL;
5994 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005995 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5996 cnt++;
5997 if (mddev->raid_disks != info->raid_disks)
5998 cnt++;
5999 if (mddev->layout != info->layout)
6000 cnt++;
6001 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6002 cnt++;
6003 if (cnt == 0)
6004 return 0;
6005 if (cnt > 1)
6006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 if (mddev->layout != info->layout) {
6009 /* Change layout
6010 * we don't need to do anything at the md level, the
6011 * personality will take care of it all.
6012 */
NeilBrown50ac1682009-06-18 08:47:55 +10006013 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006015 else {
6016 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10006017 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10006018 if (rv)
6019 mddev->new_layout = mddev->layout;
6020 return rv;
6021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006023 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10006024 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
NeilBrownda943b92006-01-06 00:20:54 -08006026 if (mddev->raid_disks != info->raid_disks)
6027 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
NeilBrown36fa3062005-09-09 16:23:45 -07006029 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6030 if (mddev->pers->quiesce == NULL)
6031 return -EINVAL;
6032 if (mddev->recovery || mddev->sync_thread)
6033 return -EBUSY;
6034 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6035 /* add the bitmap */
6036 if (mddev->bitmap)
6037 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11006038 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07006039 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11006040 mddev->bitmap_info.offset =
6041 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07006042 mddev->pers->quiesce(mddev, 1);
6043 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10006044 if (!rv)
6045 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07006046 if (rv)
6047 bitmap_destroy(mddev);
6048 mddev->pers->quiesce(mddev, 0);
6049 } else {
6050 /* remove the bitmap */
6051 if (!mddev->bitmap)
6052 return -ENOENT;
6053 if (mddev->bitmap->file)
6054 return -EINVAL;
6055 mddev->pers->quiesce(mddev, 1);
6056 bitmap_destroy(mddev);
6057 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11006058 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07006059 }
6060 }
NeilBrown850b2b42006-10-03 01:15:46 -07006061 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 return rv;
6063}
6064
NeilBrownfd01b882011-10-11 16:47:53 +11006065static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
NeilBrown3cb03002011-10-11 16:45:26 +11006067 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
6069 if (mddev->pers == NULL)
6070 return -ENODEV;
6071
6072 rdev = find_rdev(mddev, dev);
6073 if (!rdev)
6074 return -ENODEV;
6075
6076 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006077 if (!test_bit(Faulty, &rdev->flags))
6078 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return 0;
6080}
6081
Andre Noll2f9618c2008-04-25 18:57:58 +02006082/*
6083 * We have a problem here : there is no easy way to give a CHS
6084 * virtual geometry. We currently pretend that we have a 2 heads
6085 * 4 sectors (with a BIG number of cylinders...). This drives
6086 * dosfs just mad... ;-)
6087 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006088static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6089{
NeilBrownfd01b882011-10-11 16:47:53 +11006090 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006091
6092 geo->heads = 2;
6093 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006094 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006095 return 0;
6096}
6097
Al Viroa39907f2008-03-02 10:31:15 -05006098static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 unsigned int cmd, unsigned long arg)
6100{
6101 int err = 0;
6102 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006103 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006104 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
NeilBrown506c9e42011-12-23 10:17:26 +11006106 switch (cmd) {
6107 case RAID_VERSION:
6108 case GET_ARRAY_INFO:
6109 case GET_DISK_INFO:
6110 break;
6111 default:
6112 if (!capable(CAP_SYS_ADMIN))
6113 return -EACCES;
6114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 /*
6117 * Commands dealing with the RAID driver but not any
6118 * particular array:
6119 */
6120 switch (cmd)
6121 {
6122 case RAID_VERSION:
6123 err = get_version(argp);
6124 goto done;
6125
6126 case PRINT_RAID_DEBUG:
6127 err = 0;
6128 md_print_devices();
6129 goto done;
6130
6131#ifndef MODULE
6132 case RAID_AUTORUN:
6133 err = 0;
6134 autostart_arrays(arg);
6135 goto done;
6136#endif
6137 default:;
6138 }
6139
6140 /*
6141 * Commands creating/starting a new array:
6142 */
6143
Al Viroa39907f2008-03-02 10:31:15 -05006144 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
6146 if (!mddev) {
6147 BUG();
6148 goto abort;
6149 }
6150
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 err = mddev_lock(mddev);
6152 if (err) {
6153 printk(KERN_INFO
6154 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6155 err, cmd);
6156 goto abort;
6157 }
6158
6159 switch (cmd)
6160 {
6161 case SET_ARRAY_INFO:
6162 {
6163 mdu_array_info_t info;
6164 if (!arg)
6165 memset(&info, 0, sizeof(info));
6166 else if (copy_from_user(&info, argp, sizeof(info))) {
6167 err = -EFAULT;
6168 goto abort_unlock;
6169 }
6170 if (mddev->pers) {
6171 err = update_array_info(mddev, &info);
6172 if (err) {
6173 printk(KERN_WARNING "md: couldn't update"
6174 " array info. %d\n", err);
6175 goto abort_unlock;
6176 }
6177 goto done_unlock;
6178 }
6179 if (!list_empty(&mddev->disks)) {
6180 printk(KERN_WARNING
6181 "md: array %s already has disks!\n",
6182 mdname(mddev));
6183 err = -EBUSY;
6184 goto abort_unlock;
6185 }
6186 if (mddev->raid_disks) {
6187 printk(KERN_WARNING
6188 "md: array %s already initialised!\n",
6189 mdname(mddev));
6190 err = -EBUSY;
6191 goto abort_unlock;
6192 }
6193 err = set_array_info(mddev, &info);
6194 if (err) {
6195 printk(KERN_WARNING "md: couldn't set"
6196 " array info. %d\n", err);
6197 goto abort_unlock;
6198 }
6199 }
6200 goto done_unlock;
6201
6202 default:;
6203 }
6204
6205 /*
6206 * Commands querying/configuring an existing array:
6207 */
NeilBrown32a76272005-06-21 17:17:14 -07006208 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006209 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006210 if ((!mddev->raid_disks && !mddev->external)
6211 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6212 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6213 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 err = -ENODEV;
6215 goto abort_unlock;
6216 }
6217
6218 /*
6219 * Commands even a read-only array can execute:
6220 */
6221 switch (cmd)
6222 {
6223 case GET_ARRAY_INFO:
6224 err = get_array_info(mddev, argp);
6225 goto done_unlock;
6226
NeilBrown32a76272005-06-21 17:17:14 -07006227 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006228 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006229 goto done_unlock;
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 case GET_DISK_INFO:
6232 err = get_disk_info(mddev, argp);
6233 goto done_unlock;
6234
6235 case RESTART_ARRAY_RW:
6236 err = restart_array(mddev);
6237 goto done_unlock;
6238
6239 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006240 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 goto done_unlock;
6242
6243 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006244 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 goto done_unlock;
6246
Dan Williamse2218352010-05-12 08:25:37 +10006247 case BLKROSET:
6248 if (get_user(ro, (int __user *)(arg))) {
6249 err = -EFAULT;
6250 goto done_unlock;
6251 }
6252 err = -EINVAL;
6253
6254 /* if the bdev is going readonly the value of mddev->ro
6255 * does not matter, no writes are coming
6256 */
6257 if (ro)
6258 goto done_unlock;
6259
6260 /* are we are already prepared for writes? */
6261 if (mddev->ro != 1)
6262 goto done_unlock;
6263
6264 /* transitioning to readauto need only happen for
6265 * arrays that call md_write_start
6266 */
6267 if (mddev->pers) {
6268 err = restart_array(mddev);
6269 if (err == 0) {
6270 mddev->ro = 2;
6271 set_disk_ro(mddev->gendisk, 0);
6272 }
6273 }
6274 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 }
6276
6277 /*
6278 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006279 * superblock, so we do not allow them on read-only arrays.
6280 * However non-MD ioctls (e.g. get-size) will still come through
6281 * here and hit the 'default' below, so only disallow
6282 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006284 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006285 if (mddev->ro == 2) {
6286 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006287 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006288 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6289 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006290 } else {
6291 err = -EROFS;
6292 goto abort_unlock;
6293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 }
6295
6296 switch (cmd)
6297 {
6298 case ADD_NEW_DISK:
6299 {
6300 mdu_disk_info_t info;
6301 if (copy_from_user(&info, argp, sizeof(info)))
6302 err = -EFAULT;
6303 else
6304 err = add_new_disk(mddev, &info);
6305 goto done_unlock;
6306 }
6307
6308 case HOT_REMOVE_DISK:
6309 err = hot_remove_disk(mddev, new_decode_dev(arg));
6310 goto done_unlock;
6311
6312 case HOT_ADD_DISK:
6313 err = hot_add_disk(mddev, new_decode_dev(arg));
6314 goto done_unlock;
6315
6316 case SET_DISK_FAULTY:
6317 err = set_disk_faulty(mddev, new_decode_dev(arg));
6318 goto done_unlock;
6319
6320 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006321 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 goto done_unlock;
6323
NeilBrown32a76272005-06-21 17:17:14 -07006324 case SET_BITMAP_FILE:
6325 err = set_bitmap_file(mddev, (int)arg);
6326 goto done_unlock;
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 err = -EINVAL;
6330 goto abort_unlock;
6331 }
6332
6333done_unlock:
6334abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006335 if (mddev->hold_active == UNTIL_IOCTL &&
6336 err != -EINVAL)
6337 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 mddev_unlock(mddev);
6339
6340 return err;
6341done:
6342 if (err)
6343 MD_BUG();
6344abort:
6345 return err;
6346}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006347#ifdef CONFIG_COMPAT
6348static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6349 unsigned int cmd, unsigned long arg)
6350{
6351 switch (cmd) {
6352 case HOT_REMOVE_DISK:
6353 case HOT_ADD_DISK:
6354 case SET_DISK_FAULTY:
6355 case SET_BITMAP_FILE:
6356 /* These take in integer arg, do not convert */
6357 break;
6358 default:
6359 arg = (unsigned long)compat_ptr(arg);
6360 break;
6361 }
6362
6363 return md_ioctl(bdev, mode, cmd, arg);
6364}
6365#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
Al Viroa39907f2008-03-02 10:31:15 -05006367static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
6369 /*
6370 * Succeed if we can lock the mddev, which confirms that
6371 * it isn't being stopped right now.
6372 */
NeilBrownfd01b882011-10-11 16:47:53 +11006373 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 int err;
6375
NeilBrownd3374822009-01-09 08:31:10 +11006376 if (mddev->gendisk != bdev->bd_disk) {
6377 /* we are racing with mddev_put which is discarding this
6378 * bd_disk.
6379 */
6380 mddev_put(mddev);
6381 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006382 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006383 /* Then retry the open from the top */
6384 return -ERESTARTSYS;
6385 }
6386 BUG_ON(mddev != bdev->bd_disk->private_data);
6387
NeilBrownc8c00a62009-08-10 12:50:52 +10006388 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 goto out;
6390
6391 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006392 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006393 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006395 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 out:
6397 return err;
6398}
6399
Al Viroa39907f2008-03-02 10:31:15 -05006400static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401{
NeilBrownfd01b882011-10-11 16:47:53 +11006402 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006404 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006405 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 mddev_put(mddev);
6407
6408 return 0;
6409}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006410
6411static int md_media_changed(struct gendisk *disk)
6412{
NeilBrownfd01b882011-10-11 16:47:53 +11006413 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006414
6415 return mddev->changed;
6416}
6417
6418static int md_revalidate(struct gendisk *disk)
6419{
NeilBrownfd01b882011-10-11 16:47:53 +11006420 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006421
6422 mddev->changed = 0;
6423 return 0;
6424}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006425static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426{
6427 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006428 .open = md_open,
6429 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006430 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006431#ifdef CONFIG_COMPAT
6432 .compat_ioctl = md_compat_ioctl,
6433#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006434 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006435 .media_changed = md_media_changed,
6436 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437};
6438
Adrian Bunk75c96f82005-05-05 16:16:09 -07006439static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440{
NeilBrown2b8bf342011-10-11 16:48:23 +11006441 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 /*
6444 * md_thread is a 'system-thread', it's priority should be very
6445 * high. We avoid resource deadlocks individually in each
6446 * raid personality. (RAID5 does preallocation) We also use RR and
6447 * the very same RT priority as kswapd, thus we will never get
6448 * into a priority inversion deadlock.
6449 *
6450 * we definitely have to have equal or higher priority than
6451 * bdflush, otherwise bdflush will deadlock if there are too
6452 * many dirty RAID5 blocks.
6453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
NeilBrown6985c432005-10-19 21:23:47 -07006455 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006456 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
NeilBrown93588e22005-11-15 00:09:12 -08006458 /* We need to wait INTERRUPTIBLE so that
6459 * we don't add to the load-average.
6460 * That means we need to be sure no signals are
6461 * pending
6462 */
6463 if (signal_pending(current))
6464 flush_signals(current);
6465
6466 wait_event_interruptible_timeout
6467 (thread->wqueue,
6468 test_bit(THREAD_WAKEUP, &thread->flags)
6469 || kthread_should_stop(),
6470 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
NeilBrown6c987912011-01-14 09:13:53 +11006472 clear_bit(THREAD_WAKEUP, &thread->flags);
6473 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006474 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 return 0;
6478}
6479
NeilBrown2b8bf342011-10-11 16:48:23 +11006480void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481{
6482 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006483 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 set_bit(THREAD_WAKEUP, &thread->flags);
6485 wake_up(&thread->wqueue);
6486 }
6487}
6488
NeilBrown2b8bf342011-10-11 16:48:23 +11006489struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 const char *name)
6491{
NeilBrown2b8bf342011-10-11 16:48:23 +11006492 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
NeilBrown2b8bf342011-10-11 16:48:23 +11006494 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 if (!thread)
6496 return NULL;
6497
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 init_waitqueue_head(&thread->wqueue);
6499
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 thread->run = run;
6501 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006502 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006503 thread->tsk = kthread_run(md_thread, thread,
6504 "%s_%s",
6505 mdname(thread->mddev),
6506 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006507 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 kfree(thread);
6509 return NULL;
6510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 return thread;
6512}
6513
NeilBrown2b8bf342011-10-11 16:48:23 +11006514void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515{
NeilBrown2b8bf342011-10-11 16:48:23 +11006516 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006517 if (!thread)
6518 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006519 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006520 /* Locking ensures that mddev_unlock does not wake_up a
6521 * non-existent thread
6522 */
6523 spin_lock(&pers_lock);
6524 *threadp = NULL;
6525 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006526
6527 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 kfree(thread);
6529}
6530
NeilBrownfd01b882011-10-11 16:47:53 +11006531void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532{
6533 if (!mddev) {
6534 MD_BUG();
6535 return;
6536 }
6537
NeilBrownb2d444d2005-11-08 21:39:31 -08006538 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006540
NeilBrownde393cd2011-07-28 11:31:48 +10006541 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 return;
6543 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006544 if (mddev->degraded)
6545 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006546 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6548 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6549 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006550 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006551 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006552 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553}
6554
6555/* seq_file implementation /proc/mdstat */
6556
6557static void status_unused(struct seq_file *seq)
6558{
6559 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006560 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 seq_printf(seq, "unused devices: ");
6563
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006564 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 char b[BDEVNAME_SIZE];
6566 i++;
6567 seq_printf(seq, "%s ",
6568 bdevname(rdev->bdev,b));
6569 }
6570 if (!i)
6571 seq_printf(seq, "<none>");
6572
6573 seq_printf(seq, "\n");
6574}
6575
6576
NeilBrownfd01b882011-10-11 16:47:53 +11006577static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578{
NeilBrowndd71cf62009-05-07 12:49:35 +10006579 sector_t max_sectors, resync, res;
6580 unsigned long dt, db;
6581 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006582 int scale;
6583 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrowndd71cf62009-05-07 12:49:35 +10006585 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
6587 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006588 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006590 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
6592 /*
6593 * Should not happen.
6594 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006595 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 MD_BUG();
6597 return;
6598 }
NeilBrown4588b422006-03-27 01:18:04 -08006599 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006600 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006601 * u32, as those are the requirements for sector_div.
6602 * Thus 'scale' must be at least 10
6603 */
6604 scale = 10;
6605 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006606 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006607 scale++;
6608 }
6609 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006610 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006611
6612 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 {
NeilBrown4588b422006-03-27 01:18:04 -08006614 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 seq_printf(seq, "[");
6616 for (i = 0; i < x; i++)
6617 seq_printf(seq, "=");
6618 seq_printf(seq, ">");
6619 for (i = 0; i < y; i++)
6620 seq_printf(seq, ".");
6621 seq_printf(seq, "] ");
6622 }
NeilBrown4588b422006-03-27 01:18:04 -08006623 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006624 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6625 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006626 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6627 "check" :
6628 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6629 "resync" : "recovery"))),
6630 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006631 (unsigned long long) resync/2,
6632 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 * dt: time from mark until now
6636 * db: blocks written from mark until now
6637 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006638 *
6639 * rt is a sector_t, so could be 32bit or 64bit.
6640 * So we divide before multiply in case it is 32bit and close
6641 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006642 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006643 * near the end of resync when the number of remaining sectors
6644 * is close to 'db'.
6645 * We then divide rt by 32 after multiplying by db to compensate.
6646 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 */
6648 dt = ((jiffies - mddev->resync_mark) / HZ);
6649 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006650 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6651 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
NeilBrowndd71cf62009-05-07 12:49:35 +10006653 rt = max_sectors - resync; /* number of remaining sectors */
6654 sector_div(rt, db/32+1);
6655 rt *= dt;
6656 rt >>= 5;
6657
6658 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6659 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
NeilBrownff4e8d92006-07-10 04:44:16 -07006661 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662}
6663
6664static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6665{
6666 struct list_head *tmp;
6667 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006668 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
6670 if (l >= 0x10000)
6671 return NULL;
6672 if (!l--)
6673 /* header */
6674 return (void*)1;
6675
6676 spin_lock(&all_mddevs_lock);
6677 list_for_each(tmp,&all_mddevs)
6678 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006679 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 mddev_get(mddev);
6681 spin_unlock(&all_mddevs_lock);
6682 return mddev;
6683 }
6684 spin_unlock(&all_mddevs_lock);
6685 if (!l--)
6686 return (void*)2;/* tail */
6687 return NULL;
6688}
6689
6690static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6691{
6692 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006693 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694
6695 ++*pos;
6696 if (v == (void*)2)
6697 return NULL;
6698
6699 spin_lock(&all_mddevs_lock);
6700 if (v == (void*)1)
6701 tmp = all_mddevs.next;
6702 else
6703 tmp = mddev->all_mddevs.next;
6704 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006705 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 else {
6707 next_mddev = (void*)2;
6708 *pos = 0x10000;
6709 }
6710 spin_unlock(&all_mddevs_lock);
6711
6712 if (v != (void*)1)
6713 mddev_put(mddev);
6714 return next_mddev;
6715
6716}
6717
6718static void md_seq_stop(struct seq_file *seq, void *v)
6719{
NeilBrownfd01b882011-10-11 16:47:53 +11006720 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
6722 if (mddev && v != (void*)1 && v != (void*)2)
6723 mddev_put(mddev);
6724}
6725
6726static int md_seq_show(struct seq_file *seq, void *v)
6727{
NeilBrownfd01b882011-10-11 16:47:53 +11006728 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006729 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006730 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
6732 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006733 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 seq_printf(seq, "Personalities : ");
6735 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006736 list_for_each_entry(pers, &pers_list, list)
6737 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 spin_unlock(&pers_lock);
6740 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006741 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 return 0;
6743 }
6744 if (v == (void*)2) {
6745 status_unused(seq);
6746 return 0;
6747 }
6748
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006749 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006751
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6753 seq_printf(seq, "%s : %sactive", mdname(mddev),
6754 mddev->pers ? "" : "in");
6755 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006756 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006758 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006759 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 seq_printf(seq, " %s", mddev->pers->name);
6761 }
6762
Andre Nolldd8ac332009-03-31 14:33:13 +11006763 sectors = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11006764 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 char b[BDEVNAME_SIZE];
6766 seq_printf(seq, " %s[%d]",
6767 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006768 if (test_bit(WriteMostly, &rdev->flags))
6769 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006770 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 seq_printf(seq, "(F)");
6772 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11006773 }
6774 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07006775 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11006776 if (test_bit(Replacement, &rdev->flags))
6777 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11006778 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 }
6780
6781 if (!list_empty(&mddev->disks)) {
6782 if (mddev->pers)
6783 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006784 (unsigned long long)
6785 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 else
6787 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006788 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006790 if (mddev->persistent) {
6791 if (mddev->major_version != 0 ||
6792 mddev->minor_version != 90) {
6793 seq_printf(seq," super %d.%d",
6794 mddev->major_version,
6795 mddev->minor_version);
6796 }
NeilBrowne6910632008-02-06 01:39:51 -08006797 } else if (mddev->external)
6798 seq_printf(seq, " super external:%s",
6799 mddev->metadata_type);
6800 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006801 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
6803 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006804 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006806 if (mddev->pers->sync_request) {
6807 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006808 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006809 seq_printf(seq, "\n ");
6810 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6811 seq_printf(seq, "\tresync=DELAYED\n ");
6812 else if (mddev->recovery_cp < MaxSector)
6813 seq_printf(seq, "\tresync=PENDING\n ");
6814 }
NeilBrown32a76272005-06-21 17:17:14 -07006815 } else
6816 seq_printf(seq, "\n ");
6817
NeilBrown57148962012-03-19 12:46:40 +11006818 bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
6820 seq_printf(seq, "\n");
6821 }
6822 mddev_unlock(mddev);
6823
6824 return 0;
6825}
6826
Jan Engelhardt110518b2009-05-07 12:49:37 +10006827static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 .start = md_seq_start,
6829 .next = md_seq_next,
6830 .stop = md_seq_stop,
6831 .show = md_seq_show,
6832};
6833
6834static int md_seq_open(struct inode *inode, struct file *file)
6835{
Kay Sieversf1514632011-07-12 20:48:39 +02006836 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 int error;
6838
6839 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006840 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006841 return error;
6842
6843 seq = file->private_data;
6844 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 return error;
6846}
6847
NeilBrownd7603b72006-01-06 00:20:30 -08006848static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6849{
Kay Sieversf1514632011-07-12 20:48:39 +02006850 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006851 int mask;
6852
6853 poll_wait(filp, &md_event_waiters, wait);
6854
6855 /* always allow read */
6856 mask = POLLIN | POLLRDNORM;
6857
Kay Sieversf1514632011-07-12 20:48:39 +02006858 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006859 mask |= POLLERR | POLLPRI;
6860 return mask;
6861}
6862
Arjan van de Venfa027c22007-02-12 00:55:33 -08006863static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006864 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 .open = md_seq_open,
6866 .read = seq_read,
6867 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006868 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006869 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870};
6871
NeilBrown84fc4b52011-10-11 16:49:58 +11006872int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006875 list_add_tail(&p->list, &pers_list);
6876 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 spin_unlock(&pers_lock);
6878 return 0;
6879}
6880
NeilBrown84fc4b52011-10-11 16:49:58 +11006881int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882{
NeilBrown2604b702006-01-06 00:20:36 -08006883 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006885 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 spin_unlock(&pers_lock);
6887 return 0;
6888}
6889
NeilBrownfd01b882011-10-11 16:47:53 +11006890static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891{
NeilBrown3cb03002011-10-11 16:45:26 +11006892 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006894 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
6896 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006897 rcu_read_lock();
6898 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006900 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6901 (int)part_stat_read(&disk->part0, sectors[1]) -
6902 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006903 /* sync IO will cause sync_io to increase before the disk_stats
6904 * as sync_io is counted when a request starts, and
6905 * disk_stats is counted when it completes.
6906 * So resync activity will cause curr_events to be smaller than
6907 * when there was no such activity.
6908 * non-sync IO will cause disk_stat to increase without
6909 * increasing sync_io so curr_events will (eventually)
6910 * be larger than it was before. Once it becomes
6911 * substantially larger, the test below will cause
6912 * the array to appear non-idle, and resync will slow
6913 * down.
6914 * If there is a lot of outstanding resync activity when
6915 * we set last_event to curr_events, then all that activity
6916 * completing might cause the array to appear non-idle
6917 * and resync will be slowed down even though there might
6918 * not have been non-resync activity. This will only
6919 * happen once though. 'last_events' will soon reflect
6920 * the state where there is little or no outstanding
6921 * resync requests, and further resync activity will
6922 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006923 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006925 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 rdev->last_events = curr_events;
6927 idle = 0;
6928 }
6929 }
NeilBrown4b809912008-07-21 17:05:25 +10006930 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 return idle;
6932}
6933
NeilBrownfd01b882011-10-11 16:47:53 +11006934void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935{
6936 /* another "blocks" (512byte) blocks have been synced */
6937 atomic_sub(blocks, &mddev->recovery_active);
6938 wake_up(&mddev->recovery_wait);
6939 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006940 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 md_wakeup_thread(mddev->thread);
6942 // stop recovery, signal do_sync ....
6943 }
6944}
6945
6946
NeilBrown06d91a52005-06-21 17:17:12 -07006947/* md_write_start(mddev, bi)
6948 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006949 * in superblock) before writing, schedule a superblock update
6950 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006951 */
NeilBrownfd01b882011-10-11 16:47:53 +11006952void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953{
Neil Brown0fd62b82008-06-28 08:31:36 +10006954 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006955 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006956 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006957
NeilBrownf91de922005-11-08 21:39:36 -08006958 BUG_ON(mddev->ro == 1);
6959 if (mddev->ro == 2) {
6960 /* need to switch to read/write */
6961 mddev->ro = 0;
6962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6963 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006964 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006965 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006966 }
NeilBrown06d91a52005-06-21 17:17:12 -07006967 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006968 if (mddev->safemode == 1)
6969 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006970 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006971 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006972 if (mddev->in_sync) {
6973 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006974 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006975 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006976 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006977 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006978 }
NeilBrowna9701a32005-11-08 21:39:34 -08006979 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006980 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006981 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006982 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006983 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006984 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985}
6986
NeilBrownfd01b882011-10-11 16:47:53 +11006987void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988{
6989 if (atomic_dec_and_test(&mddev->writes_pending)) {
6990 if (mddev->safemode == 2)
6991 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006992 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6994 }
6995}
6996
NeilBrown2a2275d2007-01-26 00:57:11 -08006997/* md_allow_write(mddev)
6998 * Calling this ensures that the array is marked 'active' so that writes
6999 * may proceed without blocking. It is important to call this before
7000 * attempting a GFP_KERNEL allocation while holding the mddev lock.
7001 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07007002 *
7003 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7004 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08007005 */
NeilBrownfd01b882011-10-11 16:47:53 +11007006int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08007007{
7008 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007009 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007010 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007011 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10007012 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07007013 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007014
7015 spin_lock_irq(&mddev->write_lock);
7016 if (mddev->in_sync) {
7017 mddev->in_sync = 0;
7018 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10007019 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08007020 if (mddev->safemode_delay &&
7021 mddev->safemode == 0)
7022 mddev->safemode = 1;
7023 spin_unlock_irq(&mddev->write_lock);
7024 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007025 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08007026 } else
7027 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07007028
NeilBrown070dc6d2010-08-30 17:33:34 +10007029 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07007030 return -EAGAIN;
7031 else
7032 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08007033}
7034EXPORT_SYMBOL_GPL(md_allow_write);
7035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036#define SYNC_MARKS 10
7037#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11007038void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039{
NeilBrownfd01b882011-10-11 16:47:53 +11007040 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 unsigned int currspeed = 0,
7042 window;
NeilBrown57afd892005-06-21 17:17:13 -07007043 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 unsigned long mark[SYNC_MARKS];
7045 sector_t mark_cnt[SYNC_MARKS];
7046 int last_mark,m;
7047 struct list_head *tmp;
7048 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07007049 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007050 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07007051 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
7053 /* just incase thread restarts... */
7054 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7055 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007056 if (mddev->ro) /* never try to sync a read-only array */
7057 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
NeilBrown61df9d92006-10-03 01:15:57 -07007059 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7060 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7061 desc = "data-check";
7062 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7063 desc = "requested-resync";
7064 else
7065 desc = "resync";
7066 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7067 desc = "reshape";
7068 else
7069 desc = "recovery";
7070
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 /* we overload curr_resync somewhat here.
7072 * 0 == not engaged in resync at all
7073 * 2 == checking that there is no conflict with another sync
7074 * 1 == like 2, but have yielded to allow conflicting resync to
7075 * commense
7076 * other == active in resync - this many blocks
7077 *
7078 * Before starting a resync we must have set curr_resync to
7079 * 2, and then checked that every "conflicting" array has curr_resync
7080 * less than ours. When we find one that is the same or higher
7081 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7082 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7083 * This will mean we have to start checking from the beginning again.
7084 *
7085 */
7086
7087 do {
7088 mddev->curr_resync = 2;
7089
7090 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007091 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007092 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007093
7094 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007096 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 if (mddev2 == mddev)
7098 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007099 if (!mddev->parallel_resync
7100 && mddev2->curr_resync
7101 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 DEFINE_WAIT(wq);
7103 if (mddev < mddev2 && mddev->curr_resync == 2) {
7104 /* arbitrarily yield */
7105 mddev->curr_resync = 1;
7106 wake_up(&resync_wait);
7107 }
7108 if (mddev > mddev2 && mddev->curr_resync == 1)
7109 /* no need to wait here, we can wait the next
7110 * time 'round when curr_resync == 2
7111 */
7112 continue;
NeilBrown97441972008-09-19 11:49:54 +10007113 /* We need to wait 'interruptible' so as not to
7114 * contribute to the load average, and not to
7115 * be caught by 'softlockup'
7116 */
7117 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007118 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007119 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007120 printk(KERN_INFO "md: delaying %s of %s"
7121 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007123 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007125 if (signal_pending(current))
7126 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 schedule();
7128 finish_wait(&resync_wait, &wq);
7129 goto try_again;
7130 }
7131 finish_wait(&resync_wait, &wq);
7132 }
7133 }
7134 } while (mddev->curr_resync < 2);
7135
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007136 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007137 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007139 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 */
7141 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007142 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007143 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007144 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7145 j = mddev->resync_min;
7146 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007147 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007148
NeilBrownccfcc3c2006-03-27 01:18:09 -08007149 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007150 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007151 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007153 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007154 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007155 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007156 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007157 if (rdev->raid_disk >= 0 &&
7158 !test_bit(Faulty, &rdev->flags) &&
7159 !test_bit(In_sync, &rdev->flags) &&
7160 rdev->recovery_offset < j)
7161 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007162 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
NeilBrown61df9d92006-10-03 01:15:57 -07007165 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7166 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7167 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007168 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007169 "(but not more than %d KB/sec) for %s.\n",
7170 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
NeilBrowneea1bf32009-03-31 14:27:02 +11007172 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007173
NeilBrown57afd892005-06-21 17:17:13 -07007174 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 for (m = 0; m < SYNC_MARKS; m++) {
7176 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007177 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 }
7179 last_mark = 0;
7180 mddev->resync_mark = mark[last_mark];
7181 mddev->resync_mark_cnt = mark_cnt[last_mark];
7182
7183 /*
7184 * Tune reconstruction:
7185 */
7186 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007187 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7188 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189
7190 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 last_check = 0;
7192
7193 if (j>2) {
7194 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007195 "md: resuming %s of %s from checkpoint.\n",
7196 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 mddev->curr_resync = j;
7198 }
NeilBrown75d3da42011-01-14 09:14:34 +11007199 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
7201 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007202 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
NeilBrown57afd892005-06-21 17:17:13 -07007204 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007205
NeilBrown7a91ee12009-05-26 12:57:21 +10007206 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7207 ((mddev->curr_resync > mddev->curr_resync_completed &&
7208 (mddev->curr_resync - mddev->curr_resync_completed)
7209 > (max_sectors >> 4)) ||
7210 (j - mddev->curr_resync_completed)*2
7211 >= mddev->resync_max - mddev->curr_resync_completed
7212 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007213 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007214 wait_event(mddev->recovery_wait,
7215 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007216 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007217 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007218 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007219 }
NeilBrownacb180b2009-04-14 16:28:34 +10007220
NeilBrowne62e58a2009-07-01 13:15:35 +10007221 while (j >= mddev->resync_max && !kthread_should_stop()) {
7222 /* As this condition is controlled by user-space,
7223 * we can block indefinitely, so use '_interruptible'
7224 * to avoid triggering warnings.
7225 */
7226 flush_signals(current); /* just in case */
7227 wait_event_interruptible(mddev->recovery_wait,
7228 mddev->resync_max > j
7229 || kthread_should_stop());
7230 }
NeilBrownacb180b2009-04-14 16:28:34 +10007231
7232 if (kthread_should_stop())
7233 goto interrupted;
7234
NeilBrown57afd892005-06-21 17:17:13 -07007235 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007236 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007237 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007238 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 goto out;
7240 }
NeilBrown57afd892005-06-21 17:17:13 -07007241
7242 if (!skipped) { /* actual IO requested */
7243 io_sectors += sectors;
7244 atomic_add(sectors, &mddev->recovery_active);
7245 }
7246
NeilBrowne875ece2011-07-28 11:39:24 +10007247 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7248 break;
7249
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 j += sectors;
7251 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007252 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007253 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007254 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007255 * visible in /proc/mdstat
7256 */
7257 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007258
7259 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 continue;
7261
NeilBrown57afd892005-06-21 17:17:13 -07007262 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 repeat:
7264 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7265 /* step marks */
7266 int next = (last_mark+1) % SYNC_MARKS;
7267
7268 mddev->resync_mark = mark[next];
7269 mddev->resync_mark_cnt = mark_cnt[next];
7270 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007271 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 last_mark = next;
7273 }
7274
7275
NeilBrownc6207272008-02-06 01:39:52 -08007276 if (kthread_should_stop())
7277 goto interrupted;
7278
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
7280 /*
7281 * this loop exits only if either when we are slower than
7282 * the 'hard' speed limit, or the system was IO-idle for
7283 * a jiffy.
7284 * the system might be non-idle CPU-wise, but we only care
7285 * about not overloading the IO subsystem. (things like an
7286 * e2fsck being done on the RAID array should execute fast)
7287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 cond_resched();
7289
NeilBrown57afd892005-06-21 17:17:13 -07007290 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7291 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
NeilBrown88202a02006-01-06 00:21:36 -08007293 if (currspeed > speed_min(mddev)) {
7294 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007295 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007296 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 goto repeat;
7298 }
7299 }
7300 }
NeilBrown61df9d92006-10-03 01:15:57 -07007301 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 /*
7303 * this also signals 'finished resyncing' to md_stop
7304 */
7305 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7307
7308 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007309 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
NeilBrowndfc70642008-05-23 13:04:39 -07007311 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007312 mddev->curr_resync > 2) {
7313 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7314 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7315 if (mddev->curr_resync >= mddev->recovery_cp) {
7316 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007317 "md: checkpointing %s of %s.\n",
7318 desc, mdname(mddev));
NeilBrowndb91ff52012-02-07 12:01:51 +11007319 mddev->recovery_cp =
7320 mddev->curr_resync_completed;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007321 }
7322 } else
7323 mddev->recovery_cp = MaxSector;
7324 } else {
7325 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7326 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007327 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11007328 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007329 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007330 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007331 !test_bit(Faulty, &rdev->flags) &&
7332 !test_bit(In_sync, &rdev->flags) &&
7333 rdev->recovery_offset < mddev->curr_resync)
7334 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007335 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 }
NeilBrowndb91ff52012-02-07 12:01:51 +11007338 skip:
NeilBrown17571282006-12-10 02:20:52 -08007339 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
NeilBrownc07b70a2009-12-14 12:49:48 +11007341 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7342 /* We completed so min/max setting can be forgotten if used. */
7343 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7344 mddev->resync_min = 0;
7345 mddev->resync_max = MaxSector;
7346 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7347 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 mddev->curr_resync = 0;
7349 wake_up(&resync_wait);
7350 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7351 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007352 return;
7353
7354 interrupted:
7355 /*
7356 * got a signal, exit.
7357 */
7358 printk(KERN_INFO
7359 "md: md_do_sync() got signal ... exiting\n");
7360 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7361 goto out;
7362
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363}
NeilBrown29269552006-03-27 01:18:10 -08007364EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
NeilBrownfd01b882011-10-11 16:47:53 +11007366static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007367{
NeilBrown3cb03002011-10-11 16:45:26 +11007368 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007369 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11007370 int removed = 0;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007371
NeilBrown97e4f422009-03-31 14:33:13 +11007372 mddev->curr_resync_completed = 0;
7373
NeilBrowndafb20f2012-03-19 12:46:39 +11007374 rdev_for_each(rdev, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007375 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007376 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007377 (test_bit(Faulty, &rdev->flags) ||
7378 ! test_bit(In_sync, &rdev->flags)) &&
7379 atomic_read(&rdev->nr_pending)==0) {
7380 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007381 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007382 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007383 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11007384 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007385 }
7386 }
NeilBrownf2a371c2012-01-09 00:46:41 +11007387 if (removed)
7388 sysfs_notify(&mddev->kobj, NULL,
7389 "degraded");
7390
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007391
NeilBrowndafb20f2012-03-19 12:46:39 +11007392 rdev_for_each(rdev, mddev) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007393 if (rdev->raid_disk >= 0 &&
7394 !test_bit(In_sync, &rdev->flags) &&
7395 !test_bit(Faulty, &rdev->flags))
7396 spares++;
7397 if (rdev->raid_disk < 0
7398 && !test_bit(Faulty, &rdev->flags)) {
7399 rdev->recovery_offset = 0;
7400 if (mddev->pers->
7401 hot_add_disk(mddev, rdev) == 0) {
7402 if (sysfs_link_rdev(mddev, rdev))
7403 /* failure here is OK */;
NeilBrowndfc70642008-05-23 13:04:39 -07007404 spares++;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007405 md_new_event(mddev);
7406 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007407 }
NeilBrowndfc70642008-05-23 13:04:39 -07007408 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007409 }
7410 return spares;
7411}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007412
NeilBrownfd01b882011-10-11 16:47:53 +11007413static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007414{
NeilBrown3cb03002011-10-11 16:45:26 +11007415 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007416
7417 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007418 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007419 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7420 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7421 /* success...*/
7422 /* activate any spares */
7423 if (mddev->pers->spare_active(mddev))
7424 sysfs_notify(&mddev->kobj, NULL,
7425 "degraded");
7426 }
7427 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7428 mddev->pers->finish_reshape)
7429 mddev->pers->finish_reshape(mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007430
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007431 /* If array is no-longer degraded, then any saved_raid_disk
7432 * information must be scrapped. Also if any device is now
7433 * In_sync we must scrape the saved_raid_disk for that device
7434 * do the superblock for an incrementally recovered device
7435 * written out.
NeilBrown7ebc0be2011-01-14 09:14:33 +11007436 */
NeilBrowndafb20f2012-03-19 12:46:39 +11007437 rdev_for_each(rdev, mddev)
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007438 if (!mddev->degraded ||
7439 test_bit(In_sync, &rdev->flags))
NeilBrown7ebc0be2011-01-14 09:14:33 +11007440 rdev->saved_raid_disk = -1;
7441
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11007442 md_update_sb(mddev, 1);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007443 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7444 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7445 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7446 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7447 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7448 /* flag recovery needed just to double check */
7449 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7450 sysfs_notify_dirent_safe(mddev->sysfs_action);
7451 md_new_event(mddev);
Jonathan Brassow768e5872011-07-27 11:00:37 +10007452 if (mddev->event_work.func)
7453 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007454}
7455
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456/*
7457 * This routine is regularly called by all per-raid-array threads to
7458 * deal with generic issues like resync and super-block update.
7459 * Raid personalities that don't have a thread (linear/raid0) do not
7460 * need this as they never do any recovery or update the superblock.
7461 *
7462 * It does not do any resync itself, but rather "forks" off other threads
7463 * to do that as needed.
7464 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7465 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007466 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 * and wakeups up this thread which will reap the thread and finish up.
7468 * This thread also removes any faulty devices (with nr_pending == 0).
7469 *
7470 * The overall approach is:
7471 * 1/ if the superblock needs updating, update it.
7472 * 2/ If a recovery thread is running, don't do anything else.
7473 * 3/ If recovery has finished, clean up, possibly marking spares active.
7474 * 4/ If there are any faulty devices, remove them.
7475 * 5/ If array is degraded, try to add spares devices
7476 * 6/ If array has spares or is not in-sync, start a resync thread.
7477 */
NeilBrownfd01b882011-10-11 16:47:53 +11007478void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007480 if (mddev->suspended)
7481 return;
7482
NeilBrown5f404022005-06-21 17:17:16 -07007483 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007484 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
NeilBrownfca4d842005-06-21 17:17:11 -07007486 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007487 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007488 printk(KERN_INFO "md: %s in immediate safe mode\n",
7489 mdname(mddev));
7490 mddev->safemode = 2;
7491 }
7492 flush_signals(current);
7493 }
7494
NeilBrownc89a8ee2008-08-05 15:54:13 +10007495 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007498 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007500 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007501 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007502 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7503 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 ))
7505 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007506
NeilBrowndf5b89b2006-03-27 01:18:20 -08007507 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007508 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007509
NeilBrownc89a8ee2008-08-05 15:54:13 +10007510 if (mddev->ro) {
7511 /* Only thing we do on a ro array is remove
7512 * failed devices.
7513 */
NeilBrown3cb03002011-10-11 16:45:26 +11007514 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11007515 rdev_for_each(rdev, mddev)
NeilBrowna8c42c72011-01-31 13:47:13 +11007516 if (rdev->raid_disk >= 0 &&
7517 !test_bit(Blocked, &rdev->flags) &&
7518 test_bit(Faulty, &rdev->flags) &&
7519 atomic_read(&rdev->nr_pending)==0) {
7520 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11007521 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007522 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007523 rdev->raid_disk = -1;
7524 }
7525 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007526 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7527 goto unlock;
7528 }
7529
NeilBrown31a59e32008-04-30 00:52:30 -07007530 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007531 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007532 spin_lock_irq(&mddev->write_lock);
7533 if (mddev->safemode &&
7534 !atomic_read(&mddev->writes_pending) &&
7535 !mddev->in_sync &&
7536 mddev->recovery_cp == MaxSector) {
7537 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007538 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007539 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007540 }
7541 if (mddev->safemode == 1)
7542 mddev->safemode = 0;
7543 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007544 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007545 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007546 }
NeilBrownfca4d842005-06-21 17:17:11 -07007547
NeilBrown850b2b42006-10-03 01:15:46 -07007548 if (mddev->flags)
7549 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007550
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7552 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7553 /* resync/recovery still happening */
7554 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7555 goto unlock;
7556 }
7557 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007558 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 goto unlock;
7560 }
Neil Brown72a23c22008-06-28 08:31:41 +10007561 /* Set RUNNING before clearing NEEDED to avoid
7562 * any transients in the value of "sync_action".
7563 */
7564 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007565 /* Clear some bits that don't mean anything, but
7566 * might be left set
7567 */
NeilBrown24dd4692005-11-08 21:39:26 -08007568 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7569 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570
NeilBrowned209582012-04-24 10:23:14 +10007571 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7572 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007573 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 /* no recovery is running.
7575 * remove any failed drives, then
7576 * add spares if possible.
7577 * Spare are also removed and re-added, to allow
7578 * the personality to fail the re-add.
7579 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007581 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007582 if (mddev->pers->check_reshape == NULL ||
7583 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007584 /* Cannot proceed */
7585 goto unlock;
7586 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007587 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007588 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007589 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7590 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007591 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007592 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007593 } else if (mddev->recovery_cp < MaxSector) {
7594 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007595 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007596 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7597 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007598 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007599
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007601 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7602 /* We are adding a device or devices to an array
7603 * which has the bitmap stored on all devices.
7604 * So make sure all bitmap pages get written
7605 */
7606 bitmap_write_all(mddev->bitmap);
7607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 mddev->sync_thread = md_register_thread(md_do_sync,
7609 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007610 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 if (!mddev->sync_thread) {
7612 printk(KERN_ERR "%s: could not start resync"
7613 " thread...\n",
7614 mdname(mddev));
7615 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007616 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7617 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7618 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7619 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7620 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007621 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007623 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007624 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 }
7626 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007627 if (!mddev->sync_thread) {
7628 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7629 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7630 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007631 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007632 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 mddev_unlock(mddev);
7635 }
7636}
7637
NeilBrownfd01b882011-10-11 16:47:53 +11007638void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007639{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007640 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007641 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007642 !test_bit(Blocked, &rdev->flags) &&
7643 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007644 msecs_to_jiffies(5000));
7645 rdev_dec_pending(rdev, mddev);
7646}
7647EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7648
NeilBrown2230dfe2011-07-28 11:31:46 +10007649
7650/* Bad block management.
7651 * We can record which blocks on each device are 'bad' and so just
7652 * fail those blocks, or that stripe, rather than the whole device.
7653 * Entries in the bad-block table are 64bits wide. This comprises:
7654 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7655 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7656 * A 'shift' can be set so that larger blocks are tracked and
7657 * consequently larger devices can be covered.
7658 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7659 *
7660 * Locking of the bad-block table uses a seqlock so md_is_badblock
7661 * might need to retry if it is very unlucky.
7662 * We will sometimes want to check for bad blocks in a bi_end_io function,
7663 * so we use the write_seqlock_irq variant.
7664 *
7665 * When looking for a bad block we specify a range and want to
7666 * know if any block in the range is bad. So we binary-search
7667 * to the last range that starts at-or-before the given endpoint,
7668 * (or "before the sector after the target range")
7669 * then see if it ends after the given start.
7670 * We return
7671 * 0 if there are no known bad blocks in the range
7672 * 1 if there are known bad block which are all acknowledged
7673 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7674 * plus the start/length of the first bad section we overlap.
7675 */
7676int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7677 sector_t *first_bad, int *bad_sectors)
7678{
7679 int hi;
7680 int lo = 0;
7681 u64 *p = bb->page;
7682 int rv = 0;
7683 sector_t target = s + sectors;
7684 unsigned seq;
7685
7686 if (bb->shift > 0) {
7687 /* round the start down, and the end up */
7688 s >>= bb->shift;
7689 target += (1<<bb->shift) - 1;
7690 target >>= bb->shift;
7691 sectors = target - s;
7692 }
7693 /* 'target' is now the first block after the bad range */
7694
7695retry:
7696 seq = read_seqbegin(&bb->lock);
7697
7698 hi = bb->count;
7699
7700 /* Binary search between lo and hi for 'target'
7701 * i.e. for the last range that starts before 'target'
7702 */
7703 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7704 * are known not to be the last range before target.
7705 * VARIANT: hi-lo is the number of possible
7706 * ranges, and decreases until it reaches 1
7707 */
7708 while (hi - lo > 1) {
7709 int mid = (lo + hi) / 2;
7710 sector_t a = BB_OFFSET(p[mid]);
7711 if (a < target)
7712 /* This could still be the one, earlier ranges
7713 * could not. */
7714 lo = mid;
7715 else
7716 /* This and later ranges are definitely out. */
7717 hi = mid;
7718 }
7719 /* 'lo' might be the last that started before target, but 'hi' isn't */
7720 if (hi > lo) {
7721 /* need to check all range that end after 's' to see if
7722 * any are unacknowledged.
7723 */
7724 while (lo >= 0 &&
7725 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7726 if (BB_OFFSET(p[lo]) < target) {
7727 /* starts before the end, and finishes after
7728 * the start, so they must overlap
7729 */
7730 if (rv != -1 && BB_ACK(p[lo]))
7731 rv = 1;
7732 else
7733 rv = -1;
7734 *first_bad = BB_OFFSET(p[lo]);
7735 *bad_sectors = BB_LEN(p[lo]);
7736 }
7737 lo--;
7738 }
7739 }
7740
7741 if (read_seqretry(&bb->lock, seq))
7742 goto retry;
7743
7744 return rv;
7745}
7746EXPORT_SYMBOL_GPL(md_is_badblock);
7747
7748/*
7749 * Add a range of bad blocks to the table.
7750 * This might extend the table, or might contract it
7751 * if two adjacent ranges can be merged.
7752 * We binary-search to find the 'insertion' point, then
7753 * decide how best to handle it.
7754 */
7755static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7756 int acknowledged)
7757{
7758 u64 *p;
7759 int lo, hi;
7760 int rv = 1;
7761
7762 if (bb->shift < 0)
7763 /* badblocks are disabled */
7764 return 0;
7765
7766 if (bb->shift) {
7767 /* round the start down, and the end up */
7768 sector_t next = s + sectors;
7769 s >>= bb->shift;
7770 next += (1<<bb->shift) - 1;
7771 next >>= bb->shift;
7772 sectors = next - s;
7773 }
7774
7775 write_seqlock_irq(&bb->lock);
7776
7777 p = bb->page;
7778 lo = 0;
7779 hi = bb->count;
7780 /* Find the last range that starts at-or-before 's' */
7781 while (hi - lo > 1) {
7782 int mid = (lo + hi) / 2;
7783 sector_t a = BB_OFFSET(p[mid]);
7784 if (a <= s)
7785 lo = mid;
7786 else
7787 hi = mid;
7788 }
7789 if (hi > lo && BB_OFFSET(p[lo]) > s)
7790 hi = lo;
7791
7792 if (hi > lo) {
7793 /* we found a range that might merge with the start
7794 * of our new range
7795 */
7796 sector_t a = BB_OFFSET(p[lo]);
7797 sector_t e = a + BB_LEN(p[lo]);
7798 int ack = BB_ACK(p[lo]);
7799 if (e >= s) {
7800 /* Yes, we can merge with a previous range */
7801 if (s == a && s + sectors >= e)
7802 /* new range covers old */
7803 ack = acknowledged;
7804 else
7805 ack = ack && acknowledged;
7806
7807 if (e < s + sectors)
7808 e = s + sectors;
7809 if (e - a <= BB_MAX_LEN) {
7810 p[lo] = BB_MAKE(a, e-a, ack);
7811 s = e;
7812 } else {
7813 /* does not all fit in one range,
7814 * make p[lo] maximal
7815 */
7816 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7817 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7818 s = a + BB_MAX_LEN;
7819 }
7820 sectors = e - s;
7821 }
7822 }
7823 if (sectors && hi < bb->count) {
7824 /* 'hi' points to the first range that starts after 's'.
7825 * Maybe we can merge with the start of that range */
7826 sector_t a = BB_OFFSET(p[hi]);
7827 sector_t e = a + BB_LEN(p[hi]);
7828 int ack = BB_ACK(p[hi]);
7829 if (a <= s + sectors) {
7830 /* merging is possible */
7831 if (e <= s + sectors) {
7832 /* full overlap */
7833 e = s + sectors;
7834 ack = acknowledged;
7835 } else
7836 ack = ack && acknowledged;
7837
7838 a = s;
7839 if (e - a <= BB_MAX_LEN) {
7840 p[hi] = BB_MAKE(a, e-a, ack);
7841 s = e;
7842 } else {
7843 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7844 s = a + BB_MAX_LEN;
7845 }
7846 sectors = e - s;
7847 lo = hi;
7848 hi++;
7849 }
7850 }
7851 if (sectors == 0 && hi < bb->count) {
7852 /* we might be able to combine lo and hi */
7853 /* Note: 's' is at the end of 'lo' */
7854 sector_t a = BB_OFFSET(p[hi]);
7855 int lolen = BB_LEN(p[lo]);
7856 int hilen = BB_LEN(p[hi]);
7857 int newlen = lolen + hilen - (s - a);
7858 if (s >= a && newlen < BB_MAX_LEN) {
7859 /* yes, we can combine them */
7860 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7861 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7862 memmove(p + hi, p + hi + 1,
7863 (bb->count - hi - 1) * 8);
7864 bb->count--;
7865 }
7866 }
7867 while (sectors) {
7868 /* didn't merge (it all).
7869 * Need to add a range just before 'hi' */
7870 if (bb->count >= MD_MAX_BADBLOCKS) {
7871 /* No room for more */
7872 rv = 0;
7873 break;
7874 } else {
7875 int this_sectors = sectors;
7876 memmove(p + hi + 1, p + hi,
7877 (bb->count - hi) * 8);
7878 bb->count++;
7879
7880 if (this_sectors > BB_MAX_LEN)
7881 this_sectors = BB_MAX_LEN;
7882 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7883 sectors -= this_sectors;
7884 s += this_sectors;
7885 }
7886 }
7887
7888 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007889 if (!acknowledged)
7890 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007891 write_sequnlock_irq(&bb->lock);
7892
7893 return rv;
7894}
7895
NeilBrown3cb03002011-10-11 16:45:26 +11007896int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007897 int acknowledged)
7898{
7899 int rv = md_set_badblocks(&rdev->badblocks,
7900 s + rdev->data_offset, sectors, acknowledged);
7901 if (rv) {
7902 /* Make sure they get written out promptly */
NeilBrown8bd2f0a2011-12-08 16:26:08 +11007903 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown2230dfe2011-07-28 11:31:46 +10007904 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7905 md_wakeup_thread(rdev->mddev->thread);
7906 }
7907 return rv;
7908}
7909EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7910
7911/*
7912 * Remove a range of bad blocks from the table.
7913 * This may involve extending the table if we spilt a region,
7914 * but it must not fail. So if the table becomes full, we just
7915 * drop the remove request.
7916 */
7917static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7918{
7919 u64 *p;
7920 int lo, hi;
7921 sector_t target = s + sectors;
7922 int rv = 0;
7923
7924 if (bb->shift > 0) {
7925 /* When clearing we round the start up and the end down.
7926 * This should not matter as the shift should align with
7927 * the block size and no rounding should ever be needed.
7928 * However it is better the think a block is bad when it
7929 * isn't than to think a block is not bad when it is.
7930 */
7931 s += (1<<bb->shift) - 1;
7932 s >>= bb->shift;
7933 target >>= bb->shift;
7934 sectors = target - s;
7935 }
7936
7937 write_seqlock_irq(&bb->lock);
7938
7939 p = bb->page;
7940 lo = 0;
7941 hi = bb->count;
7942 /* Find the last range that starts before 'target' */
7943 while (hi - lo > 1) {
7944 int mid = (lo + hi) / 2;
7945 sector_t a = BB_OFFSET(p[mid]);
7946 if (a < target)
7947 lo = mid;
7948 else
7949 hi = mid;
7950 }
7951 if (hi > lo) {
7952 /* p[lo] is the last range that could overlap the
7953 * current range. Earlier ranges could also overlap,
7954 * but only this one can overlap the end of the range.
7955 */
7956 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7957 /* Partial overlap, leave the tail of this range */
7958 int ack = BB_ACK(p[lo]);
7959 sector_t a = BB_OFFSET(p[lo]);
7960 sector_t end = a + BB_LEN(p[lo]);
7961
7962 if (a < s) {
7963 /* we need to split this range */
7964 if (bb->count >= MD_MAX_BADBLOCKS) {
7965 rv = 0;
7966 goto out;
7967 }
7968 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7969 bb->count++;
7970 p[lo] = BB_MAKE(a, s-a, ack);
7971 lo++;
7972 }
7973 p[lo] = BB_MAKE(target, end - target, ack);
7974 /* there is no longer an overlap */
7975 hi = lo;
7976 lo--;
7977 }
7978 while (lo >= 0 &&
7979 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7980 /* This range does overlap */
7981 if (BB_OFFSET(p[lo]) < s) {
7982 /* Keep the early parts of this range. */
7983 int ack = BB_ACK(p[lo]);
7984 sector_t start = BB_OFFSET(p[lo]);
7985 p[lo] = BB_MAKE(start, s - start, ack);
7986 /* now low doesn't overlap, so.. */
7987 break;
7988 }
7989 lo--;
7990 }
7991 /* 'lo' is strictly before, 'hi' is strictly after,
7992 * anything between needs to be discarded
7993 */
7994 if (hi - lo > 1) {
7995 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7996 bb->count -= (hi - lo - 1);
7997 }
7998 }
7999
8000 bb->changed = 1;
8001out:
8002 write_sequnlock_irq(&bb->lock);
8003 return rv;
8004}
8005
NeilBrown3cb03002011-10-11 16:45:26 +11008006int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10008007{
8008 return md_clear_badblocks(&rdev->badblocks,
8009 s + rdev->data_offset,
8010 sectors);
8011}
8012EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8013
8014/*
8015 * Acknowledge all bad blocks in a list.
8016 * This only succeeds if ->changed is clear. It is used by
8017 * in-kernel metadata updates
8018 */
8019void md_ack_all_badblocks(struct badblocks *bb)
8020{
8021 if (bb->page == NULL || bb->changed)
8022 /* no point even trying */
8023 return;
8024 write_seqlock_irq(&bb->lock);
8025
majianpengecb178b2012-03-19 12:46:42 +11008026 if (bb->changed == 0 && bb->unacked_exist) {
NeilBrown2230dfe2011-07-28 11:31:46 +10008027 u64 *p = bb->page;
8028 int i;
8029 for (i = 0; i < bb->count ; i++) {
8030 if (!BB_ACK(p[i])) {
8031 sector_t start = BB_OFFSET(p[i]);
8032 int len = BB_LEN(p[i]);
8033 p[i] = BB_MAKE(start, len, 1);
8034 }
8035 }
NeilBrownde393cd2011-07-28 11:31:48 +10008036 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10008037 }
8038 write_sequnlock_irq(&bb->lock);
8039}
8040EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8041
NeilBrown16c791a2011-07-28 11:31:47 +10008042/* sysfs access to bad-blocks list.
8043 * We present two files.
8044 * 'bad-blocks' lists sector numbers and lengths of ranges that
8045 * are recorded as bad. The list is truncated to fit within
8046 * the one-page limit of sysfs.
8047 * Writing "sector length" to this file adds an acknowledged
8048 * bad block list.
8049 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8050 * been acknowledged. Writing to this file adds bad blocks
8051 * without acknowledging them. This is largely for testing.
8052 */
8053
8054static ssize_t
8055badblocks_show(struct badblocks *bb, char *page, int unack)
8056{
8057 size_t len;
8058 int i;
8059 u64 *p = bb->page;
8060 unsigned seq;
8061
8062 if (bb->shift < 0)
8063 return 0;
8064
8065retry:
8066 seq = read_seqbegin(&bb->lock);
8067
8068 len = 0;
8069 i = 0;
8070
8071 while (len < PAGE_SIZE && i < bb->count) {
8072 sector_t s = BB_OFFSET(p[i]);
8073 unsigned int length = BB_LEN(p[i]);
8074 int ack = BB_ACK(p[i]);
8075 i++;
8076
8077 if (unack && ack)
8078 continue;
8079
8080 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8081 (unsigned long long)s << bb->shift,
8082 length << bb->shift);
8083 }
NeilBrownde393cd2011-07-28 11:31:48 +10008084 if (unack && len == 0)
8085 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008086
8087 if (read_seqretry(&bb->lock, seq))
8088 goto retry;
8089
8090 return len;
8091}
8092
8093#define DO_DEBUG 1
8094
8095static ssize_t
8096badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8097{
8098 unsigned long long sector;
8099 int length;
8100 char newline;
8101#ifdef DO_DEBUG
8102 /* Allow clearing via sysfs *only* for testing/debugging.
8103 * Normally only a successful write may clear a badblock
8104 */
8105 int clear = 0;
8106 if (page[0] == '-') {
8107 clear = 1;
8108 page++;
8109 }
8110#endif /* DO_DEBUG */
8111
8112 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8113 case 3:
8114 if (newline != '\n')
8115 return -EINVAL;
8116 case 2:
8117 if (length <= 0)
8118 return -EINVAL;
8119 break;
8120 default:
8121 return -EINVAL;
8122 }
8123
8124#ifdef DO_DEBUG
8125 if (clear) {
8126 md_clear_badblocks(bb, sector, length);
8127 return len;
8128 }
8129#endif /* DO_DEBUG */
8130 if (md_set_badblocks(bb, sector, length, !unack))
8131 return len;
8132 else
8133 return -ENOSPC;
8134}
8135
Adrian Bunk75c96f82005-05-05 16:16:09 -07008136static int md_notify_reboot(struct notifier_block *this,
8137 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138{
8139 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008140 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008141 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
NeilBrownc744a652012-03-19 12:46:37 +11008143 for_each_mddev(mddev, tmp) {
8144 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10008145 if (mddev->pers)
8146 __md_stop_writes(mddev);
NeilBrownc744a652012-03-19 12:46:37 +11008147 mddev->safemode = 2;
8148 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008149 }
NeilBrownc744a652012-03-19 12:46:37 +11008150 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151 }
NeilBrownc744a652012-03-19 12:46:37 +11008152 /*
8153 * certain more exotic SCSI devices are known to be
8154 * volatile wrt too early system reboots. While the
8155 * right place to handle this issue is the given
8156 * driver, we do want to have a safe RAID driver ...
8157 */
8158 if (need_delay)
8159 mdelay(1000*1);
8160
Linus Torvalds1da177e2005-04-16 15:20:36 -07008161 return NOTIFY_DONE;
8162}
8163
Adrian Bunk75c96f82005-05-05 16:16:09 -07008164static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 .notifier_call = md_notify_reboot,
8166 .next = NULL,
8167 .priority = INT_MAX, /* before any real devices */
8168};
8169
8170static void md_geninit(void)
8171{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008172 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008174 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175}
8176
Adrian Bunk75c96f82005-05-05 16:16:09 -07008177static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178{
Tejun Heoe804ac72010-10-15 15:36:08 +02008179 int ret = -ENOMEM;
8180
Tejun Heoada609e2011-01-25 14:35:54 +01008181 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008182 if (!md_wq)
8183 goto err_wq;
8184
8185 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8186 if (!md_misc_wq)
8187 goto err_misc_wq;
8188
8189 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8190 goto err_md;
8191
8192 if ((ret = register_blkdev(0, "mdp")) < 0)
8193 goto err_mdp;
8194 mdp_major = ret;
8195
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008196 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008197 md_probe, NULL, NULL);
8198 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 md_probe, NULL, NULL);
8200
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008202 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203
8204 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206
Tejun Heoe804ac72010-10-15 15:36:08 +02008207err_mdp:
8208 unregister_blkdev(MD_MAJOR, "md");
8209err_md:
8210 destroy_workqueue(md_misc_wq);
8211err_misc_wq:
8212 destroy_workqueue(md_wq);
8213err_wq:
8214 return ret;
8215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216
8217#ifndef MODULE
8218
8219/*
8220 * Searches all registered partitions for autorun RAID arrays
8221 * at boot time.
8222 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008223
8224static LIST_HEAD(all_detected_devices);
8225struct detected_devices_node {
8226 struct list_head list;
8227 dev_t dev;
8228};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
8230void md_autodetect_dev(dev_t dev)
8231{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008232 struct detected_devices_node *node_detected_dev;
8233
8234 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8235 if (node_detected_dev) {
8236 node_detected_dev->dev = dev;
8237 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8238 } else {
8239 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8240 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242}
8243
8244
8245static void autostart_arrays(int part)
8246{
NeilBrown3cb03002011-10-11 16:45:26 +11008247 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008248 struct detected_devices_node *node_detected_dev;
8249 dev_t dev;
8250 int i_scanned, i_passed;
8251
8252 i_scanned = 0;
8253 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254
8255 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8256
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008257 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8258 i_scanned++;
8259 node_detected_dev = list_entry(all_detected_devices.next,
8260 struct detected_devices_node, list);
8261 list_del(&node_detected_dev->list);
8262 dev = node_detected_dev->dev;
8263 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008264 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 if (IS_ERR(rdev))
8266 continue;
8267
NeilBrownb2d444d2005-11-08 21:39:31 -08008268 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 MD_BUG();
8270 continue;
8271 }
NeilBrownd0fae182008-03-04 14:29:31 -08008272 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008274 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008276
8277 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8278 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279
8280 autorun_devices(part);
8281}
8282
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008283#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284
8285static __exit void md_exit(void)
8286{
NeilBrownfd01b882011-10-11 16:47:53 +11008287 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008289
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008290 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008291 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008293 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 unregister_blkdev(mdp_major, "mdp");
8295 unregister_reboot_notifier(&md_notifier);
8296 unregister_sysctl_table(raid_table_header);
8297 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008298 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008300 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008302 destroy_workqueue(md_misc_wq);
8303 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008304}
8305
Dan Williams685784a2007-07-09 11:56:42 -07008306subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307module_exit(md_exit)
8308
NeilBrownf91de922005-11-08 21:39:36 -08008309static int get_ro(char *buffer, struct kernel_param *kp)
8310{
8311 return sprintf(buffer, "%d", start_readonly);
8312}
8313static int set_ro(const char *val, struct kernel_param *kp)
8314{
8315 char *e;
8316 int num = simple_strtoul(val, &e, 10);
8317 if (*val && (*e == '\0' || *e == '\n')) {
8318 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008319 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008320 }
8321 return -EINVAL;
8322}
8323
NeilBrown80ca3a42006-07-10 04:44:18 -07008324module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8325module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08008326
NeilBrownefeb53c2009-01-09 08:31:10 +11008327module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008328
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329EXPORT_SYMBOL(register_md_personality);
8330EXPORT_SYMBOL(unregister_md_personality);
8331EXPORT_SYMBOL(md_error);
8332EXPORT_SYMBOL(md_done_sync);
8333EXPORT_SYMBOL(md_write_start);
8334EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335EXPORT_SYMBOL(md_register_thread);
8336EXPORT_SYMBOL(md_unregister_thread);
8337EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338EXPORT_SYMBOL(md_check_recovery);
8339MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008340MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008341MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008342MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);