blob: 84c46a161927999ebfc6c5d56d98e76247e66ae4 [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
Tejun Heoe9c74692010-09-03 11:56:18 +0200374static void submit_flushes(mddev_t *mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100375{
376 mdk_rdev_t *rdev;
377
378 rcu_read_lock();
379 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
380 if (rdev->raid_disk >= 0 &&
381 !test_bit(Faulty, &rdev->flags)) {
382 /* Take two references, one is dropped
383 * when request finishes, one after
384 * we reclaim rcu_read_lock
385 */
386 struct bio *bi;
387 atomic_inc(&rdev->nr_pending);
388 atomic_inc(&rdev->nr_pending);
389 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100390 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200391 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100392 bi->bi_private = rdev;
393 bi->bi_bdev = rdev->bdev;
394 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200395 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100396 rcu_read_lock();
397 rdev_dec_pending(rdev, mddev);
398 }
399 rcu_read_unlock();
400}
401
Tejun Heoe9c74692010-09-03 11:56:18 +0200402static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100403{
Tejun Heoe9c74692010-09-03 11:56:18 +0200404 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
405 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100406
407 atomic_set(&mddev->flush_pending, 1);
408
Tejun Heoe9c74692010-09-03 11:56:18 +0200409 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100410 /* an empty barrier - all done */
411 bio_endio(bio, 0);
412 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200413 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100414 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100415 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100416 }
417 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 mddev->flush_bio = NULL;
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 wake_up(&mddev->sb_wait);
420 }
421}
422
Tejun Heoe9c74692010-09-03 11:56:18 +0200423void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100424{
425 spin_lock_irq(&mddev->write_lock);
426 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100428 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200429 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 spin_unlock_irq(&mddev->write_lock);
431
432 atomic_set(&mddev->flush_pending, 1);
Tejun Heoe9c74692010-09-03 11:56:18 +0200433 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
NeilBrowna2826aa2009-12-14 12:49:49 +1100434
Tejun Heoe9c74692010-09-03 11:56:18 +0200435 submit_flushes(mddev);
NeilBrowna2826aa2009-12-14 12:49:49 +1100436
437 if (atomic_dec_and_test(&mddev->flush_pending))
Tejun Heoe804ac72010-10-15 15:36:08 +0200438 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100439}
Tejun Heoe9c74692010-09-03 11:56:18 +0200440EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100441
NeilBrown2ac87402010-06-01 19:37:29 +1000442/* Support for plugging.
443 * This mirrors the plugging support in request_queue, but does not
444 * require having a whole queue
445 */
446static void plugger_work(struct work_struct *work)
447{
448 struct plug_handle *plug =
449 container_of(work, struct plug_handle, unplug_work);
450 plug->unplug_fn(plug);
451}
452static void plugger_timeout(unsigned long data)
453{
454 struct plug_handle *plug = (void *)data;
455 kblockd_schedule_work(NULL, &plug->unplug_work);
456}
457void plugger_init(struct plug_handle *plug,
458 void (*unplug_fn)(struct plug_handle *))
459{
460 plug->unplug_flag = 0;
461 plug->unplug_fn = unplug_fn;
462 init_timer(&plug->unplug_timer);
463 plug->unplug_timer.function = plugger_timeout;
464 plug->unplug_timer.data = (unsigned long)plug;
465 INIT_WORK(&plug->unplug_work, plugger_work);
466}
467EXPORT_SYMBOL_GPL(plugger_init);
468
469void plugger_set_plug(struct plug_handle *plug)
470{
471 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
472 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
473}
474EXPORT_SYMBOL_GPL(plugger_set_plug);
475
476int plugger_remove_plug(struct plug_handle *plug)
477{
478 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
479 del_timer(&plug->unplug_timer);
480 return 1;
481 } else
482 return 0;
483}
484EXPORT_SYMBOL_GPL(plugger_remove_plug);
485
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static inline mddev_t *mddev_get(mddev_t *mddev)
488{
489 atomic_inc(&mddev->active);
490 return mddev;
491}
492
Dan Williams5fd3a172009-03-04 00:57:25 -0700493static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static void mddev_put(mddev_t *mddev)
496{
NeilBrowna167f662010-10-26 18:31:13 +1100497 struct bio_set *bs = NULL;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
500 return;
NeilBrownd3374822009-01-09 08:31:10 +1100501 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100502 mddev->ctime == 0 && !mddev->hold_active) {
503 /* Array is not configured at all, and not held active,
504 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100506 bs = mddev->bio_set;
507 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100508 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200509 /* We did a probe so need to clean up. Call
510 * queue_work inside the spinlock so that
511 * flush_workqueue() after mddev_find will
512 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100513 */
514 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200515 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100516 } else
517 kfree(mddev);
518 }
519 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100520 if (bs)
521 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
NeilBrown390ee602010-06-01 19:37:27 +1000524void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100525{
526 mutex_init(&mddev->open_mutex);
527 mutex_init(&mddev->reconfig_mutex);
528 mutex_init(&mddev->bitmap_info.mutex);
529 INIT_LIST_HEAD(&mddev->disks);
530 INIT_LIST_HEAD(&mddev->all_mddevs);
531 init_timer(&mddev->safemode_timer);
532 atomic_set(&mddev->active, 1);
533 atomic_set(&mddev->openers, 0);
534 atomic_set(&mddev->active_io, 0);
535 spin_lock_init(&mddev->write_lock);
536 atomic_set(&mddev->flush_pending, 0);
537 init_waitqueue_head(&mddev->sb_wait);
538 init_waitqueue_head(&mddev->recovery_wait);
539 mddev->reshape_position = MaxSector;
540 mddev->resync_min = 0;
541 mddev->resync_max = MaxSector;
542 mddev->level = LEVEL_NONE;
543}
NeilBrown390ee602010-06-01 19:37:27 +1000544EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static mddev_t * mddev_find(dev_t unit)
547{
548 mddev_t *mddev, *new = NULL;
549
550 retry:
551 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
NeilBrownefeb53c2009-01-09 08:31:10 +1100553 if (unit) {
554 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
555 if (mddev->unit == unit) {
556 mddev_get(mddev);
557 spin_unlock(&all_mddevs_lock);
558 kfree(new);
559 return mddev;
560 }
561
562 if (new) {
563 list_add(&new->all_mddevs, &all_mddevs);
564 spin_unlock(&all_mddevs_lock);
565 new->hold_active = UNTIL_IOCTL;
566 return new;
567 }
568 } else if (new) {
569 /* find an unused unit number */
570 static int next_minor = 512;
571 int start = next_minor;
572 int is_free = 0;
573 int dev = 0;
574 while (!is_free) {
575 dev = MKDEV(MD_MAJOR, next_minor);
576 next_minor++;
577 if (next_minor > MINORMASK)
578 next_minor = 0;
579 if (next_minor == start) {
580 /* Oh dear, all in use. */
581 spin_unlock(&all_mddevs_lock);
582 kfree(new);
583 return NULL;
584 }
585
586 is_free = 1;
587 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
588 if (mddev->unit == dev) {
589 is_free = 0;
590 break;
591 }
592 }
593 new->unit = dev;
594 new->md_minor = MINOR(dev);
595 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 list_add(&new->all_mddevs, &all_mddevs);
597 spin_unlock(&all_mddevs_lock);
598 return new;
599 }
600 spin_unlock(&all_mddevs_lock);
601
NeilBrown9ffae0c2006-01-06 00:20:32 -0800602 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!new)
604 return NULL;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 new->unit = unit;
607 if (MAJOR(unit) == MD_MAJOR)
608 new->md_minor = MINOR(unit);
609 else
610 new->md_minor = MINOR(unit) >> MdpMinorShift;
611
NeilBrownfafd7fb2010-04-01 15:55:30 +1100612 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto retry;
615}
616
617static inline int mddev_lock(mddev_t * mddev)
618{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800619 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Dan Williamsb522adc2009-03-31 15:00:31 +1100622static inline int mddev_is_locked(mddev_t *mddev)
623{
624 return mutex_is_locked(&mddev->reconfig_mutex);
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static inline int mddev_trylock(mddev_t * mddev)
628{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800629 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
NeilBrownb6eb1272010-04-15 10:13:47 +1000632static struct attribute_group md_redundancy_group;
633
NeilBrowna64c8762010-04-14 17:15:37 +1000634static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
NeilBrowna64c8762010-04-14 17:15:37 +1000636 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000637 /* These cannot be removed under reconfig_mutex as
638 * an access to the files will try to take reconfig_mutex
639 * while holding the file unremovable, which leads to
640 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000641 * So hold set sysfs_active while the remove in happeing,
642 * and anything else which might set ->to_remove or my
643 * otherwise change the sysfs namespace will fail with
644 * -EBUSY if sysfs_active is still set.
645 * We set sysfs_active under reconfig_mutex and elsewhere
646 * test it under the same mutex to ensure its correct value
647 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000648 */
NeilBrowna64c8762010-04-14 17:15:37 +1000649 struct attribute_group *to_remove = mddev->to_remove;
650 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000651 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000652 mutex_unlock(&mddev->reconfig_mutex);
653
NeilBrown00bcb4a2010-06-01 19:37:23 +1000654 if (mddev->kobj.sd) {
655 if (to_remove != &md_redundancy_group)
656 sysfs_remove_group(&mddev->kobj, to_remove);
657 if (mddev->pers == NULL ||
658 mddev->pers->sync_request == NULL) {
659 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
660 if (mddev->sysfs_action)
661 sysfs_put(mddev->sysfs_action);
662 mddev->sysfs_action = NULL;
663 }
NeilBrowna64c8762010-04-14 17:15:37 +1000664 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000665 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000666 } else
667 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
NeilBrown005eca52005-08-22 13:11:08 -0700669 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
NeilBrown2989ddb2006-01-06 00:20:43 -0800672static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100674 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100676 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (rdev->desc_nr == nr)
678 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return NULL;
681}
682
683static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mdk_rdev_t *rdev;
686
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100687 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (rdev->bdev->bd_dev == dev)
689 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return NULL;
692}
693
NeilBrownd9d166c2006-01-06 00:20:51 -0800694static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800695{
696 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800697 list_for_each_entry(pers, &pers_list, list) {
698 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800699 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800700 if (strcmp(pers->name, clevel)==0)
701 return pers;
702 }
NeilBrown2604b702006-01-06 00:20:36 -0800703 return NULL;
704}
705
Andre Nollb73df2d2008-07-11 22:02:23 +1000706/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700707static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Mike Snitzer77304d22010-11-08 14:39:12 +0100709 sector_t num_sectors = i_size_read(bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000710 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713static int alloc_disk_sb(mdk_rdev_t * rdev)
714{
715 if (rdev->sb_page)
716 MD_BUG();
717
718 rdev->sb_page = alloc_page(GFP_KERNEL);
719 if (!rdev->sb_page) {
720 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000721 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 return 0;
725}
726
727static void free_disk_sb(mdk_rdev_t * rdev)
728{
729 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800730 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 rdev->sb_loaded = 0;
732 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000733 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100734 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736}
737
738
NeilBrown6712ecf2007-09-27 12:47:43 +0200739static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700740{
741 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800742 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700743
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700744 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
745 printk("md: super_written gets error=%d, uptodate=%d\n",
746 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
747 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800748 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700749 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700750
NeilBrowna9701a32005-11-08 21:39:34 -0800751 if (atomic_dec_and_test(&mddev->pending_writes))
752 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700753 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700754}
755
756void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
757 sector_t sector, int size, struct page *page)
758{
759 /* write first size bytes of page to sector of rdev
760 * Increment mddev->pending_writes before returning
761 * and decrement it on completion, waking up sb_wait
762 * if zero is reached.
763 * If an error occurred, call md_error
764 */
NeilBrowna167f662010-10-26 18:31:13 +1100765 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700766
767 bio->bi_bdev = rdev->bdev;
768 bio->bi_sector = sector;
769 bio_add_page(bio, page, size, 0);
770 bio->bi_private = rdev;
771 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800772
NeilBrown7bfa19f2005-06-21 17:17:28 -0700773 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200774 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
775 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800776}
777
778void md_super_wait(mddev_t *mddev)
779{
Tejun Heoe9c74692010-09-03 11:56:18 +0200780 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800781 DEFINE_WAIT(wq);
782 for(;;) {
783 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
784 if (atomic_read(&mddev->pending_writes)==0)
785 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800786 schedule();
787 }
788 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700789}
790
NeilBrown6712ecf2007-09-27 12:47:43 +0200791static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
NeilBrown2b193362010-10-27 15:16:40 +1100796int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
797 struct page *page, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
NeilBrowna167f662010-10-26 18:31:13 +1100799 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct completion event;
801 int ret;
802
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200803 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
NeilBrown2b193362010-10-27 15:16:40 +1100805 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 bio->bi_sector = sector;
807 bio_add_page(bio, page, size, 0);
808 init_completion(&event);
809 bio->bi_private = &event;
810 bio->bi_end_io = bi_complete;
811 submit_bio(rw, bio);
812 wait_for_completion(&event);
813
814 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
815 bio_put(bio);
816 return ret;
817}
NeilBrowna8745db2006-01-06 00:20:34 -0800818EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
NeilBrown0002b272005-09-09 16:23:53 -0700820static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 char b[BDEVNAME_SIZE];
823 if (!rdev->sb_page) {
824 MD_BUG();
825 return -EINVAL;
826 }
827 if (rdev->sb_loaded)
828 return 0;
829
830
NeilBrown2b193362010-10-27 15:16:40 +1100831 if (!sync_page_io(rdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto fail;
833 rdev->sb_loaded = 1;
834 return 0;
835
836fail:
837 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
838 bdevname(rdev->bdev,b));
839 return -EINVAL;
840}
841
842static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
843{
Andre Noll05710462008-07-11 22:02:20 +1000844 return sb1->set_uuid0 == sb2->set_uuid0 &&
845 sb1->set_uuid1 == sb2->set_uuid1 &&
846 sb1->set_uuid2 == sb2->set_uuid2 &&
847 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
851{
852 int ret;
853 mdp_super_t *tmp1, *tmp2;
854
855 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
856 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
857
858 if (!tmp1 || !tmp2) {
859 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100860 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 goto abort;
862 }
863
864 *tmp1 = *sb1;
865 *tmp2 = *sb2;
866
867 /*
868 * nr_disks is not constant
869 */
870 tmp1->nr_disks = 0;
871 tmp2->nr_disks = 0;
872
Andre Nollce0c8e02008-07-11 22:02:20 +1000873 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700875 kfree(tmp1);
876 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return ret;
878}
879
NeilBrown4d167f02007-05-09 02:35:37 -0700880
881static u32 md_csum_fold(u32 csum)
882{
883 csum = (csum & 0xffff) + (csum >> 16);
884 return (csum & 0xffff) + (csum >> 16);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static unsigned int calc_sb_csum(mdp_super_t * sb)
888{
NeilBrown4d167f02007-05-09 02:35:37 -0700889 u64 newcsum = 0;
890 u32 *sb32 = (u32*)sb;
891 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 unsigned int disk_csum, csum;
893
894 disk_csum = sb->sb_csum;
895 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700896
897 for (i = 0; i < MD_SB_BYTES/4 ; i++)
898 newcsum += sb32[i];
899 csum = (newcsum & 0xffffffff) + (newcsum>>32);
900
901
902#ifdef CONFIG_ALPHA
903 /* This used to use csum_partial, which was wrong for several
904 * reasons including that different results are returned on
905 * different architectures. It isn't critical that we get exactly
906 * the same return value as before (we always csum_fold before
907 * testing, and that removes any differences). However as we
908 * know that csum_partial always returned a 16bit value on
909 * alphas, do a fold to maximise conformity to previous behaviour.
910 */
911 sb->sb_csum = md_csum_fold(disk_csum);
912#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return csum;
916}
917
918
919/*
920 * Handle superblock details.
921 * We want to be able to handle multiple superblock formats
922 * so we have a common interface to them all, and an array of
923 * different handlers.
924 * We rely on user-space to write the initial superblock, and support
925 * reading and updating of superblocks.
926 * Interface methods are:
927 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
928 * loads and validates a superblock on dev.
929 * if refdev != NULL, compare superblocks on both devices
930 * Return:
931 * 0 - dev has a superblock that is compatible with refdev
932 * 1 - dev has a superblock that is compatible and newer than refdev
933 * so dev should be used as the refdev in future
934 * -EINVAL superblock incompatible or invalid
935 * -othererror e.g. -EIO
936 *
937 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
938 * Verify that dev is acceptable into mddev.
939 * The first time, mddev->raid_disks will be 0, and data from
940 * dev should be merged in. Subsequent calls check that dev
941 * is new enough. Return 0 or -EINVAL
942 *
943 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
944 * Update the superblock for rdev with data in mddev
945 * This does not write to disc.
946 *
947 */
948
949struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000950 char *name;
951 struct module *owner;
952 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
953 int minor_version);
954 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
955 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
956 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000957 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958};
959
960/*
Andre Noll0894cc32009-06-18 08:49:23 +1000961 * Check that the given mddev has no bitmap.
962 *
963 * This function is called from the run method of all personalities that do not
964 * support bitmaps. It prints an error message and returns non-zero if mddev
965 * has a bitmap. Otherwise, it returns 0.
966 *
967 */
968int md_check_no_bitmap(mddev_t *mddev)
969{
NeilBrownc3d97142009-12-14 12:49:52 +1100970 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000971 return 0;
972 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
973 mdname(mddev), mddev->pers->name);
974 return 1;
975}
976EXPORT_SYMBOL(md_check_no_bitmap);
977
978/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 * load_super for 0.90.0
980 */
981static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
982{
983 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
984 mdp_super_t *sb;
985 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /*
Andre Noll0f420352008-07-11 22:02:23 +1000988 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * it's at the end of the disk.
990 *
991 * It also happens to be a multiple of 4Kb.
992 */
Andre Noll0f420352008-07-11 22:02:23 +1000993 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
NeilBrown0002b272005-09-09 16:23:53 -0700995 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (ret) return ret;
997
998 ret = -EINVAL;
999
1000 bdevname(rdev->bdev, b);
1001 sb = (mdp_super_t*)page_address(rdev->sb_page);
1002
1003 if (sb->md_magic != MD_SB_MAGIC) {
1004 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1005 b);
1006 goto abort;
1007 }
1008
1009 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001010 sb->minor_version < 90 ||
1011 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1013 sb->major_version, sb->minor_version,
1014 b);
1015 goto abort;
1016 }
1017
1018 if (sb->raid_disks <= 0)
1019 goto abort;
1020
NeilBrown4d167f02007-05-09 02:35:37 -07001021 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1023 b);
1024 goto abort;
1025 }
1026
1027 rdev->preferred_minor = sb->md_minor;
1028 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001029 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (sb->level == LEVEL_MULTIPATH)
1032 rdev->desc_nr = -1;
1033 else
1034 rdev->desc_nr = sb->this_disk.number;
1035
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001036 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001038 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 __u64 ev1, ev2;
1040 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1041 if (!uuid_equal(refsb, sb)) {
1042 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1043 b, bdevname(refdev->bdev,b2));
1044 goto abort;
1045 }
1046 if (!sb_equal(refsb, sb)) {
1047 printk(KERN_WARNING "md: %s has same UUID"
1048 " but different superblock to %s\n",
1049 b, bdevname(refdev->bdev, b2));
1050 goto abort;
1051 }
1052 ev1 = md_event(sb);
1053 ev2 = md_event(refsb);
1054 if (ev1 > ev2)
1055 ret = 1;
1056 else
1057 ret = 0;
1058 }
NeilBrown8190e752009-06-18 08:48:58 +10001059 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Andre Nolldd8ac332009-03-31 14:33:13 +11001061 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001062 /* "this cannot possibly happen" ... */
1063 ret = -EINVAL;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 abort:
1066 return ret;
1067}
1068
1069/*
1070 * validate_super for 0.90.0
1071 */
1072static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1073{
1074 mdp_disk_t *desc;
1075 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001076 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
NeilBrown41158c72005-06-21 17:17:25 -07001078 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001079 clear_bit(Faulty, &rdev->flags);
1080 clear_bit(In_sync, &rdev->flags);
1081 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (mddev->raid_disks == 0) {
1084 mddev->major_version = 0;
1085 mddev->minor_version = sb->minor_version;
1086 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001087 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001088 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 mddev->ctime = sb->ctime;
1090 mddev->utime = sb->utime;
1091 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001092 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 mddev->layout = sb->layout;
1094 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001095 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001096 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001097 mddev->bitmap_info.offset = 0;
1098 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
NeilBrownf6705572006-03-27 01:18:11 -08001100 if (mddev->minor_version >= 91) {
1101 mddev->reshape_position = sb->reshape_position;
1102 mddev->delta_disks = sb->delta_disks;
1103 mddev->new_level = sb->new_level;
1104 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001105 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001106 } else {
1107 mddev->reshape_position = MaxSector;
1108 mddev->delta_disks = 0;
1109 mddev->new_level = mddev->level;
1110 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001111 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (sb->state & (1<<MD_SB_CLEAN))
1115 mddev->recovery_cp = MaxSector;
1116 else {
1117 if (sb->events_hi == sb->cp_events_hi &&
1118 sb->events_lo == sb->cp_events_lo) {
1119 mddev->recovery_cp = sb->recovery_cp;
1120 } else
1121 mddev->recovery_cp = 0;
1122 }
1123
1124 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1125 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1126 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1127 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1128
1129 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001130
1131 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001132 mddev->bitmap_info.file == NULL)
1133 mddev->bitmap_info.offset =
1134 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001135
NeilBrown41158c72005-06-21 17:17:25 -07001136 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001137 /* Insist on good event counter while assembling, except
1138 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001140 if (sb->disks[rdev->desc_nr].state & (
1141 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1142 if (ev1 < mddev->events)
1143 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001144 } else if (mddev->bitmap) {
1145 /* if adding to array with a bitmap, then we can accept an
1146 * older device ... but not too old.
1147 */
NeilBrown41158c72005-06-21 17:17:25 -07001148 if (ev1 < mddev->bitmap->events_cleared)
1149 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001150 } else {
1151 if (ev1 < mddev->events)
1152 /* just a hot-add of a new device, leave raid_disk at -1 */
1153 return 0;
1154 }
NeilBrown41158c72005-06-21 17:17:25 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 desc = sb->disks + rdev->desc_nr;
1158
1159 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001160 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001161 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1162 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001163 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001165 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1166 /* active but not in sync implies recovery up to
1167 * reshape position. We don't know exactly where
1168 * that is, so set to zero for now */
1169 if (mddev->minor_version >= 91) {
1170 rdev->recovery_offset = 0;
1171 rdev->raid_disk = desc->raid_disk;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001174 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1175 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001176 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001177 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return 0;
1179}
1180
1181/*
1182 * sync_super for 0.90.0
1183 */
1184static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1185{
1186 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mdk_rdev_t *rdev2;
1188 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* make rdev->sb match mddev data..
1192 *
1193 * 1/ zero out disks
1194 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1195 * 3/ any empty disks < next_spare become removed
1196 *
1197 * disks[0] gets initialised to REMOVED because
1198 * we cannot be sure from other fields if it has
1199 * been initialised or not.
1200 */
1201 int i;
1202 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1203
NeilBrown61181562005-09-09 16:24:02 -07001204 rdev->sb_size = MD_SB_BYTES;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 sb = (mdp_super_t*)page_address(rdev->sb_page);
1207
1208 memset(sb, 0, sizeof(*sb));
1209
1210 sb->md_magic = MD_SB_MAGIC;
1211 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 sb->patch_version = mddev->patch_version;
1213 sb->gvalid_words = 0; /* ignored */
1214 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1215 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1216 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1217 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1218
1219 sb->ctime = mddev->ctime;
1220 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001221 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sb->raid_disks = mddev->raid_disks;
1223 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001224 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 sb->utime = mddev->utime;
1226 sb->state = 0;
1227 sb->events_hi = (mddev->events>>32);
1228 sb->events_lo = (u32)mddev->events;
1229
NeilBrownf6705572006-03-27 01:18:11 -08001230 if (mddev->reshape_position == MaxSector)
1231 sb->minor_version = 90;
1232 else {
1233 sb->minor_version = 91;
1234 sb->reshape_position = mddev->reshape_position;
1235 sb->new_level = mddev->new_level;
1236 sb->delta_disks = mddev->delta_disks;
1237 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001238 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001239 }
1240 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (mddev->in_sync)
1242 {
1243 sb->recovery_cp = mddev->recovery_cp;
1244 sb->cp_events_hi = (mddev->events>>32);
1245 sb->cp_events_lo = (u32)mddev->events;
1246 if (mddev->recovery_cp == MaxSector)
1247 sb->state = (1<< MD_SB_CLEAN);
1248 } else
1249 sb->recovery_cp = 0;
1250
1251 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001252 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
NeilBrownc3d97142009-12-14 12:49:52 +11001254 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001255 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001258 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001260 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001261 int is_active = test_bit(In_sync, &rdev2->flags);
1262
1263 if (rdev2->raid_disk >= 0 &&
1264 sb->minor_version >= 91)
1265 /* we have nowhere to store the recovery_offset,
1266 * but if it is not below the reshape_position,
1267 * we can piggy-back on that.
1268 */
1269 is_active = 1;
1270 if (rdev2->raid_disk < 0 ||
1271 test_bit(Faulty, &rdev2->flags))
1272 is_active = 0;
1273 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001274 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001276 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001277 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 d = &sb->disks[rdev2->desc_nr];
1279 nr_disks++;
1280 d->number = rdev2->desc_nr;
1281 d->major = MAJOR(rdev2->bdev->bd_dev);
1282 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 d->raid_disk = rdev2->raid_disk;
1285 else
1286 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001287 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001289 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001291 if (test_bit(In_sync, &rdev2->flags))
1292 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 active++;
1294 working++;
1295 } else {
1296 d->state = 0;
1297 spare++;
1298 working++;
1299 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001300 if (test_bit(WriteMostly, &rdev2->flags))
1301 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* now set the "removed" and "faulty" bits on any missing devices */
1304 for (i=0 ; i < mddev->raid_disks ; i++) {
1305 mdp_disk_t *d = &sb->disks[i];
1306 if (d->state == 0 && d->number == 0) {
1307 d->number = i;
1308 d->raid_disk = i;
1309 d->state = (1<<MD_DISK_REMOVED);
1310 d->state |= (1<<MD_DISK_FAULTY);
1311 failed++;
1312 }
1313 }
1314 sb->nr_disks = nr_disks;
1315 sb->active_disks = active;
1316 sb->working_disks = working;
1317 sb->failed_disks = failed;
1318 sb->spare_disks = spare;
1319
1320 sb->this_disk = sb->disks[rdev->desc_nr];
1321 sb->sb_csum = calc_sb_csum(sb);
1322}
1323
1324/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 * rdev_size_change for 0.90.0
1326 */
1327static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001328super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001329{
Andre Noll58c0fed2009-03-31 14:33:13 +11001330 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001332 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001334 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001335 if (!num_sectors || num_sectors > rdev->sb_start)
1336 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001337 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338 rdev->sb_page);
1339 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001340 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341}
1342
1343
1344/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * version 1 superblock
1346 */
1347
NeilBrown1c05b4b2006-10-21 10:24:08 -07001348static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001350 __le32 disk_csum;
1351 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 unsigned long long newcsum;
1353 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001354 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int i;
1356
1357 disk_csum = sb->sb_csum;
1358 sb->sb_csum = 0;
1359 newcsum = 0;
1360 for (i=0; size>=4; size -= 4 )
1361 newcsum += le32_to_cpu(*isuper++);
1362
1363 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001364 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1367 sb->sb_csum = disk_csum;
1368 return cpu_to_le32(csum);
1369}
1370
1371static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1372{
1373 struct mdp_superblock_1 *sb;
1374 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001375 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001377 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /*
Andre Noll0f420352008-07-11 22:02:23 +10001380 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * It is always aligned to a 4K boundary and
1382 * depeding on minor_version, it can be:
1383 * 0: At least 8K, but less than 12K, from end of device
1384 * 1: At start of device
1385 * 2: 4K from start of device.
1386 */
1387 switch(minor_version) {
1388 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001389 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001390 sb_start -= 8*2;
1391 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 break;
1393 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001394 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001397 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 default:
1400 return -EINVAL;
1401 }
Andre Noll0f420352008-07-11 22:02:23 +10001402 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
NeilBrown0002b272005-09-09 16:23:53 -07001404 /* superblock is rarely larger than 1K, but it can be larger,
1405 * and it is safe to read 4k, so we do that
1406 */
1407 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (ret) return ret;
1409
1410
1411 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1412
1413 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1414 sb->major_version != cpu_to_le32(1) ||
1415 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001416 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001417 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EINVAL;
1419
1420 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1421 printk("md: invalid superblock checksum on %s\n",
1422 bdevname(rdev->bdev,b));
1423 return -EINVAL;
1424 }
1425 if (le64_to_cpu(sb->data_size) < 10) {
1426 printk("md: data_size too small on %s\n",
1427 bdevname(rdev->bdev,b));
1428 return -EINVAL;
1429 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rdev->preferred_minor = 0xffff;
1432 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001433 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
NeilBrown0002b272005-09-09 16:23:53 -07001435 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001436 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001437 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001438 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1439
1440 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001441 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001442 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001443
NeilBrown31b65a02006-07-10 04:44:14 -07001444 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1445 rdev->desc_nr = -1;
1446 else
1447 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1448
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001449 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001450 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 __u64 ev1, ev2;
1453 struct mdp_superblock_1 *refsb =
1454 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1455
1456 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1457 sb->level != refsb->level ||
1458 sb->layout != refsb->layout ||
1459 sb->chunksize != refsb->chunksize) {
1460 printk(KERN_WARNING "md: %s has strangely different"
1461 " superblock to %s\n",
1462 bdevname(rdev->bdev,b),
1463 bdevname(refdev->bdev,b2));
1464 return -EINVAL;
1465 }
1466 ev1 = le64_to_cpu(sb->events);
1467 ev2 = le64_to_cpu(refsb->events);
1468
1469 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001470 ret = 1;
1471 else
1472 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
NeilBrowna1801f82008-03-04 14:29:31 -08001474 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001475 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001476 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001478 rdev->sectors = rdev->sb_start;
1479 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001481 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001482 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001483 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001484 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1488{
1489 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001490 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
NeilBrown41158c72005-06-21 17:17:25 -07001492 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001493 clear_bit(Faulty, &rdev->flags);
1494 clear_bit(In_sync, &rdev->flags);
1495 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (mddev->raid_disks == 0) {
1498 mddev->major_version = 1;
1499 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001500 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001501 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1503 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1504 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001505 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mddev->layout = le32_to_cpu(sb->layout);
1507 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001508 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001509 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001510 mddev->bitmap_info.offset = 0;
1511 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1514 memcpy(mddev->uuid, sb->set_uuid, 16);
1515
1516 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001517
NeilBrown71c08052005-09-09 16:23:51 -07001518 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001519 mddev->bitmap_info.file == NULL )
1520 mddev->bitmap_info.offset =
1521 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001522
NeilBrownf6705572006-03-27 01:18:11 -08001523 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1524 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1525 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1526 mddev->new_level = le32_to_cpu(sb->new_level);
1527 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001528 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001529 } else {
1530 mddev->reshape_position = MaxSector;
1531 mddev->delta_disks = 0;
1532 mddev->new_level = mddev->level;
1533 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001534 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001535 }
1536
NeilBrown41158c72005-06-21 17:17:25 -07001537 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001538 /* Insist of good event counter while assembling, except for
1539 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001541 if (rdev->desc_nr >= 0 &&
1542 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1543 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1544 if (ev1 < mddev->events)
1545 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001546 } else if (mddev->bitmap) {
1547 /* If adding to array with a bitmap, then we can accept an
1548 * older device, but not too old.
1549 */
NeilBrown41158c72005-06-21 17:17:25 -07001550 if (ev1 < mddev->bitmap->events_cleared)
1551 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001552 } else {
1553 if (ev1 < mddev->events)
1554 /* just a hot-add of a new device, leave raid_disk at -1 */
1555 return 0;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (mddev->level != LEVEL_MULTIPATH) {
1558 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001559 if (rdev->desc_nr < 0 ||
1560 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1561 role = 0xffff;
1562 rdev->desc_nr = -1;
1563 } else
1564 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 switch(role) {
1566 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 break;
1568 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001569 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 break;
1571 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001572 if ((le32_to_cpu(sb->feature_map) &
1573 MD_FEATURE_RECOVERY_OFFSET))
1574 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1575 else
1576 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 rdev->raid_disk = role;
1578 break;
1579 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001580 if (sb->devflags & WriteMostly1)
1581 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001582 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001583 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586}
1587
1588static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1589{
1590 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 mdk_rdev_t *rdev2;
1592 int max_dev, i;
1593 /* make rdev->sb match mddev and rdev data. */
1594
1595 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1596
1597 sb->feature_map = 0;
1598 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001599 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 memset(sb->pad1, 0, sizeof(sb->pad1));
1601 memset(sb->pad2, 0, sizeof(sb->pad2));
1602 memset(sb->pad3, 0, sizeof(sb->pad3));
1603
1604 sb->utime = cpu_to_le64((__u64)mddev->utime);
1605 sb->events = cpu_to_le64(mddev->events);
1606 if (mddev->in_sync)
1607 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1608 else
1609 sb->resync_offset = cpu_to_le64(0);
1610
NeilBrown1c05b4b2006-10-21 10:24:08 -07001611 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001612
NeilBrownf0ca3402006-02-02 14:28:04 -08001613 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001614 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001615 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001616 sb->level = cpu_to_le32(mddev->level);
1617 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001618
NeilBrownc3d97142009-12-14 12:49:52 +11001619 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1620 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001621 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001622 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001623
1624 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001625 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001626 sb->feature_map |=
1627 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1628 sb->recovery_offset =
1629 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001630 }
1631
NeilBrownf6705572006-03-27 01:18:11 -08001632 if (mddev->reshape_position != MaxSector) {
1633 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1634 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1635 sb->new_layout = cpu_to_le32(mddev->new_layout);
1636 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1637 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001638 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001639 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001642 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (rdev2->desc_nr+1 > max_dev)
1644 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001645
NeilBrown70471da2009-08-03 10:59:57 +10001646 if (max_dev > le32_to_cpu(sb->max_dev)) {
1647 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001648 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001649 rdev->sb_size = max_dev * 2 + 256;
1650 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1651 if (rdev->sb_size & bmask)
1652 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001653 } else
1654 max_dev = le32_to_cpu(sb->max_dev);
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 for (i=0; i<max_dev;i++)
1657 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1658
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001659 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001661 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001663 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001665 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001666 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 else
1668 sb->dev_roles[i] = cpu_to_le16(0xffff);
1669 }
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 sb->sb_csum = calc_sb_1_csum(sb);
1672}
1673
Chris Webb0cd17fe2008-06-28 08:31:46 +10001674static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001675super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001676{
1677 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001678 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001679 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001680 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001681 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001682 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001683 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001684 max_sectors -= rdev->data_offset;
1685 if (!num_sectors || num_sectors > max_sectors)
1686 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001687 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001688 /* minor version 0 with bitmap we can't move */
1689 return 0;
1690 } else {
1691 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001692 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001693 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001694 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001695 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001696 if (!num_sectors || num_sectors > max_sectors)
1697 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001698 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001699 }
1700 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001701 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001702 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001703 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001704 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001705 rdev->sb_page);
1706 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001707 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Adrian Bunk75c96f82005-05-05 16:16:09 -07001710static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 [0] = {
1712 .name = "0.90.0",
1713 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001714 .load_super = super_90_load,
1715 .validate_super = super_90_validate,
1716 .sync_super = super_90_sync,
1717 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 },
1719 [1] = {
1720 .name = "md-1",
1721 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001722 .load_super = super_1_load,
1723 .validate_super = super_1_validate,
1724 .sync_super = super_1_sync,
1725 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 },
1727};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1730{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001731 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
NeilBrown4b809912008-07-21 17:05:25 +10001733 rcu_read_lock();
1734 rdev_for_each_rcu(rdev, mddev1)
1735 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001736 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001737 rdev2->bdev->bd_contains) {
1738 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001739 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001740 }
1741 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
1744
1745static LIST_HEAD(pending_raid_disks);
1746
Andre Nollac5e7112009-08-03 10:59:47 +10001747/*
1748 * Try to register data integrity profile for an mddev
1749 *
1750 * This is called when an array is started and after a disk has been kicked
1751 * from the array. It only succeeds if all working and active component devices
1752 * are integrity capable with matching profiles.
1753 */
1754int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001755{
Andre Nollac5e7112009-08-03 10:59:47 +10001756 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001757
Andre Nollac5e7112009-08-03 10:59:47 +10001758 if (list_empty(&mddev->disks))
1759 return 0; /* nothing to do */
1760 if (blk_get_integrity(mddev->gendisk))
1761 return 0; /* already registered */
1762 list_for_each_entry(rdev, &mddev->disks, same_set) {
1763 /* skip spares and non-functional disks */
1764 if (test_bit(Faulty, &rdev->flags))
1765 continue;
1766 if (rdev->raid_disk < 0)
1767 continue;
1768 /*
1769 * If at least one rdev is not integrity capable, we can not
1770 * enable data integrity for the md device.
1771 */
1772 if (!bdev_get_integrity(rdev->bdev))
1773 return -EINVAL;
1774 if (!reference) {
1775 /* Use the first rdev as the reference */
1776 reference = rdev;
1777 continue;
1778 }
1779 /* does this rdev's profile match the reference profile? */
1780 if (blk_integrity_compare(reference->bdev->bd_disk,
1781 rdev->bdev->bd_disk) < 0)
1782 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001783 }
Andre Nollac5e7112009-08-03 10:59:47 +10001784 /*
1785 * All component devices are integrity capable and have matching
1786 * profiles, register the common profile for the md device.
1787 */
1788 if (blk_integrity_register(mddev->gendisk,
1789 bdev_get_integrity(reference->bdev)) != 0) {
1790 printk(KERN_ERR "md: failed to register integrity for %s\n",
1791 mdname(mddev));
1792 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001793 }
Andre Nollac5e7112009-08-03 10:59:47 +10001794 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1795 mdname(mddev));
1796 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001797}
Andre Nollac5e7112009-08-03 10:59:47 +10001798EXPORT_SYMBOL(md_integrity_register);
1799
1800/* Disable data integrity if non-capable/non-matching disk is being added */
1801void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1802{
1803 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1804 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1805
1806 if (!bi_mddev) /* nothing to do */
1807 return;
1808 if (rdev->raid_disk < 0) /* skip spares */
1809 return;
1810 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1811 rdev->bdev->bd_disk) >= 0)
1812 return;
1813 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1814 blk_integrity_unregister(mddev->gendisk);
1815}
1816EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1819{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001820 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001821 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001822 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001823 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 if (rdev->mddev) {
1826 MD_BUG();
1827 return -EINVAL;
1828 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001829
1830 /* prevent duplicates */
1831 if (find_rdev(mddev, rdev->bdev->bd_dev))
1832 return -EEXIST;
1833
Andre Nolldd8ac332009-03-31 14:33:13 +11001834 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1835 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1836 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001837 if (mddev->pers) {
1838 /* Cannot change size, so fail
1839 * If mddev->level <= 0, then we don't care
1840 * about aligning sizes (e.g. linear)
1841 */
1842 if (mddev->level > 0)
1843 return -ENOSPC;
1844 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001845 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* Verify rdev->desc_nr is unique.
1849 * If it is -1, assign a free number, else
1850 * check number is not in use
1851 */
1852 if (rdev->desc_nr < 0) {
1853 int choice = 0;
1854 if (mddev->pers) choice = mddev->raid_disks;
1855 while (find_rdev_nr(mddev, choice))
1856 choice++;
1857 rdev->desc_nr = choice;
1858 } else {
1859 if (find_rdev_nr(mddev, rdev->desc_nr))
1860 return -EBUSY;
1861 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001862 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1863 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1864 mdname(mddev), mddev->max_disks);
1865 return -EBUSY;
1866 }
NeilBrown19133a42005-11-08 21:39:35 -08001867 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001868 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001869 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001872 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001873
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001874 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001875 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001876
Tejun Heo0762b8b2008-08-25 19:56:12 +09001877 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001878 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1879 /* failure here is OK */;
1880 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001881
NeilBrown4b809912008-07-21 17:05:25 +10001882 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001883 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001884
1885 /* May as well allow recovery to be retried once */
1886 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001889
1890 fail:
1891 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1892 b, mdname(mddev));
1893 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
NeilBrown177a99b2008-02-06 01:39:56 -08001896static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001897{
1898 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1899 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001900 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1904{
1905 char b[BDEVNAME_SIZE];
1906 if (!rdev->mddev) {
1907 MD_BUG();
1908 return;
1909 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001910 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001911 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1913 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001914 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001915 sysfs_put(rdev->sysfs_state);
1916 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001917 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001918 * writing to 'remove' to "dev/state". We also need
1919 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001920 */
NeilBrown4b809912008-07-21 17:05:25 +10001921 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001922 INIT_WORK(&rdev->del_work, md_delayed_delete);
1923 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001924 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927/*
1928 * prevent the device from being mounted, repartitioned or
1929 * otherwise reused by a RAID array (or any other kernel
1930 * subsystem), by bd_claiming the device.
1931 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001932static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 int err = 0;
1935 struct block_device *bdev;
1936 char b[BDEVNAME_SIZE];
1937
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001938 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (IS_ERR(bdev)) {
1940 printk(KERN_ERR "md: could not open %s.\n",
1941 __bdevname(dev, b));
1942 return PTR_ERR(bdev);
1943 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001944 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (err) {
1946 printk(KERN_ERR "md: could not bd_claim %s.\n",
1947 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001948 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return err;
1950 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001951 if (!shared)
1952 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 rdev->bdev = bdev;
1954 return err;
1955}
1956
1957static void unlock_rdev(mdk_rdev_t *rdev)
1958{
1959 struct block_device *bdev = rdev->bdev;
1960 rdev->bdev = NULL;
1961 if (!bdev)
1962 MD_BUG();
1963 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001964 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967void md_autodetect_dev(dev_t dev);
1968
1969static void export_rdev(mdk_rdev_t * rdev)
1970{
1971 char b[BDEVNAME_SIZE];
1972 printk(KERN_INFO "md: export_rdev(%s)\n",
1973 bdevname(rdev->bdev,b));
1974 if (rdev->mddev)
1975 MD_BUG();
1976 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001978 if (test_bit(AutoDetected, &rdev->flags))
1979 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#endif
1981 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001982 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985static void kick_rdev_from_array(mdk_rdev_t * rdev)
1986{
1987 unbind_rdev_from_array(rdev);
1988 export_rdev(rdev);
1989}
1990
1991static void export_array(mddev_t *mddev)
1992{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001993 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
NeilBrownd089c6a2008-02-06 01:39:59 -08001995 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (!rdev->mddev) {
1997 MD_BUG();
1998 continue;
1999 }
2000 kick_rdev_from_array(rdev);
2001 }
2002 if (!list_empty(&mddev->disks))
2003 MD_BUG();
2004 mddev->raid_disks = 0;
2005 mddev->major_version = 0;
2006}
2007
2008static void print_desc(mdp_disk_t *desc)
2009{
2010 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2011 desc->major,desc->minor,desc->raid_disk,desc->state);
2012}
2013
Cheng Renquancd2ac932009-01-09 08:31:08 +11002014static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 int i;
2017
2018 printk(KERN_INFO
2019 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2020 sb->major_version, sb->minor_version, sb->patch_version,
2021 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2022 sb->ctime);
2023 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2024 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2025 sb->md_minor, sb->layout, sb->chunk_size);
2026 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2027 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2028 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2029 sb->failed_disks, sb->spare_disks,
2030 sb->sb_csum, (unsigned long)sb->events_lo);
2031
2032 printk(KERN_INFO);
2033 for (i = 0; i < MD_SB_DISKS; i++) {
2034 mdp_disk_t *desc;
2035
2036 desc = sb->disks + i;
2037 if (desc->number || desc->major || desc->minor ||
2038 desc->raid_disk || (desc->state && (desc->state != 4))) {
2039 printk(" D %2d: ", i);
2040 print_desc(desc);
2041 }
2042 }
2043 printk(KERN_INFO "md: THIS: ");
2044 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
Cheng Renquancd2ac932009-01-09 08:31:08 +11002047static void print_sb_1(struct mdp_superblock_1 *sb)
2048{
2049 __u8 *uuid;
2050
2051 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002052 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002053 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002054 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002055 le32_to_cpu(sb->major_version),
2056 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002057 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002058 sb->set_name,
2059 (unsigned long long)le64_to_cpu(sb->ctime)
2060 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2061
2062 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002063 printk(KERN_INFO
2064 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002065 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002066 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002067 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2068 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002069 le32_to_cpu(sb->level),
2070 (unsigned long long)le64_to_cpu(sb->size),
2071 le32_to_cpu(sb->raid_disks),
2072 le32_to_cpu(sb->layout),
2073 le32_to_cpu(sb->chunksize),
2074 (unsigned long long)le64_to_cpu(sb->data_offset),
2075 (unsigned long long)le64_to_cpu(sb->data_size),
2076 (unsigned long long)le64_to_cpu(sb->super_offset),
2077 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2078 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002079 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002080 sb->devflags,
2081 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2082 (unsigned long long)le64_to_cpu(sb->events),
2083 (unsigned long long)le64_to_cpu(sb->resync_offset),
2084 le32_to_cpu(sb->sb_csum),
2085 le32_to_cpu(sb->max_dev)
2086 );
2087}
2088
2089static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002092 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2093 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002094 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2095 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002097 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2098 switch (major_version) {
2099 case 0:
2100 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2101 break;
2102 case 1:
2103 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2104 break;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else
2107 printk(KERN_INFO "md: no rdev superblock!\n");
2108}
2109
Adrian Bunk5e563412006-06-26 00:27:42 -07002110static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002112 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 mdk_rdev_t *rdev;
2114 mddev_t *mddev;
2115 char b[BDEVNAME_SIZE];
2116
2117 printk("\n");
2118 printk("md: **********************************\n");
2119 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2120 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002121 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
NeilBrown32a76272005-06-21 17:17:14 -07002123 if (mddev->bitmap)
2124 bitmap_print_sb(mddev->bitmap);
2125 else
2126 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002127 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 printk("<%s>", bdevname(rdev->bdev,b));
2129 printk("\n");
2130
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002131 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002132 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 printk("md: **********************************\n");
2135 printk("\n");
2136}
2137
2138
NeilBrown42543762006-06-26 00:27:57 -07002139static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
NeilBrown42543762006-06-26 00:27:57 -07002141 /* Update each superblock (in-memory image), but
2142 * if we are allowed to, skip spares which already
2143 * have the right event counter, or have one earlier
2144 * (which would mean they aren't being marked as dirty
2145 * with the rest of the array)
2146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002148 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002149 if (rdev->sb_events == mddev->events ||
2150 (nospares &&
2151 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002152 rdev->sb_events+1 == mddev->events)) {
2153 /* Don't update this superblock */
2154 rdev->sb_loaded = 2;
2155 } else {
2156 super_types[mddev->major_version].
2157 sync_super(mddev, rdev);
2158 rdev->sb_loaded = 1;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161}
2162
NeilBrown850b2b42006-10-03 01:15:46 -07002163static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002166 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002167 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002170 /* First make sure individual recovery_offsets are correct */
2171 list_for_each_entry(rdev, &mddev->disks, same_set) {
2172 if (rdev->raid_disk >= 0 &&
2173 mddev->delta_disks >= 0 &&
2174 !test_bit(In_sync, &rdev->flags) &&
2175 mddev->curr_resync_completed > rdev->recovery_offset)
2176 rdev->recovery_offset = mddev->curr_resync_completed;
2177
2178 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002179 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002180 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002181 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002182 if (!mddev->external)
2183 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002184 wake_up(&mddev->sb_wait);
2185 return;
2186 }
2187
NeilBrowna9701a32005-11-08 21:39:34 -08002188 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002189
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002190 mddev->utime = get_seconds();
2191
NeilBrown850b2b42006-10-03 01:15:46 -07002192 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2193 force_change = 1;
2194 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2195 /* just a clean<-> dirty transition, possibly leave spares alone,
2196 * though if events isn't the right even/odd, we will have to do
2197 * spares after all
2198 */
2199 nospares = 1;
2200 if (force_change)
2201 nospares = 0;
2202 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002203 /* If the array is degraded, then skipping spares is both
2204 * dangerous and fairly pointless.
2205 * Dangerous because a device that was removed from the array
2206 * might have a event_count that still looks up-to-date,
2207 * so it can be re-added without a resync.
2208 * Pointless because if there are any spares to skip,
2209 * then a recovery will happen and soon that array won't
2210 * be degraded any more and the spare can go back to sleep then.
2211 */
NeilBrown850b2b42006-10-03 01:15:46 -07002212 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002213
NeilBrown06d91a52005-06-21 17:17:12 -07002214 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002215
2216 /* If this is just a dirty<->clean transition, and the array is clean
2217 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002218 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002219 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002220 && mddev->can_decrease_events
2221 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002222 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002223 mddev->can_decrease_events = 0;
2224 } else {
NeilBrown42543762006-06-26 00:27:57 -07002225 /* otherwise we have to go forward and ... */
2226 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002227 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!mddev->events) {
2231 /*
2232 * oops, this 64-bit counter should never wrap.
2233 * Either we are in around ~1 trillion A.C., assuming
2234 * 1 reboot per second, or we have a bug:
2235 */
2236 MD_BUG();
2237 mddev->events --;
2238 }
NeilBrowne6910632008-02-06 01:39:51 -08002239 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002240 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 dprintk(KERN_INFO
2243 "md: updating %s RAID superblock on device (in sync %d)\n",
2244 mdname(mddev),mddev->in_sync);
2245
NeilBrown4ad13662007-07-17 04:06:13 -07002246 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002247 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 char b[BDEVNAME_SIZE];
2249 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002250 if (rdev->sb_loaded != 1)
2251 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002252 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 dprintk("(skipping faulty ");
2254
2255 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002256 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002257 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002258 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002259 rdev->sb_page);
2260 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2261 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002262 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002263 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 } else
2266 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002267 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /* only need to write one superblock... */
2269 break;
2270 }
NeilBrowna9701a32005-11-08 21:39:34 -08002271 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002272 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002273
NeilBrowna9701a32005-11-08 21:39:34 -08002274 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002275 if (mddev->in_sync != sync_req ||
2276 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002277 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002278 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002279 goto repeat;
2280 }
NeilBrown850b2b42006-10-03 01:15:46 -07002281 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002282 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002283 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002284 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2285 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Andre Noll7f6ce762008-03-23 18:34:54 +01002289/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002290 * We want to accept with case. For this we use cmd_match.
2291 */
2292static int cmd_match(const char *cmd, const char *str)
2293{
2294 /* See if cmd, written into a sysfs file, matches
2295 * str. They must either be the same, or cmd can
2296 * have a trailing newline
2297 */
2298 while (*cmd && *str && *cmd == *str) {
2299 cmd++;
2300 str++;
2301 }
2302 if (*cmd == '\n')
2303 cmd++;
2304 if (*str || *cmd)
2305 return 0;
2306 return 1;
2307}
2308
NeilBrown86e6ffd2005-11-08 21:39:24 -08002309struct rdev_sysfs_entry {
2310 struct attribute attr;
2311 ssize_t (*show)(mdk_rdev_t *, char *);
2312 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2313};
2314
2315static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002316state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002317{
2318 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002319 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002320
NeilBrownb2d444d2005-11-08 21:39:31 -08002321 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002322 len+= sprintf(page+len, "%sfaulty",sep);
2323 sep = ",";
2324 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002325 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326 len += sprintf(page+len, "%sin_sync",sep);
2327 sep = ",";
2328 }
NeilBrownf6556752006-06-26 00:28:01 -07002329 if (test_bit(WriteMostly, &rdev->flags)) {
2330 len += sprintf(page+len, "%swrite_mostly",sep);
2331 sep = ",";
2332 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002333 if (test_bit(Blocked, &rdev->flags)) {
2334 len += sprintf(page+len, "%sblocked", sep);
2335 sep = ",";
2336 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002337 if (!test_bit(Faulty, &rdev->flags) &&
2338 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002339 len += sprintf(page+len, "%sspare", sep);
2340 sep = ",";
2341 }
2342 return len+sprintf(page+len, "\n");
2343}
2344
NeilBrown45dc2de2006-06-26 00:27:58 -07002345static ssize_t
2346state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2347{
2348 /* can write
2349 * faulty - simulates and error
2350 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002351 * writemostly - sets write_mostly
2352 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002353 * blocked - sets the Blocked flag
2354 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002355 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002356 */
2357 int err = -EINVAL;
2358 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2359 md_error(rdev->mddev, rdev);
2360 err = 0;
2361 } else if (cmd_match(buf, "remove")) {
2362 if (rdev->raid_disk >= 0)
2363 err = -EBUSY;
2364 else {
2365 mddev_t *mddev = rdev->mddev;
2366 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002367 if (mddev->pers)
2368 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002369 md_new_event(mddev);
2370 err = 0;
2371 }
NeilBrownf6556752006-06-26 00:28:01 -07002372 } else if (cmd_match(buf, "writemostly")) {
2373 set_bit(WriteMostly, &rdev->flags);
2374 err = 0;
2375 } else if (cmd_match(buf, "-writemostly")) {
2376 clear_bit(WriteMostly, &rdev->flags);
2377 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002378 } else if (cmd_match(buf, "blocked")) {
2379 set_bit(Blocked, &rdev->flags);
2380 err = 0;
2381 } else if (cmd_match(buf, "-blocked")) {
2382 clear_bit(Blocked, &rdev->flags);
2383 wake_up(&rdev->blocked_wait);
2384 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2385 md_wakeup_thread(rdev->mddev->thread);
2386
2387 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002388 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2389 set_bit(In_sync, &rdev->flags);
2390 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002391 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002392 if (!err)
2393 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002394 return err ? err : len;
2395}
NeilBrown80ca3a42006-07-10 04:44:18 -07002396static struct rdev_sysfs_entry rdev_state =
2397__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002398
2399static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002400errors_show(mdk_rdev_t *rdev, char *page)
2401{
2402 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2403}
2404
2405static ssize_t
2406errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2407{
2408 char *e;
2409 unsigned long n = simple_strtoul(buf, &e, 10);
2410 if (*buf && (*e == 0 || *e == '\n')) {
2411 atomic_set(&rdev->corrected_errors, n);
2412 return len;
2413 }
2414 return -EINVAL;
2415}
2416static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002417__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002418
NeilBrown014236d2006-01-06 00:20:55 -08002419static ssize_t
2420slot_show(mdk_rdev_t *rdev, char *page)
2421{
2422 if (rdev->raid_disk < 0)
2423 return sprintf(page, "none\n");
2424 else
2425 return sprintf(page, "%d\n", rdev->raid_disk);
2426}
2427
2428static ssize_t
2429slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2430{
2431 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002432 int err;
2433 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002434 int slot = simple_strtoul(buf, &e, 10);
2435 if (strncmp(buf, "none", 4)==0)
2436 slot = -1;
2437 else if (e==buf || (*e && *e!= '\n'))
2438 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002439 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002440 /* Setting 'slot' on an active array requires also
2441 * updating the 'rd%d' link, and communicating
2442 * with the personality with ->hot_*_disk.
2443 * For now we only support removing
2444 * failed/spare devices. This normally happens automatically,
2445 * but not when the metadata is externally managed.
2446 */
NeilBrownc303da62008-02-06 01:39:51 -08002447 if (rdev->raid_disk == -1)
2448 return -EEXIST;
2449 /* personality does all needed checks */
2450 if (rdev->mddev->pers->hot_add_disk == NULL)
2451 return -EINVAL;
2452 err = rdev->mddev->pers->
2453 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2454 if (err)
2455 return err;
2456 sprintf(nm, "rd%d", rdev->raid_disk);
2457 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002458 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002459 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2460 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002461 } else if (rdev->mddev->pers) {
2462 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002463 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002464 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002465 */
2466
2467 if (rdev->raid_disk != -1)
2468 return -EBUSY;
2469
2470 if (rdev->mddev->pers->hot_add_disk == NULL)
2471 return -EINVAL;
2472
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002473 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002474 if (rdev2->raid_disk == slot)
2475 return -EEXIST;
2476
2477 rdev->raid_disk = slot;
2478 if (test_bit(In_sync, &rdev->flags))
2479 rdev->saved_raid_disk = slot;
2480 else
2481 rdev->saved_raid_disk = -1;
2482 err = rdev->mddev->pers->
2483 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002484 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002485 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002486 return err;
Neil Brown52664732008-06-28 08:31:44 +10002487 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002488 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002489 sprintf(nm, "rd%d", rdev->raid_disk);
2490 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002491 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002492 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002493 } else {
2494 if (slot >= rdev->mddev->raid_disks)
2495 return -ENOSPC;
2496 rdev->raid_disk = slot;
2497 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002498 clear_bit(Faulty, &rdev->flags);
2499 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002500 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002501 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002502 }
NeilBrown014236d2006-01-06 00:20:55 -08002503 return len;
2504}
2505
2506
2507static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002508__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002509
NeilBrown93c8cad2006-01-06 00:20:56 -08002510static ssize_t
2511offset_show(mdk_rdev_t *rdev, char *page)
2512{
Andrew Morton6961ece2006-01-06 00:20:59 -08002513 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002514}
2515
2516static ssize_t
2517offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2518{
2519 char *e;
2520 unsigned long long offset = simple_strtoull(buf, &e, 10);
2521 if (e==buf || (*e && *e != '\n'))
2522 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002523 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002524 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002525 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002526 /* Must set offset before size, so overlap checks
2527 * can be sane */
2528 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002529 rdev->data_offset = offset;
2530 return len;
2531}
2532
2533static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002534__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002535
NeilBrown83303b62006-01-06 00:21:06 -08002536static ssize_t
2537rdev_size_show(mdk_rdev_t *rdev, char *page)
2538{
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002540}
2541
NeilBrownc5d79ad2008-02-06 01:39:54 -08002542static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2543{
2544 /* check if two start/length pairs overlap */
2545 if (s1+l1 <= s2)
2546 return 0;
2547 if (s2+l2 <= s1)
2548 return 0;
2549 return 1;
2550}
2551
Dan Williamsb522adc2009-03-31 15:00:31 +11002552static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2553{
2554 unsigned long long blocks;
2555 sector_t new;
2556
2557 if (strict_strtoull(buf, 10, &blocks) < 0)
2558 return -EINVAL;
2559
2560 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2561 return -EINVAL; /* sector conversion overflow */
2562
2563 new = blocks * 2;
2564 if (new != blocks * 2)
2565 return -EINVAL; /* unsigned long long to sector_t overflow */
2566
2567 *sectors = new;
2568 return 0;
2569}
2570
NeilBrown83303b62006-01-06 00:21:06 -08002571static ssize_t
2572rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2573{
NeilBrown27c529b2008-03-04 14:29:33 -08002574 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002575 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002576 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002577
Dan Williamsb522adc2009-03-31 15:00:31 +11002578 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002579 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002580 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002581 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002582 sectors = super_types[my_mddev->major_version].
2583 rdev_size_change(rdev, sectors);
2584 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002585 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002586 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002587 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002588 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002589 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002590 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002591 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002592
Andre Nolldd8ac332009-03-31 14:33:13 +11002593 rdev->sectors = sectors;
2594 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002595 /* need to check that all other rdevs with the same ->bdev
2596 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002597 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002598 * we have to change it back, we will have the lock again.
2599 */
2600 mddev_t *mddev;
2601 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002602 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002603
NeilBrown27c529b2008-03-04 14:29:33 -08002604 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002605 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002606 mdk_rdev_t *rdev2;
2607
2608 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002609 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002610 if (test_bit(AllReserved, &rdev2->flags) ||
2611 (rdev->bdev == rdev2->bdev &&
2612 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002613 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002614 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002615 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002616 overlap = 1;
2617 break;
2618 }
2619 mddev_unlock(mddev);
2620 if (overlap) {
2621 mddev_put(mddev);
2622 break;
2623 }
2624 }
NeilBrown27c529b2008-03-04 14:29:33 -08002625 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002626 if (overlap) {
2627 /* Someone else could have slipped in a size
2628 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002629 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002630 * safe, and trust userspace not to race with
2631 * itself
2632 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002633 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002634 return -EBUSY;
2635 }
2636 }
NeilBrown83303b62006-01-06 00:21:06 -08002637 return len;
2638}
2639
2640static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002641__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002642
Dan Williams06e3c812009-12-12 21:17:12 -07002643
2644static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2645{
2646 unsigned long long recovery_start = rdev->recovery_offset;
2647
2648 if (test_bit(In_sync, &rdev->flags) ||
2649 recovery_start == MaxSector)
2650 return sprintf(page, "none\n");
2651
2652 return sprintf(page, "%llu\n", recovery_start);
2653}
2654
2655static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2656{
2657 unsigned long long recovery_start;
2658
2659 if (cmd_match(buf, "none"))
2660 recovery_start = MaxSector;
2661 else if (strict_strtoull(buf, 10, &recovery_start))
2662 return -EINVAL;
2663
2664 if (rdev->mddev->pers &&
2665 rdev->raid_disk >= 0)
2666 return -EBUSY;
2667
2668 rdev->recovery_offset = recovery_start;
2669 if (recovery_start == MaxSector)
2670 set_bit(In_sync, &rdev->flags);
2671 else
2672 clear_bit(In_sync, &rdev->flags);
2673 return len;
2674}
2675
2676static struct rdev_sysfs_entry rdev_recovery_start =
2677__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2678
NeilBrown86e6ffd2005-11-08 21:39:24 -08002679static struct attribute *rdev_default_attrs[] = {
2680 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002681 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002682 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002683 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002684 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002685 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002686 NULL,
2687};
2688static ssize_t
2689rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2690{
2691 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2692 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002693 mddev_t *mddev = rdev->mddev;
2694 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002695
2696 if (!entry->show)
2697 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002698
2699 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2700 if (!rv) {
2701 if (rdev->mddev == NULL)
2702 rv = -EBUSY;
2703 else
2704 rv = entry->show(rdev, page);
2705 mddev_unlock(mddev);
2706 }
2707 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002708}
2709
2710static ssize_t
2711rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2712 const char *page, size_t length)
2713{
2714 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2715 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002716 ssize_t rv;
2717 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002718
2719 if (!entry->store)
2720 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002721 if (!capable(CAP_SYS_ADMIN))
2722 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002723 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002724 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002725 if (rdev->mddev == NULL)
2726 rv = -EBUSY;
2727 else
2728 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002729 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002730 }
2731 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002732}
2733
2734static void rdev_free(struct kobject *ko)
2735{
2736 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2737 kfree(rdev);
2738}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002739static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002740 .show = rdev_attr_show,
2741 .store = rdev_attr_store,
2742};
2743static struct kobj_type rdev_ktype = {
2744 .release = rdev_free,
2745 .sysfs_ops = &rdev_sysfs_ops,
2746 .default_attrs = rdev_default_attrs,
2747};
2748
NeilBrowne8bb9a82010-06-01 19:37:26 +10002749void md_rdev_init(mdk_rdev_t *rdev)
2750{
2751 rdev->desc_nr = -1;
2752 rdev->saved_raid_disk = -1;
2753 rdev->raid_disk = -1;
2754 rdev->flags = 0;
2755 rdev->data_offset = 0;
2756 rdev->sb_events = 0;
2757 rdev->last_read_error.tv_sec = 0;
2758 rdev->last_read_error.tv_nsec = 0;
2759 atomic_set(&rdev->nr_pending, 0);
2760 atomic_set(&rdev->read_errors, 0);
2761 atomic_set(&rdev->corrected_errors, 0);
2762
2763 INIT_LIST_HEAD(&rdev->same_set);
2764 init_waitqueue_head(&rdev->blocked_wait);
2765}
2766EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767/*
2768 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2769 *
2770 * mark the device faulty if:
2771 *
2772 * - the device is nonexistent (zero size)
2773 * - the device has no valid superblock
2774 *
2775 * a faulty rdev _never_ has rdev->sb set.
2776 */
2777static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2778{
2779 char b[BDEVNAME_SIZE];
2780 int err;
2781 mdk_rdev_t *rdev;
2782 sector_t size;
2783
NeilBrown9ffae0c2006-01-06 00:20:32 -08002784 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (!rdev) {
2786 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2787 return ERR_PTR(-ENOMEM);
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
NeilBrowne8bb9a82010-06-01 19:37:26 +10002790 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if ((err = alloc_disk_sb(rdev)))
2792 goto abort_free;
2793
NeilBrownc5d79ad2008-02-06 01:39:54 -08002794 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (err)
2796 goto abort_free;
2797
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002798 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002799
Mike Snitzer77304d22010-11-08 14:39:12 +01002800 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (!size) {
2802 printk(KERN_WARNING
2803 "md: %s has zero or unknown size, marking faulty!\n",
2804 bdevname(rdev->bdev,b));
2805 err = -EINVAL;
2806 goto abort_free;
2807 }
2808
2809 if (super_format >= 0) {
2810 err = super_types[super_format].
2811 load_super(rdev, NULL, super_minor);
2812 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002813 printk(KERN_WARNING
2814 "md: %s does not have a valid v%d.%d "
2815 "superblock, not importing!\n",
2816 bdevname(rdev->bdev,b),
2817 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 goto abort_free;
2819 }
2820 if (err < 0) {
2821 printk(KERN_WARNING
2822 "md: could not read %s's sb, not importing!\n",
2823 bdevname(rdev->bdev,b));
2824 goto abort_free;
2825 }
2826 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return rdev;
2829
2830abort_free:
2831 if (rdev->sb_page) {
2832 if (rdev->bdev)
2833 unlock_rdev(rdev);
2834 free_disk_sb(rdev);
2835 }
2836 kfree(rdev);
2837 return ERR_PTR(err);
2838}
2839
2840/*
2841 * Check a full RAID array for plausibility
2842 */
2843
2844
NeilBrowna757e642005-04-16 15:26:42 -07002845static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002848 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 char b[BDEVNAME_SIZE];
2850
2851 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002852 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 switch (super_types[mddev->major_version].
2854 load_super(rdev, freshest, mddev->minor_version)) {
2855 case 1:
2856 freshest = rdev;
2857 break;
2858 case 0:
2859 break;
2860 default:
2861 printk( KERN_ERR \
2862 "md: fatal superblock inconsistency in %s"
2863 " -- removing from array\n",
2864 bdevname(rdev->bdev,b));
2865 kick_rdev_from_array(rdev);
2866 }
2867
2868
2869 super_types[mddev->major_version].
2870 validate_super(mddev, freshest);
2871
2872 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002873 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002874 if (mddev->max_disks &&
2875 (rdev->desc_nr >= mddev->max_disks ||
2876 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002877 printk(KERN_WARNING
2878 "md: %s: %s: only %d devices permitted\n",
2879 mdname(mddev), bdevname(rdev->bdev, b),
2880 mddev->max_disks);
2881 kick_rdev_from_array(rdev);
2882 continue;
2883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (rdev != freshest)
2885 if (super_types[mddev->major_version].
2886 validate_super(mddev, rdev)) {
2887 printk(KERN_WARNING "md: kicking non-fresh %s"
2888 " from array!\n",
2889 bdevname(rdev->bdev,b));
2890 kick_rdev_from_array(rdev);
2891 continue;
2892 }
2893 if (mddev->level == LEVEL_MULTIPATH) {
2894 rdev->desc_nr = i++;
2895 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002896 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002897 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002898 rdev->raid_disk = -1;
2899 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
NeilBrown72e02072009-12-14 12:49:55 +11002904/* Read a fixed-point number.
2905 * Numbers in sysfs attributes should be in "standard" units where
2906 * possible, so time should be in seconds.
2907 * However we internally use a a much smaller unit such as
2908 * milliseconds or jiffies.
2909 * This function takes a decimal number with a possible fractional
2910 * component, and produces an integer which is the result of
2911 * multiplying that number by 10^'scale'.
2912 * all without any floating-point arithmetic.
2913 */
2914int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2915{
2916 unsigned long result = 0;
2917 long decimals = -1;
2918 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2919 if (*cp == '.')
2920 decimals = 0;
2921 else if (decimals < scale) {
2922 unsigned int value;
2923 value = *cp - '0';
2924 result = result * 10 + value;
2925 if (decimals >= 0)
2926 decimals++;
2927 }
2928 cp++;
2929 }
2930 if (*cp == '\n')
2931 cp++;
2932 if (*cp)
2933 return -EINVAL;
2934 if (decimals < 0)
2935 decimals = 0;
2936 while (decimals < scale) {
2937 result *= 10;
2938 decimals ++;
2939 }
2940 *res = result;
2941 return 0;
2942}
2943
2944
NeilBrown19052c02008-08-05 15:54:13 +10002945static void md_safemode_timeout(unsigned long data);
2946
NeilBrowneae17012005-11-08 21:39:23 -08002947static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002948safe_delay_show(mddev_t *mddev, char *page)
2949{
2950 int msec = (mddev->safemode_delay*1000)/HZ;
2951 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2952}
2953static ssize_t
2954safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2955{
NeilBrown16f17b32006-06-26 00:27:37 -07002956 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002957
NeilBrown72e02072009-12-14 12:49:55 +11002958 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002959 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002960 if (msec == 0)
2961 mddev->safemode_delay = 0;
2962 else {
NeilBrown19052c02008-08-05 15:54:13 +10002963 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002964 mddev->safemode_delay = (msec*HZ)/1000;
2965 if (mddev->safemode_delay == 0)
2966 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002967 if (mddev->safemode_delay < old_delay)
2968 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002969 }
2970 return len;
2971}
2972static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002973__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002974
2975static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002976level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002977{
NeilBrown2604b702006-01-06 00:20:36 -08002978 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002979 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002980 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002981 else if (mddev->clevel[0])
2982 return sprintf(page, "%s\n", mddev->clevel);
2983 else if (mddev->level != LEVEL_NONE)
2984 return sprintf(page, "%d\n", mddev->level);
2985 else
2986 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002987}
2988
NeilBrownd9d166c2006-01-06 00:20:51 -08002989static ssize_t
2990level_store(mddev_t *mddev, const char *buf, size_t len)
2991{
Dan Williamsf2859af2010-05-02 10:04:16 -07002992 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002993 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002994 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002995 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002996 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002997 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002998
2999 if (mddev->pers == NULL) {
3000 if (len == 0)
3001 return 0;
3002 if (len >= sizeof(mddev->clevel))
3003 return -ENOSPC;
3004 strncpy(mddev->clevel, buf, len);
3005 if (mddev->clevel[len-1] == '\n')
3006 len--;
3007 mddev->clevel[len] = 0;
3008 mddev->level = LEVEL_NONE;
3009 return rv;
3010 }
3011
3012 /* request to change the personality. Need to ensure:
3013 * - array is not engaged in resync/recovery/reshape
3014 * - old personality can be suspended
3015 * - new personality will access other array.
3016 */
3017
NeilBrownbb4f1e92010-08-08 21:18:03 +10003018 if (mddev->sync_thread ||
3019 mddev->reshape_position != MaxSector ||
3020 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003021 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003022
3023 if (!mddev->pers->quiesce) {
3024 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3025 mdname(mddev), mddev->pers->name);
3026 return -EINVAL;
3027 }
3028
3029 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003030 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003031 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003032 strncpy(clevel, buf, len);
3033 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003034 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003035 clevel[len] = 0;
3036 if (strict_strtol(clevel, 10, &level))
3037 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003038
Dan Williamsf2859af2010-05-02 10:04:16 -07003039 if (request_module("md-%s", clevel) != 0)
3040 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003041 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003042 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003043 if (!pers || !try_module_get(pers->owner)) {
3044 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003045 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003046 return -EINVAL;
3047 }
3048 spin_unlock(&pers_lock);
3049
3050 if (pers == mddev->pers) {
3051 /* Nothing to do! */
3052 module_put(pers->owner);
3053 return rv;
3054 }
3055 if (!pers->takeover) {
3056 module_put(pers->owner);
3057 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003058 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003059 return -EINVAL;
3060 }
3061
NeilBrowne93f68a2010-06-15 09:36:03 +01003062 list_for_each_entry(rdev, &mddev->disks, same_set)
3063 rdev->new_raid_disk = rdev->raid_disk;
3064
NeilBrown245f46c2009-03-31 14:39:39 +11003065 /* ->takeover must set new_* and/or delta_disks
3066 * if it succeeds, and may set them when it fails.
3067 */
3068 priv = pers->takeover(mddev);
3069 if (IS_ERR(priv)) {
3070 mddev->new_level = mddev->level;
3071 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003072 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003073 mddev->raid_disks -= mddev->delta_disks;
3074 mddev->delta_disks = 0;
3075 module_put(pers->owner);
3076 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003077 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003078 return PTR_ERR(priv);
3079 }
3080
3081 /* Looks like we have a winner */
3082 mddev_suspend(mddev);
3083 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003084
3085 if (mddev->pers->sync_request == NULL &&
3086 pers->sync_request != NULL) {
3087 /* need to add the md_redundancy_group */
3088 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3089 printk(KERN_WARNING
3090 "md: cannot register extra attributes for %s\n",
3091 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003092 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003093 }
3094 if (mddev->pers->sync_request != NULL &&
3095 pers->sync_request == NULL) {
3096 /* need to remove the md_redundancy_group */
3097 if (mddev->to_remove == NULL)
3098 mddev->to_remove = &md_redundancy_group;
3099 }
3100
Trela Maciej54071b32010-03-08 16:02:42 +11003101 if (mddev->pers->sync_request == NULL &&
3102 mddev->external) {
3103 /* We are converting from a no-redundancy array
3104 * to a redundancy array and metadata is managed
3105 * externally so we need to be sure that writes
3106 * won't block due to a need to transition
3107 * clean->dirty
3108 * until external management is started.
3109 */
3110 mddev->in_sync = 0;
3111 mddev->safemode_delay = 0;
3112 mddev->safemode = 0;
3113 }
3114
NeilBrowne93f68a2010-06-15 09:36:03 +01003115 list_for_each_entry(rdev, &mddev->disks, same_set) {
3116 char nm[20];
3117 if (rdev->raid_disk < 0)
3118 continue;
3119 if (rdev->new_raid_disk > mddev->raid_disks)
3120 rdev->new_raid_disk = -1;
3121 if (rdev->new_raid_disk == rdev->raid_disk)
3122 continue;
3123 sprintf(nm, "rd%d", rdev->raid_disk);
3124 sysfs_remove_link(&mddev->kobj, nm);
3125 }
3126 list_for_each_entry(rdev, &mddev->disks, same_set) {
3127 if (rdev->raid_disk < 0)
3128 continue;
3129 if (rdev->new_raid_disk == rdev->raid_disk)
3130 continue;
3131 rdev->raid_disk = rdev->new_raid_disk;
3132 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003133 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003134 else {
3135 char nm[20];
3136 sprintf(nm, "rd%d", rdev->raid_disk);
3137 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3138 printk("md: cannot register %s for %s after level change\n",
3139 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003140 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003141 }
3142
3143 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003144 mddev->pers = pers;
3145 mddev->private = priv;
3146 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3147 mddev->level = mddev->new_level;
3148 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003149 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003150 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003151 if (mddev->pers->sync_request == NULL) {
3152 /* this is now an array without redundancy, so
3153 * it must always be in_sync
3154 */
3155 mddev->in_sync = 1;
3156 del_timer_sync(&mddev->safemode_timer);
3157 }
NeilBrown245f46c2009-03-31 14:39:39 +11003158 pers->run(mddev);
3159 mddev_resume(mddev);
3160 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3161 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3162 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003163 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003164 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003165 return rv;
3166}
3167
3168static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003169__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003170
NeilBrownd4dbd022006-06-26 00:27:59 -07003171
3172static ssize_t
3173layout_show(mddev_t *mddev, char *page)
3174{
3175 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003176 if (mddev->reshape_position != MaxSector &&
3177 mddev->layout != mddev->new_layout)
3178 return sprintf(page, "%d (%d)\n",
3179 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003180 return sprintf(page, "%d\n", mddev->layout);
3181}
3182
3183static ssize_t
3184layout_store(mddev_t *mddev, const char *buf, size_t len)
3185{
3186 char *e;
3187 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003188
3189 if (!*buf || (*e && *e != '\n'))
3190 return -EINVAL;
3191
NeilBrownb3546032009-03-31 14:56:41 +11003192 if (mddev->pers) {
3193 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003194 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003195 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003196 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003197 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003198 if (err) {
3199 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003200 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003201 }
NeilBrownb3546032009-03-31 14:56:41 +11003202 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003203 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003204 if (mddev->reshape_position == MaxSector)
3205 mddev->layout = n;
3206 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003207 return len;
3208}
3209static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003210__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003211
3212
NeilBrowneae17012005-11-08 21:39:23 -08003213static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003214raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003215{
NeilBrownbb636542005-11-08 21:39:45 -08003216 if (mddev->raid_disks == 0)
3217 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003218 if (mddev->reshape_position != MaxSector &&
3219 mddev->delta_disks != 0)
3220 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3221 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003222 return sprintf(page, "%d\n", mddev->raid_disks);
3223}
3224
NeilBrownda943b992006-01-06 00:20:54 -08003225static int update_raid_disks(mddev_t *mddev, int raid_disks);
3226
3227static ssize_t
3228raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3229{
NeilBrownda943b992006-01-06 00:20:54 -08003230 char *e;
3231 int rv = 0;
3232 unsigned long n = simple_strtoul(buf, &e, 10);
3233
3234 if (!*buf || (*e && *e != '\n'))
3235 return -EINVAL;
3236
3237 if (mddev->pers)
3238 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003239 else if (mddev->reshape_position != MaxSector) {
3240 int olddisks = mddev->raid_disks - mddev->delta_disks;
3241 mddev->delta_disks = n - olddisks;
3242 mddev->raid_disks = n;
3243 } else
NeilBrownda943b992006-01-06 00:20:54 -08003244 mddev->raid_disks = n;
3245 return rv ? rv : len;
3246}
3247static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003248__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003249
NeilBrown24dd4692005-11-08 21:39:26 -08003250static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003251chunk_size_show(mddev_t *mddev, char *page)
3252{
NeilBrown08a02ec2007-05-09 02:35:38 -07003253 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003254 mddev->chunk_sectors != mddev->new_chunk_sectors)
3255 return sprintf(page, "%d (%d)\n",
3256 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003257 mddev->chunk_sectors << 9);
3258 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003259}
3260
3261static ssize_t
3262chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3263{
NeilBrown3b343802006-01-06 00:20:47 -08003264 char *e;
3265 unsigned long n = simple_strtoul(buf, &e, 10);
3266
NeilBrown3b343802006-01-06 00:20:47 -08003267 if (!*buf || (*e && *e != '\n'))
3268 return -EINVAL;
3269
NeilBrownb3546032009-03-31 14:56:41 +11003270 if (mddev->pers) {
3271 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003272 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003273 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003274 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003275 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003276 if (err) {
3277 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003278 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003279 }
NeilBrownb3546032009-03-31 14:56:41 +11003280 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003281 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003282 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003283 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003284 }
NeilBrown3b343802006-01-06 00:20:47 -08003285 return len;
3286}
3287static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003288__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003289
NeilBrowna94213b2006-06-26 00:28:00 -07003290static ssize_t
3291resync_start_show(mddev_t *mddev, char *page)
3292{
NeilBrownd1a7c502009-03-31 15:24:32 +11003293 if (mddev->recovery_cp == MaxSector)
3294 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003295 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3296}
3297
3298static ssize_t
3299resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3300{
NeilBrowna94213b2006-06-26 00:28:00 -07003301 char *e;
3302 unsigned long long n = simple_strtoull(buf, &e, 10);
3303
3304 if (mddev->pers)
3305 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003306 if (cmd_match(buf, "none"))
3307 n = MaxSector;
3308 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003309 return -EINVAL;
3310
3311 mddev->recovery_cp = n;
3312 return len;
3313}
3314static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003315__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003316
NeilBrown9e653b62006-06-26 00:27:58 -07003317/*
3318 * The array state can be:
3319 *
3320 * clear
3321 * No devices, no size, no level
3322 * Equivalent to STOP_ARRAY ioctl
3323 * inactive
3324 * May have some settings, but array is not active
3325 * all IO results in error
3326 * When written, doesn't tear down array, but just stops it
3327 * suspended (not supported yet)
3328 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003329 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003330 * readonly
3331 * no resync can happen. no superblocks get written.
3332 * write requests fail
3333 * read-auto
3334 * like readonly, but behaves like 'clean' on a write request.
3335 *
3336 * clean - no pending writes, but otherwise active.
3337 * When written to inactive array, starts without resync
3338 * If a write request arrives then
3339 * if metadata is known, mark 'dirty' and switch to 'active'.
3340 * if not known, block and switch to write-pending
3341 * If written to an active array that has pending writes, then fails.
3342 * active
3343 * fully active: IO and resync can be happening.
3344 * When written to inactive array, starts with resync
3345 *
3346 * write-pending
3347 * clean, but writes are blocked waiting for 'active' to be written.
3348 *
3349 * active-idle
3350 * like active, but no writes have been seen for a while (100msec).
3351 *
3352 */
3353enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3354 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003355static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003356 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3357 "write-pending", "active-idle", NULL };
3358
3359static int match_word(const char *word, char **list)
3360{
3361 int n;
3362 for (n=0; list[n]; n++)
3363 if (cmd_match(word, list[n]))
3364 break;
3365 return n;
3366}
3367
3368static ssize_t
3369array_state_show(mddev_t *mddev, char *page)
3370{
3371 enum array_state st = inactive;
3372
3373 if (mddev->pers)
3374 switch(mddev->ro) {
3375 case 1:
3376 st = readonly;
3377 break;
3378 case 2:
3379 st = read_auto;
3380 break;
3381 case 0:
3382 if (mddev->in_sync)
3383 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003384 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003385 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003386 else if (mddev->safemode)
3387 st = active_idle;
3388 else
3389 st = active;
3390 }
3391 else {
3392 if (list_empty(&mddev->disks) &&
3393 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003394 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003395 st = clear;
3396 else
3397 st = inactive;
3398 }
3399 return sprintf(page, "%s\n", array_states[st]);
3400}
3401
Neil Browndf5b20c2008-07-11 22:02:22 +10003402static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003403static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003404static int do_md_run(mddev_t * mddev);
3405static int restart_array(mddev_t *mddev);
3406
3407static ssize_t
3408array_state_store(mddev_t *mddev, const char *buf, size_t len)
3409{
3410 int err = -EINVAL;
3411 enum array_state st = match_word(buf, array_states);
3412 switch(st) {
3413 case bad_word:
3414 break;
3415 case clear:
3416 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003417 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003418 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003419 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003420 break;
3421 case inactive:
3422 /* stopping an active array */
3423 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003424 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003425 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003426 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003427 } else
3428 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003429 break;
3430 case suspended:
3431 break; /* not supported yet */
3432 case readonly:
3433 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003434 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003435 else {
3436 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003437 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003438 err = do_md_run(mddev);
3439 }
3440 break;
3441 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003442 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003443 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003444 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003445 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003446 err = restart_array(mddev);
3447 if (err == 0) {
3448 mddev->ro = 2;
3449 set_disk_ro(mddev->gendisk, 0);
3450 }
NeilBrown9e653b62006-06-26 00:27:58 -07003451 } else {
3452 mddev->ro = 2;
3453 err = do_md_run(mddev);
3454 }
3455 break;
3456 case clean:
3457 if (mddev->pers) {
3458 restart_array(mddev);
3459 spin_lock_irq(&mddev->write_lock);
3460 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003461 if (mddev->in_sync == 0) {
3462 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003463 if (mddev->safemode == 1)
3464 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003465 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003466 }
3467 err = 0;
3468 } else
3469 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003470 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003471 } else
3472 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003473 break;
3474 case active:
3475 if (mddev->pers) {
3476 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003477 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003478 wake_up(&mddev->sb_wait);
3479 err = 0;
3480 } else {
3481 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003482 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003483 err = do_md_run(mddev);
3484 }
3485 break;
3486 case write_pending:
3487 case active_idle:
3488 /* these cannot be set */
3489 break;
3490 }
3491 if (err)
3492 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003493 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003494 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003495 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003496 }
NeilBrown9e653b62006-06-26 00:27:58 -07003497}
NeilBrown80ca3a42006-07-10 04:44:18 -07003498static struct md_sysfs_entry md_array_state =
3499__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003500
NeilBrown6d7ff732006-01-06 00:21:16 -08003501static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003502max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3503 return sprintf(page, "%d\n",
3504 atomic_read(&mddev->max_corr_read_errors));
3505}
3506
3507static ssize_t
3508max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3509{
3510 char *e;
3511 unsigned long n = simple_strtoul(buf, &e, 10);
3512
3513 if (*buf && (*e == 0 || *e == '\n')) {
3514 atomic_set(&mddev->max_corr_read_errors, n);
3515 return len;
3516 }
3517 return -EINVAL;
3518}
3519
3520static struct md_sysfs_entry max_corr_read_errors =
3521__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3522 max_corrected_read_errors_store);
3523
3524static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003525null_show(mddev_t *mddev, char *page)
3526{
3527 return -EINVAL;
3528}
3529
3530static ssize_t
3531new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3532{
3533 /* buf must be %d:%d\n? giving major and minor numbers */
3534 /* The new device is added to the array.
3535 * If the array has a persistent superblock, we read the
3536 * superblock to initialise info and check validity.
3537 * Otherwise, only checking done is that in bind_rdev_to_array,
3538 * which mainly checks size.
3539 */
3540 char *e;
3541 int major = simple_strtoul(buf, &e, 10);
3542 int minor;
3543 dev_t dev;
3544 mdk_rdev_t *rdev;
3545 int err;
3546
3547 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3548 return -EINVAL;
3549 minor = simple_strtoul(e+1, &e, 10);
3550 if (*e && *e != '\n')
3551 return -EINVAL;
3552 dev = MKDEV(major, minor);
3553 if (major != MAJOR(dev) ||
3554 minor != MINOR(dev))
3555 return -EOVERFLOW;
3556
3557
3558 if (mddev->persistent) {
3559 rdev = md_import_device(dev, mddev->major_version,
3560 mddev->minor_version);
3561 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3562 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3563 mdk_rdev_t, same_set);
3564 err = super_types[mddev->major_version]
3565 .load_super(rdev, rdev0, mddev->minor_version);
3566 if (err < 0)
3567 goto out;
3568 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003569 } else if (mddev->external)
3570 rdev = md_import_device(dev, -2, -1);
3571 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003572 rdev = md_import_device(dev, -1, -1);
3573
3574 if (IS_ERR(rdev))
3575 return PTR_ERR(rdev);
3576 err = bind_rdev_to_array(rdev, mddev);
3577 out:
3578 if (err)
3579 export_rdev(rdev);
3580 return err ? err : len;
3581}
3582
3583static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003584__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003585
3586static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003587bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3588{
3589 char *end;
3590 unsigned long chunk, end_chunk;
3591
3592 if (!mddev->bitmap)
3593 goto out;
3594 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3595 while (*buf) {
3596 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3597 if (buf == end) break;
3598 if (*end == '-') { /* range */
3599 buf = end + 1;
3600 end_chunk = simple_strtoul(buf, &end, 0);
3601 if (buf == end) break;
3602 }
3603 if (*end && !isspace(*end)) break;
3604 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003605 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003606 }
3607 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3608out:
3609 return len;
3610}
3611
3612static struct md_sysfs_entry md_bitmap =
3613__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3614
3615static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003616size_show(mddev_t *mddev, char *page)
3617{
Andre Noll58c0fed2009-03-31 14:33:13 +11003618 return sprintf(page, "%llu\n",
3619 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003620}
3621
Andre Nolld71f9f82008-07-11 22:02:22 +10003622static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003623
3624static ssize_t
3625size_store(mddev_t *mddev, const char *buf, size_t len)
3626{
3627 /* If array is inactive, we can reduce the component size, but
3628 * not increase it (except from 0).
3629 * If array is active, we can try an on-line resize
3630 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003631 sector_t sectors;
3632 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003633
Andre Noll58c0fed2009-03-31 14:33:13 +11003634 if (err < 0)
3635 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003636 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003637 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003638 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003639 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003640 if (mddev->dev_sectors == 0 ||
3641 mddev->dev_sectors > sectors)
3642 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003643 else
3644 err = -ENOSPC;
3645 }
3646 return err ? err : len;
3647}
3648
3649static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003650__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003651
NeilBrown8bb93aa2006-01-06 00:20:50 -08003652
3653/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003654 * This is one of
3655 * 'none' for arrays with no metadata (good luck...)
3656 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003657 * or N.M for internally known formats
3658 */
3659static ssize_t
3660metadata_show(mddev_t *mddev, char *page)
3661{
3662 if (mddev->persistent)
3663 return sprintf(page, "%d.%d\n",
3664 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003665 else if (mddev->external)
3666 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003667 else
3668 return sprintf(page, "none\n");
3669}
3670
3671static ssize_t
3672metadata_store(mddev_t *mddev, const char *buf, size_t len)
3673{
3674 int major, minor;
3675 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003676 /* Changing the details of 'external' metadata is
3677 * always permitted. Otherwise there must be
3678 * no devices attached to the array.
3679 */
3680 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3681 ;
3682 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003683 return -EBUSY;
3684
3685 if (cmd_match(buf, "none")) {
3686 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003687 mddev->external = 0;
3688 mddev->major_version = 0;
3689 mddev->minor_version = 90;
3690 return len;
3691 }
3692 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003693 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003694 if (namelen >= sizeof(mddev->metadata_type))
3695 namelen = sizeof(mddev->metadata_type)-1;
3696 strncpy(mddev->metadata_type, buf+9, namelen);
3697 mddev->metadata_type[namelen] = 0;
3698 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3699 mddev->metadata_type[--namelen] = 0;
3700 mddev->persistent = 0;
3701 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003702 mddev->major_version = 0;
3703 mddev->minor_version = 90;
3704 return len;
3705 }
3706 major = simple_strtoul(buf, &e, 10);
3707 if (e==buf || *e != '.')
3708 return -EINVAL;
3709 buf = e+1;
3710 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003711 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003712 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003713 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003714 return -ENOENT;
3715 mddev->major_version = major;
3716 mddev->minor_version = minor;
3717 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003718 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003719 return len;
3720}
3721
3722static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003723__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003724
NeilBrowna35b0d62006-01-06 00:20:49 -08003725static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003726action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003727{
NeilBrown7eec3142005-11-08 21:39:44 -08003728 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003729 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3730 type = "frozen";
3731 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003732 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003733 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3734 type = "reshape";
3735 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003736 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3737 type = "resync";
3738 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3739 type = "check";
3740 else
3741 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003742 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003743 type = "recover";
3744 }
3745 return sprintf(page, "%s\n", type);
3746}
3747
3748static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003749action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003750{
NeilBrown7eec3142005-11-08 21:39:44 -08003751 if (!mddev->pers || !mddev->pers->sync_request)
3752 return -EINVAL;
3753
NeilBrownb6a9ce62009-05-26 09:41:17 +10003754 if (cmd_match(page, "frozen"))
3755 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3756 else
3757 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3758
3759 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003760 if (mddev->sync_thread) {
3761 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3762 md_unregister_thread(mddev->sync_thread);
3763 mddev->sync_thread = NULL;
3764 mddev->recovery = 0;
3765 }
NeilBrown03c902e2006-01-06 00:20:46 -08003766 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3767 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003768 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003769 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003770 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003771 else if (cmd_match(page, "recover")) {
3772 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3773 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3774 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003775 int err;
3776 if (mddev->pers->start_reshape == NULL)
3777 return -EINVAL;
3778 err = mddev->pers->start_reshape(mddev);
3779 if (err)
3780 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003781 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003782 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003783 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003784 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003785 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003786 return -EINVAL;
3787 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3788 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003789 }
NeilBrown03c902e2006-01-06 00:20:46 -08003790 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003791 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003792 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003793 return len;
3794}
3795
NeilBrown9d888832005-11-08 21:39:26 -08003796static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003797mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003798{
3799 return sprintf(page, "%llu\n",
3800 (unsigned long long) mddev->resync_mismatches);
3801}
3802
NeilBrown80ca3a42006-07-10 04:44:18 -07003803static struct md_sysfs_entry md_scan_mode =
3804__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003805
NeilBrown96de1e62005-11-08 21:39:39 -08003806
NeilBrown80ca3a42006-07-10 04:44:18 -07003807static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003808
NeilBrown88202a02006-01-06 00:21:36 -08003809static ssize_t
3810sync_min_show(mddev_t *mddev, char *page)
3811{
3812 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3813 mddev->sync_speed_min ? "local": "system");
3814}
3815
3816static ssize_t
3817sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3818{
3819 int min;
3820 char *e;
3821 if (strncmp(buf, "system", 6)==0) {
3822 mddev->sync_speed_min = 0;
3823 return len;
3824 }
3825 min = simple_strtoul(buf, &e, 10);
3826 if (buf == e || (*e && *e != '\n') || min <= 0)
3827 return -EINVAL;
3828 mddev->sync_speed_min = min;
3829 return len;
3830}
3831
3832static struct md_sysfs_entry md_sync_min =
3833__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3834
3835static ssize_t
3836sync_max_show(mddev_t *mddev, char *page)
3837{
3838 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3839 mddev->sync_speed_max ? "local": "system");
3840}
3841
3842static ssize_t
3843sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3844{
3845 int max;
3846 char *e;
3847 if (strncmp(buf, "system", 6)==0) {
3848 mddev->sync_speed_max = 0;
3849 return len;
3850 }
3851 max = simple_strtoul(buf, &e, 10);
3852 if (buf == e || (*e && *e != '\n') || max <= 0)
3853 return -EINVAL;
3854 mddev->sync_speed_max = max;
3855 return len;
3856}
3857
3858static struct md_sysfs_entry md_sync_max =
3859__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3860
Iustin Popd7f3d292007-10-16 23:30:54 -07003861static ssize_t
3862degraded_show(mddev_t *mddev, char *page)
3863{
3864 return sprintf(page, "%d\n", mddev->degraded);
3865}
3866static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003867
3868static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003869sync_force_parallel_show(mddev_t *mddev, char *page)
3870{
3871 return sprintf(page, "%d\n", mddev->parallel_resync);
3872}
3873
3874static ssize_t
3875sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3876{
3877 long n;
3878
3879 if (strict_strtol(buf, 10, &n))
3880 return -EINVAL;
3881
3882 if (n != 0 && n != 1)
3883 return -EINVAL;
3884
3885 mddev->parallel_resync = n;
3886
3887 if (mddev->sync_thread)
3888 wake_up(&resync_wait);
3889
3890 return len;
3891}
3892
3893/* force parallel resync, even with shared block devices */
3894static struct md_sysfs_entry md_sync_force_parallel =
3895__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3896 sync_force_parallel_show, sync_force_parallel_store);
3897
3898static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003899sync_speed_show(mddev_t *mddev, char *page)
3900{
3901 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003902 if (mddev->curr_resync == 0)
3903 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003904 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3905 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003906 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003907 db = resync - mddev->resync_mark_cnt;
3908 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003909}
3910
NeilBrown80ca3a42006-07-10 04:44:18 -07003911static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003912
3913static ssize_t
3914sync_completed_show(mddev_t *mddev, char *page)
3915{
Andre Noll58c0fed2009-03-31 14:33:13 +11003916 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003917
NeilBrownacb180b2009-04-14 16:28:34 +10003918 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3919 return sprintf(page, "none\n");
3920
NeilBrown88202a02006-01-06 00:21:36 -08003921 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003922 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003923 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003924 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003925
NeilBrownacb180b2009-04-14 16:28:34 +10003926 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003927 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003928}
3929
NeilBrown80ca3a42006-07-10 04:44:18 -07003930static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003931
NeilBrowne464eaf2006-03-27 01:18:14 -08003932static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003933min_sync_show(mddev_t *mddev, char *page)
3934{
3935 return sprintf(page, "%llu\n",
3936 (unsigned long long)mddev->resync_min);
3937}
3938static ssize_t
3939min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3940{
3941 unsigned long long min;
3942 if (strict_strtoull(buf, 10, &min))
3943 return -EINVAL;
3944 if (min > mddev->resync_max)
3945 return -EINVAL;
3946 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3947 return -EBUSY;
3948
3949 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003950 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003951 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003952 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003953 return -EINVAL;
3954 }
3955 mddev->resync_min = min;
3956
3957 return len;
3958}
3959
3960static struct md_sysfs_entry md_min_sync =
3961__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3962
3963static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003964max_sync_show(mddev_t *mddev, char *page)
3965{
3966 if (mddev->resync_max == MaxSector)
3967 return sprintf(page, "max\n");
3968 else
3969 return sprintf(page, "%llu\n",
3970 (unsigned long long)mddev->resync_max);
3971}
3972static ssize_t
3973max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3974{
3975 if (strncmp(buf, "max", 3) == 0)
3976 mddev->resync_max = MaxSector;
3977 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003978 unsigned long long max;
3979 if (strict_strtoull(buf, 10, &max))
3980 return -EINVAL;
3981 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003982 return -EINVAL;
3983 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003984 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003985 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3986 return -EBUSY;
3987
3988 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003989 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003990 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003991 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003992 return -EINVAL;
3993 }
3994 mddev->resync_max = max;
3995 }
3996 wake_up(&mddev->recovery_wait);
3997 return len;
3998}
3999
4000static struct md_sysfs_entry md_max_sync =
4001__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4002
4003static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004004suspend_lo_show(mddev_t *mddev, char *page)
4005{
4006 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4007}
4008
4009static ssize_t
4010suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4011{
4012 char *e;
4013 unsigned long long new = simple_strtoull(buf, &e, 10);
4014
NeilBrownb8d966e2009-07-01 11:14:04 +10004015 if (mddev->pers == NULL ||
4016 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004017 return -EINVAL;
4018 if (buf == e || (*e && *e != '\n'))
4019 return -EINVAL;
4020 if (new >= mddev->suspend_hi ||
4021 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
4022 mddev->suspend_lo = new;
4023 mddev->pers->quiesce(mddev, 2);
4024 return len;
4025 } else
4026 return -EINVAL;
4027}
4028static struct md_sysfs_entry md_suspend_lo =
4029__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4030
4031
4032static ssize_t
4033suspend_hi_show(mddev_t *mddev, char *page)
4034{
4035 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4036}
4037
4038static ssize_t
4039suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4040{
4041 char *e;
4042 unsigned long long new = simple_strtoull(buf, &e, 10);
4043
NeilBrownb8d966e2009-07-01 11:14:04 +10004044 if (mddev->pers == NULL ||
4045 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004046 return -EINVAL;
4047 if (buf == e || (*e && *e != '\n'))
4048 return -EINVAL;
4049 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
4050 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
4051 mddev->suspend_hi = new;
4052 mddev->pers->quiesce(mddev, 1);
4053 mddev->pers->quiesce(mddev, 0);
4054 return len;
4055 } else
4056 return -EINVAL;
4057}
4058static struct md_sysfs_entry md_suspend_hi =
4059__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4060
NeilBrown08a02ec2007-05-09 02:35:38 -07004061static ssize_t
4062reshape_position_show(mddev_t *mddev, char *page)
4063{
4064 if (mddev->reshape_position != MaxSector)
4065 return sprintf(page, "%llu\n",
4066 (unsigned long long)mddev->reshape_position);
4067 strcpy(page, "none\n");
4068 return 5;
4069}
4070
4071static ssize_t
4072reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4073{
4074 char *e;
4075 unsigned long long new = simple_strtoull(buf, &e, 10);
4076 if (mddev->pers)
4077 return -EBUSY;
4078 if (buf == e || (*e && *e != '\n'))
4079 return -EINVAL;
4080 mddev->reshape_position = new;
4081 mddev->delta_disks = 0;
4082 mddev->new_level = mddev->level;
4083 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004084 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004085 return len;
4086}
4087
4088static struct md_sysfs_entry md_reshape_position =
4089__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4090 reshape_position_store);
4091
Dan Williamsb522adc2009-03-31 15:00:31 +11004092static ssize_t
4093array_size_show(mddev_t *mddev, char *page)
4094{
4095 if (mddev->external_size)
4096 return sprintf(page, "%llu\n",
4097 (unsigned long long)mddev->array_sectors/2);
4098 else
4099 return sprintf(page, "default\n");
4100}
4101
4102static ssize_t
4103array_size_store(mddev_t *mddev, const char *buf, size_t len)
4104{
4105 sector_t sectors;
4106
4107 if (strncmp(buf, "default", 7) == 0) {
4108 if (mddev->pers)
4109 sectors = mddev->pers->size(mddev, 0, 0);
4110 else
4111 sectors = mddev->array_sectors;
4112
4113 mddev->external_size = 0;
4114 } else {
4115 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4116 return -EINVAL;
4117 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004118 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004119
4120 mddev->external_size = 1;
4121 }
4122
4123 mddev->array_sectors = sectors;
4124 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004125 if (mddev->pers)
4126 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004127
4128 return len;
4129}
4130
4131static struct md_sysfs_entry md_array_size =
4132__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4133 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004134
NeilBrowneae17012005-11-08 21:39:23 -08004135static struct attribute *md_default_attrs[] = {
4136 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004137 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004138 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004139 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004140 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004141 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004142 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004143 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004144 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004145 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004146 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004147 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004148 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004149 NULL,
4150};
4151
4152static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004153 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004154 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004155 &md_sync_min.attr,
4156 &md_sync_max.attr,
4157 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004158 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004159 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004160 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004161 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004162 &md_suspend_lo.attr,
4163 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004164 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004165 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004166 NULL,
4167};
NeilBrown411036f2005-11-08 21:39:40 -08004168static struct attribute_group md_redundancy_group = {
4169 .name = NULL,
4170 .attrs = md_redundancy_attrs,
4171};
4172
NeilBrowneae17012005-11-08 21:39:23 -08004173
4174static ssize_t
4175md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4176{
4177 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4178 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004179 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004180
4181 if (!entry->show)
4182 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004183 rv = mddev_lock(mddev);
4184 if (!rv) {
4185 rv = entry->show(mddev, page);
4186 mddev_unlock(mddev);
4187 }
NeilBrown96de1e62005-11-08 21:39:39 -08004188 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004189}
4190
4191static ssize_t
4192md_attr_store(struct kobject *kobj, struct attribute *attr,
4193 const char *page, size_t length)
4194{
4195 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4196 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004197 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004198
4199 if (!entry->store)
4200 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004201 if (!capable(CAP_SYS_ADMIN))
4202 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004203 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004204 if (mddev->hold_active == UNTIL_IOCTL)
4205 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004206 if (!rv) {
4207 rv = entry->store(mddev, page, length);
4208 mddev_unlock(mddev);
4209 }
NeilBrown96de1e62005-11-08 21:39:39 -08004210 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004211}
4212
4213static void md_free(struct kobject *ko)
4214{
4215 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004216
4217 if (mddev->sysfs_state)
4218 sysfs_put(mddev->sysfs_state);
4219
4220 if (mddev->gendisk) {
4221 del_gendisk(mddev->gendisk);
4222 put_disk(mddev->gendisk);
4223 }
4224 if (mddev->queue)
4225 blk_cleanup_queue(mddev->queue);
4226
NeilBrowneae17012005-11-08 21:39:23 -08004227 kfree(mddev);
4228}
4229
Emese Revfy52cf25d2010-01-19 02:58:23 +01004230static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004231 .show = md_attr_show,
4232 .store = md_attr_store,
4233};
4234static struct kobj_type md_ktype = {
4235 .release = md_free,
4236 .sysfs_ops = &md_sysfs_ops,
4237 .default_attrs = md_default_attrs,
4238};
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240int mdp_major = 0;
4241
Dan Williams5fd3a172009-03-04 00:57:25 -07004242static void mddev_delayed_delete(struct work_struct *ws)
4243{
4244 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4245
NeilBrown43a70502009-12-14 12:49:55 +11004246 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004247 kobject_del(&mddev->kobj);
4248 kobject_put(&mddev->kobj);
4249}
4250
NeilBrownefeb53c2009-01-09 08:31:10 +11004251static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004253 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 mddev_t *mddev = mddev_find(dev);
4255 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004256 int partitioned;
4257 int shift;
4258 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004259 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004262 return -ENODEV;
4263
4264 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4265 shift = partitioned ? MdpMinorShift : 0;
4266 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Tejun Heoe804ac72010-10-15 15:36:08 +02004268 /* wait for any previous instance of this device to be
4269 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004270 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004271 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004272
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004273 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004274 error = -EEXIST;
4275 if (mddev->gendisk)
4276 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004277
4278 if (name) {
4279 /* Need to ensure that 'name' is not a duplicate.
4280 */
4281 mddev_t *mddev2;
4282 spin_lock(&all_mddevs_lock);
4283
4284 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4285 if (mddev2->gendisk &&
4286 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4287 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004288 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004289 }
4290 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
NeilBrown8b765392009-01-09 08:31:08 +11004292
NeilBrown0909dc42009-07-01 12:27:21 +10004293 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004294 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004295 if (!mddev->queue)
4296 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004297 mddev->queue->queuedata = mddev;
4298
NeilBrown8b765392009-01-09 08:31:08 +11004299 /* Can be unlocked because the queue is new: no concurrency */
4300 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4301
NeilBrown409c57f2009-03-31 14:39:39 +11004302 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 disk = alloc_disk(1 << shift);
4305 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004306 blk_cleanup_queue(mddev->queue);
4307 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004308 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004310 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004312 if (name)
4313 strcpy(disk->disk_name, name);
4314 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004316 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 disk->fops = &md_fops;
4319 disk->private_data = mddev;
4320 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004321 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004322 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004323 * remove it now.
4324 */
4325 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 add_disk(disk);
4327 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004328 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4329 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004330 if (error) {
4331 /* This isn't possible, but as kobject_init_and_add is marked
4332 * __must_check, we must do something with the result
4333 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004334 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4335 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004336 error = 0;
4337 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004338 if (mddev->kobj.sd &&
4339 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004340 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004341
4342 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004343 abort:
4344 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004345 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004346 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004347 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004348 }
NeilBrownd3374822009-01-09 08:31:10 +11004349 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004350 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004351}
4352
4353static struct kobject *md_probe(dev_t dev, int *part, void *data)
4354{
4355 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return NULL;
4357}
4358
NeilBrownefeb53c2009-01-09 08:31:10 +11004359static int add_named_array(const char *val, struct kernel_param *kp)
4360{
4361 /* val must be "md_*" where * is not all digits.
4362 * We allocate an array with a large free minor number, and
4363 * set the name to val. val must not already be an active name.
4364 */
4365 int len = strlen(val);
4366 char buf[DISK_NAME_LEN];
4367
4368 while (len && val[len-1] == '\n')
4369 len--;
4370 if (len >= DISK_NAME_LEN)
4371 return -E2BIG;
4372 strlcpy(buf, val, len+1);
4373 if (strncmp(buf, "md_", 3) != 0)
4374 return -EINVAL;
4375 return md_alloc(0, buf);
4376}
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378static void md_safemode_timeout(unsigned long data)
4379{
4380 mddev_t *mddev = (mddev_t *) data;
4381
Neil Brown0fd62b82008-06-28 08:31:36 +10004382 if (!atomic_read(&mddev->writes_pending)) {
4383 mddev->safemode = 1;
4384 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004385 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 md_wakeup_thread(mddev->thread);
4388}
4389
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004390static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
NeilBrown390ee602010-06-01 19:37:27 +10004392int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
NeilBrown2604b702006-01-06 00:20:36 -08004394 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004396 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
NeilBrowna757e642005-04-16 15:26:42 -07004398 if (list_empty(&mddev->disks))
4399 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
4402 if (mddev->pers)
4403 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004404 /* Cannot run until previous stop completes properly */
4405 if (mddev->sysfs_active)
4406 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 /*
4409 * Analyze all RAID superblock(s)
4410 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004411 if (!mddev->raid_disks) {
4412 if (!mddev->persistent)
4413 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004414 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
NeilBrownd9d166c2006-01-06 00:20:51 -08004417 if (mddev->level != LEVEL_NONE)
4418 request_module("md-level-%d", mddev->level);
4419 else if (mddev->clevel[0])
4420 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
4422 /*
4423 * Drop all container device buffers, from now on
4424 * the only valid external interface is through the md
4425 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004427 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004428 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 continue;
4430 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004431 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004432
4433 /* perform some consistency tests on the device.
4434 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004435 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004436 */
Andre Noll0f420352008-07-11 22:02:23 +10004437 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004438 if (mddev->dev_sectors &&
4439 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004440 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004441 printk("md: %s: data overlaps metadata\n",
4442 mdname(mddev));
4443 return -EINVAL;
4444 }
4445 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004446 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004447 > rdev->data_offset) {
4448 printk("md: %s: metadata overlaps data\n",
4449 mdname(mddev));
4450 return -EINVAL;
4451 }
4452 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004453 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
NeilBrowna167f662010-10-26 18:31:13 +11004456 if (mddev->bio_set == NULL)
4457 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004460 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004461 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004463 if (mddev->level != LEVEL_NONE)
4464 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4465 mddev->level);
4466 else
4467 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4468 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470 }
NeilBrown2604b702006-01-06 00:20:36 -08004471 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004473 if (mddev->level != pers->level) {
4474 mddev->level = pers->level;
4475 mddev->new_level = pers->level;
4476 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004477 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
NeilBrownf6705572006-03-27 01:18:11 -08004479 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004480 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004481 /* This personality cannot handle reshaping... */
4482 mddev->pers = NULL;
4483 module_put(pers->owner);
4484 return -EINVAL;
4485 }
4486
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004487 if (pers->sync_request) {
4488 /* Warn if this is a potentially silly
4489 * configuration.
4490 */
4491 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4492 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004493 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004494
4495 list_for_each_entry(rdev, &mddev->disks, same_set)
4496 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004497 if (rdev < rdev2 &&
4498 rdev->bdev->bd_contains ==
4499 rdev2->bdev->bd_contains) {
4500 printk(KERN_WARNING
4501 "%s: WARNING: %s appears to be"
4502 " on the same physical disk as"
4503 " %s.\n",
4504 mdname(mddev),
4505 bdevname(rdev->bdev,b),
4506 bdevname(rdev2->bdev,b2));
4507 warned = 1;
4508 }
4509 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004510
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004511 if (warned)
4512 printk(KERN_WARNING
4513 "True protection against single-disk"
4514 " failure might be compromised.\n");
4515 }
4516
NeilBrown657390d2005-08-26 18:34:16 -07004517 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004518 /* may be over-ridden by personality */
4519 mddev->resync_max_sectors = mddev->dev_sectors;
4520
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004521 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
NeilBrown0f9552b2009-12-30 12:08:50 +11004523 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004524 mddev->ro = 2; /* read-only, but switch on first write */
4525
NeilBrownb15c2e52006-01-06 00:20:16 -08004526 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004527 if (err)
4528 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004529 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4530 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4531 " but 'external_size' not in effect?\n", __func__);
4532 printk(KERN_ERR
4533 "md: invalid array_size %llu > default size %llu\n",
4534 (unsigned long long)mddev->array_sectors / 2,
4535 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4536 err = -EINVAL;
4537 mddev->pers->stop(mddev);
4538 }
4539 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004540 err = bitmap_create(mddev);
4541 if (err) {
4542 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4543 mdname(mddev), err);
4544 mddev->pers->stop(mddev);
4545 }
4546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 module_put(mddev->pers->owner);
4549 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004550 bitmap_destroy(mddev);
4551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004553 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004554 if (mddev->kobj.sd &&
4555 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004556 printk(KERN_WARNING
4557 "md: cannot register extra attributes for %s\n",
4558 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004559 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004560 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004561 mddev->ro = 0;
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004564 atomic_set(&mddev->max_corr_read_errors,
4565 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 mddev->safemode = 0;
4567 mddev->safemode_timer.function = md_safemode_timeout;
4568 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004569 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004571
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004572 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004573 if (rdev->raid_disk >= 0) {
4574 char nm[20];
4575 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004576 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004577 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4581
NeilBrown850b2b42006-10-03 01:15:46 -07004582 if (mddev->flags)
4583 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
NeilBrown0b8c9de2006-07-10 04:44:15 -07004585 md_wakeup_thread(mddev->thread);
4586 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004587
NeilBrownd7603b72006-01-06 00:20:30 -08004588 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004589 sysfs_notify_dirent_safe(mddev->sysfs_state);
4590 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004591 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 return 0;
4593}
NeilBrown390ee602010-06-01 19:37:27 +10004594EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
NeilBrownfe60b012010-03-29 11:10:42 +11004596static int do_md_run(mddev_t *mddev)
4597{
4598 int err;
4599
4600 err = md_run(mddev);
4601 if (err)
4602 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004603 err = bitmap_load(mddev);
4604 if (err) {
4605 bitmap_destroy(mddev);
4606 goto out;
4607 }
NeilBrownfe60b012010-03-29 11:10:42 +11004608 set_capacity(mddev->gendisk, mddev->array_sectors);
4609 revalidate_disk(mddev->gendisk);
4610 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4611out:
4612 return err;
4613}
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615static int restart_array(mddev_t *mddev)
4616{
4617 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Andre Noll80fab1d2008-07-11 22:02:21 +10004619 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004621 return -ENXIO;
4622 if (!mddev->pers)
4623 return -EINVAL;
4624 if (!mddev->ro)
4625 return -EBUSY;
4626 mddev->safemode = 0;
4627 mddev->ro = 0;
4628 set_disk_ro(disk, 0);
4629 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4630 mdname(mddev));
4631 /* Kick recovery or resync if necessary */
4632 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4633 md_wakeup_thread(mddev->thread);
4634 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004635 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
NeilBrownacc55e22006-06-26 00:27:47 -07004639/* similar to deny_write_access, but accounts for our holding a reference
4640 * to the file ourselves */
4641static int deny_bitmap_write_access(struct file * file)
4642{
4643 struct inode *inode = file->f_mapping->host;
4644
4645 spin_lock(&inode->i_lock);
4646 if (atomic_read(&inode->i_writecount) > 1) {
4647 spin_unlock(&inode->i_lock);
4648 return -ETXTBSY;
4649 }
4650 atomic_set(&inode->i_writecount, -1);
4651 spin_unlock(&inode->i_lock);
4652
4653 return 0;
4654}
4655
NeilBrown43a70502009-12-14 12:49:55 +11004656void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004657{
4658 struct inode *inode = file->f_mapping->host;
4659
4660 spin_lock(&inode->i_lock);
4661 atomic_set(&inode->i_writecount, 1);
4662 spin_unlock(&inode->i_lock);
4663}
4664
NeilBrown6177b472010-03-29 11:37:13 +11004665static void md_clean(mddev_t *mddev)
4666{
4667 mddev->array_sectors = 0;
4668 mddev->external_size = 0;
4669 mddev->dev_sectors = 0;
4670 mddev->raid_disks = 0;
4671 mddev->recovery_cp = 0;
4672 mddev->resync_min = 0;
4673 mddev->resync_max = MaxSector;
4674 mddev->reshape_position = MaxSector;
4675 mddev->external = 0;
4676 mddev->persistent = 0;
4677 mddev->level = LEVEL_NONE;
4678 mddev->clevel[0] = 0;
4679 mddev->flags = 0;
4680 mddev->ro = 0;
4681 mddev->metadata_type[0] = 0;
4682 mddev->chunk_sectors = 0;
4683 mddev->ctime = mddev->utime = 0;
4684 mddev->layout = 0;
4685 mddev->max_disks = 0;
4686 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004687 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004688 mddev->delta_disks = 0;
4689 mddev->new_level = LEVEL_NONE;
4690 mddev->new_layout = 0;
4691 mddev->new_chunk_sectors = 0;
4692 mddev->curr_resync = 0;
4693 mddev->resync_mismatches = 0;
4694 mddev->suspend_lo = mddev->suspend_hi = 0;
4695 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4696 mddev->recovery = 0;
4697 mddev->in_sync = 0;
4698 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004699 mddev->safemode = 0;
4700 mddev->bitmap_info.offset = 0;
4701 mddev->bitmap_info.default_offset = 0;
4702 mddev->bitmap_info.chunksize = 0;
4703 mddev->bitmap_info.daemon_sleep = 0;
4704 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004705 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004706}
4707
NeilBrown390ee602010-06-01 19:37:27 +10004708void md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004709{
4710 if (mddev->sync_thread) {
4711 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4712 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4713 md_unregister_thread(mddev->sync_thread);
4714 mddev->sync_thread = NULL;
4715 }
4716
4717 del_timer_sync(&mddev->safemode_timer);
4718
4719 bitmap_flush(mddev);
4720 md_super_wait(mddev);
4721
4722 if (!mddev->in_sync || mddev->flags) {
4723 /* mark array as shutdown cleanly */
4724 mddev->in_sync = 1;
4725 md_update_sb(mddev, 1);
4726 }
4727}
NeilBrown390ee602010-06-01 19:37:27 +10004728EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004729
NeilBrown390ee602010-06-01 19:37:27 +10004730void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004731{
4732 mddev->pers->stop(mddev);
4733 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4734 mddev->to_remove = &md_redundancy_group;
4735 module_put(mddev->pers->owner);
4736 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004737 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004738}
NeilBrown390ee602010-06-01 19:37:27 +10004739EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004740
NeilBrowna4bd82d2010-03-29 13:23:10 +11004741static int md_set_readonly(mddev_t *mddev, int is_open)
4742{
4743 int err = 0;
4744 mutex_lock(&mddev->open_mutex);
4745 if (atomic_read(&mddev->openers) > is_open) {
4746 printk("md: %s still in use.\n",mdname(mddev));
4747 err = -EBUSY;
4748 goto out;
4749 }
4750 if (mddev->pers) {
4751 md_stop_writes(mddev);
4752
4753 err = -ENXIO;
4754 if (mddev->ro==1)
4755 goto out;
4756 mddev->ro = 1;
4757 set_disk_ro(mddev->gendisk, 1);
4758 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004759 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004760 err = 0;
4761 }
4762out:
4763 mutex_unlock(&mddev->open_mutex);
4764 return err;
4765}
4766
NeilBrown9e653b62006-06-26 00:27:58 -07004767/* mode:
4768 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004769 * 2 - stop but do not disassemble array
4770 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004771static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004774 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
NeilBrownc8c00a62009-08-10 12:50:52 +10004776 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004777 if (atomic_read(&mddev->openers) > is_open ||
4778 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004779 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004780 mutex_unlock(&mddev->open_mutex);
4781 return -EBUSY;
4782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
NeilBrown6e17b022010-08-07 21:41:19 +10004784 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004785 if (mddev->ro)
4786 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004787
NeilBrown390ee602010-06-01 19:37:27 +10004788 md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004789 md_stop(mddev);
4790 mddev->queue->merge_bvec_fn = NULL;
4791 mddev->queue->unplug_fn = NULL;
4792 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004793
NeilBrowna4bd82d2010-03-29 13:23:10 +11004794 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004795 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004796
NeilBrowna4bd82d2010-03-29 13:23:10 +11004797 list_for_each_entry(rdev, &mddev->disks, same_set)
4798 if (rdev->raid_disk >= 0) {
4799 char nm[20];
4800 sprintf(nm, "rd%d", rdev->raid_disk);
4801 sysfs_remove_link(&mddev->kobj, nm);
4802 }
NeilBrownc4647292009-05-07 12:51:06 +10004803
NeilBrowna4bd82d2010-03-29 13:23:10 +11004804 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004805 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004806 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004807
NeilBrowna4bd82d2010-03-29 13:23:10 +11004808 if (mddev->ro)
4809 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004810 } else
4811 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 /*
4813 * Free resources if final stop
4814 */
NeilBrown9e653b62006-06-26 00:27:58 -07004815 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4817
NeilBrown978f9462006-02-02 14:28:05 -08004818 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004819 if (mddev->bitmap_info.file) {
4820 restore_bitmap_write_access(mddev->bitmap_info.file);
4821 fput(mddev->bitmap_info.file);
4822 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004823 }
NeilBrownc3d97142009-12-14 12:49:52 +11004824 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 export_array(mddev);
4827
NeilBrown6177b472010-03-29 11:37:13 +11004828 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004829 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004830 if (mddev->hold_active == UNTIL_STOP)
4831 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004832 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004833 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004834 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004835 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837}
4838
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004839#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840static void autorun_array(mddev_t *mddev)
4841{
4842 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 int err;
4844
NeilBrowna757e642005-04-16 15:26:42 -07004845 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 printk(KERN_INFO "md: running: ");
4849
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004850 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 char b[BDEVNAME_SIZE];
4852 printk("<%s>", bdevname(rdev->bdev,b));
4853 }
4854 printk("\n");
4855
NeilBrownd710e132008-10-13 11:55:12 +11004856 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (err) {
4858 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004859 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
4861}
4862
4863/*
4864 * lets try to run arrays based on all disks that have arrived
4865 * until now. (those are in pending_raid_disks)
4866 *
4867 * the method: pick the first pending disk, collect all disks with
4868 * the same UUID, remove all from the pending list and put them into
4869 * the 'same_array' list. Then order this list based on superblock
4870 * update time (freshest comes first), kick out 'old' disks and
4871 * compare superblocks. If everything's fine then run it.
4872 *
4873 * If "unit" is allocated, then bump its reference count
4874 */
4875static void autorun_devices(int part)
4876{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004877 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 mddev_t *mddev;
4879 char b[BDEVNAME_SIZE];
4880
4881 printk(KERN_INFO "md: autorun ...\n");
4882 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004883 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004885 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 rdev0 = list_entry(pending_raid_disks.next,
4887 mdk_rdev_t, same_set);
4888
4889 printk(KERN_INFO "md: considering %s ...\n",
4890 bdevname(rdev0->bdev,b));
4891 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004892 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 if (super_90_load(rdev, rdev0, 0) >= 0) {
4894 printk(KERN_INFO "md: adding %s ...\n",
4895 bdevname(rdev->bdev,b));
4896 list_move(&rdev->same_set, &candidates);
4897 }
4898 /*
4899 * now we have a set of devices, with all of them having
4900 * mostly sane superblocks. It's time to allocate the
4901 * mddev.
4902 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004903 if (part) {
4904 dev = MKDEV(mdp_major,
4905 rdev0->preferred_minor << MdpMinorShift);
4906 unit = MINOR(dev) >> MdpMinorShift;
4907 } else {
4908 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4909 unit = MINOR(dev);
4910 }
4911 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4913 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4914 break;
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 md_probe(dev, NULL, NULL);
4918 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004919 if (!mddev || !mddev->gendisk) {
4920 if (mddev)
4921 mddev_put(mddev);
4922 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 "md: cannot allocate memory for md drive.\n");
4924 break;
4925 }
4926 if (mddev_lock(mddev))
4927 printk(KERN_WARNING "md: %s locked, cannot run\n",
4928 mdname(mddev));
4929 else if (mddev->raid_disks || mddev->major_version
4930 || !list_empty(&mddev->disks)) {
4931 printk(KERN_WARNING
4932 "md: %s already running, cannot run %s\n",
4933 mdname(mddev), bdevname(rdev0->bdev,b));
4934 mddev_unlock(mddev);
4935 } else {
4936 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004937 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004938 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 list_del_init(&rdev->same_set);
4940 if (bind_rdev_to_array(rdev, mddev))
4941 export_rdev(rdev);
4942 }
4943 autorun_array(mddev);
4944 mddev_unlock(mddev);
4945 }
4946 /* on success, candidates will be empty, on error
4947 * it won't...
4948 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004949 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004950 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 mddev_put(mddev);
4954 }
4955 printk(KERN_INFO "md: ... autorun DONE.\n");
4956}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004957#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959static int get_version(void __user * arg)
4960{
4961 mdu_version_t ver;
4962
4963 ver.major = MD_MAJOR_VERSION;
4964 ver.minor = MD_MINOR_VERSION;
4965 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4966
4967 if (copy_to_user(arg, &ver, sizeof(ver)))
4968 return -EFAULT;
4969
4970 return 0;
4971}
4972
4973static int get_array_info(mddev_t * mddev, void __user * arg)
4974{
4975 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004976 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
NeilBrowna9f326e2009-09-23 18:06:41 +10004979 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004980 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004982 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 failed++;
4984 else {
4985 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004986 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004987 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 else
4989 spare++;
4990 }
4991 }
4992
4993 info.major_version = mddev->major_version;
4994 info.minor_version = mddev->minor_version;
4995 info.patch_version = MD_PATCHLEVEL_VERSION;
4996 info.ctime = mddev->ctime;
4997 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004998 info.size = mddev->dev_sectors / 2;
4999 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005000 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 info.nr_disks = nr;
5002 info.raid_disks = mddev->raid_disks;
5003 info.md_minor = mddev->md_minor;
5004 info.not_persistent= !mddev->persistent;
5005
5006 info.utime = mddev->utime;
5007 info.state = 0;
5008 if (mddev->in_sync)
5009 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005010 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005011 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005012 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 info.working_disks = working;
5014 info.failed_disks = failed;
5015 info.spare_disks = spare;
5016
5017 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005018 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 if (copy_to_user(arg, &info, sizeof(info)))
5021 return -EFAULT;
5022
5023 return 0;
5024}
5025
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005026static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005027{
5028 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5029 char *ptr, *buf = NULL;
5030 int err = -ENOMEM;
5031
Dan Williamsb5470dc2008-06-27 21:44:04 -07005032 if (md_allow_write(mddev))
5033 file = kmalloc(sizeof(*file), GFP_NOIO);
5034 else
5035 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005036
NeilBrown32a76272005-06-21 17:17:14 -07005037 if (!file)
5038 goto out;
5039
5040 /* bitmap disabled, zero the first byte and copy out */
5041 if (!mddev->bitmap || !mddev->bitmap->file) {
5042 file->pathname[0] = '\0';
5043 goto copy_out;
5044 }
5045
5046 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5047 if (!buf)
5048 goto out;
5049
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005050 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5051 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005052 goto out;
5053
5054 strcpy(file->pathname, ptr);
5055
5056copy_out:
5057 err = 0;
5058 if (copy_to_user(arg, file, sizeof(*file)))
5059 err = -EFAULT;
5060out:
5061 kfree(buf);
5062 kfree(file);
5063 return err;
5064}
5065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066static int get_disk_info(mddev_t * mddev, void __user * arg)
5067{
5068 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 mdk_rdev_t *rdev;
5070
5071 if (copy_from_user(&info, arg, sizeof(info)))
5072 return -EFAULT;
5073
Andre Noll26ef3792008-07-11 22:02:21 +10005074 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 if (rdev) {
5076 info.major = MAJOR(rdev->bdev->bd_dev);
5077 info.minor = MINOR(rdev->bdev->bd_dev);
5078 info.raid_disk = rdev->raid_disk;
5079 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005080 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005082 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 info.state |= (1<<MD_DISK_ACTIVE);
5084 info.state |= (1<<MD_DISK_SYNC);
5085 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005086 if (test_bit(WriteMostly, &rdev->flags))
5087 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 } else {
5089 info.major = info.minor = 0;
5090 info.raid_disk = -1;
5091 info.state = (1<<MD_DISK_REMOVED);
5092 }
5093
5094 if (copy_to_user(arg, &info, sizeof(info)))
5095 return -EFAULT;
5096
5097 return 0;
5098}
5099
5100static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5101{
5102 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5103 mdk_rdev_t *rdev;
5104 dev_t dev = MKDEV(info->major,info->minor);
5105
5106 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5107 return -EOVERFLOW;
5108
5109 if (!mddev->raid_disks) {
5110 int err;
5111 /* expecting a device which has a superblock */
5112 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5113 if (IS_ERR(rdev)) {
5114 printk(KERN_WARNING
5115 "md: md_import_device returned %ld\n",
5116 PTR_ERR(rdev));
5117 return PTR_ERR(rdev);
5118 }
5119 if (!list_empty(&mddev->disks)) {
5120 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5121 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005122 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 .load_super(rdev, rdev0, mddev->minor_version);
5124 if (err < 0) {
5125 printk(KERN_WARNING
5126 "md: %s has different UUID to %s\n",
5127 bdevname(rdev->bdev,b),
5128 bdevname(rdev0->bdev,b2));
5129 export_rdev(rdev);
5130 return -EINVAL;
5131 }
5132 }
5133 err = bind_rdev_to_array(rdev, mddev);
5134 if (err)
5135 export_rdev(rdev);
5136 return err;
5137 }
5138
5139 /*
5140 * add_new_disk can be used once the array is assembled
5141 * to add "hot spares". They must already have a superblock
5142 * written
5143 */
5144 if (mddev->pers) {
5145 int err;
5146 if (!mddev->pers->hot_add_disk) {
5147 printk(KERN_WARNING
5148 "%s: personality does not support diskops!\n",
5149 mdname(mddev));
5150 return -EINVAL;
5151 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005152 if (mddev->persistent)
5153 rdev = md_import_device(dev, mddev->major_version,
5154 mddev->minor_version);
5155 else
5156 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 if (IS_ERR(rdev)) {
5158 printk(KERN_WARNING
5159 "md: md_import_device returned %ld\n",
5160 PTR_ERR(rdev));
5161 return PTR_ERR(rdev);
5162 }
NeilBrown41158c72005-06-21 17:17:25 -07005163 /* set save_raid_disk if appropriate */
5164 if (!mddev->persistent) {
5165 if (info->state & (1<<MD_DISK_SYNC) &&
5166 info->raid_disk < mddev->raid_disks)
5167 rdev->raid_disk = info->raid_disk;
5168 else
5169 rdev->raid_disk = -1;
5170 } else
5171 super_types[mddev->major_version].
5172 validate_super(mddev, rdev);
5173 rdev->saved_raid_disk = rdev->raid_disk;
5174
NeilBrownb2d444d2005-11-08 21:39:31 -08005175 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005176 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5177 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005178 else
5179 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 rdev->raid_disk = -1;
5182 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005183 if (!err && !mddev->pers->hot_remove_disk) {
5184 /* If there is hot_add_disk but no hot_remove_disk
5185 * then added disks for geometry changes,
5186 * and should be added immediately.
5187 */
5188 super_types[mddev->major_version].
5189 validate_super(mddev, rdev);
5190 err = mddev->pers->hot_add_disk(mddev, rdev);
5191 if (err)
5192 unbind_rdev_from_array(rdev);
5193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (err)
5195 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005196 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005197 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005198
NeilBrown17571282006-12-10 02:20:52 -08005199 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005200 if (mddev->degraded)
5201 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005202 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005203 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return err;
5205 }
5206
5207 /* otherwise, add_new_disk is only allowed
5208 * for major_version==0 superblocks
5209 */
5210 if (mddev->major_version != 0) {
5211 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5212 mdname(mddev));
5213 return -EINVAL;
5214 }
5215
5216 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5217 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005218 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 if (IS_ERR(rdev)) {
5220 printk(KERN_WARNING
5221 "md: error, md_import_device() returned %ld\n",
5222 PTR_ERR(rdev));
5223 return PTR_ERR(rdev);
5224 }
5225 rdev->desc_nr = info->number;
5226 if (info->raid_disk < mddev->raid_disks)
5227 rdev->raid_disk = info->raid_disk;
5228 else
5229 rdev->raid_disk = -1;
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005232 if (info->state & (1<<MD_DISK_SYNC))
5233 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005235 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5236 set_bit(WriteMostly, &rdev->flags);
5237
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (!mddev->persistent) {
5239 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005240 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5241 } else
Andre Noll0f420352008-07-11 22:02:23 +10005242 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005243 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
NeilBrown2bf071b2006-01-06 00:20:55 -08005245 err = bind_rdev_to_array(rdev, mddev);
5246 if (err) {
5247 export_rdev(rdev);
5248 return err;
5249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 }
5251
5252 return 0;
5253}
5254
5255static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5256{
5257 char b[BDEVNAME_SIZE];
5258 mdk_rdev_t *rdev;
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 rdev = find_rdev(mddev, dev);
5261 if (!rdev)
5262 return -ENXIO;
5263
5264 if (rdev->raid_disk >= 0)
5265 goto busy;
5266
5267 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005268 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005269 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
5271 return 0;
5272busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005273 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 bdevname(rdev->bdev,b), mdname(mddev));
5275 return -EBUSY;
5276}
5277
5278static int hot_add_disk(mddev_t * mddev, dev_t dev)
5279{
5280 char b[BDEVNAME_SIZE];
5281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 mdk_rdev_t *rdev;
5283
5284 if (!mddev->pers)
5285 return -ENODEV;
5286
5287 if (mddev->major_version != 0) {
5288 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5289 " version-0 superblocks.\n",
5290 mdname(mddev));
5291 return -EINVAL;
5292 }
5293 if (!mddev->pers->hot_add_disk) {
5294 printk(KERN_WARNING
5295 "%s: personality does not support diskops!\n",
5296 mdname(mddev));
5297 return -EINVAL;
5298 }
5299
NeilBrownd710e132008-10-13 11:55:12 +11005300 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 if (IS_ERR(rdev)) {
5302 printk(KERN_WARNING
5303 "md: error, md_import_device() returned %ld\n",
5304 PTR_ERR(rdev));
5305 return -EINVAL;
5306 }
5307
5308 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005309 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005311 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
NeilBrown8190e752009-06-18 08:48:58 +10005313 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
NeilBrownb2d444d2005-11-08 21:39:31 -08005315 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 printk(KERN_WARNING
5317 "md: can not hot-add faulty %s disk to %s!\n",
5318 bdevname(rdev->bdev,b), mdname(mddev));
5319 err = -EINVAL;
5320 goto abort_export;
5321 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005322 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005324 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005325 err = bind_rdev_to_array(rdev, mddev);
5326 if (err)
5327 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
5329 /*
5330 * The rest should better be atomic, we can have disk failures
5331 * noticed in interrupt contexts ...
5332 */
5333
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 rdev->raid_disk = -1;
5335
NeilBrown850b2b42006-10-03 01:15:46 -07005336 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 /*
5339 * Kick recovery, maybe this spare has to be added to the
5340 * array immediately.
5341 */
5342 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5343 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005344 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 return 0;
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347abort_export:
5348 export_rdev(rdev);
5349 return err;
5350}
5351
NeilBrown32a76272005-06-21 17:17:14 -07005352static int set_bitmap_file(mddev_t *mddev, int fd)
5353{
5354 int err;
5355
NeilBrown36fa3062005-09-09 16:23:45 -07005356 if (mddev->pers) {
5357 if (!mddev->pers->quiesce)
5358 return -EBUSY;
5359 if (mddev->recovery || mddev->sync_thread)
5360 return -EBUSY;
5361 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005362 }
5363
NeilBrown36fa3062005-09-09 16:23:45 -07005364
5365 if (fd >= 0) {
5366 if (mddev->bitmap)
5367 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005368 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005369
NeilBrownc3d97142009-12-14 12:49:52 +11005370 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005371 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5372 mdname(mddev));
5373 return -EBADF;
5374 }
5375
NeilBrownc3d97142009-12-14 12:49:52 +11005376 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005377 if (err) {
5378 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5379 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005380 fput(mddev->bitmap_info.file);
5381 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005382 return err;
5383 }
NeilBrownc3d97142009-12-14 12:49:52 +11005384 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005385 } else if (mddev->bitmap == NULL)
5386 return -ENOENT; /* cannot remove what isn't there */
5387 err = 0;
5388 if (mddev->pers) {
5389 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005390 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005391 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005392 if (!err)
5393 err = bitmap_load(mddev);
5394 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005395 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005396 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005397 fd = -1; /* make sure to put the file */
5398 }
NeilBrown36fa3062005-09-09 16:23:45 -07005399 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005400 }
5401 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005402 if (mddev->bitmap_info.file) {
5403 restore_bitmap_write_access(mddev->bitmap_info.file);
5404 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005405 }
NeilBrownc3d97142009-12-14 12:49:52 +11005406 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005407 }
5408
NeilBrown32a76272005-06-21 17:17:14 -07005409 return err;
5410}
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412/*
5413 * set_array_info is used two different ways
5414 * The original usage is when creating a new array.
5415 * In this usage, raid_disks is > 0 and it together with
5416 * level, size, not_persistent,layout,chunksize determine the
5417 * shape of the array.
5418 * This will always create an array with a type-0.90.0 superblock.
5419 * The newer usage is when assembling an array.
5420 * In this case raid_disks will be 0, and the major_version field is
5421 * use to determine which style super-blocks are to be found on the devices.
5422 * The minor and patch _version numbers are also kept incase the
5423 * super_block handler wishes to interpret them.
5424 */
5425static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5426{
5427
5428 if (info->raid_disks == 0) {
5429 /* just setting version number for superblock loading */
5430 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005431 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 super_types[info->major_version].name == NULL) {
5433 /* maybe try to auto-load a module? */
5434 printk(KERN_INFO
5435 "md: superblock version %d not known\n",
5436 info->major_version);
5437 return -EINVAL;
5438 }
5439 mddev->major_version = info->major_version;
5440 mddev->minor_version = info->minor_version;
5441 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005442 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005443 /* ensure mddev_put doesn't delete this now that there
5444 * is some minimal configuration.
5445 */
5446 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 return 0;
5448 }
5449 mddev->major_version = MD_MAJOR_VERSION;
5450 mddev->minor_version = MD_MINOR_VERSION;
5451 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5452 mddev->ctime = get_seconds();
5453
5454 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005455 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005456 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 mddev->raid_disks = info->raid_disks;
5458 /* don't set md_minor, it is determined by which /dev/md* was
5459 * openned
5460 */
5461 if (info->state & (1<<MD_SB_CLEAN))
5462 mddev->recovery_cp = MaxSector;
5463 else
5464 mddev->recovery_cp = 0;
5465 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005466 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005469 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 mddev->max_disks = MD_SB_DISKS;
5472
NeilBrowne6910632008-02-06 01:39:51 -08005473 if (mddev->persistent)
5474 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005475 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
NeilBrownc3d97142009-12-14 12:49:52 +11005477 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5478 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005479
NeilBrownf6705572006-03-27 01:18:11 -08005480 mddev->reshape_position = MaxSector;
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 /*
5483 * Generate a 128 bit UUID
5484 */
5485 get_random_bytes(mddev->uuid, 16);
5486
NeilBrownf6705572006-03-27 01:18:11 -08005487 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005488 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005489 mddev->new_layout = mddev->layout;
5490 mddev->delta_disks = 0;
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 return 0;
5493}
5494
Dan Williams1f403622009-03-31 14:59:03 +11005495void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5496{
Dan Williamsb522adc2009-03-31 15:00:31 +11005497 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5498
5499 if (mddev->external_size)
5500 return;
5501
Dan Williams1f403622009-03-31 14:59:03 +11005502 mddev->array_sectors = array_sectors;
5503}
5504EXPORT_SYMBOL(md_set_array_sectors);
5505
Andre Nolld71f9f82008-07-11 22:02:22 +10005506static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005507{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005508 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005509 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005510 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005511
5512 if (mddev->pers->resize == NULL)
5513 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005514 /* The "num_sectors" is the number of sectors of each device that
5515 * is used. This can only make sense for arrays with redundancy.
5516 * linear and raid0 always use whatever space is available. We can only
5517 * consider changing this number if no resync or reconstruction is
5518 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005519 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005520 * of each device. If num_sectors is zero, we find the largest size
5521 * that fits.
5522
NeilBrowna35b0d62006-01-06 00:20:49 -08005523 */
5524 if (mddev->sync_thread)
5525 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005526 if (mddev->bitmap)
5527 /* Sorry, cannot grow a bitmap yet, just remove it,
5528 * grow, and re-add.
5529 */
5530 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005531 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005532 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005533
Andre Nolld71f9f82008-07-11 22:02:22 +10005534 if (fit && (num_sectors == 0 || num_sectors > avail))
5535 num_sectors = avail;
5536 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005537 return -ENOSPC;
5538 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005539 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005540 if (!rv)
5541 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005542 return rv;
5543}
5544
NeilBrownda943b992006-01-06 00:20:54 -08005545static int update_raid_disks(mddev_t *mddev, int raid_disks)
5546{
5547 int rv;
5548 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005549 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005550 return -EINVAL;
5551 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005552 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005553 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005554 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005555 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005556 mddev->delta_disks = raid_disks - mddev->raid_disks;
5557
5558 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005559 return rv;
5560}
5561
5562
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563/*
5564 * update_array_info is used to change the configuration of an
5565 * on-line array.
5566 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5567 * fields in the info are checked against the array.
5568 * Any differences that cannot be handled will cause an error.
5569 * Normally, only one change can be managed at a time.
5570 */
5571static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5572{
5573 int rv = 0;
5574 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005575 int state = 0;
5576
5577 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005578 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005579 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
5581 if (mddev->major_version != info->major_version ||
5582 mddev->minor_version != info->minor_version ||
5583/* mddev->patch_version != info->patch_version || */
5584 mddev->ctime != info->ctime ||
5585 mddev->level != info->level ||
5586/* mddev->layout != info->layout || */
5587 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005588 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005589 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5590 ((state^info->state) & 0xfffffe00)
5591 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 return -EINVAL;
5593 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005594 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5595 cnt++;
5596 if (mddev->raid_disks != info->raid_disks)
5597 cnt++;
5598 if (mddev->layout != info->layout)
5599 cnt++;
5600 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5601 cnt++;
5602 if (cnt == 0)
5603 return 0;
5604 if (cnt > 1)
5605 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607 if (mddev->layout != info->layout) {
5608 /* Change layout
5609 * we don't need to do anything at the md level, the
5610 * personality will take care of it all.
5611 */
NeilBrown50ac1682009-06-18 08:47:55 +10005612 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005614 else {
5615 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005616 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005617 if (rv)
5618 mddev->new_layout = mddev->layout;
5619 return rv;
5620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005622 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005623 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
NeilBrownda943b992006-01-06 00:20:54 -08005625 if (mddev->raid_disks != info->raid_disks)
5626 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
NeilBrown36fa3062005-09-09 16:23:45 -07005628 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5629 if (mddev->pers->quiesce == NULL)
5630 return -EINVAL;
5631 if (mddev->recovery || mddev->sync_thread)
5632 return -EBUSY;
5633 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5634 /* add the bitmap */
5635 if (mddev->bitmap)
5636 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005637 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005638 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005639 mddev->bitmap_info.offset =
5640 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005641 mddev->pers->quiesce(mddev, 1);
5642 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005643 if (!rv)
5644 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005645 if (rv)
5646 bitmap_destroy(mddev);
5647 mddev->pers->quiesce(mddev, 0);
5648 } else {
5649 /* remove the bitmap */
5650 if (!mddev->bitmap)
5651 return -ENOENT;
5652 if (mddev->bitmap->file)
5653 return -EINVAL;
5654 mddev->pers->quiesce(mddev, 1);
5655 bitmap_destroy(mddev);
5656 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005657 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005658 }
5659 }
NeilBrown850b2b42006-10-03 01:15:46 -07005660 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 return rv;
5662}
5663
5664static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5665{
5666 mdk_rdev_t *rdev;
5667
5668 if (mddev->pers == NULL)
5669 return -ENODEV;
5670
5671 rdev = find_rdev(mddev, dev);
5672 if (!rdev)
5673 return -ENODEV;
5674
5675 md_error(mddev, rdev);
5676 return 0;
5677}
5678
Andre Noll2f9618c2008-04-25 18:57:58 +02005679/*
5680 * We have a problem here : there is no easy way to give a CHS
5681 * virtual geometry. We currently pretend that we have a 2 heads
5682 * 4 sectors (with a BIG number of cylinders...). This drives
5683 * dosfs just mad... ;-)
5684 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005685static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5686{
5687 mddev_t *mddev = bdev->bd_disk->private_data;
5688
5689 geo->heads = 2;
5690 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005691 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005692 return 0;
5693}
5694
Al Viroa39907f2008-03-02 10:31:15 -05005695static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 unsigned int cmd, unsigned long arg)
5697{
5698 int err = 0;
5699 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005701 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 if (!capable(CAP_SYS_ADMIN))
5704 return -EACCES;
5705
5706 /*
5707 * Commands dealing with the RAID driver but not any
5708 * particular array:
5709 */
5710 switch (cmd)
5711 {
5712 case RAID_VERSION:
5713 err = get_version(argp);
5714 goto done;
5715
5716 case PRINT_RAID_DEBUG:
5717 err = 0;
5718 md_print_devices();
5719 goto done;
5720
5721#ifndef MODULE
5722 case RAID_AUTORUN:
5723 err = 0;
5724 autostart_arrays(arg);
5725 goto done;
5726#endif
5727 default:;
5728 }
5729
5730 /*
5731 * Commands creating/starting a new array:
5732 */
5733
Al Viroa39907f2008-03-02 10:31:15 -05005734 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 if (!mddev) {
5737 BUG();
5738 goto abort;
5739 }
5740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 err = mddev_lock(mddev);
5742 if (err) {
5743 printk(KERN_INFO
5744 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5745 err, cmd);
5746 goto abort;
5747 }
5748
5749 switch (cmd)
5750 {
5751 case SET_ARRAY_INFO:
5752 {
5753 mdu_array_info_t info;
5754 if (!arg)
5755 memset(&info, 0, sizeof(info));
5756 else if (copy_from_user(&info, argp, sizeof(info))) {
5757 err = -EFAULT;
5758 goto abort_unlock;
5759 }
5760 if (mddev->pers) {
5761 err = update_array_info(mddev, &info);
5762 if (err) {
5763 printk(KERN_WARNING "md: couldn't update"
5764 " array info. %d\n", err);
5765 goto abort_unlock;
5766 }
5767 goto done_unlock;
5768 }
5769 if (!list_empty(&mddev->disks)) {
5770 printk(KERN_WARNING
5771 "md: array %s already has disks!\n",
5772 mdname(mddev));
5773 err = -EBUSY;
5774 goto abort_unlock;
5775 }
5776 if (mddev->raid_disks) {
5777 printk(KERN_WARNING
5778 "md: array %s already initialised!\n",
5779 mdname(mddev));
5780 err = -EBUSY;
5781 goto abort_unlock;
5782 }
5783 err = set_array_info(mddev, &info);
5784 if (err) {
5785 printk(KERN_WARNING "md: couldn't set"
5786 " array info. %d\n", err);
5787 goto abort_unlock;
5788 }
5789 }
5790 goto done_unlock;
5791
5792 default:;
5793 }
5794
5795 /*
5796 * Commands querying/configuring an existing array:
5797 */
NeilBrown32a76272005-06-21 17:17:14 -07005798 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005799 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005800 if ((!mddev->raid_disks && !mddev->external)
5801 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5802 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5803 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 err = -ENODEV;
5805 goto abort_unlock;
5806 }
5807
5808 /*
5809 * Commands even a read-only array can execute:
5810 */
5811 switch (cmd)
5812 {
5813 case GET_ARRAY_INFO:
5814 err = get_array_info(mddev, argp);
5815 goto done_unlock;
5816
NeilBrown32a76272005-06-21 17:17:14 -07005817 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005818 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005819 goto done_unlock;
5820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 case GET_DISK_INFO:
5822 err = get_disk_info(mddev, argp);
5823 goto done_unlock;
5824
5825 case RESTART_ARRAY_RW:
5826 err = restart_array(mddev);
5827 goto done_unlock;
5828
5829 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005830 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 goto done_unlock;
5832
5833 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005834 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 goto done_unlock;
5836
Dan Williamse2218352010-05-12 08:25:37 +10005837 case BLKROSET:
5838 if (get_user(ro, (int __user *)(arg))) {
5839 err = -EFAULT;
5840 goto done_unlock;
5841 }
5842 err = -EINVAL;
5843
5844 /* if the bdev is going readonly the value of mddev->ro
5845 * does not matter, no writes are coming
5846 */
5847 if (ro)
5848 goto done_unlock;
5849
5850 /* are we are already prepared for writes? */
5851 if (mddev->ro != 1)
5852 goto done_unlock;
5853
5854 /* transitioning to readauto need only happen for
5855 * arrays that call md_write_start
5856 */
5857 if (mddev->pers) {
5858 err = restart_array(mddev);
5859 if (err == 0) {
5860 mddev->ro = 2;
5861 set_disk_ro(mddev->gendisk, 0);
5862 }
5863 }
5864 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 }
5866
5867 /*
5868 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005869 * superblock, so we do not allow them on read-only arrays.
5870 * However non-MD ioctls (e.g. get-size) will still come through
5871 * here and hit the 'default' below, so only disallow
5872 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005874 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005875 if (mddev->ro == 2) {
5876 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005877 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005878 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5879 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005880 } else {
5881 err = -EROFS;
5882 goto abort_unlock;
5883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 }
5885
5886 switch (cmd)
5887 {
5888 case ADD_NEW_DISK:
5889 {
5890 mdu_disk_info_t info;
5891 if (copy_from_user(&info, argp, sizeof(info)))
5892 err = -EFAULT;
5893 else
5894 err = add_new_disk(mddev, &info);
5895 goto done_unlock;
5896 }
5897
5898 case HOT_REMOVE_DISK:
5899 err = hot_remove_disk(mddev, new_decode_dev(arg));
5900 goto done_unlock;
5901
5902 case HOT_ADD_DISK:
5903 err = hot_add_disk(mddev, new_decode_dev(arg));
5904 goto done_unlock;
5905
5906 case SET_DISK_FAULTY:
5907 err = set_disk_faulty(mddev, new_decode_dev(arg));
5908 goto done_unlock;
5909
5910 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005911 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 goto done_unlock;
5913
NeilBrown32a76272005-06-21 17:17:14 -07005914 case SET_BITMAP_FILE:
5915 err = set_bitmap_file(mddev, (int)arg);
5916 goto done_unlock;
5917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 err = -EINVAL;
5920 goto abort_unlock;
5921 }
5922
5923done_unlock:
5924abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005925 if (mddev->hold_active == UNTIL_IOCTL &&
5926 err != -EINVAL)
5927 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 mddev_unlock(mddev);
5929
5930 return err;
5931done:
5932 if (err)
5933 MD_BUG();
5934abort:
5935 return err;
5936}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005937#ifdef CONFIG_COMPAT
5938static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5939 unsigned int cmd, unsigned long arg)
5940{
5941 switch (cmd) {
5942 case HOT_REMOVE_DISK:
5943 case HOT_ADD_DISK:
5944 case SET_DISK_FAULTY:
5945 case SET_BITMAP_FILE:
5946 /* These take in integer arg, do not convert */
5947 break;
5948 default:
5949 arg = (unsigned long)compat_ptr(arg);
5950 break;
5951 }
5952
5953 return md_ioctl(bdev, mode, cmd, arg);
5954}
5955#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
Al Viroa39907f2008-03-02 10:31:15 -05005957static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
5959 /*
5960 * Succeed if we can lock the mddev, which confirms that
5961 * it isn't being stopped right now.
5962 */
NeilBrownd3374822009-01-09 08:31:10 +11005963 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 int err;
5965
NeilBrownd3374822009-01-09 08:31:10 +11005966 if (mddev->gendisk != bdev->bd_disk) {
5967 /* we are racing with mddev_put which is discarding this
5968 * bd_disk.
5969 */
5970 mddev_put(mddev);
5971 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02005972 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005973 /* Then retry the open from the top */
5974 return -ERESTARTSYS;
5975 }
5976 BUG_ON(mddev != bdev->bd_disk->private_data);
5977
NeilBrownc8c00a62009-08-10 12:50:52 +10005978 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 goto out;
5980
5981 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005982 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005983 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
NeilBrownf3b99be2010-06-24 13:31:03 +10005985 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 out:
5987 return err;
5988}
5989
Al Viroa39907f2008-03-02 10:31:15 -05005990static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991{
Al Viroa39907f2008-03-02 10:31:15 -05005992 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005994 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005995 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 mddev_put(mddev);
5997
5998 return 0;
5999}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006000static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001{
6002 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006003 .open = md_open,
6004 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006005 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006006#ifdef CONFIG_COMPAT
6007 .compat_ioctl = md_compat_ioctl,
6008#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006009 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010};
6011
Adrian Bunk75c96f82005-05-05 16:16:09 -07006012static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
6014 mdk_thread_t *thread = arg;
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 /*
6017 * md_thread is a 'system-thread', it's priority should be very
6018 * high. We avoid resource deadlocks individually in each
6019 * raid personality. (RAID5 does preallocation) We also use RR and
6020 * the very same RT priority as kswapd, thus we will never get
6021 * into a priority inversion deadlock.
6022 *
6023 * we definitely have to have equal or higher priority than
6024 * bdflush, otherwise bdflush will deadlock if there are too
6025 * many dirty RAID5 blocks.
6026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
NeilBrown6985c432005-10-19 21:23:47 -07006028 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006029 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
NeilBrown93588e22005-11-15 00:09:12 -08006031 /* We need to wait INTERRUPTIBLE so that
6032 * we don't add to the load-average.
6033 * That means we need to be sure no signals are
6034 * pending
6035 */
6036 if (signal_pending(current))
6037 flush_signals(current);
6038
6039 wait_event_interruptible_timeout
6040 (thread->wqueue,
6041 test_bit(THREAD_WAKEUP, &thread->flags)
6042 || kthread_should_stop(),
6043 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
6045 clear_bit(THREAD_WAKEUP, &thread->flags);
6046
NeilBrown787453c2005-11-08 21:39:43 -08006047 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return 0;
6051}
6052
6053void md_wakeup_thread(mdk_thread_t *thread)
6054{
6055 if (thread) {
6056 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6057 set_bit(THREAD_WAKEUP, &thread->flags);
6058 wake_up(&thread->wqueue);
6059 }
6060}
6061
6062mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6063 const char *name)
6064{
6065 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
NeilBrown9ffae0c2006-01-06 00:20:32 -08006067 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 if (!thread)
6069 return NULL;
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 init_waitqueue_head(&thread->wqueue);
6072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 thread->run = run;
6074 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006075 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006076 thread->tsk = kthread_run(md_thread, thread,
6077 "%s_%s",
6078 mdname(thread->mddev),
6079 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006080 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 kfree(thread);
6082 return NULL;
6083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 return thread;
6085}
6086
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087void md_unregister_thread(mdk_thread_t *thread)
6088{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006089 if (!thread)
6090 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006091 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006092
6093 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 kfree(thread);
6095}
6096
6097void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6098{
6099 if (!mddev) {
6100 MD_BUG();
6101 return;
6102 }
6103
NeilBrownb2d444d2005-11-08 21:39:31 -08006104 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006106
6107 if (mddev->external)
6108 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6111 mdname(mddev),
6112 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6113 __builtin_return_address(0),__builtin_return_address(1),
6114 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006115*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006116 if (!mddev->pers)
6117 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 if (!mddev->pers->error_handler)
6119 return;
6120 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006121 if (mddev->degraded)
6122 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006123 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6125 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6126 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006127 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006128 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006129 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130}
6131
6132/* seq_file implementation /proc/mdstat */
6133
6134static void status_unused(struct seq_file *seq)
6135{
6136 int i = 0;
6137 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
6139 seq_printf(seq, "unused devices: ");
6140
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006141 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 char b[BDEVNAME_SIZE];
6143 i++;
6144 seq_printf(seq, "%s ",
6145 bdevname(rdev->bdev,b));
6146 }
6147 if (!i)
6148 seq_printf(seq, "<none>");
6149
6150 seq_printf(seq, "\n");
6151}
6152
6153
6154static void status_resync(struct seq_file *seq, mddev_t * mddev)
6155{
NeilBrowndd71cf62009-05-07 12:49:35 +10006156 sector_t max_sectors, resync, res;
6157 unsigned long dt, db;
6158 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006159 int scale;
6160 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
NeilBrowndd71cf62009-05-07 12:49:35 +10006162 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
6164 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006165 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006167 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
6169 /*
6170 * Should not happen.
6171 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006172 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 MD_BUG();
6174 return;
6175 }
NeilBrown4588b422006-03-27 01:18:04 -08006176 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006177 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006178 * u32, as those are the requirements for sector_div.
6179 * Thus 'scale' must be at least 10
6180 */
6181 scale = 10;
6182 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006183 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006184 scale++;
6185 }
6186 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006187 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006188
6189 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 {
NeilBrown4588b422006-03-27 01:18:04 -08006191 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 seq_printf(seq, "[");
6193 for (i = 0; i < x; i++)
6194 seq_printf(seq, "=");
6195 seq_printf(seq, ">");
6196 for (i = 0; i < y; i++)
6197 seq_printf(seq, ".");
6198 seq_printf(seq, "] ");
6199 }
NeilBrown4588b422006-03-27 01:18:04 -08006200 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006201 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6202 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006203 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6204 "check" :
6205 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6206 "resync" : "recovery"))),
6207 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006208 (unsigned long long) resync/2,
6209 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 * dt: time from mark until now
6213 * db: blocks written from mark until now
6214 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006215 *
6216 * rt is a sector_t, so could be 32bit or 64bit.
6217 * So we divide before multiply in case it is 32bit and close
6218 * to the limit.
6219 * We scale the divisor (db) by 32 to avoid loosing precision
6220 * near the end of resync when the number of remaining sectors
6221 * is close to 'db'.
6222 * We then divide rt by 32 after multiplying by db to compensate.
6223 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 */
6225 dt = ((jiffies - mddev->resync_mark) / HZ);
6226 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006227 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6228 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
NeilBrowndd71cf62009-05-07 12:49:35 +10006230 rt = max_sectors - resync; /* number of remaining sectors */
6231 sector_div(rt, db/32+1);
6232 rt *= dt;
6233 rt >>= 5;
6234
6235 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6236 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
NeilBrownff4e8d92006-07-10 04:44:16 -07006238 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6242{
6243 struct list_head *tmp;
6244 loff_t l = *pos;
6245 mddev_t *mddev;
6246
6247 if (l >= 0x10000)
6248 return NULL;
6249 if (!l--)
6250 /* header */
6251 return (void*)1;
6252
6253 spin_lock(&all_mddevs_lock);
6254 list_for_each(tmp,&all_mddevs)
6255 if (!l--) {
6256 mddev = list_entry(tmp, mddev_t, all_mddevs);
6257 mddev_get(mddev);
6258 spin_unlock(&all_mddevs_lock);
6259 return mddev;
6260 }
6261 spin_unlock(&all_mddevs_lock);
6262 if (!l--)
6263 return (void*)2;/* tail */
6264 return NULL;
6265}
6266
6267static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6268{
6269 struct list_head *tmp;
6270 mddev_t *next_mddev, *mddev = v;
6271
6272 ++*pos;
6273 if (v == (void*)2)
6274 return NULL;
6275
6276 spin_lock(&all_mddevs_lock);
6277 if (v == (void*)1)
6278 tmp = all_mddevs.next;
6279 else
6280 tmp = mddev->all_mddevs.next;
6281 if (tmp != &all_mddevs)
6282 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6283 else {
6284 next_mddev = (void*)2;
6285 *pos = 0x10000;
6286 }
6287 spin_unlock(&all_mddevs_lock);
6288
6289 if (v != (void*)1)
6290 mddev_put(mddev);
6291 return next_mddev;
6292
6293}
6294
6295static void md_seq_stop(struct seq_file *seq, void *v)
6296{
6297 mddev_t *mddev = v;
6298
6299 if (mddev && v != (void*)1 && v != (void*)2)
6300 mddev_put(mddev);
6301}
6302
NeilBrownd7603b72006-01-06 00:20:30 -08006303struct mdstat_info {
6304 int event;
6305};
6306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307static int md_seq_show(struct seq_file *seq, void *v)
6308{
6309 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006310 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006312 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006313 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
6315 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006316 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 seq_printf(seq, "Personalities : ");
6318 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006319 list_for_each_entry(pers, &pers_list, list)
6320 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
6322 spin_unlock(&pers_lock);
6323 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006324 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 return 0;
6326 }
6327 if (v == (void*)2) {
6328 status_unused(seq);
6329 return 0;
6330 }
6331
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006332 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006334
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6336 seq_printf(seq, "%s : %sactive", mdname(mddev),
6337 mddev->pers ? "" : "in");
6338 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006339 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006341 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006342 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 seq_printf(seq, " %s", mddev->pers->name);
6344 }
6345
Andre Nolldd8ac332009-03-31 14:33:13 +11006346 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006347 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 char b[BDEVNAME_SIZE];
6349 seq_printf(seq, " %s[%d]",
6350 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006351 if (test_bit(WriteMostly, &rdev->flags))
6352 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006353 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 seq_printf(seq, "(F)");
6355 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006356 } else if (rdev->raid_disk < 0)
6357 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006358 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 }
6360
6361 if (!list_empty(&mddev->disks)) {
6362 if (mddev->pers)
6363 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006364 (unsigned long long)
6365 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 else
6367 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006368 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006370 if (mddev->persistent) {
6371 if (mddev->major_version != 0 ||
6372 mddev->minor_version != 90) {
6373 seq_printf(seq," super %d.%d",
6374 mddev->major_version,
6375 mddev->minor_version);
6376 }
NeilBrowne6910632008-02-06 01:39:51 -08006377 } else if (mddev->external)
6378 seq_printf(seq, " super external:%s",
6379 mddev->metadata_type);
6380 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006381 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006384 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006386 if (mddev->pers->sync_request) {
6387 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006388 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006389 seq_printf(seq, "\n ");
6390 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6391 seq_printf(seq, "\tresync=DELAYED\n ");
6392 else if (mddev->recovery_cp < MaxSector)
6393 seq_printf(seq, "\tresync=PENDING\n ");
6394 }
NeilBrown32a76272005-06-21 17:17:14 -07006395 } else
6396 seq_printf(seq, "\n ");
6397
6398 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006399 unsigned long chunk_kb;
6400 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006401 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006402 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006403 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6404 "%lu%s chunk",
6405 bitmap->pages - bitmap->missing_pages,
6406 bitmap->pages,
6407 (bitmap->pages - bitmap->missing_pages)
6408 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006409 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006410 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006411 if (bitmap->file) {
6412 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006413 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006414 }
NeilBrown78d742d2005-06-21 17:17:15 -07006415
NeilBrown32a76272005-06-21 17:17:14 -07006416 seq_printf(seq, "\n");
6417 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
6419
6420 seq_printf(seq, "\n");
6421 }
6422 mddev_unlock(mddev);
6423
6424 return 0;
6425}
6426
Jan Engelhardt110518b2009-05-07 12:49:37 +10006427static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 .start = md_seq_start,
6429 .next = md_seq_next,
6430 .stop = md_seq_stop,
6431 .show = md_seq_show,
6432};
6433
6434static int md_seq_open(struct inode *inode, struct file *file)
6435{
6436 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006437 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6438 if (mi == NULL)
6439 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
6441 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006442 if (error)
6443 kfree(mi);
6444 else {
6445 struct seq_file *p = file->private_data;
6446 p->private = mi;
6447 mi->event = atomic_read(&md_event_count);
6448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 return error;
6450}
6451
NeilBrownd7603b72006-01-06 00:20:30 -08006452static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6453{
6454 struct seq_file *m = filp->private_data;
6455 struct mdstat_info *mi = m->private;
6456 int mask;
6457
6458 poll_wait(filp, &md_event_waiters, wait);
6459
6460 /* always allow read */
6461 mask = POLLIN | POLLRDNORM;
6462
6463 if (mi->event != atomic_read(&md_event_count))
6464 mask |= POLLERR | POLLPRI;
6465 return mask;
6466}
6467
Arjan van de Venfa027c22007-02-12 00:55:33 -08006468static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006469 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 .open = md_seq_open,
6471 .read = seq_read,
6472 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006473 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006474 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475};
6476
NeilBrown2604b702006-01-06 00:20:36 -08006477int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006480 list_add_tail(&p->list, &pers_list);
6481 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 spin_unlock(&pers_lock);
6483 return 0;
6484}
6485
NeilBrown2604b702006-01-06 00:20:36 -08006486int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487{
NeilBrown2604b702006-01-06 00:20:36 -08006488 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006490 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 spin_unlock(&pers_lock);
6492 return 0;
6493}
6494
NeilBrowneea1bf32009-03-31 14:27:02 +11006495static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496{
6497 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006499 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
6501 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006502 rcu_read_lock();
6503 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006505 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6506 (int)part_stat_read(&disk->part0, sectors[1]) -
6507 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006508 /* sync IO will cause sync_io to increase before the disk_stats
6509 * as sync_io is counted when a request starts, and
6510 * disk_stats is counted when it completes.
6511 * So resync activity will cause curr_events to be smaller than
6512 * when there was no such activity.
6513 * non-sync IO will cause disk_stat to increase without
6514 * increasing sync_io so curr_events will (eventually)
6515 * be larger than it was before. Once it becomes
6516 * substantially larger, the test below will cause
6517 * the array to appear non-idle, and resync will slow
6518 * down.
6519 * If there is a lot of outstanding resync activity when
6520 * we set last_event to curr_events, then all that activity
6521 * completing might cause the array to appear non-idle
6522 * and resync will be slowed down even though there might
6523 * not have been non-resync activity. This will only
6524 * happen once though. 'last_events' will soon reflect
6525 * the state where there is little or no outstanding
6526 * resync requests, and further resync activity will
6527 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006528 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006530 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 rdev->last_events = curr_events;
6532 idle = 0;
6533 }
6534 }
NeilBrown4b809912008-07-21 17:05:25 +10006535 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 return idle;
6537}
6538
6539void md_done_sync(mddev_t *mddev, int blocks, int ok)
6540{
6541 /* another "blocks" (512byte) blocks have been synced */
6542 atomic_sub(blocks, &mddev->recovery_active);
6543 wake_up(&mddev->recovery_wait);
6544 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006545 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 md_wakeup_thread(mddev->thread);
6547 // stop recovery, signal do_sync ....
6548 }
6549}
6550
6551
NeilBrown06d91a52005-06-21 17:17:12 -07006552/* md_write_start(mddev, bi)
6553 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006554 * in superblock) before writing, schedule a superblock update
6555 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006556 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006557void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558{
Neil Brown0fd62b82008-06-28 08:31:36 +10006559 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006560 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006561 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006562
NeilBrownf91de922005-11-08 21:39:36 -08006563 BUG_ON(mddev->ro == 1);
6564 if (mddev->ro == 2) {
6565 /* need to switch to read/write */
6566 mddev->ro = 0;
6567 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6568 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006569 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006570 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006571 }
NeilBrown06d91a52005-06-21 17:17:12 -07006572 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006573 if (mddev->safemode == 1)
6574 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006575 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006576 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006577 if (mddev->in_sync) {
6578 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006579 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006580 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006581 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006582 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006583 }
NeilBrowna9701a32005-11-08 21:39:34 -08006584 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006585 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006586 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006587 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006588 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006589 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
6591
6592void md_write_end(mddev_t *mddev)
6593{
6594 if (atomic_dec_and_test(&mddev->writes_pending)) {
6595 if (mddev->safemode == 2)
6596 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006597 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6599 }
6600}
6601
NeilBrown2a2275d2007-01-26 00:57:11 -08006602/* md_allow_write(mddev)
6603 * Calling this ensures that the array is marked 'active' so that writes
6604 * may proceed without blocking. It is important to call this before
6605 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6606 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006607 *
6608 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6609 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006610 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006611int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006612{
6613 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006614 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006615 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006616 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006617 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006618 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006619
6620 spin_lock_irq(&mddev->write_lock);
6621 if (mddev->in_sync) {
6622 mddev->in_sync = 0;
6623 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006624 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006625 if (mddev->safemode_delay &&
6626 mddev->safemode == 0)
6627 mddev->safemode = 1;
6628 spin_unlock_irq(&mddev->write_lock);
6629 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006630 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006631 } else
6632 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006633
NeilBrown070dc6d2010-08-30 17:33:34 +10006634 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006635 return -EAGAIN;
6636 else
6637 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006638}
6639EXPORT_SYMBOL_GPL(md_allow_write);
6640
NeilBrownb63d7c22010-06-01 19:37:33 +10006641void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006642{
6643 if (mddev->queue)
6644 blk_unplug(mddev->queue);
6645 if (mddev->plug)
6646 mddev->plug->unplug_fn(mddev->plug);
6647}
6648
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649#define SYNC_MARKS 10
6650#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006651void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652{
6653 mddev_t *mddev2;
6654 unsigned int currspeed = 0,
6655 window;
NeilBrown57afd892005-06-21 17:17:13 -07006656 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 unsigned long mark[SYNC_MARKS];
6658 sector_t mark_cnt[SYNC_MARKS];
6659 int last_mark,m;
6660 struct list_head *tmp;
6661 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006662 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006663 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006664 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
6666 /* just incase thread restarts... */
6667 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6668 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006669 if (mddev->ro) /* never try to sync a read-only array */
6670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
NeilBrown61df9d92006-10-03 01:15:57 -07006672 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6673 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6674 desc = "data-check";
6675 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6676 desc = "requested-resync";
6677 else
6678 desc = "resync";
6679 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6680 desc = "reshape";
6681 else
6682 desc = "recovery";
6683
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 /* we overload curr_resync somewhat here.
6685 * 0 == not engaged in resync at all
6686 * 2 == checking that there is no conflict with another sync
6687 * 1 == like 2, but have yielded to allow conflicting resync to
6688 * commense
6689 * other == active in resync - this many blocks
6690 *
6691 * Before starting a resync we must have set curr_resync to
6692 * 2, and then checked that every "conflicting" array has curr_resync
6693 * less than ours. When we find one that is the same or higher
6694 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6695 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6696 * This will mean we have to start checking from the beginning again.
6697 *
6698 */
6699
6700 do {
6701 mddev->curr_resync = 2;
6702
6703 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006704 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006705 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006706
6707 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006709 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 if (mddev2 == mddev)
6711 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006712 if (!mddev->parallel_resync
6713 && mddev2->curr_resync
6714 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 DEFINE_WAIT(wq);
6716 if (mddev < mddev2 && mddev->curr_resync == 2) {
6717 /* arbitrarily yield */
6718 mddev->curr_resync = 1;
6719 wake_up(&resync_wait);
6720 }
6721 if (mddev > mddev2 && mddev->curr_resync == 1)
6722 /* no need to wait here, we can wait the next
6723 * time 'round when curr_resync == 2
6724 */
6725 continue;
NeilBrown97441972008-09-19 11:49:54 +10006726 /* We need to wait 'interruptible' so as not to
6727 * contribute to the load average, and not to
6728 * be caught by 'softlockup'
6729 */
6730 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006731 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006732 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006733 printk(KERN_INFO "md: delaying %s of %s"
6734 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006736 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006738 if (signal_pending(current))
6739 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 schedule();
6741 finish_wait(&resync_wait, &wq);
6742 goto try_again;
6743 }
6744 finish_wait(&resync_wait, &wq);
6745 }
6746 }
6747 } while (mddev->curr_resync < 2);
6748
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006749 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006750 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006752 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 */
6754 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006755 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006756 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006757 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6758 j = mddev->resync_min;
6759 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006760 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006761
NeilBrownccfcc3c2006-03-27 01:18:09 -08006762 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006763 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006764 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006766 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006767 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006768 rcu_read_lock();
6769 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006770 if (rdev->raid_disk >= 0 &&
6771 !test_bit(Faulty, &rdev->flags) &&
6772 !test_bit(In_sync, &rdev->flags) &&
6773 rdev->recovery_offset < j)
6774 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006775 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
NeilBrown61df9d92006-10-03 01:15:57 -07006778 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6779 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6780 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006781 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006782 "(but not more than %d KB/sec) for %s.\n",
6783 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
NeilBrowneea1bf32009-03-31 14:27:02 +11006785 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006786
NeilBrown57afd892005-06-21 17:17:13 -07006787 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 for (m = 0; m < SYNC_MARKS; m++) {
6789 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006790 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 }
6792 last_mark = 0;
6793 mddev->resync_mark = mark[last_mark];
6794 mddev->resync_mark_cnt = mark_cnt[last_mark];
6795
6796 /*
6797 * Tune reconstruction:
6798 */
6799 window = 32*(PAGE_SIZE/512);
6800 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6801 window/2,(unsigned long long) max_sectors/2);
6802
6803 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 last_check = 0;
6805
6806 if (j>2) {
6807 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006808 "md: resuming %s of %s from checkpoint.\n",
6809 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 mddev->curr_resync = j;
6811 }
NeilBrownefa59332009-12-14 12:49:49 +11006812 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
6814 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006815 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
NeilBrown57afd892005-06-21 17:17:13 -07006817 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006818
NeilBrown7a91ee12009-05-26 12:57:21 +10006819 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6820 ((mddev->curr_resync > mddev->curr_resync_completed &&
6821 (mddev->curr_resync - mddev->curr_resync_completed)
6822 > (max_sectors >> 4)) ||
6823 (j - mddev->curr_resync_completed)*2
6824 >= mddev->resync_max - mddev->curr_resync_completed
6825 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006826 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006827 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006828 wait_event(mddev->recovery_wait,
6829 atomic_read(&mddev->recovery_active) == 0);
6830 mddev->curr_resync_completed =
6831 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006832 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006833 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006834 }
NeilBrownacb180b2009-04-14 16:28:34 +10006835
NeilBrowne62e58a2009-07-01 13:15:35 +10006836 while (j >= mddev->resync_max && !kthread_should_stop()) {
6837 /* As this condition is controlled by user-space,
6838 * we can block indefinitely, so use '_interruptible'
6839 * to avoid triggering warnings.
6840 */
6841 flush_signals(current); /* just in case */
6842 wait_event_interruptible(mddev->recovery_wait,
6843 mddev->resync_max > j
6844 || kthread_should_stop());
6845 }
NeilBrownacb180b2009-04-14 16:28:34 +10006846
6847 if (kthread_should_stop())
6848 goto interrupted;
6849
NeilBrown57afd892005-06-21 17:17:13 -07006850 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006851 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006852 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006853 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 goto out;
6855 }
NeilBrown57afd892005-06-21 17:17:13 -07006856
6857 if (!skipped) { /* actual IO requested */
6858 io_sectors += sectors;
6859 atomic_add(sectors, &mddev->recovery_active);
6860 }
6861
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 j += sectors;
6863 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006864 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006865 if (last_check == 0)
6866 /* this is the earliers that rebuilt will be
6867 * visible in /proc/mdstat
6868 */
6869 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006870
6871 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 continue;
6873
NeilBrown57afd892005-06-21 17:17:13 -07006874 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
NeilBrowndfc70642008-05-23 13:04:39 -07006876 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 break;
6878
6879 repeat:
6880 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6881 /* step marks */
6882 int next = (last_mark+1) % SYNC_MARKS;
6883
6884 mddev->resync_mark = mark[next];
6885 mddev->resync_mark_cnt = mark_cnt[next];
6886 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006887 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 last_mark = next;
6889 }
6890
6891
NeilBrownc6207272008-02-06 01:39:52 -08006892 if (kthread_should_stop())
6893 goto interrupted;
6894
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
6896 /*
6897 * this loop exits only if either when we are slower than
6898 * the 'hard' speed limit, or the system was IO-idle for
6899 * a jiffy.
6900 * the system might be non-idle CPU-wise, but we only care
6901 * about not overloading the IO subsystem. (things like an
6902 * e2fsck being done on the RAID array should execute fast)
6903 */
NeilBrown252ac522010-06-01 19:37:29 +10006904 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 cond_resched();
6906
NeilBrown57afd892005-06-21 17:17:13 -07006907 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6908 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
NeilBrown88202a02006-01-06 00:21:36 -08006910 if (currspeed > speed_min(mddev)) {
6911 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006912 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006913 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 goto repeat;
6915 }
6916 }
6917 }
NeilBrown61df9d92006-10-03 01:15:57 -07006918 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 /*
6920 * this also signals 'finished resyncing' to md_stop
6921 */
6922 out:
NeilBrown252ac522010-06-01 19:37:29 +10006923 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
6925 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6926
6927 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006928 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
NeilBrowndfc70642008-05-23 13:04:39 -07006930 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006931 mddev->curr_resync > 2) {
6932 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6933 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6934 if (mddev->curr_resync >= mddev->recovery_cp) {
6935 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006936 "md: checkpointing %s of %s.\n",
6937 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006938 mddev->recovery_cp = mddev->curr_resync;
6939 }
6940 } else
6941 mddev->recovery_cp = MaxSector;
6942 } else {
6943 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6944 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006945 rcu_read_lock();
6946 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006947 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006948 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006949 !test_bit(Faulty, &rdev->flags) &&
6950 !test_bit(In_sync, &rdev->flags) &&
6951 rdev->recovery_offset < mddev->curr_resync)
6952 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006953 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 }
NeilBrown17571282006-12-10 02:20:52 -08006956 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006959 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6960 /* We completed so min/max setting can be forgotten if used. */
6961 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6962 mddev->resync_min = 0;
6963 mddev->resync_max = MaxSector;
6964 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6965 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006967 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6968 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006969 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 wake_up(&resync_wait);
6971 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6972 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006973 return;
6974
6975 interrupted:
6976 /*
6977 * got a signal, exit.
6978 */
6979 printk(KERN_INFO
6980 "md: md_do_sync() got signal ... exiting\n");
6981 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6982 goto out;
6983
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984}
NeilBrown29269552006-03-27 01:18:10 -08006985EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
6987
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006988static int remove_and_add_spares(mddev_t *mddev)
6989{
6990 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006991 int spares = 0;
6992
NeilBrown97e4f422009-03-31 14:33:13 +11006993 mddev->curr_resync_completed = 0;
6994
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006995 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006996 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006997 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006998 (test_bit(Faulty, &rdev->flags) ||
6999 ! test_bit(In_sync, &rdev->flags)) &&
7000 atomic_read(&rdev->nr_pending)==0) {
7001 if (mddev->pers->hot_remove_disk(
7002 mddev, rdev->raid_disk)==0) {
7003 char nm[20];
7004 sprintf(nm,"rd%d", rdev->raid_disk);
7005 sysfs_remove_link(&mddev->kobj, nm);
7006 rdev->raid_disk = -1;
7007 }
7008 }
7009
NeilBrown4044ba52009-01-09 08:31:11 +11007010 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007011 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007012 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007013 !test_bit(In_sync, &rdev->flags) &&
7014 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007015 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007016 if (rdev->raid_disk < 0
7017 && !test_bit(Faulty, &rdev->flags)) {
7018 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007019 if (mddev->pers->
7020 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007021 char nm[20];
7022 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007023 if (sysfs_create_link(&mddev->kobj,
7024 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007025 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007026 spares++;
7027 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007028 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007029 } else
7030 break;
7031 }
NeilBrowndfc70642008-05-23 13:04:39 -07007032 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007033 }
7034 return spares;
7035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036/*
7037 * This routine is regularly called by all per-raid-array threads to
7038 * deal with generic issues like resync and super-block update.
7039 * Raid personalities that don't have a thread (linear/raid0) do not
7040 * need this as they never do any recovery or update the superblock.
7041 *
7042 * It does not do any resync itself, but rather "forks" off other threads
7043 * to do that as needed.
7044 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7045 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007046 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 * and wakeups up this thread which will reap the thread and finish up.
7048 * This thread also removes any faulty devices (with nr_pending == 0).
7049 *
7050 * The overall approach is:
7051 * 1/ if the superblock needs updating, update it.
7052 * 2/ If a recovery thread is running, don't do anything else.
7053 * 3/ If recovery has finished, clean up, possibly marking spares active.
7054 * 4/ If there are any faulty devices, remove them.
7055 * 5/ If array is degraded, try to add spares devices
7056 * 6/ If array has spares or is not in-sync, start a resync thread.
7057 */
7058void md_check_recovery(mddev_t *mddev)
7059{
7060 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061
7062
NeilBrown5f404022005-06-21 17:17:16 -07007063 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007064 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
7066 if (mddev->ro)
7067 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007068
7069 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007070 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007071 printk(KERN_INFO "md: %s in immediate safe mode\n",
7072 mdname(mddev));
7073 mddev->safemode = 2;
7074 }
7075 flush_signals(current);
7076 }
7077
NeilBrownc89a8ee2008-08-05 15:54:13 +10007078 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007081 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007083 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007084 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007085 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7086 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 ))
7088 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007089
NeilBrowndf5b89b2006-03-27 01:18:20 -08007090 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007091 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007092
NeilBrownc89a8ee2008-08-05 15:54:13 +10007093 if (mddev->ro) {
7094 /* Only thing we do on a ro array is remove
7095 * failed devices.
7096 */
7097 remove_and_add_spares(mddev);
7098 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7099 goto unlock;
7100 }
7101
NeilBrown31a59e32008-04-30 00:52:30 -07007102 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007103 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007104 spin_lock_irq(&mddev->write_lock);
7105 if (mddev->safemode &&
7106 !atomic_read(&mddev->writes_pending) &&
7107 !mddev->in_sync &&
7108 mddev->recovery_cp == MaxSector) {
7109 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007110 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007111 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007112 }
7113 if (mddev->safemode == 1)
7114 mddev->safemode = 0;
7115 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007116 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007117 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007118 }
NeilBrownfca4d842005-06-21 17:17:11 -07007119
NeilBrown850b2b42006-10-03 01:15:46 -07007120 if (mddev->flags)
7121 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7124 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7125 /* resync/recovery still happening */
7126 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7127 goto unlock;
7128 }
7129 if (mddev->sync_thread) {
7130 /* resync has finished, collect result */
7131 md_unregister_thread(mddev->sync_thread);
7132 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007133 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7134 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 /* success...*/
7136 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007137 if (mddev->pers->spare_active(mddev))
7138 sysfs_notify(&mddev->kobj, NULL,
7139 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 }
NeilBrowncea9c222009-03-31 15:15:05 +11007141 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7142 mddev->pers->finish_reshape)
7143 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007144 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007145
7146 /* if array is no-longer degraded, then any saved_raid_disk
7147 * information must be scrapped
7148 */
7149 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007150 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007151 rdev->saved_raid_disk = -1;
7152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 mddev->recovery = 0;
7154 /* flag recovery needed just to double check */
7155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007156 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007157 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 goto unlock;
7159 }
Neil Brown72a23c22008-06-28 08:31:41 +10007160 /* Set RUNNING before clearing NEEDED to avoid
7161 * any transients in the value of "sync_action".
7162 */
7163 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7164 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007165 /* Clear some bits that don't mean anything, but
7166 * might be left set
7167 */
NeilBrown24dd4692005-11-08 21:39:26 -08007168 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7169 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007171 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7172 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 /* no recovery is running.
7174 * remove any failed drives, then
7175 * add spares if possible.
7176 * Spare are also removed and re-added, to allow
7177 * the personality to fail the re-add.
7178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007180 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007181 if (mddev->pers->check_reshape == NULL ||
7182 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007183 /* Cannot proceed */
7184 goto unlock;
7185 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007186 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007187 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007188 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7189 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007190 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007191 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007192 } else if (mddev->recovery_cp < MaxSector) {
7193 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007194 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007195 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7196 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007200 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7201 /* We are adding a device or devices to an array
7202 * which has the bitmap stored on all devices.
7203 * So make sure all bitmap pages get written
7204 */
7205 bitmap_write_all(mddev->bitmap);
7206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 mddev->sync_thread = md_register_thread(md_do_sync,
7208 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007209 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 if (!mddev->sync_thread) {
7211 printk(KERN_ERR "%s: could not start resync"
7212 " thread...\n",
7213 mdname(mddev));
7214 /* leave the spares where they are, it shouldn't hurt */
7215 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007216 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007218 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007219 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 }
7221 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007222 if (!mddev->sync_thread) {
7223 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7224 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7225 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007226 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007227 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 mddev_unlock(mddev);
7230 }
7231}
7232
Dan Williams6bfe0b42008-04-30 00:52:32 -07007233void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7234{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007235 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007236 wait_event_timeout(rdev->blocked_wait,
7237 !test_bit(Blocked, &rdev->flags),
7238 msecs_to_jiffies(5000));
7239 rdev_dec_pending(rdev, mddev);
7240}
7241EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7242
Adrian Bunk75c96f82005-05-05 16:16:09 -07007243static int md_notify_reboot(struct notifier_block *this,
7244 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245{
7246 struct list_head *tmp;
7247 mddev_t *mddev;
7248
7249 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7250
7251 printk(KERN_INFO "md: stopping all md devices.\n");
7252
NeilBrown29ac4aa2008-02-06 01:39:58 -08007253 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007254 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007255 /* Force a switch to readonly even array
7256 * appears to still be in use. Hence
7257 * the '100'.
7258 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007259 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007260 mddev_unlock(mddev);
7261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 /*
7263 * certain more exotic SCSI devices are known to be
7264 * volatile wrt too early system reboots. While the
7265 * right place to handle this issue is the given
7266 * driver, we do want to have a safe RAID driver ...
7267 */
7268 mdelay(1000*1);
7269 }
7270 return NOTIFY_DONE;
7271}
7272
Adrian Bunk75c96f82005-05-05 16:16:09 -07007273static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274 .notifier_call = md_notify_reboot,
7275 .next = NULL,
7276 .priority = INT_MAX, /* before any real devices */
7277};
7278
7279static void md_geninit(void)
7280{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7282
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007283 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284}
7285
Adrian Bunk75c96f82005-05-05 16:16:09 -07007286static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287{
Tejun Heoe804ac72010-10-15 15:36:08 +02007288 int ret = -ENOMEM;
7289
7290 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7291 if (!md_wq)
7292 goto err_wq;
7293
7294 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7295 if (!md_misc_wq)
7296 goto err_misc_wq;
7297
7298 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7299 goto err_md;
7300
7301 if ((ret = register_blkdev(0, "mdp")) < 0)
7302 goto err_mdp;
7303 mdp_major = ret;
7304
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007305 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007306 md_probe, NULL, NULL);
7307 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 md_probe, NULL, NULL);
7309
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007311 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312
7313 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315
Tejun Heoe804ac72010-10-15 15:36:08 +02007316err_mdp:
7317 unregister_blkdev(MD_MAJOR, "md");
7318err_md:
7319 destroy_workqueue(md_misc_wq);
7320err_misc_wq:
7321 destroy_workqueue(md_wq);
7322err_wq:
7323 return ret;
7324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
7326#ifndef MODULE
7327
7328/*
7329 * Searches all registered partitions for autorun RAID arrays
7330 * at boot time.
7331 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007332
7333static LIST_HEAD(all_detected_devices);
7334struct detected_devices_node {
7335 struct list_head list;
7336 dev_t dev;
7337};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
7339void md_autodetect_dev(dev_t dev)
7340{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007341 struct detected_devices_node *node_detected_dev;
7342
7343 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7344 if (node_detected_dev) {
7345 node_detected_dev->dev = dev;
7346 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7347 } else {
7348 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7349 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351}
7352
7353
7354static void autostart_arrays(int part)
7355{
7356 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007357 struct detected_devices_node *node_detected_dev;
7358 dev_t dev;
7359 int i_scanned, i_passed;
7360
7361 i_scanned = 0;
7362 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
7364 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7365
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007366 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7367 i_scanned++;
7368 node_detected_dev = list_entry(all_detected_devices.next,
7369 struct detected_devices_node, list);
7370 list_del(&node_detected_dev->list);
7371 dev = node_detected_dev->dev;
7372 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007373 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 if (IS_ERR(rdev))
7375 continue;
7376
NeilBrownb2d444d2005-11-08 21:39:31 -08007377 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 MD_BUG();
7379 continue;
7380 }
NeilBrownd0fae182008-03-04 14:29:31 -08007381 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007383 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007385
7386 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7387 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388
7389 autorun_devices(part);
7390}
7391
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007392#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394static __exit void md_exit(void)
7395{
7396 mddev_t *mddev;
7397 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007398
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007399 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007400 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007402 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 unregister_blkdev(mdp_major, "mdp");
7404 unregister_reboot_notifier(&md_notifier);
7405 unregister_sysctl_table(raid_table_header);
7406 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007407 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007409 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007411 destroy_workqueue(md_misc_wq);
7412 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413}
7414
Dan Williams685784a2007-07-09 11:56:42 -07007415subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416module_exit(md_exit)
7417
NeilBrownf91de922005-11-08 21:39:36 -08007418static int get_ro(char *buffer, struct kernel_param *kp)
7419{
7420 return sprintf(buffer, "%d", start_readonly);
7421}
7422static int set_ro(const char *val, struct kernel_param *kp)
7423{
7424 char *e;
7425 int num = simple_strtoul(val, &e, 10);
7426 if (*val && (*e == '\0' || *e == '\n')) {
7427 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007428 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007429 }
7430 return -EINVAL;
7431}
7432
NeilBrown80ca3a42006-07-10 04:44:18 -07007433module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7434module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007435
NeilBrownefeb53c2009-01-09 08:31:10 +11007436module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007437
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438EXPORT_SYMBOL(register_md_personality);
7439EXPORT_SYMBOL(unregister_md_personality);
7440EXPORT_SYMBOL(md_error);
7441EXPORT_SYMBOL(md_done_sync);
7442EXPORT_SYMBOL(md_write_start);
7443EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444EXPORT_SYMBOL(md_register_thread);
7445EXPORT_SYMBOL(md_unregister_thread);
7446EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447EXPORT_SYMBOL(md_check_recovery);
7448MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007449MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007450MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007451MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);