blob: 0bc10cc4b961682403fe4d83784541f3838ce9fa [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#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
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown2604b702006-01-06 00:20:36 -080064static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(pers_lock);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067static void md_print_devices(void);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
Adrian Bunk5e563412006-06-26 00:27:42 -070073#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Robert Becker1e509152009-12-14 12:49:58 +110076 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
79 */
80#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700115 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800116 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 },
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800125 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static ctl_table raid_dir_table[] = {
129 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .procname = "raid",
131 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700132 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .child = raid_table,
134 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800135 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static ctl_table raid_root_table[] = {
139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
144 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800145 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700148static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
NeilBrownf91de922005-11-08 21:39:36 -0800150static int start_readonly;
151
NeilBrowna167f662010-10-26 18:31:13 +1100152/* bio_clone_mddev
153 * like bio_clone, but with a local bio set
154 */
155
156static void mddev_bio_destructor(struct bio *bio)
157{
158 mddev_t *mddev, **mddevp;
159
160 mddevp = (void*)bio;
161 mddev = mddevp[-1];
162
163 bio_free(bio, mddev->bio_set);
164}
165
166struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
167 mddev_t *mddev)
168{
169 struct bio *b;
170 mddev_t **mddevp;
171
172 if (!mddev || !mddev->bio_set)
173 return bio_alloc(gfp_mask, nr_iovecs);
174
175 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
176 mddev->bio_set);
177 if (!b)
178 return NULL;
179 mddevp = (void*)b;
180 mddevp[-1] = mddev;
181 b->bi_destructor = mddev_bio_destructor;
182 return b;
183}
184EXPORT_SYMBOL_GPL(bio_alloc_mddev);
185
186struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
187 mddev_t *mddev)
188{
189 struct bio *b;
190 mddev_t **mddevp;
191
192 if (!mddev || !mddev->bio_set)
193 return bio_clone(bio, gfp_mask);
194
195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
196 mddev->bio_set);
197 if (!b)
198 return NULL;
199 mddevp = (void*)b;
200 mddevp[-1] = mddev;
201 b->bi_destructor = mddev_bio_destructor;
202 __bio_clone(b, bio);
203 if (bio_integrity(bio)) {
204 int ret;
205
206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
207
208 if (ret < 0) {
209 bio_put(b);
210 return NULL;
211 }
212 }
213
214 return b;
215}
216EXPORT_SYMBOL_GPL(bio_clone_mddev);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
NeilBrownd7603b72006-01-06 00:20:30 -0800219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
222 * count increases.
223 *
224 * Events are:
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
227 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800228static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800229static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800230void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800231{
232 atomic_inc(&md_event_count);
233 wake_up(&md_event_waiters);
234}
NeilBrown29269552006-03-27 01:18:10 -0800235EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800236
NeilBrownc331eb02006-05-30 21:27:13 -0700237/* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
239 */
Adrian Bunk05381952006-06-26 00:28:01 -0700240static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700241{
242 atomic_inc(&md_event_count);
243 wake_up(&md_event_waiters);
244}
245
NeilBrownd7603b72006-01-06 00:20:30 -0800246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
249 */
250static LIST_HEAD(all_mddevs);
251static DEFINE_SPINLOCK(all_mddevs_lock);
252
253
254/*
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
260 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800261#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
265 mddev = NULL;}); \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
273 tmp = tmp->next;}) \
274 )
275
276
NeilBrown409c57f2009-03-31 14:39:39 +1100277/* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
283 */
284static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
NeilBrown49077322010-03-25 16:20:56 +1100286 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100287 mddev_t *mddev = q->queuedata;
288 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100289 int cpu;
290
NeilBrown0ca69882011-01-14 09:14:33 +1100291 if (mddev == NULL || mddev->pers == NULL
292 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100293 bio_io_error(bio);
294 return 0;
295 }
NeilBrown0ca69882011-01-14 09:14:33 +1100296 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100297 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200298 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100299 DEFINE_WAIT(__wait);
300 for (;;) {
301 prepare_to_wait(&mddev->sb_wait, &__wait,
302 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200303 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100304 break;
305 rcu_read_unlock();
306 schedule();
307 rcu_read_lock();
308 }
309 finish_wait(&mddev->sb_wait, &__wait);
310 }
311 atomic_inc(&mddev->active_io);
312 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100313
NeilBrown21a52c62010-04-01 15:02:13 +1100314 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100315
316 cpu = part_stat_lock();
317 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
318 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
319 bio_sectors(bio));
320 part_stat_unlock();
321
NeilBrown409c57f2009-03-31 14:39:39 +1100322 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
323 wake_up(&mddev->sb_wait);
324
325 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
NeilBrown9e35b992010-04-06 14:23:02 +1000328/* mddev_suspend makes sure no new requests are submitted
329 * to the device, and that any requests that have been submitted
330 * are completely handled.
331 * Once ->stop is called and completes, the module will be completely
332 * unused.
333 */
NeilBrown390ee602010-06-01 19:37:27 +1000334void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100335{
336 BUG_ON(mddev->suspended);
337 mddev->suspended = 1;
338 synchronize_rcu();
339 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
340 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100341}
NeilBrown390ee602010-06-01 19:37:27 +1000342EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100343
NeilBrown390ee602010-06-01 19:37:27 +1000344void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100345{
346 mddev->suspended = 0;
347 wake_up(&mddev->sb_wait);
348 mddev->pers->quiesce(mddev, 0);
349}
NeilBrown390ee602010-06-01 19:37:27 +1000350EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100351
NeilBrown3fa841d2009-09-23 18:10:29 +1000352int mddev_congested(mddev_t *mddev, int bits)
353{
354 return mddev->suspended;
355}
356EXPORT_SYMBOL(mddev_congested);
357
NeilBrowna2826aa2009-12-14 12:49:49 +1100358/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200359 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100360 */
361
Tejun Heoe9c74692010-09-03 11:56:18 +0200362static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100363{
364 mdk_rdev_t *rdev = bio->bi_private;
365 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100366
367 rdev_dec_pending(rdev, mddev);
368
369 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200370 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200371 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100372 }
373 bio_put(bio);
374}
375
NeilBrowna7a07e62010-12-09 16:04:25 +1100376static void md_submit_flush_data(struct work_struct *ws);
377
NeilBrowna035fc32010-12-09 16:17:51 +1100378static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100379{
NeilBrowna035fc32010-12-09 16:17:51 +1100380 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100381 mdk_rdev_t *rdev;
382
NeilBrowna7a07e62010-12-09 16:04:25 +1100383 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
384 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100385 rcu_read_lock();
386 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
387 if (rdev->raid_disk >= 0 &&
388 !test_bit(Faulty, &rdev->flags)) {
389 /* Take two references, one is dropped
390 * when request finishes, one after
391 * we reclaim rcu_read_lock
392 */
393 struct bio *bi;
394 atomic_inc(&rdev->nr_pending);
395 atomic_inc(&rdev->nr_pending);
396 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100397 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200398 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100399 bi->bi_private = rdev;
400 bi->bi_bdev = rdev->bdev;
401 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200402 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100403 rcu_read_lock();
404 rdev_dec_pending(rdev, mddev);
405 }
406 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100407 if (atomic_dec_and_test(&mddev->flush_pending))
408 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100409}
410
Tejun Heoe9c74692010-09-03 11:56:18 +0200411static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100412{
Tejun Heoe9c74692010-09-03 11:56:18 +0200413 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
414 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100415
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 /* an empty barrier - all done */
418 bio_endio(bio, 0);
419 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200420 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100421 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100422 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100423 }
NeilBrown2b74e122010-12-09 15:59:01 +1100424
425 mddev->flush_bio = NULL;
426 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100427}
428
Tejun Heoe9c74692010-09-03 11:56:18 +0200429void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100430{
431 spin_lock_irq(&mddev->write_lock);
432 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100434 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100436 spin_unlock_irq(&mddev->write_lock);
437
NeilBrowna035fc32010-12-09 16:17:51 +1100438 INIT_WORK(&mddev->flush_work, submit_flushes);
439 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100440}
Tejun Heoe9c74692010-09-03 11:56:18 +0200441EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100442
NeilBrown2ac87402010-06-01 19:37:29 +1000443/* Support for plugging.
444 * This mirrors the plugging support in request_queue, but does not
445 * require having a whole queue
446 */
447static void plugger_work(struct work_struct *work)
448{
449 struct plug_handle *plug =
450 container_of(work, struct plug_handle, unplug_work);
451 plug->unplug_fn(plug);
452}
453static void plugger_timeout(unsigned long data)
454{
455 struct plug_handle *plug = (void *)data;
456 kblockd_schedule_work(NULL, &plug->unplug_work);
457}
458void plugger_init(struct plug_handle *plug,
459 void (*unplug_fn)(struct plug_handle *))
460{
461 plug->unplug_flag = 0;
462 plug->unplug_fn = unplug_fn;
463 init_timer(&plug->unplug_timer);
464 plug->unplug_timer.function = plugger_timeout;
465 plug->unplug_timer.data = (unsigned long)plug;
466 INIT_WORK(&plug->unplug_work, plugger_work);
467}
468EXPORT_SYMBOL_GPL(plugger_init);
469
470void plugger_set_plug(struct plug_handle *plug)
471{
472 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
473 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
474}
475EXPORT_SYMBOL_GPL(plugger_set_plug);
476
477int plugger_remove_plug(struct plug_handle *plug)
478{
479 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
480 del_timer(&plug->unplug_timer);
481 return 1;
482 } else
483 return 0;
484}
485EXPORT_SYMBOL_GPL(plugger_remove_plug);
486
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static inline mddev_t *mddev_get(mddev_t *mddev)
489{
490 atomic_inc(&mddev->active);
491 return mddev;
492}
493
Dan Williams5fd3a172009-03-04 00:57:25 -0700494static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static void mddev_put(mddev_t *mddev)
497{
NeilBrowna167f662010-10-26 18:31:13 +1100498 struct bio_set *bs = NULL;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
501 return;
NeilBrownd3374822009-01-09 08:31:10 +1100502 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100503 mddev->ctime == 0 && !mddev->hold_active) {
504 /* Array is not configured at all, and not held active,
505 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100507 bs = mddev->bio_set;
508 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100509 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200510 /* We did a probe so need to clean up. Call
511 * queue_work inside the spinlock so that
512 * flush_workqueue() after mddev_find will
513 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100514 */
515 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200516 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100517 } else
518 kfree(mddev);
519 }
520 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100521 if (bs)
522 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
NeilBrown390ee602010-06-01 19:37:27 +1000525void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100526{
527 mutex_init(&mddev->open_mutex);
528 mutex_init(&mddev->reconfig_mutex);
529 mutex_init(&mddev->bitmap_info.mutex);
530 INIT_LIST_HEAD(&mddev->disks);
531 INIT_LIST_HEAD(&mddev->all_mddevs);
532 init_timer(&mddev->safemode_timer);
533 atomic_set(&mddev->active, 1);
534 atomic_set(&mddev->openers, 0);
535 atomic_set(&mddev->active_io, 0);
536 spin_lock_init(&mddev->write_lock);
537 atomic_set(&mddev->flush_pending, 0);
538 init_waitqueue_head(&mddev->sb_wait);
539 init_waitqueue_head(&mddev->recovery_wait);
540 mddev->reshape_position = MaxSector;
541 mddev->resync_min = 0;
542 mddev->resync_max = MaxSector;
543 mddev->level = LEVEL_NONE;
544}
NeilBrown390ee602010-06-01 19:37:27 +1000545EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547static mddev_t * mddev_find(dev_t unit)
548{
549 mddev_t *mddev, *new = NULL;
550
551 retry:
552 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
NeilBrownefeb53c2009-01-09 08:31:10 +1100554 if (unit) {
555 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
556 if (mddev->unit == unit) {
557 mddev_get(mddev);
558 spin_unlock(&all_mddevs_lock);
559 kfree(new);
560 return mddev;
561 }
562
563 if (new) {
564 list_add(&new->all_mddevs, &all_mddevs);
565 spin_unlock(&all_mddevs_lock);
566 new->hold_active = UNTIL_IOCTL;
567 return new;
568 }
569 } else if (new) {
570 /* find an unused unit number */
571 static int next_minor = 512;
572 int start = next_minor;
573 int is_free = 0;
574 int dev = 0;
575 while (!is_free) {
576 dev = MKDEV(MD_MAJOR, next_minor);
577 next_minor++;
578 if (next_minor > MINORMASK)
579 next_minor = 0;
580 if (next_minor == start) {
581 /* Oh dear, all in use. */
582 spin_unlock(&all_mddevs_lock);
583 kfree(new);
584 return NULL;
585 }
586
587 is_free = 1;
588 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
589 if (mddev->unit == dev) {
590 is_free = 0;
591 break;
592 }
593 }
594 new->unit = dev;
595 new->md_minor = MINOR(dev);
596 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 list_add(&new->all_mddevs, &all_mddevs);
598 spin_unlock(&all_mddevs_lock);
599 return new;
600 }
601 spin_unlock(&all_mddevs_lock);
602
NeilBrown9ffae0c2006-01-06 00:20:32 -0800603 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!new)
605 return NULL;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 new->unit = unit;
608 if (MAJOR(unit) == MD_MAJOR)
609 new->md_minor = MINOR(unit);
610 else
611 new->md_minor = MINOR(unit) >> MdpMinorShift;
612
NeilBrownfafd7fb2010-04-01 15:55:30 +1100613 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto retry;
616}
617
618static inline int mddev_lock(mddev_t * mddev)
619{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800620 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Dan Williamsb522adc2009-03-31 15:00:31 +1100623static inline int mddev_is_locked(mddev_t *mddev)
624{
625 return mutex_is_locked(&mddev->reconfig_mutex);
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static inline int mddev_trylock(mddev_t * mddev)
629{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800630 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
NeilBrownb6eb1272010-04-15 10:13:47 +1000633static struct attribute_group md_redundancy_group;
634
NeilBrowna64c8762010-04-14 17:15:37 +1000635static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
NeilBrowna64c8762010-04-14 17:15:37 +1000637 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000638 /* These cannot be removed under reconfig_mutex as
639 * an access to the files will try to take reconfig_mutex
640 * while holding the file unremovable, which leads to
641 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000642 * So hold set sysfs_active while the remove in happeing,
643 * and anything else which might set ->to_remove or my
644 * otherwise change the sysfs namespace will fail with
645 * -EBUSY if sysfs_active is still set.
646 * We set sysfs_active under reconfig_mutex and elsewhere
647 * test it under the same mutex to ensure its correct value
648 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000649 */
NeilBrowna64c8762010-04-14 17:15:37 +1000650 struct attribute_group *to_remove = mddev->to_remove;
651 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000652 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000653 mutex_unlock(&mddev->reconfig_mutex);
654
NeilBrown00bcb4a2010-06-01 19:37:23 +1000655 if (mddev->kobj.sd) {
656 if (to_remove != &md_redundancy_group)
657 sysfs_remove_group(&mddev->kobj, to_remove);
658 if (mddev->pers == NULL ||
659 mddev->pers->sync_request == NULL) {
660 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
661 if (mddev->sysfs_action)
662 sysfs_put(mddev->sysfs_action);
663 mddev->sysfs_action = NULL;
664 }
NeilBrowna64c8762010-04-14 17:15:37 +1000665 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000666 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000667 } else
668 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
NeilBrown005eca52005-08-22 13:11:08 -0700670 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
NeilBrown2989ddb2006-01-06 00:20:43 -0800673static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100675 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100677 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (rdev->desc_nr == nr)
679 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return NULL;
682}
683
684static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 mdk_rdev_t *rdev;
687
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100688 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (rdev->bdev->bd_dev == dev)
690 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return NULL;
693}
694
NeilBrownd9d166c2006-01-06 00:20:51 -0800695static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800696{
697 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800698 list_for_each_entry(pers, &pers_list, list) {
699 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800700 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800701 if (strcmp(pers->name, clevel)==0)
702 return pers;
703 }
NeilBrown2604b702006-01-06 00:20:36 -0800704 return NULL;
705}
706
Andre Nollb73df2d2008-07-11 22:02:23 +1000707/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100708static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100710 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000711 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static int alloc_disk_sb(mdk_rdev_t * rdev)
715{
716 if (rdev->sb_page)
717 MD_BUG();
718
719 rdev->sb_page = alloc_page(GFP_KERNEL);
720 if (!rdev->sb_page) {
721 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000722 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 return 0;
726}
727
728static void free_disk_sb(mdk_rdev_t * rdev)
729{
730 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800731 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 rdev->sb_loaded = 0;
733 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000734 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100735 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737}
738
739
NeilBrown6712ecf2007-09-27 12:47:43 +0200740static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700741{
742 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800743 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700744
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700745 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
746 printk("md: super_written gets error=%d, uptodate=%d\n",
747 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
748 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800749 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700750 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700751
NeilBrowna9701a32005-11-08 21:39:34 -0800752 if (atomic_dec_and_test(&mddev->pending_writes))
753 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700754 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700755}
756
757void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
758 sector_t sector, int size, struct page *page)
759{
760 /* write first size bytes of page to sector of rdev
761 * Increment mddev->pending_writes before returning
762 * and decrement it on completion, waking up sb_wait
763 * if zero is reached.
764 * If an error occurred, call md_error
765 */
NeilBrowna167f662010-10-26 18:31:13 +1100766 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767
768 bio->bi_bdev = rdev->bdev;
769 bio->bi_sector = sector;
770 bio_add_page(bio, page, size, 0);
771 bio->bi_private = rdev;
772 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800773
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200775 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
776 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800777}
778
779void md_super_wait(mddev_t *mddev)
780{
Tejun Heoe9c74692010-09-03 11:56:18 +0200781 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800782 DEFINE_WAIT(wq);
783 for(;;) {
784 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
785 if (atomic_read(&mddev->pending_writes)==0)
786 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800787 schedule();
788 }
789 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790}
791
NeilBrown6712ecf2007-09-27 12:47:43 +0200792static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
NeilBrown2b193362010-10-27 15:16:40 +1100797int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100798 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
NeilBrowna167f662010-10-26 18:31:13 +1100800 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct completion event;
802 int ret;
803
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200804 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
NeilBrown2b193362010-10-27 15:16:40 +1100806 bio->bi_bdev = rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100807 if (metadata_op)
808 bio->bi_sector = sector + rdev->sb_start;
809 else
810 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 bio_add_page(bio, page, size, 0);
812 init_completion(&event);
813 bio->bi_private = &event;
814 bio->bi_end_io = bi_complete;
815 submit_bio(rw, bio);
816 wait_for_completion(&event);
817
818 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
819 bio_put(bio);
820 return ret;
821}
NeilBrowna8745db2006-01-06 00:20:34 -0800822EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
NeilBrown0002b272005-09-09 16:23:53 -0700824static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 char b[BDEVNAME_SIZE];
827 if (!rdev->sb_page) {
828 MD_BUG();
829 return -EINVAL;
830 }
831 if (rdev->sb_loaded)
832 return 0;
833
834
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100835 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto fail;
837 rdev->sb_loaded = 1;
838 return 0;
839
840fail:
841 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
842 bdevname(rdev->bdev,b));
843 return -EINVAL;
844}
845
846static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
847{
Andre Noll05710462008-07-11 22:02:20 +1000848 return sb1->set_uuid0 == sb2->set_uuid0 &&
849 sb1->set_uuid1 == sb2->set_uuid1 &&
850 sb1->set_uuid2 == sb2->set_uuid2 &&
851 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
855{
856 int ret;
857 mdp_super_t *tmp1, *tmp2;
858
859 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
860 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
861
862 if (!tmp1 || !tmp2) {
863 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100864 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto abort;
866 }
867
868 *tmp1 = *sb1;
869 *tmp2 = *sb2;
870
871 /*
872 * nr_disks is not constant
873 */
874 tmp1->nr_disks = 0;
875 tmp2->nr_disks = 0;
876
Andre Nollce0c8e02008-07-11 22:02:20 +1000877 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700879 kfree(tmp1);
880 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ret;
882}
883
NeilBrown4d167f02007-05-09 02:35:37 -0700884
885static u32 md_csum_fold(u32 csum)
886{
887 csum = (csum & 0xffff) + (csum >> 16);
888 return (csum & 0xffff) + (csum >> 16);
889}
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891static unsigned int calc_sb_csum(mdp_super_t * sb)
892{
NeilBrown4d167f02007-05-09 02:35:37 -0700893 u64 newcsum = 0;
894 u32 *sb32 = (u32*)sb;
895 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 unsigned int disk_csum, csum;
897
898 disk_csum = sb->sb_csum;
899 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700900
901 for (i = 0; i < MD_SB_BYTES/4 ; i++)
902 newcsum += sb32[i];
903 csum = (newcsum & 0xffffffff) + (newcsum>>32);
904
905
906#ifdef CONFIG_ALPHA
907 /* This used to use csum_partial, which was wrong for several
908 * reasons including that different results are returned on
909 * different architectures. It isn't critical that we get exactly
910 * the same return value as before (we always csum_fold before
911 * testing, and that removes any differences). However as we
912 * know that csum_partial always returned a 16bit value on
913 * alphas, do a fold to maximise conformity to previous behaviour.
914 */
915 sb->sb_csum = md_csum_fold(disk_csum);
916#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return csum;
920}
921
922
923/*
924 * Handle superblock details.
925 * We want to be able to handle multiple superblock formats
926 * so we have a common interface to them all, and an array of
927 * different handlers.
928 * We rely on user-space to write the initial superblock, and support
929 * reading and updating of superblocks.
930 * Interface methods are:
931 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
932 * loads and validates a superblock on dev.
933 * if refdev != NULL, compare superblocks on both devices
934 * Return:
935 * 0 - dev has a superblock that is compatible with refdev
936 * 1 - dev has a superblock that is compatible and newer than refdev
937 * so dev should be used as the refdev in future
938 * -EINVAL superblock incompatible or invalid
939 * -othererror e.g. -EIO
940 *
941 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
942 * Verify that dev is acceptable into mddev.
943 * The first time, mddev->raid_disks will be 0, and data from
944 * dev should be merged in. Subsequent calls check that dev
945 * is new enough. Return 0 or -EINVAL
946 *
947 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
948 * Update the superblock for rdev with data in mddev
949 * This does not write to disc.
950 *
951 */
952
953struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000954 char *name;
955 struct module *owner;
956 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
957 int minor_version);
958 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
959 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
960 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000961 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962};
963
964/*
Andre Noll0894cc32009-06-18 08:49:23 +1000965 * Check that the given mddev has no bitmap.
966 *
967 * This function is called from the run method of all personalities that do not
968 * support bitmaps. It prints an error message and returns non-zero if mddev
969 * has a bitmap. Otherwise, it returns 0.
970 *
971 */
972int md_check_no_bitmap(mddev_t *mddev)
973{
NeilBrownc3d97142009-12-14 12:49:52 +1100974 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000975 return 0;
976 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
977 mdname(mddev), mddev->pers->name);
978 return 1;
979}
980EXPORT_SYMBOL(md_check_no_bitmap);
981
982/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * load_super for 0.90.0
984 */
985static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
986{
987 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
988 mdp_super_t *sb;
989 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
Andre Noll0f420352008-07-11 22:02:23 +1000992 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * it's at the end of the disk.
994 *
995 * It also happens to be a multiple of 4Kb.
996 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100997 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
NeilBrown0002b272005-09-09 16:23:53 -0700999 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (ret) return ret;
1001
1002 ret = -EINVAL;
1003
1004 bdevname(rdev->bdev, b);
1005 sb = (mdp_super_t*)page_address(rdev->sb_page);
1006
1007 if (sb->md_magic != MD_SB_MAGIC) {
1008 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1009 b);
1010 goto abort;
1011 }
1012
1013 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001014 sb->minor_version < 90 ||
1015 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1017 sb->major_version, sb->minor_version,
1018 b);
1019 goto abort;
1020 }
1021
1022 if (sb->raid_disks <= 0)
1023 goto abort;
1024
NeilBrown4d167f02007-05-09 02:35:37 -07001025 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1027 b);
1028 goto abort;
1029 }
1030
1031 rdev->preferred_minor = sb->md_minor;
1032 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001033 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (sb->level == LEVEL_MULTIPATH)
1036 rdev->desc_nr = -1;
1037 else
1038 rdev->desc_nr = sb->this_disk.number;
1039
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001040 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001042 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 __u64 ev1, ev2;
1044 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1045 if (!uuid_equal(refsb, sb)) {
1046 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1047 b, bdevname(refdev->bdev,b2));
1048 goto abort;
1049 }
1050 if (!sb_equal(refsb, sb)) {
1051 printk(KERN_WARNING "md: %s has same UUID"
1052 " but different superblock to %s\n",
1053 b, bdevname(refdev->bdev, b2));
1054 goto abort;
1055 }
1056 ev1 = md_event(sb);
1057 ev2 = md_event(refsb);
1058 if (ev1 > ev2)
1059 ret = 1;
1060 else
1061 ret = 0;
1062 }
NeilBrown8190e752009-06-18 08:48:58 +10001063 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Andre Nolldd8ac332009-03-31 14:33:13 +11001065 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001066 /* "this cannot possibly happen" ... */
1067 ret = -EINVAL;
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 abort:
1070 return ret;
1071}
1072
1073/*
1074 * validate_super for 0.90.0
1075 */
1076static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1077{
1078 mdp_disk_t *desc;
1079 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001080 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
NeilBrown41158c72005-06-21 17:17:25 -07001082 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001083 clear_bit(Faulty, &rdev->flags);
1084 clear_bit(In_sync, &rdev->flags);
1085 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (mddev->raid_disks == 0) {
1088 mddev->major_version = 0;
1089 mddev->minor_version = sb->minor_version;
1090 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001091 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001092 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 mddev->ctime = sb->ctime;
1094 mddev->utime = sb->utime;
1095 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001096 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 mddev->layout = sb->layout;
1098 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001099 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001100 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001101 mddev->bitmap_info.offset = 0;
1102 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
NeilBrownf6705572006-03-27 01:18:11 -08001104 if (mddev->minor_version >= 91) {
1105 mddev->reshape_position = sb->reshape_position;
1106 mddev->delta_disks = sb->delta_disks;
1107 mddev->new_level = sb->new_level;
1108 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001109 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001110 } else {
1111 mddev->reshape_position = MaxSector;
1112 mddev->delta_disks = 0;
1113 mddev->new_level = mddev->level;
1114 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001115 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (sb->state & (1<<MD_SB_CLEAN))
1119 mddev->recovery_cp = MaxSector;
1120 else {
1121 if (sb->events_hi == sb->cp_events_hi &&
1122 sb->events_lo == sb->cp_events_lo) {
1123 mddev->recovery_cp = sb->recovery_cp;
1124 } else
1125 mddev->recovery_cp = 0;
1126 }
1127
1128 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1129 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1130 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1131 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1132
1133 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001134
1135 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001136 mddev->bitmap_info.file == NULL)
1137 mddev->bitmap_info.offset =
1138 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001139
NeilBrown41158c72005-06-21 17:17:25 -07001140 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001141 /* Insist on good event counter while assembling, except
1142 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001144 if (sb->disks[rdev->desc_nr].state & (
1145 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1146 if (ev1 < mddev->events)
1147 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001148 } else if (mddev->bitmap) {
1149 /* if adding to array with a bitmap, then we can accept an
1150 * older device ... but not too old.
1151 */
NeilBrown41158c72005-06-21 17:17:25 -07001152 if (ev1 < mddev->bitmap->events_cleared)
1153 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001154 } else {
1155 if (ev1 < mddev->events)
1156 /* just a hot-add of a new device, leave raid_disk at -1 */
1157 return 0;
1158 }
NeilBrown41158c72005-06-21 17:17:25 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 desc = sb->disks + rdev->desc_nr;
1162
1163 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001164 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001165 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1166 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001167 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001169 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1170 /* active but not in sync implies recovery up to
1171 * reshape position. We don't know exactly where
1172 * that is, so set to zero for now */
1173 if (mddev->minor_version >= 91) {
1174 rdev->recovery_offset = 0;
1175 rdev->raid_disk = desc->raid_disk;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001178 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1179 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001180 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001181 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183}
1184
1185/*
1186 * sync_super for 0.90.0
1187 */
1188static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1189{
1190 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 mdk_rdev_t *rdev2;
1192 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* make rdev->sb match mddev data..
1196 *
1197 * 1/ zero out disks
1198 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1199 * 3/ any empty disks < next_spare become removed
1200 *
1201 * disks[0] gets initialised to REMOVED because
1202 * we cannot be sure from other fields if it has
1203 * been initialised or not.
1204 */
1205 int i;
1206 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1207
NeilBrown61181562005-09-09 16:24:02 -07001208 rdev->sb_size = MD_SB_BYTES;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 sb = (mdp_super_t*)page_address(rdev->sb_page);
1211
1212 memset(sb, 0, sizeof(*sb));
1213
1214 sb->md_magic = MD_SB_MAGIC;
1215 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sb->patch_version = mddev->patch_version;
1217 sb->gvalid_words = 0; /* ignored */
1218 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1219 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1220 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1221 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1222
1223 sb->ctime = mddev->ctime;
1224 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001225 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 sb->raid_disks = mddev->raid_disks;
1227 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001228 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 sb->utime = mddev->utime;
1230 sb->state = 0;
1231 sb->events_hi = (mddev->events>>32);
1232 sb->events_lo = (u32)mddev->events;
1233
NeilBrownf6705572006-03-27 01:18:11 -08001234 if (mddev->reshape_position == MaxSector)
1235 sb->minor_version = 90;
1236 else {
1237 sb->minor_version = 91;
1238 sb->reshape_position = mddev->reshape_position;
1239 sb->new_level = mddev->new_level;
1240 sb->delta_disks = mddev->delta_disks;
1241 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001242 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001243 }
1244 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (mddev->in_sync)
1246 {
1247 sb->recovery_cp = mddev->recovery_cp;
1248 sb->cp_events_hi = (mddev->events>>32);
1249 sb->cp_events_lo = (u32)mddev->events;
1250 if (mddev->recovery_cp == MaxSector)
1251 sb->state = (1<< MD_SB_CLEAN);
1252 } else
1253 sb->recovery_cp = 0;
1254
1255 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001256 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
NeilBrownc3d97142009-12-14 12:49:52 +11001258 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001259 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001262 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001264 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001265 int is_active = test_bit(In_sync, &rdev2->flags);
1266
1267 if (rdev2->raid_disk >= 0 &&
1268 sb->minor_version >= 91)
1269 /* we have nowhere to store the recovery_offset,
1270 * but if it is not below the reshape_position,
1271 * we can piggy-back on that.
1272 */
1273 is_active = 1;
1274 if (rdev2->raid_disk < 0 ||
1275 test_bit(Faulty, &rdev2->flags))
1276 is_active = 0;
1277 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001278 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001280 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001281 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d = &sb->disks[rdev2->desc_nr];
1283 nr_disks++;
1284 d->number = rdev2->desc_nr;
1285 d->major = MAJOR(rdev2->bdev->bd_dev);
1286 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001287 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d->raid_disk = rdev2->raid_disk;
1289 else
1290 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001291 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001293 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001295 if (test_bit(In_sync, &rdev2->flags))
1296 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 active++;
1298 working++;
1299 } else {
1300 d->state = 0;
1301 spare++;
1302 working++;
1303 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001304 if (test_bit(WriteMostly, &rdev2->flags))
1305 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* now set the "removed" and "faulty" bits on any missing devices */
1308 for (i=0 ; i < mddev->raid_disks ; i++) {
1309 mdp_disk_t *d = &sb->disks[i];
1310 if (d->state == 0 && d->number == 0) {
1311 d->number = i;
1312 d->raid_disk = i;
1313 d->state = (1<<MD_DISK_REMOVED);
1314 d->state |= (1<<MD_DISK_FAULTY);
1315 failed++;
1316 }
1317 }
1318 sb->nr_disks = nr_disks;
1319 sb->active_disks = active;
1320 sb->working_disks = working;
1321 sb->failed_disks = failed;
1322 sb->spare_disks = spare;
1323
1324 sb->this_disk = sb->disks[rdev->desc_nr];
1325 sb->sb_csum = calc_sb_csum(sb);
1326}
1327
1328/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329 * rdev_size_change for 0.90.0
1330 */
1331static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001332super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333{
Andre Noll58c0fed2009-03-31 14:33:13 +11001334 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001335 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001336 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001338 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001339 if (!num_sectors || num_sectors > rdev->sb_start)
1340 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001341 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 rdev->sb_page);
1343 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001344 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001345}
1346
1347
1348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * version 1 superblock
1350 */
1351
NeilBrown1c05b4b2006-10-21 10:24:08 -07001352static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001354 __le32 disk_csum;
1355 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 unsigned long long newcsum;
1357 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001358 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int i;
1360
1361 disk_csum = sb->sb_csum;
1362 sb->sb_csum = 0;
1363 newcsum = 0;
1364 for (i=0; size>=4; size -= 4 )
1365 newcsum += le32_to_cpu(*isuper++);
1366
1367 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001368 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1371 sb->sb_csum = disk_csum;
1372 return cpu_to_le32(csum);
1373}
1374
1375static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1376{
1377 struct mdp_superblock_1 *sb;
1378 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001379 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001381 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
Andre Noll0f420352008-07-11 22:02:23 +10001384 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * It is always aligned to a 4K boundary and
1386 * depeding on minor_version, it can be:
1387 * 0: At least 8K, but less than 12K, from end of device
1388 * 1: At start of device
1389 * 2: 4K from start of device.
1390 */
1391 switch(minor_version) {
1392 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001393 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001394 sb_start -= 8*2;
1395 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001398 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
1400 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001401 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 default:
1404 return -EINVAL;
1405 }
Andre Noll0f420352008-07-11 22:02:23 +10001406 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
NeilBrown0002b272005-09-09 16:23:53 -07001408 /* superblock is rarely larger than 1K, but it can be larger,
1409 * and it is safe to read 4k, so we do that
1410 */
1411 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (ret) return ret;
1413
1414
1415 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1416
1417 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1418 sb->major_version != cpu_to_le32(1) ||
1419 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001420 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001421 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -EINVAL;
1423
1424 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1425 printk("md: invalid superblock checksum on %s\n",
1426 bdevname(rdev->bdev,b));
1427 return -EINVAL;
1428 }
1429 if (le64_to_cpu(sb->data_size) < 10) {
1430 printk("md: data_size too small on %s\n",
1431 bdevname(rdev->bdev,b));
1432 return -EINVAL;
1433 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 rdev->preferred_minor = 0xffff;
1436 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001437 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
NeilBrown0002b272005-09-09 16:23:53 -07001439 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001440 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001441 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001442 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1443
1444 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001445 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001446 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001447
NeilBrown31b65a02006-07-10 04:44:14 -07001448 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1449 rdev->desc_nr = -1;
1450 else
1451 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1452
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001453 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001454 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001455 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 __u64 ev1, ev2;
1457 struct mdp_superblock_1 *refsb =
1458 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1459
1460 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1461 sb->level != refsb->level ||
1462 sb->layout != refsb->layout ||
1463 sb->chunksize != refsb->chunksize) {
1464 printk(KERN_WARNING "md: %s has strangely different"
1465 " superblock to %s\n",
1466 bdevname(rdev->bdev,b),
1467 bdevname(refdev->bdev,b2));
1468 return -EINVAL;
1469 }
1470 ev1 = le64_to_cpu(sb->events);
1471 ev2 = le64_to_cpu(refsb->events);
1472
1473 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001474 ret = 1;
1475 else
1476 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
NeilBrowna1801f82008-03-04 14:29:31 -08001478 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001479 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001480 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001482 rdev->sectors = rdev->sb_start;
1483 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001485 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001486 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001487 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001488 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1492{
1493 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001494 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
NeilBrown41158c72005-06-21 17:17:25 -07001496 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001497 clear_bit(Faulty, &rdev->flags);
1498 clear_bit(In_sync, &rdev->flags);
1499 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (mddev->raid_disks == 0) {
1502 mddev->major_version = 1;
1503 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001504 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001505 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1507 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1508 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001509 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 mddev->layout = le32_to_cpu(sb->layout);
1511 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001512 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001513 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001514 mddev->bitmap_info.offset = 0;
1515 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1518 memcpy(mddev->uuid, sb->set_uuid, 16);
1519
1520 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001521
NeilBrown71c08052005-09-09 16:23:51 -07001522 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001523 mddev->bitmap_info.file == NULL )
1524 mddev->bitmap_info.offset =
1525 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001526
NeilBrownf6705572006-03-27 01:18:11 -08001527 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1528 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1529 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1530 mddev->new_level = le32_to_cpu(sb->new_level);
1531 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001532 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001533 } else {
1534 mddev->reshape_position = MaxSector;
1535 mddev->delta_disks = 0;
1536 mddev->new_level = mddev->level;
1537 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001538 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001539 }
1540
NeilBrown41158c72005-06-21 17:17:25 -07001541 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001542 /* Insist of good event counter while assembling, except for
1543 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001545 if (rdev->desc_nr >= 0 &&
1546 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1547 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1548 if (ev1 < mddev->events)
1549 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001550 } else if (mddev->bitmap) {
1551 /* If adding to array with a bitmap, then we can accept an
1552 * older device, but not too old.
1553 */
NeilBrown41158c72005-06-21 17:17:25 -07001554 if (ev1 < mddev->bitmap->events_cleared)
1555 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001556 } else {
1557 if (ev1 < mddev->events)
1558 /* just a hot-add of a new device, leave raid_disk at -1 */
1559 return 0;
1560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (mddev->level != LEVEL_MULTIPATH) {
1562 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001563 if (rdev->desc_nr < 0 ||
1564 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1565 role = 0xffff;
1566 rdev->desc_nr = -1;
1567 } else
1568 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 switch(role) {
1570 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001573 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001576 if ((le32_to_cpu(sb->feature_map) &
1577 MD_FEATURE_RECOVERY_OFFSET))
1578 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1579 else
1580 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 rdev->raid_disk = role;
1582 break;
1583 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001584 if (sb->devflags & WriteMostly1)
1585 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001586 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001587 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590}
1591
1592static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1593{
1594 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mdk_rdev_t *rdev2;
1596 int max_dev, i;
1597 /* make rdev->sb match mddev and rdev data. */
1598
1599 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1600
1601 sb->feature_map = 0;
1602 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001603 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 memset(sb->pad1, 0, sizeof(sb->pad1));
1605 memset(sb->pad2, 0, sizeof(sb->pad2));
1606 memset(sb->pad3, 0, sizeof(sb->pad3));
1607
1608 sb->utime = cpu_to_le64((__u64)mddev->utime);
1609 sb->events = cpu_to_le64(mddev->events);
1610 if (mddev->in_sync)
1611 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1612 else
1613 sb->resync_offset = cpu_to_le64(0);
1614
NeilBrown1c05b4b2006-10-21 10:24:08 -07001615 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001616
NeilBrownf0ca3402006-02-02 14:28:04 -08001617 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001618 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001619 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001620 sb->level = cpu_to_le32(mddev->level);
1621 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001622
NeilBrownc3d97142009-12-14 12:49:52 +11001623 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1624 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001625 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001626 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001627
1628 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001629 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001630 sb->feature_map |=
1631 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1632 sb->recovery_offset =
1633 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001634 }
1635
NeilBrownf6705572006-03-27 01:18:11 -08001636 if (mddev->reshape_position != MaxSector) {
1637 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1638 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1639 sb->new_layout = cpu_to_le32(mddev->new_layout);
1640 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1641 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001642 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001643 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001646 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (rdev2->desc_nr+1 > max_dev)
1648 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001649
NeilBrown70471da2009-08-03 10:59:57 +10001650 if (max_dev > le32_to_cpu(sb->max_dev)) {
1651 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001652 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001653 rdev->sb_size = max_dev * 2 + 256;
1654 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1655 if (rdev->sb_size & bmask)
1656 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001657 } else
1658 max_dev = le32_to_cpu(sb->max_dev);
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 for (i=0; i<max_dev;i++)
1661 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1662
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001663 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001665 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001667 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001669 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001670 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 else
1672 sb->dev_roles[i] = cpu_to_le16(0xffff);
1673 }
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 sb->sb_csum = calc_sb_1_csum(sb);
1676}
1677
Chris Webb0cd17fe2008-06-28 08:31:46 +10001678static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001679super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001680{
1681 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001682 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001683 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001684 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001685 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001686 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001687 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001688 max_sectors -= rdev->data_offset;
1689 if (!num_sectors || num_sectors > max_sectors)
1690 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001691 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001692 /* minor version 0 with bitmap we can't move */
1693 return 0;
1694 } else {
1695 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001696 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001697 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001698 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001699 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001700 if (!num_sectors || num_sectors > max_sectors)
1701 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001702 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001703 }
1704 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001705 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001706 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001707 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001708 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001709 rdev->sb_page);
1710 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001711 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001712}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Adrian Bunk75c96f82005-05-05 16:16:09 -07001714static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 [0] = {
1716 .name = "0.90.0",
1717 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001718 .load_super = super_90_load,
1719 .validate_super = super_90_validate,
1720 .sync_super = super_90_sync,
1721 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 },
1723 [1] = {
1724 .name = "md-1",
1725 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001726 .load_super = super_1_load,
1727 .validate_super = super_1_validate,
1728 .sync_super = super_1_sync,
1729 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 },
1731};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1734{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001735 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
NeilBrown4b809912008-07-21 17:05:25 +10001737 rcu_read_lock();
1738 rdev_for_each_rcu(rdev, mddev1)
1739 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001740 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001741 rdev2->bdev->bd_contains) {
1742 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001743 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001744 }
1745 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 0;
1747}
1748
1749static LIST_HEAD(pending_raid_disks);
1750
Andre Nollac5e7112009-08-03 10:59:47 +10001751/*
1752 * Try to register data integrity profile for an mddev
1753 *
1754 * This is called when an array is started and after a disk has been kicked
1755 * from the array. It only succeeds if all working and active component devices
1756 * are integrity capable with matching profiles.
1757 */
1758int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001759{
Andre Nollac5e7112009-08-03 10:59:47 +10001760 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001761
Andre Nollac5e7112009-08-03 10:59:47 +10001762 if (list_empty(&mddev->disks))
1763 return 0; /* nothing to do */
1764 if (blk_get_integrity(mddev->gendisk))
1765 return 0; /* already registered */
1766 list_for_each_entry(rdev, &mddev->disks, same_set) {
1767 /* skip spares and non-functional disks */
1768 if (test_bit(Faulty, &rdev->flags))
1769 continue;
1770 if (rdev->raid_disk < 0)
1771 continue;
1772 /*
1773 * If at least one rdev is not integrity capable, we can not
1774 * enable data integrity for the md device.
1775 */
1776 if (!bdev_get_integrity(rdev->bdev))
1777 return -EINVAL;
1778 if (!reference) {
1779 /* Use the first rdev as the reference */
1780 reference = rdev;
1781 continue;
1782 }
1783 /* does this rdev's profile match the reference profile? */
1784 if (blk_integrity_compare(reference->bdev->bd_disk,
1785 rdev->bdev->bd_disk) < 0)
1786 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001787 }
Andre Nollac5e7112009-08-03 10:59:47 +10001788 /*
1789 * All component devices are integrity capable and have matching
1790 * profiles, register the common profile for the md device.
1791 */
1792 if (blk_integrity_register(mddev->gendisk,
1793 bdev_get_integrity(reference->bdev)) != 0) {
1794 printk(KERN_ERR "md: failed to register integrity for %s\n",
1795 mdname(mddev));
1796 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001797 }
Andre Nollac5e7112009-08-03 10:59:47 +10001798 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1799 mdname(mddev));
1800 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001801}
Andre Nollac5e7112009-08-03 10:59:47 +10001802EXPORT_SYMBOL(md_integrity_register);
1803
1804/* Disable data integrity if non-capable/non-matching disk is being added */
1805void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1806{
1807 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1808 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1809
1810 if (!bi_mddev) /* nothing to do */
1811 return;
1812 if (rdev->raid_disk < 0) /* skip spares */
1813 return;
1814 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1815 rdev->bdev->bd_disk) >= 0)
1816 return;
1817 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1818 blk_integrity_unregister(mddev->gendisk);
1819}
1820EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1823{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001824 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001825 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001826 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001827 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (rdev->mddev) {
1830 MD_BUG();
1831 return -EINVAL;
1832 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001833
1834 /* prevent duplicates */
1835 if (find_rdev(mddev, rdev->bdev->bd_dev))
1836 return -EEXIST;
1837
Andre Nolldd8ac332009-03-31 14:33:13 +11001838 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1839 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1840 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001841 if (mddev->pers) {
1842 /* Cannot change size, so fail
1843 * If mddev->level <= 0, then we don't care
1844 * about aligning sizes (e.g. linear)
1845 */
1846 if (mddev->level > 0)
1847 return -ENOSPC;
1848 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001849 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* Verify rdev->desc_nr is unique.
1853 * If it is -1, assign a free number, else
1854 * check number is not in use
1855 */
1856 if (rdev->desc_nr < 0) {
1857 int choice = 0;
1858 if (mddev->pers) choice = mddev->raid_disks;
1859 while (find_rdev_nr(mddev, choice))
1860 choice++;
1861 rdev->desc_nr = choice;
1862 } else {
1863 if (find_rdev_nr(mddev, rdev->desc_nr))
1864 return -EBUSY;
1865 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001866 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1867 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1868 mdname(mddev), mddev->max_disks);
1869 return -EBUSY;
1870 }
NeilBrown19133a42005-11-08 21:39:35 -08001871 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001872 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001873 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001876 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001877
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001878 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001879 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001880
Tejun Heo0762b8b2008-08-25 19:56:12 +09001881 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001882 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1883 /* failure here is OK */;
1884 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001885
NeilBrown4b809912008-07-21 17:05:25 +10001886 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001887 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001888
1889 /* May as well allow recovery to be retried once */
1890 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001893
1894 fail:
1895 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1896 b, mdname(mddev));
1897 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
NeilBrown177a99b2008-02-06 01:39:56 -08001900static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001901{
1902 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1903 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001904 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1908{
1909 char b[BDEVNAME_SIZE];
1910 if (!rdev->mddev) {
1911 MD_BUG();
1912 return;
1913 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001914 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001915 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1917 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001918 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001919 sysfs_put(rdev->sysfs_state);
1920 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001921 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001922 * writing to 'remove' to "dev/state". We also need
1923 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001924 */
NeilBrown4b809912008-07-21 17:05:25 +10001925 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001926 INIT_WORK(&rdev->del_work, md_delayed_delete);
1927 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001928 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931/*
1932 * prevent the device from being mounted, repartitioned or
1933 * otherwise reused by a RAID array (or any other kernel
1934 * subsystem), by bd_claiming the device.
1935 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001936static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 int err = 0;
1939 struct block_device *bdev;
1940 char b[BDEVNAME_SIZE];
1941
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001942 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (IS_ERR(bdev)) {
1944 printk(KERN_ERR "md: could not open %s.\n",
1945 __bdevname(dev, b));
1946 return PTR_ERR(bdev);
1947 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001948 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (err) {
1950 printk(KERN_ERR "md: could not bd_claim %s.\n",
1951 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001952 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return err;
1954 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001955 if (!shared)
1956 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 rdev->bdev = bdev;
1958 return err;
1959}
1960
1961static void unlock_rdev(mdk_rdev_t *rdev)
1962{
1963 struct block_device *bdev = rdev->bdev;
1964 rdev->bdev = NULL;
1965 if (!bdev)
1966 MD_BUG();
1967 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001968 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971void md_autodetect_dev(dev_t dev);
1972
1973static void export_rdev(mdk_rdev_t * rdev)
1974{
1975 char b[BDEVNAME_SIZE];
1976 printk(KERN_INFO "md: export_rdev(%s)\n",
1977 bdevname(rdev->bdev,b));
1978 if (rdev->mddev)
1979 MD_BUG();
1980 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001982 if (test_bit(AutoDetected, &rdev->flags))
1983 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984#endif
1985 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001986 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
1989static void kick_rdev_from_array(mdk_rdev_t * rdev)
1990{
1991 unbind_rdev_from_array(rdev);
1992 export_rdev(rdev);
1993}
1994
1995static void export_array(mddev_t *mddev)
1996{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001997 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
NeilBrownd089c6a2008-02-06 01:39:59 -08001999 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (!rdev->mddev) {
2001 MD_BUG();
2002 continue;
2003 }
2004 kick_rdev_from_array(rdev);
2005 }
2006 if (!list_empty(&mddev->disks))
2007 MD_BUG();
2008 mddev->raid_disks = 0;
2009 mddev->major_version = 0;
2010}
2011
2012static void print_desc(mdp_disk_t *desc)
2013{
2014 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2015 desc->major,desc->minor,desc->raid_disk,desc->state);
2016}
2017
Cheng Renquancd2ac932009-01-09 08:31:08 +11002018static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 int i;
2021
2022 printk(KERN_INFO
2023 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2024 sb->major_version, sb->minor_version, sb->patch_version,
2025 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2026 sb->ctime);
2027 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2028 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2029 sb->md_minor, sb->layout, sb->chunk_size);
2030 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2031 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2032 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2033 sb->failed_disks, sb->spare_disks,
2034 sb->sb_csum, (unsigned long)sb->events_lo);
2035
2036 printk(KERN_INFO);
2037 for (i = 0; i < MD_SB_DISKS; i++) {
2038 mdp_disk_t *desc;
2039
2040 desc = sb->disks + i;
2041 if (desc->number || desc->major || desc->minor ||
2042 desc->raid_disk || (desc->state && (desc->state != 4))) {
2043 printk(" D %2d: ", i);
2044 print_desc(desc);
2045 }
2046 }
2047 printk(KERN_INFO "md: THIS: ");
2048 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Cheng Renquancd2ac932009-01-09 08:31:08 +11002051static void print_sb_1(struct mdp_superblock_1 *sb)
2052{
2053 __u8 *uuid;
2054
2055 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002056 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002057 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002058 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002059 le32_to_cpu(sb->major_version),
2060 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002061 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002062 sb->set_name,
2063 (unsigned long long)le64_to_cpu(sb->ctime)
2064 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2065
2066 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002067 printk(KERN_INFO
2068 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002069 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002070 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002071 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2072 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002073 le32_to_cpu(sb->level),
2074 (unsigned long long)le64_to_cpu(sb->size),
2075 le32_to_cpu(sb->raid_disks),
2076 le32_to_cpu(sb->layout),
2077 le32_to_cpu(sb->chunksize),
2078 (unsigned long long)le64_to_cpu(sb->data_offset),
2079 (unsigned long long)le64_to_cpu(sb->data_size),
2080 (unsigned long long)le64_to_cpu(sb->super_offset),
2081 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2082 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002083 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002084 sb->devflags,
2085 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2086 (unsigned long long)le64_to_cpu(sb->events),
2087 (unsigned long long)le64_to_cpu(sb->resync_offset),
2088 le32_to_cpu(sb->sb_csum),
2089 le32_to_cpu(sb->max_dev)
2090 );
2091}
2092
2093static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002096 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2097 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002098 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2099 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002101 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2102 switch (major_version) {
2103 case 0:
2104 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2105 break;
2106 case 1:
2107 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2108 break;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else
2111 printk(KERN_INFO "md: no rdev superblock!\n");
2112}
2113
Adrian Bunk5e563412006-06-26 00:27:42 -07002114static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002116 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 mdk_rdev_t *rdev;
2118 mddev_t *mddev;
2119 char b[BDEVNAME_SIZE];
2120
2121 printk("\n");
2122 printk("md: **********************************\n");
2123 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2124 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002125 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
NeilBrown32a76272005-06-21 17:17:14 -07002127 if (mddev->bitmap)
2128 bitmap_print_sb(mddev->bitmap);
2129 else
2130 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002131 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 printk("<%s>", bdevname(rdev->bdev,b));
2133 printk("\n");
2134
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002135 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002136 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 printk("md: **********************************\n");
2139 printk("\n");
2140}
2141
2142
NeilBrown42543762006-06-26 00:27:57 -07002143static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
NeilBrown42543762006-06-26 00:27:57 -07002145 /* Update each superblock (in-memory image), but
2146 * if we are allowed to, skip spares which already
2147 * have the right event counter, or have one earlier
2148 * (which would mean they aren't being marked as dirty
2149 * with the rest of the array)
2150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002152 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_events == mddev->events ||
2154 (nospares &&
2155 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002156 rdev->sb_events+1 == mddev->events)) {
2157 /* Don't update this superblock */
2158 rdev->sb_loaded = 2;
2159 } else {
2160 super_types[mddev->major_version].
2161 sync_super(mddev, rdev);
2162 rdev->sb_loaded = 1;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165}
2166
NeilBrown850b2b42006-10-03 01:15:46 -07002167static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002170 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002171 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002174 /* First make sure individual recovery_offsets are correct */
2175 list_for_each_entry(rdev, &mddev->disks, same_set) {
2176 if (rdev->raid_disk >= 0 &&
2177 mddev->delta_disks >= 0 &&
2178 !test_bit(In_sync, &rdev->flags) &&
2179 mddev->curr_resync_completed > rdev->recovery_offset)
2180 rdev->recovery_offset = mddev->curr_resync_completed;
2181
2182 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002183 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002184 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002185 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002186 if (!mddev->external)
2187 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002188 wake_up(&mddev->sb_wait);
2189 return;
2190 }
2191
NeilBrowna9701a32005-11-08 21:39:34 -08002192 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002193
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002194 mddev->utime = get_seconds();
2195
NeilBrown850b2b42006-10-03 01:15:46 -07002196 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2197 force_change = 1;
2198 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2199 /* just a clean<-> dirty transition, possibly leave spares alone,
2200 * though if events isn't the right even/odd, we will have to do
2201 * spares after all
2202 */
2203 nospares = 1;
2204 if (force_change)
2205 nospares = 0;
2206 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002207 /* If the array is degraded, then skipping spares is both
2208 * dangerous and fairly pointless.
2209 * Dangerous because a device that was removed from the array
2210 * might have a event_count that still looks up-to-date,
2211 * so it can be re-added without a resync.
2212 * Pointless because if there are any spares to skip,
2213 * then a recovery will happen and soon that array won't
2214 * be degraded any more and the spare can go back to sleep then.
2215 */
NeilBrown850b2b42006-10-03 01:15:46 -07002216 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002217
NeilBrown06d91a52005-06-21 17:17:12 -07002218 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002219
2220 /* If this is just a dirty<->clean transition, and the array is clean
2221 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002222 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002223 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002224 && mddev->can_decrease_events
2225 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002226 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002227 mddev->can_decrease_events = 0;
2228 } else {
NeilBrown42543762006-06-26 00:27:57 -07002229 /* otherwise we have to go forward and ... */
2230 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002231 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 if (!mddev->events) {
2235 /*
2236 * oops, this 64-bit counter should never wrap.
2237 * Either we are in around ~1 trillion A.C., assuming
2238 * 1 reboot per second, or we have a bug:
2239 */
2240 MD_BUG();
2241 mddev->events --;
2242 }
NeilBrowne6910632008-02-06 01:39:51 -08002243 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002244 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 dprintk(KERN_INFO
2247 "md: updating %s RAID superblock on device (in sync %d)\n",
2248 mdname(mddev),mddev->in_sync);
2249
NeilBrown4ad13662007-07-17 04:06:13 -07002250 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002251 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 char b[BDEVNAME_SIZE];
2253 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002254 if (rdev->sb_loaded != 1)
2255 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002256 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 dprintk("(skipping faulty ");
2258
2259 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002260 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002261 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002262 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002263 rdev->sb_page);
2264 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2265 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002266 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002267 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 } else
2270 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002271 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /* only need to write one superblock... */
2273 break;
2274 }
NeilBrowna9701a32005-11-08 21:39:34 -08002275 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002276 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002277
NeilBrowna9701a32005-11-08 21:39:34 -08002278 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002279 if (mddev->in_sync != sync_req ||
2280 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002281 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002282 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002283 goto repeat;
2284 }
NeilBrown850b2b42006-10-03 01:15:46 -07002285 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002286 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002287 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002288 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2289 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Andre Noll7f6ce762008-03-23 18:34:54 +01002293/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002294 * We want to accept with case. For this we use cmd_match.
2295 */
2296static int cmd_match(const char *cmd, const char *str)
2297{
2298 /* See if cmd, written into a sysfs file, matches
2299 * str. They must either be the same, or cmd can
2300 * have a trailing newline
2301 */
2302 while (*cmd && *str && *cmd == *str) {
2303 cmd++;
2304 str++;
2305 }
2306 if (*cmd == '\n')
2307 cmd++;
2308 if (*str || *cmd)
2309 return 0;
2310 return 1;
2311}
2312
NeilBrown86e6ffd2005-11-08 21:39:24 -08002313struct rdev_sysfs_entry {
2314 struct attribute attr;
2315 ssize_t (*show)(mdk_rdev_t *, char *);
2316 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2317};
2318
2319static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002320state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002321{
2322 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002323 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002324
NeilBrownb2d444d2005-11-08 21:39:31 -08002325 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326 len+= sprintf(page+len, "%sfaulty",sep);
2327 sep = ",";
2328 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002329 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002330 len += sprintf(page+len, "%sin_sync",sep);
2331 sep = ",";
2332 }
NeilBrownf6556752006-06-26 00:28:01 -07002333 if (test_bit(WriteMostly, &rdev->flags)) {
2334 len += sprintf(page+len, "%swrite_mostly",sep);
2335 sep = ",";
2336 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002337 if (test_bit(Blocked, &rdev->flags)) {
2338 len += sprintf(page+len, "%sblocked", sep);
2339 sep = ",";
2340 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002341 if (!test_bit(Faulty, &rdev->flags) &&
2342 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002343 len += sprintf(page+len, "%sspare", sep);
2344 sep = ",";
2345 }
2346 return len+sprintf(page+len, "\n");
2347}
2348
NeilBrown45dc2de2006-06-26 00:27:58 -07002349static ssize_t
2350state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2351{
2352 /* can write
2353 * faulty - simulates and error
2354 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002355 * writemostly - sets write_mostly
2356 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002357 * blocked - sets the Blocked flag
2358 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002359 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002360 */
2361 int err = -EINVAL;
2362 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2363 md_error(rdev->mddev, rdev);
2364 err = 0;
2365 } else if (cmd_match(buf, "remove")) {
2366 if (rdev->raid_disk >= 0)
2367 err = -EBUSY;
2368 else {
2369 mddev_t *mddev = rdev->mddev;
2370 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002371 if (mddev->pers)
2372 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002373 md_new_event(mddev);
2374 err = 0;
2375 }
NeilBrownf6556752006-06-26 00:28:01 -07002376 } else if (cmd_match(buf, "writemostly")) {
2377 set_bit(WriteMostly, &rdev->flags);
2378 err = 0;
2379 } else if (cmd_match(buf, "-writemostly")) {
2380 clear_bit(WriteMostly, &rdev->flags);
2381 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002382 } else if (cmd_match(buf, "blocked")) {
2383 set_bit(Blocked, &rdev->flags);
2384 err = 0;
2385 } else if (cmd_match(buf, "-blocked")) {
2386 clear_bit(Blocked, &rdev->flags);
2387 wake_up(&rdev->blocked_wait);
2388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2389 md_wakeup_thread(rdev->mddev->thread);
2390
2391 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002392 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2393 set_bit(In_sync, &rdev->flags);
2394 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002395 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002396 if (!err)
2397 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002398 return err ? err : len;
2399}
NeilBrown80ca3a42006-07-10 04:44:18 -07002400static struct rdev_sysfs_entry rdev_state =
2401__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002402
2403static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002404errors_show(mdk_rdev_t *rdev, char *page)
2405{
2406 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2407}
2408
2409static ssize_t
2410errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2411{
2412 char *e;
2413 unsigned long n = simple_strtoul(buf, &e, 10);
2414 if (*buf && (*e == 0 || *e == '\n')) {
2415 atomic_set(&rdev->corrected_errors, n);
2416 return len;
2417 }
2418 return -EINVAL;
2419}
2420static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002421__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002422
NeilBrown014236d2006-01-06 00:20:55 -08002423static ssize_t
2424slot_show(mdk_rdev_t *rdev, char *page)
2425{
2426 if (rdev->raid_disk < 0)
2427 return sprintf(page, "none\n");
2428 else
2429 return sprintf(page, "%d\n", rdev->raid_disk);
2430}
2431
2432static ssize_t
2433slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2434{
2435 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002436 int err;
2437 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002438 int slot = simple_strtoul(buf, &e, 10);
2439 if (strncmp(buf, "none", 4)==0)
2440 slot = -1;
2441 else if (e==buf || (*e && *e!= '\n'))
2442 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002443 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002444 /* Setting 'slot' on an active array requires also
2445 * updating the 'rd%d' link, and communicating
2446 * with the personality with ->hot_*_disk.
2447 * For now we only support removing
2448 * failed/spare devices. This normally happens automatically,
2449 * but not when the metadata is externally managed.
2450 */
NeilBrownc303da62008-02-06 01:39:51 -08002451 if (rdev->raid_disk == -1)
2452 return -EEXIST;
2453 /* personality does all needed checks */
2454 if (rdev->mddev->pers->hot_add_disk == NULL)
2455 return -EINVAL;
2456 err = rdev->mddev->pers->
2457 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2458 if (err)
2459 return err;
2460 sprintf(nm, "rd%d", rdev->raid_disk);
2461 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002462 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002463 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2464 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002465 } else if (rdev->mddev->pers) {
2466 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002467 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002468 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002469 */
2470
2471 if (rdev->raid_disk != -1)
2472 return -EBUSY;
2473
2474 if (rdev->mddev->pers->hot_add_disk == NULL)
2475 return -EINVAL;
2476
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002477 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002478 if (rdev2->raid_disk == slot)
2479 return -EEXIST;
2480
2481 rdev->raid_disk = slot;
2482 if (test_bit(In_sync, &rdev->flags))
2483 rdev->saved_raid_disk = slot;
2484 else
2485 rdev->saved_raid_disk = -1;
2486 err = rdev->mddev->pers->
2487 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002488 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002489 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002490 return err;
Neil Brown52664732008-06-28 08:31:44 +10002491 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002492 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002493 sprintf(nm, "rd%d", rdev->raid_disk);
2494 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002495 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002496 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002497 } else {
2498 if (slot >= rdev->mddev->raid_disks)
2499 return -ENOSPC;
2500 rdev->raid_disk = slot;
2501 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002502 clear_bit(Faulty, &rdev->flags);
2503 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002504 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002505 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002506 }
NeilBrown014236d2006-01-06 00:20:55 -08002507 return len;
2508}
2509
2510
2511static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002512__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002513
NeilBrown93c8cad2006-01-06 00:20:56 -08002514static ssize_t
2515offset_show(mdk_rdev_t *rdev, char *page)
2516{
Andrew Morton6961ece2006-01-06 00:20:59 -08002517 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002518}
2519
2520static ssize_t
2521offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2522{
2523 char *e;
2524 unsigned long long offset = simple_strtoull(buf, &e, 10);
2525 if (e==buf || (*e && *e != '\n'))
2526 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002527 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002528 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002529 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002530 /* Must set offset before size, so overlap checks
2531 * can be sane */
2532 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002533 rdev->data_offset = offset;
2534 return len;
2535}
2536
2537static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002538__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002539
NeilBrown83303b62006-01-06 00:21:06 -08002540static ssize_t
2541rdev_size_show(mdk_rdev_t *rdev, char *page)
2542{
Andre Nolldd8ac332009-03-31 14:33:13 +11002543 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002544}
2545
NeilBrownc5d79ad2008-02-06 01:39:54 -08002546static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2547{
2548 /* check if two start/length pairs overlap */
2549 if (s1+l1 <= s2)
2550 return 0;
2551 if (s2+l2 <= s1)
2552 return 0;
2553 return 1;
2554}
2555
Dan Williamsb522adc2009-03-31 15:00:31 +11002556static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2557{
2558 unsigned long long blocks;
2559 sector_t new;
2560
2561 if (strict_strtoull(buf, 10, &blocks) < 0)
2562 return -EINVAL;
2563
2564 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2565 return -EINVAL; /* sector conversion overflow */
2566
2567 new = blocks * 2;
2568 if (new != blocks * 2)
2569 return -EINVAL; /* unsigned long long to sector_t overflow */
2570
2571 *sectors = new;
2572 return 0;
2573}
2574
NeilBrown83303b62006-01-06 00:21:06 -08002575static ssize_t
2576rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2577{
NeilBrown27c529b2008-03-04 14:29:33 -08002578 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002579 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002580 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002581
Dan Williamsb522adc2009-03-31 15:00:31 +11002582 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002583 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002584 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002585 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002586 sectors = super_types[my_mddev->major_version].
2587 rdev_size_change(rdev, sectors);
2588 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002589 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002590 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002591 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002592 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002593 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002594 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002595 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002596
Andre Nolldd8ac332009-03-31 14:33:13 +11002597 rdev->sectors = sectors;
2598 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002599 /* need to check that all other rdevs with the same ->bdev
2600 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002601 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002602 * we have to change it back, we will have the lock again.
2603 */
2604 mddev_t *mddev;
2605 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002606 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002607
NeilBrown27c529b2008-03-04 14:29:33 -08002608 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002609 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002610 mdk_rdev_t *rdev2;
2611
2612 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002613 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002614 if (test_bit(AllReserved, &rdev2->flags) ||
2615 (rdev->bdev == rdev2->bdev &&
2616 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002617 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002618 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002619 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002620 overlap = 1;
2621 break;
2622 }
2623 mddev_unlock(mddev);
2624 if (overlap) {
2625 mddev_put(mddev);
2626 break;
2627 }
2628 }
NeilBrown27c529b2008-03-04 14:29:33 -08002629 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002630 if (overlap) {
2631 /* Someone else could have slipped in a size
2632 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002633 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002634 * safe, and trust userspace not to race with
2635 * itself
2636 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002637 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002638 return -EBUSY;
2639 }
2640 }
NeilBrown83303b62006-01-06 00:21:06 -08002641 return len;
2642}
2643
2644static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002645__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002646
Dan Williams06e3c812009-12-12 21:17:12 -07002647
2648static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2649{
2650 unsigned long long recovery_start = rdev->recovery_offset;
2651
2652 if (test_bit(In_sync, &rdev->flags) ||
2653 recovery_start == MaxSector)
2654 return sprintf(page, "none\n");
2655
2656 return sprintf(page, "%llu\n", recovery_start);
2657}
2658
2659static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2660{
2661 unsigned long long recovery_start;
2662
2663 if (cmd_match(buf, "none"))
2664 recovery_start = MaxSector;
2665 else if (strict_strtoull(buf, 10, &recovery_start))
2666 return -EINVAL;
2667
2668 if (rdev->mddev->pers &&
2669 rdev->raid_disk >= 0)
2670 return -EBUSY;
2671
2672 rdev->recovery_offset = recovery_start;
2673 if (recovery_start == MaxSector)
2674 set_bit(In_sync, &rdev->flags);
2675 else
2676 clear_bit(In_sync, &rdev->flags);
2677 return len;
2678}
2679
2680static struct rdev_sysfs_entry rdev_recovery_start =
2681__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2682
NeilBrown86e6ffd2005-11-08 21:39:24 -08002683static struct attribute *rdev_default_attrs[] = {
2684 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002685 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002686 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002687 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002688 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002689 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002690 NULL,
2691};
2692static ssize_t
2693rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2694{
2695 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2696 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002697 mddev_t *mddev = rdev->mddev;
2698 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002699
2700 if (!entry->show)
2701 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002702
2703 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2704 if (!rv) {
2705 if (rdev->mddev == NULL)
2706 rv = -EBUSY;
2707 else
2708 rv = entry->show(rdev, page);
2709 mddev_unlock(mddev);
2710 }
2711 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002712}
2713
2714static ssize_t
2715rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2716 const char *page, size_t length)
2717{
2718 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2719 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002720 ssize_t rv;
2721 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002722
2723 if (!entry->store)
2724 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002725 if (!capable(CAP_SYS_ADMIN))
2726 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002727 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002728 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002729 if (rdev->mddev == NULL)
2730 rv = -EBUSY;
2731 else
2732 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002733 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002734 }
2735 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002736}
2737
2738static void rdev_free(struct kobject *ko)
2739{
2740 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2741 kfree(rdev);
2742}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002743static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002744 .show = rdev_attr_show,
2745 .store = rdev_attr_store,
2746};
2747static struct kobj_type rdev_ktype = {
2748 .release = rdev_free,
2749 .sysfs_ops = &rdev_sysfs_ops,
2750 .default_attrs = rdev_default_attrs,
2751};
2752
NeilBrowne8bb9a82010-06-01 19:37:26 +10002753void md_rdev_init(mdk_rdev_t *rdev)
2754{
2755 rdev->desc_nr = -1;
2756 rdev->saved_raid_disk = -1;
2757 rdev->raid_disk = -1;
2758 rdev->flags = 0;
2759 rdev->data_offset = 0;
2760 rdev->sb_events = 0;
2761 rdev->last_read_error.tv_sec = 0;
2762 rdev->last_read_error.tv_nsec = 0;
2763 atomic_set(&rdev->nr_pending, 0);
2764 atomic_set(&rdev->read_errors, 0);
2765 atomic_set(&rdev->corrected_errors, 0);
2766
2767 INIT_LIST_HEAD(&rdev->same_set);
2768 init_waitqueue_head(&rdev->blocked_wait);
2769}
2770EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771/*
2772 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2773 *
2774 * mark the device faulty if:
2775 *
2776 * - the device is nonexistent (zero size)
2777 * - the device has no valid superblock
2778 *
2779 * a faulty rdev _never_ has rdev->sb set.
2780 */
2781static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2782{
2783 char b[BDEVNAME_SIZE];
2784 int err;
2785 mdk_rdev_t *rdev;
2786 sector_t size;
2787
NeilBrown9ffae0c2006-01-06 00:20:32 -08002788 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (!rdev) {
2790 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2791 return ERR_PTR(-ENOMEM);
2792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
NeilBrowne8bb9a82010-06-01 19:37:26 +10002794 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if ((err = alloc_disk_sb(rdev)))
2796 goto abort_free;
2797
NeilBrownc5d79ad2008-02-06 01:39:54 -08002798 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (err)
2800 goto abort_free;
2801
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002802 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002803
Mike Snitzer77304d22010-11-08 14:39:12 +01002804 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (!size) {
2806 printk(KERN_WARNING
2807 "md: %s has zero or unknown size, marking faulty!\n",
2808 bdevname(rdev->bdev,b));
2809 err = -EINVAL;
2810 goto abort_free;
2811 }
2812
2813 if (super_format >= 0) {
2814 err = super_types[super_format].
2815 load_super(rdev, NULL, super_minor);
2816 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002817 printk(KERN_WARNING
2818 "md: %s does not have a valid v%d.%d "
2819 "superblock, not importing!\n",
2820 bdevname(rdev->bdev,b),
2821 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 goto abort_free;
2823 }
2824 if (err < 0) {
2825 printk(KERN_WARNING
2826 "md: could not read %s's sb, not importing!\n",
2827 bdevname(rdev->bdev,b));
2828 goto abort_free;
2829 }
2830 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return rdev;
2833
2834abort_free:
2835 if (rdev->sb_page) {
2836 if (rdev->bdev)
2837 unlock_rdev(rdev);
2838 free_disk_sb(rdev);
2839 }
2840 kfree(rdev);
2841 return ERR_PTR(err);
2842}
2843
2844/*
2845 * Check a full RAID array for plausibility
2846 */
2847
2848
NeilBrowna757e642005-04-16 15:26:42 -07002849static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002852 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 char b[BDEVNAME_SIZE];
2854
2855 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002856 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 switch (super_types[mddev->major_version].
2858 load_super(rdev, freshest, mddev->minor_version)) {
2859 case 1:
2860 freshest = rdev;
2861 break;
2862 case 0:
2863 break;
2864 default:
2865 printk( KERN_ERR \
2866 "md: fatal superblock inconsistency in %s"
2867 " -- removing from array\n",
2868 bdevname(rdev->bdev,b));
2869 kick_rdev_from_array(rdev);
2870 }
2871
2872
2873 super_types[mddev->major_version].
2874 validate_super(mddev, freshest);
2875
2876 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002877 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002878 if (mddev->max_disks &&
2879 (rdev->desc_nr >= mddev->max_disks ||
2880 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002881 printk(KERN_WARNING
2882 "md: %s: %s: only %d devices permitted\n",
2883 mdname(mddev), bdevname(rdev->bdev, b),
2884 mddev->max_disks);
2885 kick_rdev_from_array(rdev);
2886 continue;
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (rdev != freshest)
2889 if (super_types[mddev->major_version].
2890 validate_super(mddev, rdev)) {
2891 printk(KERN_WARNING "md: kicking non-fresh %s"
2892 " from array!\n",
2893 bdevname(rdev->bdev,b));
2894 kick_rdev_from_array(rdev);
2895 continue;
2896 }
2897 if (mddev->level == LEVEL_MULTIPATH) {
2898 rdev->desc_nr = i++;
2899 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002900 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002901 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002902 rdev->raid_disk = -1;
2903 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
NeilBrown72e02072009-12-14 12:49:55 +11002908/* Read a fixed-point number.
2909 * Numbers in sysfs attributes should be in "standard" units where
2910 * possible, so time should be in seconds.
2911 * However we internally use a a much smaller unit such as
2912 * milliseconds or jiffies.
2913 * This function takes a decimal number with a possible fractional
2914 * component, and produces an integer which is the result of
2915 * multiplying that number by 10^'scale'.
2916 * all without any floating-point arithmetic.
2917 */
2918int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2919{
2920 unsigned long result = 0;
2921 long decimals = -1;
2922 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2923 if (*cp == '.')
2924 decimals = 0;
2925 else if (decimals < scale) {
2926 unsigned int value;
2927 value = *cp - '0';
2928 result = result * 10 + value;
2929 if (decimals >= 0)
2930 decimals++;
2931 }
2932 cp++;
2933 }
2934 if (*cp == '\n')
2935 cp++;
2936 if (*cp)
2937 return -EINVAL;
2938 if (decimals < 0)
2939 decimals = 0;
2940 while (decimals < scale) {
2941 result *= 10;
2942 decimals ++;
2943 }
2944 *res = result;
2945 return 0;
2946}
2947
2948
NeilBrown19052c02008-08-05 15:54:13 +10002949static void md_safemode_timeout(unsigned long data);
2950
NeilBrowneae17012005-11-08 21:39:23 -08002951static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002952safe_delay_show(mddev_t *mddev, char *page)
2953{
2954 int msec = (mddev->safemode_delay*1000)/HZ;
2955 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2956}
2957static ssize_t
2958safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2959{
NeilBrown16f17b32006-06-26 00:27:37 -07002960 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002961
NeilBrown72e02072009-12-14 12:49:55 +11002962 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002963 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002964 if (msec == 0)
2965 mddev->safemode_delay = 0;
2966 else {
NeilBrown19052c02008-08-05 15:54:13 +10002967 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002968 mddev->safemode_delay = (msec*HZ)/1000;
2969 if (mddev->safemode_delay == 0)
2970 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002971 if (mddev->safemode_delay < old_delay)
2972 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002973 }
2974 return len;
2975}
2976static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002977__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002978
2979static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002980level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002981{
NeilBrown2604b702006-01-06 00:20:36 -08002982 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002983 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002984 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002985 else if (mddev->clevel[0])
2986 return sprintf(page, "%s\n", mddev->clevel);
2987 else if (mddev->level != LEVEL_NONE)
2988 return sprintf(page, "%d\n", mddev->level);
2989 else
2990 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002991}
2992
NeilBrownd9d166c2006-01-06 00:20:51 -08002993static ssize_t
2994level_store(mddev_t *mddev, const char *buf, size_t len)
2995{
Dan Williamsf2859af2010-05-02 10:04:16 -07002996 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002997 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002998 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002999 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003000 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003001 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003002
3003 if (mddev->pers == NULL) {
3004 if (len == 0)
3005 return 0;
3006 if (len >= sizeof(mddev->clevel))
3007 return -ENOSPC;
3008 strncpy(mddev->clevel, buf, len);
3009 if (mddev->clevel[len-1] == '\n')
3010 len--;
3011 mddev->clevel[len] = 0;
3012 mddev->level = LEVEL_NONE;
3013 return rv;
3014 }
3015
3016 /* request to change the personality. Need to ensure:
3017 * - array is not engaged in resync/recovery/reshape
3018 * - old personality can be suspended
3019 * - new personality will access other array.
3020 */
3021
NeilBrownbb4f1e92010-08-08 21:18:03 +10003022 if (mddev->sync_thread ||
3023 mddev->reshape_position != MaxSector ||
3024 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003025 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003026
3027 if (!mddev->pers->quiesce) {
3028 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3029 mdname(mddev), mddev->pers->name);
3030 return -EINVAL;
3031 }
3032
3033 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003034 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003035 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003036 strncpy(clevel, buf, len);
3037 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003038 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003039 clevel[len] = 0;
3040 if (strict_strtol(clevel, 10, &level))
3041 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003042
Dan Williamsf2859af2010-05-02 10:04:16 -07003043 if (request_module("md-%s", clevel) != 0)
3044 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003045 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003046 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003047 if (!pers || !try_module_get(pers->owner)) {
3048 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003049 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003050 return -EINVAL;
3051 }
3052 spin_unlock(&pers_lock);
3053
3054 if (pers == mddev->pers) {
3055 /* Nothing to do! */
3056 module_put(pers->owner);
3057 return rv;
3058 }
3059 if (!pers->takeover) {
3060 module_put(pers->owner);
3061 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003062 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003063 return -EINVAL;
3064 }
3065
NeilBrowne93f68a2010-06-15 09:36:03 +01003066 list_for_each_entry(rdev, &mddev->disks, same_set)
3067 rdev->new_raid_disk = rdev->raid_disk;
3068
NeilBrown245f46c2009-03-31 14:39:39 +11003069 /* ->takeover must set new_* and/or delta_disks
3070 * if it succeeds, and may set them when it fails.
3071 */
3072 priv = pers->takeover(mddev);
3073 if (IS_ERR(priv)) {
3074 mddev->new_level = mddev->level;
3075 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003076 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003077 mddev->raid_disks -= mddev->delta_disks;
3078 mddev->delta_disks = 0;
3079 module_put(pers->owner);
3080 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003081 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003082 return PTR_ERR(priv);
3083 }
3084
3085 /* Looks like we have a winner */
3086 mddev_suspend(mddev);
3087 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003088
3089 if (mddev->pers->sync_request == NULL &&
3090 pers->sync_request != NULL) {
3091 /* need to add the md_redundancy_group */
3092 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3093 printk(KERN_WARNING
3094 "md: cannot register extra attributes for %s\n",
3095 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003096 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003097 }
3098 if (mddev->pers->sync_request != NULL &&
3099 pers->sync_request == NULL) {
3100 /* need to remove the md_redundancy_group */
3101 if (mddev->to_remove == NULL)
3102 mddev->to_remove = &md_redundancy_group;
3103 }
3104
Trela Maciej54071b32010-03-08 16:02:42 +11003105 if (mddev->pers->sync_request == NULL &&
3106 mddev->external) {
3107 /* We are converting from a no-redundancy array
3108 * to a redundancy array and metadata is managed
3109 * externally so we need to be sure that writes
3110 * won't block due to a need to transition
3111 * clean->dirty
3112 * until external management is started.
3113 */
3114 mddev->in_sync = 0;
3115 mddev->safemode_delay = 0;
3116 mddev->safemode = 0;
3117 }
3118
NeilBrowne93f68a2010-06-15 09:36:03 +01003119 list_for_each_entry(rdev, &mddev->disks, same_set) {
3120 char nm[20];
3121 if (rdev->raid_disk < 0)
3122 continue;
3123 if (rdev->new_raid_disk > mddev->raid_disks)
3124 rdev->new_raid_disk = -1;
3125 if (rdev->new_raid_disk == rdev->raid_disk)
3126 continue;
3127 sprintf(nm, "rd%d", rdev->raid_disk);
3128 sysfs_remove_link(&mddev->kobj, nm);
3129 }
3130 list_for_each_entry(rdev, &mddev->disks, same_set) {
3131 if (rdev->raid_disk < 0)
3132 continue;
3133 if (rdev->new_raid_disk == rdev->raid_disk)
3134 continue;
3135 rdev->raid_disk = rdev->new_raid_disk;
3136 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003137 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003138 else {
3139 char nm[20];
3140 sprintf(nm, "rd%d", rdev->raid_disk);
3141 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3142 printk("md: cannot register %s for %s after level change\n",
3143 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003144 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003145 }
3146
3147 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003148 mddev->pers = pers;
3149 mddev->private = priv;
3150 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3151 mddev->level = mddev->new_level;
3152 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003153 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003154 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003155 if (mddev->pers->sync_request == NULL) {
3156 /* this is now an array without redundancy, so
3157 * it must always be in_sync
3158 */
3159 mddev->in_sync = 1;
3160 del_timer_sync(&mddev->safemode_timer);
3161 }
NeilBrown245f46c2009-03-31 14:39:39 +11003162 pers->run(mddev);
3163 mddev_resume(mddev);
3164 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3166 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003167 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003168 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003169 return rv;
3170}
3171
3172static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003173__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003174
NeilBrownd4dbd022006-06-26 00:27:59 -07003175
3176static ssize_t
3177layout_show(mddev_t *mddev, char *page)
3178{
3179 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003180 if (mddev->reshape_position != MaxSector &&
3181 mddev->layout != mddev->new_layout)
3182 return sprintf(page, "%d (%d)\n",
3183 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003184 return sprintf(page, "%d\n", mddev->layout);
3185}
3186
3187static ssize_t
3188layout_store(mddev_t *mddev, const char *buf, size_t len)
3189{
3190 char *e;
3191 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003192
3193 if (!*buf || (*e && *e != '\n'))
3194 return -EINVAL;
3195
NeilBrownb3546032009-03-31 14:56:41 +11003196 if (mddev->pers) {
3197 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003198 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003199 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003200 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003201 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003202 if (err) {
3203 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003204 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003205 }
NeilBrownb3546032009-03-31 14:56:41 +11003206 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003207 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003208 if (mddev->reshape_position == MaxSector)
3209 mddev->layout = n;
3210 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003211 return len;
3212}
3213static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003214__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003215
3216
NeilBrowneae17012005-11-08 21:39:23 -08003217static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003218raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003219{
NeilBrownbb636542005-11-08 21:39:45 -08003220 if (mddev->raid_disks == 0)
3221 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003222 if (mddev->reshape_position != MaxSector &&
3223 mddev->delta_disks != 0)
3224 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3225 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003226 return sprintf(page, "%d\n", mddev->raid_disks);
3227}
3228
NeilBrownda943b992006-01-06 00:20:54 -08003229static int update_raid_disks(mddev_t *mddev, int raid_disks);
3230
3231static ssize_t
3232raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3233{
NeilBrownda943b992006-01-06 00:20:54 -08003234 char *e;
3235 int rv = 0;
3236 unsigned long n = simple_strtoul(buf, &e, 10);
3237
3238 if (!*buf || (*e && *e != '\n'))
3239 return -EINVAL;
3240
3241 if (mddev->pers)
3242 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003243 else if (mddev->reshape_position != MaxSector) {
3244 int olddisks = mddev->raid_disks - mddev->delta_disks;
3245 mddev->delta_disks = n - olddisks;
3246 mddev->raid_disks = n;
3247 } else
NeilBrownda943b992006-01-06 00:20:54 -08003248 mddev->raid_disks = n;
3249 return rv ? rv : len;
3250}
3251static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003252__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003253
NeilBrown24dd4692005-11-08 21:39:26 -08003254static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003255chunk_size_show(mddev_t *mddev, char *page)
3256{
NeilBrown08a02ec2007-05-09 02:35:38 -07003257 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003258 mddev->chunk_sectors != mddev->new_chunk_sectors)
3259 return sprintf(page, "%d (%d)\n",
3260 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003261 mddev->chunk_sectors << 9);
3262 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003263}
3264
3265static ssize_t
3266chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3267{
NeilBrown3b343802006-01-06 00:20:47 -08003268 char *e;
3269 unsigned long n = simple_strtoul(buf, &e, 10);
3270
NeilBrown3b343802006-01-06 00:20:47 -08003271 if (!*buf || (*e && *e != '\n'))
3272 return -EINVAL;
3273
NeilBrownb3546032009-03-31 14:56:41 +11003274 if (mddev->pers) {
3275 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003276 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003277 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003278 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003279 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003280 if (err) {
3281 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003282 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003283 }
NeilBrownb3546032009-03-31 14:56:41 +11003284 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003285 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003286 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003287 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003288 }
NeilBrown3b343802006-01-06 00:20:47 -08003289 return len;
3290}
3291static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003292__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003293
NeilBrowna94213b2006-06-26 00:28:00 -07003294static ssize_t
3295resync_start_show(mddev_t *mddev, char *page)
3296{
NeilBrownd1a7c502009-03-31 15:24:32 +11003297 if (mddev->recovery_cp == MaxSector)
3298 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003299 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3300}
3301
3302static ssize_t
3303resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3304{
NeilBrowna94213b2006-06-26 00:28:00 -07003305 char *e;
3306 unsigned long long n = simple_strtoull(buf, &e, 10);
3307
3308 if (mddev->pers)
3309 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003310 if (cmd_match(buf, "none"))
3311 n = MaxSector;
3312 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003313 return -EINVAL;
3314
3315 mddev->recovery_cp = n;
3316 return len;
3317}
3318static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003319__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003320
NeilBrown9e653b62006-06-26 00:27:58 -07003321/*
3322 * The array state can be:
3323 *
3324 * clear
3325 * No devices, no size, no level
3326 * Equivalent to STOP_ARRAY ioctl
3327 * inactive
3328 * May have some settings, but array is not active
3329 * all IO results in error
3330 * When written, doesn't tear down array, but just stops it
3331 * suspended (not supported yet)
3332 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003333 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003334 * readonly
3335 * no resync can happen. no superblocks get written.
3336 * write requests fail
3337 * read-auto
3338 * like readonly, but behaves like 'clean' on a write request.
3339 *
3340 * clean - no pending writes, but otherwise active.
3341 * When written to inactive array, starts without resync
3342 * If a write request arrives then
3343 * if metadata is known, mark 'dirty' and switch to 'active'.
3344 * if not known, block and switch to write-pending
3345 * If written to an active array that has pending writes, then fails.
3346 * active
3347 * fully active: IO and resync can be happening.
3348 * When written to inactive array, starts with resync
3349 *
3350 * write-pending
3351 * clean, but writes are blocked waiting for 'active' to be written.
3352 *
3353 * active-idle
3354 * like active, but no writes have been seen for a while (100msec).
3355 *
3356 */
3357enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3358 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003359static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003360 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3361 "write-pending", "active-idle", NULL };
3362
3363static int match_word(const char *word, char **list)
3364{
3365 int n;
3366 for (n=0; list[n]; n++)
3367 if (cmd_match(word, list[n]))
3368 break;
3369 return n;
3370}
3371
3372static ssize_t
3373array_state_show(mddev_t *mddev, char *page)
3374{
3375 enum array_state st = inactive;
3376
3377 if (mddev->pers)
3378 switch(mddev->ro) {
3379 case 1:
3380 st = readonly;
3381 break;
3382 case 2:
3383 st = read_auto;
3384 break;
3385 case 0:
3386 if (mddev->in_sync)
3387 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003388 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003389 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003390 else if (mddev->safemode)
3391 st = active_idle;
3392 else
3393 st = active;
3394 }
3395 else {
3396 if (list_empty(&mddev->disks) &&
3397 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003398 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003399 st = clear;
3400 else
3401 st = inactive;
3402 }
3403 return sprintf(page, "%s\n", array_states[st]);
3404}
3405
Neil Browndf5b20c2008-07-11 22:02:22 +10003406static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003407static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003408static int do_md_run(mddev_t * mddev);
3409static int restart_array(mddev_t *mddev);
3410
3411static ssize_t
3412array_state_store(mddev_t *mddev, const char *buf, size_t len)
3413{
3414 int err = -EINVAL;
3415 enum array_state st = match_word(buf, array_states);
3416 switch(st) {
3417 case bad_word:
3418 break;
3419 case clear:
3420 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003421 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003422 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003423 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003424 break;
3425 case inactive:
3426 /* stopping an active array */
3427 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003428 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003429 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003430 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003431 } else
3432 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003433 break;
3434 case suspended:
3435 break; /* not supported yet */
3436 case readonly:
3437 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003438 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003439 else {
3440 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003441 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003442 err = do_md_run(mddev);
3443 }
3444 break;
3445 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003446 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003447 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003448 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003449 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003450 err = restart_array(mddev);
3451 if (err == 0) {
3452 mddev->ro = 2;
3453 set_disk_ro(mddev->gendisk, 0);
3454 }
NeilBrown9e653b62006-06-26 00:27:58 -07003455 } else {
3456 mddev->ro = 2;
3457 err = do_md_run(mddev);
3458 }
3459 break;
3460 case clean:
3461 if (mddev->pers) {
3462 restart_array(mddev);
3463 spin_lock_irq(&mddev->write_lock);
3464 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003465 if (mddev->in_sync == 0) {
3466 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003467 if (mddev->safemode == 1)
3468 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003469 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003470 }
3471 err = 0;
3472 } else
3473 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003474 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003475 } else
3476 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003477 break;
3478 case active:
3479 if (mddev->pers) {
3480 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003481 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003482 wake_up(&mddev->sb_wait);
3483 err = 0;
3484 } else {
3485 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003486 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003487 err = do_md_run(mddev);
3488 }
3489 break;
3490 case write_pending:
3491 case active_idle:
3492 /* these cannot be set */
3493 break;
3494 }
3495 if (err)
3496 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003497 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003498 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003499 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003500 }
NeilBrown9e653b62006-06-26 00:27:58 -07003501}
NeilBrown80ca3a42006-07-10 04:44:18 -07003502static struct md_sysfs_entry md_array_state =
3503__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003504
NeilBrown6d7ff7382006-01-06 00:21:16 -08003505static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003506max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3507 return sprintf(page, "%d\n",
3508 atomic_read(&mddev->max_corr_read_errors));
3509}
3510
3511static ssize_t
3512max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3513{
3514 char *e;
3515 unsigned long n = simple_strtoul(buf, &e, 10);
3516
3517 if (*buf && (*e == 0 || *e == '\n')) {
3518 atomic_set(&mddev->max_corr_read_errors, n);
3519 return len;
3520 }
3521 return -EINVAL;
3522}
3523
3524static struct md_sysfs_entry max_corr_read_errors =
3525__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3526 max_corrected_read_errors_store);
3527
3528static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003529null_show(mddev_t *mddev, char *page)
3530{
3531 return -EINVAL;
3532}
3533
3534static ssize_t
3535new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3536{
3537 /* buf must be %d:%d\n? giving major and minor numbers */
3538 /* The new device is added to the array.
3539 * If the array has a persistent superblock, we read the
3540 * superblock to initialise info and check validity.
3541 * Otherwise, only checking done is that in bind_rdev_to_array,
3542 * which mainly checks size.
3543 */
3544 char *e;
3545 int major = simple_strtoul(buf, &e, 10);
3546 int minor;
3547 dev_t dev;
3548 mdk_rdev_t *rdev;
3549 int err;
3550
3551 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3552 return -EINVAL;
3553 minor = simple_strtoul(e+1, &e, 10);
3554 if (*e && *e != '\n')
3555 return -EINVAL;
3556 dev = MKDEV(major, minor);
3557 if (major != MAJOR(dev) ||
3558 minor != MINOR(dev))
3559 return -EOVERFLOW;
3560
3561
3562 if (mddev->persistent) {
3563 rdev = md_import_device(dev, mddev->major_version,
3564 mddev->minor_version);
3565 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3566 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3567 mdk_rdev_t, same_set);
3568 err = super_types[mddev->major_version]
3569 .load_super(rdev, rdev0, mddev->minor_version);
3570 if (err < 0)
3571 goto out;
3572 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003573 } else if (mddev->external)
3574 rdev = md_import_device(dev, -2, -1);
3575 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003576 rdev = md_import_device(dev, -1, -1);
3577
3578 if (IS_ERR(rdev))
3579 return PTR_ERR(rdev);
3580 err = bind_rdev_to_array(rdev, mddev);
3581 out:
3582 if (err)
3583 export_rdev(rdev);
3584 return err ? err : len;
3585}
3586
3587static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003588__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003589
3590static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003591bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3592{
3593 char *end;
3594 unsigned long chunk, end_chunk;
3595
3596 if (!mddev->bitmap)
3597 goto out;
3598 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3599 while (*buf) {
3600 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3601 if (buf == end) break;
3602 if (*end == '-') { /* range */
3603 buf = end + 1;
3604 end_chunk = simple_strtoul(buf, &end, 0);
3605 if (buf == end) break;
3606 }
3607 if (*end && !isspace(*end)) break;
3608 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003609 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003610 }
3611 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3612out:
3613 return len;
3614}
3615
3616static struct md_sysfs_entry md_bitmap =
3617__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3618
3619static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003620size_show(mddev_t *mddev, char *page)
3621{
Andre Noll58c0fed2009-03-31 14:33:13 +11003622 return sprintf(page, "%llu\n",
3623 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003624}
3625
Andre Nolld71f9f82008-07-11 22:02:22 +10003626static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003627
3628static ssize_t
3629size_store(mddev_t *mddev, const char *buf, size_t len)
3630{
3631 /* If array is inactive, we can reduce the component size, but
3632 * not increase it (except from 0).
3633 * If array is active, we can try an on-line resize
3634 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003635 sector_t sectors;
3636 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003637
Andre Noll58c0fed2009-03-31 14:33:13 +11003638 if (err < 0)
3639 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003640 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003641 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003642 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003643 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003644 if (mddev->dev_sectors == 0 ||
3645 mddev->dev_sectors > sectors)
3646 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003647 else
3648 err = -ENOSPC;
3649 }
3650 return err ? err : len;
3651}
3652
3653static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003654__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003655
NeilBrown8bb93aa2006-01-06 00:20:50 -08003656
3657/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003658 * This is one of
3659 * 'none' for arrays with no metadata (good luck...)
3660 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003661 * or N.M for internally known formats
3662 */
3663static ssize_t
3664metadata_show(mddev_t *mddev, char *page)
3665{
3666 if (mddev->persistent)
3667 return sprintf(page, "%d.%d\n",
3668 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003669 else if (mddev->external)
3670 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003671 else
3672 return sprintf(page, "none\n");
3673}
3674
3675static ssize_t
3676metadata_store(mddev_t *mddev, const char *buf, size_t len)
3677{
3678 int major, minor;
3679 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003680 /* Changing the details of 'external' metadata is
3681 * always permitted. Otherwise there must be
3682 * no devices attached to the array.
3683 */
3684 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3685 ;
3686 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003687 return -EBUSY;
3688
3689 if (cmd_match(buf, "none")) {
3690 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003691 mddev->external = 0;
3692 mddev->major_version = 0;
3693 mddev->minor_version = 90;
3694 return len;
3695 }
3696 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003697 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003698 if (namelen >= sizeof(mddev->metadata_type))
3699 namelen = sizeof(mddev->metadata_type)-1;
3700 strncpy(mddev->metadata_type, buf+9, namelen);
3701 mddev->metadata_type[namelen] = 0;
3702 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3703 mddev->metadata_type[--namelen] = 0;
3704 mddev->persistent = 0;
3705 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003706 mddev->major_version = 0;
3707 mddev->minor_version = 90;
3708 return len;
3709 }
3710 major = simple_strtoul(buf, &e, 10);
3711 if (e==buf || *e != '.')
3712 return -EINVAL;
3713 buf = e+1;
3714 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003715 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003716 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003717 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003718 return -ENOENT;
3719 mddev->major_version = major;
3720 mddev->minor_version = minor;
3721 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003722 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003723 return len;
3724}
3725
3726static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003727__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003728
NeilBrowna35b0d62006-01-06 00:20:49 -08003729static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003730action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003731{
NeilBrown7eec3142005-11-08 21:39:44 -08003732 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003733 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3734 type = "frozen";
3735 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003736 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003737 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3738 type = "reshape";
3739 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003740 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3741 type = "resync";
3742 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3743 type = "check";
3744 else
3745 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003746 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003747 type = "recover";
3748 }
3749 return sprintf(page, "%s\n", type);
3750}
3751
NeilBrown7ebc0be2011-01-14 09:14:33 +11003752static void reap_sync_thread(mddev_t *mddev);
3753
NeilBrown24dd4692005-11-08 21:39:26 -08003754static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003755action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003756{
NeilBrown7eec3142005-11-08 21:39:44 -08003757 if (!mddev->pers || !mddev->pers->sync_request)
3758 return -EINVAL;
3759
NeilBrownb6a9ce62009-05-26 09:41:17 +10003760 if (cmd_match(page, "frozen"))
3761 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3762 else
3763 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3764
3765 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003766 if (mddev->sync_thread) {
3767 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003768 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003769 }
NeilBrown03c902e2006-01-06 00:20:46 -08003770 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3771 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003772 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003773 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003774 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003775 else if (cmd_match(page, "recover")) {
3776 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3778 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003779 int err;
3780 if (mddev->pers->start_reshape == NULL)
3781 return -EINVAL;
3782 err = mddev->pers->start_reshape(mddev);
3783 if (err)
3784 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003785 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003786 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003787 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003788 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003789 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003790 return -EINVAL;
3791 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3792 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003793 }
NeilBrown03c902e2006-01-06 00:20:46 -08003794 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003795 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003796 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003797 return len;
3798}
3799
NeilBrown9d888832005-11-08 21:39:26 -08003800static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003801mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003802{
3803 return sprintf(page, "%llu\n",
3804 (unsigned long long) mddev->resync_mismatches);
3805}
3806
NeilBrown80ca3a42006-07-10 04:44:18 -07003807static struct md_sysfs_entry md_scan_mode =
3808__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003809
NeilBrown96de1e62005-11-08 21:39:39 -08003810
NeilBrown80ca3a42006-07-10 04:44:18 -07003811static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003812
NeilBrown88202a02006-01-06 00:21:36 -08003813static ssize_t
3814sync_min_show(mddev_t *mddev, char *page)
3815{
3816 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3817 mddev->sync_speed_min ? "local": "system");
3818}
3819
3820static ssize_t
3821sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3822{
3823 int min;
3824 char *e;
3825 if (strncmp(buf, "system", 6)==0) {
3826 mddev->sync_speed_min = 0;
3827 return len;
3828 }
3829 min = simple_strtoul(buf, &e, 10);
3830 if (buf == e || (*e && *e != '\n') || min <= 0)
3831 return -EINVAL;
3832 mddev->sync_speed_min = min;
3833 return len;
3834}
3835
3836static struct md_sysfs_entry md_sync_min =
3837__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3838
3839static ssize_t
3840sync_max_show(mddev_t *mddev, char *page)
3841{
3842 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3843 mddev->sync_speed_max ? "local": "system");
3844}
3845
3846static ssize_t
3847sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3848{
3849 int max;
3850 char *e;
3851 if (strncmp(buf, "system", 6)==0) {
3852 mddev->sync_speed_max = 0;
3853 return len;
3854 }
3855 max = simple_strtoul(buf, &e, 10);
3856 if (buf == e || (*e && *e != '\n') || max <= 0)
3857 return -EINVAL;
3858 mddev->sync_speed_max = max;
3859 return len;
3860}
3861
3862static struct md_sysfs_entry md_sync_max =
3863__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3864
Iustin Popd7f3d292007-10-16 23:30:54 -07003865static ssize_t
3866degraded_show(mddev_t *mddev, char *page)
3867{
3868 return sprintf(page, "%d\n", mddev->degraded);
3869}
3870static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003871
3872static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003873sync_force_parallel_show(mddev_t *mddev, char *page)
3874{
3875 return sprintf(page, "%d\n", mddev->parallel_resync);
3876}
3877
3878static ssize_t
3879sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3880{
3881 long n;
3882
3883 if (strict_strtol(buf, 10, &n))
3884 return -EINVAL;
3885
3886 if (n != 0 && n != 1)
3887 return -EINVAL;
3888
3889 mddev->parallel_resync = n;
3890
3891 if (mddev->sync_thread)
3892 wake_up(&resync_wait);
3893
3894 return len;
3895}
3896
3897/* force parallel resync, even with shared block devices */
3898static struct md_sysfs_entry md_sync_force_parallel =
3899__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3900 sync_force_parallel_show, sync_force_parallel_store);
3901
3902static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003903sync_speed_show(mddev_t *mddev, char *page)
3904{
3905 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003906 if (mddev->curr_resync == 0)
3907 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003908 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3909 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003910 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003911 db = resync - mddev->resync_mark_cnt;
3912 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003913}
3914
NeilBrown80ca3a42006-07-10 04:44:18 -07003915static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003916
3917static ssize_t
3918sync_completed_show(mddev_t *mddev, char *page)
3919{
Andre Noll58c0fed2009-03-31 14:33:13 +11003920 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003921
NeilBrownacb180b2009-04-14 16:28:34 +10003922 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3923 return sprintf(page, "none\n");
3924
NeilBrown88202a02006-01-06 00:21:36 -08003925 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003926 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003927 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003928 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003929
NeilBrownacb180b2009-04-14 16:28:34 +10003930 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003931 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003932}
3933
NeilBrown80ca3a42006-07-10 04:44:18 -07003934static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003935
NeilBrowne464eaf2006-03-27 01:18:14 -08003936static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003937min_sync_show(mddev_t *mddev, char *page)
3938{
3939 return sprintf(page, "%llu\n",
3940 (unsigned long long)mddev->resync_min);
3941}
3942static ssize_t
3943min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3944{
3945 unsigned long long min;
3946 if (strict_strtoull(buf, 10, &min))
3947 return -EINVAL;
3948 if (min > mddev->resync_max)
3949 return -EINVAL;
3950 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3951 return -EBUSY;
3952
3953 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003954 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003955 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003956 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003957 return -EINVAL;
3958 }
3959 mddev->resync_min = min;
3960
3961 return len;
3962}
3963
3964static struct md_sysfs_entry md_min_sync =
3965__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3966
3967static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003968max_sync_show(mddev_t *mddev, char *page)
3969{
3970 if (mddev->resync_max == MaxSector)
3971 return sprintf(page, "max\n");
3972 else
3973 return sprintf(page, "%llu\n",
3974 (unsigned long long)mddev->resync_max);
3975}
3976static ssize_t
3977max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3978{
3979 if (strncmp(buf, "max", 3) == 0)
3980 mddev->resync_max = MaxSector;
3981 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003982 unsigned long long max;
3983 if (strict_strtoull(buf, 10, &max))
3984 return -EINVAL;
3985 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003986 return -EINVAL;
3987 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003988 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003989 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3990 return -EBUSY;
3991
3992 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003993 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003994 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003995 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003996 return -EINVAL;
3997 }
3998 mddev->resync_max = max;
3999 }
4000 wake_up(&mddev->recovery_wait);
4001 return len;
4002}
4003
4004static struct md_sysfs_entry md_max_sync =
4005__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4006
4007static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004008suspend_lo_show(mddev_t *mddev, char *page)
4009{
4010 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4011}
4012
4013static ssize_t
4014suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4015{
4016 char *e;
4017 unsigned long long new = simple_strtoull(buf, &e, 10);
4018
NeilBrownb8d966e2009-07-01 11:14:04 +10004019 if (mddev->pers == NULL ||
4020 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004021 return -EINVAL;
4022 if (buf == e || (*e && *e != '\n'))
4023 return -EINVAL;
4024 if (new >= mddev->suspend_hi ||
4025 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
4026 mddev->suspend_lo = new;
4027 mddev->pers->quiesce(mddev, 2);
4028 return len;
4029 } else
4030 return -EINVAL;
4031}
4032static struct md_sysfs_entry md_suspend_lo =
4033__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4034
4035
4036static ssize_t
4037suspend_hi_show(mddev_t *mddev, char *page)
4038{
4039 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4040}
4041
4042static ssize_t
4043suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4044{
4045 char *e;
4046 unsigned long long new = simple_strtoull(buf, &e, 10);
4047
NeilBrownb8d966e2009-07-01 11:14:04 +10004048 if (mddev->pers == NULL ||
4049 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004050 return -EINVAL;
4051 if (buf == e || (*e && *e != '\n'))
4052 return -EINVAL;
4053 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
4054 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
4055 mddev->suspend_hi = new;
4056 mddev->pers->quiesce(mddev, 1);
4057 mddev->pers->quiesce(mddev, 0);
4058 return len;
4059 } else
4060 return -EINVAL;
4061}
4062static struct md_sysfs_entry md_suspend_hi =
4063__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4064
NeilBrown08a02ec2007-05-09 02:35:38 -07004065static ssize_t
4066reshape_position_show(mddev_t *mddev, char *page)
4067{
4068 if (mddev->reshape_position != MaxSector)
4069 return sprintf(page, "%llu\n",
4070 (unsigned long long)mddev->reshape_position);
4071 strcpy(page, "none\n");
4072 return 5;
4073}
4074
4075static ssize_t
4076reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4077{
4078 char *e;
4079 unsigned long long new = simple_strtoull(buf, &e, 10);
4080 if (mddev->pers)
4081 return -EBUSY;
4082 if (buf == e || (*e && *e != '\n'))
4083 return -EINVAL;
4084 mddev->reshape_position = new;
4085 mddev->delta_disks = 0;
4086 mddev->new_level = mddev->level;
4087 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004088 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004089 return len;
4090}
4091
4092static struct md_sysfs_entry md_reshape_position =
4093__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4094 reshape_position_store);
4095
Dan Williamsb522adc2009-03-31 15:00:31 +11004096static ssize_t
4097array_size_show(mddev_t *mddev, char *page)
4098{
4099 if (mddev->external_size)
4100 return sprintf(page, "%llu\n",
4101 (unsigned long long)mddev->array_sectors/2);
4102 else
4103 return sprintf(page, "default\n");
4104}
4105
4106static ssize_t
4107array_size_store(mddev_t *mddev, const char *buf, size_t len)
4108{
4109 sector_t sectors;
4110
4111 if (strncmp(buf, "default", 7) == 0) {
4112 if (mddev->pers)
4113 sectors = mddev->pers->size(mddev, 0, 0);
4114 else
4115 sectors = mddev->array_sectors;
4116
4117 mddev->external_size = 0;
4118 } else {
4119 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4120 return -EINVAL;
4121 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004122 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004123
4124 mddev->external_size = 1;
4125 }
4126
4127 mddev->array_sectors = sectors;
4128 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004129 if (mddev->pers)
4130 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004131
4132 return len;
4133}
4134
4135static struct md_sysfs_entry md_array_size =
4136__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4137 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004138
NeilBrowneae17012005-11-08 21:39:23 -08004139static struct attribute *md_default_attrs[] = {
4140 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004141 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004142 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004143 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004144 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004145 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004146 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004147 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004148 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004149 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004150 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004151 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004152 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004153 NULL,
4154};
4155
4156static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004157 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004158 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004159 &md_sync_min.attr,
4160 &md_sync_max.attr,
4161 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004162 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004163 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004164 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004165 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004166 &md_suspend_lo.attr,
4167 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004168 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004169 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004170 NULL,
4171};
NeilBrown411036f2005-11-08 21:39:40 -08004172static struct attribute_group md_redundancy_group = {
4173 .name = NULL,
4174 .attrs = md_redundancy_attrs,
4175};
4176
NeilBrowneae17012005-11-08 21:39:23 -08004177
4178static ssize_t
4179md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4180{
4181 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4182 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004183 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004184
4185 if (!entry->show)
4186 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004187 rv = mddev_lock(mddev);
4188 if (!rv) {
4189 rv = entry->show(mddev, page);
4190 mddev_unlock(mddev);
4191 }
NeilBrown96de1e62005-11-08 21:39:39 -08004192 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004193}
4194
4195static ssize_t
4196md_attr_store(struct kobject *kobj, struct attribute *attr,
4197 const char *page, size_t length)
4198{
4199 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4200 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004201 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004202
4203 if (!entry->store)
4204 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004205 if (!capable(CAP_SYS_ADMIN))
4206 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004207 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004208 if (mddev->hold_active == UNTIL_IOCTL)
4209 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004210 if (!rv) {
4211 rv = entry->store(mddev, page, length);
4212 mddev_unlock(mddev);
4213 }
NeilBrown96de1e62005-11-08 21:39:39 -08004214 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004215}
4216
4217static void md_free(struct kobject *ko)
4218{
4219 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004220
4221 if (mddev->sysfs_state)
4222 sysfs_put(mddev->sysfs_state);
4223
4224 if (mddev->gendisk) {
4225 del_gendisk(mddev->gendisk);
4226 put_disk(mddev->gendisk);
4227 }
4228 if (mddev->queue)
4229 blk_cleanup_queue(mddev->queue);
4230
NeilBrowneae17012005-11-08 21:39:23 -08004231 kfree(mddev);
4232}
4233
Emese Revfy52cf25d2010-01-19 02:58:23 +01004234static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004235 .show = md_attr_show,
4236 .store = md_attr_store,
4237};
4238static struct kobj_type md_ktype = {
4239 .release = md_free,
4240 .sysfs_ops = &md_sysfs_ops,
4241 .default_attrs = md_default_attrs,
4242};
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244int mdp_major = 0;
4245
Dan Williams5fd3a172009-03-04 00:57:25 -07004246static void mddev_delayed_delete(struct work_struct *ws)
4247{
4248 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4249
NeilBrown43a70502009-12-14 12:49:55 +11004250 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004251 kobject_del(&mddev->kobj);
4252 kobject_put(&mddev->kobj);
4253}
4254
NeilBrownefeb53c2009-01-09 08:31:10 +11004255static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004257 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 mddev_t *mddev = mddev_find(dev);
4259 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004260 int partitioned;
4261 int shift;
4262 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004263 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004266 return -ENODEV;
4267
4268 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4269 shift = partitioned ? MdpMinorShift : 0;
4270 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Tejun Heoe804ac72010-10-15 15:36:08 +02004272 /* wait for any previous instance of this device to be
4273 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004274 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004275 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004276
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004277 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004278 error = -EEXIST;
4279 if (mddev->gendisk)
4280 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004281
4282 if (name) {
4283 /* Need to ensure that 'name' is not a duplicate.
4284 */
4285 mddev_t *mddev2;
4286 spin_lock(&all_mddevs_lock);
4287
4288 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4289 if (mddev2->gendisk &&
4290 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4291 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004292 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004293 }
4294 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
NeilBrown8b765392009-01-09 08:31:08 +11004296
NeilBrown0909dc42009-07-01 12:27:21 +10004297 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004298 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004299 if (!mddev->queue)
4300 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004301 mddev->queue->queuedata = mddev;
4302
NeilBrown409c57f2009-03-31 14:39:39 +11004303 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 disk = alloc_disk(1 << shift);
4306 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004307 blk_cleanup_queue(mddev->queue);
4308 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004309 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004311 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004313 if (name)
4314 strcpy(disk->disk_name, name);
4315 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004317 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 disk->fops = &md_fops;
4320 disk->private_data = mddev;
4321 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004322 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004323 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004324 * remove it now.
4325 */
4326 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 add_disk(disk);
4328 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004329 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4330 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004331 if (error) {
4332 /* This isn't possible, but as kobject_init_and_add is marked
4333 * __must_check, we must do something with the result
4334 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004335 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4336 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004337 error = 0;
4338 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004339 if (mddev->kobj.sd &&
4340 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004341 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004342
4343 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004344 abort:
4345 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004346 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004347 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004348 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004349 }
NeilBrownd3374822009-01-09 08:31:10 +11004350 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004351 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004352}
4353
4354static struct kobject *md_probe(dev_t dev, int *part, void *data)
4355{
4356 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return NULL;
4358}
4359
NeilBrownefeb53c2009-01-09 08:31:10 +11004360static int add_named_array(const char *val, struct kernel_param *kp)
4361{
4362 /* val must be "md_*" where * is not all digits.
4363 * We allocate an array with a large free minor number, and
4364 * set the name to val. val must not already be an active name.
4365 */
4366 int len = strlen(val);
4367 char buf[DISK_NAME_LEN];
4368
4369 while (len && val[len-1] == '\n')
4370 len--;
4371 if (len >= DISK_NAME_LEN)
4372 return -E2BIG;
4373 strlcpy(buf, val, len+1);
4374 if (strncmp(buf, "md_", 3) != 0)
4375 return -EINVAL;
4376 return md_alloc(0, buf);
4377}
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379static void md_safemode_timeout(unsigned long data)
4380{
4381 mddev_t *mddev = (mddev_t *) data;
4382
Neil Brown0fd62b82008-06-28 08:31:36 +10004383 if (!atomic_read(&mddev->writes_pending)) {
4384 mddev->safemode = 1;
4385 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004386 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 md_wakeup_thread(mddev->thread);
4389}
4390
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004391static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
NeilBrown390ee602010-06-01 19:37:27 +10004393int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
NeilBrown2604b702006-01-06 00:20:36 -08004395 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004397 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
NeilBrowna757e642005-04-16 15:26:42 -07004399 if (list_empty(&mddev->disks))
4400 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 if (mddev->pers)
4404 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004405 /* Cannot run until previous stop completes properly */
4406 if (mddev->sysfs_active)
4407 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 /*
4410 * Analyze all RAID superblock(s)
4411 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004412 if (!mddev->raid_disks) {
4413 if (!mddev->persistent)
4414 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004415 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
NeilBrownd9d166c2006-01-06 00:20:51 -08004418 if (mddev->level != LEVEL_NONE)
4419 request_module("md-level-%d", mddev->level);
4420 else if (mddev->clevel[0])
4421 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /*
4424 * Drop all container device buffers, from now on
4425 * the only valid external interface is through the md
4426 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004428 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004429 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 continue;
4431 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004432 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004433
4434 /* perform some consistency tests on the device.
4435 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004436 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004437 */
Andre Noll0f420352008-07-11 22:02:23 +10004438 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004439 if (mddev->dev_sectors &&
4440 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004441 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004442 printk("md: %s: data overlaps metadata\n",
4443 mdname(mddev));
4444 return -EINVAL;
4445 }
4446 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004447 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004448 > rdev->data_offset) {
4449 printk("md: %s: metadata overlaps data\n",
4450 mdname(mddev));
4451 return -EINVAL;
4452 }
4453 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004454 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456
NeilBrowna167f662010-10-26 18:31:13 +11004457 if (mddev->bio_set == NULL)
4458 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004461 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004462 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004464 if (mddev->level != LEVEL_NONE)
4465 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4466 mddev->level);
4467 else
4468 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4469 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 return -EINVAL;
4471 }
NeilBrown2604b702006-01-06 00:20:36 -08004472 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004474 if (mddev->level != pers->level) {
4475 mddev->level = pers->level;
4476 mddev->new_level = pers->level;
4477 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004478 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
NeilBrownf6705572006-03-27 01:18:11 -08004480 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004481 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004482 /* This personality cannot handle reshaping... */
4483 mddev->pers = NULL;
4484 module_put(pers->owner);
4485 return -EINVAL;
4486 }
4487
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004488 if (pers->sync_request) {
4489 /* Warn if this is a potentially silly
4490 * configuration.
4491 */
4492 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4493 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004494 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004495
4496 list_for_each_entry(rdev, &mddev->disks, same_set)
4497 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004498 if (rdev < rdev2 &&
4499 rdev->bdev->bd_contains ==
4500 rdev2->bdev->bd_contains) {
4501 printk(KERN_WARNING
4502 "%s: WARNING: %s appears to be"
4503 " on the same physical disk as"
4504 " %s.\n",
4505 mdname(mddev),
4506 bdevname(rdev->bdev,b),
4507 bdevname(rdev2->bdev,b2));
4508 warned = 1;
4509 }
4510 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004511
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004512 if (warned)
4513 printk(KERN_WARNING
4514 "True protection against single-disk"
4515 " failure might be compromised.\n");
4516 }
4517
NeilBrown657390d2005-08-26 18:34:16 -07004518 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004519 /* may be over-ridden by personality */
4520 mddev->resync_max_sectors = mddev->dev_sectors;
4521
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004522 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
NeilBrown0f9552b52009-12-30 12:08:50 +11004524 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004525 mddev->ro = 2; /* read-only, but switch on first write */
4526
NeilBrownb15c2e52006-01-06 00:20:16 -08004527 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004528 if (err)
4529 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004530 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4531 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4532 " but 'external_size' not in effect?\n", __func__);
4533 printk(KERN_ERR
4534 "md: invalid array_size %llu > default size %llu\n",
4535 (unsigned long long)mddev->array_sectors / 2,
4536 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4537 err = -EINVAL;
4538 mddev->pers->stop(mddev);
4539 }
4540 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004541 err = bitmap_create(mddev);
4542 if (err) {
4543 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4544 mdname(mddev), err);
4545 mddev->pers->stop(mddev);
4546 }
4547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 module_put(mddev->pers->owner);
4550 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004551 bitmap_destroy(mddev);
4552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004554 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004555 if (mddev->kobj.sd &&
4556 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004557 printk(KERN_WARNING
4558 "md: cannot register extra attributes for %s\n",
4559 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004560 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004561 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004562 mddev->ro = 0;
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004565 atomic_set(&mddev->max_corr_read_errors,
4566 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 mddev->safemode = 0;
4568 mddev->safemode_timer.function = md_safemode_timeout;
4569 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004570 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004572 smp_wmb();
4573 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004574 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004575 if (rdev->raid_disk >= 0) {
4576 char nm[20];
4577 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004578 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004579 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4583
NeilBrown850b2b42006-10-03 01:15:46 -07004584 if (mddev->flags)
4585 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
NeilBrown0b8c9de2006-07-10 04:44:15 -07004587 md_wakeup_thread(mddev->thread);
4588 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004589
NeilBrownd7603b72006-01-06 00:20:30 -08004590 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004591 sysfs_notify_dirent_safe(mddev->sysfs_state);
4592 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004593 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return 0;
4595}
NeilBrown390ee602010-06-01 19:37:27 +10004596EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
NeilBrownfe60b012010-03-29 11:10:42 +11004598static int do_md_run(mddev_t *mddev)
4599{
4600 int err;
4601
4602 err = md_run(mddev);
4603 if (err)
4604 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004605 err = bitmap_load(mddev);
4606 if (err) {
4607 bitmap_destroy(mddev);
4608 goto out;
4609 }
NeilBrownfe60b012010-03-29 11:10:42 +11004610 set_capacity(mddev->gendisk, mddev->array_sectors);
4611 revalidate_disk(mddev->gendisk);
4612 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4613out:
4614 return err;
4615}
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617static int restart_array(mddev_t *mddev)
4618{
4619 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Andre Noll80fab1d2008-07-11 22:02:21 +10004621 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004623 return -ENXIO;
4624 if (!mddev->pers)
4625 return -EINVAL;
4626 if (!mddev->ro)
4627 return -EBUSY;
4628 mddev->safemode = 0;
4629 mddev->ro = 0;
4630 set_disk_ro(disk, 0);
4631 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4632 mdname(mddev));
4633 /* Kick recovery or resync if necessary */
4634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4635 md_wakeup_thread(mddev->thread);
4636 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004637 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
NeilBrownacc55e22006-06-26 00:27:47 -07004641/* similar to deny_write_access, but accounts for our holding a reference
4642 * to the file ourselves */
4643static int deny_bitmap_write_access(struct file * file)
4644{
4645 struct inode *inode = file->f_mapping->host;
4646
4647 spin_lock(&inode->i_lock);
4648 if (atomic_read(&inode->i_writecount) > 1) {
4649 spin_unlock(&inode->i_lock);
4650 return -ETXTBSY;
4651 }
4652 atomic_set(&inode->i_writecount, -1);
4653 spin_unlock(&inode->i_lock);
4654
4655 return 0;
4656}
4657
NeilBrown43a70502009-12-14 12:49:55 +11004658void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004659{
4660 struct inode *inode = file->f_mapping->host;
4661
4662 spin_lock(&inode->i_lock);
4663 atomic_set(&inode->i_writecount, 1);
4664 spin_unlock(&inode->i_lock);
4665}
4666
NeilBrown6177b472010-03-29 11:37:13 +11004667static void md_clean(mddev_t *mddev)
4668{
4669 mddev->array_sectors = 0;
4670 mddev->external_size = 0;
4671 mddev->dev_sectors = 0;
4672 mddev->raid_disks = 0;
4673 mddev->recovery_cp = 0;
4674 mddev->resync_min = 0;
4675 mddev->resync_max = MaxSector;
4676 mddev->reshape_position = MaxSector;
4677 mddev->external = 0;
4678 mddev->persistent = 0;
4679 mddev->level = LEVEL_NONE;
4680 mddev->clevel[0] = 0;
4681 mddev->flags = 0;
4682 mddev->ro = 0;
4683 mddev->metadata_type[0] = 0;
4684 mddev->chunk_sectors = 0;
4685 mddev->ctime = mddev->utime = 0;
4686 mddev->layout = 0;
4687 mddev->max_disks = 0;
4688 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004689 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004690 mddev->delta_disks = 0;
4691 mddev->new_level = LEVEL_NONE;
4692 mddev->new_layout = 0;
4693 mddev->new_chunk_sectors = 0;
4694 mddev->curr_resync = 0;
4695 mddev->resync_mismatches = 0;
4696 mddev->suspend_lo = mddev->suspend_hi = 0;
4697 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4698 mddev->recovery = 0;
4699 mddev->in_sync = 0;
4700 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004701 mddev->safemode = 0;
4702 mddev->bitmap_info.offset = 0;
4703 mddev->bitmap_info.default_offset = 0;
4704 mddev->bitmap_info.chunksize = 0;
4705 mddev->bitmap_info.daemon_sleep = 0;
4706 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004707 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004708}
4709
NeilBrowndefad612011-01-14 09:14:33 +11004710static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004711{
4712 if (mddev->sync_thread) {
4713 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4714 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004715 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004716 }
4717
4718 del_timer_sync(&mddev->safemode_timer);
4719
4720 bitmap_flush(mddev);
4721 md_super_wait(mddev);
4722
4723 if (!mddev->in_sync || mddev->flags) {
4724 /* mark array as shutdown cleanly */
4725 mddev->in_sync = 1;
4726 md_update_sb(mddev, 1);
4727 }
4728}
NeilBrowndefad612011-01-14 09:14:33 +11004729
4730void md_stop_writes(mddev_t *mddev)
4731{
4732 mddev_lock(mddev);
4733 __md_stop_writes(mddev);
4734 mddev_unlock(mddev);
4735}
NeilBrown390ee602010-06-01 19:37:27 +10004736EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004737
NeilBrown390ee602010-06-01 19:37:27 +10004738void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004739{
NeilBrown0ca69882011-01-14 09:14:33 +11004740 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004741 mddev->pers->stop(mddev);
4742 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4743 mddev->to_remove = &md_redundancy_group;
4744 module_put(mddev->pers->owner);
4745 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004746 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004747}
NeilBrown390ee602010-06-01 19:37:27 +10004748EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004749
NeilBrowna4bd82d2010-03-29 13:23:10 +11004750static int md_set_readonly(mddev_t *mddev, int is_open)
4751{
4752 int err = 0;
4753 mutex_lock(&mddev->open_mutex);
4754 if (atomic_read(&mddev->openers) > is_open) {
4755 printk("md: %s still in use.\n",mdname(mddev));
4756 err = -EBUSY;
4757 goto out;
4758 }
4759 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004760 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004761
4762 err = -ENXIO;
4763 if (mddev->ro==1)
4764 goto out;
4765 mddev->ro = 1;
4766 set_disk_ro(mddev->gendisk, 1);
4767 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004768 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004769 err = 0;
4770 }
4771out:
4772 mutex_unlock(&mddev->open_mutex);
4773 return err;
4774}
4775
NeilBrown9e653b62006-06-26 00:27:58 -07004776/* mode:
4777 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004778 * 2 - stop but do not disassemble array
4779 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004780static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004783 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
NeilBrownc8c00a62009-08-10 12:50:52 +10004785 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004786 if (atomic_read(&mddev->openers) > is_open ||
4787 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004788 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004789 mutex_unlock(&mddev->open_mutex);
4790 return -EBUSY;
4791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
NeilBrown6e17b022010-08-07 21:41:19 +10004793 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004794 if (mddev->ro)
4795 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004796
NeilBrowndefad612011-01-14 09:14:33 +11004797 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004798 md_stop(mddev);
4799 mddev->queue->merge_bvec_fn = NULL;
4800 mddev->queue->unplug_fn = NULL;
4801 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004802
NeilBrowna4bd82d2010-03-29 13:23:10 +11004803 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004804 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004805
NeilBrowna4bd82d2010-03-29 13:23:10 +11004806 list_for_each_entry(rdev, &mddev->disks, same_set)
4807 if (rdev->raid_disk >= 0) {
4808 char nm[20];
4809 sprintf(nm, "rd%d", rdev->raid_disk);
4810 sysfs_remove_link(&mddev->kobj, nm);
4811 }
NeilBrownc4647292009-05-07 12:51:06 +10004812
NeilBrowna4bd82d2010-03-29 13:23:10 +11004813 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004814 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004815 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004816
NeilBrowna4bd82d2010-03-29 13:23:10 +11004817 if (mddev->ro)
4818 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004819 } else
4820 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 /*
4822 * Free resources if final stop
4823 */
NeilBrown9e653b62006-06-26 00:27:58 -07004824 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4826
NeilBrown978f9462006-02-02 14:28:05 -08004827 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004828 if (mddev->bitmap_info.file) {
4829 restore_bitmap_write_access(mddev->bitmap_info.file);
4830 fput(mddev->bitmap_info.file);
4831 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004832 }
NeilBrownc3d97142009-12-14 12:49:52 +11004833 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 export_array(mddev);
4836
NeilBrown6177b472010-03-29 11:37:13 +11004837 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004838 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004839 if (mddev->hold_active == UNTIL_STOP)
4840 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004841 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004842 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004843 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004844 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004848#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849static void autorun_array(mddev_t *mddev)
4850{
4851 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 int err;
4853
NeilBrowna757e642005-04-16 15:26:42 -07004854 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 printk(KERN_INFO "md: running: ");
4858
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004859 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 char b[BDEVNAME_SIZE];
4861 printk("<%s>", bdevname(rdev->bdev,b));
4862 }
4863 printk("\n");
4864
NeilBrownd710e132008-10-13 11:55:12 +11004865 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (err) {
4867 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004868 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 }
4870}
4871
4872/*
4873 * lets try to run arrays based on all disks that have arrived
4874 * until now. (those are in pending_raid_disks)
4875 *
4876 * the method: pick the first pending disk, collect all disks with
4877 * the same UUID, remove all from the pending list and put them into
4878 * the 'same_array' list. Then order this list based on superblock
4879 * update time (freshest comes first), kick out 'old' disks and
4880 * compare superblocks. If everything's fine then run it.
4881 *
4882 * If "unit" is allocated, then bump its reference count
4883 */
4884static void autorun_devices(int part)
4885{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004886 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 mddev_t *mddev;
4888 char b[BDEVNAME_SIZE];
4889
4890 printk(KERN_INFO "md: autorun ...\n");
4891 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004892 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004894 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 rdev0 = list_entry(pending_raid_disks.next,
4896 mdk_rdev_t, same_set);
4897
4898 printk(KERN_INFO "md: considering %s ...\n",
4899 bdevname(rdev0->bdev,b));
4900 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004901 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 if (super_90_load(rdev, rdev0, 0) >= 0) {
4903 printk(KERN_INFO "md: adding %s ...\n",
4904 bdevname(rdev->bdev,b));
4905 list_move(&rdev->same_set, &candidates);
4906 }
4907 /*
4908 * now we have a set of devices, with all of them having
4909 * mostly sane superblocks. It's time to allocate the
4910 * mddev.
4911 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004912 if (part) {
4913 dev = MKDEV(mdp_major,
4914 rdev0->preferred_minor << MdpMinorShift);
4915 unit = MINOR(dev) >> MdpMinorShift;
4916 } else {
4917 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4918 unit = MINOR(dev);
4919 }
4920 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4922 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4923 break;
4924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
4926 md_probe(dev, NULL, NULL);
4927 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004928 if (!mddev || !mddev->gendisk) {
4929 if (mddev)
4930 mddev_put(mddev);
4931 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 "md: cannot allocate memory for md drive.\n");
4933 break;
4934 }
4935 if (mddev_lock(mddev))
4936 printk(KERN_WARNING "md: %s locked, cannot run\n",
4937 mdname(mddev));
4938 else if (mddev->raid_disks || mddev->major_version
4939 || !list_empty(&mddev->disks)) {
4940 printk(KERN_WARNING
4941 "md: %s already running, cannot run %s\n",
4942 mdname(mddev), bdevname(rdev0->bdev,b));
4943 mddev_unlock(mddev);
4944 } else {
4945 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004946 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004947 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 list_del_init(&rdev->same_set);
4949 if (bind_rdev_to_array(rdev, mddev))
4950 export_rdev(rdev);
4951 }
4952 autorun_array(mddev);
4953 mddev_unlock(mddev);
4954 }
4955 /* on success, candidates will be empty, on error
4956 * it won't...
4957 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004958 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004959 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 mddev_put(mddev);
4963 }
4964 printk(KERN_INFO "md: ... autorun DONE.\n");
4965}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004966#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968static int get_version(void __user * arg)
4969{
4970 mdu_version_t ver;
4971
4972 ver.major = MD_MAJOR_VERSION;
4973 ver.minor = MD_MINOR_VERSION;
4974 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4975
4976 if (copy_to_user(arg, &ver, sizeof(ver)))
4977 return -EFAULT;
4978
4979 return 0;
4980}
4981
4982static int get_array_info(mddev_t * mddev, void __user * arg)
4983{
4984 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004985 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
NeilBrowna9f326e2009-09-23 18:06:41 +10004988 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004989 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004991 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 failed++;
4993 else {
4994 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004995 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004996 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 else
4998 spare++;
4999 }
5000 }
5001
5002 info.major_version = mddev->major_version;
5003 info.minor_version = mddev->minor_version;
5004 info.patch_version = MD_PATCHLEVEL_VERSION;
5005 info.ctime = mddev->ctime;
5006 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005007 info.size = mddev->dev_sectors / 2;
5008 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005009 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 info.nr_disks = nr;
5011 info.raid_disks = mddev->raid_disks;
5012 info.md_minor = mddev->md_minor;
5013 info.not_persistent= !mddev->persistent;
5014
5015 info.utime = mddev->utime;
5016 info.state = 0;
5017 if (mddev->in_sync)
5018 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005019 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005020 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005021 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 info.working_disks = working;
5023 info.failed_disks = failed;
5024 info.spare_disks = spare;
5025
5026 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005027 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 if (copy_to_user(arg, &info, sizeof(info)))
5030 return -EFAULT;
5031
5032 return 0;
5033}
5034
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005035static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005036{
5037 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5038 char *ptr, *buf = NULL;
5039 int err = -ENOMEM;
5040
Dan Williamsb5470dc2008-06-27 21:44:04 -07005041 if (md_allow_write(mddev))
5042 file = kmalloc(sizeof(*file), GFP_NOIO);
5043 else
5044 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005045
NeilBrown32a76272005-06-21 17:17:14 -07005046 if (!file)
5047 goto out;
5048
5049 /* bitmap disabled, zero the first byte and copy out */
5050 if (!mddev->bitmap || !mddev->bitmap->file) {
5051 file->pathname[0] = '\0';
5052 goto copy_out;
5053 }
5054
5055 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5056 if (!buf)
5057 goto out;
5058
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005059 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5060 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005061 goto out;
5062
5063 strcpy(file->pathname, ptr);
5064
5065copy_out:
5066 err = 0;
5067 if (copy_to_user(arg, file, sizeof(*file)))
5068 err = -EFAULT;
5069out:
5070 kfree(buf);
5071 kfree(file);
5072 return err;
5073}
5074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075static int get_disk_info(mddev_t * mddev, void __user * arg)
5076{
5077 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 mdk_rdev_t *rdev;
5079
5080 if (copy_from_user(&info, arg, sizeof(info)))
5081 return -EFAULT;
5082
Andre Noll26ef3792008-07-11 22:02:21 +10005083 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 if (rdev) {
5085 info.major = MAJOR(rdev->bdev->bd_dev);
5086 info.minor = MINOR(rdev->bdev->bd_dev);
5087 info.raid_disk = rdev->raid_disk;
5088 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005089 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005091 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 info.state |= (1<<MD_DISK_ACTIVE);
5093 info.state |= (1<<MD_DISK_SYNC);
5094 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005095 if (test_bit(WriteMostly, &rdev->flags))
5096 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 } else {
5098 info.major = info.minor = 0;
5099 info.raid_disk = -1;
5100 info.state = (1<<MD_DISK_REMOVED);
5101 }
5102
5103 if (copy_to_user(arg, &info, sizeof(info)))
5104 return -EFAULT;
5105
5106 return 0;
5107}
5108
5109static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5110{
5111 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5112 mdk_rdev_t *rdev;
5113 dev_t dev = MKDEV(info->major,info->minor);
5114
5115 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5116 return -EOVERFLOW;
5117
5118 if (!mddev->raid_disks) {
5119 int err;
5120 /* expecting a device which has a superblock */
5121 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5122 if (IS_ERR(rdev)) {
5123 printk(KERN_WARNING
5124 "md: md_import_device returned %ld\n",
5125 PTR_ERR(rdev));
5126 return PTR_ERR(rdev);
5127 }
5128 if (!list_empty(&mddev->disks)) {
5129 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5130 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005131 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 .load_super(rdev, rdev0, mddev->minor_version);
5133 if (err < 0) {
5134 printk(KERN_WARNING
5135 "md: %s has different UUID to %s\n",
5136 bdevname(rdev->bdev,b),
5137 bdevname(rdev0->bdev,b2));
5138 export_rdev(rdev);
5139 return -EINVAL;
5140 }
5141 }
5142 err = bind_rdev_to_array(rdev, mddev);
5143 if (err)
5144 export_rdev(rdev);
5145 return err;
5146 }
5147
5148 /*
5149 * add_new_disk can be used once the array is assembled
5150 * to add "hot spares". They must already have a superblock
5151 * written
5152 */
5153 if (mddev->pers) {
5154 int err;
5155 if (!mddev->pers->hot_add_disk) {
5156 printk(KERN_WARNING
5157 "%s: personality does not support diskops!\n",
5158 mdname(mddev));
5159 return -EINVAL;
5160 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005161 if (mddev->persistent)
5162 rdev = md_import_device(dev, mddev->major_version,
5163 mddev->minor_version);
5164 else
5165 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (IS_ERR(rdev)) {
5167 printk(KERN_WARNING
5168 "md: md_import_device returned %ld\n",
5169 PTR_ERR(rdev));
5170 return PTR_ERR(rdev);
5171 }
NeilBrown1a855a02010-12-09 16:36:28 +11005172 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005173 if (!mddev->persistent) {
5174 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005175 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005176 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005177 set_bit(In_sync, &rdev->flags);
5178 } else
NeilBrown41158c72005-06-21 17:17:25 -07005179 rdev->raid_disk = -1;
5180 } else
5181 super_types[mddev->major_version].
5182 validate_super(mddev, rdev);
NeilBrown1a855a02010-12-09 16:36:28 +11005183 if (test_bit(In_sync, &rdev->flags))
5184 rdev->saved_raid_disk = rdev->raid_disk;
5185 else
5186 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005187
NeilBrownb2d444d2005-11-08 21:39:31 -08005188 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005189 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5190 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005191 else
5192 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 rdev->raid_disk = -1;
5195 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005196 if (!err && !mddev->pers->hot_remove_disk) {
5197 /* If there is hot_add_disk but no hot_remove_disk
5198 * then added disks for geometry changes,
5199 * and should be added immediately.
5200 */
5201 super_types[mddev->major_version].
5202 validate_super(mddev, rdev);
5203 err = mddev->pers->hot_add_disk(mddev, rdev);
5204 if (err)
5205 unbind_rdev_from_array(rdev);
5206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 if (err)
5208 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005209 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005210 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005211
NeilBrown17571282006-12-10 02:20:52 -08005212 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005213 if (mddev->degraded)
5214 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005215 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005216 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 return err;
5218 }
5219
5220 /* otherwise, add_new_disk is only allowed
5221 * for major_version==0 superblocks
5222 */
5223 if (mddev->major_version != 0) {
5224 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5225 mdname(mddev));
5226 return -EINVAL;
5227 }
5228
5229 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5230 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005231 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (IS_ERR(rdev)) {
5233 printk(KERN_WARNING
5234 "md: error, md_import_device() returned %ld\n",
5235 PTR_ERR(rdev));
5236 return PTR_ERR(rdev);
5237 }
5238 rdev->desc_nr = info->number;
5239 if (info->raid_disk < mddev->raid_disks)
5240 rdev->raid_disk = info->raid_disk;
5241 else
5242 rdev->raid_disk = -1;
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005245 if (info->state & (1<<MD_DISK_SYNC))
5246 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005248 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5249 set_bit(WriteMostly, &rdev->flags);
5250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 if (!mddev->persistent) {
5252 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005253 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5254 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005255 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005256 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
NeilBrown2bf071b2006-01-06 00:20:55 -08005258 err = bind_rdev_to_array(rdev, mddev);
5259 if (err) {
5260 export_rdev(rdev);
5261 return err;
5262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264
5265 return 0;
5266}
5267
5268static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5269{
5270 char b[BDEVNAME_SIZE];
5271 mdk_rdev_t *rdev;
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 rdev = find_rdev(mddev, dev);
5274 if (!rdev)
5275 return -ENXIO;
5276
5277 if (rdev->raid_disk >= 0)
5278 goto busy;
5279
5280 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005281 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005282 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
5284 return 0;
5285busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005286 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 bdevname(rdev->bdev,b), mdname(mddev));
5288 return -EBUSY;
5289}
5290
5291static int hot_add_disk(mddev_t * mddev, dev_t dev)
5292{
5293 char b[BDEVNAME_SIZE];
5294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 mdk_rdev_t *rdev;
5296
5297 if (!mddev->pers)
5298 return -ENODEV;
5299
5300 if (mddev->major_version != 0) {
5301 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5302 " version-0 superblocks.\n",
5303 mdname(mddev));
5304 return -EINVAL;
5305 }
5306 if (!mddev->pers->hot_add_disk) {
5307 printk(KERN_WARNING
5308 "%s: personality does not support diskops!\n",
5309 mdname(mddev));
5310 return -EINVAL;
5311 }
5312
NeilBrownd710e132008-10-13 11:55:12 +11005313 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 if (IS_ERR(rdev)) {
5315 printk(KERN_WARNING
5316 "md: error, md_import_device() returned %ld\n",
5317 PTR_ERR(rdev));
5318 return -EINVAL;
5319 }
5320
5321 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005322 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005324 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
NeilBrown8190e752009-06-18 08:48:58 +10005326 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
NeilBrownb2d444d2005-11-08 21:39:31 -08005328 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 printk(KERN_WARNING
5330 "md: can not hot-add faulty %s disk to %s!\n",
5331 bdevname(rdev->bdev,b), mdname(mddev));
5332 err = -EINVAL;
5333 goto abort_export;
5334 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005335 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005337 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005338 err = bind_rdev_to_array(rdev, mddev);
5339 if (err)
5340 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 /*
5343 * The rest should better be atomic, we can have disk failures
5344 * noticed in interrupt contexts ...
5345 */
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 rdev->raid_disk = -1;
5348
NeilBrown850b2b42006-10-03 01:15:46 -07005349 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 /*
5352 * Kick recovery, maybe this spare has to be added to the
5353 * array immediately.
5354 */
5355 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5356 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005357 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 return 0;
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360abort_export:
5361 export_rdev(rdev);
5362 return err;
5363}
5364
NeilBrown32a76272005-06-21 17:17:14 -07005365static int set_bitmap_file(mddev_t *mddev, int fd)
5366{
5367 int err;
5368
NeilBrown36fa3062005-09-09 16:23:45 -07005369 if (mddev->pers) {
5370 if (!mddev->pers->quiesce)
5371 return -EBUSY;
5372 if (mddev->recovery || mddev->sync_thread)
5373 return -EBUSY;
5374 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005375 }
5376
NeilBrown36fa3062005-09-09 16:23:45 -07005377
5378 if (fd >= 0) {
5379 if (mddev->bitmap)
5380 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005381 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005382
NeilBrownc3d97142009-12-14 12:49:52 +11005383 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005384 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5385 mdname(mddev));
5386 return -EBADF;
5387 }
5388
NeilBrownc3d97142009-12-14 12:49:52 +11005389 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005390 if (err) {
5391 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5392 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005393 fput(mddev->bitmap_info.file);
5394 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005395 return err;
5396 }
NeilBrownc3d97142009-12-14 12:49:52 +11005397 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005398 } else if (mddev->bitmap == NULL)
5399 return -ENOENT; /* cannot remove what isn't there */
5400 err = 0;
5401 if (mddev->pers) {
5402 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005403 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005404 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005405 if (!err)
5406 err = bitmap_load(mddev);
5407 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005408 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005409 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005410 fd = -1; /* make sure to put the file */
5411 }
NeilBrown36fa3062005-09-09 16:23:45 -07005412 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005413 }
5414 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005415 if (mddev->bitmap_info.file) {
5416 restore_bitmap_write_access(mddev->bitmap_info.file);
5417 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005418 }
NeilBrownc3d97142009-12-14 12:49:52 +11005419 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005420 }
5421
NeilBrown32a76272005-06-21 17:17:14 -07005422 return err;
5423}
5424
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425/*
5426 * set_array_info is used two different ways
5427 * The original usage is when creating a new array.
5428 * In this usage, raid_disks is > 0 and it together with
5429 * level, size, not_persistent,layout,chunksize determine the
5430 * shape of the array.
5431 * This will always create an array with a type-0.90.0 superblock.
5432 * The newer usage is when assembling an array.
5433 * In this case raid_disks will be 0, and the major_version field is
5434 * use to determine which style super-blocks are to be found on the devices.
5435 * The minor and patch _version numbers are also kept incase the
5436 * super_block handler wishes to interpret them.
5437 */
5438static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5439{
5440
5441 if (info->raid_disks == 0) {
5442 /* just setting version number for superblock loading */
5443 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005444 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 super_types[info->major_version].name == NULL) {
5446 /* maybe try to auto-load a module? */
5447 printk(KERN_INFO
5448 "md: superblock version %d not known\n",
5449 info->major_version);
5450 return -EINVAL;
5451 }
5452 mddev->major_version = info->major_version;
5453 mddev->minor_version = info->minor_version;
5454 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005455 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005456 /* ensure mddev_put doesn't delete this now that there
5457 * is some minimal configuration.
5458 */
5459 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return 0;
5461 }
5462 mddev->major_version = MD_MAJOR_VERSION;
5463 mddev->minor_version = MD_MINOR_VERSION;
5464 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5465 mddev->ctime = get_seconds();
5466
5467 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005468 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005469 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 mddev->raid_disks = info->raid_disks;
5471 /* don't set md_minor, it is determined by which /dev/md* was
5472 * openned
5473 */
5474 if (info->state & (1<<MD_SB_CLEAN))
5475 mddev->recovery_cp = MaxSector;
5476 else
5477 mddev->recovery_cp = 0;
5478 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005479 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005482 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 mddev->max_disks = MD_SB_DISKS;
5485
NeilBrowne6910632008-02-06 01:39:51 -08005486 if (mddev->persistent)
5487 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005488 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
NeilBrownc3d97142009-12-14 12:49:52 +11005490 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5491 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005492
NeilBrownf6705572006-03-27 01:18:11 -08005493 mddev->reshape_position = MaxSector;
5494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 /*
5496 * Generate a 128 bit UUID
5497 */
5498 get_random_bytes(mddev->uuid, 16);
5499
NeilBrownf6705572006-03-27 01:18:11 -08005500 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005501 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005502 mddev->new_layout = mddev->layout;
5503 mddev->delta_disks = 0;
5504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 return 0;
5506}
5507
Dan Williams1f403622009-03-31 14:59:03 +11005508void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5509{
Dan Williamsb522adc2009-03-31 15:00:31 +11005510 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5511
5512 if (mddev->external_size)
5513 return;
5514
Dan Williams1f403622009-03-31 14:59:03 +11005515 mddev->array_sectors = array_sectors;
5516}
5517EXPORT_SYMBOL(md_set_array_sectors);
5518
Andre Nolld71f9f82008-07-11 22:02:22 +10005519static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005520{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005521 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005522 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005523 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005524
5525 if (mddev->pers->resize == NULL)
5526 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005527 /* The "num_sectors" is the number of sectors of each device that
5528 * is used. This can only make sense for arrays with redundancy.
5529 * linear and raid0 always use whatever space is available. We can only
5530 * consider changing this number if no resync or reconstruction is
5531 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005532 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005533 * of each device. If num_sectors is zero, we find the largest size
5534 * that fits.
5535
NeilBrowna35b0d62006-01-06 00:20:49 -08005536 */
5537 if (mddev->sync_thread)
5538 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005539 if (mddev->bitmap)
5540 /* Sorry, cannot grow a bitmap yet, just remove it,
5541 * grow, and re-add.
5542 */
5543 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005544 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005545 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005546
Andre Nolld71f9f82008-07-11 22:02:22 +10005547 if (fit && (num_sectors == 0 || num_sectors > avail))
5548 num_sectors = avail;
5549 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005550 return -ENOSPC;
5551 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005552 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005553 if (!rv)
5554 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005555 return rv;
5556}
5557
NeilBrownda943b992006-01-06 00:20:54 -08005558static int update_raid_disks(mddev_t *mddev, int raid_disks)
5559{
5560 int rv;
5561 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005562 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005563 return -EINVAL;
5564 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005565 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005566 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005567 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005568 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005569 mddev->delta_disks = raid_disks - mddev->raid_disks;
5570
5571 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005572 return rv;
5573}
5574
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576/*
5577 * update_array_info is used to change the configuration of an
5578 * on-line array.
5579 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5580 * fields in the info are checked against the array.
5581 * Any differences that cannot be handled will cause an error.
5582 * Normally, only one change can be managed at a time.
5583 */
5584static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5585{
5586 int rv = 0;
5587 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005588 int state = 0;
5589
5590 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005591 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005592 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
5594 if (mddev->major_version != info->major_version ||
5595 mddev->minor_version != info->minor_version ||
5596/* mddev->patch_version != info->patch_version || */
5597 mddev->ctime != info->ctime ||
5598 mddev->level != info->level ||
5599/* mddev->layout != info->layout || */
5600 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005601 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005602 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5603 ((state^info->state) & 0xfffffe00)
5604 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 return -EINVAL;
5606 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005607 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5608 cnt++;
5609 if (mddev->raid_disks != info->raid_disks)
5610 cnt++;
5611 if (mddev->layout != info->layout)
5612 cnt++;
5613 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5614 cnt++;
5615 if (cnt == 0)
5616 return 0;
5617 if (cnt > 1)
5618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 if (mddev->layout != info->layout) {
5621 /* Change layout
5622 * we don't need to do anything at the md level, the
5623 * personality will take care of it all.
5624 */
NeilBrown50ac1682009-06-18 08:47:55 +10005625 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005627 else {
5628 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005629 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005630 if (rv)
5631 mddev->new_layout = mddev->layout;
5632 return rv;
5633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005635 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005636 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
NeilBrownda943b992006-01-06 00:20:54 -08005638 if (mddev->raid_disks != info->raid_disks)
5639 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
NeilBrown36fa3062005-09-09 16:23:45 -07005641 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5642 if (mddev->pers->quiesce == NULL)
5643 return -EINVAL;
5644 if (mddev->recovery || mddev->sync_thread)
5645 return -EBUSY;
5646 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5647 /* add the bitmap */
5648 if (mddev->bitmap)
5649 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005650 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005651 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005652 mddev->bitmap_info.offset =
5653 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005654 mddev->pers->quiesce(mddev, 1);
5655 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005656 if (!rv)
5657 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005658 if (rv)
5659 bitmap_destroy(mddev);
5660 mddev->pers->quiesce(mddev, 0);
5661 } else {
5662 /* remove the bitmap */
5663 if (!mddev->bitmap)
5664 return -ENOENT;
5665 if (mddev->bitmap->file)
5666 return -EINVAL;
5667 mddev->pers->quiesce(mddev, 1);
5668 bitmap_destroy(mddev);
5669 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005670 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005671 }
5672 }
NeilBrown850b2b42006-10-03 01:15:46 -07005673 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 return rv;
5675}
5676
5677static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5678{
5679 mdk_rdev_t *rdev;
5680
5681 if (mddev->pers == NULL)
5682 return -ENODEV;
5683
5684 rdev = find_rdev(mddev, dev);
5685 if (!rdev)
5686 return -ENODEV;
5687
5688 md_error(mddev, rdev);
5689 return 0;
5690}
5691
Andre Noll2f9618c2008-04-25 18:57:58 +02005692/*
5693 * We have a problem here : there is no easy way to give a CHS
5694 * virtual geometry. We currently pretend that we have a 2 heads
5695 * 4 sectors (with a BIG number of cylinders...). This drives
5696 * dosfs just mad... ;-)
5697 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005698static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5699{
5700 mddev_t *mddev = bdev->bd_disk->private_data;
5701
5702 geo->heads = 2;
5703 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005704 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005705 return 0;
5706}
5707
Al Viroa39907f2008-03-02 10:31:15 -05005708static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 unsigned int cmd, unsigned long arg)
5710{
5711 int err = 0;
5712 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005714 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 if (!capable(CAP_SYS_ADMIN))
5717 return -EACCES;
5718
5719 /*
5720 * Commands dealing with the RAID driver but not any
5721 * particular array:
5722 */
5723 switch (cmd)
5724 {
5725 case RAID_VERSION:
5726 err = get_version(argp);
5727 goto done;
5728
5729 case PRINT_RAID_DEBUG:
5730 err = 0;
5731 md_print_devices();
5732 goto done;
5733
5734#ifndef MODULE
5735 case RAID_AUTORUN:
5736 err = 0;
5737 autostart_arrays(arg);
5738 goto done;
5739#endif
5740 default:;
5741 }
5742
5743 /*
5744 * Commands creating/starting a new array:
5745 */
5746
Al Viroa39907f2008-03-02 10:31:15 -05005747 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
5749 if (!mddev) {
5750 BUG();
5751 goto abort;
5752 }
5753
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 err = mddev_lock(mddev);
5755 if (err) {
5756 printk(KERN_INFO
5757 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5758 err, cmd);
5759 goto abort;
5760 }
5761
5762 switch (cmd)
5763 {
5764 case SET_ARRAY_INFO:
5765 {
5766 mdu_array_info_t info;
5767 if (!arg)
5768 memset(&info, 0, sizeof(info));
5769 else if (copy_from_user(&info, argp, sizeof(info))) {
5770 err = -EFAULT;
5771 goto abort_unlock;
5772 }
5773 if (mddev->pers) {
5774 err = update_array_info(mddev, &info);
5775 if (err) {
5776 printk(KERN_WARNING "md: couldn't update"
5777 " array info. %d\n", err);
5778 goto abort_unlock;
5779 }
5780 goto done_unlock;
5781 }
5782 if (!list_empty(&mddev->disks)) {
5783 printk(KERN_WARNING
5784 "md: array %s already has disks!\n",
5785 mdname(mddev));
5786 err = -EBUSY;
5787 goto abort_unlock;
5788 }
5789 if (mddev->raid_disks) {
5790 printk(KERN_WARNING
5791 "md: array %s already initialised!\n",
5792 mdname(mddev));
5793 err = -EBUSY;
5794 goto abort_unlock;
5795 }
5796 err = set_array_info(mddev, &info);
5797 if (err) {
5798 printk(KERN_WARNING "md: couldn't set"
5799 " array info. %d\n", err);
5800 goto abort_unlock;
5801 }
5802 }
5803 goto done_unlock;
5804
5805 default:;
5806 }
5807
5808 /*
5809 * Commands querying/configuring an existing array:
5810 */
NeilBrown32a76272005-06-21 17:17:14 -07005811 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005812 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005813 if ((!mddev->raid_disks && !mddev->external)
5814 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5815 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5816 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 err = -ENODEV;
5818 goto abort_unlock;
5819 }
5820
5821 /*
5822 * Commands even a read-only array can execute:
5823 */
5824 switch (cmd)
5825 {
5826 case GET_ARRAY_INFO:
5827 err = get_array_info(mddev, argp);
5828 goto done_unlock;
5829
NeilBrown32a76272005-06-21 17:17:14 -07005830 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005831 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005832 goto done_unlock;
5833
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 case GET_DISK_INFO:
5835 err = get_disk_info(mddev, argp);
5836 goto done_unlock;
5837
5838 case RESTART_ARRAY_RW:
5839 err = restart_array(mddev);
5840 goto done_unlock;
5841
5842 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005843 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 goto done_unlock;
5845
5846 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005847 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 goto done_unlock;
5849
Dan Williamse2218352010-05-12 08:25:37 +10005850 case BLKROSET:
5851 if (get_user(ro, (int __user *)(arg))) {
5852 err = -EFAULT;
5853 goto done_unlock;
5854 }
5855 err = -EINVAL;
5856
5857 /* if the bdev is going readonly the value of mddev->ro
5858 * does not matter, no writes are coming
5859 */
5860 if (ro)
5861 goto done_unlock;
5862
5863 /* are we are already prepared for writes? */
5864 if (mddev->ro != 1)
5865 goto done_unlock;
5866
5867 /* transitioning to readauto need only happen for
5868 * arrays that call md_write_start
5869 */
5870 if (mddev->pers) {
5871 err = restart_array(mddev);
5872 if (err == 0) {
5873 mddev->ro = 2;
5874 set_disk_ro(mddev->gendisk, 0);
5875 }
5876 }
5877 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
5879
5880 /*
5881 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005882 * superblock, so we do not allow them on read-only arrays.
5883 * However non-MD ioctls (e.g. get-size) will still come through
5884 * here and hit the 'default' below, so only disallow
5885 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005887 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005888 if (mddev->ro == 2) {
5889 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005890 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005891 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5892 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005893 } else {
5894 err = -EROFS;
5895 goto abort_unlock;
5896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 }
5898
5899 switch (cmd)
5900 {
5901 case ADD_NEW_DISK:
5902 {
5903 mdu_disk_info_t info;
5904 if (copy_from_user(&info, argp, sizeof(info)))
5905 err = -EFAULT;
5906 else
5907 err = add_new_disk(mddev, &info);
5908 goto done_unlock;
5909 }
5910
5911 case HOT_REMOVE_DISK:
5912 err = hot_remove_disk(mddev, new_decode_dev(arg));
5913 goto done_unlock;
5914
5915 case HOT_ADD_DISK:
5916 err = hot_add_disk(mddev, new_decode_dev(arg));
5917 goto done_unlock;
5918
5919 case SET_DISK_FAULTY:
5920 err = set_disk_faulty(mddev, new_decode_dev(arg));
5921 goto done_unlock;
5922
5923 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005924 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 goto done_unlock;
5926
NeilBrown32a76272005-06-21 17:17:14 -07005927 case SET_BITMAP_FILE:
5928 err = set_bitmap_file(mddev, (int)arg);
5929 goto done_unlock;
5930
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 err = -EINVAL;
5933 goto abort_unlock;
5934 }
5935
5936done_unlock:
5937abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005938 if (mddev->hold_active == UNTIL_IOCTL &&
5939 err != -EINVAL)
5940 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 mddev_unlock(mddev);
5942
5943 return err;
5944done:
5945 if (err)
5946 MD_BUG();
5947abort:
5948 return err;
5949}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005950#ifdef CONFIG_COMPAT
5951static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5952 unsigned int cmd, unsigned long arg)
5953{
5954 switch (cmd) {
5955 case HOT_REMOVE_DISK:
5956 case HOT_ADD_DISK:
5957 case SET_DISK_FAULTY:
5958 case SET_BITMAP_FILE:
5959 /* These take in integer arg, do not convert */
5960 break;
5961 default:
5962 arg = (unsigned long)compat_ptr(arg);
5963 break;
5964 }
5965
5966 return md_ioctl(bdev, mode, cmd, arg);
5967}
5968#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Al Viroa39907f2008-03-02 10:31:15 -05005970static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971{
5972 /*
5973 * Succeed if we can lock the mddev, which confirms that
5974 * it isn't being stopped right now.
5975 */
NeilBrownd3374822009-01-09 08:31:10 +11005976 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 int err;
5978
NeilBrownd3374822009-01-09 08:31:10 +11005979 if (mddev->gendisk != bdev->bd_disk) {
5980 /* we are racing with mddev_put which is discarding this
5981 * bd_disk.
5982 */
5983 mddev_put(mddev);
5984 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02005985 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005986 /* Then retry the open from the top */
5987 return -ERESTARTSYS;
5988 }
5989 BUG_ON(mddev != bdev->bd_disk->private_data);
5990
NeilBrownc8c00a62009-08-10 12:50:52 +10005991 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 goto out;
5993
5994 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005995 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005996 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
NeilBrownf3b99be2010-06-24 13:31:03 +10005998 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 out:
6000 return err;
6001}
6002
Al Viroa39907f2008-03-02 10:31:15 -05006003static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004{
Al Viroa39907f2008-03-02 10:31:15 -05006005 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006007 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006008 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 mddev_put(mddev);
6010
6011 return 0;
6012}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006013static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
6015 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006016 .open = md_open,
6017 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006018 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006019#ifdef CONFIG_COMPAT
6020 .compat_ioctl = md_compat_ioctl,
6021#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006022 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023};
6024
Adrian Bunk75c96f82005-05-05 16:16:09 -07006025static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026{
6027 mdk_thread_t *thread = arg;
6028
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 /*
6030 * md_thread is a 'system-thread', it's priority should be very
6031 * high. We avoid resource deadlocks individually in each
6032 * raid personality. (RAID5 does preallocation) We also use RR and
6033 * the very same RT priority as kswapd, thus we will never get
6034 * into a priority inversion deadlock.
6035 *
6036 * we definitely have to have equal or higher priority than
6037 * bdflush, otherwise bdflush will deadlock if there are too
6038 * many dirty RAID5 blocks.
6039 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
NeilBrown6985c432005-10-19 21:23:47 -07006041 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006042 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
NeilBrown93588e22005-11-15 00:09:12 -08006044 /* We need to wait INTERRUPTIBLE so that
6045 * we don't add to the load-average.
6046 * That means we need to be sure no signals are
6047 * pending
6048 */
6049 if (signal_pending(current))
6050 flush_signals(current);
6051
6052 wait_event_interruptible_timeout
6053 (thread->wqueue,
6054 test_bit(THREAD_WAKEUP, &thread->flags)
6055 || kthread_should_stop(),
6056 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
NeilBrown6c987912011-01-14 09:13:53 +11006058 clear_bit(THREAD_WAKEUP, &thread->flags);
6059 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006060 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 return 0;
6064}
6065
6066void md_wakeup_thread(mdk_thread_t *thread)
6067{
6068 if (thread) {
6069 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6070 set_bit(THREAD_WAKEUP, &thread->flags);
6071 wake_up(&thread->wqueue);
6072 }
6073}
6074
6075mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6076 const char *name)
6077{
6078 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
NeilBrown9ffae0c2006-01-06 00:20:32 -08006080 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 if (!thread)
6082 return NULL;
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 init_waitqueue_head(&thread->wqueue);
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 thread->run = run;
6087 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006088 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006089 thread->tsk = kthread_run(md_thread, thread,
6090 "%s_%s",
6091 mdname(thread->mddev),
6092 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006093 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 kfree(thread);
6095 return NULL;
6096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return thread;
6098}
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100void md_unregister_thread(mdk_thread_t *thread)
6101{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006102 if (!thread)
6103 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006104 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006105
6106 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 kfree(thread);
6108}
6109
6110void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6111{
6112 if (!mddev) {
6113 MD_BUG();
6114 return;
6115 }
6116
NeilBrownb2d444d2005-11-08 21:39:31 -08006117 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006119
6120 if (mddev->external)
6121 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6124 mdname(mddev),
6125 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6126 __builtin_return_address(0),__builtin_return_address(1),
6127 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006128*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006129 if (!mddev->pers)
6130 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 if (!mddev->pers->error_handler)
6132 return;
6133 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006134 if (mddev->degraded)
6135 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006136 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6138 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6139 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006140 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006141 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006142 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143}
6144
6145/* seq_file implementation /proc/mdstat */
6146
6147static void status_unused(struct seq_file *seq)
6148{
6149 int i = 0;
6150 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
6152 seq_printf(seq, "unused devices: ");
6153
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006154 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 char b[BDEVNAME_SIZE];
6156 i++;
6157 seq_printf(seq, "%s ",
6158 bdevname(rdev->bdev,b));
6159 }
6160 if (!i)
6161 seq_printf(seq, "<none>");
6162
6163 seq_printf(seq, "\n");
6164}
6165
6166
6167static void status_resync(struct seq_file *seq, mddev_t * mddev)
6168{
NeilBrowndd71cf62009-05-07 12:49:35 +10006169 sector_t max_sectors, resync, res;
6170 unsigned long dt, db;
6171 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006172 int scale;
6173 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
NeilBrowndd71cf62009-05-07 12:49:35 +10006175 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006178 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006180 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
6182 /*
6183 * Should not happen.
6184 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006185 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 MD_BUG();
6187 return;
6188 }
NeilBrown4588b422006-03-27 01:18:04 -08006189 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006190 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006191 * u32, as those are the requirements for sector_div.
6192 * Thus 'scale' must be at least 10
6193 */
6194 scale = 10;
6195 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006196 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006197 scale++;
6198 }
6199 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006200 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006201
6202 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 {
NeilBrown4588b422006-03-27 01:18:04 -08006204 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 seq_printf(seq, "[");
6206 for (i = 0; i < x; i++)
6207 seq_printf(seq, "=");
6208 seq_printf(seq, ">");
6209 for (i = 0; i < y; i++)
6210 seq_printf(seq, ".");
6211 seq_printf(seq, "] ");
6212 }
NeilBrown4588b422006-03-27 01:18:04 -08006213 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006214 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6215 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006216 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6217 "check" :
6218 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6219 "resync" : "recovery"))),
6220 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006221 (unsigned long long) resync/2,
6222 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 * dt: time from mark until now
6226 * db: blocks written from mark until now
6227 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006228 *
6229 * rt is a sector_t, so could be 32bit or 64bit.
6230 * So we divide before multiply in case it is 32bit and close
6231 * to the limit.
6232 * We scale the divisor (db) by 32 to avoid loosing precision
6233 * near the end of resync when the number of remaining sectors
6234 * is close to 'db'.
6235 * We then divide rt by 32 after multiplying by db to compensate.
6236 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 */
6238 dt = ((jiffies - mddev->resync_mark) / HZ);
6239 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006240 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6241 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
NeilBrowndd71cf62009-05-07 12:49:35 +10006243 rt = max_sectors - resync; /* number of remaining sectors */
6244 sector_div(rt, db/32+1);
6245 rt *= dt;
6246 rt >>= 5;
6247
6248 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6249 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
NeilBrownff4e8d92006-07-10 04:44:16 -07006251 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252}
6253
6254static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6255{
6256 struct list_head *tmp;
6257 loff_t l = *pos;
6258 mddev_t *mddev;
6259
6260 if (l >= 0x10000)
6261 return NULL;
6262 if (!l--)
6263 /* header */
6264 return (void*)1;
6265
6266 spin_lock(&all_mddevs_lock);
6267 list_for_each(tmp,&all_mddevs)
6268 if (!l--) {
6269 mddev = list_entry(tmp, mddev_t, all_mddevs);
6270 mddev_get(mddev);
6271 spin_unlock(&all_mddevs_lock);
6272 return mddev;
6273 }
6274 spin_unlock(&all_mddevs_lock);
6275 if (!l--)
6276 return (void*)2;/* tail */
6277 return NULL;
6278}
6279
6280static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6281{
6282 struct list_head *tmp;
6283 mddev_t *next_mddev, *mddev = v;
6284
6285 ++*pos;
6286 if (v == (void*)2)
6287 return NULL;
6288
6289 spin_lock(&all_mddevs_lock);
6290 if (v == (void*)1)
6291 tmp = all_mddevs.next;
6292 else
6293 tmp = mddev->all_mddevs.next;
6294 if (tmp != &all_mddevs)
6295 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6296 else {
6297 next_mddev = (void*)2;
6298 *pos = 0x10000;
6299 }
6300 spin_unlock(&all_mddevs_lock);
6301
6302 if (v != (void*)1)
6303 mddev_put(mddev);
6304 return next_mddev;
6305
6306}
6307
6308static void md_seq_stop(struct seq_file *seq, void *v)
6309{
6310 mddev_t *mddev = v;
6311
6312 if (mddev && v != (void*)1 && v != (void*)2)
6313 mddev_put(mddev);
6314}
6315
NeilBrownd7603b72006-01-06 00:20:30 -08006316struct mdstat_info {
6317 int event;
6318};
6319
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320static int md_seq_show(struct seq_file *seq, void *v)
6321{
6322 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006323 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006325 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006326 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
6328 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006329 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 seq_printf(seq, "Personalities : ");
6331 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006332 list_for_each_entry(pers, &pers_list, list)
6333 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
6335 spin_unlock(&pers_lock);
6336 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006337 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return 0;
6339 }
6340 if (v == (void*)2) {
6341 status_unused(seq);
6342 return 0;
6343 }
6344
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006345 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6349 seq_printf(seq, "%s : %sactive", mdname(mddev),
6350 mddev->pers ? "" : "in");
6351 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006352 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006354 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006355 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 seq_printf(seq, " %s", mddev->pers->name);
6357 }
6358
Andre Nolldd8ac332009-03-31 14:33:13 +11006359 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006360 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 char b[BDEVNAME_SIZE];
6362 seq_printf(seq, " %s[%d]",
6363 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006364 if (test_bit(WriteMostly, &rdev->flags))
6365 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006366 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 seq_printf(seq, "(F)");
6368 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006369 } else if (rdev->raid_disk < 0)
6370 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006371 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 }
6373
6374 if (!list_empty(&mddev->disks)) {
6375 if (mddev->pers)
6376 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006377 (unsigned long long)
6378 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 else
6380 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006381 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006383 if (mddev->persistent) {
6384 if (mddev->major_version != 0 ||
6385 mddev->minor_version != 90) {
6386 seq_printf(seq," super %d.%d",
6387 mddev->major_version,
6388 mddev->minor_version);
6389 }
NeilBrowne6910632008-02-06 01:39:51 -08006390 } else if (mddev->external)
6391 seq_printf(seq, " super external:%s",
6392 mddev->metadata_type);
6393 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006394 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006397 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006399 if (mddev->pers->sync_request) {
6400 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006401 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006402 seq_printf(seq, "\n ");
6403 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6404 seq_printf(seq, "\tresync=DELAYED\n ");
6405 else if (mddev->recovery_cp < MaxSector)
6406 seq_printf(seq, "\tresync=PENDING\n ");
6407 }
NeilBrown32a76272005-06-21 17:17:14 -07006408 } else
6409 seq_printf(seq, "\n ");
6410
6411 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006412 unsigned long chunk_kb;
6413 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006414 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006415 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006416 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6417 "%lu%s chunk",
6418 bitmap->pages - bitmap->missing_pages,
6419 bitmap->pages,
6420 (bitmap->pages - bitmap->missing_pages)
6421 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006422 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006423 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006424 if (bitmap->file) {
6425 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006426 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006427 }
NeilBrown78d742d2005-06-21 17:17:15 -07006428
NeilBrown32a76272005-06-21 17:17:14 -07006429 seq_printf(seq, "\n");
6430 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 }
6432
6433 seq_printf(seq, "\n");
6434 }
6435 mddev_unlock(mddev);
6436
6437 return 0;
6438}
6439
Jan Engelhardt110518b2009-05-07 12:49:37 +10006440static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 .start = md_seq_start,
6442 .next = md_seq_next,
6443 .stop = md_seq_stop,
6444 .show = md_seq_show,
6445};
6446
6447static int md_seq_open(struct inode *inode, struct file *file)
6448{
6449 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006450 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6451 if (mi == NULL)
6452 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
6454 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006455 if (error)
6456 kfree(mi);
6457 else {
6458 struct seq_file *p = file->private_data;
6459 p->private = mi;
6460 mi->event = atomic_read(&md_event_count);
6461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 return error;
6463}
6464
NeilBrownd7603b72006-01-06 00:20:30 -08006465static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6466{
6467 struct seq_file *m = filp->private_data;
6468 struct mdstat_info *mi = m->private;
6469 int mask;
6470
6471 poll_wait(filp, &md_event_waiters, wait);
6472
6473 /* always allow read */
6474 mask = POLLIN | POLLRDNORM;
6475
6476 if (mi->event != atomic_read(&md_event_count))
6477 mask |= POLLERR | POLLPRI;
6478 return mask;
6479}
6480
Arjan van de Venfa027c22007-02-12 00:55:33 -08006481static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006482 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 .open = md_seq_open,
6484 .read = seq_read,
6485 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006486 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006487 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488};
6489
NeilBrown2604b702006-01-06 00:20:36 -08006490int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006493 list_add_tail(&p->list, &pers_list);
6494 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 spin_unlock(&pers_lock);
6496 return 0;
6497}
6498
NeilBrown2604b702006-01-06 00:20:36 -08006499int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500{
NeilBrown2604b702006-01-06 00:20:36 -08006501 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006503 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 spin_unlock(&pers_lock);
6505 return 0;
6506}
6507
NeilBrowneea1bf32009-03-31 14:27:02 +11006508static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509{
6510 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006512 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513
6514 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006515 rcu_read_lock();
6516 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006518 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6519 (int)part_stat_read(&disk->part0, sectors[1]) -
6520 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006521 /* sync IO will cause sync_io to increase before the disk_stats
6522 * as sync_io is counted when a request starts, and
6523 * disk_stats is counted when it completes.
6524 * So resync activity will cause curr_events to be smaller than
6525 * when there was no such activity.
6526 * non-sync IO will cause disk_stat to increase without
6527 * increasing sync_io so curr_events will (eventually)
6528 * be larger than it was before. Once it becomes
6529 * substantially larger, the test below will cause
6530 * the array to appear non-idle, and resync will slow
6531 * down.
6532 * If there is a lot of outstanding resync activity when
6533 * we set last_event to curr_events, then all that activity
6534 * completing might cause the array to appear non-idle
6535 * and resync will be slowed down even though there might
6536 * not have been non-resync activity. This will only
6537 * happen once though. 'last_events' will soon reflect
6538 * the state where there is little or no outstanding
6539 * resync requests, and further resync activity will
6540 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006541 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006543 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 rdev->last_events = curr_events;
6545 idle = 0;
6546 }
6547 }
NeilBrown4b809912008-07-21 17:05:25 +10006548 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 return idle;
6550}
6551
6552void md_done_sync(mddev_t *mddev, int blocks, int ok)
6553{
6554 /* another "blocks" (512byte) blocks have been synced */
6555 atomic_sub(blocks, &mddev->recovery_active);
6556 wake_up(&mddev->recovery_wait);
6557 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006558 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 md_wakeup_thread(mddev->thread);
6560 // stop recovery, signal do_sync ....
6561 }
6562}
6563
6564
NeilBrown06d91a52005-06-21 17:17:12 -07006565/* md_write_start(mddev, bi)
6566 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006567 * in superblock) before writing, schedule a superblock update
6568 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006569 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006570void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571{
Neil Brown0fd62b82008-06-28 08:31:36 +10006572 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006573 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006574 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006575
NeilBrownf91de922005-11-08 21:39:36 -08006576 BUG_ON(mddev->ro == 1);
6577 if (mddev->ro == 2) {
6578 /* need to switch to read/write */
6579 mddev->ro = 0;
6580 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6581 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006582 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006583 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006584 }
NeilBrown06d91a52005-06-21 17:17:12 -07006585 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006586 if (mddev->safemode == 1)
6587 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006588 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006589 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006590 if (mddev->in_sync) {
6591 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006592 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006593 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006594 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006595 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006596 }
NeilBrowna9701a32005-11-08 21:39:34 -08006597 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006598 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006599 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006600 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006601 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006602 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603}
6604
6605void md_write_end(mddev_t *mddev)
6606{
6607 if (atomic_dec_and_test(&mddev->writes_pending)) {
6608 if (mddev->safemode == 2)
6609 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006610 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6612 }
6613}
6614
NeilBrown2a2275d2007-01-26 00:57:11 -08006615/* md_allow_write(mddev)
6616 * Calling this ensures that the array is marked 'active' so that writes
6617 * may proceed without blocking. It is important to call this before
6618 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6619 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006620 *
6621 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6622 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006623 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006624int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006625{
6626 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006627 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006628 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006629 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006630 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006631 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006632
6633 spin_lock_irq(&mddev->write_lock);
6634 if (mddev->in_sync) {
6635 mddev->in_sync = 0;
6636 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006637 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006638 if (mddev->safemode_delay &&
6639 mddev->safemode == 0)
6640 mddev->safemode = 1;
6641 spin_unlock_irq(&mddev->write_lock);
6642 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006643 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006644 } else
6645 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006646
NeilBrown070dc6d2010-08-30 17:33:34 +10006647 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006648 return -EAGAIN;
6649 else
6650 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006651}
6652EXPORT_SYMBOL_GPL(md_allow_write);
6653
NeilBrownb63d7c22010-06-01 19:37:33 +10006654void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006655{
6656 if (mddev->queue)
6657 blk_unplug(mddev->queue);
6658 if (mddev->plug)
6659 mddev->plug->unplug_fn(mddev->plug);
6660}
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662#define SYNC_MARKS 10
6663#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006664void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665{
6666 mddev_t *mddev2;
6667 unsigned int currspeed = 0,
6668 window;
NeilBrown57afd892005-06-21 17:17:13 -07006669 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 unsigned long mark[SYNC_MARKS];
6671 sector_t mark_cnt[SYNC_MARKS];
6672 int last_mark,m;
6673 struct list_head *tmp;
6674 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006675 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006676 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006677 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
6679 /* just incase thread restarts... */
6680 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6681 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006682 if (mddev->ro) /* never try to sync a read-only array */
6683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
NeilBrown61df9d92006-10-03 01:15:57 -07006685 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6686 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6687 desc = "data-check";
6688 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6689 desc = "requested-resync";
6690 else
6691 desc = "resync";
6692 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6693 desc = "reshape";
6694 else
6695 desc = "recovery";
6696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 /* we overload curr_resync somewhat here.
6698 * 0 == not engaged in resync at all
6699 * 2 == checking that there is no conflict with another sync
6700 * 1 == like 2, but have yielded to allow conflicting resync to
6701 * commense
6702 * other == active in resync - this many blocks
6703 *
6704 * Before starting a resync we must have set curr_resync to
6705 * 2, and then checked that every "conflicting" array has curr_resync
6706 * less than ours. When we find one that is the same or higher
6707 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6708 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6709 * This will mean we have to start checking from the beginning again.
6710 *
6711 */
6712
6713 do {
6714 mddev->curr_resync = 2;
6715
6716 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006717 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006718 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006719
6720 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006722 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 if (mddev2 == mddev)
6724 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006725 if (!mddev->parallel_resync
6726 && mddev2->curr_resync
6727 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 DEFINE_WAIT(wq);
6729 if (mddev < mddev2 && mddev->curr_resync == 2) {
6730 /* arbitrarily yield */
6731 mddev->curr_resync = 1;
6732 wake_up(&resync_wait);
6733 }
6734 if (mddev > mddev2 && mddev->curr_resync == 1)
6735 /* no need to wait here, we can wait the next
6736 * time 'round when curr_resync == 2
6737 */
6738 continue;
NeilBrown97441972008-09-19 11:49:54 +10006739 /* We need to wait 'interruptible' so as not to
6740 * contribute to the load average, and not to
6741 * be caught by 'softlockup'
6742 */
6743 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006744 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006745 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006746 printk(KERN_INFO "md: delaying %s of %s"
6747 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006749 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006751 if (signal_pending(current))
6752 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 schedule();
6754 finish_wait(&resync_wait, &wq);
6755 goto try_again;
6756 }
6757 finish_wait(&resync_wait, &wq);
6758 }
6759 }
6760 } while (mddev->curr_resync < 2);
6761
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006762 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006763 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006765 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 */
6767 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006768 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006769 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006770 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6771 j = mddev->resync_min;
6772 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006773 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006774
NeilBrownccfcc3c2006-03-27 01:18:09 -08006775 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006776 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006777 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006779 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006780 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006781 rcu_read_lock();
6782 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006783 if (rdev->raid_disk >= 0 &&
6784 !test_bit(Faulty, &rdev->flags) &&
6785 !test_bit(In_sync, &rdev->flags) &&
6786 rdev->recovery_offset < j)
6787 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006788 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
NeilBrown61df9d92006-10-03 01:15:57 -07006791 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6792 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6793 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006794 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006795 "(but not more than %d KB/sec) for %s.\n",
6796 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797
NeilBrowneea1bf32009-03-31 14:27:02 +11006798 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006799
NeilBrown57afd892005-06-21 17:17:13 -07006800 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 for (m = 0; m < SYNC_MARKS; m++) {
6802 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006803 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 }
6805 last_mark = 0;
6806 mddev->resync_mark = mark[last_mark];
6807 mddev->resync_mark_cnt = mark_cnt[last_mark];
6808
6809 /*
6810 * Tune reconstruction:
6811 */
6812 window = 32*(PAGE_SIZE/512);
6813 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6814 window/2,(unsigned long long) max_sectors/2);
6815
6816 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 last_check = 0;
6818
6819 if (j>2) {
6820 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006821 "md: resuming %s of %s from checkpoint.\n",
6822 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 mddev->curr_resync = j;
6824 }
NeilBrownefa59332009-12-14 12:49:49 +11006825 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
6827 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006828 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829
NeilBrown57afd892005-06-21 17:17:13 -07006830 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006831
NeilBrown7a91ee12009-05-26 12:57:21 +10006832 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6833 ((mddev->curr_resync > mddev->curr_resync_completed &&
6834 (mddev->curr_resync - mddev->curr_resync_completed)
6835 > (max_sectors >> 4)) ||
6836 (j - mddev->curr_resync_completed)*2
6837 >= mddev->resync_max - mddev->curr_resync_completed
6838 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006839 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006840 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006841 wait_event(mddev->recovery_wait,
6842 atomic_read(&mddev->recovery_active) == 0);
6843 mddev->curr_resync_completed =
6844 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006845 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006846 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006847 }
NeilBrownacb180b2009-04-14 16:28:34 +10006848
NeilBrowne62e58a2009-07-01 13:15:35 +10006849 while (j >= mddev->resync_max && !kthread_should_stop()) {
6850 /* As this condition is controlled by user-space,
6851 * we can block indefinitely, so use '_interruptible'
6852 * to avoid triggering warnings.
6853 */
6854 flush_signals(current); /* just in case */
6855 wait_event_interruptible(mddev->recovery_wait,
6856 mddev->resync_max > j
6857 || kthread_should_stop());
6858 }
NeilBrownacb180b2009-04-14 16:28:34 +10006859
6860 if (kthread_should_stop())
6861 goto interrupted;
6862
NeilBrown57afd892005-06-21 17:17:13 -07006863 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006864 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006865 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006866 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 goto out;
6868 }
NeilBrown57afd892005-06-21 17:17:13 -07006869
6870 if (!skipped) { /* actual IO requested */
6871 io_sectors += sectors;
6872 atomic_add(sectors, &mddev->recovery_active);
6873 }
6874
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 j += sectors;
6876 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006877 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006878 if (last_check == 0)
6879 /* this is the earliers that rebuilt will be
6880 * visible in /proc/mdstat
6881 */
6882 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006883
6884 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 continue;
6886
NeilBrown57afd892005-06-21 17:17:13 -07006887 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
NeilBrowndfc70642008-05-23 13:04:39 -07006889 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 break;
6891
6892 repeat:
6893 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6894 /* step marks */
6895 int next = (last_mark+1) % SYNC_MARKS;
6896
6897 mddev->resync_mark = mark[next];
6898 mddev->resync_mark_cnt = mark_cnt[next];
6899 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006900 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 last_mark = next;
6902 }
6903
6904
NeilBrownc6207272008-02-06 01:39:52 -08006905 if (kthread_should_stop())
6906 goto interrupted;
6907
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908
6909 /*
6910 * this loop exits only if either when we are slower than
6911 * the 'hard' speed limit, or the system was IO-idle for
6912 * a jiffy.
6913 * the system might be non-idle CPU-wise, but we only care
6914 * about not overloading the IO subsystem. (things like an
6915 * e2fsck being done on the RAID array should execute fast)
6916 */
NeilBrown252ac522010-06-01 19:37:29 +10006917 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 cond_resched();
6919
NeilBrown57afd892005-06-21 17:17:13 -07006920 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6921 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922
NeilBrown88202a02006-01-06 00:21:36 -08006923 if (currspeed > speed_min(mddev)) {
6924 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006925 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006926 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 goto repeat;
6928 }
6929 }
6930 }
NeilBrown61df9d92006-10-03 01:15:57 -07006931 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 /*
6933 * this also signals 'finished resyncing' to md_stop
6934 */
6935 out:
NeilBrown252ac522010-06-01 19:37:29 +10006936 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
6938 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6939
6940 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006941 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
NeilBrowndfc70642008-05-23 13:04:39 -07006943 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006944 mddev->curr_resync > 2) {
6945 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6946 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6947 if (mddev->curr_resync >= mddev->recovery_cp) {
6948 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006949 "md: checkpointing %s of %s.\n",
6950 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006951 mddev->recovery_cp = mddev->curr_resync;
6952 }
6953 } else
6954 mddev->recovery_cp = MaxSector;
6955 } else {
6956 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6957 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006958 rcu_read_lock();
6959 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006960 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006961 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006962 !test_bit(Faulty, &rdev->flags) &&
6963 !test_bit(In_sync, &rdev->flags) &&
6964 rdev->recovery_offset < mddev->curr_resync)
6965 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006966 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 }
NeilBrown17571282006-12-10 02:20:52 -08006969 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006972 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6973 /* We completed so min/max setting can be forgotten if used. */
6974 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6975 mddev->resync_min = 0;
6976 mddev->resync_max = MaxSector;
6977 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6978 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006980 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6981 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006982 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 wake_up(&resync_wait);
6984 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6985 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006986 return;
6987
6988 interrupted:
6989 /*
6990 * got a signal, exit.
6991 */
6992 printk(KERN_INFO
6993 "md: md_do_sync() got signal ... exiting\n");
6994 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6995 goto out;
6996
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997}
NeilBrown29269552006-03-27 01:18:10 -08006998EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
7000
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007001static int remove_and_add_spares(mddev_t *mddev)
7002{
7003 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007004 int spares = 0;
7005
NeilBrown97e4f422009-03-31 14:33:13 +11007006 mddev->curr_resync_completed = 0;
7007
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007008 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007009 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007010 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007011 (test_bit(Faulty, &rdev->flags) ||
7012 ! test_bit(In_sync, &rdev->flags)) &&
7013 atomic_read(&rdev->nr_pending)==0) {
7014 if (mddev->pers->hot_remove_disk(
7015 mddev, rdev->raid_disk)==0) {
7016 char nm[20];
7017 sprintf(nm,"rd%d", rdev->raid_disk);
7018 sysfs_remove_link(&mddev->kobj, nm);
7019 rdev->raid_disk = -1;
7020 }
7021 }
7022
NeilBrown4044ba52009-01-09 08:31:11 +11007023 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007024 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007025 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007026 !test_bit(In_sync, &rdev->flags) &&
7027 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007028 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007029 if (rdev->raid_disk < 0
7030 && !test_bit(Faulty, &rdev->flags)) {
7031 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007032 if (mddev->pers->
7033 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007034 char nm[20];
7035 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007036 if (sysfs_create_link(&mddev->kobj,
7037 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007038 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007039 spares++;
7040 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007041 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007042 } else
7043 break;
7044 }
NeilBrowndfc70642008-05-23 13:04:39 -07007045 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007046 }
7047 return spares;
7048}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007049
7050static void reap_sync_thread(mddev_t *mddev)
7051{
7052 mdk_rdev_t *rdev;
7053
7054 /* resync has finished, collect result */
7055 md_unregister_thread(mddev->sync_thread);
7056 mddev->sync_thread = NULL;
7057 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7058 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7059 /* success...*/
7060 /* activate any spares */
7061 if (mddev->pers->spare_active(mddev))
7062 sysfs_notify(&mddev->kobj, NULL,
7063 "degraded");
7064 }
7065 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7066 mddev->pers->finish_reshape)
7067 mddev->pers->finish_reshape(mddev);
7068 md_update_sb(mddev, 1);
7069
7070 /* if array is no-longer degraded, then any saved_raid_disk
7071 * information must be scrapped
7072 */
7073 if (!mddev->degraded)
7074 list_for_each_entry(rdev, &mddev->disks, same_set)
7075 rdev->saved_raid_disk = -1;
7076
7077 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7078 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7079 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7080 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7081 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7082 /* flag recovery needed just to double check */
7083 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7084 sysfs_notify_dirent_safe(mddev->sysfs_action);
7085 md_new_event(mddev);
7086}
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088/*
7089 * This routine is regularly called by all per-raid-array threads to
7090 * deal with generic issues like resync and super-block update.
7091 * Raid personalities that don't have a thread (linear/raid0) do not
7092 * need this as they never do any recovery or update the superblock.
7093 *
7094 * It does not do any resync itself, but rather "forks" off other threads
7095 * to do that as needed.
7096 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7097 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007098 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 * and wakeups up this thread which will reap the thread and finish up.
7100 * This thread also removes any faulty devices (with nr_pending == 0).
7101 *
7102 * The overall approach is:
7103 * 1/ if the superblock needs updating, update it.
7104 * 2/ If a recovery thread is running, don't do anything else.
7105 * 3/ If recovery has finished, clean up, possibly marking spares active.
7106 * 4/ If there are any faulty devices, remove them.
7107 * 5/ If array is degraded, try to add spares devices
7108 * 6/ If array has spares or is not in-sync, start a resync thread.
7109 */
7110void md_check_recovery(mddev_t *mddev)
7111{
NeilBrown5f404022005-06-21 17:17:16 -07007112 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007113 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
7115 if (mddev->ro)
7116 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007117
7118 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007119 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007120 printk(KERN_INFO "md: %s in immediate safe mode\n",
7121 mdname(mddev));
7122 mddev->safemode = 2;
7123 }
7124 flush_signals(current);
7125 }
7126
NeilBrownc89a8ee2008-08-05 15:54:13 +10007127 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7128 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007130 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007132 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007133 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007134 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7135 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 ))
7137 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007138
NeilBrowndf5b89b2006-03-27 01:18:20 -08007139 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007140 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007141
NeilBrownc89a8ee2008-08-05 15:54:13 +10007142 if (mddev->ro) {
7143 /* Only thing we do on a ro array is remove
7144 * failed devices.
7145 */
7146 remove_and_add_spares(mddev);
7147 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7148 goto unlock;
7149 }
7150
NeilBrown31a59e32008-04-30 00:52:30 -07007151 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007152 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007153 spin_lock_irq(&mddev->write_lock);
7154 if (mddev->safemode &&
7155 !atomic_read(&mddev->writes_pending) &&
7156 !mddev->in_sync &&
7157 mddev->recovery_cp == MaxSector) {
7158 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007159 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007160 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007161 }
7162 if (mddev->safemode == 1)
7163 mddev->safemode = 0;
7164 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007165 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007166 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007167 }
NeilBrownfca4d842005-06-21 17:17:11 -07007168
NeilBrown850b2b42006-10-03 01:15:46 -07007169 if (mddev->flags)
7170 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007171
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7173 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7174 /* resync/recovery still happening */
7175 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7176 goto unlock;
7177 }
7178 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007179 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 goto unlock;
7181 }
Neil Brown72a23c22008-06-28 08:31:41 +10007182 /* Set RUNNING before clearing NEEDED to avoid
7183 * any transients in the value of "sync_action".
7184 */
7185 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7186 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007187 /* Clear some bits that don't mean anything, but
7188 * might be left set
7189 */
NeilBrown24dd4692005-11-08 21:39:26 -08007190 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7191 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007193 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7194 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 /* no recovery is running.
7196 * remove any failed drives, then
7197 * add spares if possible.
7198 * Spare are also removed and re-added, to allow
7199 * the personality to fail the re-add.
7200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007202 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007203 if (mddev->pers->check_reshape == NULL ||
7204 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007205 /* Cannot proceed */
7206 goto unlock;
7207 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007208 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007209 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007210 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7211 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007212 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007213 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007214 } else if (mddev->recovery_cp < MaxSector) {
7215 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007216 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007217 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7218 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007220
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007222 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7223 /* We are adding a device or devices to an array
7224 * which has the bitmap stored on all devices.
7225 * So make sure all bitmap pages get written
7226 */
7227 bitmap_write_all(mddev->bitmap);
7228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 mddev->sync_thread = md_register_thread(md_do_sync,
7230 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007231 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 if (!mddev->sync_thread) {
7233 printk(KERN_ERR "%s: could not start resync"
7234 " thread...\n",
7235 mdname(mddev));
7236 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007237 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7238 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7239 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7240 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7241 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007242 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007244 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007245 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 }
7247 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007248 if (!mddev->sync_thread) {
7249 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7250 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7251 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007252 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007253 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 mddev_unlock(mddev);
7256 }
7257}
7258
Dan Williams6bfe0b42008-04-30 00:52:32 -07007259void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7260{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007261 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007262 wait_event_timeout(rdev->blocked_wait,
7263 !test_bit(Blocked, &rdev->flags),
7264 msecs_to_jiffies(5000));
7265 rdev_dec_pending(rdev, mddev);
7266}
7267EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7268
Adrian Bunk75c96f82005-05-05 16:16:09 -07007269static int md_notify_reboot(struct notifier_block *this,
7270 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
7272 struct list_head *tmp;
7273 mddev_t *mddev;
7274
7275 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7276
7277 printk(KERN_INFO "md: stopping all md devices.\n");
7278
NeilBrown29ac4aa2008-02-06 01:39:58 -08007279 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007280 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007281 /* Force a switch to readonly even array
7282 * appears to still be in use. Hence
7283 * the '100'.
7284 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007285 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007286 mddev_unlock(mddev);
7287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 /*
7289 * certain more exotic SCSI devices are known to be
7290 * volatile wrt too early system reboots. While the
7291 * right place to handle this issue is the given
7292 * driver, we do want to have a safe RAID driver ...
7293 */
7294 mdelay(1000*1);
7295 }
7296 return NOTIFY_DONE;
7297}
7298
Adrian Bunk75c96f82005-05-05 16:16:09 -07007299static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 .notifier_call = md_notify_reboot,
7301 .next = NULL,
7302 .priority = INT_MAX, /* before any real devices */
7303};
7304
7305static void md_geninit(void)
7306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7308
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007309 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310}
7311
Adrian Bunk75c96f82005-05-05 16:16:09 -07007312static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313{
Tejun Heoe804ac72010-10-15 15:36:08 +02007314 int ret = -ENOMEM;
7315
7316 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7317 if (!md_wq)
7318 goto err_wq;
7319
7320 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7321 if (!md_misc_wq)
7322 goto err_misc_wq;
7323
7324 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7325 goto err_md;
7326
7327 if ((ret = register_blkdev(0, "mdp")) < 0)
7328 goto err_mdp;
7329 mdp_major = ret;
7330
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007331 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007332 md_probe, NULL, NULL);
7333 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 md_probe, NULL, NULL);
7335
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007337 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
7339 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
Tejun Heoe804ac72010-10-15 15:36:08 +02007342err_mdp:
7343 unregister_blkdev(MD_MAJOR, "md");
7344err_md:
7345 destroy_workqueue(md_misc_wq);
7346err_misc_wq:
7347 destroy_workqueue(md_wq);
7348err_wq:
7349 return ret;
7350}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351
7352#ifndef MODULE
7353
7354/*
7355 * Searches all registered partitions for autorun RAID arrays
7356 * at boot time.
7357 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007358
7359static LIST_HEAD(all_detected_devices);
7360struct detected_devices_node {
7361 struct list_head list;
7362 dev_t dev;
7363};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
7365void md_autodetect_dev(dev_t dev)
7366{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007367 struct detected_devices_node *node_detected_dev;
7368
7369 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7370 if (node_detected_dev) {
7371 node_detected_dev->dev = dev;
7372 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7373 } else {
7374 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7375 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377}
7378
7379
7380static void autostart_arrays(int part)
7381{
7382 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007383 struct detected_devices_node *node_detected_dev;
7384 dev_t dev;
7385 int i_scanned, i_passed;
7386
7387 i_scanned = 0;
7388 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
7390 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7391
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007392 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7393 i_scanned++;
7394 node_detected_dev = list_entry(all_detected_devices.next,
7395 struct detected_devices_node, list);
7396 list_del(&node_detected_dev->list);
7397 dev = node_detected_dev->dev;
7398 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007399 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 if (IS_ERR(rdev))
7401 continue;
7402
NeilBrownb2d444d2005-11-08 21:39:31 -08007403 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 MD_BUG();
7405 continue;
7406 }
NeilBrownd0fae182008-03-04 14:29:31 -08007407 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007409 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007411
7412 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7413 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414
7415 autorun_devices(part);
7416}
7417
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007418#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
7420static __exit void md_exit(void)
7421{
7422 mddev_t *mddev;
7423 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007424
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007425 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007426 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007428 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 unregister_blkdev(mdp_major, "mdp");
7430 unregister_reboot_notifier(&md_notifier);
7431 unregister_sysctl_table(raid_table_header);
7432 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007433 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007435 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007437 destroy_workqueue(md_misc_wq);
7438 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439}
7440
Dan Williams685784a2007-07-09 11:56:42 -07007441subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442module_exit(md_exit)
7443
NeilBrownf91de922005-11-08 21:39:36 -08007444static int get_ro(char *buffer, struct kernel_param *kp)
7445{
7446 return sprintf(buffer, "%d", start_readonly);
7447}
7448static int set_ro(const char *val, struct kernel_param *kp)
7449{
7450 char *e;
7451 int num = simple_strtoul(val, &e, 10);
7452 if (*val && (*e == '\0' || *e == '\n')) {
7453 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007454 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007455 }
7456 return -EINVAL;
7457}
7458
NeilBrown80ca3a42006-07-10 04:44:18 -07007459module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7460module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007461
NeilBrownefeb53c2009-01-09 08:31:10 +11007462module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007463
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464EXPORT_SYMBOL(register_md_personality);
7465EXPORT_SYMBOL(unregister_md_personality);
7466EXPORT_SYMBOL(md_error);
7467EXPORT_SYMBOL(md_done_sync);
7468EXPORT_SYMBOL(md_write_start);
7469EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470EXPORT_SYMBOL(md_register_thread);
7471EXPORT_SYMBOL(md_unregister_thread);
7472EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473EXPORT_SYMBOL(md_check_recovery);
7474MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007475MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007476MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007477MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);