blob: e71c5fa527f59ec3e6ea5fd6ce7d09746bfc437e [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
NeilBrown409c57f2009-03-31 14:39:39 +1100291 if (mddev == NULL || mddev->pers == NULL) {
292 bio_io_error(bio);
293 return 0;
294 }
295 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200296 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100297 DEFINE_WAIT(__wait);
298 for (;;) {
299 prepare_to_wait(&mddev->sb_wait, &__wait,
300 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200301 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100302 break;
303 rcu_read_unlock();
304 schedule();
305 rcu_read_lock();
306 }
307 finish_wait(&mddev->sb_wait, &__wait);
308 }
309 atomic_inc(&mddev->active_io);
310 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100311
NeilBrown21a52c62010-04-01 15:02:13 +1100312 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100313
314 cpu = part_stat_lock();
315 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
316 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
317 bio_sectors(bio));
318 part_stat_unlock();
319
NeilBrown409c57f2009-03-31 14:39:39 +1100320 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
321 wake_up(&mddev->sb_wait);
322
323 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
NeilBrown9e35b992010-04-06 14:23:02 +1000326/* mddev_suspend makes sure no new requests are submitted
327 * to the device, and that any requests that have been submitted
328 * are completely handled.
329 * Once ->stop is called and completes, the module will be completely
330 * unused.
331 */
NeilBrown390ee602010-06-01 19:37:27 +1000332void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100333{
334 BUG_ON(mddev->suspended);
335 mddev->suspended = 1;
336 synchronize_rcu();
337 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
338 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100339}
NeilBrown390ee602010-06-01 19:37:27 +1000340EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100341
NeilBrown390ee602010-06-01 19:37:27 +1000342void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100343{
344 mddev->suspended = 0;
345 wake_up(&mddev->sb_wait);
346 mddev->pers->quiesce(mddev, 0);
347}
NeilBrown390ee602010-06-01 19:37:27 +1000348EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100349
NeilBrown3fa841d2009-09-23 18:10:29 +1000350int mddev_congested(mddev_t *mddev, int bits)
351{
352 return mddev->suspended;
353}
354EXPORT_SYMBOL(mddev_congested);
355
NeilBrowna2826aa2009-12-14 12:49:49 +1100356/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200357 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100358 */
359
Tejun Heoe9c74692010-09-03 11:56:18 +0200360static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100361{
362 mdk_rdev_t *rdev = bio->bi_private;
363 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100364
365 rdev_dec_pending(rdev, mddev);
366
367 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200368 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200369 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100370 }
371 bio_put(bio);
372}
373
NeilBrowna7a07e62010-12-09 16:04:25 +1100374static void md_submit_flush_data(struct work_struct *ws);
375
NeilBrowna035fc32010-12-09 16:17:51 +1100376static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100377{
NeilBrowna035fc32010-12-09 16:17:51 +1100378 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100379 mdk_rdev_t *rdev;
380
NeilBrowna7a07e62010-12-09 16:04:25 +1100381 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
382 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100383 rcu_read_lock();
384 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
385 if (rdev->raid_disk >= 0 &&
386 !test_bit(Faulty, &rdev->flags)) {
387 /* Take two references, one is dropped
388 * when request finishes, one after
389 * we reclaim rcu_read_lock
390 */
391 struct bio *bi;
392 atomic_inc(&rdev->nr_pending);
393 atomic_inc(&rdev->nr_pending);
394 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100395 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200396 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100397 bi->bi_private = rdev;
398 bi->bi_bdev = rdev->bdev;
399 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200400 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100401 rcu_read_lock();
402 rdev_dec_pending(rdev, mddev);
403 }
404 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100405 if (atomic_dec_and_test(&mddev->flush_pending))
406 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100407}
408
Tejun Heoe9c74692010-09-03 11:56:18 +0200409static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100410{
Tejun Heoe9c74692010-09-03 11:56:18 +0200411 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
412 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100413
Tejun Heoe9c74692010-09-03 11:56:18 +0200414 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 /* an empty barrier - all done */
416 bio_endio(bio, 0);
417 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100419 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100420 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100421 }
NeilBrown2b74e122010-12-09 15:59:01 +1100422
423 mddev->flush_bio = NULL;
424 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100425}
426
Tejun Heoe9c74692010-09-03 11:56:18 +0200427void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100428{
429 spin_lock_irq(&mddev->write_lock);
430 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200431 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100432 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100434 spin_unlock_irq(&mddev->write_lock);
435
NeilBrowna035fc32010-12-09 16:17:51 +1100436 INIT_WORK(&mddev->flush_work, submit_flushes);
437 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438}
Tejun Heoe9c74692010-09-03 11:56:18 +0200439EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100440
NeilBrown2ac87402010-06-01 19:37:29 +1000441/* Support for plugging.
442 * This mirrors the plugging support in request_queue, but does not
443 * require having a whole queue
444 */
445static void plugger_work(struct work_struct *work)
446{
447 struct plug_handle *plug =
448 container_of(work, struct plug_handle, unplug_work);
449 plug->unplug_fn(plug);
450}
451static void plugger_timeout(unsigned long data)
452{
453 struct plug_handle *plug = (void *)data;
454 kblockd_schedule_work(NULL, &plug->unplug_work);
455}
456void plugger_init(struct plug_handle *plug,
457 void (*unplug_fn)(struct plug_handle *))
458{
459 plug->unplug_flag = 0;
460 plug->unplug_fn = unplug_fn;
461 init_timer(&plug->unplug_timer);
462 plug->unplug_timer.function = plugger_timeout;
463 plug->unplug_timer.data = (unsigned long)plug;
464 INIT_WORK(&plug->unplug_work, plugger_work);
465}
466EXPORT_SYMBOL_GPL(plugger_init);
467
468void plugger_set_plug(struct plug_handle *plug)
469{
470 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
471 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
472}
473EXPORT_SYMBOL_GPL(plugger_set_plug);
474
475int plugger_remove_plug(struct plug_handle *plug)
476{
477 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
478 del_timer(&plug->unplug_timer);
479 return 1;
480 } else
481 return 0;
482}
483EXPORT_SYMBOL_GPL(plugger_remove_plug);
484
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static inline mddev_t *mddev_get(mddev_t *mddev)
487{
488 atomic_inc(&mddev->active);
489 return mddev;
490}
491
Dan Williams5fd3a172009-03-04 00:57:25 -0700492static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static void mddev_put(mddev_t *mddev)
495{
NeilBrowna167f662010-10-26 18:31:13 +1100496 struct bio_set *bs = NULL;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
499 return;
NeilBrownd3374822009-01-09 08:31:10 +1100500 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100501 mddev->ctime == 0 && !mddev->hold_active) {
502 /* Array is not configured at all, and not held active,
503 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100505 bs = mddev->bio_set;
506 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100507 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200508 /* We did a probe so need to clean up. Call
509 * queue_work inside the spinlock so that
510 * flush_workqueue() after mddev_find will
511 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100512 */
513 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200514 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100515 } else
516 kfree(mddev);
517 }
518 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100519 if (bs)
520 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
NeilBrown390ee602010-06-01 19:37:27 +1000523void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100524{
525 mutex_init(&mddev->open_mutex);
526 mutex_init(&mddev->reconfig_mutex);
527 mutex_init(&mddev->bitmap_info.mutex);
528 INIT_LIST_HEAD(&mddev->disks);
529 INIT_LIST_HEAD(&mddev->all_mddevs);
530 init_timer(&mddev->safemode_timer);
531 atomic_set(&mddev->active, 1);
532 atomic_set(&mddev->openers, 0);
533 atomic_set(&mddev->active_io, 0);
534 spin_lock_init(&mddev->write_lock);
535 atomic_set(&mddev->flush_pending, 0);
536 init_waitqueue_head(&mddev->sb_wait);
537 init_waitqueue_head(&mddev->recovery_wait);
538 mddev->reshape_position = MaxSector;
539 mddev->resync_min = 0;
540 mddev->resync_max = MaxSector;
541 mddev->level = LEVEL_NONE;
542}
NeilBrown390ee602010-06-01 19:37:27 +1000543EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static mddev_t * mddev_find(dev_t unit)
546{
547 mddev_t *mddev, *new = NULL;
548
549 retry:
550 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
NeilBrownefeb53c2009-01-09 08:31:10 +1100552 if (unit) {
553 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
554 if (mddev->unit == unit) {
555 mddev_get(mddev);
556 spin_unlock(&all_mddevs_lock);
557 kfree(new);
558 return mddev;
559 }
560
561 if (new) {
562 list_add(&new->all_mddevs, &all_mddevs);
563 spin_unlock(&all_mddevs_lock);
564 new->hold_active = UNTIL_IOCTL;
565 return new;
566 }
567 } else if (new) {
568 /* find an unused unit number */
569 static int next_minor = 512;
570 int start = next_minor;
571 int is_free = 0;
572 int dev = 0;
573 while (!is_free) {
574 dev = MKDEV(MD_MAJOR, next_minor);
575 next_minor++;
576 if (next_minor > MINORMASK)
577 next_minor = 0;
578 if (next_minor == start) {
579 /* Oh dear, all in use. */
580 spin_unlock(&all_mddevs_lock);
581 kfree(new);
582 return NULL;
583 }
584
585 is_free = 1;
586 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
587 if (mddev->unit == dev) {
588 is_free = 0;
589 break;
590 }
591 }
592 new->unit = dev;
593 new->md_minor = MINOR(dev);
594 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 list_add(&new->all_mddevs, &all_mddevs);
596 spin_unlock(&all_mddevs_lock);
597 return new;
598 }
599 spin_unlock(&all_mddevs_lock);
600
NeilBrown9ffae0c2006-01-06 00:20:32 -0800601 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!new)
603 return NULL;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 new->unit = unit;
606 if (MAJOR(unit) == MD_MAJOR)
607 new->md_minor = MINOR(unit);
608 else
609 new->md_minor = MINOR(unit) >> MdpMinorShift;
610
NeilBrownfafd7fb2010-04-01 15:55:30 +1100611 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto retry;
614}
615
616static inline int mddev_lock(mddev_t * mddev)
617{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800618 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Dan Williamsb522adc2009-03-31 15:00:31 +1100621static inline int mddev_is_locked(mddev_t *mddev)
622{
623 return mutex_is_locked(&mddev->reconfig_mutex);
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static inline int mddev_trylock(mddev_t * mddev)
627{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800628 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
NeilBrownb6eb1272010-04-15 10:13:47 +1000631static struct attribute_group md_redundancy_group;
632
NeilBrowna64c8762010-04-14 17:15:37 +1000633static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
NeilBrowna64c8762010-04-14 17:15:37 +1000635 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000636 /* These cannot be removed under reconfig_mutex as
637 * an access to the files will try to take reconfig_mutex
638 * while holding the file unremovable, which leads to
639 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000640 * So hold set sysfs_active while the remove in happeing,
641 * and anything else which might set ->to_remove or my
642 * otherwise change the sysfs namespace will fail with
643 * -EBUSY if sysfs_active is still set.
644 * We set sysfs_active under reconfig_mutex and elsewhere
645 * test it under the same mutex to ensure its correct value
646 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000647 */
NeilBrowna64c8762010-04-14 17:15:37 +1000648 struct attribute_group *to_remove = mddev->to_remove;
649 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000650 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000651 mutex_unlock(&mddev->reconfig_mutex);
652
NeilBrown00bcb4a2010-06-01 19:37:23 +1000653 if (mddev->kobj.sd) {
654 if (to_remove != &md_redundancy_group)
655 sysfs_remove_group(&mddev->kobj, to_remove);
656 if (mddev->pers == NULL ||
657 mddev->pers->sync_request == NULL) {
658 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
659 if (mddev->sysfs_action)
660 sysfs_put(mddev->sysfs_action);
661 mddev->sysfs_action = NULL;
662 }
NeilBrowna64c8762010-04-14 17:15:37 +1000663 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000664 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000665 } else
666 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
NeilBrown005eca52005-08-22 13:11:08 -0700668 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
NeilBrown2989ddb2006-01-06 00:20:43 -0800671static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100673 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100675 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (rdev->desc_nr == nr)
677 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return NULL;
680}
681
682static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 mdk_rdev_t *rdev;
685
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100686 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (rdev->bdev->bd_dev == dev)
688 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return NULL;
691}
692
NeilBrownd9d166c2006-01-06 00:20:51 -0800693static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800694{
695 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800696 list_for_each_entry(pers, &pers_list, list) {
697 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800698 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800699 if (strcmp(pers->name, clevel)==0)
700 return pers;
701 }
NeilBrown2604b702006-01-06 00:20:36 -0800702 return NULL;
703}
704
Andre Nollb73df2d2008-07-11 22:02:23 +1000705/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700706static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Mike Snitzer77304d22010-11-08 14:39:12 +0100708 sector_t num_sectors = i_size_read(bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000709 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static int alloc_disk_sb(mdk_rdev_t * rdev)
713{
714 if (rdev->sb_page)
715 MD_BUG();
716
717 rdev->sb_page = alloc_page(GFP_KERNEL);
718 if (!rdev->sb_page) {
719 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 return 0;
724}
725
726static void free_disk_sb(mdk_rdev_t * rdev)
727{
728 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800729 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 rdev->sb_loaded = 0;
731 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000732 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100733 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735}
736
737
NeilBrown6712ecf2007-09-27 12:47:43 +0200738static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700739{
740 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800741 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700742
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700743 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
744 printk("md: super_written gets error=%d, uptodate=%d\n",
745 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
746 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800747 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700748 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749
NeilBrowna9701a32005-11-08 21:39:34 -0800750 if (atomic_dec_and_test(&mddev->pending_writes))
751 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700752 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700753}
754
755void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
756 sector_t sector, int size, struct page *page)
757{
758 /* write first size bytes of page to sector of rdev
759 * Increment mddev->pending_writes before returning
760 * and decrement it on completion, waking up sb_wait
761 * if zero is reached.
762 * If an error occurred, call md_error
763 */
NeilBrowna167f662010-10-26 18:31:13 +1100764 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700765
766 bio->bi_bdev = rdev->bdev;
767 bio->bi_sector = sector;
768 bio_add_page(bio, page, size, 0);
769 bio->bi_private = rdev;
770 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800771
NeilBrown7bfa19f2005-06-21 17:17:28 -0700772 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200773 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
774 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800775}
776
777void md_super_wait(mddev_t *mddev)
778{
Tejun Heoe9c74692010-09-03 11:56:18 +0200779 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800780 DEFINE_WAIT(wq);
781 for(;;) {
782 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
783 if (atomic_read(&mddev->pending_writes)==0)
784 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800785 schedule();
786 }
787 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700788}
789
NeilBrown6712ecf2007-09-27 12:47:43 +0200790static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
NeilBrown2b193362010-10-27 15:16:40 +1100795int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
796 struct page *page, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
NeilBrowna167f662010-10-26 18:31:13 +1100798 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct completion event;
800 int ret;
801
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200802 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
NeilBrown2b193362010-10-27 15:16:40 +1100804 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 bio->bi_sector = sector;
806 bio_add_page(bio, page, size, 0);
807 init_completion(&event);
808 bio->bi_private = &event;
809 bio->bi_end_io = bi_complete;
810 submit_bio(rw, bio);
811 wait_for_completion(&event);
812
813 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
814 bio_put(bio);
815 return ret;
816}
NeilBrowna8745db2006-01-06 00:20:34 -0800817EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
NeilBrown0002b272005-09-09 16:23:53 -0700819static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 char b[BDEVNAME_SIZE];
822 if (!rdev->sb_page) {
823 MD_BUG();
824 return -EINVAL;
825 }
826 if (rdev->sb_loaded)
827 return 0;
828
829
NeilBrown2b193362010-10-27 15:16:40 +1100830 if (!sync_page_io(rdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto fail;
832 rdev->sb_loaded = 1;
833 return 0;
834
835fail:
836 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
837 bdevname(rdev->bdev,b));
838 return -EINVAL;
839}
840
841static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
842{
Andre Noll05710462008-07-11 22:02:20 +1000843 return sb1->set_uuid0 == sb2->set_uuid0 &&
844 sb1->set_uuid1 == sb2->set_uuid1 &&
845 sb1->set_uuid2 == sb2->set_uuid2 &&
846 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
850{
851 int ret;
852 mdp_super_t *tmp1, *tmp2;
853
854 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
855 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
856
857 if (!tmp1 || !tmp2) {
858 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100859 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto abort;
861 }
862
863 *tmp1 = *sb1;
864 *tmp2 = *sb2;
865
866 /*
867 * nr_disks is not constant
868 */
869 tmp1->nr_disks = 0;
870 tmp2->nr_disks = 0;
871
Andre Nollce0c8e02008-07-11 22:02:20 +1000872 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700874 kfree(tmp1);
875 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877}
878
NeilBrown4d167f02007-05-09 02:35:37 -0700879
880static u32 md_csum_fold(u32 csum)
881{
882 csum = (csum & 0xffff) + (csum >> 16);
883 return (csum & 0xffff) + (csum >> 16);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static unsigned int calc_sb_csum(mdp_super_t * sb)
887{
NeilBrown4d167f02007-05-09 02:35:37 -0700888 u64 newcsum = 0;
889 u32 *sb32 = (u32*)sb;
890 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 unsigned int disk_csum, csum;
892
893 disk_csum = sb->sb_csum;
894 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700895
896 for (i = 0; i < MD_SB_BYTES/4 ; i++)
897 newcsum += sb32[i];
898 csum = (newcsum & 0xffffffff) + (newcsum>>32);
899
900
901#ifdef CONFIG_ALPHA
902 /* This used to use csum_partial, which was wrong for several
903 * reasons including that different results are returned on
904 * different architectures. It isn't critical that we get exactly
905 * the same return value as before (we always csum_fold before
906 * testing, and that removes any differences). However as we
907 * know that csum_partial always returned a 16bit value on
908 * alphas, do a fold to maximise conformity to previous behaviour.
909 */
910 sb->sb_csum = md_csum_fold(disk_csum);
911#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return csum;
915}
916
917
918/*
919 * Handle superblock details.
920 * We want to be able to handle multiple superblock formats
921 * so we have a common interface to them all, and an array of
922 * different handlers.
923 * We rely on user-space to write the initial superblock, and support
924 * reading and updating of superblocks.
925 * Interface methods are:
926 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
927 * loads and validates a superblock on dev.
928 * if refdev != NULL, compare superblocks on both devices
929 * Return:
930 * 0 - dev has a superblock that is compatible with refdev
931 * 1 - dev has a superblock that is compatible and newer than refdev
932 * so dev should be used as the refdev in future
933 * -EINVAL superblock incompatible or invalid
934 * -othererror e.g. -EIO
935 *
936 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
937 * Verify that dev is acceptable into mddev.
938 * The first time, mddev->raid_disks will be 0, and data from
939 * dev should be merged in. Subsequent calls check that dev
940 * is new enough. Return 0 or -EINVAL
941 *
942 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
943 * Update the superblock for rdev with data in mddev
944 * This does not write to disc.
945 *
946 */
947
948struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000949 char *name;
950 struct module *owner;
951 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
952 int minor_version);
953 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
954 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
955 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000956 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957};
958
959/*
Andre Noll0894cc32009-06-18 08:49:23 +1000960 * Check that the given mddev has no bitmap.
961 *
962 * This function is called from the run method of all personalities that do not
963 * support bitmaps. It prints an error message and returns non-zero if mddev
964 * has a bitmap. Otherwise, it returns 0.
965 *
966 */
967int md_check_no_bitmap(mddev_t *mddev)
968{
NeilBrownc3d97142009-12-14 12:49:52 +1100969 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000970 return 0;
971 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
972 mdname(mddev), mddev->pers->name);
973 return 1;
974}
975EXPORT_SYMBOL(md_check_no_bitmap);
976
977/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * load_super for 0.90.0
979 */
980static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
981{
982 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
983 mdp_super_t *sb;
984 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /*
Andre Noll0f420352008-07-11 22:02:23 +1000987 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * it's at the end of the disk.
989 *
990 * It also happens to be a multiple of 4Kb.
991 */
Andre Noll0f420352008-07-11 22:02:23 +1000992 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
NeilBrown0002b272005-09-09 16:23:53 -0700994 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (ret) return ret;
996
997 ret = -EINVAL;
998
999 bdevname(rdev->bdev, b);
1000 sb = (mdp_super_t*)page_address(rdev->sb_page);
1001
1002 if (sb->md_magic != MD_SB_MAGIC) {
1003 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1004 b);
1005 goto abort;
1006 }
1007
1008 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001009 sb->minor_version < 90 ||
1010 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1012 sb->major_version, sb->minor_version,
1013 b);
1014 goto abort;
1015 }
1016
1017 if (sb->raid_disks <= 0)
1018 goto abort;
1019
NeilBrown4d167f02007-05-09 02:35:37 -07001020 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1022 b);
1023 goto abort;
1024 }
1025
1026 rdev->preferred_minor = sb->md_minor;
1027 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001028 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (sb->level == LEVEL_MULTIPATH)
1031 rdev->desc_nr = -1;
1032 else
1033 rdev->desc_nr = sb->this_disk.number;
1034
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001035 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001037 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 __u64 ev1, ev2;
1039 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1040 if (!uuid_equal(refsb, sb)) {
1041 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1042 b, bdevname(refdev->bdev,b2));
1043 goto abort;
1044 }
1045 if (!sb_equal(refsb, sb)) {
1046 printk(KERN_WARNING "md: %s has same UUID"
1047 " but different superblock to %s\n",
1048 b, bdevname(refdev->bdev, b2));
1049 goto abort;
1050 }
1051 ev1 = md_event(sb);
1052 ev2 = md_event(refsb);
1053 if (ev1 > ev2)
1054 ret = 1;
1055 else
1056 ret = 0;
1057 }
NeilBrown8190e752009-06-18 08:48:58 +10001058 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Andre Nolldd8ac332009-03-31 14:33:13 +11001060 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001061 /* "this cannot possibly happen" ... */
1062 ret = -EINVAL;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 abort:
1065 return ret;
1066}
1067
1068/*
1069 * validate_super for 0.90.0
1070 */
1071static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1072{
1073 mdp_disk_t *desc;
1074 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001075 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
NeilBrown41158c72005-06-21 17:17:25 -07001077 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001078 clear_bit(Faulty, &rdev->flags);
1079 clear_bit(In_sync, &rdev->flags);
1080 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (mddev->raid_disks == 0) {
1083 mddev->major_version = 0;
1084 mddev->minor_version = sb->minor_version;
1085 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001086 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001087 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 mddev->ctime = sb->ctime;
1089 mddev->utime = sb->utime;
1090 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001091 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 mddev->layout = sb->layout;
1093 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001094 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001095 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001096 mddev->bitmap_info.offset = 0;
1097 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
NeilBrownf6705572006-03-27 01:18:11 -08001099 if (mddev->minor_version >= 91) {
1100 mddev->reshape_position = sb->reshape_position;
1101 mddev->delta_disks = sb->delta_disks;
1102 mddev->new_level = sb->new_level;
1103 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001104 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001105 } else {
1106 mddev->reshape_position = MaxSector;
1107 mddev->delta_disks = 0;
1108 mddev->new_level = mddev->level;
1109 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001110 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (sb->state & (1<<MD_SB_CLEAN))
1114 mddev->recovery_cp = MaxSector;
1115 else {
1116 if (sb->events_hi == sb->cp_events_hi &&
1117 sb->events_lo == sb->cp_events_lo) {
1118 mddev->recovery_cp = sb->recovery_cp;
1119 } else
1120 mddev->recovery_cp = 0;
1121 }
1122
1123 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1124 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1125 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1126 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1127
1128 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001129
1130 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001131 mddev->bitmap_info.file == NULL)
1132 mddev->bitmap_info.offset =
1133 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001134
NeilBrown41158c72005-06-21 17:17:25 -07001135 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001136 /* Insist on good event counter while assembling, except
1137 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001139 if (sb->disks[rdev->desc_nr].state & (
1140 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1141 if (ev1 < mddev->events)
1142 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001143 } else if (mddev->bitmap) {
1144 /* if adding to array with a bitmap, then we can accept an
1145 * older device ... but not too old.
1146 */
NeilBrown41158c72005-06-21 17:17:25 -07001147 if (ev1 < mddev->bitmap->events_cleared)
1148 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001149 } else {
1150 if (ev1 < mddev->events)
1151 /* just a hot-add of a new device, leave raid_disk at -1 */
1152 return 0;
1153 }
NeilBrown41158c72005-06-21 17:17:25 -07001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 desc = sb->disks + rdev->desc_nr;
1157
1158 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001159 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001160 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1161 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001162 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001164 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1165 /* active but not in sync implies recovery up to
1166 * reshape position. We don't know exactly where
1167 * that is, so set to zero for now */
1168 if (mddev->minor_version >= 91) {
1169 rdev->recovery_offset = 0;
1170 rdev->raid_disk = desc->raid_disk;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001173 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1174 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001175 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001176 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return 0;
1178}
1179
1180/*
1181 * sync_super for 0.90.0
1182 */
1183static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1184{
1185 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 mdk_rdev_t *rdev2;
1187 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* make rdev->sb match mddev data..
1191 *
1192 * 1/ zero out disks
1193 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1194 * 3/ any empty disks < next_spare become removed
1195 *
1196 * disks[0] gets initialised to REMOVED because
1197 * we cannot be sure from other fields if it has
1198 * been initialised or not.
1199 */
1200 int i;
1201 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1202
NeilBrown61181562005-09-09 16:24:02 -07001203 rdev->sb_size = MD_SB_BYTES;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 sb = (mdp_super_t*)page_address(rdev->sb_page);
1206
1207 memset(sb, 0, sizeof(*sb));
1208
1209 sb->md_magic = MD_SB_MAGIC;
1210 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 sb->patch_version = mddev->patch_version;
1212 sb->gvalid_words = 0; /* ignored */
1213 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1214 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1215 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1216 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1217
1218 sb->ctime = mddev->ctime;
1219 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001220 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sb->raid_disks = mddev->raid_disks;
1222 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001223 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sb->utime = mddev->utime;
1225 sb->state = 0;
1226 sb->events_hi = (mddev->events>>32);
1227 sb->events_lo = (u32)mddev->events;
1228
NeilBrownf6705572006-03-27 01:18:11 -08001229 if (mddev->reshape_position == MaxSector)
1230 sb->minor_version = 90;
1231 else {
1232 sb->minor_version = 91;
1233 sb->reshape_position = mddev->reshape_position;
1234 sb->new_level = mddev->new_level;
1235 sb->delta_disks = mddev->delta_disks;
1236 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001237 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001238 }
1239 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (mddev->in_sync)
1241 {
1242 sb->recovery_cp = mddev->recovery_cp;
1243 sb->cp_events_hi = (mddev->events>>32);
1244 sb->cp_events_lo = (u32)mddev->events;
1245 if (mddev->recovery_cp == MaxSector)
1246 sb->state = (1<< MD_SB_CLEAN);
1247 } else
1248 sb->recovery_cp = 0;
1249
1250 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001251 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
NeilBrownc3d97142009-12-14 12:49:52 +11001253 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001254 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001257 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001259 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001260 int is_active = test_bit(In_sync, &rdev2->flags);
1261
1262 if (rdev2->raid_disk >= 0 &&
1263 sb->minor_version >= 91)
1264 /* we have nowhere to store the recovery_offset,
1265 * but if it is not below the reshape_position,
1266 * we can piggy-back on that.
1267 */
1268 is_active = 1;
1269 if (rdev2->raid_disk < 0 ||
1270 test_bit(Faulty, &rdev2->flags))
1271 is_active = 0;
1272 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001273 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001275 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001276 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 d = &sb->disks[rdev2->desc_nr];
1278 nr_disks++;
1279 d->number = rdev2->desc_nr;
1280 d->major = MAJOR(rdev2->bdev->bd_dev);
1281 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001282 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 d->raid_disk = rdev2->raid_disk;
1284 else
1285 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001286 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001288 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001290 if (test_bit(In_sync, &rdev2->flags))
1291 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 active++;
1293 working++;
1294 } else {
1295 d->state = 0;
1296 spare++;
1297 working++;
1298 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001299 if (test_bit(WriteMostly, &rdev2->flags))
1300 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* now set the "removed" and "faulty" bits on any missing devices */
1303 for (i=0 ; i < mddev->raid_disks ; i++) {
1304 mdp_disk_t *d = &sb->disks[i];
1305 if (d->state == 0 && d->number == 0) {
1306 d->number = i;
1307 d->raid_disk = i;
1308 d->state = (1<<MD_DISK_REMOVED);
1309 d->state |= (1<<MD_DISK_FAULTY);
1310 failed++;
1311 }
1312 }
1313 sb->nr_disks = nr_disks;
1314 sb->active_disks = active;
1315 sb->working_disks = working;
1316 sb->failed_disks = failed;
1317 sb->spare_disks = spare;
1318
1319 sb->this_disk = sb->disks[rdev->desc_nr];
1320 sb->sb_csum = calc_sb_csum(sb);
1321}
1322
1323/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001324 * rdev_size_change for 0.90.0
1325 */
1326static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001327super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001328{
Andre Noll58c0fed2009-03-31 14:33:13 +11001329 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001331 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001332 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001333 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001334 if (!num_sectors || num_sectors > rdev->sb_start)
1335 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001336 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 rdev->sb_page);
1338 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001339 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340}
1341
1342
1343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * version 1 superblock
1345 */
1346
NeilBrown1c05b4b2006-10-21 10:24:08 -07001347static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001349 __le32 disk_csum;
1350 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 unsigned long long newcsum;
1352 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001353 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int i;
1355
1356 disk_csum = sb->sb_csum;
1357 sb->sb_csum = 0;
1358 newcsum = 0;
1359 for (i=0; size>=4; size -= 4 )
1360 newcsum += le32_to_cpu(*isuper++);
1361
1362 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001363 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1366 sb->sb_csum = disk_csum;
1367 return cpu_to_le32(csum);
1368}
1369
1370static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1371{
1372 struct mdp_superblock_1 *sb;
1373 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001374 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001376 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /*
Andre Noll0f420352008-07-11 22:02:23 +10001379 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * It is always aligned to a 4K boundary and
1381 * depeding on minor_version, it can be:
1382 * 0: At least 8K, but less than 12K, from end of device
1383 * 1: At start of device
1384 * 2: 4K from start of device.
1385 */
1386 switch(minor_version) {
1387 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001388 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001389 sb_start -= 8*2;
1390 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001393 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001396 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398 default:
1399 return -EINVAL;
1400 }
Andre Noll0f420352008-07-11 22:02:23 +10001401 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
NeilBrown0002b272005-09-09 16:23:53 -07001403 /* superblock is rarely larger than 1K, but it can be larger,
1404 * and it is safe to read 4k, so we do that
1405 */
1406 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (ret) return ret;
1408
1409
1410 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1411
1412 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1413 sb->major_version != cpu_to_le32(1) ||
1414 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001415 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001416 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EINVAL;
1418
1419 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1420 printk("md: invalid superblock checksum on %s\n",
1421 bdevname(rdev->bdev,b));
1422 return -EINVAL;
1423 }
1424 if (le64_to_cpu(sb->data_size) < 10) {
1425 printk("md: data_size too small on %s\n",
1426 bdevname(rdev->bdev,b));
1427 return -EINVAL;
1428 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 rdev->preferred_minor = 0xffff;
1431 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001432 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
NeilBrown0002b272005-09-09 16:23:53 -07001434 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001435 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001436 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001437 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1438
1439 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001440 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001441 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001442
NeilBrown31b65a02006-07-10 04:44:14 -07001443 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1444 rdev->desc_nr = -1;
1445 else
1446 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1447
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001448 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001449 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001450 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 __u64 ev1, ev2;
1452 struct mdp_superblock_1 *refsb =
1453 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1454
1455 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1456 sb->level != refsb->level ||
1457 sb->layout != refsb->layout ||
1458 sb->chunksize != refsb->chunksize) {
1459 printk(KERN_WARNING "md: %s has strangely different"
1460 " superblock to %s\n",
1461 bdevname(rdev->bdev,b),
1462 bdevname(refdev->bdev,b2));
1463 return -EINVAL;
1464 }
1465 ev1 = le64_to_cpu(sb->events);
1466 ev2 = le64_to_cpu(refsb->events);
1467
1468 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001469 ret = 1;
1470 else
1471 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
NeilBrowna1801f82008-03-04 14:29:31 -08001473 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001474 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001475 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001477 rdev->sectors = rdev->sb_start;
1478 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001480 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001481 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001482 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1487{
1488 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001489 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
NeilBrown41158c72005-06-21 17:17:25 -07001491 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001492 clear_bit(Faulty, &rdev->flags);
1493 clear_bit(In_sync, &rdev->flags);
1494 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (mddev->raid_disks == 0) {
1497 mddev->major_version = 1;
1498 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001499 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001500 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1502 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1503 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001504 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 mddev->layout = le32_to_cpu(sb->layout);
1506 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001507 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001508 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001509 mddev->bitmap_info.offset = 0;
1510 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1513 memcpy(mddev->uuid, sb->set_uuid, 16);
1514
1515 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001516
NeilBrown71c08052005-09-09 16:23:51 -07001517 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001518 mddev->bitmap_info.file == NULL )
1519 mddev->bitmap_info.offset =
1520 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001521
NeilBrownf6705572006-03-27 01:18:11 -08001522 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1523 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1524 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1525 mddev->new_level = le32_to_cpu(sb->new_level);
1526 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001527 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001528 } else {
1529 mddev->reshape_position = MaxSector;
1530 mddev->delta_disks = 0;
1531 mddev->new_level = mddev->level;
1532 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001533 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001534 }
1535
NeilBrown41158c72005-06-21 17:17:25 -07001536 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001537 /* Insist of good event counter while assembling, except for
1538 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001540 if (rdev->desc_nr >= 0 &&
1541 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1542 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1543 if (ev1 < mddev->events)
1544 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001545 } else if (mddev->bitmap) {
1546 /* If adding to array with a bitmap, then we can accept an
1547 * older device, but not too old.
1548 */
NeilBrown41158c72005-06-21 17:17:25 -07001549 if (ev1 < mddev->bitmap->events_cleared)
1550 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001551 } else {
1552 if (ev1 < mddev->events)
1553 /* just a hot-add of a new device, leave raid_disk at -1 */
1554 return 0;
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (mddev->level != LEVEL_MULTIPATH) {
1557 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001558 if (rdev->desc_nr < 0 ||
1559 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1560 role = 0xffff;
1561 rdev->desc_nr = -1;
1562 } else
1563 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 switch(role) {
1565 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001568 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
1570 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001571 if ((le32_to_cpu(sb->feature_map) &
1572 MD_FEATURE_RECOVERY_OFFSET))
1573 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1574 else
1575 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 rdev->raid_disk = role;
1577 break;
1578 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001579 if (sb->devflags & WriteMostly1)
1580 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001581 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001582 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return 0;
1585}
1586
1587static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1588{
1589 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 mdk_rdev_t *rdev2;
1591 int max_dev, i;
1592 /* make rdev->sb match mddev and rdev data. */
1593
1594 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1595
1596 sb->feature_map = 0;
1597 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001598 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 memset(sb->pad1, 0, sizeof(sb->pad1));
1600 memset(sb->pad2, 0, sizeof(sb->pad2));
1601 memset(sb->pad3, 0, sizeof(sb->pad3));
1602
1603 sb->utime = cpu_to_le64((__u64)mddev->utime);
1604 sb->events = cpu_to_le64(mddev->events);
1605 if (mddev->in_sync)
1606 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1607 else
1608 sb->resync_offset = cpu_to_le64(0);
1609
NeilBrown1c05b4b2006-10-21 10:24:08 -07001610 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001611
NeilBrownf0ca3402006-02-02 14:28:04 -08001612 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001613 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001614 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001615 sb->level = cpu_to_le32(mddev->level);
1616 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001617
NeilBrownc3d97142009-12-14 12:49:52 +11001618 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1619 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001620 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001621 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001622
1623 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001624 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001625 sb->feature_map |=
1626 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1627 sb->recovery_offset =
1628 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001629 }
1630
NeilBrownf6705572006-03-27 01:18:11 -08001631 if (mddev->reshape_position != MaxSector) {
1632 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1633 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1634 sb->new_layout = cpu_to_le32(mddev->new_layout);
1635 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1636 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001637 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001638 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001641 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (rdev2->desc_nr+1 > max_dev)
1643 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001644
NeilBrown70471da2009-08-03 10:59:57 +10001645 if (max_dev > le32_to_cpu(sb->max_dev)) {
1646 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001647 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001648 rdev->sb_size = max_dev * 2 + 256;
1649 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1650 if (rdev->sb_size & bmask)
1651 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001652 } else
1653 max_dev = le32_to_cpu(sb->max_dev);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 for (i=0; i<max_dev;i++)
1656 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1657
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001658 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001660 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001662 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001664 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001665 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 else
1667 sb->dev_roles[i] = cpu_to_le16(0xffff);
1668 }
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 sb->sb_csum = calc_sb_1_csum(sb);
1671}
1672
Chris Webb0cd17fe2008-06-28 08:31:46 +10001673static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001674super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001675{
1676 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001677 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001678 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001679 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001680 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001681 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001682 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001683 max_sectors -= rdev->data_offset;
1684 if (!num_sectors || num_sectors > max_sectors)
1685 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001686 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001687 /* minor version 0 with bitmap we can't move */
1688 return 0;
1689 } else {
1690 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001691 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001692 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001693 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001694 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001695 if (!num_sectors || num_sectors > max_sectors)
1696 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001697 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001698 }
1699 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001700 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001701 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001702 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001703 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001704 rdev->sb_page);
1705 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001706 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Adrian Bunk75c96f82005-05-05 16:16:09 -07001709static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 [0] = {
1711 .name = "0.90.0",
1712 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001713 .load_super = super_90_load,
1714 .validate_super = super_90_validate,
1715 .sync_super = super_90_sync,
1716 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 },
1718 [1] = {
1719 .name = "md-1",
1720 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001721 .load_super = super_1_load,
1722 .validate_super = super_1_validate,
1723 .sync_super = super_1_sync,
1724 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 },
1726};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1729{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001730 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
NeilBrown4b809912008-07-21 17:05:25 +10001732 rcu_read_lock();
1733 rdev_for_each_rcu(rdev, mddev1)
1734 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001735 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001736 rdev2->bdev->bd_contains) {
1737 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001738 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001739 }
1740 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return 0;
1742}
1743
1744static LIST_HEAD(pending_raid_disks);
1745
Andre Nollac5e7112009-08-03 10:59:47 +10001746/*
1747 * Try to register data integrity profile for an mddev
1748 *
1749 * This is called when an array is started and after a disk has been kicked
1750 * from the array. It only succeeds if all working and active component devices
1751 * are integrity capable with matching profiles.
1752 */
1753int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001754{
Andre Nollac5e7112009-08-03 10:59:47 +10001755 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001756
Andre Nollac5e7112009-08-03 10:59:47 +10001757 if (list_empty(&mddev->disks))
1758 return 0; /* nothing to do */
1759 if (blk_get_integrity(mddev->gendisk))
1760 return 0; /* already registered */
1761 list_for_each_entry(rdev, &mddev->disks, same_set) {
1762 /* skip spares and non-functional disks */
1763 if (test_bit(Faulty, &rdev->flags))
1764 continue;
1765 if (rdev->raid_disk < 0)
1766 continue;
1767 /*
1768 * If at least one rdev is not integrity capable, we can not
1769 * enable data integrity for the md device.
1770 */
1771 if (!bdev_get_integrity(rdev->bdev))
1772 return -EINVAL;
1773 if (!reference) {
1774 /* Use the first rdev as the reference */
1775 reference = rdev;
1776 continue;
1777 }
1778 /* does this rdev's profile match the reference profile? */
1779 if (blk_integrity_compare(reference->bdev->bd_disk,
1780 rdev->bdev->bd_disk) < 0)
1781 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001782 }
Andre Nollac5e7112009-08-03 10:59:47 +10001783 /*
1784 * All component devices are integrity capable and have matching
1785 * profiles, register the common profile for the md device.
1786 */
1787 if (blk_integrity_register(mddev->gendisk,
1788 bdev_get_integrity(reference->bdev)) != 0) {
1789 printk(KERN_ERR "md: failed to register integrity for %s\n",
1790 mdname(mddev));
1791 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001792 }
Andre Nollac5e7112009-08-03 10:59:47 +10001793 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1794 mdname(mddev));
1795 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001796}
Andre Nollac5e7112009-08-03 10:59:47 +10001797EXPORT_SYMBOL(md_integrity_register);
1798
1799/* Disable data integrity if non-capable/non-matching disk is being added */
1800void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1801{
1802 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1803 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1804
1805 if (!bi_mddev) /* nothing to do */
1806 return;
1807 if (rdev->raid_disk < 0) /* skip spares */
1808 return;
1809 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1810 rdev->bdev->bd_disk) >= 0)
1811 return;
1812 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1813 blk_integrity_unregister(mddev->gendisk);
1814}
1815EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1818{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001819 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001820 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001821 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001822 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (rdev->mddev) {
1825 MD_BUG();
1826 return -EINVAL;
1827 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001828
1829 /* prevent duplicates */
1830 if (find_rdev(mddev, rdev->bdev->bd_dev))
1831 return -EEXIST;
1832
Andre Nolldd8ac332009-03-31 14:33:13 +11001833 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1834 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1835 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001836 if (mddev->pers) {
1837 /* Cannot change size, so fail
1838 * If mddev->level <= 0, then we don't care
1839 * about aligning sizes (e.g. linear)
1840 */
1841 if (mddev->level > 0)
1842 return -ENOSPC;
1843 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001844 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /* Verify rdev->desc_nr is unique.
1848 * If it is -1, assign a free number, else
1849 * check number is not in use
1850 */
1851 if (rdev->desc_nr < 0) {
1852 int choice = 0;
1853 if (mddev->pers) choice = mddev->raid_disks;
1854 while (find_rdev_nr(mddev, choice))
1855 choice++;
1856 rdev->desc_nr = choice;
1857 } else {
1858 if (find_rdev_nr(mddev, rdev->desc_nr))
1859 return -EBUSY;
1860 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001861 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1862 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1863 mdname(mddev), mddev->max_disks);
1864 return -EBUSY;
1865 }
NeilBrown19133a42005-11-08 21:39:35 -08001866 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001867 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001868 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001871 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001872
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001873 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001874 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001875
Tejun Heo0762b8b2008-08-25 19:56:12 +09001876 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001877 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1878 /* failure here is OK */;
1879 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001880
NeilBrown4b809912008-07-21 17:05:25 +10001881 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001882 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001883
1884 /* May as well allow recovery to be retried once */
1885 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001888
1889 fail:
1890 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1891 b, mdname(mddev));
1892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
NeilBrown177a99b2008-02-06 01:39:56 -08001895static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001896{
1897 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1898 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001899 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1903{
1904 char b[BDEVNAME_SIZE];
1905 if (!rdev->mddev) {
1906 MD_BUG();
1907 return;
1908 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001909 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001910 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1912 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001913 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001914 sysfs_put(rdev->sysfs_state);
1915 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001916 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001917 * writing to 'remove' to "dev/state". We also need
1918 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001919 */
NeilBrown4b809912008-07-21 17:05:25 +10001920 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001921 INIT_WORK(&rdev->del_work, md_delayed_delete);
1922 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001923 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
1926/*
1927 * prevent the device from being mounted, repartitioned or
1928 * otherwise reused by a RAID array (or any other kernel
1929 * subsystem), by bd_claiming the device.
1930 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001931static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int err = 0;
1934 struct block_device *bdev;
1935 char b[BDEVNAME_SIZE];
1936
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001937 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (IS_ERR(bdev)) {
1939 printk(KERN_ERR "md: could not open %s.\n",
1940 __bdevname(dev, b));
1941 return PTR_ERR(bdev);
1942 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001943 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (err) {
1945 printk(KERN_ERR "md: could not bd_claim %s.\n",
1946 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001947 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return err;
1949 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001950 if (!shared)
1951 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 rdev->bdev = bdev;
1953 return err;
1954}
1955
1956static void unlock_rdev(mdk_rdev_t *rdev)
1957{
1958 struct block_device *bdev = rdev->bdev;
1959 rdev->bdev = NULL;
1960 if (!bdev)
1961 MD_BUG();
1962 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001963 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966void md_autodetect_dev(dev_t dev);
1967
1968static void export_rdev(mdk_rdev_t * rdev)
1969{
1970 char b[BDEVNAME_SIZE];
1971 printk(KERN_INFO "md: export_rdev(%s)\n",
1972 bdevname(rdev->bdev,b));
1973 if (rdev->mddev)
1974 MD_BUG();
1975 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001977 if (test_bit(AutoDetected, &rdev->flags))
1978 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979#endif
1980 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001981 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984static void kick_rdev_from_array(mdk_rdev_t * rdev)
1985{
1986 unbind_rdev_from_array(rdev);
1987 export_rdev(rdev);
1988}
1989
1990static void export_array(mddev_t *mddev)
1991{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001992 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
NeilBrownd089c6a2008-02-06 01:39:59 -08001994 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (!rdev->mddev) {
1996 MD_BUG();
1997 continue;
1998 }
1999 kick_rdev_from_array(rdev);
2000 }
2001 if (!list_empty(&mddev->disks))
2002 MD_BUG();
2003 mddev->raid_disks = 0;
2004 mddev->major_version = 0;
2005}
2006
2007static void print_desc(mdp_disk_t *desc)
2008{
2009 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2010 desc->major,desc->minor,desc->raid_disk,desc->state);
2011}
2012
Cheng Renquancd2ac932009-01-09 08:31:08 +11002013static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 int i;
2016
2017 printk(KERN_INFO
2018 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2019 sb->major_version, sb->minor_version, sb->patch_version,
2020 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2021 sb->ctime);
2022 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2023 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2024 sb->md_minor, sb->layout, sb->chunk_size);
2025 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2026 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2027 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2028 sb->failed_disks, sb->spare_disks,
2029 sb->sb_csum, (unsigned long)sb->events_lo);
2030
2031 printk(KERN_INFO);
2032 for (i = 0; i < MD_SB_DISKS; i++) {
2033 mdp_disk_t *desc;
2034
2035 desc = sb->disks + i;
2036 if (desc->number || desc->major || desc->minor ||
2037 desc->raid_disk || (desc->state && (desc->state != 4))) {
2038 printk(" D %2d: ", i);
2039 print_desc(desc);
2040 }
2041 }
2042 printk(KERN_INFO "md: THIS: ");
2043 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Cheng Renquancd2ac932009-01-09 08:31:08 +11002046static void print_sb_1(struct mdp_superblock_1 *sb)
2047{
2048 __u8 *uuid;
2049
2050 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002051 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002052 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002053 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002054 le32_to_cpu(sb->major_version),
2055 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002056 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002057 sb->set_name,
2058 (unsigned long long)le64_to_cpu(sb->ctime)
2059 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2060
2061 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002062 printk(KERN_INFO
2063 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002064 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002065 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002066 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2067 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002068 le32_to_cpu(sb->level),
2069 (unsigned long long)le64_to_cpu(sb->size),
2070 le32_to_cpu(sb->raid_disks),
2071 le32_to_cpu(sb->layout),
2072 le32_to_cpu(sb->chunksize),
2073 (unsigned long long)le64_to_cpu(sb->data_offset),
2074 (unsigned long long)le64_to_cpu(sb->data_size),
2075 (unsigned long long)le64_to_cpu(sb->super_offset),
2076 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2077 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002078 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002079 sb->devflags,
2080 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2081 (unsigned long long)le64_to_cpu(sb->events),
2082 (unsigned long long)le64_to_cpu(sb->resync_offset),
2083 le32_to_cpu(sb->sb_csum),
2084 le32_to_cpu(sb->max_dev)
2085 );
2086}
2087
2088static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
2090 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002091 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2092 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002093 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2094 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002096 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2097 switch (major_version) {
2098 case 0:
2099 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2100 break;
2101 case 1:
2102 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2103 break;
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 } else
2106 printk(KERN_INFO "md: no rdev superblock!\n");
2107}
2108
Adrian Bunk5e563412006-06-26 00:27:42 -07002109static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002111 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 mdk_rdev_t *rdev;
2113 mddev_t *mddev;
2114 char b[BDEVNAME_SIZE];
2115
2116 printk("\n");
2117 printk("md: **********************************\n");
2118 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2119 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002120 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
NeilBrown32a76272005-06-21 17:17:14 -07002122 if (mddev->bitmap)
2123 bitmap_print_sb(mddev->bitmap);
2124 else
2125 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002126 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 printk("<%s>", bdevname(rdev->bdev,b));
2128 printk("\n");
2129
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002130 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002131 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 printk("md: **********************************\n");
2134 printk("\n");
2135}
2136
2137
NeilBrown42543762006-06-26 00:27:57 -07002138static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
NeilBrown42543762006-06-26 00:27:57 -07002140 /* Update each superblock (in-memory image), but
2141 * if we are allowed to, skip spares which already
2142 * have the right event counter, or have one earlier
2143 * (which would mean they aren't being marked as dirty
2144 * with the rest of the array)
2145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002147 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002148 if (rdev->sb_events == mddev->events ||
2149 (nospares &&
2150 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002151 rdev->sb_events+1 == mddev->events)) {
2152 /* Don't update this superblock */
2153 rdev->sb_loaded = 2;
2154 } else {
2155 super_types[mddev->major_version].
2156 sync_super(mddev, rdev);
2157 rdev->sb_loaded = 1;
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160}
2161
NeilBrown850b2b42006-10-03 01:15:46 -07002162static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002165 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002166 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002169 /* First make sure individual recovery_offsets are correct */
2170 list_for_each_entry(rdev, &mddev->disks, same_set) {
2171 if (rdev->raid_disk >= 0 &&
2172 mddev->delta_disks >= 0 &&
2173 !test_bit(In_sync, &rdev->flags) &&
2174 mddev->curr_resync_completed > rdev->recovery_offset)
2175 rdev->recovery_offset = mddev->curr_resync_completed;
2176
2177 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002178 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002179 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002180 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41dc2010-10-28 17:30:20 +11002181 if (!mddev->external)
2182 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002183 wake_up(&mddev->sb_wait);
2184 return;
2185 }
2186
NeilBrowna9701a32005-11-08 21:39:34 -08002187 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002188
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002189 mddev->utime = get_seconds();
2190
NeilBrown850b2b42006-10-03 01:15:46 -07002191 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2192 force_change = 1;
2193 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2194 /* just a clean<-> dirty transition, possibly leave spares alone,
2195 * though if events isn't the right even/odd, we will have to do
2196 * spares after all
2197 */
2198 nospares = 1;
2199 if (force_change)
2200 nospares = 0;
2201 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002202 /* If the array is degraded, then skipping spares is both
2203 * dangerous and fairly pointless.
2204 * Dangerous because a device that was removed from the array
2205 * might have a event_count that still looks up-to-date,
2206 * so it can be re-added without a resync.
2207 * Pointless because if there are any spares to skip,
2208 * then a recovery will happen and soon that array won't
2209 * be degraded any more and the spare can go back to sleep then.
2210 */
NeilBrown850b2b42006-10-03 01:15:46 -07002211 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002212
NeilBrown06d91a52005-06-21 17:17:12 -07002213 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002214
2215 /* If this is just a dirty<->clean transition, and the array is clean
2216 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002217 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002218 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002219 && mddev->can_decrease_events
2220 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002221 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002222 mddev->can_decrease_events = 0;
2223 } else {
NeilBrown42543762006-06-26 00:27:57 -07002224 /* otherwise we have to go forward and ... */
2225 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002226 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if (!mddev->events) {
2230 /*
2231 * oops, this 64-bit counter should never wrap.
2232 * Either we are in around ~1 trillion A.C., assuming
2233 * 1 reboot per second, or we have a bug:
2234 */
2235 MD_BUG();
2236 mddev->events --;
2237 }
NeilBrowne6910632008-02-06 01:39:51 -08002238 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002239 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 dprintk(KERN_INFO
2242 "md: updating %s RAID superblock on device (in sync %d)\n",
2243 mdname(mddev),mddev->in_sync);
2244
NeilBrown4ad13662007-07-17 04:06:13 -07002245 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002246 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 char b[BDEVNAME_SIZE];
2248 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002249 if (rdev->sb_loaded != 1)
2250 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002251 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 dprintk("(skipping faulty ");
2253
2254 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002255 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002256 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002257 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002258 rdev->sb_page);
2259 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2260 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002261 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002262 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 } else
2265 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002266 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* only need to write one superblock... */
2268 break;
2269 }
NeilBrowna9701a32005-11-08 21:39:34 -08002270 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002271 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002272
NeilBrowna9701a32005-11-08 21:39:34 -08002273 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002274 if (mddev->in_sync != sync_req ||
2275 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002276 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002277 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002278 goto repeat;
2279 }
NeilBrown850b2b42006-10-03 01:15:46 -07002280 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002281 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002282 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002283 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2284 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Andre Noll7f6ce762008-03-23 18:34:54 +01002288/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002289 * We want to accept with case. For this we use cmd_match.
2290 */
2291static int cmd_match(const char *cmd, const char *str)
2292{
2293 /* See if cmd, written into a sysfs file, matches
2294 * str. They must either be the same, or cmd can
2295 * have a trailing newline
2296 */
2297 while (*cmd && *str && *cmd == *str) {
2298 cmd++;
2299 str++;
2300 }
2301 if (*cmd == '\n')
2302 cmd++;
2303 if (*str || *cmd)
2304 return 0;
2305 return 1;
2306}
2307
NeilBrown86e6ffd2005-11-08 21:39:24 -08002308struct rdev_sysfs_entry {
2309 struct attribute attr;
2310 ssize_t (*show)(mdk_rdev_t *, char *);
2311 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2312};
2313
2314static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002315state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002316{
2317 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002318 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002319
NeilBrownb2d444d2005-11-08 21:39:31 -08002320 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002321 len+= sprintf(page+len, "%sfaulty",sep);
2322 sep = ",";
2323 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002324 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002325 len += sprintf(page+len, "%sin_sync",sep);
2326 sep = ",";
2327 }
NeilBrownf6556752006-06-26 00:28:01 -07002328 if (test_bit(WriteMostly, &rdev->flags)) {
2329 len += sprintf(page+len, "%swrite_mostly",sep);
2330 sep = ",";
2331 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002332 if (test_bit(Blocked, &rdev->flags)) {
2333 len += sprintf(page+len, "%sblocked", sep);
2334 sep = ",";
2335 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002336 if (!test_bit(Faulty, &rdev->flags) &&
2337 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002338 len += sprintf(page+len, "%sspare", sep);
2339 sep = ",";
2340 }
2341 return len+sprintf(page+len, "\n");
2342}
2343
NeilBrown45dc2de2006-06-26 00:27:58 -07002344static ssize_t
2345state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2346{
2347 /* can write
2348 * faulty - simulates and error
2349 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002350 * writemostly - sets write_mostly
2351 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002352 * blocked - sets the Blocked flag
2353 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002354 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002355 */
2356 int err = -EINVAL;
2357 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2358 md_error(rdev->mddev, rdev);
2359 err = 0;
2360 } else if (cmd_match(buf, "remove")) {
2361 if (rdev->raid_disk >= 0)
2362 err = -EBUSY;
2363 else {
2364 mddev_t *mddev = rdev->mddev;
2365 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002366 if (mddev->pers)
2367 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002368 md_new_event(mddev);
2369 err = 0;
2370 }
NeilBrownf6556752006-06-26 00:28:01 -07002371 } else if (cmd_match(buf, "writemostly")) {
2372 set_bit(WriteMostly, &rdev->flags);
2373 err = 0;
2374 } else if (cmd_match(buf, "-writemostly")) {
2375 clear_bit(WriteMostly, &rdev->flags);
2376 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002377 } else if (cmd_match(buf, "blocked")) {
2378 set_bit(Blocked, &rdev->flags);
2379 err = 0;
2380 } else if (cmd_match(buf, "-blocked")) {
2381 clear_bit(Blocked, &rdev->flags);
2382 wake_up(&rdev->blocked_wait);
2383 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2384 md_wakeup_thread(rdev->mddev->thread);
2385
2386 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002387 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2388 set_bit(In_sync, &rdev->flags);
2389 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002390 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002391 if (!err)
2392 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002393 return err ? err : len;
2394}
NeilBrown80ca3a42006-07-10 04:44:18 -07002395static struct rdev_sysfs_entry rdev_state =
2396__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002397
2398static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002399errors_show(mdk_rdev_t *rdev, char *page)
2400{
2401 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2402}
2403
2404static ssize_t
2405errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2406{
2407 char *e;
2408 unsigned long n = simple_strtoul(buf, &e, 10);
2409 if (*buf && (*e == 0 || *e == '\n')) {
2410 atomic_set(&rdev->corrected_errors, n);
2411 return len;
2412 }
2413 return -EINVAL;
2414}
2415static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002416__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002417
NeilBrown014236d2006-01-06 00:20:55 -08002418static ssize_t
2419slot_show(mdk_rdev_t *rdev, char *page)
2420{
2421 if (rdev->raid_disk < 0)
2422 return sprintf(page, "none\n");
2423 else
2424 return sprintf(page, "%d\n", rdev->raid_disk);
2425}
2426
2427static ssize_t
2428slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2429{
2430 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002431 int err;
2432 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002433 int slot = simple_strtoul(buf, &e, 10);
2434 if (strncmp(buf, "none", 4)==0)
2435 slot = -1;
2436 else if (e==buf || (*e && *e!= '\n'))
2437 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002438 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002439 /* Setting 'slot' on an active array requires also
2440 * updating the 'rd%d' link, and communicating
2441 * with the personality with ->hot_*_disk.
2442 * For now we only support removing
2443 * failed/spare devices. This normally happens automatically,
2444 * but not when the metadata is externally managed.
2445 */
NeilBrownc303da62008-02-06 01:39:51 -08002446 if (rdev->raid_disk == -1)
2447 return -EEXIST;
2448 /* personality does all needed checks */
2449 if (rdev->mddev->pers->hot_add_disk == NULL)
2450 return -EINVAL;
2451 err = rdev->mddev->pers->
2452 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2453 if (err)
2454 return err;
2455 sprintf(nm, "rd%d", rdev->raid_disk);
2456 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002457 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002458 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2459 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002460 } else if (rdev->mddev->pers) {
2461 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002462 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002463 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002464 */
2465
2466 if (rdev->raid_disk != -1)
2467 return -EBUSY;
2468
2469 if (rdev->mddev->pers->hot_add_disk == NULL)
2470 return -EINVAL;
2471
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002472 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002473 if (rdev2->raid_disk == slot)
2474 return -EEXIST;
2475
2476 rdev->raid_disk = slot;
2477 if (test_bit(In_sync, &rdev->flags))
2478 rdev->saved_raid_disk = slot;
2479 else
2480 rdev->saved_raid_disk = -1;
2481 err = rdev->mddev->pers->
2482 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002483 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002484 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002485 return err;
Neil Brown52664732008-06-28 08:31:44 +10002486 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002487 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002488 sprintf(nm, "rd%d", rdev->raid_disk);
2489 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002490 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002491 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002492 } else {
2493 if (slot >= rdev->mddev->raid_disks)
2494 return -ENOSPC;
2495 rdev->raid_disk = slot;
2496 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002497 clear_bit(Faulty, &rdev->flags);
2498 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002499 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002500 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002501 }
NeilBrown014236d2006-01-06 00:20:55 -08002502 return len;
2503}
2504
2505
2506static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002507__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002508
NeilBrown93c8cad2006-01-06 00:20:56 -08002509static ssize_t
2510offset_show(mdk_rdev_t *rdev, char *page)
2511{
Andrew Morton6961ece2006-01-06 00:20:59 -08002512 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002513}
2514
2515static ssize_t
2516offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2517{
2518 char *e;
2519 unsigned long long offset = simple_strtoull(buf, &e, 10);
2520 if (e==buf || (*e && *e != '\n'))
2521 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002522 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002523 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002524 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002525 /* Must set offset before size, so overlap checks
2526 * can be sane */
2527 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002528 rdev->data_offset = offset;
2529 return len;
2530}
2531
2532static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002533__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002534
NeilBrown83303b62006-01-06 00:21:06 -08002535static ssize_t
2536rdev_size_show(mdk_rdev_t *rdev, char *page)
2537{
Andre Nolldd8ac332009-03-31 14:33:13 +11002538 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002539}
2540
NeilBrownc5d79ad2008-02-06 01:39:54 -08002541static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2542{
2543 /* check if two start/length pairs overlap */
2544 if (s1+l1 <= s2)
2545 return 0;
2546 if (s2+l2 <= s1)
2547 return 0;
2548 return 1;
2549}
2550
Dan Williamsb522adc2009-03-31 15:00:31 +11002551static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2552{
2553 unsigned long long blocks;
2554 sector_t new;
2555
2556 if (strict_strtoull(buf, 10, &blocks) < 0)
2557 return -EINVAL;
2558
2559 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2560 return -EINVAL; /* sector conversion overflow */
2561
2562 new = blocks * 2;
2563 if (new != blocks * 2)
2564 return -EINVAL; /* unsigned long long to sector_t overflow */
2565
2566 *sectors = new;
2567 return 0;
2568}
2569
NeilBrown83303b62006-01-06 00:21:06 -08002570static ssize_t
2571rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2572{
NeilBrown27c529b2008-03-04 14:29:33 -08002573 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002574 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002575 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002576
Dan Williamsb522adc2009-03-31 15:00:31 +11002577 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002578 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002579 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002580 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002581 sectors = super_types[my_mddev->major_version].
2582 rdev_size_change(rdev, sectors);
2583 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002584 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002585 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002586 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002587 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002588 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002589 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002590 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002591
Andre Nolldd8ac332009-03-31 14:33:13 +11002592 rdev->sectors = sectors;
2593 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002594 /* need to check that all other rdevs with the same ->bdev
2595 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002596 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002597 * we have to change it back, we will have the lock again.
2598 */
2599 mddev_t *mddev;
2600 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002601 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002602
NeilBrown27c529b2008-03-04 14:29:33 -08002603 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002604 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002605 mdk_rdev_t *rdev2;
2606
2607 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002608 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002609 if (test_bit(AllReserved, &rdev2->flags) ||
2610 (rdev->bdev == rdev2->bdev &&
2611 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002612 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002613 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002614 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002615 overlap = 1;
2616 break;
2617 }
2618 mddev_unlock(mddev);
2619 if (overlap) {
2620 mddev_put(mddev);
2621 break;
2622 }
2623 }
NeilBrown27c529b2008-03-04 14:29:33 -08002624 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002625 if (overlap) {
2626 /* Someone else could have slipped in a size
2627 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002628 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002629 * safe, and trust userspace not to race with
2630 * itself
2631 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002632 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002633 return -EBUSY;
2634 }
2635 }
NeilBrown83303b62006-01-06 00:21:06 -08002636 return len;
2637}
2638
2639static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002640__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002641
Dan Williams06e3c812009-12-12 21:17:12 -07002642
2643static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2644{
2645 unsigned long long recovery_start = rdev->recovery_offset;
2646
2647 if (test_bit(In_sync, &rdev->flags) ||
2648 recovery_start == MaxSector)
2649 return sprintf(page, "none\n");
2650
2651 return sprintf(page, "%llu\n", recovery_start);
2652}
2653
2654static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2655{
2656 unsigned long long recovery_start;
2657
2658 if (cmd_match(buf, "none"))
2659 recovery_start = MaxSector;
2660 else if (strict_strtoull(buf, 10, &recovery_start))
2661 return -EINVAL;
2662
2663 if (rdev->mddev->pers &&
2664 rdev->raid_disk >= 0)
2665 return -EBUSY;
2666
2667 rdev->recovery_offset = recovery_start;
2668 if (recovery_start == MaxSector)
2669 set_bit(In_sync, &rdev->flags);
2670 else
2671 clear_bit(In_sync, &rdev->flags);
2672 return len;
2673}
2674
2675static struct rdev_sysfs_entry rdev_recovery_start =
2676__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2677
NeilBrown86e6ffd2005-11-08 21:39:24 -08002678static struct attribute *rdev_default_attrs[] = {
2679 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002680 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002681 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002682 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002683 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002684 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002685 NULL,
2686};
2687static ssize_t
2688rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2689{
2690 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2691 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002692 mddev_t *mddev = rdev->mddev;
2693 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002694
2695 if (!entry->show)
2696 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002697
2698 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2699 if (!rv) {
2700 if (rdev->mddev == NULL)
2701 rv = -EBUSY;
2702 else
2703 rv = entry->show(rdev, page);
2704 mddev_unlock(mddev);
2705 }
2706 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002707}
2708
2709static ssize_t
2710rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2711 const char *page, size_t length)
2712{
2713 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2714 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002715 ssize_t rv;
2716 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002717
2718 if (!entry->store)
2719 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002720 if (!capable(CAP_SYS_ADMIN))
2721 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002722 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002723 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002724 if (rdev->mddev == NULL)
2725 rv = -EBUSY;
2726 else
2727 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002728 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002729 }
2730 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002731}
2732
2733static void rdev_free(struct kobject *ko)
2734{
2735 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2736 kfree(rdev);
2737}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002738static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002739 .show = rdev_attr_show,
2740 .store = rdev_attr_store,
2741};
2742static struct kobj_type rdev_ktype = {
2743 .release = rdev_free,
2744 .sysfs_ops = &rdev_sysfs_ops,
2745 .default_attrs = rdev_default_attrs,
2746};
2747
NeilBrowne8bb9a82010-06-01 19:37:26 +10002748void md_rdev_init(mdk_rdev_t *rdev)
2749{
2750 rdev->desc_nr = -1;
2751 rdev->saved_raid_disk = -1;
2752 rdev->raid_disk = -1;
2753 rdev->flags = 0;
2754 rdev->data_offset = 0;
2755 rdev->sb_events = 0;
2756 rdev->last_read_error.tv_sec = 0;
2757 rdev->last_read_error.tv_nsec = 0;
2758 atomic_set(&rdev->nr_pending, 0);
2759 atomic_set(&rdev->read_errors, 0);
2760 atomic_set(&rdev->corrected_errors, 0);
2761
2762 INIT_LIST_HEAD(&rdev->same_set);
2763 init_waitqueue_head(&rdev->blocked_wait);
2764}
2765EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766/*
2767 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2768 *
2769 * mark the device faulty if:
2770 *
2771 * - the device is nonexistent (zero size)
2772 * - the device has no valid superblock
2773 *
2774 * a faulty rdev _never_ has rdev->sb set.
2775 */
2776static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2777{
2778 char b[BDEVNAME_SIZE];
2779 int err;
2780 mdk_rdev_t *rdev;
2781 sector_t size;
2782
NeilBrown9ffae0c2006-01-06 00:20:32 -08002783 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (!rdev) {
2785 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2786 return ERR_PTR(-ENOMEM);
2787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
NeilBrowne8bb9a82010-06-01 19:37:26 +10002789 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if ((err = alloc_disk_sb(rdev)))
2791 goto abort_free;
2792
NeilBrownc5d79ad2008-02-06 01:39:54 -08002793 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (err)
2795 goto abort_free;
2796
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002797 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002798
Mike Snitzer77304d22010-11-08 14:39:12 +01002799 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (!size) {
2801 printk(KERN_WARNING
2802 "md: %s has zero or unknown size, marking faulty!\n",
2803 bdevname(rdev->bdev,b));
2804 err = -EINVAL;
2805 goto abort_free;
2806 }
2807
2808 if (super_format >= 0) {
2809 err = super_types[super_format].
2810 load_super(rdev, NULL, super_minor);
2811 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002812 printk(KERN_WARNING
2813 "md: %s does not have a valid v%d.%d "
2814 "superblock, not importing!\n",
2815 bdevname(rdev->bdev,b),
2816 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 goto abort_free;
2818 }
2819 if (err < 0) {
2820 printk(KERN_WARNING
2821 "md: could not read %s's sb, not importing!\n",
2822 bdevname(rdev->bdev,b));
2823 goto abort_free;
2824 }
2825 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return rdev;
2828
2829abort_free:
2830 if (rdev->sb_page) {
2831 if (rdev->bdev)
2832 unlock_rdev(rdev);
2833 free_disk_sb(rdev);
2834 }
2835 kfree(rdev);
2836 return ERR_PTR(err);
2837}
2838
2839/*
2840 * Check a full RAID array for plausibility
2841 */
2842
2843
NeilBrowna757e642005-04-16 15:26:42 -07002844static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
2846 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002847 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 char b[BDEVNAME_SIZE];
2849
2850 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002851 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 switch (super_types[mddev->major_version].
2853 load_super(rdev, freshest, mddev->minor_version)) {
2854 case 1:
2855 freshest = rdev;
2856 break;
2857 case 0:
2858 break;
2859 default:
2860 printk( KERN_ERR \
2861 "md: fatal superblock inconsistency in %s"
2862 " -- removing from array\n",
2863 bdevname(rdev->bdev,b));
2864 kick_rdev_from_array(rdev);
2865 }
2866
2867
2868 super_types[mddev->major_version].
2869 validate_super(mddev, freshest);
2870
2871 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002872 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002873 if (mddev->max_disks &&
2874 (rdev->desc_nr >= mddev->max_disks ||
2875 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002876 printk(KERN_WARNING
2877 "md: %s: %s: only %d devices permitted\n",
2878 mdname(mddev), bdevname(rdev->bdev, b),
2879 mddev->max_disks);
2880 kick_rdev_from_array(rdev);
2881 continue;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (rdev != freshest)
2884 if (super_types[mddev->major_version].
2885 validate_super(mddev, rdev)) {
2886 printk(KERN_WARNING "md: kicking non-fresh %s"
2887 " from array!\n",
2888 bdevname(rdev->bdev,b));
2889 kick_rdev_from_array(rdev);
2890 continue;
2891 }
2892 if (mddev->level == LEVEL_MULTIPATH) {
2893 rdev->desc_nr = i++;
2894 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002895 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002896 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002897 rdev->raid_disk = -1;
2898 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
2902
NeilBrown72e02072009-12-14 12:49:55 +11002903/* Read a fixed-point number.
2904 * Numbers in sysfs attributes should be in "standard" units where
2905 * possible, so time should be in seconds.
2906 * However we internally use a a much smaller unit such as
2907 * milliseconds or jiffies.
2908 * This function takes a decimal number with a possible fractional
2909 * component, and produces an integer which is the result of
2910 * multiplying that number by 10^'scale'.
2911 * all without any floating-point arithmetic.
2912 */
2913int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2914{
2915 unsigned long result = 0;
2916 long decimals = -1;
2917 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2918 if (*cp == '.')
2919 decimals = 0;
2920 else if (decimals < scale) {
2921 unsigned int value;
2922 value = *cp - '0';
2923 result = result * 10 + value;
2924 if (decimals >= 0)
2925 decimals++;
2926 }
2927 cp++;
2928 }
2929 if (*cp == '\n')
2930 cp++;
2931 if (*cp)
2932 return -EINVAL;
2933 if (decimals < 0)
2934 decimals = 0;
2935 while (decimals < scale) {
2936 result *= 10;
2937 decimals ++;
2938 }
2939 *res = result;
2940 return 0;
2941}
2942
2943
NeilBrown19052c02008-08-05 15:54:13 +10002944static void md_safemode_timeout(unsigned long data);
2945
NeilBrowneae17012005-11-08 21:39:23 -08002946static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002947safe_delay_show(mddev_t *mddev, char *page)
2948{
2949 int msec = (mddev->safemode_delay*1000)/HZ;
2950 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2951}
2952static ssize_t
2953safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2954{
NeilBrown16f17b32006-06-26 00:27:37 -07002955 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002956
NeilBrown72e02072009-12-14 12:49:55 +11002957 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002958 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002959 if (msec == 0)
2960 mddev->safemode_delay = 0;
2961 else {
NeilBrown19052c02008-08-05 15:54:13 +10002962 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002963 mddev->safemode_delay = (msec*HZ)/1000;
2964 if (mddev->safemode_delay == 0)
2965 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002966 if (mddev->safemode_delay < old_delay)
2967 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002968 }
2969 return len;
2970}
2971static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002972__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002973
2974static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002975level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002976{
NeilBrown2604b702006-01-06 00:20:36 -08002977 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002978 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002979 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002980 else if (mddev->clevel[0])
2981 return sprintf(page, "%s\n", mddev->clevel);
2982 else if (mddev->level != LEVEL_NONE)
2983 return sprintf(page, "%d\n", mddev->level);
2984 else
2985 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002986}
2987
NeilBrownd9d166c2006-01-06 00:20:51 -08002988static ssize_t
2989level_store(mddev_t *mddev, const char *buf, size_t len)
2990{
Dan Williamsf2859af2010-05-02 10:04:16 -07002991 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002992 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002993 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002994 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002995 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002996 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002997
2998 if (mddev->pers == NULL) {
2999 if (len == 0)
3000 return 0;
3001 if (len >= sizeof(mddev->clevel))
3002 return -ENOSPC;
3003 strncpy(mddev->clevel, buf, len);
3004 if (mddev->clevel[len-1] == '\n')
3005 len--;
3006 mddev->clevel[len] = 0;
3007 mddev->level = LEVEL_NONE;
3008 return rv;
3009 }
3010
3011 /* request to change the personality. Need to ensure:
3012 * - array is not engaged in resync/recovery/reshape
3013 * - old personality can be suspended
3014 * - new personality will access other array.
3015 */
3016
NeilBrownbb4f1e92010-08-08 21:18:03 +10003017 if (mddev->sync_thread ||
3018 mddev->reshape_position != MaxSector ||
3019 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003020 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003021
3022 if (!mddev->pers->quiesce) {
3023 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3024 mdname(mddev), mddev->pers->name);
3025 return -EINVAL;
3026 }
3027
3028 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003029 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003030 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003031 strncpy(clevel, buf, len);
3032 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003033 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003034 clevel[len] = 0;
3035 if (strict_strtol(clevel, 10, &level))
3036 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003037
Dan Williamsf2859af2010-05-02 10:04:16 -07003038 if (request_module("md-%s", clevel) != 0)
3039 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003040 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003041 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003042 if (!pers || !try_module_get(pers->owner)) {
3043 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003044 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003045 return -EINVAL;
3046 }
3047 spin_unlock(&pers_lock);
3048
3049 if (pers == mddev->pers) {
3050 /* Nothing to do! */
3051 module_put(pers->owner);
3052 return rv;
3053 }
3054 if (!pers->takeover) {
3055 module_put(pers->owner);
3056 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003057 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003058 return -EINVAL;
3059 }
3060
NeilBrowne93f68a2010-06-15 09:36:03 +01003061 list_for_each_entry(rdev, &mddev->disks, same_set)
3062 rdev->new_raid_disk = rdev->raid_disk;
3063
NeilBrown245f46c2009-03-31 14:39:39 +11003064 /* ->takeover must set new_* and/or delta_disks
3065 * if it succeeds, and may set them when it fails.
3066 */
3067 priv = pers->takeover(mddev);
3068 if (IS_ERR(priv)) {
3069 mddev->new_level = mddev->level;
3070 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003071 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003072 mddev->raid_disks -= mddev->delta_disks;
3073 mddev->delta_disks = 0;
3074 module_put(pers->owner);
3075 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003076 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003077 return PTR_ERR(priv);
3078 }
3079
3080 /* Looks like we have a winner */
3081 mddev_suspend(mddev);
3082 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003083
3084 if (mddev->pers->sync_request == NULL &&
3085 pers->sync_request != NULL) {
3086 /* need to add the md_redundancy_group */
3087 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3088 printk(KERN_WARNING
3089 "md: cannot register extra attributes for %s\n",
3090 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003091 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003092 }
3093 if (mddev->pers->sync_request != NULL &&
3094 pers->sync_request == NULL) {
3095 /* need to remove the md_redundancy_group */
3096 if (mddev->to_remove == NULL)
3097 mddev->to_remove = &md_redundancy_group;
3098 }
3099
Trela Maciej54071b32010-03-08 16:02:42 +11003100 if (mddev->pers->sync_request == NULL &&
3101 mddev->external) {
3102 /* We are converting from a no-redundancy array
3103 * to a redundancy array and metadata is managed
3104 * externally so we need to be sure that writes
3105 * won't block due to a need to transition
3106 * clean->dirty
3107 * until external management is started.
3108 */
3109 mddev->in_sync = 0;
3110 mddev->safemode_delay = 0;
3111 mddev->safemode = 0;
3112 }
3113
NeilBrowne93f68a2010-06-15 09:36:03 +01003114 list_for_each_entry(rdev, &mddev->disks, same_set) {
3115 char nm[20];
3116 if (rdev->raid_disk < 0)
3117 continue;
3118 if (rdev->new_raid_disk > mddev->raid_disks)
3119 rdev->new_raid_disk = -1;
3120 if (rdev->new_raid_disk == rdev->raid_disk)
3121 continue;
3122 sprintf(nm, "rd%d", rdev->raid_disk);
3123 sysfs_remove_link(&mddev->kobj, nm);
3124 }
3125 list_for_each_entry(rdev, &mddev->disks, same_set) {
3126 if (rdev->raid_disk < 0)
3127 continue;
3128 if (rdev->new_raid_disk == rdev->raid_disk)
3129 continue;
3130 rdev->raid_disk = rdev->new_raid_disk;
3131 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003132 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003133 else {
3134 char nm[20];
3135 sprintf(nm, "rd%d", rdev->raid_disk);
3136 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3137 printk("md: cannot register %s for %s after level change\n",
3138 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003139 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003140 }
3141
3142 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003143 mddev->pers = pers;
3144 mddev->private = priv;
3145 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3146 mddev->level = mddev->new_level;
3147 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003148 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003149 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003150 if (mddev->pers->sync_request == NULL) {
3151 /* this is now an array without redundancy, so
3152 * it must always be in_sync
3153 */
3154 mddev->in_sync = 1;
3155 del_timer_sync(&mddev->safemode_timer);
3156 }
NeilBrown245f46c2009-03-31 14:39:39 +11003157 pers->run(mddev);
3158 mddev_resume(mddev);
3159 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3160 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3161 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003162 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003163 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003164 return rv;
3165}
3166
3167static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003168__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003169
NeilBrownd4dbd022006-06-26 00:27:59 -07003170
3171static ssize_t
3172layout_show(mddev_t *mddev, char *page)
3173{
3174 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003175 if (mddev->reshape_position != MaxSector &&
3176 mddev->layout != mddev->new_layout)
3177 return sprintf(page, "%d (%d)\n",
3178 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003179 return sprintf(page, "%d\n", mddev->layout);
3180}
3181
3182static ssize_t
3183layout_store(mddev_t *mddev, const char *buf, size_t len)
3184{
3185 char *e;
3186 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003187
3188 if (!*buf || (*e && *e != '\n'))
3189 return -EINVAL;
3190
NeilBrownb3546032009-03-31 14:56:41 +11003191 if (mddev->pers) {
3192 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003193 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003194 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003195 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003196 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003197 if (err) {
3198 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003199 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003200 }
NeilBrownb3546032009-03-31 14:56:41 +11003201 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003202 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003203 if (mddev->reshape_position == MaxSector)
3204 mddev->layout = n;
3205 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003206 return len;
3207}
3208static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003209__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003210
3211
NeilBrowneae17012005-11-08 21:39:23 -08003212static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003213raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003214{
NeilBrownbb636542005-11-08 21:39:45 -08003215 if (mddev->raid_disks == 0)
3216 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003217 if (mddev->reshape_position != MaxSector &&
3218 mddev->delta_disks != 0)
3219 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3220 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003221 return sprintf(page, "%d\n", mddev->raid_disks);
3222}
3223
NeilBrownda943b992006-01-06 00:20:54 -08003224static int update_raid_disks(mddev_t *mddev, int raid_disks);
3225
3226static ssize_t
3227raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3228{
NeilBrownda943b992006-01-06 00:20:54 -08003229 char *e;
3230 int rv = 0;
3231 unsigned long n = simple_strtoul(buf, &e, 10);
3232
3233 if (!*buf || (*e && *e != '\n'))
3234 return -EINVAL;
3235
3236 if (mddev->pers)
3237 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003238 else if (mddev->reshape_position != MaxSector) {
3239 int olddisks = mddev->raid_disks - mddev->delta_disks;
3240 mddev->delta_disks = n - olddisks;
3241 mddev->raid_disks = n;
3242 } else
NeilBrownda943b992006-01-06 00:20:54 -08003243 mddev->raid_disks = n;
3244 return rv ? rv : len;
3245}
3246static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003247__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003248
NeilBrown24dd4692005-11-08 21:39:26 -08003249static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003250chunk_size_show(mddev_t *mddev, char *page)
3251{
NeilBrown08a02ec2007-05-09 02:35:38 -07003252 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003253 mddev->chunk_sectors != mddev->new_chunk_sectors)
3254 return sprintf(page, "%d (%d)\n",
3255 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003256 mddev->chunk_sectors << 9);
3257 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003258}
3259
3260static ssize_t
3261chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3262{
NeilBrown3b343802006-01-06 00:20:47 -08003263 char *e;
3264 unsigned long n = simple_strtoul(buf, &e, 10);
3265
NeilBrown3b343802006-01-06 00:20:47 -08003266 if (!*buf || (*e && *e != '\n'))
3267 return -EINVAL;
3268
NeilBrownb3546032009-03-31 14:56:41 +11003269 if (mddev->pers) {
3270 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003271 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003272 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003273 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003274 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003275 if (err) {
3276 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003277 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003278 }
NeilBrownb3546032009-03-31 14:56:41 +11003279 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003280 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003281 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003282 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003283 }
NeilBrown3b343802006-01-06 00:20:47 -08003284 return len;
3285}
3286static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003287__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003288
NeilBrowna94213b2006-06-26 00:28:00 -07003289static ssize_t
3290resync_start_show(mddev_t *mddev, char *page)
3291{
NeilBrownd1a7c502009-03-31 15:24:32 +11003292 if (mddev->recovery_cp == MaxSector)
3293 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003294 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3295}
3296
3297static ssize_t
3298resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3299{
NeilBrowna94213b2006-06-26 00:28:00 -07003300 char *e;
3301 unsigned long long n = simple_strtoull(buf, &e, 10);
3302
3303 if (mddev->pers)
3304 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003305 if (cmd_match(buf, "none"))
3306 n = MaxSector;
3307 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003308 return -EINVAL;
3309
3310 mddev->recovery_cp = n;
3311 return len;
3312}
3313static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003314__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003315
NeilBrown9e653b62006-06-26 00:27:58 -07003316/*
3317 * The array state can be:
3318 *
3319 * clear
3320 * No devices, no size, no level
3321 * Equivalent to STOP_ARRAY ioctl
3322 * inactive
3323 * May have some settings, but array is not active
3324 * all IO results in error
3325 * When written, doesn't tear down array, but just stops it
3326 * suspended (not supported yet)
3327 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003328 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003329 * readonly
3330 * no resync can happen. no superblocks get written.
3331 * write requests fail
3332 * read-auto
3333 * like readonly, but behaves like 'clean' on a write request.
3334 *
3335 * clean - no pending writes, but otherwise active.
3336 * When written to inactive array, starts without resync
3337 * If a write request arrives then
3338 * if metadata is known, mark 'dirty' and switch to 'active'.
3339 * if not known, block and switch to write-pending
3340 * If written to an active array that has pending writes, then fails.
3341 * active
3342 * fully active: IO and resync can be happening.
3343 * When written to inactive array, starts with resync
3344 *
3345 * write-pending
3346 * clean, but writes are blocked waiting for 'active' to be written.
3347 *
3348 * active-idle
3349 * like active, but no writes have been seen for a while (100msec).
3350 *
3351 */
3352enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3353 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003354static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003355 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3356 "write-pending", "active-idle", NULL };
3357
3358static int match_word(const char *word, char **list)
3359{
3360 int n;
3361 for (n=0; list[n]; n++)
3362 if (cmd_match(word, list[n]))
3363 break;
3364 return n;
3365}
3366
3367static ssize_t
3368array_state_show(mddev_t *mddev, char *page)
3369{
3370 enum array_state st = inactive;
3371
3372 if (mddev->pers)
3373 switch(mddev->ro) {
3374 case 1:
3375 st = readonly;
3376 break;
3377 case 2:
3378 st = read_auto;
3379 break;
3380 case 0:
3381 if (mddev->in_sync)
3382 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003383 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003384 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003385 else if (mddev->safemode)
3386 st = active_idle;
3387 else
3388 st = active;
3389 }
3390 else {
3391 if (list_empty(&mddev->disks) &&
3392 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003393 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003394 st = clear;
3395 else
3396 st = inactive;
3397 }
3398 return sprintf(page, "%s\n", array_states[st]);
3399}
3400
Neil Browndf5b20c2008-07-11 22:02:22 +10003401static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003402static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003403static int do_md_run(mddev_t * mddev);
3404static int restart_array(mddev_t *mddev);
3405
3406static ssize_t
3407array_state_store(mddev_t *mddev, const char *buf, size_t len)
3408{
3409 int err = -EINVAL;
3410 enum array_state st = match_word(buf, array_states);
3411 switch(st) {
3412 case bad_word:
3413 break;
3414 case clear:
3415 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003416 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003417 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003418 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003419 break;
3420 case inactive:
3421 /* stopping an active array */
3422 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003423 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003424 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003425 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003426 } else
3427 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003428 break;
3429 case suspended:
3430 break; /* not supported yet */
3431 case readonly:
3432 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003433 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003434 else {
3435 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003436 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003437 err = do_md_run(mddev);
3438 }
3439 break;
3440 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003441 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003442 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003443 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003444 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003445 err = restart_array(mddev);
3446 if (err == 0) {
3447 mddev->ro = 2;
3448 set_disk_ro(mddev->gendisk, 0);
3449 }
NeilBrown9e653b62006-06-26 00:27:58 -07003450 } else {
3451 mddev->ro = 2;
3452 err = do_md_run(mddev);
3453 }
3454 break;
3455 case clean:
3456 if (mddev->pers) {
3457 restart_array(mddev);
3458 spin_lock_irq(&mddev->write_lock);
3459 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003460 if (mddev->in_sync == 0) {
3461 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003462 if (mddev->safemode == 1)
3463 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003464 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003465 }
3466 err = 0;
3467 } else
3468 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003469 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003470 } else
3471 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003472 break;
3473 case active:
3474 if (mddev->pers) {
3475 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003476 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003477 wake_up(&mddev->sb_wait);
3478 err = 0;
3479 } else {
3480 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003481 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003482 err = do_md_run(mddev);
3483 }
3484 break;
3485 case write_pending:
3486 case active_idle:
3487 /* these cannot be set */
3488 break;
3489 }
3490 if (err)
3491 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003492 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003493 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003494 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003495 }
NeilBrown9e653b62006-06-26 00:27:58 -07003496}
NeilBrown80ca3a42006-07-10 04:44:18 -07003497static struct md_sysfs_entry md_array_state =
3498__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003499
NeilBrown6d7ff732006-01-06 00:21:16 -08003500static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003501max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3502 return sprintf(page, "%d\n",
3503 atomic_read(&mddev->max_corr_read_errors));
3504}
3505
3506static ssize_t
3507max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3508{
3509 char *e;
3510 unsigned long n = simple_strtoul(buf, &e, 10);
3511
3512 if (*buf && (*e == 0 || *e == '\n')) {
3513 atomic_set(&mddev->max_corr_read_errors, n);
3514 return len;
3515 }
3516 return -EINVAL;
3517}
3518
3519static struct md_sysfs_entry max_corr_read_errors =
3520__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3521 max_corrected_read_errors_store);
3522
3523static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003524null_show(mddev_t *mddev, char *page)
3525{
3526 return -EINVAL;
3527}
3528
3529static ssize_t
3530new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3531{
3532 /* buf must be %d:%d\n? giving major and minor numbers */
3533 /* The new device is added to the array.
3534 * If the array has a persistent superblock, we read the
3535 * superblock to initialise info and check validity.
3536 * Otherwise, only checking done is that in bind_rdev_to_array,
3537 * which mainly checks size.
3538 */
3539 char *e;
3540 int major = simple_strtoul(buf, &e, 10);
3541 int minor;
3542 dev_t dev;
3543 mdk_rdev_t *rdev;
3544 int err;
3545
3546 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3547 return -EINVAL;
3548 minor = simple_strtoul(e+1, &e, 10);
3549 if (*e && *e != '\n')
3550 return -EINVAL;
3551 dev = MKDEV(major, minor);
3552 if (major != MAJOR(dev) ||
3553 minor != MINOR(dev))
3554 return -EOVERFLOW;
3555
3556
3557 if (mddev->persistent) {
3558 rdev = md_import_device(dev, mddev->major_version,
3559 mddev->minor_version);
3560 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3561 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3562 mdk_rdev_t, same_set);
3563 err = super_types[mddev->major_version]
3564 .load_super(rdev, rdev0, mddev->minor_version);
3565 if (err < 0)
3566 goto out;
3567 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003568 } else if (mddev->external)
3569 rdev = md_import_device(dev, -2, -1);
3570 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003571 rdev = md_import_device(dev, -1, -1);
3572
3573 if (IS_ERR(rdev))
3574 return PTR_ERR(rdev);
3575 err = bind_rdev_to_array(rdev, mddev);
3576 out:
3577 if (err)
3578 export_rdev(rdev);
3579 return err ? err : len;
3580}
3581
3582static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003583__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003584
3585static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003586bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3587{
3588 char *end;
3589 unsigned long chunk, end_chunk;
3590
3591 if (!mddev->bitmap)
3592 goto out;
3593 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3594 while (*buf) {
3595 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3596 if (buf == end) break;
3597 if (*end == '-') { /* range */
3598 buf = end + 1;
3599 end_chunk = simple_strtoul(buf, &end, 0);
3600 if (buf == end) break;
3601 }
3602 if (*end && !isspace(*end)) break;
3603 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003604 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003605 }
3606 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3607out:
3608 return len;
3609}
3610
3611static struct md_sysfs_entry md_bitmap =
3612__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3613
3614static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003615size_show(mddev_t *mddev, char *page)
3616{
Andre Noll58c0fed2009-03-31 14:33:13 +11003617 return sprintf(page, "%llu\n",
3618 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003619}
3620
Andre Nolld71f9f82008-07-11 22:02:22 +10003621static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003622
3623static ssize_t
3624size_store(mddev_t *mddev, const char *buf, size_t len)
3625{
3626 /* If array is inactive, we can reduce the component size, but
3627 * not increase it (except from 0).
3628 * If array is active, we can try an on-line resize
3629 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003630 sector_t sectors;
3631 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003632
Andre Noll58c0fed2009-03-31 14:33:13 +11003633 if (err < 0)
3634 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003635 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003636 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003637 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003638 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003639 if (mddev->dev_sectors == 0 ||
3640 mddev->dev_sectors > sectors)
3641 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003642 else
3643 err = -ENOSPC;
3644 }
3645 return err ? err : len;
3646}
3647
3648static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003649__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003650
NeilBrown8bb93aa2006-01-06 00:20:50 -08003651
3652/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003653 * This is one of
3654 * 'none' for arrays with no metadata (good luck...)
3655 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003656 * or N.M for internally known formats
3657 */
3658static ssize_t
3659metadata_show(mddev_t *mddev, char *page)
3660{
3661 if (mddev->persistent)
3662 return sprintf(page, "%d.%d\n",
3663 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003664 else if (mddev->external)
3665 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003666 else
3667 return sprintf(page, "none\n");
3668}
3669
3670static ssize_t
3671metadata_store(mddev_t *mddev, const char *buf, size_t len)
3672{
3673 int major, minor;
3674 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003675 /* Changing the details of 'external' metadata is
3676 * always permitted. Otherwise there must be
3677 * no devices attached to the array.
3678 */
3679 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3680 ;
3681 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003682 return -EBUSY;
3683
3684 if (cmd_match(buf, "none")) {
3685 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003686 mddev->external = 0;
3687 mddev->major_version = 0;
3688 mddev->minor_version = 90;
3689 return len;
3690 }
3691 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003692 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003693 if (namelen >= sizeof(mddev->metadata_type))
3694 namelen = sizeof(mddev->metadata_type)-1;
3695 strncpy(mddev->metadata_type, buf+9, namelen);
3696 mddev->metadata_type[namelen] = 0;
3697 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3698 mddev->metadata_type[--namelen] = 0;
3699 mddev->persistent = 0;
3700 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003701 mddev->major_version = 0;
3702 mddev->minor_version = 90;
3703 return len;
3704 }
3705 major = simple_strtoul(buf, &e, 10);
3706 if (e==buf || *e != '.')
3707 return -EINVAL;
3708 buf = e+1;
3709 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003710 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003711 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003712 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003713 return -ENOENT;
3714 mddev->major_version = major;
3715 mddev->minor_version = minor;
3716 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003717 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003718 return len;
3719}
3720
3721static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003722__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003723
NeilBrowna35b0d62006-01-06 00:20:49 -08003724static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003725action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003726{
NeilBrown7eec3142005-11-08 21:39:44 -08003727 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003728 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3729 type = "frozen";
3730 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003731 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003732 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3733 type = "reshape";
3734 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003735 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3736 type = "resync";
3737 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3738 type = "check";
3739 else
3740 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003741 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003742 type = "recover";
3743 }
3744 return sprintf(page, "%s\n", type);
3745}
3746
3747static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003748action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003749{
NeilBrown7eec3142005-11-08 21:39:44 -08003750 if (!mddev->pers || !mddev->pers->sync_request)
3751 return -EINVAL;
3752
NeilBrownb6a9ce62009-05-26 09:41:17 +10003753 if (cmd_match(page, "frozen"))
3754 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3755 else
3756 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3757
3758 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003759 if (mddev->sync_thread) {
3760 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3761 md_unregister_thread(mddev->sync_thread);
3762 mddev->sync_thread = NULL;
3763 mddev->recovery = 0;
3764 }
NeilBrown03c902e2006-01-06 00:20:46 -08003765 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3766 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003767 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003768 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003769 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003770 else if (cmd_match(page, "recover")) {
3771 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3772 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3773 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003774 int err;
3775 if (mddev->pers->start_reshape == NULL)
3776 return -EINVAL;
3777 err = mddev->pers->start_reshape(mddev);
3778 if (err)
3779 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003780 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003781 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003782 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003783 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003784 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003785 return -EINVAL;
3786 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3787 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003788 }
NeilBrown03c902e2006-01-06 00:20:46 -08003789 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003790 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003791 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003792 return len;
3793}
3794
NeilBrown9d888832005-11-08 21:39:26 -08003795static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003796mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003797{
3798 return sprintf(page, "%llu\n",
3799 (unsigned long long) mddev->resync_mismatches);
3800}
3801
NeilBrown80ca3a42006-07-10 04:44:18 -07003802static struct md_sysfs_entry md_scan_mode =
3803__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003804
NeilBrown96de1e62005-11-08 21:39:39 -08003805
NeilBrown80ca3a42006-07-10 04:44:18 -07003806static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003807
NeilBrown88202a02006-01-06 00:21:36 -08003808static ssize_t
3809sync_min_show(mddev_t *mddev, char *page)
3810{
3811 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3812 mddev->sync_speed_min ? "local": "system");
3813}
3814
3815static ssize_t
3816sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3817{
3818 int min;
3819 char *e;
3820 if (strncmp(buf, "system", 6)==0) {
3821 mddev->sync_speed_min = 0;
3822 return len;
3823 }
3824 min = simple_strtoul(buf, &e, 10);
3825 if (buf == e || (*e && *e != '\n') || min <= 0)
3826 return -EINVAL;
3827 mddev->sync_speed_min = min;
3828 return len;
3829}
3830
3831static struct md_sysfs_entry md_sync_min =
3832__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3833
3834static ssize_t
3835sync_max_show(mddev_t *mddev, char *page)
3836{
3837 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3838 mddev->sync_speed_max ? "local": "system");
3839}
3840
3841static ssize_t
3842sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3843{
3844 int max;
3845 char *e;
3846 if (strncmp(buf, "system", 6)==0) {
3847 mddev->sync_speed_max = 0;
3848 return len;
3849 }
3850 max = simple_strtoul(buf, &e, 10);
3851 if (buf == e || (*e && *e != '\n') || max <= 0)
3852 return -EINVAL;
3853 mddev->sync_speed_max = max;
3854 return len;
3855}
3856
3857static struct md_sysfs_entry md_sync_max =
3858__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3859
Iustin Popd7f3d292007-10-16 23:30:54 -07003860static ssize_t
3861degraded_show(mddev_t *mddev, char *page)
3862{
3863 return sprintf(page, "%d\n", mddev->degraded);
3864}
3865static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003866
3867static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003868sync_force_parallel_show(mddev_t *mddev, char *page)
3869{
3870 return sprintf(page, "%d\n", mddev->parallel_resync);
3871}
3872
3873static ssize_t
3874sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3875{
3876 long n;
3877
3878 if (strict_strtol(buf, 10, &n))
3879 return -EINVAL;
3880
3881 if (n != 0 && n != 1)
3882 return -EINVAL;
3883
3884 mddev->parallel_resync = n;
3885
3886 if (mddev->sync_thread)
3887 wake_up(&resync_wait);
3888
3889 return len;
3890}
3891
3892/* force parallel resync, even with shared block devices */
3893static struct md_sysfs_entry md_sync_force_parallel =
3894__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3895 sync_force_parallel_show, sync_force_parallel_store);
3896
3897static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003898sync_speed_show(mddev_t *mddev, char *page)
3899{
3900 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003901 if (mddev->curr_resync == 0)
3902 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003903 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3904 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003905 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003906 db = resync - mddev->resync_mark_cnt;
3907 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003908}
3909
NeilBrown80ca3a42006-07-10 04:44:18 -07003910static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003911
3912static ssize_t
3913sync_completed_show(mddev_t *mddev, char *page)
3914{
Andre Noll58c0fed2009-03-31 14:33:13 +11003915 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003916
NeilBrownacb180b2009-04-14 16:28:34 +10003917 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3918 return sprintf(page, "none\n");
3919
NeilBrown88202a02006-01-06 00:21:36 -08003920 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003921 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003922 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003923 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003924
NeilBrownacb180b2009-04-14 16:28:34 +10003925 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003926 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003927}
3928
NeilBrown80ca3a42006-07-10 04:44:18 -07003929static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003930
NeilBrowne464eaf2006-03-27 01:18:14 -08003931static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003932min_sync_show(mddev_t *mddev, char *page)
3933{
3934 return sprintf(page, "%llu\n",
3935 (unsigned long long)mddev->resync_min);
3936}
3937static ssize_t
3938min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3939{
3940 unsigned long long min;
3941 if (strict_strtoull(buf, 10, &min))
3942 return -EINVAL;
3943 if (min > mddev->resync_max)
3944 return -EINVAL;
3945 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3946 return -EBUSY;
3947
3948 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003949 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003950 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003951 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003952 return -EINVAL;
3953 }
3954 mddev->resync_min = min;
3955
3956 return len;
3957}
3958
3959static struct md_sysfs_entry md_min_sync =
3960__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3961
3962static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003963max_sync_show(mddev_t *mddev, char *page)
3964{
3965 if (mddev->resync_max == MaxSector)
3966 return sprintf(page, "max\n");
3967 else
3968 return sprintf(page, "%llu\n",
3969 (unsigned long long)mddev->resync_max);
3970}
3971static ssize_t
3972max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3973{
3974 if (strncmp(buf, "max", 3) == 0)
3975 mddev->resync_max = MaxSector;
3976 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003977 unsigned long long max;
3978 if (strict_strtoull(buf, 10, &max))
3979 return -EINVAL;
3980 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003981 return -EINVAL;
3982 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003983 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003984 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3985 return -EBUSY;
3986
3987 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003988 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003989 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003990 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003991 return -EINVAL;
3992 }
3993 mddev->resync_max = max;
3994 }
3995 wake_up(&mddev->recovery_wait);
3996 return len;
3997}
3998
3999static struct md_sysfs_entry md_max_sync =
4000__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4001
4002static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004003suspend_lo_show(mddev_t *mddev, char *page)
4004{
4005 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4006}
4007
4008static ssize_t
4009suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4010{
4011 char *e;
4012 unsigned long long new = simple_strtoull(buf, &e, 10);
4013
NeilBrownb8d966e2009-07-01 11:14:04 +10004014 if (mddev->pers == NULL ||
4015 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004016 return -EINVAL;
4017 if (buf == e || (*e && *e != '\n'))
4018 return -EINVAL;
4019 if (new >= mddev->suspend_hi ||
4020 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
4021 mddev->suspend_lo = new;
4022 mddev->pers->quiesce(mddev, 2);
4023 return len;
4024 } else
4025 return -EINVAL;
4026}
4027static struct md_sysfs_entry md_suspend_lo =
4028__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4029
4030
4031static ssize_t
4032suspend_hi_show(mddev_t *mddev, char *page)
4033{
4034 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4035}
4036
4037static ssize_t
4038suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4039{
4040 char *e;
4041 unsigned long long new = simple_strtoull(buf, &e, 10);
4042
NeilBrownb8d966e2009-07-01 11:14:04 +10004043 if (mddev->pers == NULL ||
4044 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004045 return -EINVAL;
4046 if (buf == e || (*e && *e != '\n'))
4047 return -EINVAL;
4048 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
4049 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
4050 mddev->suspend_hi = new;
4051 mddev->pers->quiesce(mddev, 1);
4052 mddev->pers->quiesce(mddev, 0);
4053 return len;
4054 } else
4055 return -EINVAL;
4056}
4057static struct md_sysfs_entry md_suspend_hi =
4058__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4059
NeilBrown08a02ec2007-05-09 02:35:38 -07004060static ssize_t
4061reshape_position_show(mddev_t *mddev, char *page)
4062{
4063 if (mddev->reshape_position != MaxSector)
4064 return sprintf(page, "%llu\n",
4065 (unsigned long long)mddev->reshape_position);
4066 strcpy(page, "none\n");
4067 return 5;
4068}
4069
4070static ssize_t
4071reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4072{
4073 char *e;
4074 unsigned long long new = simple_strtoull(buf, &e, 10);
4075 if (mddev->pers)
4076 return -EBUSY;
4077 if (buf == e || (*e && *e != '\n'))
4078 return -EINVAL;
4079 mddev->reshape_position = new;
4080 mddev->delta_disks = 0;
4081 mddev->new_level = mddev->level;
4082 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004083 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004084 return len;
4085}
4086
4087static struct md_sysfs_entry md_reshape_position =
4088__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4089 reshape_position_store);
4090
Dan Williamsb522adc2009-03-31 15:00:31 +11004091static ssize_t
4092array_size_show(mddev_t *mddev, char *page)
4093{
4094 if (mddev->external_size)
4095 return sprintf(page, "%llu\n",
4096 (unsigned long long)mddev->array_sectors/2);
4097 else
4098 return sprintf(page, "default\n");
4099}
4100
4101static ssize_t
4102array_size_store(mddev_t *mddev, const char *buf, size_t len)
4103{
4104 sector_t sectors;
4105
4106 if (strncmp(buf, "default", 7) == 0) {
4107 if (mddev->pers)
4108 sectors = mddev->pers->size(mddev, 0, 0);
4109 else
4110 sectors = mddev->array_sectors;
4111
4112 mddev->external_size = 0;
4113 } else {
4114 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4115 return -EINVAL;
4116 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004117 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004118
4119 mddev->external_size = 1;
4120 }
4121
4122 mddev->array_sectors = sectors;
4123 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004124 if (mddev->pers)
4125 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004126
4127 return len;
4128}
4129
4130static struct md_sysfs_entry md_array_size =
4131__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4132 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004133
NeilBrowneae17012005-11-08 21:39:23 -08004134static struct attribute *md_default_attrs[] = {
4135 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004136 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004137 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004138 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004139 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004140 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004141 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004142 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004143 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004144 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004145 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004146 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004147 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004148 NULL,
4149};
4150
4151static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004152 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004153 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004154 &md_sync_min.attr,
4155 &md_sync_max.attr,
4156 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004157 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004158 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004159 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004160 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004161 &md_suspend_lo.attr,
4162 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004163 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004164 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004165 NULL,
4166};
NeilBrown411036f2005-11-08 21:39:40 -08004167static struct attribute_group md_redundancy_group = {
4168 .name = NULL,
4169 .attrs = md_redundancy_attrs,
4170};
4171
NeilBrowneae17012005-11-08 21:39:23 -08004172
4173static ssize_t
4174md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4175{
4176 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4177 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004178 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004179
4180 if (!entry->show)
4181 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004182 rv = mddev_lock(mddev);
4183 if (!rv) {
4184 rv = entry->show(mddev, page);
4185 mddev_unlock(mddev);
4186 }
NeilBrown96de1e62005-11-08 21:39:39 -08004187 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004188}
4189
4190static ssize_t
4191md_attr_store(struct kobject *kobj, struct attribute *attr,
4192 const char *page, size_t length)
4193{
4194 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4195 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004196 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004197
4198 if (!entry->store)
4199 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004200 if (!capable(CAP_SYS_ADMIN))
4201 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004202 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004203 if (mddev->hold_active == UNTIL_IOCTL)
4204 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004205 if (!rv) {
4206 rv = entry->store(mddev, page, length);
4207 mddev_unlock(mddev);
4208 }
NeilBrown96de1e62005-11-08 21:39:39 -08004209 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004210}
4211
4212static void md_free(struct kobject *ko)
4213{
4214 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004215
4216 if (mddev->sysfs_state)
4217 sysfs_put(mddev->sysfs_state);
4218
4219 if (mddev->gendisk) {
4220 del_gendisk(mddev->gendisk);
4221 put_disk(mddev->gendisk);
4222 }
4223 if (mddev->queue)
4224 blk_cleanup_queue(mddev->queue);
4225
NeilBrowneae17012005-11-08 21:39:23 -08004226 kfree(mddev);
4227}
4228
Emese Revfy52cf25d2010-01-19 02:58:23 +01004229static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004230 .show = md_attr_show,
4231 .store = md_attr_store,
4232};
4233static struct kobj_type md_ktype = {
4234 .release = md_free,
4235 .sysfs_ops = &md_sysfs_ops,
4236 .default_attrs = md_default_attrs,
4237};
4238
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239int mdp_major = 0;
4240
Dan Williams5fd3a172009-03-04 00:57:25 -07004241static void mddev_delayed_delete(struct work_struct *ws)
4242{
4243 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4244
NeilBrown43a70502009-12-14 12:49:55 +11004245 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004246 kobject_del(&mddev->kobj);
4247 kobject_put(&mddev->kobj);
4248}
4249
NeilBrownefeb53c2009-01-09 08:31:10 +11004250static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004252 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 mddev_t *mddev = mddev_find(dev);
4254 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004255 int partitioned;
4256 int shift;
4257 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004258 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004261 return -ENODEV;
4262
4263 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4264 shift = partitioned ? MdpMinorShift : 0;
4265 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Tejun Heoe804ac72010-10-15 15:36:08 +02004267 /* wait for any previous instance of this device to be
4268 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004269 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004270 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004271
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004272 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004273 error = -EEXIST;
4274 if (mddev->gendisk)
4275 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004276
4277 if (name) {
4278 /* Need to ensure that 'name' is not a duplicate.
4279 */
4280 mddev_t *mddev2;
4281 spin_lock(&all_mddevs_lock);
4282
4283 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4284 if (mddev2->gendisk &&
4285 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4286 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004287 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004288 }
4289 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 }
NeilBrown8b765392009-01-09 08:31:08 +11004291
NeilBrown0909dc42009-07-01 12:27:21 +10004292 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004293 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004294 if (!mddev->queue)
4295 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004296 mddev->queue->queuedata = mddev;
4297
NeilBrown8b765392009-01-09 08:31:08 +11004298 /* Can be unlocked because the queue is new: no concurrency */
4299 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4300
NeilBrown409c57f2009-03-31 14:39:39 +11004301 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 disk = alloc_disk(1 << shift);
4304 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004305 blk_cleanup_queue(mddev->queue);
4306 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004307 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004309 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004311 if (name)
4312 strcpy(disk->disk_name, name);
4313 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004315 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 disk->fops = &md_fops;
4318 disk->private_data = mddev;
4319 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004320 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004321 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004322 * remove it now.
4323 */
4324 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 add_disk(disk);
4326 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004327 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4328 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004329 if (error) {
4330 /* This isn't possible, but as kobject_init_and_add is marked
4331 * __must_check, we must do something with the result
4332 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004333 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4334 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004335 error = 0;
4336 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004337 if (mddev->kobj.sd &&
4338 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004339 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004340
4341 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004342 abort:
4343 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004344 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004345 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004346 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004347 }
NeilBrownd3374822009-01-09 08:31:10 +11004348 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004349 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004350}
4351
4352static struct kobject *md_probe(dev_t dev, int *part, void *data)
4353{
4354 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return NULL;
4356}
4357
NeilBrownefeb53c2009-01-09 08:31:10 +11004358static int add_named_array(const char *val, struct kernel_param *kp)
4359{
4360 /* val must be "md_*" where * is not all digits.
4361 * We allocate an array with a large free minor number, and
4362 * set the name to val. val must not already be an active name.
4363 */
4364 int len = strlen(val);
4365 char buf[DISK_NAME_LEN];
4366
4367 while (len && val[len-1] == '\n')
4368 len--;
4369 if (len >= DISK_NAME_LEN)
4370 return -E2BIG;
4371 strlcpy(buf, val, len+1);
4372 if (strncmp(buf, "md_", 3) != 0)
4373 return -EINVAL;
4374 return md_alloc(0, buf);
4375}
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static void md_safemode_timeout(unsigned long data)
4378{
4379 mddev_t *mddev = (mddev_t *) data;
4380
Neil Brown0fd62b82008-06-28 08:31:36 +10004381 if (!atomic_read(&mddev->writes_pending)) {
4382 mddev->safemode = 1;
4383 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004384 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 md_wakeup_thread(mddev->thread);
4387}
4388
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004389static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
NeilBrown390ee602010-06-01 19:37:27 +10004391int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
NeilBrown2604b702006-01-06 00:20:36 -08004393 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004395 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
NeilBrowna757e642005-04-16 15:26:42 -07004397 if (list_empty(&mddev->disks))
4398 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 if (mddev->pers)
4402 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004403 /* Cannot run until previous stop completes properly */
4404 if (mddev->sysfs_active)
4405 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 /*
4408 * Analyze all RAID superblock(s)
4409 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004410 if (!mddev->raid_disks) {
4411 if (!mddev->persistent)
4412 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004413 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
NeilBrownd9d166c2006-01-06 00:20:51 -08004416 if (mddev->level != LEVEL_NONE)
4417 request_module("md-level-%d", mddev->level);
4418 else if (mddev->clevel[0])
4419 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 /*
4422 * Drop all container device buffers, from now on
4423 * the only valid external interface is through the md
4424 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004426 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004427 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 continue;
4429 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004430 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004431
4432 /* perform some consistency tests on the device.
4433 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004434 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004435 */
Andre Noll0f420352008-07-11 22:02:23 +10004436 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004437 if (mddev->dev_sectors &&
4438 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004439 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004440 printk("md: %s: data overlaps metadata\n",
4441 mdname(mddev));
4442 return -EINVAL;
4443 }
4444 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004445 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004446 > rdev->data_offset) {
4447 printk("md: %s: metadata overlaps data\n",
4448 mdname(mddev));
4449 return -EINVAL;
4450 }
4451 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004452 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454
NeilBrowna167f662010-10-26 18:31:13 +11004455 if (mddev->bio_set == NULL)
4456 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004459 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004460 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004462 if (mddev->level != LEVEL_NONE)
4463 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4464 mddev->level);
4465 else
4466 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4467 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return -EINVAL;
4469 }
NeilBrown2604b702006-01-06 00:20:36 -08004470 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004472 if (mddev->level != pers->level) {
4473 mddev->level = pers->level;
4474 mddev->new_level = pers->level;
4475 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004476 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
NeilBrownf6705572006-03-27 01:18:11 -08004478 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004479 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004480 /* This personality cannot handle reshaping... */
4481 mddev->pers = NULL;
4482 module_put(pers->owner);
4483 return -EINVAL;
4484 }
4485
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004486 if (pers->sync_request) {
4487 /* Warn if this is a potentially silly
4488 * configuration.
4489 */
4490 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4491 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004492 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004493
4494 list_for_each_entry(rdev, &mddev->disks, same_set)
4495 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004496 if (rdev < rdev2 &&
4497 rdev->bdev->bd_contains ==
4498 rdev2->bdev->bd_contains) {
4499 printk(KERN_WARNING
4500 "%s: WARNING: %s appears to be"
4501 " on the same physical disk as"
4502 " %s.\n",
4503 mdname(mddev),
4504 bdevname(rdev->bdev,b),
4505 bdevname(rdev2->bdev,b2));
4506 warned = 1;
4507 }
4508 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004509
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004510 if (warned)
4511 printk(KERN_WARNING
4512 "True protection against single-disk"
4513 " failure might be compromised.\n");
4514 }
4515
NeilBrown657390d2005-08-26 18:34:16 -07004516 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004517 /* may be over-ridden by personality */
4518 mddev->resync_max_sectors = mddev->dev_sectors;
4519
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004520 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
NeilBrown0f9552b2009-12-30 12:08:50 +11004522 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004523 mddev->ro = 2; /* read-only, but switch on first write */
4524
NeilBrownb15c2e52006-01-06 00:20:16 -08004525 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004526 if (err)
4527 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004528 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4529 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4530 " but 'external_size' not in effect?\n", __func__);
4531 printk(KERN_ERR
4532 "md: invalid array_size %llu > default size %llu\n",
4533 (unsigned long long)mddev->array_sectors / 2,
4534 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4535 err = -EINVAL;
4536 mddev->pers->stop(mddev);
4537 }
4538 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004539 err = bitmap_create(mddev);
4540 if (err) {
4541 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4542 mdname(mddev), err);
4543 mddev->pers->stop(mddev);
4544 }
4545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 module_put(mddev->pers->owner);
4548 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004549 bitmap_destroy(mddev);
4550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004552 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004553 if (mddev->kobj.sd &&
4554 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004555 printk(KERN_WARNING
4556 "md: cannot register extra attributes for %s\n",
4557 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004558 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004559 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004560 mddev->ro = 0;
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004563 atomic_set(&mddev->max_corr_read_errors,
4564 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 mddev->safemode = 0;
4566 mddev->safemode_timer.function = md_safemode_timeout;
4567 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004568 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004570
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004571 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004572 if (rdev->raid_disk >= 0) {
4573 char nm[20];
4574 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004575 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004576 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4580
NeilBrown850b2b42006-10-03 01:15:46 -07004581 if (mddev->flags)
4582 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
NeilBrown0b8c9de2006-07-10 04:44:15 -07004584 md_wakeup_thread(mddev->thread);
4585 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004586
NeilBrownd7603b72006-01-06 00:20:30 -08004587 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004588 sysfs_notify_dirent_safe(mddev->sysfs_state);
4589 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004590 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return 0;
4592}
NeilBrown390ee602010-06-01 19:37:27 +10004593EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
NeilBrownfe60b012010-03-29 11:10:42 +11004595static int do_md_run(mddev_t *mddev)
4596{
4597 int err;
4598
4599 err = md_run(mddev);
4600 if (err)
4601 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004602 err = bitmap_load(mddev);
4603 if (err) {
4604 bitmap_destroy(mddev);
4605 goto out;
4606 }
NeilBrownfe60b012010-03-29 11:10:42 +11004607 set_capacity(mddev->gendisk, mddev->array_sectors);
4608 revalidate_disk(mddev->gendisk);
4609 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4610out:
4611 return err;
4612}
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614static int restart_array(mddev_t *mddev)
4615{
4616 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Andre Noll80fab1d2008-07-11 22:02:21 +10004618 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004620 return -ENXIO;
4621 if (!mddev->pers)
4622 return -EINVAL;
4623 if (!mddev->ro)
4624 return -EBUSY;
4625 mddev->safemode = 0;
4626 mddev->ro = 0;
4627 set_disk_ro(disk, 0);
4628 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4629 mdname(mddev));
4630 /* Kick recovery or resync if necessary */
4631 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4632 md_wakeup_thread(mddev->thread);
4633 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004634 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
NeilBrownacc55e22006-06-26 00:27:47 -07004638/* similar to deny_write_access, but accounts for our holding a reference
4639 * to the file ourselves */
4640static int deny_bitmap_write_access(struct file * file)
4641{
4642 struct inode *inode = file->f_mapping->host;
4643
4644 spin_lock(&inode->i_lock);
4645 if (atomic_read(&inode->i_writecount) > 1) {
4646 spin_unlock(&inode->i_lock);
4647 return -ETXTBSY;
4648 }
4649 atomic_set(&inode->i_writecount, -1);
4650 spin_unlock(&inode->i_lock);
4651
4652 return 0;
4653}
4654
NeilBrown43a70502009-12-14 12:49:55 +11004655void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004656{
4657 struct inode *inode = file->f_mapping->host;
4658
4659 spin_lock(&inode->i_lock);
4660 atomic_set(&inode->i_writecount, 1);
4661 spin_unlock(&inode->i_lock);
4662}
4663
NeilBrown6177b472010-03-29 11:37:13 +11004664static void md_clean(mddev_t *mddev)
4665{
4666 mddev->array_sectors = 0;
4667 mddev->external_size = 0;
4668 mddev->dev_sectors = 0;
4669 mddev->raid_disks = 0;
4670 mddev->recovery_cp = 0;
4671 mddev->resync_min = 0;
4672 mddev->resync_max = MaxSector;
4673 mddev->reshape_position = MaxSector;
4674 mddev->external = 0;
4675 mddev->persistent = 0;
4676 mddev->level = LEVEL_NONE;
4677 mddev->clevel[0] = 0;
4678 mddev->flags = 0;
4679 mddev->ro = 0;
4680 mddev->metadata_type[0] = 0;
4681 mddev->chunk_sectors = 0;
4682 mddev->ctime = mddev->utime = 0;
4683 mddev->layout = 0;
4684 mddev->max_disks = 0;
4685 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004686 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004687 mddev->delta_disks = 0;
4688 mddev->new_level = LEVEL_NONE;
4689 mddev->new_layout = 0;
4690 mddev->new_chunk_sectors = 0;
4691 mddev->curr_resync = 0;
4692 mddev->resync_mismatches = 0;
4693 mddev->suspend_lo = mddev->suspend_hi = 0;
4694 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4695 mddev->recovery = 0;
4696 mddev->in_sync = 0;
4697 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004698 mddev->safemode = 0;
4699 mddev->bitmap_info.offset = 0;
4700 mddev->bitmap_info.default_offset = 0;
4701 mddev->bitmap_info.chunksize = 0;
4702 mddev->bitmap_info.daemon_sleep = 0;
4703 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004704 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004705}
4706
NeilBrown390ee602010-06-01 19:37:27 +10004707void md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004708{
4709 if (mddev->sync_thread) {
4710 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4711 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4712 md_unregister_thread(mddev->sync_thread);
4713 mddev->sync_thread = NULL;
4714 }
4715
4716 del_timer_sync(&mddev->safemode_timer);
4717
4718 bitmap_flush(mddev);
4719 md_super_wait(mddev);
4720
4721 if (!mddev->in_sync || mddev->flags) {
4722 /* mark array as shutdown cleanly */
4723 mddev->in_sync = 1;
4724 md_update_sb(mddev, 1);
4725 }
4726}
NeilBrown390ee602010-06-01 19:37:27 +10004727EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004728
NeilBrown390ee602010-06-01 19:37:27 +10004729void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004730{
4731 mddev->pers->stop(mddev);
4732 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4733 mddev->to_remove = &md_redundancy_group;
4734 module_put(mddev->pers->owner);
4735 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004736 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004737}
NeilBrown390ee602010-06-01 19:37:27 +10004738EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004739
NeilBrowna4bd82d2010-03-29 13:23:10 +11004740static int md_set_readonly(mddev_t *mddev, int is_open)
4741{
4742 int err = 0;
4743 mutex_lock(&mddev->open_mutex);
4744 if (atomic_read(&mddev->openers) > is_open) {
4745 printk("md: %s still in use.\n",mdname(mddev));
4746 err = -EBUSY;
4747 goto out;
4748 }
4749 if (mddev->pers) {
4750 md_stop_writes(mddev);
4751
4752 err = -ENXIO;
4753 if (mddev->ro==1)
4754 goto out;
4755 mddev->ro = 1;
4756 set_disk_ro(mddev->gendisk, 1);
4757 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004758 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004759 err = 0;
4760 }
4761out:
4762 mutex_unlock(&mddev->open_mutex);
4763 return err;
4764}
4765
NeilBrown9e653b62006-06-26 00:27:58 -07004766/* mode:
4767 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004768 * 2 - stop but do not disassemble array
4769 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004770static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004773 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
NeilBrownc8c00a62009-08-10 12:50:52 +10004775 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004776 if (atomic_read(&mddev->openers) > is_open ||
4777 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004778 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004779 mutex_unlock(&mddev->open_mutex);
4780 return -EBUSY;
4781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
NeilBrown6e17b022010-08-07 21:41:19 +10004783 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004784 if (mddev->ro)
4785 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004786
NeilBrown390ee602010-06-01 19:37:27 +10004787 md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004788 md_stop(mddev);
4789 mddev->queue->merge_bvec_fn = NULL;
4790 mddev->queue->unplug_fn = NULL;
4791 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004792
NeilBrowna4bd82d2010-03-29 13:23:10 +11004793 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004794 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004795
NeilBrowna4bd82d2010-03-29 13:23:10 +11004796 list_for_each_entry(rdev, &mddev->disks, same_set)
4797 if (rdev->raid_disk >= 0) {
4798 char nm[20];
4799 sprintf(nm, "rd%d", rdev->raid_disk);
4800 sysfs_remove_link(&mddev->kobj, nm);
4801 }
NeilBrownc4647292009-05-07 12:51:06 +10004802
NeilBrowna4bd82d2010-03-29 13:23:10 +11004803 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004804 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004805 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004806
NeilBrowna4bd82d2010-03-29 13:23:10 +11004807 if (mddev->ro)
4808 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004809 } else
4810 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 /*
4812 * Free resources if final stop
4813 */
NeilBrown9e653b62006-06-26 00:27:58 -07004814 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4816
NeilBrown978f9462006-02-02 14:28:05 -08004817 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004818 if (mddev->bitmap_info.file) {
4819 restore_bitmap_write_access(mddev->bitmap_info.file);
4820 fput(mddev->bitmap_info.file);
4821 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004822 }
NeilBrownc3d97142009-12-14 12:49:52 +11004823 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 export_array(mddev);
4826
NeilBrown6177b472010-03-29 11:37:13 +11004827 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004828 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004829 if (mddev->hold_active == UNTIL_STOP)
4830 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004831 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004832 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004833 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004834 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004838#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839static void autorun_array(mddev_t *mddev)
4840{
4841 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 int err;
4843
NeilBrowna757e642005-04-16 15:26:42 -07004844 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847 printk(KERN_INFO "md: running: ");
4848
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004849 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 char b[BDEVNAME_SIZE];
4851 printk("<%s>", bdevname(rdev->bdev,b));
4852 }
4853 printk("\n");
4854
NeilBrownd710e132008-10-13 11:55:12 +11004855 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 if (err) {
4857 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004858 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 }
4860}
4861
4862/*
4863 * lets try to run arrays based on all disks that have arrived
4864 * until now. (those are in pending_raid_disks)
4865 *
4866 * the method: pick the first pending disk, collect all disks with
4867 * the same UUID, remove all from the pending list and put them into
4868 * the 'same_array' list. Then order this list based on superblock
4869 * update time (freshest comes first), kick out 'old' disks and
4870 * compare superblocks. If everything's fine then run it.
4871 *
4872 * If "unit" is allocated, then bump its reference count
4873 */
4874static void autorun_devices(int part)
4875{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004876 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev_t *mddev;
4878 char b[BDEVNAME_SIZE];
4879
4880 printk(KERN_INFO "md: autorun ...\n");
4881 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004882 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004884 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 rdev0 = list_entry(pending_raid_disks.next,
4886 mdk_rdev_t, same_set);
4887
4888 printk(KERN_INFO "md: considering %s ...\n",
4889 bdevname(rdev0->bdev,b));
4890 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004891 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 if (super_90_load(rdev, rdev0, 0) >= 0) {
4893 printk(KERN_INFO "md: adding %s ...\n",
4894 bdevname(rdev->bdev,b));
4895 list_move(&rdev->same_set, &candidates);
4896 }
4897 /*
4898 * now we have a set of devices, with all of them having
4899 * mostly sane superblocks. It's time to allocate the
4900 * mddev.
4901 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004902 if (part) {
4903 dev = MKDEV(mdp_major,
4904 rdev0->preferred_minor << MdpMinorShift);
4905 unit = MINOR(dev) >> MdpMinorShift;
4906 } else {
4907 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4908 unit = MINOR(dev);
4909 }
4910 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4912 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4913 break;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 md_probe(dev, NULL, NULL);
4917 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004918 if (!mddev || !mddev->gendisk) {
4919 if (mddev)
4920 mddev_put(mddev);
4921 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 "md: cannot allocate memory for md drive.\n");
4923 break;
4924 }
4925 if (mddev_lock(mddev))
4926 printk(KERN_WARNING "md: %s locked, cannot run\n",
4927 mdname(mddev));
4928 else if (mddev->raid_disks || mddev->major_version
4929 || !list_empty(&mddev->disks)) {
4930 printk(KERN_WARNING
4931 "md: %s already running, cannot run %s\n",
4932 mdname(mddev), bdevname(rdev0->bdev,b));
4933 mddev_unlock(mddev);
4934 } else {
4935 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004936 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004937 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 list_del_init(&rdev->same_set);
4939 if (bind_rdev_to_array(rdev, mddev))
4940 export_rdev(rdev);
4941 }
4942 autorun_array(mddev);
4943 mddev_unlock(mddev);
4944 }
4945 /* on success, candidates will be empty, on error
4946 * it won't...
4947 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004948 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004949 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 mddev_put(mddev);
4953 }
4954 printk(KERN_INFO "md: ... autorun DONE.\n");
4955}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004956#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958static int get_version(void __user * arg)
4959{
4960 mdu_version_t ver;
4961
4962 ver.major = MD_MAJOR_VERSION;
4963 ver.minor = MD_MINOR_VERSION;
4964 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4965
4966 if (copy_to_user(arg, &ver, sizeof(ver)))
4967 return -EFAULT;
4968
4969 return 0;
4970}
4971
4972static int get_array_info(mddev_t * mddev, void __user * arg)
4973{
4974 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004975 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
NeilBrowna9f326e2009-09-23 18:06:41 +10004978 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004979 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004981 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 failed++;
4983 else {
4984 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004985 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004986 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 else
4988 spare++;
4989 }
4990 }
4991
4992 info.major_version = mddev->major_version;
4993 info.minor_version = mddev->minor_version;
4994 info.patch_version = MD_PATCHLEVEL_VERSION;
4995 info.ctime = mddev->ctime;
4996 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004997 info.size = mddev->dev_sectors / 2;
4998 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004999 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 info.nr_disks = nr;
5001 info.raid_disks = mddev->raid_disks;
5002 info.md_minor = mddev->md_minor;
5003 info.not_persistent= !mddev->persistent;
5004
5005 info.utime = mddev->utime;
5006 info.state = 0;
5007 if (mddev->in_sync)
5008 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005009 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005010 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005011 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 info.working_disks = working;
5013 info.failed_disks = failed;
5014 info.spare_disks = spare;
5015
5016 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005017 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 if (copy_to_user(arg, &info, sizeof(info)))
5020 return -EFAULT;
5021
5022 return 0;
5023}
5024
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005025static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005026{
5027 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5028 char *ptr, *buf = NULL;
5029 int err = -ENOMEM;
5030
Dan Williamsb5470dc2008-06-27 21:44:04 -07005031 if (md_allow_write(mddev))
5032 file = kmalloc(sizeof(*file), GFP_NOIO);
5033 else
5034 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005035
NeilBrown32a76272005-06-21 17:17:14 -07005036 if (!file)
5037 goto out;
5038
5039 /* bitmap disabled, zero the first byte and copy out */
5040 if (!mddev->bitmap || !mddev->bitmap->file) {
5041 file->pathname[0] = '\0';
5042 goto copy_out;
5043 }
5044
5045 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5046 if (!buf)
5047 goto out;
5048
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005049 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5050 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005051 goto out;
5052
5053 strcpy(file->pathname, ptr);
5054
5055copy_out:
5056 err = 0;
5057 if (copy_to_user(arg, file, sizeof(*file)))
5058 err = -EFAULT;
5059out:
5060 kfree(buf);
5061 kfree(file);
5062 return err;
5063}
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065static int get_disk_info(mddev_t * mddev, void __user * arg)
5066{
5067 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 mdk_rdev_t *rdev;
5069
5070 if (copy_from_user(&info, arg, sizeof(info)))
5071 return -EFAULT;
5072
Andre Noll26ef3792008-07-11 22:02:21 +10005073 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 if (rdev) {
5075 info.major = MAJOR(rdev->bdev->bd_dev);
5076 info.minor = MINOR(rdev->bdev->bd_dev);
5077 info.raid_disk = rdev->raid_disk;
5078 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005079 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005081 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 info.state |= (1<<MD_DISK_ACTIVE);
5083 info.state |= (1<<MD_DISK_SYNC);
5084 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005085 if (test_bit(WriteMostly, &rdev->flags))
5086 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 } else {
5088 info.major = info.minor = 0;
5089 info.raid_disk = -1;
5090 info.state = (1<<MD_DISK_REMOVED);
5091 }
5092
5093 if (copy_to_user(arg, &info, sizeof(info)))
5094 return -EFAULT;
5095
5096 return 0;
5097}
5098
5099static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5100{
5101 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5102 mdk_rdev_t *rdev;
5103 dev_t dev = MKDEV(info->major,info->minor);
5104
5105 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5106 return -EOVERFLOW;
5107
5108 if (!mddev->raid_disks) {
5109 int err;
5110 /* expecting a device which has a superblock */
5111 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5112 if (IS_ERR(rdev)) {
5113 printk(KERN_WARNING
5114 "md: md_import_device returned %ld\n",
5115 PTR_ERR(rdev));
5116 return PTR_ERR(rdev);
5117 }
5118 if (!list_empty(&mddev->disks)) {
5119 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5120 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005121 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 .load_super(rdev, rdev0, mddev->minor_version);
5123 if (err < 0) {
5124 printk(KERN_WARNING
5125 "md: %s has different UUID to %s\n",
5126 bdevname(rdev->bdev,b),
5127 bdevname(rdev0->bdev,b2));
5128 export_rdev(rdev);
5129 return -EINVAL;
5130 }
5131 }
5132 err = bind_rdev_to_array(rdev, mddev);
5133 if (err)
5134 export_rdev(rdev);
5135 return err;
5136 }
5137
5138 /*
5139 * add_new_disk can be used once the array is assembled
5140 * to add "hot spares". They must already have a superblock
5141 * written
5142 */
5143 if (mddev->pers) {
5144 int err;
5145 if (!mddev->pers->hot_add_disk) {
5146 printk(KERN_WARNING
5147 "%s: personality does not support diskops!\n",
5148 mdname(mddev));
5149 return -EINVAL;
5150 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005151 if (mddev->persistent)
5152 rdev = md_import_device(dev, mddev->major_version,
5153 mddev->minor_version);
5154 else
5155 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 if (IS_ERR(rdev)) {
5157 printk(KERN_WARNING
5158 "md: md_import_device returned %ld\n",
5159 PTR_ERR(rdev));
5160 return PTR_ERR(rdev);
5161 }
NeilBrown1a855a02010-12-09 16:36:28 +11005162 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005163 if (!mddev->persistent) {
5164 if (info->state & (1<<MD_DISK_SYNC) &&
5165 info->raid_disk < mddev->raid_disks)
5166 rdev->raid_disk = info->raid_disk;
5167 else
5168 rdev->raid_disk = -1;
5169 } else
5170 super_types[mddev->major_version].
5171 validate_super(mddev, rdev);
NeilBrown1a855a02010-12-09 16:36:28 +11005172 if (test_bit(In_sync, &rdev->flags))
5173 rdev->saved_raid_disk = rdev->raid_disk;
5174 else
5175 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005176
NeilBrownb2d444d2005-11-08 21:39:31 -08005177 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005178 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5179 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005180 else
5181 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 rdev->raid_disk = -1;
5184 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005185 if (!err && !mddev->pers->hot_remove_disk) {
5186 /* If there is hot_add_disk but no hot_remove_disk
5187 * then added disks for geometry changes,
5188 * and should be added immediately.
5189 */
5190 super_types[mddev->major_version].
5191 validate_super(mddev, rdev);
5192 err = mddev->pers->hot_add_disk(mddev, rdev);
5193 if (err)
5194 unbind_rdev_from_array(rdev);
5195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 if (err)
5197 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005198 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005199 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005200
NeilBrown17571282006-12-10 02:20:52 -08005201 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005202 if (mddev->degraded)
5203 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005205 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 return err;
5207 }
5208
5209 /* otherwise, add_new_disk is only allowed
5210 * for major_version==0 superblocks
5211 */
5212 if (mddev->major_version != 0) {
5213 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5214 mdname(mddev));
5215 return -EINVAL;
5216 }
5217
5218 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5219 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005220 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 if (IS_ERR(rdev)) {
5222 printk(KERN_WARNING
5223 "md: error, md_import_device() returned %ld\n",
5224 PTR_ERR(rdev));
5225 return PTR_ERR(rdev);
5226 }
5227 rdev->desc_nr = info->number;
5228 if (info->raid_disk < mddev->raid_disks)
5229 rdev->raid_disk = info->raid_disk;
5230 else
5231 rdev->raid_disk = -1;
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005234 if (info->state & (1<<MD_DISK_SYNC))
5235 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005237 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5238 set_bit(WriteMostly, &rdev->flags);
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (!mddev->persistent) {
5241 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005242 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5243 } else
Andre Noll0f420352008-07-11 22:02:23 +10005244 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005245 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
NeilBrown2bf071b2006-01-06 00:20:55 -08005247 err = bind_rdev_to_array(rdev, mddev);
5248 if (err) {
5249 export_rdev(rdev);
5250 return err;
5251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 }
5253
5254 return 0;
5255}
5256
5257static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5258{
5259 char b[BDEVNAME_SIZE];
5260 mdk_rdev_t *rdev;
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 rdev = find_rdev(mddev, dev);
5263 if (!rdev)
5264 return -ENXIO;
5265
5266 if (rdev->raid_disk >= 0)
5267 goto busy;
5268
5269 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005270 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005271 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
5273 return 0;
5274busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005275 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 bdevname(rdev->bdev,b), mdname(mddev));
5277 return -EBUSY;
5278}
5279
5280static int hot_add_disk(mddev_t * mddev, dev_t dev)
5281{
5282 char b[BDEVNAME_SIZE];
5283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 mdk_rdev_t *rdev;
5285
5286 if (!mddev->pers)
5287 return -ENODEV;
5288
5289 if (mddev->major_version != 0) {
5290 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5291 " version-0 superblocks.\n",
5292 mdname(mddev));
5293 return -EINVAL;
5294 }
5295 if (!mddev->pers->hot_add_disk) {
5296 printk(KERN_WARNING
5297 "%s: personality does not support diskops!\n",
5298 mdname(mddev));
5299 return -EINVAL;
5300 }
5301
NeilBrownd710e132008-10-13 11:55:12 +11005302 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 if (IS_ERR(rdev)) {
5304 printk(KERN_WARNING
5305 "md: error, md_import_device() returned %ld\n",
5306 PTR_ERR(rdev));
5307 return -EINVAL;
5308 }
5309
5310 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005311 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005313 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
NeilBrown8190e752009-06-18 08:48:58 +10005315 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
NeilBrownb2d444d2005-11-08 21:39:31 -08005317 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 printk(KERN_WARNING
5319 "md: can not hot-add faulty %s disk to %s!\n",
5320 bdevname(rdev->bdev,b), mdname(mddev));
5321 err = -EINVAL;
5322 goto abort_export;
5323 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005324 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005326 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005327 err = bind_rdev_to_array(rdev, mddev);
5328 if (err)
5329 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
5331 /*
5332 * The rest should better be atomic, we can have disk failures
5333 * noticed in interrupt contexts ...
5334 */
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 rdev->raid_disk = -1;
5337
NeilBrown850b2b42006-10-03 01:15:46 -07005338 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 /*
5341 * Kick recovery, maybe this spare has to be added to the
5342 * array immediately.
5343 */
5344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5345 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005346 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 return 0;
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349abort_export:
5350 export_rdev(rdev);
5351 return err;
5352}
5353
NeilBrown32a76272005-06-21 17:17:14 -07005354static int set_bitmap_file(mddev_t *mddev, int fd)
5355{
5356 int err;
5357
NeilBrown36fa3062005-09-09 16:23:45 -07005358 if (mddev->pers) {
5359 if (!mddev->pers->quiesce)
5360 return -EBUSY;
5361 if (mddev->recovery || mddev->sync_thread)
5362 return -EBUSY;
5363 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005364 }
5365
NeilBrown36fa3062005-09-09 16:23:45 -07005366
5367 if (fd >= 0) {
5368 if (mddev->bitmap)
5369 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005370 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005371
NeilBrownc3d97142009-12-14 12:49:52 +11005372 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005373 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5374 mdname(mddev));
5375 return -EBADF;
5376 }
5377
NeilBrownc3d97142009-12-14 12:49:52 +11005378 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005379 if (err) {
5380 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5381 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005382 fput(mddev->bitmap_info.file);
5383 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005384 return err;
5385 }
NeilBrownc3d97142009-12-14 12:49:52 +11005386 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005387 } else if (mddev->bitmap == NULL)
5388 return -ENOENT; /* cannot remove what isn't there */
5389 err = 0;
5390 if (mddev->pers) {
5391 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005392 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005393 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005394 if (!err)
5395 err = bitmap_load(mddev);
5396 }
NeilBrownd7375ab322006-06-26 00:27:43 -07005397 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005398 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005399 fd = -1; /* make sure to put the file */
5400 }
NeilBrown36fa3062005-09-09 16:23:45 -07005401 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005402 }
5403 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005404 if (mddev->bitmap_info.file) {
5405 restore_bitmap_write_access(mddev->bitmap_info.file);
5406 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005407 }
NeilBrownc3d97142009-12-14 12:49:52 +11005408 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005409 }
5410
NeilBrown32a76272005-06-21 17:17:14 -07005411 return err;
5412}
5413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414/*
5415 * set_array_info is used two different ways
5416 * The original usage is when creating a new array.
5417 * In this usage, raid_disks is > 0 and it together with
5418 * level, size, not_persistent,layout,chunksize determine the
5419 * shape of the array.
5420 * This will always create an array with a type-0.90.0 superblock.
5421 * The newer usage is when assembling an array.
5422 * In this case raid_disks will be 0, and the major_version field is
5423 * use to determine which style super-blocks are to be found on the devices.
5424 * The minor and patch _version numbers are also kept incase the
5425 * super_block handler wishes to interpret them.
5426 */
5427static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5428{
5429
5430 if (info->raid_disks == 0) {
5431 /* just setting version number for superblock loading */
5432 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005433 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 super_types[info->major_version].name == NULL) {
5435 /* maybe try to auto-load a module? */
5436 printk(KERN_INFO
5437 "md: superblock version %d not known\n",
5438 info->major_version);
5439 return -EINVAL;
5440 }
5441 mddev->major_version = info->major_version;
5442 mddev->minor_version = info->minor_version;
5443 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005444 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005445 /* ensure mddev_put doesn't delete this now that there
5446 * is some minimal configuration.
5447 */
5448 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 return 0;
5450 }
5451 mddev->major_version = MD_MAJOR_VERSION;
5452 mddev->minor_version = MD_MINOR_VERSION;
5453 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5454 mddev->ctime = get_seconds();
5455
5456 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005457 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005458 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 mddev->raid_disks = info->raid_disks;
5460 /* don't set md_minor, it is determined by which /dev/md* was
5461 * openned
5462 */
5463 if (info->state & (1<<MD_SB_CLEAN))
5464 mddev->recovery_cp = MaxSector;
5465 else
5466 mddev->recovery_cp = 0;
5467 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005468 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
5470 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005471 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
5473 mddev->max_disks = MD_SB_DISKS;
5474
NeilBrowne6910632008-02-06 01:39:51 -08005475 if (mddev->persistent)
5476 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005477 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
NeilBrownc3d97142009-12-14 12:49:52 +11005479 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5480 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005481
NeilBrownf6705572006-03-27 01:18:11 -08005482 mddev->reshape_position = MaxSector;
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 /*
5485 * Generate a 128 bit UUID
5486 */
5487 get_random_bytes(mddev->uuid, 16);
5488
NeilBrownf6705572006-03-27 01:18:11 -08005489 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005490 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005491 mddev->new_layout = mddev->layout;
5492 mddev->delta_disks = 0;
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 return 0;
5495}
5496
Dan Williams1f403622009-03-31 14:59:03 +11005497void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5498{
Dan Williamsb522adc2009-03-31 15:00:31 +11005499 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5500
5501 if (mddev->external_size)
5502 return;
5503
Dan Williams1f403622009-03-31 14:59:03 +11005504 mddev->array_sectors = array_sectors;
5505}
5506EXPORT_SYMBOL(md_set_array_sectors);
5507
Andre Nolld71f9f82008-07-11 22:02:22 +10005508static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005509{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005510 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005511 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005512 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005513
5514 if (mddev->pers->resize == NULL)
5515 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005516 /* The "num_sectors" is the number of sectors of each device that
5517 * is used. This can only make sense for arrays with redundancy.
5518 * linear and raid0 always use whatever space is available. We can only
5519 * consider changing this number if no resync or reconstruction is
5520 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005521 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005522 * of each device. If num_sectors is zero, we find the largest size
5523 * that fits.
5524
NeilBrowna35b0d62006-01-06 00:20:49 -08005525 */
5526 if (mddev->sync_thread)
5527 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005528 if (mddev->bitmap)
5529 /* Sorry, cannot grow a bitmap yet, just remove it,
5530 * grow, and re-add.
5531 */
5532 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005533 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005534 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005535
Andre Nolld71f9f82008-07-11 22:02:22 +10005536 if (fit && (num_sectors == 0 || num_sectors > avail))
5537 num_sectors = avail;
5538 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005539 return -ENOSPC;
5540 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005541 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005542 if (!rv)
5543 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005544 return rv;
5545}
5546
NeilBrownda943b992006-01-06 00:20:54 -08005547static int update_raid_disks(mddev_t *mddev, int raid_disks)
5548{
5549 int rv;
5550 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005551 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005552 return -EINVAL;
5553 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005554 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005555 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005556 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005557 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005558 mddev->delta_disks = raid_disks - mddev->raid_disks;
5559
5560 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005561 return rv;
5562}
5563
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565/*
5566 * update_array_info is used to change the configuration of an
5567 * on-line array.
5568 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5569 * fields in the info are checked against the array.
5570 * Any differences that cannot be handled will cause an error.
5571 * Normally, only one change can be managed at a time.
5572 */
5573static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5574{
5575 int rv = 0;
5576 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005577 int state = 0;
5578
5579 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005580 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005581 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 if (mddev->major_version != info->major_version ||
5584 mddev->minor_version != info->minor_version ||
5585/* mddev->patch_version != info->patch_version || */
5586 mddev->ctime != info->ctime ||
5587 mddev->level != info->level ||
5588/* mddev->layout != info->layout || */
5589 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005590 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005591 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5592 ((state^info->state) & 0xfffffe00)
5593 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return -EINVAL;
5595 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005596 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5597 cnt++;
5598 if (mddev->raid_disks != info->raid_disks)
5599 cnt++;
5600 if (mddev->layout != info->layout)
5601 cnt++;
5602 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5603 cnt++;
5604 if (cnt == 0)
5605 return 0;
5606 if (cnt > 1)
5607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 if (mddev->layout != info->layout) {
5610 /* Change layout
5611 * we don't need to do anything at the md level, the
5612 * personality will take care of it all.
5613 */
NeilBrown50ac1682009-06-18 08:47:55 +10005614 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005616 else {
5617 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005618 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005619 if (rv)
5620 mddev->new_layout = mddev->layout;
5621 return rv;
5622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005624 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005625 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
NeilBrownda943b992006-01-06 00:20:54 -08005627 if (mddev->raid_disks != info->raid_disks)
5628 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
NeilBrown36fa3062005-09-09 16:23:45 -07005630 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5631 if (mddev->pers->quiesce == NULL)
5632 return -EINVAL;
5633 if (mddev->recovery || mddev->sync_thread)
5634 return -EBUSY;
5635 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5636 /* add the bitmap */
5637 if (mddev->bitmap)
5638 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005639 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005640 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005641 mddev->bitmap_info.offset =
5642 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005643 mddev->pers->quiesce(mddev, 1);
5644 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005645 if (!rv)
5646 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005647 if (rv)
5648 bitmap_destroy(mddev);
5649 mddev->pers->quiesce(mddev, 0);
5650 } else {
5651 /* remove the bitmap */
5652 if (!mddev->bitmap)
5653 return -ENOENT;
5654 if (mddev->bitmap->file)
5655 return -EINVAL;
5656 mddev->pers->quiesce(mddev, 1);
5657 bitmap_destroy(mddev);
5658 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005659 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005660 }
5661 }
NeilBrown850b2b42006-10-03 01:15:46 -07005662 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 return rv;
5664}
5665
5666static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5667{
5668 mdk_rdev_t *rdev;
5669
5670 if (mddev->pers == NULL)
5671 return -ENODEV;
5672
5673 rdev = find_rdev(mddev, dev);
5674 if (!rdev)
5675 return -ENODEV;
5676
5677 md_error(mddev, rdev);
5678 return 0;
5679}
5680
Andre Noll2f9618c2008-04-25 18:57:58 +02005681/*
5682 * We have a problem here : there is no easy way to give a CHS
5683 * virtual geometry. We currently pretend that we have a 2 heads
5684 * 4 sectors (with a BIG number of cylinders...). This drives
5685 * dosfs just mad... ;-)
5686 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005687static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5688{
5689 mddev_t *mddev = bdev->bd_disk->private_data;
5690
5691 geo->heads = 2;
5692 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005693 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005694 return 0;
5695}
5696
Al Viroa39907f2008-03-02 10:31:15 -05005697static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 unsigned int cmd, unsigned long arg)
5699{
5700 int err = 0;
5701 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005703 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705 if (!capable(CAP_SYS_ADMIN))
5706 return -EACCES;
5707
5708 /*
5709 * Commands dealing with the RAID driver but not any
5710 * particular array:
5711 */
5712 switch (cmd)
5713 {
5714 case RAID_VERSION:
5715 err = get_version(argp);
5716 goto done;
5717
5718 case PRINT_RAID_DEBUG:
5719 err = 0;
5720 md_print_devices();
5721 goto done;
5722
5723#ifndef MODULE
5724 case RAID_AUTORUN:
5725 err = 0;
5726 autostart_arrays(arg);
5727 goto done;
5728#endif
5729 default:;
5730 }
5731
5732 /*
5733 * Commands creating/starting a new array:
5734 */
5735
Al Viroa39907f2008-03-02 10:31:15 -05005736 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
5738 if (!mddev) {
5739 BUG();
5740 goto abort;
5741 }
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 err = mddev_lock(mddev);
5744 if (err) {
5745 printk(KERN_INFO
5746 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5747 err, cmd);
5748 goto abort;
5749 }
5750
5751 switch (cmd)
5752 {
5753 case SET_ARRAY_INFO:
5754 {
5755 mdu_array_info_t info;
5756 if (!arg)
5757 memset(&info, 0, sizeof(info));
5758 else if (copy_from_user(&info, argp, sizeof(info))) {
5759 err = -EFAULT;
5760 goto abort_unlock;
5761 }
5762 if (mddev->pers) {
5763 err = update_array_info(mddev, &info);
5764 if (err) {
5765 printk(KERN_WARNING "md: couldn't update"
5766 " array info. %d\n", err);
5767 goto abort_unlock;
5768 }
5769 goto done_unlock;
5770 }
5771 if (!list_empty(&mddev->disks)) {
5772 printk(KERN_WARNING
5773 "md: array %s already has disks!\n",
5774 mdname(mddev));
5775 err = -EBUSY;
5776 goto abort_unlock;
5777 }
5778 if (mddev->raid_disks) {
5779 printk(KERN_WARNING
5780 "md: array %s already initialised!\n",
5781 mdname(mddev));
5782 err = -EBUSY;
5783 goto abort_unlock;
5784 }
5785 err = set_array_info(mddev, &info);
5786 if (err) {
5787 printk(KERN_WARNING "md: couldn't set"
5788 " array info. %d\n", err);
5789 goto abort_unlock;
5790 }
5791 }
5792 goto done_unlock;
5793
5794 default:;
5795 }
5796
5797 /*
5798 * Commands querying/configuring an existing array:
5799 */
NeilBrown32a76272005-06-21 17:17:14 -07005800 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005801 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005802 if ((!mddev->raid_disks && !mddev->external)
5803 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5804 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5805 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 err = -ENODEV;
5807 goto abort_unlock;
5808 }
5809
5810 /*
5811 * Commands even a read-only array can execute:
5812 */
5813 switch (cmd)
5814 {
5815 case GET_ARRAY_INFO:
5816 err = get_array_info(mddev, argp);
5817 goto done_unlock;
5818
NeilBrown32a76272005-06-21 17:17:14 -07005819 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005820 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005821 goto done_unlock;
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 case GET_DISK_INFO:
5824 err = get_disk_info(mddev, argp);
5825 goto done_unlock;
5826
5827 case RESTART_ARRAY_RW:
5828 err = restart_array(mddev);
5829 goto done_unlock;
5830
5831 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005832 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 goto done_unlock;
5834
5835 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005836 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 goto done_unlock;
5838
Dan Williamse2218352010-05-12 08:25:37 +10005839 case BLKROSET:
5840 if (get_user(ro, (int __user *)(arg))) {
5841 err = -EFAULT;
5842 goto done_unlock;
5843 }
5844 err = -EINVAL;
5845
5846 /* if the bdev is going readonly the value of mddev->ro
5847 * does not matter, no writes are coming
5848 */
5849 if (ro)
5850 goto done_unlock;
5851
5852 /* are we are already prepared for writes? */
5853 if (mddev->ro != 1)
5854 goto done_unlock;
5855
5856 /* transitioning to readauto need only happen for
5857 * arrays that call md_write_start
5858 */
5859 if (mddev->pers) {
5860 err = restart_array(mddev);
5861 if (err == 0) {
5862 mddev->ro = 2;
5863 set_disk_ro(mddev->gendisk, 0);
5864 }
5865 }
5866 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 }
5868
5869 /*
5870 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005871 * superblock, so we do not allow them on read-only arrays.
5872 * However non-MD ioctls (e.g. get-size) will still come through
5873 * here and hit the 'default' below, so only disallow
5874 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005876 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005877 if (mddev->ro == 2) {
5878 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005879 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005880 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5881 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005882 } else {
5883 err = -EROFS;
5884 goto abort_unlock;
5885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 }
5887
5888 switch (cmd)
5889 {
5890 case ADD_NEW_DISK:
5891 {
5892 mdu_disk_info_t info;
5893 if (copy_from_user(&info, argp, sizeof(info)))
5894 err = -EFAULT;
5895 else
5896 err = add_new_disk(mddev, &info);
5897 goto done_unlock;
5898 }
5899
5900 case HOT_REMOVE_DISK:
5901 err = hot_remove_disk(mddev, new_decode_dev(arg));
5902 goto done_unlock;
5903
5904 case HOT_ADD_DISK:
5905 err = hot_add_disk(mddev, new_decode_dev(arg));
5906 goto done_unlock;
5907
5908 case SET_DISK_FAULTY:
5909 err = set_disk_faulty(mddev, new_decode_dev(arg));
5910 goto done_unlock;
5911
5912 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005913 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 goto done_unlock;
5915
NeilBrown32a76272005-06-21 17:17:14 -07005916 case SET_BITMAP_FILE:
5917 err = set_bitmap_file(mddev, (int)arg);
5918 goto done_unlock;
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 err = -EINVAL;
5922 goto abort_unlock;
5923 }
5924
5925done_unlock:
5926abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005927 if (mddev->hold_active == UNTIL_IOCTL &&
5928 err != -EINVAL)
5929 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 mddev_unlock(mddev);
5931
5932 return err;
5933done:
5934 if (err)
5935 MD_BUG();
5936abort:
5937 return err;
5938}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005939#ifdef CONFIG_COMPAT
5940static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5941 unsigned int cmd, unsigned long arg)
5942{
5943 switch (cmd) {
5944 case HOT_REMOVE_DISK:
5945 case HOT_ADD_DISK:
5946 case SET_DISK_FAULTY:
5947 case SET_BITMAP_FILE:
5948 /* These take in integer arg, do not convert */
5949 break;
5950 default:
5951 arg = (unsigned long)compat_ptr(arg);
5952 break;
5953 }
5954
5955 return md_ioctl(bdev, mode, cmd, arg);
5956}
5957#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
Al Viroa39907f2008-03-02 10:31:15 -05005959static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960{
5961 /*
5962 * Succeed if we can lock the mddev, which confirms that
5963 * it isn't being stopped right now.
5964 */
NeilBrownd3374822009-01-09 08:31:10 +11005965 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 int err;
5967
NeilBrownd3374822009-01-09 08:31:10 +11005968 if (mddev->gendisk != bdev->bd_disk) {
5969 /* we are racing with mddev_put which is discarding this
5970 * bd_disk.
5971 */
5972 mddev_put(mddev);
5973 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02005974 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005975 /* Then retry the open from the top */
5976 return -ERESTARTSYS;
5977 }
5978 BUG_ON(mddev != bdev->bd_disk->private_data);
5979
NeilBrownc8c00a62009-08-10 12:50:52 +10005980 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 goto out;
5982
5983 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005984 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005985 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
NeilBrownf3b99be2010-06-24 13:31:03 +10005987 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 out:
5989 return err;
5990}
5991
Al Viroa39907f2008-03-02 10:31:15 -05005992static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
Al Viroa39907f2008-03-02 10:31:15 -05005994 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005996 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005997 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 mddev_put(mddev);
5999
6000 return 0;
6001}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006002static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006005 .open = md_open,
6006 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006007 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006008#ifdef CONFIG_COMPAT
6009 .compat_ioctl = md_compat_ioctl,
6010#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006011 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012};
6013
Adrian Bunk75c96f82005-05-05 16:16:09 -07006014static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
6016 mdk_thread_t *thread = arg;
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 /*
6019 * md_thread is a 'system-thread', it's priority should be very
6020 * high. We avoid resource deadlocks individually in each
6021 * raid personality. (RAID5 does preallocation) We also use RR and
6022 * the very same RT priority as kswapd, thus we will never get
6023 * into a priority inversion deadlock.
6024 *
6025 * we definitely have to have equal or higher priority than
6026 * bdflush, otherwise bdflush will deadlock if there are too
6027 * many dirty RAID5 blocks.
6028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
NeilBrown6985c432005-10-19 21:23:47 -07006030 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006031 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
NeilBrown93588e22005-11-15 00:09:12 -08006033 /* We need to wait INTERRUPTIBLE so that
6034 * we don't add to the load-average.
6035 * That means we need to be sure no signals are
6036 * pending
6037 */
6038 if (signal_pending(current))
6039 flush_signals(current);
6040
6041 wait_event_interruptible_timeout
6042 (thread->wqueue,
6043 test_bit(THREAD_WAKEUP, &thread->flags)
6044 || kthread_should_stop(),
6045 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
NeilBrown589a5942010-12-09 17:02:14 +11006047 if (test_and_clear_bit(THREAD_WAKEUP, &thread->flags))
6048 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 return 0;
6052}
6053
6054void md_wakeup_thread(mdk_thread_t *thread)
6055{
6056 if (thread) {
6057 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6058 set_bit(THREAD_WAKEUP, &thread->flags);
6059 wake_up(&thread->wqueue);
6060 }
6061}
6062
6063mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6064 const char *name)
6065{
6066 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
NeilBrown9ffae0c2006-01-06 00:20:32 -08006068 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 if (!thread)
6070 return NULL;
6071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 init_waitqueue_head(&thread->wqueue);
6073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 thread->run = run;
6075 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006076 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006077 thread->tsk = kthread_run(md_thread, thread,
6078 "%s_%s",
6079 mdname(thread->mddev),
6080 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006081 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 kfree(thread);
6083 return NULL;
6084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return thread;
6086}
6087
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088void md_unregister_thread(mdk_thread_t *thread)
6089{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006090 if (!thread)
6091 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006092 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006093
6094 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 kfree(thread);
6096}
6097
6098void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6099{
6100 if (!mddev) {
6101 MD_BUG();
6102 return;
6103 }
6104
NeilBrownb2d444d2005-11-08 21:39:31 -08006105 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006107
6108 if (mddev->external)
6109 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6112 mdname(mddev),
6113 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6114 __builtin_return_address(0),__builtin_return_address(1),
6115 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006116*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006117 if (!mddev->pers)
6118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 if (!mddev->pers->error_handler)
6120 return;
6121 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006122 if (mddev->degraded)
6123 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006124 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6126 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6127 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006128 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006129 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006130 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131}
6132
6133/* seq_file implementation /proc/mdstat */
6134
6135static void status_unused(struct seq_file *seq)
6136{
6137 int i = 0;
6138 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
6140 seq_printf(seq, "unused devices: ");
6141
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006142 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 char b[BDEVNAME_SIZE];
6144 i++;
6145 seq_printf(seq, "%s ",
6146 bdevname(rdev->bdev,b));
6147 }
6148 if (!i)
6149 seq_printf(seq, "<none>");
6150
6151 seq_printf(seq, "\n");
6152}
6153
6154
6155static void status_resync(struct seq_file *seq, mddev_t * mddev)
6156{
NeilBrowndd71cf62009-05-07 12:49:35 +10006157 sector_t max_sectors, resync, res;
6158 unsigned long dt, db;
6159 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006160 int scale;
6161 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
NeilBrowndd71cf62009-05-07 12:49:35 +10006163 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
6165 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006166 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006168 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 /*
6171 * Should not happen.
6172 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006173 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 MD_BUG();
6175 return;
6176 }
NeilBrown4588b422006-03-27 01:18:04 -08006177 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006178 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006179 * u32, as those are the requirements for sector_div.
6180 * Thus 'scale' must be at least 10
6181 */
6182 scale = 10;
6183 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006184 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006185 scale++;
6186 }
6187 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006188 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006189
6190 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 {
NeilBrown4588b422006-03-27 01:18:04 -08006192 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 seq_printf(seq, "[");
6194 for (i = 0; i < x; i++)
6195 seq_printf(seq, "=");
6196 seq_printf(seq, ">");
6197 for (i = 0; i < y; i++)
6198 seq_printf(seq, ".");
6199 seq_printf(seq, "] ");
6200 }
NeilBrown4588b422006-03-27 01:18:04 -08006201 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006202 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6203 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006204 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6205 "check" :
6206 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6207 "resync" : "recovery"))),
6208 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006209 (unsigned long long) resync/2,
6210 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 * dt: time from mark until now
6214 * db: blocks written from mark until now
6215 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006216 *
6217 * rt is a sector_t, so could be 32bit or 64bit.
6218 * So we divide before multiply in case it is 32bit and close
6219 * to the limit.
6220 * We scale the divisor (db) by 32 to avoid loosing precision
6221 * near the end of resync when the number of remaining sectors
6222 * is close to 'db'.
6223 * We then divide rt by 32 after multiplying by db to compensate.
6224 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 */
6226 dt = ((jiffies - mddev->resync_mark) / HZ);
6227 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006228 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6229 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
NeilBrowndd71cf62009-05-07 12:49:35 +10006231 rt = max_sectors - resync; /* number of remaining sectors */
6232 sector_div(rt, db/32+1);
6233 rt *= dt;
6234 rt >>= 5;
6235
6236 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6237 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
NeilBrownff4e8d92006-07-10 04:44:16 -07006239 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
6242static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6243{
6244 struct list_head *tmp;
6245 loff_t l = *pos;
6246 mddev_t *mddev;
6247
6248 if (l >= 0x10000)
6249 return NULL;
6250 if (!l--)
6251 /* header */
6252 return (void*)1;
6253
6254 spin_lock(&all_mddevs_lock);
6255 list_for_each(tmp,&all_mddevs)
6256 if (!l--) {
6257 mddev = list_entry(tmp, mddev_t, all_mddevs);
6258 mddev_get(mddev);
6259 spin_unlock(&all_mddevs_lock);
6260 return mddev;
6261 }
6262 spin_unlock(&all_mddevs_lock);
6263 if (!l--)
6264 return (void*)2;/* tail */
6265 return NULL;
6266}
6267
6268static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6269{
6270 struct list_head *tmp;
6271 mddev_t *next_mddev, *mddev = v;
6272
6273 ++*pos;
6274 if (v == (void*)2)
6275 return NULL;
6276
6277 spin_lock(&all_mddevs_lock);
6278 if (v == (void*)1)
6279 tmp = all_mddevs.next;
6280 else
6281 tmp = mddev->all_mddevs.next;
6282 if (tmp != &all_mddevs)
6283 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6284 else {
6285 next_mddev = (void*)2;
6286 *pos = 0x10000;
6287 }
6288 spin_unlock(&all_mddevs_lock);
6289
6290 if (v != (void*)1)
6291 mddev_put(mddev);
6292 return next_mddev;
6293
6294}
6295
6296static void md_seq_stop(struct seq_file *seq, void *v)
6297{
6298 mddev_t *mddev = v;
6299
6300 if (mddev && v != (void*)1 && v != (void*)2)
6301 mddev_put(mddev);
6302}
6303
NeilBrownd7603b72006-01-06 00:20:30 -08006304struct mdstat_info {
6305 int event;
6306};
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308static int md_seq_show(struct seq_file *seq, void *v)
6309{
6310 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006311 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006313 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006314 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006317 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 seq_printf(seq, "Personalities : ");
6319 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006320 list_for_each_entry(pers, &pers_list, list)
6321 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
6323 spin_unlock(&pers_lock);
6324 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006325 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 return 0;
6327 }
6328 if (v == (void*)2) {
6329 status_unused(seq);
6330 return 0;
6331 }
6332
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006333 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006335
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6337 seq_printf(seq, "%s : %sactive", mdname(mddev),
6338 mddev->pers ? "" : "in");
6339 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006340 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006342 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006343 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 seq_printf(seq, " %s", mddev->pers->name);
6345 }
6346
Andre Nolldd8ac332009-03-31 14:33:13 +11006347 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006348 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 char b[BDEVNAME_SIZE];
6350 seq_printf(seq, " %s[%d]",
6351 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006352 if (test_bit(WriteMostly, &rdev->flags))
6353 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006354 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 seq_printf(seq, "(F)");
6356 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006357 } else if (rdev->raid_disk < 0)
6358 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006359 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
6361
6362 if (!list_empty(&mddev->disks)) {
6363 if (mddev->pers)
6364 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006365 (unsigned long long)
6366 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 else
6368 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006369 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006371 if (mddev->persistent) {
6372 if (mddev->major_version != 0 ||
6373 mddev->minor_version != 90) {
6374 seq_printf(seq," super %d.%d",
6375 mddev->major_version,
6376 mddev->minor_version);
6377 }
NeilBrowne6910632008-02-06 01:39:51 -08006378 } else if (mddev->external)
6379 seq_printf(seq, " super external:%s",
6380 mddev->metadata_type);
6381 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006382 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006385 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006387 if (mddev->pers->sync_request) {
6388 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006389 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006390 seq_printf(seq, "\n ");
6391 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6392 seq_printf(seq, "\tresync=DELAYED\n ");
6393 else if (mddev->recovery_cp < MaxSector)
6394 seq_printf(seq, "\tresync=PENDING\n ");
6395 }
NeilBrown32a76272005-06-21 17:17:14 -07006396 } else
6397 seq_printf(seq, "\n ");
6398
6399 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006400 unsigned long chunk_kb;
6401 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006402 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006403 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006404 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6405 "%lu%s chunk",
6406 bitmap->pages - bitmap->missing_pages,
6407 bitmap->pages,
6408 (bitmap->pages - bitmap->missing_pages)
6409 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006410 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006411 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006412 if (bitmap->file) {
6413 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006414 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006415 }
NeilBrown78d742d2005-06-21 17:17:15 -07006416
NeilBrown32a76272005-06-21 17:17:14 -07006417 seq_printf(seq, "\n");
6418 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
6420
6421 seq_printf(seq, "\n");
6422 }
6423 mddev_unlock(mddev);
6424
6425 return 0;
6426}
6427
Jan Engelhardt110518b2009-05-07 12:49:37 +10006428static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 .start = md_seq_start,
6430 .next = md_seq_next,
6431 .stop = md_seq_stop,
6432 .show = md_seq_show,
6433};
6434
6435static int md_seq_open(struct inode *inode, struct file *file)
6436{
6437 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006438 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6439 if (mi == NULL)
6440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
6442 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006443 if (error)
6444 kfree(mi);
6445 else {
6446 struct seq_file *p = file->private_data;
6447 p->private = mi;
6448 mi->event = atomic_read(&md_event_count);
6449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 return error;
6451}
6452
NeilBrownd7603b72006-01-06 00:20:30 -08006453static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6454{
6455 struct seq_file *m = filp->private_data;
6456 struct mdstat_info *mi = m->private;
6457 int mask;
6458
6459 poll_wait(filp, &md_event_waiters, wait);
6460
6461 /* always allow read */
6462 mask = POLLIN | POLLRDNORM;
6463
6464 if (mi->event != atomic_read(&md_event_count))
6465 mask |= POLLERR | POLLPRI;
6466 return mask;
6467}
6468
Arjan van de Venfa027c22007-02-12 00:55:33 -08006469static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006470 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 .open = md_seq_open,
6472 .read = seq_read,
6473 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006474 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006475 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476};
6477
NeilBrown2604b702006-01-06 00:20:36 -08006478int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006481 list_add_tail(&p->list, &pers_list);
6482 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 spin_unlock(&pers_lock);
6484 return 0;
6485}
6486
NeilBrown2604b702006-01-06 00:20:36 -08006487int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488{
NeilBrown2604b702006-01-06 00:20:36 -08006489 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006491 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 spin_unlock(&pers_lock);
6493 return 0;
6494}
6495
NeilBrowneea1bf32009-03-31 14:27:02 +11006496static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497{
6498 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006500 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
6502 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006503 rcu_read_lock();
6504 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006506 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6507 (int)part_stat_read(&disk->part0, sectors[1]) -
6508 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006509 /* sync IO will cause sync_io to increase before the disk_stats
6510 * as sync_io is counted when a request starts, and
6511 * disk_stats is counted when it completes.
6512 * So resync activity will cause curr_events to be smaller than
6513 * when there was no such activity.
6514 * non-sync IO will cause disk_stat to increase without
6515 * increasing sync_io so curr_events will (eventually)
6516 * be larger than it was before. Once it becomes
6517 * substantially larger, the test below will cause
6518 * the array to appear non-idle, and resync will slow
6519 * down.
6520 * If there is a lot of outstanding resync activity when
6521 * we set last_event to curr_events, then all that activity
6522 * completing might cause the array to appear non-idle
6523 * and resync will be slowed down even though there might
6524 * not have been non-resync activity. This will only
6525 * happen once though. 'last_events' will soon reflect
6526 * the state where there is little or no outstanding
6527 * resync requests, and further resync activity will
6528 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006529 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006531 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 rdev->last_events = curr_events;
6533 idle = 0;
6534 }
6535 }
NeilBrown4b809912008-07-21 17:05:25 +10006536 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 return idle;
6538}
6539
6540void md_done_sync(mddev_t *mddev, int blocks, int ok)
6541{
6542 /* another "blocks" (512byte) blocks have been synced */
6543 atomic_sub(blocks, &mddev->recovery_active);
6544 wake_up(&mddev->recovery_wait);
6545 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006546 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 md_wakeup_thread(mddev->thread);
6548 // stop recovery, signal do_sync ....
6549 }
6550}
6551
6552
NeilBrown06d91a52005-06-21 17:17:12 -07006553/* md_write_start(mddev, bi)
6554 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006555 * in superblock) before writing, schedule a superblock update
6556 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006557 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006558void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559{
Neil Brown0fd62b82008-06-28 08:31:36 +10006560 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006561 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006562 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006563
NeilBrownf91de922005-11-08 21:39:36 -08006564 BUG_ON(mddev->ro == 1);
6565 if (mddev->ro == 2) {
6566 /* need to switch to read/write */
6567 mddev->ro = 0;
6568 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6569 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006570 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006571 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006572 }
NeilBrown06d91a52005-06-21 17:17:12 -07006573 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006574 if (mddev->safemode == 1)
6575 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006576 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006577 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006578 if (mddev->in_sync) {
6579 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006580 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006581 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006582 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006583 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006584 }
NeilBrowna9701a32005-11-08 21:39:34 -08006585 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006586 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006587 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006588 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006589 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006590 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591}
6592
6593void md_write_end(mddev_t *mddev)
6594{
6595 if (atomic_dec_and_test(&mddev->writes_pending)) {
6596 if (mddev->safemode == 2)
6597 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006598 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6600 }
6601}
6602
NeilBrown2a2275d2007-01-26 00:57:11 -08006603/* md_allow_write(mddev)
6604 * Calling this ensures that the array is marked 'active' so that writes
6605 * may proceed without blocking. It is important to call this before
6606 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6607 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006608 *
6609 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6610 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006611 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006612int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006613{
6614 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006615 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006616 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006617 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006618 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006619 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006620
6621 spin_lock_irq(&mddev->write_lock);
6622 if (mddev->in_sync) {
6623 mddev->in_sync = 0;
6624 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006625 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006626 if (mddev->safemode_delay &&
6627 mddev->safemode == 0)
6628 mddev->safemode = 1;
6629 spin_unlock_irq(&mddev->write_lock);
6630 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006631 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006632 } else
6633 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006634
NeilBrown070dc6d2010-08-30 17:33:34 +10006635 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006636 return -EAGAIN;
6637 else
6638 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006639}
6640EXPORT_SYMBOL_GPL(md_allow_write);
6641
NeilBrownb63d7c22010-06-01 19:37:33 +10006642void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006643{
6644 if (mddev->queue)
6645 blk_unplug(mddev->queue);
6646 if (mddev->plug)
6647 mddev->plug->unplug_fn(mddev->plug);
6648}
6649
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650#define SYNC_MARKS 10
6651#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006652void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653{
6654 mddev_t *mddev2;
6655 unsigned int currspeed = 0,
6656 window;
NeilBrown57afd892005-06-21 17:17:13 -07006657 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 unsigned long mark[SYNC_MARKS];
6659 sector_t mark_cnt[SYNC_MARKS];
6660 int last_mark,m;
6661 struct list_head *tmp;
6662 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006663 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006664 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006665 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
6667 /* just incase thread restarts... */
6668 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6669 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006670 if (mddev->ro) /* never try to sync a read-only array */
6671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
NeilBrown61df9d92006-10-03 01:15:57 -07006673 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6674 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6675 desc = "data-check";
6676 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6677 desc = "requested-resync";
6678 else
6679 desc = "resync";
6680 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6681 desc = "reshape";
6682 else
6683 desc = "recovery";
6684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 /* we overload curr_resync somewhat here.
6686 * 0 == not engaged in resync at all
6687 * 2 == checking that there is no conflict with another sync
6688 * 1 == like 2, but have yielded to allow conflicting resync to
6689 * commense
6690 * other == active in resync - this many blocks
6691 *
6692 * Before starting a resync we must have set curr_resync to
6693 * 2, and then checked that every "conflicting" array has curr_resync
6694 * less than ours. When we find one that is the same or higher
6695 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6696 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6697 * This will mean we have to start checking from the beginning again.
6698 *
6699 */
6700
6701 do {
6702 mddev->curr_resync = 2;
6703
6704 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006705 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006706 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006707
6708 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006710 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 if (mddev2 == mddev)
6712 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006713 if (!mddev->parallel_resync
6714 && mddev2->curr_resync
6715 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 DEFINE_WAIT(wq);
6717 if (mddev < mddev2 && mddev->curr_resync == 2) {
6718 /* arbitrarily yield */
6719 mddev->curr_resync = 1;
6720 wake_up(&resync_wait);
6721 }
6722 if (mddev > mddev2 && mddev->curr_resync == 1)
6723 /* no need to wait here, we can wait the next
6724 * time 'round when curr_resync == 2
6725 */
6726 continue;
NeilBrown97441972008-09-19 11:49:54 +10006727 /* We need to wait 'interruptible' so as not to
6728 * contribute to the load average, and not to
6729 * be caught by 'softlockup'
6730 */
6731 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006732 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006733 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006734 printk(KERN_INFO "md: delaying %s of %s"
6735 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006737 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006739 if (signal_pending(current))
6740 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 schedule();
6742 finish_wait(&resync_wait, &wq);
6743 goto try_again;
6744 }
6745 finish_wait(&resync_wait, &wq);
6746 }
6747 }
6748 } while (mddev->curr_resync < 2);
6749
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006750 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006751 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006753 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 */
6755 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006756 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006757 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006758 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6759 j = mddev->resync_min;
6760 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006761 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006762
NeilBrownccfcc3c2006-03-27 01:18:09 -08006763 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006764 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006765 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006767 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006768 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006769 rcu_read_lock();
6770 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006771 if (rdev->raid_disk >= 0 &&
6772 !test_bit(Faulty, &rdev->flags) &&
6773 !test_bit(In_sync, &rdev->flags) &&
6774 rdev->recovery_offset < j)
6775 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006776 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
NeilBrown61df9d92006-10-03 01:15:57 -07006779 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6780 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6781 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006782 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006783 "(but not more than %d KB/sec) for %s.\n",
6784 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
NeilBrowneea1bf32009-03-31 14:27:02 +11006786 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006787
NeilBrown57afd892005-06-21 17:17:13 -07006788 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 for (m = 0; m < SYNC_MARKS; m++) {
6790 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006791 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 }
6793 last_mark = 0;
6794 mddev->resync_mark = mark[last_mark];
6795 mddev->resync_mark_cnt = mark_cnt[last_mark];
6796
6797 /*
6798 * Tune reconstruction:
6799 */
6800 window = 32*(PAGE_SIZE/512);
6801 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6802 window/2,(unsigned long long) max_sectors/2);
6803
6804 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 last_check = 0;
6806
6807 if (j>2) {
6808 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006809 "md: resuming %s of %s from checkpoint.\n",
6810 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 mddev->curr_resync = j;
6812 }
NeilBrownefa59332009-12-14 12:49:49 +11006813 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
6815 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006816 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
NeilBrown57afd892005-06-21 17:17:13 -07006818 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006819
NeilBrown7a91ee12009-05-26 12:57:21 +10006820 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6821 ((mddev->curr_resync > mddev->curr_resync_completed &&
6822 (mddev->curr_resync - mddev->curr_resync_completed)
6823 > (max_sectors >> 4)) ||
6824 (j - mddev->curr_resync_completed)*2
6825 >= mddev->resync_max - mddev->curr_resync_completed
6826 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006827 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006828 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006829 wait_event(mddev->recovery_wait,
6830 atomic_read(&mddev->recovery_active) == 0);
6831 mddev->curr_resync_completed =
6832 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006833 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006834 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006835 }
NeilBrownacb180b2009-04-14 16:28:34 +10006836
NeilBrowne62e58a2009-07-01 13:15:35 +10006837 while (j >= mddev->resync_max && !kthread_should_stop()) {
6838 /* As this condition is controlled by user-space,
6839 * we can block indefinitely, so use '_interruptible'
6840 * to avoid triggering warnings.
6841 */
6842 flush_signals(current); /* just in case */
6843 wait_event_interruptible(mddev->recovery_wait,
6844 mddev->resync_max > j
6845 || kthread_should_stop());
6846 }
NeilBrownacb180b2009-04-14 16:28:34 +10006847
6848 if (kthread_should_stop())
6849 goto interrupted;
6850
NeilBrown57afd892005-06-21 17:17:13 -07006851 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006852 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006853 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 goto out;
6856 }
NeilBrown57afd892005-06-21 17:17:13 -07006857
6858 if (!skipped) { /* actual IO requested */
6859 io_sectors += sectors;
6860 atomic_add(sectors, &mddev->recovery_active);
6861 }
6862
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 j += sectors;
6864 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006865 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006866 if (last_check == 0)
6867 /* this is the earliers that rebuilt will be
6868 * visible in /proc/mdstat
6869 */
6870 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006871
6872 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 continue;
6874
NeilBrown57afd892005-06-21 17:17:13 -07006875 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
NeilBrowndfc70642008-05-23 13:04:39 -07006877 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 break;
6879
6880 repeat:
6881 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6882 /* step marks */
6883 int next = (last_mark+1) % SYNC_MARKS;
6884
6885 mddev->resync_mark = mark[next];
6886 mddev->resync_mark_cnt = mark_cnt[next];
6887 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006888 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 last_mark = next;
6890 }
6891
6892
NeilBrownc6207272008-02-06 01:39:52 -08006893 if (kthread_should_stop())
6894 goto interrupted;
6895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
6897 /*
6898 * this loop exits only if either when we are slower than
6899 * the 'hard' speed limit, or the system was IO-idle for
6900 * a jiffy.
6901 * the system might be non-idle CPU-wise, but we only care
6902 * about not overloading the IO subsystem. (things like an
6903 * e2fsck being done on the RAID array should execute fast)
6904 */
NeilBrown252ac522010-06-01 19:37:29 +10006905 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 cond_resched();
6907
NeilBrown57afd892005-06-21 17:17:13 -07006908 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6909 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
NeilBrown88202a02006-01-06 00:21:36 -08006911 if (currspeed > speed_min(mddev)) {
6912 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006913 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006914 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 goto repeat;
6916 }
6917 }
6918 }
NeilBrown61df9d92006-10-03 01:15:57 -07006919 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 /*
6921 * this also signals 'finished resyncing' to md_stop
6922 */
6923 out:
NeilBrown252ac522010-06-01 19:37:29 +10006924 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
6926 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6927
6928 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006929 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930
NeilBrowndfc70642008-05-23 13:04:39 -07006931 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006932 mddev->curr_resync > 2) {
6933 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6934 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6935 if (mddev->curr_resync >= mddev->recovery_cp) {
6936 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006937 "md: checkpointing %s of %s.\n",
6938 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006939 mddev->recovery_cp = mddev->curr_resync;
6940 }
6941 } else
6942 mddev->recovery_cp = MaxSector;
6943 } else {
6944 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6945 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006946 rcu_read_lock();
6947 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006948 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006949 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006950 !test_bit(Faulty, &rdev->flags) &&
6951 !test_bit(In_sync, &rdev->flags) &&
6952 rdev->recovery_offset < mddev->curr_resync)
6953 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006954 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 }
NeilBrown17571282006-12-10 02:20:52 -08006957 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006960 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6961 /* We completed so min/max setting can be forgotten if used. */
6962 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6963 mddev->resync_min = 0;
6964 mddev->resync_max = MaxSector;
6965 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6966 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006968 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6969 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006970 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 wake_up(&resync_wait);
6972 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6973 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006974 return;
6975
6976 interrupted:
6977 /*
6978 * got a signal, exit.
6979 */
6980 printk(KERN_INFO
6981 "md: md_do_sync() got signal ... exiting\n");
6982 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6983 goto out;
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985}
NeilBrown29269552006-03-27 01:18:10 -08006986EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
6988
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006989static int remove_and_add_spares(mddev_t *mddev)
6990{
6991 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006992 int spares = 0;
6993
NeilBrown97e4f422009-03-31 14:33:13 +11006994 mddev->curr_resync_completed = 0;
6995
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006996 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006997 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006998 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006999 (test_bit(Faulty, &rdev->flags) ||
7000 ! test_bit(In_sync, &rdev->flags)) &&
7001 atomic_read(&rdev->nr_pending)==0) {
7002 if (mddev->pers->hot_remove_disk(
7003 mddev, rdev->raid_disk)==0) {
7004 char nm[20];
7005 sprintf(nm,"rd%d", rdev->raid_disk);
7006 sysfs_remove_link(&mddev->kobj, nm);
7007 rdev->raid_disk = -1;
7008 }
7009 }
7010
NeilBrown4044ba52009-01-09 08:31:11 +11007011 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007012 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007013 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007014 !test_bit(In_sync, &rdev->flags) &&
7015 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007016 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007017 if (rdev->raid_disk < 0
7018 && !test_bit(Faulty, &rdev->flags)) {
7019 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007020 if (mddev->pers->
7021 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007022 char nm[20];
7023 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007024 if (sysfs_create_link(&mddev->kobj,
7025 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007026 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007027 spares++;
7028 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007029 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007030 } else
7031 break;
7032 }
NeilBrowndfc70642008-05-23 13:04:39 -07007033 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007034 }
7035 return spares;
7036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037/*
7038 * This routine is regularly called by all per-raid-array threads to
7039 * deal with generic issues like resync and super-block update.
7040 * Raid personalities that don't have a thread (linear/raid0) do not
7041 * need this as they never do any recovery or update the superblock.
7042 *
7043 * It does not do any resync itself, but rather "forks" off other threads
7044 * to do that as needed.
7045 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7046 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007047 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 * and wakeups up this thread which will reap the thread and finish up.
7049 * This thread also removes any faulty devices (with nr_pending == 0).
7050 *
7051 * The overall approach is:
7052 * 1/ if the superblock needs updating, update it.
7053 * 2/ If a recovery thread is running, don't do anything else.
7054 * 3/ If recovery has finished, clean up, possibly marking spares active.
7055 * 4/ If there are any faulty devices, remove them.
7056 * 5/ If array is degraded, try to add spares devices
7057 * 6/ If array has spares or is not in-sync, start a resync thread.
7058 */
7059void md_check_recovery(mddev_t *mddev)
7060{
7061 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062
7063
NeilBrown5f404022005-06-21 17:17:16 -07007064 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007065 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
7067 if (mddev->ro)
7068 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007069
7070 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007071 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007072 printk(KERN_INFO "md: %s in immediate safe mode\n",
7073 mdname(mddev));
7074 mddev->safemode = 2;
7075 }
7076 flush_signals(current);
7077 }
7078
NeilBrownc89a8ee2008-08-05 15:54:13 +10007079 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7080 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007082 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007084 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007085 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007086 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7087 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 ))
7089 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007090
NeilBrowndf5b89b2006-03-27 01:18:20 -08007091 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007092 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007093
NeilBrownc89a8ee2008-08-05 15:54:13 +10007094 if (mddev->ro) {
7095 /* Only thing we do on a ro array is remove
7096 * failed devices.
7097 */
7098 remove_and_add_spares(mddev);
7099 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7100 goto unlock;
7101 }
7102
NeilBrown31a59e32008-04-30 00:52:30 -07007103 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007104 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007105 spin_lock_irq(&mddev->write_lock);
7106 if (mddev->safemode &&
7107 !atomic_read(&mddev->writes_pending) &&
7108 !mddev->in_sync &&
7109 mddev->recovery_cp == MaxSector) {
7110 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007111 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007112 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007113 }
7114 if (mddev->safemode == 1)
7115 mddev->safemode = 0;
7116 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007117 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007118 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007119 }
NeilBrownfca4d842005-06-21 17:17:11 -07007120
NeilBrown850b2b42006-10-03 01:15:46 -07007121 if (mddev->flags)
7122 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007123
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7125 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7126 /* resync/recovery still happening */
7127 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7128 goto unlock;
7129 }
7130 if (mddev->sync_thread) {
7131 /* resync has finished, collect result */
7132 md_unregister_thread(mddev->sync_thread);
7133 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007134 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7135 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 /* success...*/
7137 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007138 if (mddev->pers->spare_active(mddev))
7139 sysfs_notify(&mddev->kobj, NULL,
7140 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 }
NeilBrowncea9c222009-03-31 15:15:05 +11007142 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7143 mddev->pers->finish_reshape)
7144 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007145 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007146
7147 /* if array is no-longer degraded, then any saved_raid_disk
7148 * information must be scrapped
7149 */
7150 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007151 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007152 rdev->saved_raid_disk = -1;
7153
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 mddev->recovery = 0;
7155 /* flag recovery needed just to double check */
7156 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007157 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007158 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 goto unlock;
7160 }
Neil Brown72a23c22008-06-28 08:31:41 +10007161 /* Set RUNNING before clearing NEEDED to avoid
7162 * any transients in the value of "sync_action".
7163 */
7164 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7165 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007166 /* Clear some bits that don't mean anything, but
7167 * might be left set
7168 */
NeilBrown24dd4692005-11-08 21:39:26 -08007169 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7170 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007172 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7173 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 /* no recovery is running.
7175 * remove any failed drives, then
7176 * add spares if possible.
7177 * Spare are also removed and re-added, to allow
7178 * the personality to fail the re-add.
7179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007181 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007182 if (mddev->pers->check_reshape == NULL ||
7183 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007184 /* Cannot proceed */
7185 goto unlock;
7186 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007187 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007188 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007189 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7190 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007191 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007192 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007193 } else if (mddev->recovery_cp < MaxSector) {
7194 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007195 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007196 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7197 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007199
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007201 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7202 /* We are adding a device or devices to an array
7203 * which has the bitmap stored on all devices.
7204 * So make sure all bitmap pages get written
7205 */
7206 bitmap_write_all(mddev->bitmap);
7207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 mddev->sync_thread = md_register_thread(md_do_sync,
7209 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007210 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 if (!mddev->sync_thread) {
7212 printk(KERN_ERR "%s: could not start resync"
7213 " thread...\n",
7214 mdname(mddev));
7215 /* leave the spares where they are, it shouldn't hurt */
7216 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007217 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007219 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007220 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 }
7222 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007223 if (!mddev->sync_thread) {
7224 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7225 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7226 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007227 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007228 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 mddev_unlock(mddev);
7231 }
7232}
7233
Dan Williams6bfe0b42008-04-30 00:52:32 -07007234void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7235{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007236 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007237 wait_event_timeout(rdev->blocked_wait,
7238 !test_bit(Blocked, &rdev->flags),
7239 msecs_to_jiffies(5000));
7240 rdev_dec_pending(rdev, mddev);
7241}
7242EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7243
Adrian Bunk75c96f82005-05-05 16:16:09 -07007244static int md_notify_reboot(struct notifier_block *this,
7245 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246{
7247 struct list_head *tmp;
7248 mddev_t *mddev;
7249
7250 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7251
7252 printk(KERN_INFO "md: stopping all md devices.\n");
7253
NeilBrown29ac4aa2008-02-06 01:39:58 -08007254 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007255 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007256 /* Force a switch to readonly even array
7257 * appears to still be in use. Hence
7258 * the '100'.
7259 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007260 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007261 mddev_unlock(mddev);
7262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 /*
7264 * certain more exotic SCSI devices are known to be
7265 * volatile wrt too early system reboots. While the
7266 * right place to handle this issue is the given
7267 * driver, we do want to have a safe RAID driver ...
7268 */
7269 mdelay(1000*1);
7270 }
7271 return NOTIFY_DONE;
7272}
7273
Adrian Bunk75c96f82005-05-05 16:16:09 -07007274static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 .notifier_call = md_notify_reboot,
7276 .next = NULL,
7277 .priority = INT_MAX, /* before any real devices */
7278};
7279
7280static void md_geninit(void)
7281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7283
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007284 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285}
7286
Adrian Bunk75c96f82005-05-05 16:16:09 -07007287static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288{
Tejun Heoe804ac72010-10-15 15:36:08 +02007289 int ret = -ENOMEM;
7290
7291 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7292 if (!md_wq)
7293 goto err_wq;
7294
7295 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7296 if (!md_misc_wq)
7297 goto err_misc_wq;
7298
7299 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7300 goto err_md;
7301
7302 if ((ret = register_blkdev(0, "mdp")) < 0)
7303 goto err_mdp;
7304 mdp_major = ret;
7305
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007306 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007307 md_probe, NULL, NULL);
7308 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 md_probe, NULL, NULL);
7310
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007312 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313
7314 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Tejun Heoe804ac72010-10-15 15:36:08 +02007317err_mdp:
7318 unregister_blkdev(MD_MAJOR, "md");
7319err_md:
7320 destroy_workqueue(md_misc_wq);
7321err_misc_wq:
7322 destroy_workqueue(md_wq);
7323err_wq:
7324 return ret;
7325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326
7327#ifndef MODULE
7328
7329/*
7330 * Searches all registered partitions for autorun RAID arrays
7331 * at boot time.
7332 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007333
7334static LIST_HEAD(all_detected_devices);
7335struct detected_devices_node {
7336 struct list_head list;
7337 dev_t dev;
7338};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339
7340void md_autodetect_dev(dev_t dev)
7341{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007342 struct detected_devices_node *node_detected_dev;
7343
7344 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7345 if (node_detected_dev) {
7346 node_detected_dev->dev = dev;
7347 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7348 } else {
7349 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7350 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352}
7353
7354
7355static void autostart_arrays(int part)
7356{
7357 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007358 struct detected_devices_node *node_detected_dev;
7359 dev_t dev;
7360 int i_scanned, i_passed;
7361
7362 i_scanned = 0;
7363 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
7365 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7366
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007367 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7368 i_scanned++;
7369 node_detected_dev = list_entry(all_detected_devices.next,
7370 struct detected_devices_node, list);
7371 list_del(&node_detected_dev->list);
7372 dev = node_detected_dev->dev;
7373 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007374 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 if (IS_ERR(rdev))
7376 continue;
7377
NeilBrownb2d444d2005-11-08 21:39:31 -08007378 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 MD_BUG();
7380 continue;
7381 }
NeilBrownd0fae182008-03-04 14:29:31 -08007382 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007384 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007386
7387 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7388 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
7390 autorun_devices(part);
7391}
7392
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007393#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
7395static __exit void md_exit(void)
7396{
7397 mddev_t *mddev;
7398 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007399
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007400 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007401 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007403 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 unregister_blkdev(mdp_major, "mdp");
7405 unregister_reboot_notifier(&md_notifier);
7406 unregister_sysctl_table(raid_table_header);
7407 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007408 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007410 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007412 destroy_workqueue(md_misc_wq);
7413 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414}
7415
Dan Williams685784a2007-07-09 11:56:42 -07007416subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417module_exit(md_exit)
7418
NeilBrownf91de922005-11-08 21:39:36 -08007419static int get_ro(char *buffer, struct kernel_param *kp)
7420{
7421 return sprintf(buffer, "%d", start_readonly);
7422}
7423static int set_ro(const char *val, struct kernel_param *kp)
7424{
7425 char *e;
7426 int num = simple_strtoul(val, &e, 10);
7427 if (*val && (*e == '\0' || *e == '\n')) {
7428 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007429 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007430 }
7431 return -EINVAL;
7432}
7433
NeilBrown80ca3a42006-07-10 04:44:18 -07007434module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7435module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007436
NeilBrownefeb53c2009-01-09 08:31:10 +11007437module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007438
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439EXPORT_SYMBOL(register_md_personality);
7440EXPORT_SYMBOL(unregister_md_personality);
7441EXPORT_SYMBOL(md_error);
7442EXPORT_SYMBOL(md_done_sync);
7443EXPORT_SYMBOL(md_write_start);
7444EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445EXPORT_SYMBOL(md_register_thread);
7446EXPORT_SYMBOL(md_unregister_thread);
7447EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448EXPORT_SYMBOL(md_check_recovery);
7449MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007450MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007451MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007452MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);