blob: 2a3205053b2a54e86cfaf8d7f6b68678a49f79b1 [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;
Chris Masone91ece52011-02-07 19:21:48 -0500290 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100291
NeilBrown0ca69882011-01-14 09:14:33 +1100292 if (mddev == NULL || mddev->pers == NULL
293 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100294 bio_io_error(bio);
295 return 0;
296 }
NeilBrown0ca69882011-01-14 09:14:33 +1100297 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100298 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200299 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100300 DEFINE_WAIT(__wait);
301 for (;;) {
302 prepare_to_wait(&mddev->sb_wait, &__wait,
303 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200304 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100305 break;
306 rcu_read_unlock();
307 schedule();
308 rcu_read_lock();
309 }
310 finish_wait(&mddev->sb_wait, &__wait);
311 }
312 atomic_inc(&mddev->active_io);
313 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100314
Chris Masone91ece52011-02-07 19:21:48 -0500315 /*
316 * save the sectors now since our bio can
317 * go away inside make_request
318 */
319 sectors = bio_sectors(bio);
NeilBrown21a52c62010-04-01 15:02:13 +1100320 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100321
322 cpu = part_stat_lock();
323 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500324 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100325 part_stat_unlock();
326
NeilBrown409c57f2009-03-31 14:39:39 +1100327 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
328 wake_up(&mddev->sb_wait);
329
330 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
NeilBrown9e35b992010-04-06 14:23:02 +1000333/* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
337 * unused.
338 */
NeilBrown390ee602010-06-01 19:37:27 +1000339void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100340{
341 BUG_ON(mddev->suspended);
342 mddev->suspended = 1;
343 synchronize_rcu();
344 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
345 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100346}
NeilBrown390ee602010-06-01 19:37:27 +1000347EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100348
NeilBrown390ee602010-06-01 19:37:27 +1000349void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100350{
351 mddev->suspended = 0;
352 wake_up(&mddev->sb_wait);
353 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500354
355 md_wakeup_thread(mddev->thread);
356 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100357}
NeilBrown390ee602010-06-01 19:37:27 +1000358EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100359
NeilBrown3fa841d2009-09-23 18:10:29 +1000360int mddev_congested(mddev_t *mddev, int bits)
361{
362 return mddev->suspended;
363}
364EXPORT_SYMBOL(mddev_congested);
365
NeilBrowna2826aa2009-12-14 12:49:49 +1100366/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200367 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100368 */
369
Tejun Heoe9c74692010-09-03 11:56:18 +0200370static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100371{
372 mdk_rdev_t *rdev = bio->bi_private;
373 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100374
375 rdev_dec_pending(rdev, mddev);
376
377 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200379 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100380 }
381 bio_put(bio);
382}
383
NeilBrowna7a07e62010-12-09 16:04:25 +1100384static void md_submit_flush_data(struct work_struct *ws);
385
NeilBrowna035fc32010-12-09 16:17:51 +1100386static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100387{
NeilBrowna035fc32010-12-09 16:17:51 +1100388 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 mdk_rdev_t *rdev;
390
NeilBrowna7a07e62010-12-09 16:04:25 +1100391 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
392 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 rcu_read_lock();
394 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
395 if (rdev->raid_disk >= 0 &&
396 !test_bit(Faulty, &rdev->flags)) {
397 /* Take two references, one is dropped
398 * when request finishes, one after
399 * we reclaim rcu_read_lock
400 */
401 struct bio *bi;
402 atomic_inc(&rdev->nr_pending);
403 atomic_inc(&rdev->nr_pending);
404 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100405 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 bi->bi_private = rdev;
408 bi->bi_bdev = rdev->bdev;
409 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 rcu_read_lock();
412 rdev_dec_pending(rdev, mddev);
413 }
414 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100415 if (atomic_dec_and_test(&mddev->flush_pending))
416 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100417}
418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420{
Tejun Heoe9c74692010-09-03 11:56:18 +0200421 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
422 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 /* an empty barrier - all done */
426 bio_endio(bio, 0);
427 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100429 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 }
NeilBrown2b74e122010-12-09 15:59:01 +1100432
433 mddev->flush_bio = NULL;
434 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435}
436
Tejun Heoe9c74692010-09-03 11:56:18 +0200437void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100438{
439 spin_lock_irq(&mddev->write_lock);
440 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200441 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100442 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200443 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 spin_unlock_irq(&mddev->write_lock);
445
NeilBrowna035fc32010-12-09 16:17:51 +1100446 INIT_WORK(&mddev->flush_work, submit_flushes);
447 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448}
Tejun Heoe9c74692010-09-03 11:56:18 +0200449EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100450
NeilBrown97658cd2011-04-18 18:25:42 +1000451/* Support for plugging.
452 * This mirrors the plugging support in request_queue, but does not
453 * require having a whole queue or request structures.
454 * We allocate an md_plug_cb for each md device and each thread it gets
455 * plugged on. This links tot the private plug_handle structure in the
456 * personality data where we keep a count of the number of outstanding
457 * plugs so other code can see if a plug is active.
458 */
459struct md_plug_cb {
460 struct blk_plug_cb cb;
461 mddev_t *mddev;
462};
463
464static void plugger_unplug(struct blk_plug_cb *cb)
465{
466 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
467 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
468 md_wakeup_thread(mdcb->mddev->thread);
469 kfree(mdcb);
470}
471
472/* Check that an unplug wakeup will come shortly.
473 * If not, wakeup the md thread immediately
474 */
475int mddev_check_plugged(mddev_t *mddev)
476{
477 struct blk_plug *plug = current->plug;
478 struct md_plug_cb *mdcb;
479
480 if (!plug)
481 return 0;
482
483 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
484 if (mdcb->cb.callback == plugger_unplug &&
485 mdcb->mddev == mddev) {
486 /* Already on the list, move to top */
487 if (mdcb != list_first_entry(&plug->cb_list,
488 struct md_plug_cb,
489 cb.list))
490 list_move(&mdcb->cb.list, &plug->cb_list);
491 return 1;
492 }
493 }
494 /* Not currently on the callback list */
495 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
496 if (!mdcb)
497 return 0;
498
499 mdcb->mddev = mddev;
500 mdcb->cb.callback = plugger_unplug;
501 atomic_inc(&mddev->plug_cnt);
502 list_add(&mdcb->cb.list, &plug->cb_list);
503 return 1;
504}
505EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static inline mddev_t *mddev_get(mddev_t *mddev)
508{
509 atomic_inc(&mddev->active);
510 return mddev;
511}
512
Dan Williams5fd3a172009-03-04 00:57:25 -0700513static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static void mddev_put(mddev_t *mddev)
516{
NeilBrowna167f662010-10-26 18:31:13 +1100517 struct bio_set *bs = NULL;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
520 return;
NeilBrownd3374822009-01-09 08:31:10 +1100521 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100522 mddev->ctime == 0 && !mddev->hold_active) {
523 /* Array is not configured at all, and not held active,
524 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100526 bs = mddev->bio_set;
527 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100528 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200529 /* We did a probe so need to clean up. Call
530 * queue_work inside the spinlock so that
531 * flush_workqueue() after mddev_find will
532 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100533 */
534 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200535 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100536 } else
537 kfree(mddev);
538 }
539 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100540 if (bs)
541 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
NeilBrown390ee602010-06-01 19:37:27 +1000544void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100545{
546 mutex_init(&mddev->open_mutex);
547 mutex_init(&mddev->reconfig_mutex);
548 mutex_init(&mddev->bitmap_info.mutex);
549 INIT_LIST_HEAD(&mddev->disks);
550 INIT_LIST_HEAD(&mddev->all_mddevs);
551 init_timer(&mddev->safemode_timer);
552 atomic_set(&mddev->active, 1);
553 atomic_set(&mddev->openers, 0);
554 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000555 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100556 spin_lock_init(&mddev->write_lock);
557 atomic_set(&mddev->flush_pending, 0);
558 init_waitqueue_head(&mddev->sb_wait);
559 init_waitqueue_head(&mddev->recovery_wait);
560 mddev->reshape_position = MaxSector;
561 mddev->resync_min = 0;
562 mddev->resync_max = MaxSector;
563 mddev->level = LEVEL_NONE;
564}
NeilBrown390ee602010-06-01 19:37:27 +1000565EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static mddev_t * mddev_find(dev_t unit)
568{
569 mddev_t *mddev, *new = NULL;
570
NeilBrown8f5f02c2011-02-16 13:58:51 +1100571 if (unit && MAJOR(unit) != MD_MAJOR)
572 unit &= ~((1<<MdpMinorShift)-1);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 retry:
575 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
NeilBrownefeb53c2009-01-09 08:31:10 +1100577 if (unit) {
578 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
579 if (mddev->unit == unit) {
580 mddev_get(mddev);
581 spin_unlock(&all_mddevs_lock);
582 kfree(new);
583 return mddev;
584 }
585
586 if (new) {
587 list_add(&new->all_mddevs, &all_mddevs);
588 spin_unlock(&all_mddevs_lock);
589 new->hold_active = UNTIL_IOCTL;
590 return new;
591 }
592 } else if (new) {
593 /* find an unused unit number */
594 static int next_minor = 512;
595 int start = next_minor;
596 int is_free = 0;
597 int dev = 0;
598 while (!is_free) {
599 dev = MKDEV(MD_MAJOR, next_minor);
600 next_minor++;
601 if (next_minor > MINORMASK)
602 next_minor = 0;
603 if (next_minor == start) {
604 /* Oh dear, all in use. */
605 spin_unlock(&all_mddevs_lock);
606 kfree(new);
607 return NULL;
608 }
609
610 is_free = 1;
611 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
612 if (mddev->unit == dev) {
613 is_free = 0;
614 break;
615 }
616 }
617 new->unit = dev;
618 new->md_minor = MINOR(dev);
619 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 list_add(&new->all_mddevs, &all_mddevs);
621 spin_unlock(&all_mddevs_lock);
622 return new;
623 }
624 spin_unlock(&all_mddevs_lock);
625
NeilBrown9ffae0c2006-01-06 00:20:32 -0800626 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!new)
628 return NULL;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 new->unit = unit;
631 if (MAJOR(unit) == MD_MAJOR)
632 new->md_minor = MINOR(unit);
633 else
634 new->md_minor = MINOR(unit) >> MdpMinorShift;
635
NeilBrownfafd7fb2010-04-01 15:55:30 +1100636 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto retry;
639}
640
641static inline int mddev_lock(mddev_t * mddev)
642{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800643 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Dan Williamsb522adc2009-03-31 15:00:31 +1100646static inline int mddev_is_locked(mddev_t *mddev)
647{
648 return mutex_is_locked(&mddev->reconfig_mutex);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static inline int mddev_trylock(mddev_t * mddev)
652{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800653 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
NeilBrownb6eb1272010-04-15 10:13:47 +1000656static struct attribute_group md_redundancy_group;
657
NeilBrowna64c8762010-04-14 17:15:37 +1000658static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
NeilBrowna64c8762010-04-14 17:15:37 +1000660 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000661 /* These cannot be removed under reconfig_mutex as
662 * an access to the files will try to take reconfig_mutex
663 * while holding the file unremovable, which leads to
664 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000665 * So hold set sysfs_active while the remove in happeing,
666 * and anything else which might set ->to_remove or my
667 * otherwise change the sysfs namespace will fail with
668 * -EBUSY if sysfs_active is still set.
669 * We set sysfs_active under reconfig_mutex and elsewhere
670 * test it under the same mutex to ensure its correct value
671 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000672 */
NeilBrowna64c8762010-04-14 17:15:37 +1000673 struct attribute_group *to_remove = mddev->to_remove;
674 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000675 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000676 mutex_unlock(&mddev->reconfig_mutex);
677
NeilBrown00bcb4a2010-06-01 19:37:23 +1000678 if (mddev->kobj.sd) {
679 if (to_remove != &md_redundancy_group)
680 sysfs_remove_group(&mddev->kobj, to_remove);
681 if (mddev->pers == NULL ||
682 mddev->pers->sync_request == NULL) {
683 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
684 if (mddev->sysfs_action)
685 sysfs_put(mddev->sysfs_action);
686 mddev->sysfs_action = NULL;
687 }
NeilBrowna64c8762010-04-14 17:15:37 +1000688 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000689 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000690 } else
691 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
NeilBrown005eca52005-08-22 13:11:08 -0700693 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrown2989ddb2006-01-06 00:20:43 -0800696static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100698 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100700 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (rdev->desc_nr == nr)
702 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return NULL;
705}
706
707static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 mdk_rdev_t *rdev;
710
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100711 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (rdev->bdev->bd_dev == dev)
713 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return NULL;
716}
717
NeilBrownd9d166c2006-01-06 00:20:51 -0800718static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800719{
720 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800721 list_for_each_entry(pers, &pers_list, list) {
722 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800723 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800724 if (strcmp(pers->name, clevel)==0)
725 return pers;
726 }
NeilBrown2604b702006-01-06 00:20:36 -0800727 return NULL;
728}
729
Andre Nollb73df2d2008-07-11 22:02:23 +1000730/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100731static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100733 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000734 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int alloc_disk_sb(mdk_rdev_t * rdev)
738{
739 if (rdev->sb_page)
740 MD_BUG();
741
742 rdev->sb_page = alloc_page(GFP_KERNEL);
743 if (!rdev->sb_page) {
744 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000745 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 return 0;
749}
750
751static void free_disk_sb(mdk_rdev_t * rdev)
752{
753 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800754 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 rdev->sb_loaded = 0;
756 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000757 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100758 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760}
761
762
NeilBrown6712ecf2007-09-27 12:47:43 +0200763static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700764{
765 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800766 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700768 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
769 printk("md: super_written gets error=%d, uptodate=%d\n",
770 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
771 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800772 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700773 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774
NeilBrowna9701a32005-11-08 21:39:34 -0800775 if (atomic_dec_and_test(&mddev->pending_writes))
776 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700777 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700778}
779
780void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
781 sector_t sector, int size, struct page *page)
782{
783 /* write first size bytes of page to sector of rdev
784 * Increment mddev->pending_writes before returning
785 * and decrement it on completion, waking up sb_wait
786 * if zero is reached.
787 * If an error occurred, call md_error
788 */
NeilBrowna167f662010-10-26 18:31:13 +1100789 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100791 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700792 bio->bi_sector = sector;
793 bio_add_page(bio, page, size, 0);
794 bio->bi_private = rdev;
795 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800796
NeilBrown7bfa19f2005-06-21 17:17:28 -0700797 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100798 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800799}
800
801void md_super_wait(mddev_t *mddev)
802{
Tejun Heoe9c74692010-09-03 11:56:18 +0200803 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800804 DEFINE_WAIT(wq);
805 for(;;) {
806 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
807 if (atomic_read(&mddev->pending_writes)==0)
808 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800809 schedule();
810 }
811 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700812}
813
NeilBrown6712ecf2007-09-27 12:47:43 +0200814static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
NeilBrown2b193362010-10-27 15:16:40 +1100819int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100820 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
NeilBrowna167f662010-10-26 18:31:13 +1100822 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct completion event;
824 int ret;
825
Jens Axboe721a9602011-03-09 11:56:30 +0100826 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100828 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
829 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100830 if (metadata_op)
831 bio->bi_sector = sector + rdev->sb_start;
832 else
833 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 bio_add_page(bio, page, size, 0);
835 init_completion(&event);
836 bio->bi_private = &event;
837 bio->bi_end_io = bi_complete;
838 submit_bio(rw, bio);
839 wait_for_completion(&event);
840
841 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
842 bio_put(bio);
843 return ret;
844}
NeilBrowna8745db2006-01-06 00:20:34 -0800845EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
NeilBrown0002b272005-09-09 16:23:53 -0700847static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 char b[BDEVNAME_SIZE];
850 if (!rdev->sb_page) {
851 MD_BUG();
852 return -EINVAL;
853 }
854 if (rdev->sb_loaded)
855 return 0;
856
857
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100858 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto fail;
860 rdev->sb_loaded = 1;
861 return 0;
862
863fail:
864 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
865 bdevname(rdev->bdev,b));
866 return -EINVAL;
867}
868
869static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
870{
Andre Noll05710462008-07-11 22:02:20 +1000871 return sb1->set_uuid0 == sb2->set_uuid0 &&
872 sb1->set_uuid1 == sb2->set_uuid1 &&
873 sb1->set_uuid2 == sb2->set_uuid2 &&
874 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
878{
879 int ret;
880 mdp_super_t *tmp1, *tmp2;
881
882 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
883 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
884
885 if (!tmp1 || !tmp2) {
886 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100887 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto abort;
889 }
890
891 *tmp1 = *sb1;
892 *tmp2 = *sb2;
893
894 /*
895 * nr_disks is not constant
896 */
897 tmp1->nr_disks = 0;
898 tmp2->nr_disks = 0;
899
Andre Nollce0c8e02008-07-11 22:02:20 +1000900 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700902 kfree(tmp1);
903 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return ret;
905}
906
NeilBrown4d167f02007-05-09 02:35:37 -0700907
908static u32 md_csum_fold(u32 csum)
909{
910 csum = (csum & 0xffff) + (csum >> 16);
911 return (csum & 0xffff) + (csum >> 16);
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static unsigned int calc_sb_csum(mdp_super_t * sb)
915{
NeilBrown4d167f02007-05-09 02:35:37 -0700916 u64 newcsum = 0;
917 u32 *sb32 = (u32*)sb;
918 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int disk_csum, csum;
920
921 disk_csum = sb->sb_csum;
922 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700923
924 for (i = 0; i < MD_SB_BYTES/4 ; i++)
925 newcsum += sb32[i];
926 csum = (newcsum & 0xffffffff) + (newcsum>>32);
927
928
929#ifdef CONFIG_ALPHA
930 /* This used to use csum_partial, which was wrong for several
931 * reasons including that different results are returned on
932 * different architectures. It isn't critical that we get exactly
933 * the same return value as before (we always csum_fold before
934 * testing, and that removes any differences). However as we
935 * know that csum_partial always returned a 16bit value on
936 * alphas, do a fold to maximise conformity to previous behaviour.
937 */
938 sb->sb_csum = md_csum_fold(disk_csum);
939#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return csum;
943}
944
945
946/*
947 * Handle superblock details.
948 * We want to be able to handle multiple superblock formats
949 * so we have a common interface to them all, and an array of
950 * different handlers.
951 * We rely on user-space to write the initial superblock, and support
952 * reading and updating of superblocks.
953 * Interface methods are:
954 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
955 * loads and validates a superblock on dev.
956 * if refdev != NULL, compare superblocks on both devices
957 * Return:
958 * 0 - dev has a superblock that is compatible with refdev
959 * 1 - dev has a superblock that is compatible and newer than refdev
960 * so dev should be used as the refdev in future
961 * -EINVAL superblock incompatible or invalid
962 * -othererror e.g. -EIO
963 *
964 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
965 * Verify that dev is acceptable into mddev.
966 * The first time, mddev->raid_disks will be 0, and data from
967 * dev should be merged in. Subsequent calls check that dev
968 * is new enough. Return 0 or -EINVAL
969 *
970 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
971 * Update the superblock for rdev with data in mddev
972 * This does not write to disc.
973 *
974 */
975
976struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000977 char *name;
978 struct module *owner;
979 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
980 int minor_version);
981 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
982 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
983 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000984 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985};
986
987/*
Andre Noll0894cc32009-06-18 08:49:23 +1000988 * Check that the given mddev has no bitmap.
989 *
990 * This function is called from the run method of all personalities that do not
991 * support bitmaps. It prints an error message and returns non-zero if mddev
992 * has a bitmap. Otherwise, it returns 0.
993 *
994 */
995int md_check_no_bitmap(mddev_t *mddev)
996{
NeilBrownc3d97142009-12-14 12:49:52 +1100997 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000998 return 0;
999 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1000 mdname(mddev), mddev->pers->name);
1001 return 1;
1002}
1003EXPORT_SYMBOL(md_check_no_bitmap);
1004
1005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * load_super for 0.90.0
1007 */
1008static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1009{
1010 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1011 mdp_super_t *sb;
1012 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
Andre Noll0f420352008-07-11 22:02:23 +10001015 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * it's at the end of the disk.
1017 *
1018 * It also happens to be a multiple of 4Kb.
1019 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001020 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
NeilBrown0002b272005-09-09 16:23:53 -07001022 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ret) return ret;
1024
1025 ret = -EINVAL;
1026
1027 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +10001028 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (sb->md_magic != MD_SB_MAGIC) {
1031 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1032 b);
1033 goto abort;
1034 }
1035
1036 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001037 sb->minor_version < 90 ||
1038 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1040 sb->major_version, sb->minor_version,
1041 b);
1042 goto abort;
1043 }
1044
1045 if (sb->raid_disks <= 0)
1046 goto abort;
1047
NeilBrown4d167f02007-05-09 02:35:37 -07001048 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1050 b);
1051 goto abort;
1052 }
1053
1054 rdev->preferred_minor = sb->md_minor;
1055 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001056 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (sb->level == LEVEL_MULTIPATH)
1059 rdev->desc_nr = -1;
1060 else
1061 rdev->desc_nr = sb->this_disk.number;
1062
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001063 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001065 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001067 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!uuid_equal(refsb, sb)) {
1069 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1070 b, bdevname(refdev->bdev,b2));
1071 goto abort;
1072 }
1073 if (!sb_equal(refsb, sb)) {
1074 printk(KERN_WARNING "md: %s has same UUID"
1075 " but different superblock to %s\n",
1076 b, bdevname(refdev->bdev, b2));
1077 goto abort;
1078 }
1079 ev1 = md_event(sb);
1080 ev2 = md_event(refsb);
1081 if (ev1 > ev2)
1082 ret = 1;
1083 else
1084 ret = 0;
1085 }
NeilBrown8190e752009-06-18 08:48:58 +10001086 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Andre Nolldd8ac332009-03-31 14:33:13 +11001088 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001089 /* "this cannot possibly happen" ... */
1090 ret = -EINVAL;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 abort:
1093 return ret;
1094}
1095
1096/*
1097 * validate_super for 0.90.0
1098 */
1099static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1100{
1101 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001102 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001103 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown41158c72005-06-21 17:17:25 -07001105 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001106 clear_bit(Faulty, &rdev->flags);
1107 clear_bit(In_sync, &rdev->flags);
1108 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (mddev->raid_disks == 0) {
1111 mddev->major_version = 0;
1112 mddev->minor_version = sb->minor_version;
1113 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001114 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001115 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mddev->ctime = sb->ctime;
1117 mddev->utime = sb->utime;
1118 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001119 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 mddev->layout = sb->layout;
1121 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001122 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001123 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mddev->bitmap_info.offset = 0;
1125 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
NeilBrownf6705572006-03-27 01:18:11 -08001127 if (mddev->minor_version >= 91) {
1128 mddev->reshape_position = sb->reshape_position;
1129 mddev->delta_disks = sb->delta_disks;
1130 mddev->new_level = sb->new_level;
1131 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001132 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001133 } else {
1134 mddev->reshape_position = MaxSector;
1135 mddev->delta_disks = 0;
1136 mddev->new_level = mddev->level;
1137 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001138 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (sb->state & (1<<MD_SB_CLEAN))
1142 mddev->recovery_cp = MaxSector;
1143 else {
1144 if (sb->events_hi == sb->cp_events_hi &&
1145 sb->events_lo == sb->cp_events_lo) {
1146 mddev->recovery_cp = sb->recovery_cp;
1147 } else
1148 mddev->recovery_cp = 0;
1149 }
1150
1151 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1152 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1153 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1154 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1155
1156 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001157
1158 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001159 mddev->bitmap_info.file == NULL)
1160 mddev->bitmap_info.offset =
1161 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001162
NeilBrown41158c72005-06-21 17:17:25 -07001163 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001164 /* Insist on good event counter while assembling, except
1165 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001167 if (sb->disks[rdev->desc_nr].state & (
1168 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1169 if (ev1 < mddev->events)
1170 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001171 } else if (mddev->bitmap) {
1172 /* if adding to array with a bitmap, then we can accept an
1173 * older device ... but not too old.
1174 */
NeilBrown41158c72005-06-21 17:17:25 -07001175 if (ev1 < mddev->bitmap->events_cleared)
1176 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001177 } else {
1178 if (ev1 < mddev->events)
1179 /* just a hot-add of a new device, leave raid_disk at -1 */
1180 return 0;
1181 }
NeilBrown41158c72005-06-21 17:17:25 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 desc = sb->disks + rdev->desc_nr;
1185
1186 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001188 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1189 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001192 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1193 /* active but not in sync implies recovery up to
1194 * reshape position. We don't know exactly where
1195 * that is, so set to zero for now */
1196 if (mddev->minor_version >= 91) {
1197 rdev->recovery_offset = 0;
1198 rdev->raid_disk = desc->raid_disk;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001201 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1202 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001203 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001204 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
1208/*
1209 * sync_super for 0.90.0
1210 */
1211static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1212{
1213 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 mdk_rdev_t *rdev2;
1215 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /* make rdev->sb match mddev data..
1219 *
1220 * 1/ zero out disks
1221 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1222 * 3/ any empty disks < next_spare become removed
1223 *
1224 * disks[0] gets initialised to REMOVED because
1225 * we cannot be sure from other fields if it has
1226 * been initialised or not.
1227 */
1228 int i;
1229 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1230
NeilBrown61181562005-09-09 16:24:02 -07001231 rdev->sb_size = MD_SB_BYTES;
1232
Namhyung Kim65a06f02011-07-27 11:00:36 +10001233 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 memset(sb, 0, sizeof(*sb));
1236
1237 sb->md_magic = MD_SB_MAGIC;
1238 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sb->patch_version = mddev->patch_version;
1240 sb->gvalid_words = 0; /* ignored */
1241 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1242 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1243 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1244 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1245
1246 sb->ctime = mddev->ctime;
1247 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001248 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->raid_disks = mddev->raid_disks;
1250 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001251 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sb->utime = mddev->utime;
1253 sb->state = 0;
1254 sb->events_hi = (mddev->events>>32);
1255 sb->events_lo = (u32)mddev->events;
1256
NeilBrownf6705572006-03-27 01:18:11 -08001257 if (mddev->reshape_position == MaxSector)
1258 sb->minor_version = 90;
1259 else {
1260 sb->minor_version = 91;
1261 sb->reshape_position = mddev->reshape_position;
1262 sb->new_level = mddev->new_level;
1263 sb->delta_disks = mddev->delta_disks;
1264 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001265 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001266 }
1267 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (mddev->in_sync)
1269 {
1270 sb->recovery_cp = mddev->recovery_cp;
1271 sb->cp_events_hi = (mddev->events>>32);
1272 sb->cp_events_lo = (u32)mddev->events;
1273 if (mddev->recovery_cp == MaxSector)
1274 sb->state = (1<< MD_SB_CLEAN);
1275 } else
1276 sb->recovery_cp = 0;
1277
1278 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001279 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
NeilBrownc3d97142009-12-14 12:49:52 +11001281 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001282 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001285 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001287 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001288 int is_active = test_bit(In_sync, &rdev2->flags);
1289
1290 if (rdev2->raid_disk >= 0 &&
1291 sb->minor_version >= 91)
1292 /* we have nowhere to store the recovery_offset,
1293 * but if it is not below the reshape_position,
1294 * we can piggy-back on that.
1295 */
1296 is_active = 1;
1297 if (rdev2->raid_disk < 0 ||
1298 test_bit(Faulty, &rdev2->flags))
1299 is_active = 0;
1300 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001301 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001303 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001304 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 d = &sb->disks[rdev2->desc_nr];
1306 nr_disks++;
1307 d->number = rdev2->desc_nr;
1308 d->major = MAJOR(rdev2->bdev->bd_dev);
1309 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001310 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 d->raid_disk = rdev2->raid_disk;
1312 else
1313 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001314 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001316 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001318 if (test_bit(In_sync, &rdev2->flags))
1319 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 active++;
1321 working++;
1322 } else {
1323 d->state = 0;
1324 spare++;
1325 working++;
1326 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001327 if (test_bit(WriteMostly, &rdev2->flags))
1328 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* now set the "removed" and "faulty" bits on any missing devices */
1331 for (i=0 ; i < mddev->raid_disks ; i++) {
1332 mdp_disk_t *d = &sb->disks[i];
1333 if (d->state == 0 && d->number == 0) {
1334 d->number = i;
1335 d->raid_disk = i;
1336 d->state = (1<<MD_DISK_REMOVED);
1337 d->state |= (1<<MD_DISK_FAULTY);
1338 failed++;
1339 }
1340 }
1341 sb->nr_disks = nr_disks;
1342 sb->active_disks = active;
1343 sb->working_disks = working;
1344 sb->failed_disks = failed;
1345 sb->spare_disks = spare;
1346
1347 sb->this_disk = sb->disks[rdev->desc_nr];
1348 sb->sb_csum = calc_sb_csum(sb);
1349}
1350
1351/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352 * rdev_size_change for 0.90.0
1353 */
1354static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001355super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001356{
Andre Noll58c0fed2009-03-31 14:33:13 +11001357 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001358 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001359 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001360 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001361 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001362 if (!num_sectors || num_sectors > rdev->sb_start)
1363 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365 rdev->sb_page);
1366 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001367 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001368}
1369
1370
1371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * version 1 superblock
1373 */
1374
NeilBrown1c05b4b2006-10-21 10:24:08 -07001375static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001377 __le32 disk_csum;
1378 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned long long newcsum;
1380 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001381 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int i;
1383
1384 disk_csum = sb->sb_csum;
1385 sb->sb_csum = 0;
1386 newcsum = 0;
1387 for (i=0; size>=4; size -= 4 )
1388 newcsum += le32_to_cpu(*isuper++);
1389
1390 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001391 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1394 sb->sb_csum = disk_csum;
1395 return cpu_to_le32(csum);
1396}
1397
1398static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1399{
1400 struct mdp_superblock_1 *sb;
1401 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001402 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001404 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
Andre Noll0f420352008-07-11 22:02:23 +10001407 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1413 */
1414 switch(minor_version) {
1415 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001416 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001417 sb_start -= 8*2;
1418 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001421 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001424 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 default:
1427 return -EINVAL;
1428 }
Andre Noll0f420352008-07-11 22:02:23 +10001429 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
NeilBrown0002b272005-09-09 16:23:53 -07001431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1433 */
1434 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (ret) return ret;
1436
1437
Namhyung Kim65a06f02011-07-27 11:00:36 +10001438 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1441 sb->major_version != cpu_to_le32(1) ||
1442 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001443 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001444 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EINVAL;
1446
1447 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1448 printk("md: invalid superblock checksum on %s\n",
1449 bdevname(rdev->bdev,b));
1450 return -EINVAL;
1451 }
1452 if (le64_to_cpu(sb->data_size) < 10) {
1453 printk("md: data_size too small on %s\n",
1454 bdevname(rdev->bdev,b));
1455 return -EINVAL;
1456 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 rdev->preferred_minor = 0xffff;
1459 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001460 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
NeilBrown0002b272005-09-09 16:23:53 -07001462 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001463 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001464 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001465 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1466
1467 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001468 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001469 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001470
NeilBrown31b65a02006-07-10 04:44:14 -07001471 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1472 rdev->desc_nr = -1;
1473 else
1474 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1475
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001476 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001477 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001478 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001480 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1483 sb->level != refsb->level ||
1484 sb->layout != refsb->layout ||
1485 sb->chunksize != refsb->chunksize) {
1486 printk(KERN_WARNING "md: %s has strangely different"
1487 " superblock to %s\n",
1488 bdevname(rdev->bdev,b),
1489 bdevname(refdev->bdev,b2));
1490 return -EINVAL;
1491 }
1492 ev1 = le64_to_cpu(sb->events);
1493 ev2 = le64_to_cpu(refsb->events);
1494
1495 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001496 ret = 1;
1497 else
1498 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
NeilBrowna1801f82008-03-04 14:29:31 -08001500 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001501 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001502 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001504 rdev->sectors = rdev->sb_start;
1505 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001507 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001508 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001509 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1514{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001515 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001516 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
NeilBrown41158c72005-06-21 17:17:25 -07001518 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001519 clear_bit(Faulty, &rdev->flags);
1520 clear_bit(In_sync, &rdev->flags);
1521 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (mddev->raid_disks == 0) {
1524 mddev->major_version = 1;
1525 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001526 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001527 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1529 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1530 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001531 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 mddev->layout = le32_to_cpu(sb->layout);
1533 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001534 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001535 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001536 mddev->bitmap_info.offset = 0;
1537 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1540 memcpy(mddev->uuid, sb->set_uuid, 16);
1541
1542 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001543
NeilBrown71c08052005-09-09 16:23:51 -07001544 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001545 mddev->bitmap_info.file == NULL )
1546 mddev->bitmap_info.offset =
1547 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001548
NeilBrownf6705572006-03-27 01:18:11 -08001549 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1550 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1551 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1552 mddev->new_level = le32_to_cpu(sb->new_level);
1553 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001554 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001555 } else {
1556 mddev->reshape_position = MaxSector;
1557 mddev->delta_disks = 0;
1558 mddev->new_level = mddev->level;
1559 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001560 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001561 }
1562
NeilBrown41158c72005-06-21 17:17:25 -07001563 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001564 /* Insist of good event counter while assembling, except for
1565 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001567 if (rdev->desc_nr >= 0 &&
1568 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1569 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1570 if (ev1 < mddev->events)
1571 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001572 } else if (mddev->bitmap) {
1573 /* If adding to array with a bitmap, then we can accept an
1574 * older device, but not too old.
1575 */
NeilBrown41158c72005-06-21 17:17:25 -07001576 if (ev1 < mddev->bitmap->events_cleared)
1577 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001578 } else {
1579 if (ev1 < mddev->events)
1580 /* just a hot-add of a new device, leave raid_disk at -1 */
1581 return 0;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (mddev->level != LEVEL_MULTIPATH) {
1584 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001585 if (rdev->desc_nr < 0 ||
1586 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1587 role = 0xffff;
1588 rdev->desc_nr = -1;
1589 } else
1590 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 switch(role) {
1592 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 break;
1594 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001595 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001598 if ((le32_to_cpu(sb->feature_map) &
1599 MD_FEATURE_RECOVERY_OFFSET))
1600 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1601 else
1602 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 rdev->raid_disk = role;
1604 break;
1605 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001606 if (sb->devflags & WriteMostly1)
1607 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001608 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001609 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
1612}
1613
1614static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1615{
1616 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 mdk_rdev_t *rdev2;
1618 int max_dev, i;
1619 /* make rdev->sb match mddev and rdev data. */
1620
Namhyung Kim65a06f02011-07-27 11:00:36 +10001621 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 sb->feature_map = 0;
1624 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001625 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 memset(sb->pad1, 0, sizeof(sb->pad1));
1627 memset(sb->pad2, 0, sizeof(sb->pad2));
1628 memset(sb->pad3, 0, sizeof(sb->pad3));
1629
1630 sb->utime = cpu_to_le64((__u64)mddev->utime);
1631 sb->events = cpu_to_le64(mddev->events);
1632 if (mddev->in_sync)
1633 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1634 else
1635 sb->resync_offset = cpu_to_le64(0);
1636
NeilBrown1c05b4b2006-10-21 10:24:08 -07001637 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001638
NeilBrownf0ca3402006-02-02 14:28:04 -08001639 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001640 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001641 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001642 sb->level = cpu_to_le32(mddev->level);
1643 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001644
NeilBrownc3d97142009-12-14 12:49:52 +11001645 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1646 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001647 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001648 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001649
1650 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001651 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001652 sb->feature_map |=
1653 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1654 sb->recovery_offset =
1655 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 }
1657
NeilBrownf6705572006-03-27 01:18:11 -08001658 if (mddev->reshape_position != MaxSector) {
1659 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1660 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1661 sb->new_layout = cpu_to_le32(mddev->new_layout);
1662 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1663 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001664 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001665 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001668 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (rdev2->desc_nr+1 > max_dev)
1670 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001671
NeilBrown70471da2009-08-03 10:59:57 +10001672 if (max_dev > le32_to_cpu(sb->max_dev)) {
1673 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001674 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001675 rdev->sb_size = max_dev * 2 + 256;
1676 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1677 if (rdev->sb_size & bmask)
1678 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001679 } else
1680 max_dev = le32_to_cpu(sb->max_dev);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 for (i=0; i<max_dev;i++)
1683 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1684
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001685 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001687 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001689 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001691 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001692 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 else
1694 sb->dev_roles[i] = cpu_to_le16(0xffff);
1695 }
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 sb->sb_csum = calc_sb_1_csum(sb);
1698}
1699
Chris Webb0cd17fe2008-06-28 08:31:46 +10001700static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001701super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001702{
1703 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001704 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001705 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001706 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001707 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001708 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001709 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001710 max_sectors -= rdev->data_offset;
1711 if (!num_sectors || num_sectors > max_sectors)
1712 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001713 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001714 /* minor version 0 with bitmap we can't move */
1715 return 0;
1716 } else {
1717 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001718 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001719 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001720 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001721 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001722 if (!num_sectors || num_sectors > max_sectors)
1723 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001724 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001725 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001726 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001727 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001728 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001729 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001730 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001731 rdev->sb_page);
1732 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001733 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001734}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Adrian Bunk75c96f82005-05-05 16:16:09 -07001736static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 [0] = {
1738 .name = "0.90.0",
1739 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001740 .load_super = super_90_load,
1741 .validate_super = super_90_validate,
1742 .sync_super = super_90_sync,
1743 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 },
1745 [1] = {
1746 .name = "md-1",
1747 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001748 .load_super = super_1_load,
1749 .validate_super = super_1_validate,
1750 .sync_super = super_1_sync,
1751 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 },
1753};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jonathan Brassow076f9682011-06-07 17:51:30 -05001755static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1756{
1757 if (mddev->sync_super) {
1758 mddev->sync_super(mddev, rdev);
1759 return;
1760 }
1761
1762 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1763
1764 super_types[mddev->major_version].sync_super(mddev, rdev);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1768{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001769 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
NeilBrown4b809912008-07-21 17:05:25 +10001771 rcu_read_lock();
1772 rdev_for_each_rcu(rdev, mddev1)
1773 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001774 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001775 rdev2->bdev->bd_contains) {
1776 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001777 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001778 }
1779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781}
1782
1783static LIST_HEAD(pending_raid_disks);
1784
Andre Nollac5e7112009-08-03 10:59:47 +10001785/*
1786 * Try to register data integrity profile for an mddev
1787 *
1788 * This is called when an array is started and after a disk has been kicked
1789 * from the array. It only succeeds if all working and active component devices
1790 * are integrity capable with matching profiles.
1791 */
1792int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001793{
Andre Nollac5e7112009-08-03 10:59:47 +10001794 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001795
Andre Nollac5e7112009-08-03 10:59:47 +10001796 if (list_empty(&mddev->disks))
1797 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001798 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1799 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001800 list_for_each_entry(rdev, &mddev->disks, same_set) {
1801 /* skip spares and non-functional disks */
1802 if (test_bit(Faulty, &rdev->flags))
1803 continue;
1804 if (rdev->raid_disk < 0)
1805 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001806 if (!reference) {
1807 /* Use the first rdev as the reference */
1808 reference = rdev;
1809 continue;
1810 }
1811 /* does this rdev's profile match the reference profile? */
1812 if (blk_integrity_compare(reference->bdev->bd_disk,
1813 rdev->bdev->bd_disk) < 0)
1814 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001815 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001816 if (!reference || !bdev_get_integrity(reference->bdev))
1817 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001818 /*
1819 * All component devices are integrity capable and have matching
1820 * profiles, register the common profile for the md device.
1821 */
1822 if (blk_integrity_register(mddev->gendisk,
1823 bdev_get_integrity(reference->bdev)) != 0) {
1824 printk(KERN_ERR "md: failed to register integrity for %s\n",
1825 mdname(mddev));
1826 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001827 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001828 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1829 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1830 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1831 mdname(mddev));
1832 return -EINVAL;
1833 }
Andre Nollac5e7112009-08-03 10:59:47 +10001834 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001835}
Andre Nollac5e7112009-08-03 10:59:47 +10001836EXPORT_SYMBOL(md_integrity_register);
1837
1838/* Disable data integrity if non-capable/non-matching disk is being added */
1839void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1840{
1841 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1842 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1843
1844 if (!bi_mddev) /* nothing to do */
1845 return;
1846 if (rdev->raid_disk < 0) /* skip spares */
1847 return;
1848 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1849 rdev->bdev->bd_disk) >= 0)
1850 return;
1851 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1852 blk_integrity_unregister(mddev->gendisk);
1853}
1854EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1857{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001858 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001859 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001860 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001861 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (rdev->mddev) {
1864 MD_BUG();
1865 return -EINVAL;
1866 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001867
1868 /* prevent duplicates */
1869 if (find_rdev(mddev, rdev->bdev->bd_dev))
1870 return -EEXIST;
1871
Andre Nolldd8ac332009-03-31 14:33:13 +11001872 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1873 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1874 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001875 if (mddev->pers) {
1876 /* Cannot change size, so fail
1877 * If mddev->level <= 0, then we don't care
1878 * about aligning sizes (e.g. linear)
1879 */
1880 if (mddev->level > 0)
1881 return -ENOSPC;
1882 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001883 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* Verify rdev->desc_nr is unique.
1887 * If it is -1, assign a free number, else
1888 * check number is not in use
1889 */
1890 if (rdev->desc_nr < 0) {
1891 int choice = 0;
1892 if (mddev->pers) choice = mddev->raid_disks;
1893 while (find_rdev_nr(mddev, choice))
1894 choice++;
1895 rdev->desc_nr = choice;
1896 } else {
1897 if (find_rdev_nr(mddev, rdev->desc_nr))
1898 return -EBUSY;
1899 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001900 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1901 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1902 mdname(mddev), mddev->max_disks);
1903 return -EBUSY;
1904 }
NeilBrown19133a42005-11-08 21:39:35 -08001905 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001906 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001907 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001910 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001911
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001912 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001913 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001914
Tejun Heo0762b8b2008-08-25 19:56:12 +09001915 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001916 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1917 /* failure here is OK */;
1918 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001919
NeilBrown4b809912008-07-21 17:05:25 +10001920 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001921 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001922
1923 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10001924 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001927
1928 fail:
1929 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1930 b, mdname(mddev));
1931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
NeilBrown177a99b2008-02-06 01:39:56 -08001934static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001935{
1936 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1937 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001938 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1942{
1943 char b[BDEVNAME_SIZE];
1944 if (!rdev->mddev) {
1945 MD_BUG();
1946 return;
1947 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001948 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001949 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1951 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001952 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001953 sysfs_put(rdev->sysfs_state);
1954 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001955 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001956 * writing to 'remove' to "dev/state". We also need
1957 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001958 */
NeilBrown4b809912008-07-21 17:05:25 +10001959 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001960 INIT_WORK(&rdev->del_work, md_delayed_delete);
1961 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001962 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965/*
1966 * prevent the device from being mounted, repartitioned or
1967 * otherwise reused by a RAID array (or any other kernel
1968 * subsystem), by bd_claiming the device.
1969 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001970static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 int err = 0;
1973 struct block_device *bdev;
1974 char b[BDEVNAME_SIZE];
1975
Tejun Heod4d77622010-11-13 11:55:18 +01001976 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1977 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (IS_ERR(bdev)) {
1979 printk(KERN_ERR "md: could not open %s.\n",
1980 __bdevname(dev, b));
1981 return PTR_ERR(bdev);
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rdev->bdev = bdev;
1984 return err;
1985}
1986
1987static void unlock_rdev(mdk_rdev_t *rdev)
1988{
1989 struct block_device *bdev = rdev->bdev;
1990 rdev->bdev = NULL;
1991 if (!bdev)
1992 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001993 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
1996void md_autodetect_dev(dev_t dev);
1997
1998static void export_rdev(mdk_rdev_t * rdev)
1999{
2000 char b[BDEVNAME_SIZE];
2001 printk(KERN_INFO "md: export_rdev(%s)\n",
2002 bdevname(rdev->bdev,b));
2003 if (rdev->mddev)
2004 MD_BUG();
2005 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002007 if (test_bit(AutoDetected, &rdev->flags))
2008 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009#endif
2010 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002011 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014static void kick_rdev_from_array(mdk_rdev_t * rdev)
2015{
2016 unbind_rdev_from_array(rdev);
2017 export_rdev(rdev);
2018}
2019
2020static void export_array(mddev_t *mddev)
2021{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002022 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
NeilBrownd089c6a2008-02-06 01:39:59 -08002024 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (!rdev->mddev) {
2026 MD_BUG();
2027 continue;
2028 }
2029 kick_rdev_from_array(rdev);
2030 }
2031 if (!list_empty(&mddev->disks))
2032 MD_BUG();
2033 mddev->raid_disks = 0;
2034 mddev->major_version = 0;
2035}
2036
2037static void print_desc(mdp_disk_t *desc)
2038{
2039 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2040 desc->major,desc->minor,desc->raid_disk,desc->state);
2041}
2042
Cheng Renquancd2ac932009-01-09 08:31:08 +11002043static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 int i;
2046
2047 printk(KERN_INFO
2048 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2049 sb->major_version, sb->minor_version, sb->patch_version,
2050 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2051 sb->ctime);
2052 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2053 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2054 sb->md_minor, sb->layout, sb->chunk_size);
2055 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2056 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2057 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2058 sb->failed_disks, sb->spare_disks,
2059 sb->sb_csum, (unsigned long)sb->events_lo);
2060
2061 printk(KERN_INFO);
2062 for (i = 0; i < MD_SB_DISKS; i++) {
2063 mdp_disk_t *desc;
2064
2065 desc = sb->disks + i;
2066 if (desc->number || desc->major || desc->minor ||
2067 desc->raid_disk || (desc->state && (desc->state != 4))) {
2068 printk(" D %2d: ", i);
2069 print_desc(desc);
2070 }
2071 }
2072 printk(KERN_INFO "md: THIS: ");
2073 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Cheng Renquancd2ac932009-01-09 08:31:08 +11002076static void print_sb_1(struct mdp_superblock_1 *sb)
2077{
2078 __u8 *uuid;
2079
2080 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002081 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002082 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002083 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002084 le32_to_cpu(sb->major_version),
2085 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002086 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002087 sb->set_name,
2088 (unsigned long long)le64_to_cpu(sb->ctime)
2089 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2090
2091 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002092 printk(KERN_INFO
2093 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002094 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002095 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002096 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2097 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002098 le32_to_cpu(sb->level),
2099 (unsigned long long)le64_to_cpu(sb->size),
2100 le32_to_cpu(sb->raid_disks),
2101 le32_to_cpu(sb->layout),
2102 le32_to_cpu(sb->chunksize),
2103 (unsigned long long)le64_to_cpu(sb->data_offset),
2104 (unsigned long long)le64_to_cpu(sb->data_size),
2105 (unsigned long long)le64_to_cpu(sb->super_offset),
2106 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2107 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002108 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002109 sb->devflags,
2110 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2111 (unsigned long long)le64_to_cpu(sb->events),
2112 (unsigned long long)le64_to_cpu(sb->resync_offset),
2113 le32_to_cpu(sb->sb_csum),
2114 le32_to_cpu(sb->max_dev)
2115 );
2116}
2117
2118static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002121 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2122 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002123 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2124 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002126 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2127 switch (major_version) {
2128 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002129 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002130 break;
2131 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002132 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002133 break;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 } else
2136 printk(KERN_INFO "md: no rdev superblock!\n");
2137}
2138
Adrian Bunk5e563412006-06-26 00:27:42 -07002139static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002141 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 mdk_rdev_t *rdev;
2143 mddev_t *mddev;
2144 char b[BDEVNAME_SIZE];
2145
2146 printk("\n");
2147 printk("md: **********************************\n");
2148 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2149 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002150 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
NeilBrown32a76272005-06-21 17:17:14 -07002152 if (mddev->bitmap)
2153 bitmap_print_sb(mddev->bitmap);
2154 else
2155 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002156 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 printk("<%s>", bdevname(rdev->bdev,b));
2158 printk("\n");
2159
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002160 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002161 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 printk("md: **********************************\n");
2164 printk("\n");
2165}
2166
2167
NeilBrown42543762006-06-26 00:27:57 -07002168static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
NeilBrown42543762006-06-26 00:27:57 -07002170 /* Update each superblock (in-memory image), but
2171 * if we are allowed to, skip spares which already
2172 * have the right event counter, or have one earlier
2173 * (which would mean they aren't being marked as dirty
2174 * with the rest of the array)
2175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002177 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002178 if (rdev->sb_events == mddev->events ||
2179 (nospares &&
2180 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002181 rdev->sb_events+1 == mddev->events)) {
2182 /* Don't update this superblock */
2183 rdev->sb_loaded = 2;
2184 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002185 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002186 rdev->sb_loaded = 1;
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189}
2190
NeilBrown850b2b42006-10-03 01:15:46 -07002191static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002194 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002195 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002198 /* First make sure individual recovery_offsets are correct */
2199 list_for_each_entry(rdev, &mddev->disks, same_set) {
2200 if (rdev->raid_disk >= 0 &&
2201 mddev->delta_disks >= 0 &&
2202 !test_bit(In_sync, &rdev->flags) &&
2203 mddev->curr_resync_completed > rdev->recovery_offset)
2204 rdev->recovery_offset = mddev->curr_resync_completed;
2205
2206 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002207 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002208 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002209 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002210 if (!mddev->external)
2211 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002212 wake_up(&mddev->sb_wait);
2213 return;
2214 }
2215
NeilBrowna9701a32005-11-08 21:39:34 -08002216 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002217
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002218 mddev->utime = get_seconds();
2219
NeilBrown850b2b42006-10-03 01:15:46 -07002220 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2221 force_change = 1;
2222 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2223 /* just a clean<-> dirty transition, possibly leave spares alone,
2224 * though if events isn't the right even/odd, we will have to do
2225 * spares after all
2226 */
2227 nospares = 1;
2228 if (force_change)
2229 nospares = 0;
2230 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002231 /* If the array is degraded, then skipping spares is both
2232 * dangerous and fairly pointless.
2233 * Dangerous because a device that was removed from the array
2234 * might have a event_count that still looks up-to-date,
2235 * so it can be re-added without a resync.
2236 * Pointless because if there are any spares to skip,
2237 * then a recovery will happen and soon that array won't
2238 * be degraded any more and the spare can go back to sleep then.
2239 */
NeilBrown850b2b42006-10-03 01:15:46 -07002240 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002241
NeilBrown06d91a52005-06-21 17:17:12 -07002242 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002243
2244 /* If this is just a dirty<->clean transition, and the array is clean
2245 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002246 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002247 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002248 && mddev->can_decrease_events
2249 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002250 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002251 mddev->can_decrease_events = 0;
2252 } else {
NeilBrown42543762006-06-26 00:27:57 -07002253 /* otherwise we have to go forward and ... */
2254 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002255 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (!mddev->events) {
2259 /*
2260 * oops, this 64-bit counter should never wrap.
2261 * Either we are in around ~1 trillion A.C., assuming
2262 * 1 reboot per second, or we have a bug:
2263 */
2264 MD_BUG();
2265 mddev->events --;
2266 }
NeilBrowne6910632008-02-06 01:39:51 -08002267 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002268 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 dprintk(KERN_INFO
2271 "md: updating %s RAID superblock on device (in sync %d)\n",
2272 mdname(mddev),mddev->in_sync);
2273
NeilBrown4ad13662007-07-17 04:06:13 -07002274 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002275 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 char b[BDEVNAME_SIZE];
2277 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002278 if (rdev->sb_loaded != 1)
2279 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002280 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 dprintk("(skipping faulty ");
2282
2283 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002284 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002285 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002286 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002287 rdev->sb_page);
2288 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2289 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002290 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002291 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 } else
2294 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002295 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* only need to write one superblock... */
2297 break;
2298 }
NeilBrowna9701a32005-11-08 21:39:34 -08002299 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002300 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002301
NeilBrowna9701a32005-11-08 21:39:34 -08002302 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002303 if (mddev->in_sync != sync_req ||
2304 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002305 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002306 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002307 goto repeat;
2308 }
NeilBrown850b2b42006-10-03 01:15:46 -07002309 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002310 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002311 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002312 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2313 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Andre Noll7f6ce762008-03-23 18:34:54 +01002317/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002318 * We want to accept with case. For this we use cmd_match.
2319 */
2320static int cmd_match(const char *cmd, const char *str)
2321{
2322 /* See if cmd, written into a sysfs file, matches
2323 * str. They must either be the same, or cmd can
2324 * have a trailing newline
2325 */
2326 while (*cmd && *str && *cmd == *str) {
2327 cmd++;
2328 str++;
2329 }
2330 if (*cmd == '\n')
2331 cmd++;
2332 if (*str || *cmd)
2333 return 0;
2334 return 1;
2335}
2336
NeilBrown86e6ffd2005-11-08 21:39:24 -08002337struct rdev_sysfs_entry {
2338 struct attribute attr;
2339 ssize_t (*show)(mdk_rdev_t *, char *);
2340 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2341};
2342
2343static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002344state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002345{
2346 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002347 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002348
NeilBrownb2d444d2005-11-08 21:39:31 -08002349 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002350 len+= sprintf(page+len, "%sfaulty",sep);
2351 sep = ",";
2352 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002353 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002354 len += sprintf(page+len, "%sin_sync",sep);
2355 sep = ",";
2356 }
NeilBrownf6556752006-06-26 00:28:01 -07002357 if (test_bit(WriteMostly, &rdev->flags)) {
2358 len += sprintf(page+len, "%swrite_mostly",sep);
2359 sep = ",";
2360 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002361 if (test_bit(Blocked, &rdev->flags)) {
2362 len += sprintf(page+len, "%sblocked", sep);
2363 sep = ",";
2364 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002365 if (!test_bit(Faulty, &rdev->flags) &&
2366 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002367 len += sprintf(page+len, "%sspare", sep);
2368 sep = ",";
2369 }
2370 return len+sprintf(page+len, "\n");
2371}
2372
NeilBrown45dc2de2006-06-26 00:27:58 -07002373static ssize_t
2374state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2375{
2376 /* can write
2377 * faulty - simulates and error
2378 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002379 * writemostly - sets write_mostly
2380 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002381 * blocked - sets the Blocked flag
2382 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002383 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002384 */
2385 int err = -EINVAL;
2386 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2387 md_error(rdev->mddev, rdev);
2388 err = 0;
2389 } else if (cmd_match(buf, "remove")) {
2390 if (rdev->raid_disk >= 0)
2391 err = -EBUSY;
2392 else {
2393 mddev_t *mddev = rdev->mddev;
2394 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002395 if (mddev->pers)
2396 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002397 md_new_event(mddev);
2398 err = 0;
2399 }
NeilBrownf6556752006-06-26 00:28:01 -07002400 } else if (cmd_match(buf, "writemostly")) {
2401 set_bit(WriteMostly, &rdev->flags);
2402 err = 0;
2403 } else if (cmd_match(buf, "-writemostly")) {
2404 clear_bit(WriteMostly, &rdev->flags);
2405 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002406 } else if (cmd_match(buf, "blocked")) {
2407 set_bit(Blocked, &rdev->flags);
2408 err = 0;
2409 } else if (cmd_match(buf, "-blocked")) {
2410 clear_bit(Blocked, &rdev->flags);
2411 wake_up(&rdev->blocked_wait);
2412 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2413 md_wakeup_thread(rdev->mddev->thread);
2414
2415 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002416 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2417 set_bit(In_sync, &rdev->flags);
2418 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002419 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002420 if (!err)
2421 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002422 return err ? err : len;
2423}
NeilBrown80ca3a42006-07-10 04:44:18 -07002424static struct rdev_sysfs_entry rdev_state =
2425__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002426
2427static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002428errors_show(mdk_rdev_t *rdev, char *page)
2429{
2430 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2431}
2432
2433static ssize_t
2434errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2435{
2436 char *e;
2437 unsigned long n = simple_strtoul(buf, &e, 10);
2438 if (*buf && (*e == 0 || *e == '\n')) {
2439 atomic_set(&rdev->corrected_errors, n);
2440 return len;
2441 }
2442 return -EINVAL;
2443}
2444static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002445__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002446
NeilBrown014236d2006-01-06 00:20:55 -08002447static ssize_t
2448slot_show(mdk_rdev_t *rdev, char *page)
2449{
2450 if (rdev->raid_disk < 0)
2451 return sprintf(page, "none\n");
2452 else
2453 return sprintf(page, "%d\n", rdev->raid_disk);
2454}
2455
2456static ssize_t
2457slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2458{
2459 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002460 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002461 int slot = simple_strtoul(buf, &e, 10);
2462 if (strncmp(buf, "none", 4)==0)
2463 slot = -1;
2464 else if (e==buf || (*e && *e!= '\n'))
2465 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002466 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002467 /* Setting 'slot' on an active array requires also
2468 * updating the 'rd%d' link, and communicating
2469 * with the personality with ->hot_*_disk.
2470 * For now we only support removing
2471 * failed/spare devices. This normally happens automatically,
2472 * but not when the metadata is externally managed.
2473 */
NeilBrownc303da62008-02-06 01:39:51 -08002474 if (rdev->raid_disk == -1)
2475 return -EEXIST;
2476 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002477 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002478 return -EINVAL;
2479 err = rdev->mddev->pers->
2480 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2481 if (err)
2482 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002483 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002484 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002485 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2486 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002487 } else if (rdev->mddev->pers) {
2488 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002489 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002490 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002491 */
2492
2493 if (rdev->raid_disk != -1)
2494 return -EBUSY;
2495
NeilBrownc6751b22011-02-02 11:57:13 +11002496 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2497 return -EBUSY;
2498
Neil Brown6c2fce22008-06-28 08:31:31 +10002499 if (rdev->mddev->pers->hot_add_disk == NULL)
2500 return -EINVAL;
2501
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002502 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002503 if (rdev2->raid_disk == slot)
2504 return -EEXIST;
2505
NeilBrownba1b41b2011-01-14 09:14:34 +11002506 if (slot >= rdev->mddev->raid_disks &&
2507 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2508 return -ENOSPC;
2509
Neil Brown6c2fce22008-06-28 08:31:31 +10002510 rdev->raid_disk = slot;
2511 if (test_bit(In_sync, &rdev->flags))
2512 rdev->saved_raid_disk = slot;
2513 else
2514 rdev->saved_raid_disk = -1;
2515 err = rdev->mddev->pers->
2516 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002517 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002518 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002519 return err;
Neil Brown52664732008-06-28 08:31:44 +10002520 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002521 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002522 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002523 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002524 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002525 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002526 if (slot >= rdev->mddev->raid_disks &&
2527 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002528 return -ENOSPC;
2529 rdev->raid_disk = slot;
2530 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531 clear_bit(Faulty, &rdev->flags);
2532 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002533 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002534 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002535 }
NeilBrown014236d2006-01-06 00:20:55 -08002536 return len;
2537}
2538
2539
2540static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002541__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002542
NeilBrown93c8cad2006-01-06 00:20:56 -08002543static ssize_t
2544offset_show(mdk_rdev_t *rdev, char *page)
2545{
Andrew Morton6961ece2006-01-06 00:20:59 -08002546 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002547}
2548
2549static ssize_t
2550offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2551{
2552 char *e;
2553 unsigned long long offset = simple_strtoull(buf, &e, 10);
2554 if (e==buf || (*e && *e != '\n'))
2555 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002556 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002557 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002558 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 /* Must set offset before size, so overlap checks
2560 * can be sane */
2561 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002562 rdev->data_offset = offset;
2563 return len;
2564}
2565
2566static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002567__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002568
NeilBrown83303b62006-01-06 00:21:06 -08002569static ssize_t
2570rdev_size_show(mdk_rdev_t *rdev, char *page)
2571{
Andre Nolldd8ac332009-03-31 14:33:13 +11002572 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002573}
2574
NeilBrownc5d79ad2008-02-06 01:39:54 -08002575static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2576{
2577 /* check if two start/length pairs overlap */
2578 if (s1+l1 <= s2)
2579 return 0;
2580 if (s2+l2 <= s1)
2581 return 0;
2582 return 1;
2583}
2584
Dan Williamsb522adc2009-03-31 15:00:31 +11002585static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2586{
2587 unsigned long long blocks;
2588 sector_t new;
2589
2590 if (strict_strtoull(buf, 10, &blocks) < 0)
2591 return -EINVAL;
2592
2593 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2594 return -EINVAL; /* sector conversion overflow */
2595
2596 new = blocks * 2;
2597 if (new != blocks * 2)
2598 return -EINVAL; /* unsigned long long to sector_t overflow */
2599
2600 *sectors = new;
2601 return 0;
2602}
2603
NeilBrown83303b62006-01-06 00:21:06 -08002604static ssize_t
2605rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2606{
NeilBrown27c529b2008-03-04 14:29:33 -08002607 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002608 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002609 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002610
Dan Williamsb522adc2009-03-31 15:00:31 +11002611 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002612 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002613 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002614 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002615 sectors = super_types[my_mddev->major_version].
2616 rdev_size_change(rdev, sectors);
2617 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002618 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002619 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002620 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002621 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002622 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002623 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002624 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002625
Andre Nolldd8ac332009-03-31 14:33:13 +11002626 rdev->sectors = sectors;
2627 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002628 /* need to check that all other rdevs with the same ->bdev
2629 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002630 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002631 * we have to change it back, we will have the lock again.
2632 */
2633 mddev_t *mddev;
2634 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002635 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002636
NeilBrown27c529b2008-03-04 14:29:33 -08002637 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002638 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002639 mdk_rdev_t *rdev2;
2640
2641 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002642 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002643 if (rdev->bdev == rdev2->bdev &&
2644 rdev != rdev2 &&
2645 overlaps(rdev->data_offset, rdev->sectors,
2646 rdev2->data_offset,
2647 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002648 overlap = 1;
2649 break;
2650 }
2651 mddev_unlock(mddev);
2652 if (overlap) {
2653 mddev_put(mddev);
2654 break;
2655 }
2656 }
NeilBrown27c529b2008-03-04 14:29:33 -08002657 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002658 if (overlap) {
2659 /* Someone else could have slipped in a size
2660 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002661 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002662 * safe, and trust userspace not to race with
2663 * itself
2664 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002665 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002666 return -EBUSY;
2667 }
2668 }
NeilBrown83303b62006-01-06 00:21:06 -08002669 return len;
2670}
2671
2672static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002673__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002674
Dan Williams06e3c812009-12-12 21:17:12 -07002675
2676static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2677{
2678 unsigned long long recovery_start = rdev->recovery_offset;
2679
2680 if (test_bit(In_sync, &rdev->flags) ||
2681 recovery_start == MaxSector)
2682 return sprintf(page, "none\n");
2683
2684 return sprintf(page, "%llu\n", recovery_start);
2685}
2686
2687static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2688{
2689 unsigned long long recovery_start;
2690
2691 if (cmd_match(buf, "none"))
2692 recovery_start = MaxSector;
2693 else if (strict_strtoull(buf, 10, &recovery_start))
2694 return -EINVAL;
2695
2696 if (rdev->mddev->pers &&
2697 rdev->raid_disk >= 0)
2698 return -EBUSY;
2699
2700 rdev->recovery_offset = recovery_start;
2701 if (recovery_start == MaxSector)
2702 set_bit(In_sync, &rdev->flags);
2703 else
2704 clear_bit(In_sync, &rdev->flags);
2705 return len;
2706}
2707
2708static struct rdev_sysfs_entry rdev_recovery_start =
2709__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2710
NeilBrown86e6ffd2005-11-08 21:39:24 -08002711static struct attribute *rdev_default_attrs[] = {
2712 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002713 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002714 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002715 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002716 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002717 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002718 NULL,
2719};
2720static ssize_t
2721rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2722{
2723 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2724 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002725 mddev_t *mddev = rdev->mddev;
2726 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002727
2728 if (!entry->show)
2729 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002730
2731 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2732 if (!rv) {
2733 if (rdev->mddev == NULL)
2734 rv = -EBUSY;
2735 else
2736 rv = entry->show(rdev, page);
2737 mddev_unlock(mddev);
2738 }
2739 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002740}
2741
2742static ssize_t
2743rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2744 const char *page, size_t length)
2745{
2746 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2747 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002748 ssize_t rv;
2749 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002750
2751 if (!entry->store)
2752 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002753 if (!capable(CAP_SYS_ADMIN))
2754 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002755 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002756 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002757 if (rdev->mddev == NULL)
2758 rv = -EBUSY;
2759 else
2760 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002761 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002762 }
2763 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002764}
2765
2766static void rdev_free(struct kobject *ko)
2767{
2768 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2769 kfree(rdev);
2770}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002771static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002772 .show = rdev_attr_show,
2773 .store = rdev_attr_store,
2774};
2775static struct kobj_type rdev_ktype = {
2776 .release = rdev_free,
2777 .sysfs_ops = &rdev_sysfs_ops,
2778 .default_attrs = rdev_default_attrs,
2779};
2780
NeilBrowne8bb9a82010-06-01 19:37:26 +10002781void md_rdev_init(mdk_rdev_t *rdev)
2782{
2783 rdev->desc_nr = -1;
2784 rdev->saved_raid_disk = -1;
2785 rdev->raid_disk = -1;
2786 rdev->flags = 0;
2787 rdev->data_offset = 0;
2788 rdev->sb_events = 0;
2789 rdev->last_read_error.tv_sec = 0;
2790 rdev->last_read_error.tv_nsec = 0;
2791 atomic_set(&rdev->nr_pending, 0);
2792 atomic_set(&rdev->read_errors, 0);
2793 atomic_set(&rdev->corrected_errors, 0);
2794
2795 INIT_LIST_HEAD(&rdev->same_set);
2796 init_waitqueue_head(&rdev->blocked_wait);
2797}
2798EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799/*
2800 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2801 *
2802 * mark the device faulty if:
2803 *
2804 * - the device is nonexistent (zero size)
2805 * - the device has no valid superblock
2806 *
2807 * a faulty rdev _never_ has rdev->sb set.
2808 */
2809static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2810{
2811 char b[BDEVNAME_SIZE];
2812 int err;
2813 mdk_rdev_t *rdev;
2814 sector_t size;
2815
NeilBrown9ffae0c2006-01-06 00:20:32 -08002816 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (!rdev) {
2818 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2819 return ERR_PTR(-ENOMEM);
2820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
NeilBrowne8bb9a82010-06-01 19:37:26 +10002822 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if ((err = alloc_disk_sb(rdev)))
2824 goto abort_free;
2825
NeilBrownc5d79ad2008-02-06 01:39:54 -08002826 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (err)
2828 goto abort_free;
2829
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002830 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002831
Mike Snitzer77304d22010-11-08 14:39:12 +01002832 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (!size) {
2834 printk(KERN_WARNING
2835 "md: %s has zero or unknown size, marking faulty!\n",
2836 bdevname(rdev->bdev,b));
2837 err = -EINVAL;
2838 goto abort_free;
2839 }
2840
2841 if (super_format >= 0) {
2842 err = super_types[super_format].
2843 load_super(rdev, NULL, super_minor);
2844 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002845 printk(KERN_WARNING
2846 "md: %s does not have a valid v%d.%d "
2847 "superblock, not importing!\n",
2848 bdevname(rdev->bdev,b),
2849 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 goto abort_free;
2851 }
2852 if (err < 0) {
2853 printk(KERN_WARNING
2854 "md: could not read %s's sb, not importing!\n",
2855 bdevname(rdev->bdev,b));
2856 goto abort_free;
2857 }
2858 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return rdev;
2861
2862abort_free:
2863 if (rdev->sb_page) {
2864 if (rdev->bdev)
2865 unlock_rdev(rdev);
2866 free_disk_sb(rdev);
2867 }
2868 kfree(rdev);
2869 return ERR_PTR(err);
2870}
2871
2872/*
2873 * Check a full RAID array for plausibility
2874 */
2875
2876
NeilBrowna757e642005-04-16 15:26:42 -07002877static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
2879 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002880 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 char b[BDEVNAME_SIZE];
2882
2883 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002884 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 switch (super_types[mddev->major_version].
2886 load_super(rdev, freshest, mddev->minor_version)) {
2887 case 1:
2888 freshest = rdev;
2889 break;
2890 case 0:
2891 break;
2892 default:
2893 printk( KERN_ERR \
2894 "md: fatal superblock inconsistency in %s"
2895 " -- removing from array\n",
2896 bdevname(rdev->bdev,b));
2897 kick_rdev_from_array(rdev);
2898 }
2899
2900
2901 super_types[mddev->major_version].
2902 validate_super(mddev, freshest);
2903
2904 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002905 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002906 if (mddev->max_disks &&
2907 (rdev->desc_nr >= mddev->max_disks ||
2908 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002909 printk(KERN_WARNING
2910 "md: %s: %s: only %d devices permitted\n",
2911 mdname(mddev), bdevname(rdev->bdev, b),
2912 mddev->max_disks);
2913 kick_rdev_from_array(rdev);
2914 continue;
2915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (rdev != freshest)
2917 if (super_types[mddev->major_version].
2918 validate_super(mddev, rdev)) {
2919 printk(KERN_WARNING "md: kicking non-fresh %s"
2920 " from array!\n",
2921 bdevname(rdev->bdev,b));
2922 kick_rdev_from_array(rdev);
2923 continue;
2924 }
2925 if (mddev->level == LEVEL_MULTIPATH) {
2926 rdev->desc_nr = i++;
2927 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002928 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002929 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002930 rdev->raid_disk = -1;
2931 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
NeilBrown72e02072009-12-14 12:49:55 +11002936/* Read a fixed-point number.
2937 * Numbers in sysfs attributes should be in "standard" units where
2938 * possible, so time should be in seconds.
2939 * However we internally use a a much smaller unit such as
2940 * milliseconds or jiffies.
2941 * This function takes a decimal number with a possible fractional
2942 * component, and produces an integer which is the result of
2943 * multiplying that number by 10^'scale'.
2944 * all without any floating-point arithmetic.
2945 */
2946int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2947{
2948 unsigned long result = 0;
2949 long decimals = -1;
2950 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2951 if (*cp == '.')
2952 decimals = 0;
2953 else if (decimals < scale) {
2954 unsigned int value;
2955 value = *cp - '0';
2956 result = result * 10 + value;
2957 if (decimals >= 0)
2958 decimals++;
2959 }
2960 cp++;
2961 }
2962 if (*cp == '\n')
2963 cp++;
2964 if (*cp)
2965 return -EINVAL;
2966 if (decimals < 0)
2967 decimals = 0;
2968 while (decimals < scale) {
2969 result *= 10;
2970 decimals ++;
2971 }
2972 *res = result;
2973 return 0;
2974}
2975
2976
NeilBrown19052c02008-08-05 15:54:13 +10002977static void md_safemode_timeout(unsigned long data);
2978
NeilBrowneae17012005-11-08 21:39:23 -08002979static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002980safe_delay_show(mddev_t *mddev, char *page)
2981{
2982 int msec = (mddev->safemode_delay*1000)/HZ;
2983 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2984}
2985static ssize_t
2986safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2987{
NeilBrown16f17b32006-06-26 00:27:37 -07002988 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002989
NeilBrown72e02072009-12-14 12:49:55 +11002990 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002991 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002992 if (msec == 0)
2993 mddev->safemode_delay = 0;
2994 else {
NeilBrown19052c02008-08-05 15:54:13 +10002995 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002996 mddev->safemode_delay = (msec*HZ)/1000;
2997 if (mddev->safemode_delay == 0)
2998 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002999 if (mddev->safemode_delay < old_delay)
3000 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003001 }
3002 return len;
3003}
3004static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003005__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003006
3007static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003008level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003009{
NeilBrown2604b702006-01-06 00:20:36 -08003010 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003011 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003012 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003013 else if (mddev->clevel[0])
3014 return sprintf(page, "%s\n", mddev->clevel);
3015 else if (mddev->level != LEVEL_NONE)
3016 return sprintf(page, "%d\n", mddev->level);
3017 else
3018 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003019}
3020
NeilBrownd9d166c2006-01-06 00:20:51 -08003021static ssize_t
3022level_store(mddev_t *mddev, const char *buf, size_t len)
3023{
Dan Williamsf2859af2010-05-02 10:04:16 -07003024 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003025 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003026 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003027 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003028 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003029 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003030
3031 if (mddev->pers == NULL) {
3032 if (len == 0)
3033 return 0;
3034 if (len >= sizeof(mddev->clevel))
3035 return -ENOSPC;
3036 strncpy(mddev->clevel, buf, len);
3037 if (mddev->clevel[len-1] == '\n')
3038 len--;
3039 mddev->clevel[len] = 0;
3040 mddev->level = LEVEL_NONE;
3041 return rv;
3042 }
3043
3044 /* request to change the personality. Need to ensure:
3045 * - array is not engaged in resync/recovery/reshape
3046 * - old personality can be suspended
3047 * - new personality will access other array.
3048 */
3049
NeilBrownbb4f1e92010-08-08 21:18:03 +10003050 if (mddev->sync_thread ||
3051 mddev->reshape_position != MaxSector ||
3052 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003053 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003054
3055 if (!mddev->pers->quiesce) {
3056 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3057 mdname(mddev), mddev->pers->name);
3058 return -EINVAL;
3059 }
3060
3061 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003062 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003063 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003064 strncpy(clevel, buf, len);
3065 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003066 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003067 clevel[len] = 0;
3068 if (strict_strtol(clevel, 10, &level))
3069 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003070
Dan Williamsf2859af2010-05-02 10:04:16 -07003071 if (request_module("md-%s", clevel) != 0)
3072 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003073 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003074 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003075 if (!pers || !try_module_get(pers->owner)) {
3076 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003077 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003078 return -EINVAL;
3079 }
3080 spin_unlock(&pers_lock);
3081
3082 if (pers == mddev->pers) {
3083 /* Nothing to do! */
3084 module_put(pers->owner);
3085 return rv;
3086 }
3087 if (!pers->takeover) {
3088 module_put(pers->owner);
3089 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003090 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003091 return -EINVAL;
3092 }
3093
NeilBrowne93f68a2010-06-15 09:36:03 +01003094 list_for_each_entry(rdev, &mddev->disks, same_set)
3095 rdev->new_raid_disk = rdev->raid_disk;
3096
NeilBrown245f46c2009-03-31 14:39:39 +11003097 /* ->takeover must set new_* and/or delta_disks
3098 * if it succeeds, and may set them when it fails.
3099 */
3100 priv = pers->takeover(mddev);
3101 if (IS_ERR(priv)) {
3102 mddev->new_level = mddev->level;
3103 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003104 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003105 mddev->raid_disks -= mddev->delta_disks;
3106 mddev->delta_disks = 0;
3107 module_put(pers->owner);
3108 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003109 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003110 return PTR_ERR(priv);
3111 }
3112
3113 /* Looks like we have a winner */
3114 mddev_suspend(mddev);
3115 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003116
3117 if (mddev->pers->sync_request == NULL &&
3118 pers->sync_request != NULL) {
3119 /* need to add the md_redundancy_group */
3120 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3121 printk(KERN_WARNING
3122 "md: cannot register extra attributes for %s\n",
3123 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003124 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003125 }
3126 if (mddev->pers->sync_request != NULL &&
3127 pers->sync_request == NULL) {
3128 /* need to remove the md_redundancy_group */
3129 if (mddev->to_remove == NULL)
3130 mddev->to_remove = &md_redundancy_group;
3131 }
3132
Trela Maciej54071b32010-03-08 16:02:42 +11003133 if (mddev->pers->sync_request == NULL &&
3134 mddev->external) {
3135 /* We are converting from a no-redundancy array
3136 * to a redundancy array and metadata is managed
3137 * externally so we need to be sure that writes
3138 * won't block due to a need to transition
3139 * clean->dirty
3140 * until external management is started.
3141 */
3142 mddev->in_sync = 0;
3143 mddev->safemode_delay = 0;
3144 mddev->safemode = 0;
3145 }
3146
NeilBrowne93f68a2010-06-15 09:36:03 +01003147 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003148 if (rdev->raid_disk < 0)
3149 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003150 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003151 rdev->new_raid_disk = -1;
3152 if (rdev->new_raid_disk == rdev->raid_disk)
3153 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003154 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003155 }
3156 list_for_each_entry(rdev, &mddev->disks, same_set) {
3157 if (rdev->raid_disk < 0)
3158 continue;
3159 if (rdev->new_raid_disk == rdev->raid_disk)
3160 continue;
3161 rdev->raid_disk = rdev->new_raid_disk;
3162 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003163 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003164 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003165 if (sysfs_link_rdev(mddev, rdev))
3166 printk(KERN_WARNING "md: cannot register rd%d"
3167 " for %s after level change\n",
3168 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003169 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003170 }
3171
3172 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003173 mddev->pers = pers;
3174 mddev->private = priv;
3175 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3176 mddev->level = mddev->new_level;
3177 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003178 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003179 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003180 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003181 if (mddev->pers->sync_request == NULL) {
3182 /* this is now an array without redundancy, so
3183 * it must always be in_sync
3184 */
3185 mddev->in_sync = 1;
3186 del_timer_sync(&mddev->safemode_timer);
3187 }
NeilBrown245f46c2009-03-31 14:39:39 +11003188 pers->run(mddev);
3189 mddev_resume(mddev);
3190 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3192 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003193 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003194 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003195 return rv;
3196}
3197
3198static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003199__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003200
NeilBrownd4dbd022006-06-26 00:27:59 -07003201
3202static ssize_t
3203layout_show(mddev_t *mddev, char *page)
3204{
3205 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003206 if (mddev->reshape_position != MaxSector &&
3207 mddev->layout != mddev->new_layout)
3208 return sprintf(page, "%d (%d)\n",
3209 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003210 return sprintf(page, "%d\n", mddev->layout);
3211}
3212
3213static ssize_t
3214layout_store(mddev_t *mddev, const char *buf, size_t len)
3215{
3216 char *e;
3217 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003218
3219 if (!*buf || (*e && *e != '\n'))
3220 return -EINVAL;
3221
NeilBrownb3546032009-03-31 14:56:41 +11003222 if (mddev->pers) {
3223 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003224 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003225 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003226 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003227 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003228 if (err) {
3229 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003230 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003231 }
NeilBrownb3546032009-03-31 14:56:41 +11003232 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003233 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003234 if (mddev->reshape_position == MaxSector)
3235 mddev->layout = n;
3236 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003237 return len;
3238}
3239static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003240__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003241
3242
NeilBrowneae17012005-11-08 21:39:23 -08003243static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003244raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003245{
NeilBrownbb636542005-11-08 21:39:45 -08003246 if (mddev->raid_disks == 0)
3247 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003248 if (mddev->reshape_position != MaxSector &&
3249 mddev->delta_disks != 0)
3250 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3251 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003252 return sprintf(page, "%d\n", mddev->raid_disks);
3253}
3254
NeilBrownda943b92006-01-06 00:20:54 -08003255static int update_raid_disks(mddev_t *mddev, int raid_disks);
3256
3257static ssize_t
3258raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3259{
NeilBrownda943b92006-01-06 00:20:54 -08003260 char *e;
3261 int rv = 0;
3262 unsigned long n = simple_strtoul(buf, &e, 10);
3263
3264 if (!*buf || (*e && *e != '\n'))
3265 return -EINVAL;
3266
3267 if (mddev->pers)
3268 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003269 else if (mddev->reshape_position != MaxSector) {
3270 int olddisks = mddev->raid_disks - mddev->delta_disks;
3271 mddev->delta_disks = n - olddisks;
3272 mddev->raid_disks = n;
3273 } else
NeilBrownda943b92006-01-06 00:20:54 -08003274 mddev->raid_disks = n;
3275 return rv ? rv : len;
3276}
3277static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003278__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003279
NeilBrown24dd4692005-11-08 21:39:26 -08003280static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003281chunk_size_show(mddev_t *mddev, char *page)
3282{
NeilBrown08a02ec2007-05-09 02:35:38 -07003283 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003284 mddev->chunk_sectors != mddev->new_chunk_sectors)
3285 return sprintf(page, "%d (%d)\n",
3286 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003287 mddev->chunk_sectors << 9);
3288 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003289}
3290
3291static ssize_t
3292chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3293{
NeilBrown3b343802006-01-06 00:20:47 -08003294 char *e;
3295 unsigned long n = simple_strtoul(buf, &e, 10);
3296
NeilBrown3b343802006-01-06 00:20:47 -08003297 if (!*buf || (*e && *e != '\n'))
3298 return -EINVAL;
3299
NeilBrownb3546032009-03-31 14:56:41 +11003300 if (mddev->pers) {
3301 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003302 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003303 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003304 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003305 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003306 if (err) {
3307 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003308 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003309 }
NeilBrownb3546032009-03-31 14:56:41 +11003310 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003311 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003312 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003313 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003314 }
NeilBrown3b343802006-01-06 00:20:47 -08003315 return len;
3316}
3317static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003318__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003319
NeilBrowna94213b2006-06-26 00:28:00 -07003320static ssize_t
3321resync_start_show(mddev_t *mddev, char *page)
3322{
NeilBrownd1a7c502009-03-31 15:24:32 +11003323 if (mddev->recovery_cp == MaxSector)
3324 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003325 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3326}
3327
3328static ssize_t
3329resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3330{
NeilBrowna94213b2006-06-26 00:28:00 -07003331 char *e;
3332 unsigned long long n = simple_strtoull(buf, &e, 10);
3333
NeilBrownb0986362011-05-11 15:52:21 +10003334 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003335 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003336 if (cmd_match(buf, "none"))
3337 n = MaxSector;
3338 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003339 return -EINVAL;
3340
3341 mddev->recovery_cp = n;
3342 return len;
3343}
3344static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003345__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003346
NeilBrown9e653b62006-06-26 00:27:58 -07003347/*
3348 * The array state can be:
3349 *
3350 * clear
3351 * No devices, no size, no level
3352 * Equivalent to STOP_ARRAY ioctl
3353 * inactive
3354 * May have some settings, but array is not active
3355 * all IO results in error
3356 * When written, doesn't tear down array, but just stops it
3357 * suspended (not supported yet)
3358 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003359 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003360 * readonly
3361 * no resync can happen. no superblocks get written.
3362 * write requests fail
3363 * read-auto
3364 * like readonly, but behaves like 'clean' on a write request.
3365 *
3366 * clean - no pending writes, but otherwise active.
3367 * When written to inactive array, starts without resync
3368 * If a write request arrives then
3369 * if metadata is known, mark 'dirty' and switch to 'active'.
3370 * if not known, block and switch to write-pending
3371 * If written to an active array that has pending writes, then fails.
3372 * active
3373 * fully active: IO and resync can be happening.
3374 * When written to inactive array, starts with resync
3375 *
3376 * write-pending
3377 * clean, but writes are blocked waiting for 'active' to be written.
3378 *
3379 * active-idle
3380 * like active, but no writes have been seen for a while (100msec).
3381 *
3382 */
3383enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3384 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003385static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003386 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3387 "write-pending", "active-idle", NULL };
3388
3389static int match_word(const char *word, char **list)
3390{
3391 int n;
3392 for (n=0; list[n]; n++)
3393 if (cmd_match(word, list[n]))
3394 break;
3395 return n;
3396}
3397
3398static ssize_t
3399array_state_show(mddev_t *mddev, char *page)
3400{
3401 enum array_state st = inactive;
3402
3403 if (mddev->pers)
3404 switch(mddev->ro) {
3405 case 1:
3406 st = readonly;
3407 break;
3408 case 2:
3409 st = read_auto;
3410 break;
3411 case 0:
3412 if (mddev->in_sync)
3413 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003414 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003415 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003416 else if (mddev->safemode)
3417 st = active_idle;
3418 else
3419 st = active;
3420 }
3421 else {
3422 if (list_empty(&mddev->disks) &&
3423 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003424 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003425 st = clear;
3426 else
3427 st = inactive;
3428 }
3429 return sprintf(page, "%s\n", array_states[st]);
3430}
3431
Neil Browndf5b20c2008-07-11 22:02:22 +10003432static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003433static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003434static int do_md_run(mddev_t * mddev);
3435static int restart_array(mddev_t *mddev);
3436
3437static ssize_t
3438array_state_store(mddev_t *mddev, const char *buf, size_t len)
3439{
3440 int err = -EINVAL;
3441 enum array_state st = match_word(buf, array_states);
3442 switch(st) {
3443 case bad_word:
3444 break;
3445 case clear:
3446 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003447 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003448 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003449 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003450 break;
3451 case inactive:
3452 /* stopping an active array */
3453 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003454 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003455 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003456 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003457 } else
3458 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003459 break;
3460 case suspended:
3461 break; /* not supported yet */
3462 case readonly:
3463 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003464 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003465 else {
3466 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003467 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003468 err = do_md_run(mddev);
3469 }
3470 break;
3471 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003472 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003473 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003474 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003475 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003476 err = restart_array(mddev);
3477 if (err == 0) {
3478 mddev->ro = 2;
3479 set_disk_ro(mddev->gendisk, 0);
3480 }
NeilBrown9e653b62006-06-26 00:27:58 -07003481 } else {
3482 mddev->ro = 2;
3483 err = do_md_run(mddev);
3484 }
3485 break;
3486 case clean:
3487 if (mddev->pers) {
3488 restart_array(mddev);
3489 spin_lock_irq(&mddev->write_lock);
3490 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003491 if (mddev->in_sync == 0) {
3492 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003493 if (mddev->safemode == 1)
3494 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003495 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003496 }
3497 err = 0;
3498 } else
3499 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003500 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003501 } else
3502 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003503 break;
3504 case active:
3505 if (mddev->pers) {
3506 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003507 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003508 wake_up(&mddev->sb_wait);
3509 err = 0;
3510 } else {
3511 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003512 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003513 err = do_md_run(mddev);
3514 }
3515 break;
3516 case write_pending:
3517 case active_idle:
3518 /* these cannot be set */
3519 break;
3520 }
3521 if (err)
3522 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003523 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003524 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003525 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003526 }
NeilBrown9e653b62006-06-26 00:27:58 -07003527}
NeilBrown80ca3a42006-07-10 04:44:18 -07003528static struct md_sysfs_entry md_array_state =
3529__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003530
NeilBrown6d7ff732006-01-06 00:21:16 -08003531static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003532max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3533 return sprintf(page, "%d\n",
3534 atomic_read(&mddev->max_corr_read_errors));
3535}
3536
3537static ssize_t
3538max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3539{
3540 char *e;
3541 unsigned long n = simple_strtoul(buf, &e, 10);
3542
3543 if (*buf && (*e == 0 || *e == '\n')) {
3544 atomic_set(&mddev->max_corr_read_errors, n);
3545 return len;
3546 }
3547 return -EINVAL;
3548}
3549
3550static struct md_sysfs_entry max_corr_read_errors =
3551__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3552 max_corrected_read_errors_store);
3553
3554static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003555null_show(mddev_t *mddev, char *page)
3556{
3557 return -EINVAL;
3558}
3559
3560static ssize_t
3561new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3562{
3563 /* buf must be %d:%d\n? giving major and minor numbers */
3564 /* The new device is added to the array.
3565 * If the array has a persistent superblock, we read the
3566 * superblock to initialise info and check validity.
3567 * Otherwise, only checking done is that in bind_rdev_to_array,
3568 * which mainly checks size.
3569 */
3570 char *e;
3571 int major = simple_strtoul(buf, &e, 10);
3572 int minor;
3573 dev_t dev;
3574 mdk_rdev_t *rdev;
3575 int err;
3576
3577 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3578 return -EINVAL;
3579 minor = simple_strtoul(e+1, &e, 10);
3580 if (*e && *e != '\n')
3581 return -EINVAL;
3582 dev = MKDEV(major, minor);
3583 if (major != MAJOR(dev) ||
3584 minor != MINOR(dev))
3585 return -EOVERFLOW;
3586
3587
3588 if (mddev->persistent) {
3589 rdev = md_import_device(dev, mddev->major_version,
3590 mddev->minor_version);
3591 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3592 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3593 mdk_rdev_t, same_set);
3594 err = super_types[mddev->major_version]
3595 .load_super(rdev, rdev0, mddev->minor_version);
3596 if (err < 0)
3597 goto out;
3598 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003599 } else if (mddev->external)
3600 rdev = md_import_device(dev, -2, -1);
3601 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003602 rdev = md_import_device(dev, -1, -1);
3603
3604 if (IS_ERR(rdev))
3605 return PTR_ERR(rdev);
3606 err = bind_rdev_to_array(rdev, mddev);
3607 out:
3608 if (err)
3609 export_rdev(rdev);
3610 return err ? err : len;
3611}
3612
3613static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003614__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003615
3616static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003617bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3618{
3619 char *end;
3620 unsigned long chunk, end_chunk;
3621
3622 if (!mddev->bitmap)
3623 goto out;
3624 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3625 while (*buf) {
3626 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3627 if (buf == end) break;
3628 if (*end == '-') { /* range */
3629 buf = end + 1;
3630 end_chunk = simple_strtoul(buf, &end, 0);
3631 if (buf == end) break;
3632 }
3633 if (*end && !isspace(*end)) break;
3634 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003635 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003636 }
3637 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3638out:
3639 return len;
3640}
3641
3642static struct md_sysfs_entry md_bitmap =
3643__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3644
3645static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003646size_show(mddev_t *mddev, char *page)
3647{
Andre Noll58c0fed2009-03-31 14:33:13 +11003648 return sprintf(page, "%llu\n",
3649 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003650}
3651
Andre Nolld71f9f82008-07-11 22:02:22 +10003652static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003653
3654static ssize_t
3655size_store(mddev_t *mddev, const char *buf, size_t len)
3656{
3657 /* If array is inactive, we can reduce the component size, but
3658 * not increase it (except from 0).
3659 * If array is active, we can try an on-line resize
3660 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003661 sector_t sectors;
3662 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003663
Andre Noll58c0fed2009-03-31 14:33:13 +11003664 if (err < 0)
3665 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003666 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003667 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003668 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003669 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003670 if (mddev->dev_sectors == 0 ||
3671 mddev->dev_sectors > sectors)
3672 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003673 else
3674 err = -ENOSPC;
3675 }
3676 return err ? err : len;
3677}
3678
3679static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003680__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003681
NeilBrown8bb93aa2006-01-06 00:20:50 -08003682
3683/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003684 * This is one of
3685 * 'none' for arrays with no metadata (good luck...)
3686 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003687 * or N.M for internally known formats
3688 */
3689static ssize_t
3690metadata_show(mddev_t *mddev, char *page)
3691{
3692 if (mddev->persistent)
3693 return sprintf(page, "%d.%d\n",
3694 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003695 else if (mddev->external)
3696 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003697 else
3698 return sprintf(page, "none\n");
3699}
3700
3701static ssize_t
3702metadata_store(mddev_t *mddev, const char *buf, size_t len)
3703{
3704 int major, minor;
3705 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003706 /* Changing the details of 'external' metadata is
3707 * always permitted. Otherwise there must be
3708 * no devices attached to the array.
3709 */
3710 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3711 ;
3712 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003713 return -EBUSY;
3714
3715 if (cmd_match(buf, "none")) {
3716 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003717 mddev->external = 0;
3718 mddev->major_version = 0;
3719 mddev->minor_version = 90;
3720 return len;
3721 }
3722 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003723 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003724 if (namelen >= sizeof(mddev->metadata_type))
3725 namelen = sizeof(mddev->metadata_type)-1;
3726 strncpy(mddev->metadata_type, buf+9, namelen);
3727 mddev->metadata_type[namelen] = 0;
3728 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3729 mddev->metadata_type[--namelen] = 0;
3730 mddev->persistent = 0;
3731 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003732 mddev->major_version = 0;
3733 mddev->minor_version = 90;
3734 return len;
3735 }
3736 major = simple_strtoul(buf, &e, 10);
3737 if (e==buf || *e != '.')
3738 return -EINVAL;
3739 buf = e+1;
3740 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003741 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003742 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003743 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003744 return -ENOENT;
3745 mddev->major_version = major;
3746 mddev->minor_version = minor;
3747 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003748 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003749 return len;
3750}
3751
3752static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003753__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003754
NeilBrowna35b0d62006-01-06 00:20:49 -08003755static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003756action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003757{
NeilBrown7eec3142005-11-08 21:39:44 -08003758 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003759 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3760 type = "frozen";
3761 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003762 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003763 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3764 type = "reshape";
3765 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003766 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3767 type = "resync";
3768 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3769 type = "check";
3770 else
3771 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003772 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003773 type = "recover";
3774 }
3775 return sprintf(page, "%s\n", type);
3776}
3777
NeilBrown7ebc0be2011-01-14 09:14:33 +11003778static void reap_sync_thread(mddev_t *mddev);
3779
NeilBrown24dd4692005-11-08 21:39:26 -08003780static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003781action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003782{
NeilBrown7eec3142005-11-08 21:39:44 -08003783 if (!mddev->pers || !mddev->pers->sync_request)
3784 return -EINVAL;
3785
NeilBrownb6a9ce62009-05-26 09:41:17 +10003786 if (cmd_match(page, "frozen"))
3787 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3788 else
3789 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3790
3791 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003792 if (mddev->sync_thread) {
3793 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003794 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003795 }
NeilBrown03c902e2006-01-06 00:20:46 -08003796 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3797 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003798 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003799 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003801 else if (cmd_match(page, "recover")) {
3802 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3803 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3804 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003805 int err;
3806 if (mddev->pers->start_reshape == NULL)
3807 return -EINVAL;
3808 err = mddev->pers->start_reshape(mddev);
3809 if (err)
3810 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003811 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003812 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003813 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003814 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003815 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003816 return -EINVAL;
3817 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3818 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003819 }
NeilBrown03c902e2006-01-06 00:20:46 -08003820 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003821 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003822 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003823 return len;
3824}
3825
NeilBrown9d888832005-11-08 21:39:26 -08003826static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003827mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003828{
3829 return sprintf(page, "%llu\n",
3830 (unsigned long long) mddev->resync_mismatches);
3831}
3832
NeilBrown80ca3a42006-07-10 04:44:18 -07003833static struct md_sysfs_entry md_scan_mode =
3834__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003835
NeilBrown96de1e62005-11-08 21:39:39 -08003836
NeilBrown80ca3a42006-07-10 04:44:18 -07003837static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003838
NeilBrown88202a02006-01-06 00:21:36 -08003839static ssize_t
3840sync_min_show(mddev_t *mddev, char *page)
3841{
3842 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3843 mddev->sync_speed_min ? "local": "system");
3844}
3845
3846static ssize_t
3847sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3848{
3849 int min;
3850 char *e;
3851 if (strncmp(buf, "system", 6)==0) {
3852 mddev->sync_speed_min = 0;
3853 return len;
3854 }
3855 min = simple_strtoul(buf, &e, 10);
3856 if (buf == e || (*e && *e != '\n') || min <= 0)
3857 return -EINVAL;
3858 mddev->sync_speed_min = min;
3859 return len;
3860}
3861
3862static struct md_sysfs_entry md_sync_min =
3863__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3864
3865static ssize_t
3866sync_max_show(mddev_t *mddev, char *page)
3867{
3868 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3869 mddev->sync_speed_max ? "local": "system");
3870}
3871
3872static ssize_t
3873sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3874{
3875 int max;
3876 char *e;
3877 if (strncmp(buf, "system", 6)==0) {
3878 mddev->sync_speed_max = 0;
3879 return len;
3880 }
3881 max = simple_strtoul(buf, &e, 10);
3882 if (buf == e || (*e && *e != '\n') || max <= 0)
3883 return -EINVAL;
3884 mddev->sync_speed_max = max;
3885 return len;
3886}
3887
3888static struct md_sysfs_entry md_sync_max =
3889__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3890
Iustin Popd7f3d292007-10-16 23:30:54 -07003891static ssize_t
3892degraded_show(mddev_t *mddev, char *page)
3893{
3894 return sprintf(page, "%d\n", mddev->degraded);
3895}
3896static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003897
3898static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003899sync_force_parallel_show(mddev_t *mddev, char *page)
3900{
3901 return sprintf(page, "%d\n", mddev->parallel_resync);
3902}
3903
3904static ssize_t
3905sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3906{
3907 long n;
3908
3909 if (strict_strtol(buf, 10, &n))
3910 return -EINVAL;
3911
3912 if (n != 0 && n != 1)
3913 return -EINVAL;
3914
3915 mddev->parallel_resync = n;
3916
3917 if (mddev->sync_thread)
3918 wake_up(&resync_wait);
3919
3920 return len;
3921}
3922
3923/* force parallel resync, even with shared block devices */
3924static struct md_sysfs_entry md_sync_force_parallel =
3925__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3926 sync_force_parallel_show, sync_force_parallel_store);
3927
3928static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003929sync_speed_show(mddev_t *mddev, char *page)
3930{
3931 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003932 if (mddev->curr_resync == 0)
3933 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003934 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3935 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003936 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003937 db = resync - mddev->resync_mark_cnt;
3938 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003939}
3940
NeilBrown80ca3a42006-07-10 04:44:18 -07003941static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003942
3943static ssize_t
3944sync_completed_show(mddev_t *mddev, char *page)
3945{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003946 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003947
NeilBrownacb180b2009-04-14 16:28:34 +10003948 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3949 return sprintf(page, "none\n");
3950
NeilBrown88202a02006-01-06 00:21:36 -08003951 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003952 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003953 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003954 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003955
NeilBrownacb180b2009-04-14 16:28:34 +10003956 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003957 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003958}
3959
NeilBrown80ca3a42006-07-10 04:44:18 -07003960static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003961
NeilBrowne464eaf2006-03-27 01:18:14 -08003962static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003963min_sync_show(mddev_t *mddev, char *page)
3964{
3965 return sprintf(page, "%llu\n",
3966 (unsigned long long)mddev->resync_min);
3967}
3968static ssize_t
3969min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3970{
3971 unsigned long long min;
3972 if (strict_strtoull(buf, 10, &min))
3973 return -EINVAL;
3974 if (min > mddev->resync_max)
3975 return -EINVAL;
3976 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3977 return -EBUSY;
3978
3979 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003980 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003981 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003982 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003983 return -EINVAL;
3984 }
3985 mddev->resync_min = min;
3986
3987 return len;
3988}
3989
3990static struct md_sysfs_entry md_min_sync =
3991__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3992
3993static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003994max_sync_show(mddev_t *mddev, char *page)
3995{
3996 if (mddev->resync_max == MaxSector)
3997 return sprintf(page, "max\n");
3998 else
3999 return sprintf(page, "%llu\n",
4000 (unsigned long long)mddev->resync_max);
4001}
4002static ssize_t
4003max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4004{
4005 if (strncmp(buf, "max", 3) == 0)
4006 mddev->resync_max = MaxSector;
4007 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004008 unsigned long long max;
4009 if (strict_strtoull(buf, 10, &max))
4010 return -EINVAL;
4011 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004012 return -EINVAL;
4013 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004014 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004015 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4016 return -EBUSY;
4017
4018 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004019 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004020 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004021 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004022 return -EINVAL;
4023 }
4024 mddev->resync_max = max;
4025 }
4026 wake_up(&mddev->recovery_wait);
4027 return len;
4028}
4029
4030static struct md_sysfs_entry md_max_sync =
4031__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4032
4033static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004034suspend_lo_show(mddev_t *mddev, char *page)
4035{
4036 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4037}
4038
4039static ssize_t
4040suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4041{
4042 char *e;
4043 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004044 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004045
NeilBrownb8d966e2009-07-01 11:14:04 +10004046 if (mddev->pers == NULL ||
4047 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004048 return -EINVAL;
4049 if (buf == e || (*e && *e != '\n'))
4050 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004051
4052 mddev->suspend_lo = new;
4053 if (new >= old)
4054 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004055 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004056 else {
4057 /* Expanding suspended region - need to wait */
4058 mddev->pers->quiesce(mddev, 1);
4059 mddev->pers->quiesce(mddev, 0);
4060 }
4061 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004062}
4063static struct md_sysfs_entry md_suspend_lo =
4064__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4065
4066
4067static ssize_t
4068suspend_hi_show(mddev_t *mddev, char *page)
4069{
4070 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4071}
4072
4073static ssize_t
4074suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4075{
4076 char *e;
4077 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004078 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004079
NeilBrownb8d966e2009-07-01 11:14:04 +10004080 if (mddev->pers == NULL ||
4081 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004082 return -EINVAL;
4083 if (buf == e || (*e && *e != '\n'))
4084 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004085
4086 mddev->suspend_hi = new;
4087 if (new <= old)
4088 /* Shrinking suspended region */
4089 mddev->pers->quiesce(mddev, 2);
4090 else {
4091 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004092 mddev->pers->quiesce(mddev, 1);
4093 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004094 }
4095 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004096}
4097static struct md_sysfs_entry md_suspend_hi =
4098__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4099
NeilBrown08a02ec2007-05-09 02:35:38 -07004100static ssize_t
4101reshape_position_show(mddev_t *mddev, char *page)
4102{
4103 if (mddev->reshape_position != MaxSector)
4104 return sprintf(page, "%llu\n",
4105 (unsigned long long)mddev->reshape_position);
4106 strcpy(page, "none\n");
4107 return 5;
4108}
4109
4110static ssize_t
4111reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4112{
4113 char *e;
4114 unsigned long long new = simple_strtoull(buf, &e, 10);
4115 if (mddev->pers)
4116 return -EBUSY;
4117 if (buf == e || (*e && *e != '\n'))
4118 return -EINVAL;
4119 mddev->reshape_position = new;
4120 mddev->delta_disks = 0;
4121 mddev->new_level = mddev->level;
4122 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004123 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004124 return len;
4125}
4126
4127static struct md_sysfs_entry md_reshape_position =
4128__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4129 reshape_position_store);
4130
Dan Williamsb522adc2009-03-31 15:00:31 +11004131static ssize_t
4132array_size_show(mddev_t *mddev, char *page)
4133{
4134 if (mddev->external_size)
4135 return sprintf(page, "%llu\n",
4136 (unsigned long long)mddev->array_sectors/2);
4137 else
4138 return sprintf(page, "default\n");
4139}
4140
4141static ssize_t
4142array_size_store(mddev_t *mddev, const char *buf, size_t len)
4143{
4144 sector_t sectors;
4145
4146 if (strncmp(buf, "default", 7) == 0) {
4147 if (mddev->pers)
4148 sectors = mddev->pers->size(mddev, 0, 0);
4149 else
4150 sectors = mddev->array_sectors;
4151
4152 mddev->external_size = 0;
4153 } else {
4154 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4155 return -EINVAL;
4156 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004157 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004158
4159 mddev->external_size = 1;
4160 }
4161
4162 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004163 if (mddev->pers) {
4164 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004165 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004166 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004167 return len;
4168}
4169
4170static struct md_sysfs_entry md_array_size =
4171__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4172 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004173
NeilBrowneae17012005-11-08 21:39:23 -08004174static struct attribute *md_default_attrs[] = {
4175 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004176 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004177 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004178 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004179 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004180 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004181 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004182 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004183 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004184 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004185 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004186 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004187 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004188 NULL,
4189};
4190
4191static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004192 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004193 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004194 &md_sync_min.attr,
4195 &md_sync_max.attr,
4196 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004197 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004198 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004199 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004200 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004201 &md_suspend_lo.attr,
4202 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004203 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004204 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004205 NULL,
4206};
NeilBrown411036f2005-11-08 21:39:40 -08004207static struct attribute_group md_redundancy_group = {
4208 .name = NULL,
4209 .attrs = md_redundancy_attrs,
4210};
4211
NeilBrowneae17012005-11-08 21:39:23 -08004212
4213static ssize_t
4214md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4215{
4216 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4217 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004218 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004219
4220 if (!entry->show)
4221 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004222 rv = mddev_lock(mddev);
4223 if (!rv) {
4224 rv = entry->show(mddev, page);
4225 mddev_unlock(mddev);
4226 }
NeilBrown96de1e62005-11-08 21:39:39 -08004227 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004228}
4229
4230static ssize_t
4231md_attr_store(struct kobject *kobj, struct attribute *attr,
4232 const char *page, size_t length)
4233{
4234 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4235 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004236 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004237
4238 if (!entry->store)
4239 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004240 if (!capable(CAP_SYS_ADMIN))
4241 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004242 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004243 if (mddev->hold_active == UNTIL_IOCTL)
4244 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004245 if (!rv) {
4246 rv = entry->store(mddev, page, length);
4247 mddev_unlock(mddev);
4248 }
NeilBrown96de1e62005-11-08 21:39:39 -08004249 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004250}
4251
4252static void md_free(struct kobject *ko)
4253{
4254 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004255
4256 if (mddev->sysfs_state)
4257 sysfs_put(mddev->sysfs_state);
4258
4259 if (mddev->gendisk) {
4260 del_gendisk(mddev->gendisk);
4261 put_disk(mddev->gendisk);
4262 }
4263 if (mddev->queue)
4264 blk_cleanup_queue(mddev->queue);
4265
NeilBrowneae17012005-11-08 21:39:23 -08004266 kfree(mddev);
4267}
4268
Emese Revfy52cf25d2010-01-19 02:58:23 +01004269static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004270 .show = md_attr_show,
4271 .store = md_attr_store,
4272};
4273static struct kobj_type md_ktype = {
4274 .release = md_free,
4275 .sysfs_ops = &md_sysfs_ops,
4276 .default_attrs = md_default_attrs,
4277};
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279int mdp_major = 0;
4280
Dan Williams5fd3a172009-03-04 00:57:25 -07004281static void mddev_delayed_delete(struct work_struct *ws)
4282{
4283 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4284
NeilBrown43a70502009-12-14 12:49:55 +11004285 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004286 kobject_del(&mddev->kobj);
4287 kobject_put(&mddev->kobj);
4288}
4289
NeilBrownefeb53c2009-01-09 08:31:10 +11004290static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004292 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 mddev_t *mddev = mddev_find(dev);
4294 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004295 int partitioned;
4296 int shift;
4297 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004298 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004301 return -ENODEV;
4302
4303 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4304 shift = partitioned ? MdpMinorShift : 0;
4305 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Tejun Heoe804ac72010-10-15 15:36:08 +02004307 /* wait for any previous instance of this device to be
4308 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004309 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004310 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004311
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004312 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004313 error = -EEXIST;
4314 if (mddev->gendisk)
4315 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004316
4317 if (name) {
4318 /* Need to ensure that 'name' is not a duplicate.
4319 */
4320 mddev_t *mddev2;
4321 spin_lock(&all_mddevs_lock);
4322
4323 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4324 if (mddev2->gendisk &&
4325 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4326 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004327 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004328 }
4329 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
NeilBrown8b765392009-01-09 08:31:08 +11004331
NeilBrown0909dc42009-07-01 12:27:21 +10004332 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004333 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004334 if (!mddev->queue)
4335 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004336 mddev->queue->queuedata = mddev;
4337
NeilBrown409c57f2009-03-31 14:39:39 +11004338 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 disk = alloc_disk(1 << shift);
4341 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004342 blk_cleanup_queue(mddev->queue);
4343 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004344 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004346 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004348 if (name)
4349 strcpy(disk->disk_name, name);
4350 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004352 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 disk->fops = &md_fops;
4355 disk->private_data = mddev;
4356 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004357 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004358 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004359 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004360 * remove it now.
4361 */
4362 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004364 /* As soon as we call add_disk(), another thread could get
4365 * through to md_open, so make sure it doesn't get too far
4366 */
4367 mutex_lock(&mddev->open_mutex);
4368 add_disk(disk);
4369
Tejun Heoed9e1982008-08-25 19:56:05 +09004370 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4371 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004372 if (error) {
4373 /* This isn't possible, but as kobject_init_and_add is marked
4374 * __must_check, we must do something with the result
4375 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004376 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4377 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004378 error = 0;
4379 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004380 if (mddev->kobj.sd &&
4381 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004382 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004383 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004384 abort:
4385 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004386 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004387 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004388 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004389 }
NeilBrownd3374822009-01-09 08:31:10 +11004390 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004391 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004392}
4393
4394static struct kobject *md_probe(dev_t dev, int *part, void *data)
4395{
4396 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return NULL;
4398}
4399
NeilBrownefeb53c2009-01-09 08:31:10 +11004400static int add_named_array(const char *val, struct kernel_param *kp)
4401{
4402 /* val must be "md_*" where * is not all digits.
4403 * We allocate an array with a large free minor number, and
4404 * set the name to val. val must not already be an active name.
4405 */
4406 int len = strlen(val);
4407 char buf[DISK_NAME_LEN];
4408
4409 while (len && val[len-1] == '\n')
4410 len--;
4411 if (len >= DISK_NAME_LEN)
4412 return -E2BIG;
4413 strlcpy(buf, val, len+1);
4414 if (strncmp(buf, "md_", 3) != 0)
4415 return -EINVAL;
4416 return md_alloc(0, buf);
4417}
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419static void md_safemode_timeout(unsigned long data)
4420{
4421 mddev_t *mddev = (mddev_t *) data;
4422
Neil Brown0fd62b82008-06-28 08:31:36 +10004423 if (!atomic_read(&mddev->writes_pending)) {
4424 mddev->safemode = 1;
4425 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004426 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 md_wakeup_thread(mddev->thread);
4429}
4430
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004431static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
NeilBrown390ee602010-06-01 19:37:27 +10004433int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
NeilBrown2604b702006-01-06 00:20:36 -08004435 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004437 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
NeilBrowna757e642005-04-16 15:26:42 -07004439 if (list_empty(&mddev->disks))
4440 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 if (mddev->pers)
4444 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004445 /* Cannot run until previous stop completes properly */
4446 if (mddev->sysfs_active)
4447 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 /*
4450 * Analyze all RAID superblock(s)
4451 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004452 if (!mddev->raid_disks) {
4453 if (!mddev->persistent)
4454 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004455 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
NeilBrownd9d166c2006-01-06 00:20:51 -08004458 if (mddev->level != LEVEL_NONE)
4459 request_module("md-level-%d", mddev->level);
4460 else if (mddev->clevel[0])
4461 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 /*
4464 * Drop all container device buffers, from now on
4465 * the only valid external interface is through the md
4466 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004468 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004469 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 continue;
4471 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004472 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004473
4474 /* perform some consistency tests on the device.
4475 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004476 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004477 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004478 if (rdev->meta_bdev) {
4479 /* Nothing to check */;
4480 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004481 if (mddev->dev_sectors &&
4482 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004483 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004484 printk("md: %s: data overlaps metadata\n",
4485 mdname(mddev));
4486 return -EINVAL;
4487 }
4488 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004489 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004490 > rdev->data_offset) {
4491 printk("md: %s: metadata overlaps data\n",
4492 mdname(mddev));
4493 return -EINVAL;
4494 }
4495 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004496 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
4498
NeilBrowna167f662010-10-26 18:31:13 +11004499 if (mddev->bio_set == NULL)
4500 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004503 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004504 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004506 if (mddev->level != LEVEL_NONE)
4507 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4508 mddev->level);
4509 else
4510 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4511 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return -EINVAL;
4513 }
NeilBrown2604b702006-01-06 00:20:36 -08004514 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004516 if (mddev->level != pers->level) {
4517 mddev->level = pers->level;
4518 mddev->new_level = pers->level;
4519 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004520 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
NeilBrownf6705572006-03-27 01:18:11 -08004522 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004523 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004524 /* This personality cannot handle reshaping... */
4525 mddev->pers = NULL;
4526 module_put(pers->owner);
4527 return -EINVAL;
4528 }
4529
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004530 if (pers->sync_request) {
4531 /* Warn if this is a potentially silly
4532 * configuration.
4533 */
4534 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4535 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004536 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004537
4538 list_for_each_entry(rdev, &mddev->disks, same_set)
4539 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004540 if (rdev < rdev2 &&
4541 rdev->bdev->bd_contains ==
4542 rdev2->bdev->bd_contains) {
4543 printk(KERN_WARNING
4544 "%s: WARNING: %s appears to be"
4545 " on the same physical disk as"
4546 " %s.\n",
4547 mdname(mddev),
4548 bdevname(rdev->bdev,b),
4549 bdevname(rdev2->bdev,b2));
4550 warned = 1;
4551 }
4552 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004553
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004554 if (warned)
4555 printk(KERN_WARNING
4556 "True protection against single-disk"
4557 " failure might be compromised.\n");
4558 }
4559
NeilBrown657390d2005-08-26 18:34:16 -07004560 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004561 /* may be over-ridden by personality */
4562 mddev->resync_max_sectors = mddev->dev_sectors;
4563
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004564 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
NeilBrown0f9552b2009-12-30 12:08:50 +11004566 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004567 mddev->ro = 2; /* read-only, but switch on first write */
4568
NeilBrownb15c2e52006-01-06 00:20:16 -08004569 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004570 if (err)
4571 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004572 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4573 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4574 " but 'external_size' not in effect?\n", __func__);
4575 printk(KERN_ERR
4576 "md: invalid array_size %llu > default size %llu\n",
4577 (unsigned long long)mddev->array_sectors / 2,
4578 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4579 err = -EINVAL;
4580 mddev->pers->stop(mddev);
4581 }
4582 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004583 err = bitmap_create(mddev);
4584 if (err) {
4585 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4586 mdname(mddev), err);
4587 mddev->pers->stop(mddev);
4588 }
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 module_put(mddev->pers->owner);
4592 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004593 bitmap_destroy(mddev);
4594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004596 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004597 if (mddev->kobj.sd &&
4598 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004599 printk(KERN_WARNING
4600 "md: cannot register extra attributes for %s\n",
4601 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004602 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004603 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004604 mddev->ro = 0;
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004607 atomic_set(&mddev->max_corr_read_errors,
4608 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 mddev->safemode = 0;
4610 mddev->safemode_timer.function = md_safemode_timeout;
4611 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004612 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004614 smp_wmb();
4615 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004616 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004617 if (rdev->raid_disk >= 0)
4618 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004619 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4622
NeilBrown850b2b42006-10-03 01:15:46 -07004623 if (mddev->flags)
4624 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
NeilBrownd7603b72006-01-06 00:20:30 -08004626 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004627 sysfs_notify_dirent_safe(mddev->sysfs_state);
4628 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004629 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return 0;
4631}
NeilBrown390ee602010-06-01 19:37:27 +10004632EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
NeilBrownfe60b012010-03-29 11:10:42 +11004634static int do_md_run(mddev_t *mddev)
4635{
4636 int err;
4637
4638 err = md_run(mddev);
4639 if (err)
4640 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004641 err = bitmap_load(mddev);
4642 if (err) {
4643 bitmap_destroy(mddev);
4644 goto out;
4645 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004646
4647 md_wakeup_thread(mddev->thread);
4648 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4649
NeilBrownfe60b012010-03-29 11:10:42 +11004650 set_capacity(mddev->gendisk, mddev->array_sectors);
4651 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004652 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004653 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4654out:
4655 return err;
4656}
4657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658static int restart_array(mddev_t *mddev)
4659{
4660 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Andre Noll80fab1d2008-07-11 22:02:21 +10004662 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004664 return -ENXIO;
4665 if (!mddev->pers)
4666 return -EINVAL;
4667 if (!mddev->ro)
4668 return -EBUSY;
4669 mddev->safemode = 0;
4670 mddev->ro = 0;
4671 set_disk_ro(disk, 0);
4672 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4673 mdname(mddev));
4674 /* Kick recovery or resync if necessary */
4675 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4676 md_wakeup_thread(mddev->thread);
4677 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004678 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
NeilBrownacc55e22006-06-26 00:27:47 -07004682/* similar to deny_write_access, but accounts for our holding a reference
4683 * to the file ourselves */
4684static int deny_bitmap_write_access(struct file * file)
4685{
4686 struct inode *inode = file->f_mapping->host;
4687
4688 spin_lock(&inode->i_lock);
4689 if (atomic_read(&inode->i_writecount) > 1) {
4690 spin_unlock(&inode->i_lock);
4691 return -ETXTBSY;
4692 }
4693 atomic_set(&inode->i_writecount, -1);
4694 spin_unlock(&inode->i_lock);
4695
4696 return 0;
4697}
4698
NeilBrown43a70502009-12-14 12:49:55 +11004699void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004700{
4701 struct inode *inode = file->f_mapping->host;
4702
4703 spin_lock(&inode->i_lock);
4704 atomic_set(&inode->i_writecount, 1);
4705 spin_unlock(&inode->i_lock);
4706}
4707
NeilBrown6177b472010-03-29 11:37:13 +11004708static void md_clean(mddev_t *mddev)
4709{
4710 mddev->array_sectors = 0;
4711 mddev->external_size = 0;
4712 mddev->dev_sectors = 0;
4713 mddev->raid_disks = 0;
4714 mddev->recovery_cp = 0;
4715 mddev->resync_min = 0;
4716 mddev->resync_max = MaxSector;
4717 mddev->reshape_position = MaxSector;
4718 mddev->external = 0;
4719 mddev->persistent = 0;
4720 mddev->level = LEVEL_NONE;
4721 mddev->clevel[0] = 0;
4722 mddev->flags = 0;
4723 mddev->ro = 0;
4724 mddev->metadata_type[0] = 0;
4725 mddev->chunk_sectors = 0;
4726 mddev->ctime = mddev->utime = 0;
4727 mddev->layout = 0;
4728 mddev->max_disks = 0;
4729 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004730 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004731 mddev->delta_disks = 0;
4732 mddev->new_level = LEVEL_NONE;
4733 mddev->new_layout = 0;
4734 mddev->new_chunk_sectors = 0;
4735 mddev->curr_resync = 0;
4736 mddev->resync_mismatches = 0;
4737 mddev->suspend_lo = mddev->suspend_hi = 0;
4738 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4739 mddev->recovery = 0;
4740 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004741 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004742 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004743 mddev->safemode = 0;
4744 mddev->bitmap_info.offset = 0;
4745 mddev->bitmap_info.default_offset = 0;
4746 mddev->bitmap_info.chunksize = 0;
4747 mddev->bitmap_info.daemon_sleep = 0;
4748 mddev->bitmap_info.max_write_behind = 0;
4749}
4750
NeilBrowndefad612011-01-14 09:14:33 +11004751static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004752{
4753 if (mddev->sync_thread) {
4754 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4755 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004756 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004757 }
4758
4759 del_timer_sync(&mddev->safemode_timer);
4760
4761 bitmap_flush(mddev);
4762 md_super_wait(mddev);
4763
4764 if (!mddev->in_sync || mddev->flags) {
4765 /* mark array as shutdown cleanly */
4766 mddev->in_sync = 1;
4767 md_update_sb(mddev, 1);
4768 }
4769}
NeilBrowndefad612011-01-14 09:14:33 +11004770
4771void md_stop_writes(mddev_t *mddev)
4772{
4773 mddev_lock(mddev);
4774 __md_stop_writes(mddev);
4775 mddev_unlock(mddev);
4776}
NeilBrown390ee602010-06-01 19:37:27 +10004777EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004778
NeilBrown390ee602010-06-01 19:37:27 +10004779void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004780{
NeilBrown0ca69882011-01-14 09:14:33 +11004781 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004782 mddev->pers->stop(mddev);
4783 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4784 mddev->to_remove = &md_redundancy_group;
4785 module_put(mddev->pers->owner);
4786 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004787 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004788}
NeilBrown390ee602010-06-01 19:37:27 +10004789EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004790
NeilBrowna4bd82d2010-03-29 13:23:10 +11004791static int md_set_readonly(mddev_t *mddev, int is_open)
4792{
4793 int err = 0;
4794 mutex_lock(&mddev->open_mutex);
4795 if (atomic_read(&mddev->openers) > is_open) {
4796 printk("md: %s still in use.\n",mdname(mddev));
4797 err = -EBUSY;
4798 goto out;
4799 }
4800 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004801 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004802
4803 err = -ENXIO;
4804 if (mddev->ro==1)
4805 goto out;
4806 mddev->ro = 1;
4807 set_disk_ro(mddev->gendisk, 1);
4808 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004809 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004810 err = 0;
4811 }
4812out:
4813 mutex_unlock(&mddev->open_mutex);
4814 return err;
4815}
4816
NeilBrown9e653b62006-06-26 00:27:58 -07004817/* mode:
4818 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004819 * 2 - stop but do not disassemble array
4820 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004821static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004824 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
NeilBrownc8c00a62009-08-10 12:50:52 +10004826 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004827 if (atomic_read(&mddev->openers) > is_open ||
4828 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004829 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004830 mutex_unlock(&mddev->open_mutex);
4831 return -EBUSY;
4832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
NeilBrown6e17b022010-08-07 21:41:19 +10004834 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004835 if (mddev->ro)
4836 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004837
NeilBrowndefad612011-01-14 09:14:33 +11004838 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004839 md_stop(mddev);
4840 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004841 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004842
NeilBrowna4bd82d2010-03-29 13:23:10 +11004843 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004844 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004845
NeilBrowna4bd82d2010-03-29 13:23:10 +11004846 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004847 if (rdev->raid_disk >= 0)
4848 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10004849
NeilBrowna4bd82d2010-03-29 13:23:10 +11004850 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004851 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004852 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004853 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004854
NeilBrowna4bd82d2010-03-29 13:23:10 +11004855 if (mddev->ro)
4856 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004857 } else
4858 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 /*
4860 * Free resources if final stop
4861 */
NeilBrown9e653b62006-06-26 00:27:58 -07004862 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4864
NeilBrown978f9462006-02-02 14:28:05 -08004865 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004866 if (mddev->bitmap_info.file) {
4867 restore_bitmap_write_access(mddev->bitmap_info.file);
4868 fput(mddev->bitmap_info.file);
4869 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004870 }
NeilBrownc3d97142009-12-14 12:49:52 +11004871 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 export_array(mddev);
4874
NeilBrown6177b472010-03-29 11:37:13 +11004875 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004876 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004877 if (mddev->hold_active == UNTIL_STOP)
4878 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004879 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004880 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004881 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004882 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884}
4885
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004886#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887static void autorun_array(mddev_t *mddev)
4888{
4889 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 int err;
4891
NeilBrowna757e642005-04-16 15:26:42 -07004892 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895 printk(KERN_INFO "md: running: ");
4896
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004897 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 char b[BDEVNAME_SIZE];
4899 printk("<%s>", bdevname(rdev->bdev,b));
4900 }
4901 printk("\n");
4902
NeilBrownd710e132008-10-13 11:55:12 +11004903 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (err) {
4905 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004906 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 }
4908}
4909
4910/*
4911 * lets try to run arrays based on all disks that have arrived
4912 * until now. (those are in pending_raid_disks)
4913 *
4914 * the method: pick the first pending disk, collect all disks with
4915 * the same UUID, remove all from the pending list and put them into
4916 * the 'same_array' list. Then order this list based on superblock
4917 * update time (freshest comes first), kick out 'old' disks and
4918 * compare superblocks. If everything's fine then run it.
4919 *
4920 * If "unit" is allocated, then bump its reference count
4921 */
4922static void autorun_devices(int part)
4923{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004924 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 mddev_t *mddev;
4926 char b[BDEVNAME_SIZE];
4927
4928 printk(KERN_INFO "md: autorun ...\n");
4929 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004930 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004932 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 rdev0 = list_entry(pending_raid_disks.next,
4934 mdk_rdev_t, same_set);
4935
4936 printk(KERN_INFO "md: considering %s ...\n",
4937 bdevname(rdev0->bdev,b));
4938 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004939 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (super_90_load(rdev, rdev0, 0) >= 0) {
4941 printk(KERN_INFO "md: adding %s ...\n",
4942 bdevname(rdev->bdev,b));
4943 list_move(&rdev->same_set, &candidates);
4944 }
4945 /*
4946 * now we have a set of devices, with all of them having
4947 * mostly sane superblocks. It's time to allocate the
4948 * mddev.
4949 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004950 if (part) {
4951 dev = MKDEV(mdp_major,
4952 rdev0->preferred_minor << MdpMinorShift);
4953 unit = MINOR(dev) >> MdpMinorShift;
4954 } else {
4955 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4956 unit = MINOR(dev);
4957 }
4958 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4960 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4961 break;
4962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
4964 md_probe(dev, NULL, NULL);
4965 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004966 if (!mddev || !mddev->gendisk) {
4967 if (mddev)
4968 mddev_put(mddev);
4969 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 "md: cannot allocate memory for md drive.\n");
4971 break;
4972 }
4973 if (mddev_lock(mddev))
4974 printk(KERN_WARNING "md: %s locked, cannot run\n",
4975 mdname(mddev));
4976 else if (mddev->raid_disks || mddev->major_version
4977 || !list_empty(&mddev->disks)) {
4978 printk(KERN_WARNING
4979 "md: %s already running, cannot run %s\n",
4980 mdname(mddev), bdevname(rdev0->bdev,b));
4981 mddev_unlock(mddev);
4982 } else {
4983 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004984 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004985 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 list_del_init(&rdev->same_set);
4987 if (bind_rdev_to_array(rdev, mddev))
4988 export_rdev(rdev);
4989 }
4990 autorun_array(mddev);
4991 mddev_unlock(mddev);
4992 }
4993 /* on success, candidates will be empty, on error
4994 * it won't...
4995 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004996 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004997 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 mddev_put(mddev);
5001 }
5002 printk(KERN_INFO "md: ... autorun DONE.\n");
5003}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005004#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006static int get_version(void __user * arg)
5007{
5008 mdu_version_t ver;
5009
5010 ver.major = MD_MAJOR_VERSION;
5011 ver.minor = MD_MINOR_VERSION;
5012 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5013
5014 if (copy_to_user(arg, &ver, sizeof(ver)))
5015 return -EFAULT;
5016
5017 return 0;
5018}
5019
5020static int get_array_info(mddev_t * mddev, void __user * arg)
5021{
5022 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005023 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
NeilBrowna9f326e2009-09-23 18:06:41 +10005026 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005027 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005029 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 failed++;
5031 else {
5032 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005033 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005034 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 else
5036 spare++;
5037 }
5038 }
5039
5040 info.major_version = mddev->major_version;
5041 info.minor_version = mddev->minor_version;
5042 info.patch_version = MD_PATCHLEVEL_VERSION;
5043 info.ctime = mddev->ctime;
5044 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005045 info.size = mddev->dev_sectors / 2;
5046 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005047 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 info.nr_disks = nr;
5049 info.raid_disks = mddev->raid_disks;
5050 info.md_minor = mddev->md_minor;
5051 info.not_persistent= !mddev->persistent;
5052
5053 info.utime = mddev->utime;
5054 info.state = 0;
5055 if (mddev->in_sync)
5056 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005057 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005058 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005059 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 info.working_disks = working;
5061 info.failed_disks = failed;
5062 info.spare_disks = spare;
5063
5064 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005065 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 if (copy_to_user(arg, &info, sizeof(info)))
5068 return -EFAULT;
5069
5070 return 0;
5071}
5072
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005073static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005074{
5075 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5076 char *ptr, *buf = NULL;
5077 int err = -ENOMEM;
5078
Dan Williamsb5470dc2008-06-27 21:44:04 -07005079 if (md_allow_write(mddev))
5080 file = kmalloc(sizeof(*file), GFP_NOIO);
5081 else
5082 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005083
NeilBrown32a76272005-06-21 17:17:14 -07005084 if (!file)
5085 goto out;
5086
5087 /* bitmap disabled, zero the first byte and copy out */
5088 if (!mddev->bitmap || !mddev->bitmap->file) {
5089 file->pathname[0] = '\0';
5090 goto copy_out;
5091 }
5092
5093 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5094 if (!buf)
5095 goto out;
5096
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005097 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5098 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005099 goto out;
5100
5101 strcpy(file->pathname, ptr);
5102
5103copy_out:
5104 err = 0;
5105 if (copy_to_user(arg, file, sizeof(*file)))
5106 err = -EFAULT;
5107out:
5108 kfree(buf);
5109 kfree(file);
5110 return err;
5111}
5112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113static int get_disk_info(mddev_t * mddev, void __user * arg)
5114{
5115 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 mdk_rdev_t *rdev;
5117
5118 if (copy_from_user(&info, arg, sizeof(info)))
5119 return -EFAULT;
5120
Andre Noll26ef3792008-07-11 22:02:21 +10005121 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 if (rdev) {
5123 info.major = MAJOR(rdev->bdev->bd_dev);
5124 info.minor = MINOR(rdev->bdev->bd_dev);
5125 info.raid_disk = rdev->raid_disk;
5126 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005127 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005129 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 info.state |= (1<<MD_DISK_ACTIVE);
5131 info.state |= (1<<MD_DISK_SYNC);
5132 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005133 if (test_bit(WriteMostly, &rdev->flags))
5134 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 } else {
5136 info.major = info.minor = 0;
5137 info.raid_disk = -1;
5138 info.state = (1<<MD_DISK_REMOVED);
5139 }
5140
5141 if (copy_to_user(arg, &info, sizeof(info)))
5142 return -EFAULT;
5143
5144 return 0;
5145}
5146
5147static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5148{
5149 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5150 mdk_rdev_t *rdev;
5151 dev_t dev = MKDEV(info->major,info->minor);
5152
5153 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5154 return -EOVERFLOW;
5155
5156 if (!mddev->raid_disks) {
5157 int err;
5158 /* expecting a device which has a superblock */
5159 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5160 if (IS_ERR(rdev)) {
5161 printk(KERN_WARNING
5162 "md: md_import_device returned %ld\n",
5163 PTR_ERR(rdev));
5164 return PTR_ERR(rdev);
5165 }
5166 if (!list_empty(&mddev->disks)) {
5167 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5168 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005169 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 .load_super(rdev, rdev0, mddev->minor_version);
5171 if (err < 0) {
5172 printk(KERN_WARNING
5173 "md: %s has different UUID to %s\n",
5174 bdevname(rdev->bdev,b),
5175 bdevname(rdev0->bdev,b2));
5176 export_rdev(rdev);
5177 return -EINVAL;
5178 }
5179 }
5180 err = bind_rdev_to_array(rdev, mddev);
5181 if (err)
5182 export_rdev(rdev);
5183 return err;
5184 }
5185
5186 /*
5187 * add_new_disk can be used once the array is assembled
5188 * to add "hot spares". They must already have a superblock
5189 * written
5190 */
5191 if (mddev->pers) {
5192 int err;
5193 if (!mddev->pers->hot_add_disk) {
5194 printk(KERN_WARNING
5195 "%s: personality does not support diskops!\n",
5196 mdname(mddev));
5197 return -EINVAL;
5198 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005199 if (mddev->persistent)
5200 rdev = md_import_device(dev, mddev->major_version,
5201 mddev->minor_version);
5202 else
5203 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 if (IS_ERR(rdev)) {
5205 printk(KERN_WARNING
5206 "md: md_import_device returned %ld\n",
5207 PTR_ERR(rdev));
5208 return PTR_ERR(rdev);
5209 }
NeilBrown1a855a02010-12-09 16:36:28 +11005210 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005211 if (!mddev->persistent) {
5212 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005213 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005214 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005215 set_bit(In_sync, &rdev->flags);
5216 } else
NeilBrown41158c72005-06-21 17:17:25 -07005217 rdev->raid_disk = -1;
5218 } else
5219 super_types[mddev->major_version].
5220 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005221 if ((info->state & (1<<MD_DISK_SYNC)) &&
5222 (!test_bit(In_sync, &rdev->flags) ||
5223 rdev->raid_disk != info->raid_disk)) {
5224 /* This was a hot-add request, but events doesn't
5225 * match, so reject it.
5226 */
5227 export_rdev(rdev);
5228 return -EINVAL;
5229 }
5230
NeilBrown1a855a02010-12-09 16:36:28 +11005231 if (test_bit(In_sync, &rdev->flags))
5232 rdev->saved_raid_disk = rdev->raid_disk;
5233 else
5234 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005235
NeilBrownb2d444d2005-11-08 21:39:31 -08005236 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005237 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5238 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005239 else
5240 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005241
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 rdev->raid_disk = -1;
5243 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005244 if (!err && !mddev->pers->hot_remove_disk) {
5245 /* If there is hot_add_disk but no hot_remove_disk
5246 * then added disks for geometry changes,
5247 * and should be added immediately.
5248 */
5249 super_types[mddev->major_version].
5250 validate_super(mddev, rdev);
5251 err = mddev->pers->hot_add_disk(mddev, rdev);
5252 if (err)
5253 unbind_rdev_from_array(rdev);
5254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 if (err)
5256 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005257 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005258 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005259
NeilBrown17571282006-12-10 02:20:52 -08005260 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005261 if (mddev->degraded)
5262 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005264 if (!err)
5265 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005266 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return err;
5268 }
5269
5270 /* otherwise, add_new_disk is only allowed
5271 * for major_version==0 superblocks
5272 */
5273 if (mddev->major_version != 0) {
5274 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5275 mdname(mddev));
5276 return -EINVAL;
5277 }
5278
5279 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5280 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005281 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (IS_ERR(rdev)) {
5283 printk(KERN_WARNING
5284 "md: error, md_import_device() returned %ld\n",
5285 PTR_ERR(rdev));
5286 return PTR_ERR(rdev);
5287 }
5288 rdev->desc_nr = info->number;
5289 if (info->raid_disk < mddev->raid_disks)
5290 rdev->raid_disk = info->raid_disk;
5291 else
5292 rdev->raid_disk = -1;
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005295 if (info->state & (1<<MD_DISK_SYNC))
5296 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005298 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5299 set_bit(WriteMostly, &rdev->flags);
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 if (!mddev->persistent) {
5302 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005303 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5304 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005305 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005306 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
NeilBrown2bf071b2006-01-06 00:20:55 -08005308 err = bind_rdev_to_array(rdev, mddev);
5309 if (err) {
5310 export_rdev(rdev);
5311 return err;
5312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 }
5314
5315 return 0;
5316}
5317
5318static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5319{
5320 char b[BDEVNAME_SIZE];
5321 mdk_rdev_t *rdev;
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 rdev = find_rdev(mddev, dev);
5324 if (!rdev)
5325 return -ENXIO;
5326
5327 if (rdev->raid_disk >= 0)
5328 goto busy;
5329
5330 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005331 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005332 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 return 0;
5335busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005336 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 bdevname(rdev->bdev,b), mdname(mddev));
5338 return -EBUSY;
5339}
5340
5341static int hot_add_disk(mddev_t * mddev, dev_t dev)
5342{
5343 char b[BDEVNAME_SIZE];
5344 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 mdk_rdev_t *rdev;
5346
5347 if (!mddev->pers)
5348 return -ENODEV;
5349
5350 if (mddev->major_version != 0) {
5351 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5352 " version-0 superblocks.\n",
5353 mdname(mddev));
5354 return -EINVAL;
5355 }
5356 if (!mddev->pers->hot_add_disk) {
5357 printk(KERN_WARNING
5358 "%s: personality does not support diskops!\n",
5359 mdname(mddev));
5360 return -EINVAL;
5361 }
5362
NeilBrownd710e132008-10-13 11:55:12 +11005363 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (IS_ERR(rdev)) {
5365 printk(KERN_WARNING
5366 "md: error, md_import_device() returned %ld\n",
5367 PTR_ERR(rdev));
5368 return -EINVAL;
5369 }
5370
5371 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005372 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005374 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
NeilBrown8190e752009-06-18 08:48:58 +10005376 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
NeilBrownb2d444d2005-11-08 21:39:31 -08005378 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 printk(KERN_WARNING
5380 "md: can not hot-add faulty %s disk to %s!\n",
5381 bdevname(rdev->bdev,b), mdname(mddev));
5382 err = -EINVAL;
5383 goto abort_export;
5384 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005385 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005387 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005388 err = bind_rdev_to_array(rdev, mddev);
5389 if (err)
5390 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
5392 /*
5393 * The rest should better be atomic, we can have disk failures
5394 * noticed in interrupt contexts ...
5395 */
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 rdev->raid_disk = -1;
5398
NeilBrown850b2b42006-10-03 01:15:46 -07005399 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
5401 /*
5402 * Kick recovery, maybe this spare has to be added to the
5403 * array immediately.
5404 */
5405 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5406 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005407 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return 0;
5409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410abort_export:
5411 export_rdev(rdev);
5412 return err;
5413}
5414
NeilBrown32a76272005-06-21 17:17:14 -07005415static int set_bitmap_file(mddev_t *mddev, int fd)
5416{
5417 int err;
5418
NeilBrown36fa3062005-09-09 16:23:45 -07005419 if (mddev->pers) {
5420 if (!mddev->pers->quiesce)
5421 return -EBUSY;
5422 if (mddev->recovery || mddev->sync_thread)
5423 return -EBUSY;
5424 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005425 }
5426
NeilBrown36fa3062005-09-09 16:23:45 -07005427
5428 if (fd >= 0) {
5429 if (mddev->bitmap)
5430 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005431 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005432
NeilBrownc3d97142009-12-14 12:49:52 +11005433 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005434 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5435 mdname(mddev));
5436 return -EBADF;
5437 }
5438
NeilBrownc3d97142009-12-14 12:49:52 +11005439 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005440 if (err) {
5441 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5442 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005443 fput(mddev->bitmap_info.file);
5444 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005445 return err;
5446 }
NeilBrownc3d97142009-12-14 12:49:52 +11005447 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005448 } else if (mddev->bitmap == NULL)
5449 return -ENOENT; /* cannot remove what isn't there */
5450 err = 0;
5451 if (mddev->pers) {
5452 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005453 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005454 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005455 if (!err)
5456 err = bitmap_load(mddev);
5457 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005458 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005459 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005460 fd = -1; /* make sure to put the file */
5461 }
NeilBrown36fa3062005-09-09 16:23:45 -07005462 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005463 }
5464 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005465 if (mddev->bitmap_info.file) {
5466 restore_bitmap_write_access(mddev->bitmap_info.file);
5467 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005468 }
NeilBrownc3d97142009-12-14 12:49:52 +11005469 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005470 }
5471
NeilBrown32a76272005-06-21 17:17:14 -07005472 return err;
5473}
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475/*
5476 * set_array_info is used two different ways
5477 * The original usage is when creating a new array.
5478 * In this usage, raid_disks is > 0 and it together with
5479 * level, size, not_persistent,layout,chunksize determine the
5480 * shape of the array.
5481 * This will always create an array with a type-0.90.0 superblock.
5482 * The newer usage is when assembling an array.
5483 * In this case raid_disks will be 0, and the major_version field is
5484 * use to determine which style super-blocks are to be found on the devices.
5485 * The minor and patch _version numbers are also kept incase the
5486 * super_block handler wishes to interpret them.
5487 */
5488static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5489{
5490
5491 if (info->raid_disks == 0) {
5492 /* just setting version number for superblock loading */
5493 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005494 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 super_types[info->major_version].name == NULL) {
5496 /* maybe try to auto-load a module? */
5497 printk(KERN_INFO
5498 "md: superblock version %d not known\n",
5499 info->major_version);
5500 return -EINVAL;
5501 }
5502 mddev->major_version = info->major_version;
5503 mddev->minor_version = info->minor_version;
5504 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005505 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005506 /* ensure mddev_put doesn't delete this now that there
5507 * is some minimal configuration.
5508 */
5509 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 return 0;
5511 }
5512 mddev->major_version = MD_MAJOR_VERSION;
5513 mddev->minor_version = MD_MINOR_VERSION;
5514 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5515 mddev->ctime = get_seconds();
5516
5517 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005518 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005519 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 mddev->raid_disks = info->raid_disks;
5521 /* don't set md_minor, it is determined by which /dev/md* was
5522 * openned
5523 */
5524 if (info->state & (1<<MD_SB_CLEAN))
5525 mddev->recovery_cp = MaxSector;
5526 else
5527 mddev->recovery_cp = 0;
5528 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005529 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
5531 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005532 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
5534 mddev->max_disks = MD_SB_DISKS;
5535
NeilBrowne6910632008-02-06 01:39:51 -08005536 if (mddev->persistent)
5537 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005538 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
NeilBrownc3d97142009-12-14 12:49:52 +11005540 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5541 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005542
NeilBrownf6705572006-03-27 01:18:11 -08005543 mddev->reshape_position = MaxSector;
5544
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 /*
5546 * Generate a 128 bit UUID
5547 */
5548 get_random_bytes(mddev->uuid, 16);
5549
NeilBrownf6705572006-03-27 01:18:11 -08005550 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005551 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005552 mddev->new_layout = mddev->layout;
5553 mddev->delta_disks = 0;
5554
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return 0;
5556}
5557
Dan Williams1f403622009-03-31 14:59:03 +11005558void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5559{
Dan Williamsb522adc2009-03-31 15:00:31 +11005560 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5561
5562 if (mddev->external_size)
5563 return;
5564
Dan Williams1f403622009-03-31 14:59:03 +11005565 mddev->array_sectors = array_sectors;
5566}
5567EXPORT_SYMBOL(md_set_array_sectors);
5568
Andre Nolld71f9f82008-07-11 22:02:22 +10005569static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005570{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005571 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005572 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005573 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005574
5575 if (mddev->pers->resize == NULL)
5576 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005577 /* The "num_sectors" is the number of sectors of each device that
5578 * is used. This can only make sense for arrays with redundancy.
5579 * linear and raid0 always use whatever space is available. We can only
5580 * consider changing this number if no resync or reconstruction is
5581 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005582 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005583 * of each device. If num_sectors is zero, we find the largest size
5584 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005585 */
5586 if (mddev->sync_thread)
5587 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005588 if (mddev->bitmap)
5589 /* Sorry, cannot grow a bitmap yet, just remove it,
5590 * grow, and re-add.
5591 */
5592 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005593 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005594 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005595
Andre Nolld71f9f82008-07-11 22:02:22 +10005596 if (fit && (num_sectors == 0 || num_sectors > avail))
5597 num_sectors = avail;
5598 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005599 return -ENOSPC;
5600 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005601 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005602 if (!rv)
5603 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005604 return rv;
5605}
5606
NeilBrownda943b92006-01-06 00:20:54 -08005607static int update_raid_disks(mddev_t *mddev, int raid_disks)
5608{
5609 int rv;
5610 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005611 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005612 return -EINVAL;
5613 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005614 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005615 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005616 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005617 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005618 mddev->delta_disks = raid_disks - mddev->raid_disks;
5619
5620 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005621 if (rv < 0)
5622 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005623 return rv;
5624}
5625
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627/*
5628 * update_array_info is used to change the configuration of an
5629 * on-line array.
5630 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5631 * fields in the info are checked against the array.
5632 * Any differences that cannot be handled will cause an error.
5633 * Normally, only one change can be managed at a time.
5634 */
5635static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5636{
5637 int rv = 0;
5638 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005639 int state = 0;
5640
5641 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005642 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005643 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
5645 if (mddev->major_version != info->major_version ||
5646 mddev->minor_version != info->minor_version ||
5647/* mddev->patch_version != info->patch_version || */
5648 mddev->ctime != info->ctime ||
5649 mddev->level != info->level ||
5650/* mddev->layout != info->layout || */
5651 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005652 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005653 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5654 ((state^info->state) & 0xfffffe00)
5655 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 return -EINVAL;
5657 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005658 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5659 cnt++;
5660 if (mddev->raid_disks != info->raid_disks)
5661 cnt++;
5662 if (mddev->layout != info->layout)
5663 cnt++;
5664 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5665 cnt++;
5666 if (cnt == 0)
5667 return 0;
5668 if (cnt > 1)
5669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 if (mddev->layout != info->layout) {
5672 /* Change layout
5673 * we don't need to do anything at the md level, the
5674 * personality will take care of it all.
5675 */
NeilBrown50ac1682009-06-18 08:47:55 +10005676 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005678 else {
5679 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005680 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005681 if (rv)
5682 mddev->new_layout = mddev->layout;
5683 return rv;
5684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005686 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005687 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
NeilBrownda943b92006-01-06 00:20:54 -08005689 if (mddev->raid_disks != info->raid_disks)
5690 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
NeilBrown36fa3062005-09-09 16:23:45 -07005692 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5693 if (mddev->pers->quiesce == NULL)
5694 return -EINVAL;
5695 if (mddev->recovery || mddev->sync_thread)
5696 return -EBUSY;
5697 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5698 /* add the bitmap */
5699 if (mddev->bitmap)
5700 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005701 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005702 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005703 mddev->bitmap_info.offset =
5704 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005705 mddev->pers->quiesce(mddev, 1);
5706 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005707 if (!rv)
5708 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005709 if (rv)
5710 bitmap_destroy(mddev);
5711 mddev->pers->quiesce(mddev, 0);
5712 } else {
5713 /* remove the bitmap */
5714 if (!mddev->bitmap)
5715 return -ENOENT;
5716 if (mddev->bitmap->file)
5717 return -EINVAL;
5718 mddev->pers->quiesce(mddev, 1);
5719 bitmap_destroy(mddev);
5720 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005721 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005722 }
5723 }
NeilBrown850b2b42006-10-03 01:15:46 -07005724 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 return rv;
5726}
5727
5728static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5729{
5730 mdk_rdev_t *rdev;
5731
5732 if (mddev->pers == NULL)
5733 return -ENODEV;
5734
5735 rdev = find_rdev(mddev, dev);
5736 if (!rdev)
5737 return -ENODEV;
5738
5739 md_error(mddev, rdev);
5740 return 0;
5741}
5742
Andre Noll2f9618c2008-04-25 18:57:58 +02005743/*
5744 * We have a problem here : there is no easy way to give a CHS
5745 * virtual geometry. We currently pretend that we have a 2 heads
5746 * 4 sectors (with a BIG number of cylinders...). This drives
5747 * dosfs just mad... ;-)
5748 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005749static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5750{
5751 mddev_t *mddev = bdev->bd_disk->private_data;
5752
5753 geo->heads = 2;
5754 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005755 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005756 return 0;
5757}
5758
Al Viroa39907f2008-03-02 10:31:15 -05005759static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 unsigned int cmd, unsigned long arg)
5761{
5762 int err = 0;
5763 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005765 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 if (!capable(CAP_SYS_ADMIN))
5768 return -EACCES;
5769
5770 /*
5771 * Commands dealing with the RAID driver but not any
5772 * particular array:
5773 */
5774 switch (cmd)
5775 {
5776 case RAID_VERSION:
5777 err = get_version(argp);
5778 goto done;
5779
5780 case PRINT_RAID_DEBUG:
5781 err = 0;
5782 md_print_devices();
5783 goto done;
5784
5785#ifndef MODULE
5786 case RAID_AUTORUN:
5787 err = 0;
5788 autostart_arrays(arg);
5789 goto done;
5790#endif
5791 default:;
5792 }
5793
5794 /*
5795 * Commands creating/starting a new array:
5796 */
5797
Al Viroa39907f2008-03-02 10:31:15 -05005798 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
5800 if (!mddev) {
5801 BUG();
5802 goto abort;
5803 }
5804
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 err = mddev_lock(mddev);
5806 if (err) {
5807 printk(KERN_INFO
5808 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5809 err, cmd);
5810 goto abort;
5811 }
5812
5813 switch (cmd)
5814 {
5815 case SET_ARRAY_INFO:
5816 {
5817 mdu_array_info_t info;
5818 if (!arg)
5819 memset(&info, 0, sizeof(info));
5820 else if (copy_from_user(&info, argp, sizeof(info))) {
5821 err = -EFAULT;
5822 goto abort_unlock;
5823 }
5824 if (mddev->pers) {
5825 err = update_array_info(mddev, &info);
5826 if (err) {
5827 printk(KERN_WARNING "md: couldn't update"
5828 " array info. %d\n", err);
5829 goto abort_unlock;
5830 }
5831 goto done_unlock;
5832 }
5833 if (!list_empty(&mddev->disks)) {
5834 printk(KERN_WARNING
5835 "md: array %s already has disks!\n",
5836 mdname(mddev));
5837 err = -EBUSY;
5838 goto abort_unlock;
5839 }
5840 if (mddev->raid_disks) {
5841 printk(KERN_WARNING
5842 "md: array %s already initialised!\n",
5843 mdname(mddev));
5844 err = -EBUSY;
5845 goto abort_unlock;
5846 }
5847 err = set_array_info(mddev, &info);
5848 if (err) {
5849 printk(KERN_WARNING "md: couldn't set"
5850 " array info. %d\n", err);
5851 goto abort_unlock;
5852 }
5853 }
5854 goto done_unlock;
5855
5856 default:;
5857 }
5858
5859 /*
5860 * Commands querying/configuring an existing array:
5861 */
NeilBrown32a76272005-06-21 17:17:14 -07005862 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005863 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005864 if ((!mddev->raid_disks && !mddev->external)
5865 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5866 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5867 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 err = -ENODEV;
5869 goto abort_unlock;
5870 }
5871
5872 /*
5873 * Commands even a read-only array can execute:
5874 */
5875 switch (cmd)
5876 {
5877 case GET_ARRAY_INFO:
5878 err = get_array_info(mddev, argp);
5879 goto done_unlock;
5880
NeilBrown32a76272005-06-21 17:17:14 -07005881 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005882 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005883 goto done_unlock;
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 case GET_DISK_INFO:
5886 err = get_disk_info(mddev, argp);
5887 goto done_unlock;
5888
5889 case RESTART_ARRAY_RW:
5890 err = restart_array(mddev);
5891 goto done_unlock;
5892
5893 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005894 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 goto done_unlock;
5896
5897 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005898 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 goto done_unlock;
5900
Dan Williamse2218352010-05-12 08:25:37 +10005901 case BLKROSET:
5902 if (get_user(ro, (int __user *)(arg))) {
5903 err = -EFAULT;
5904 goto done_unlock;
5905 }
5906 err = -EINVAL;
5907
5908 /* if the bdev is going readonly the value of mddev->ro
5909 * does not matter, no writes are coming
5910 */
5911 if (ro)
5912 goto done_unlock;
5913
5914 /* are we are already prepared for writes? */
5915 if (mddev->ro != 1)
5916 goto done_unlock;
5917
5918 /* transitioning to readauto need only happen for
5919 * arrays that call md_write_start
5920 */
5921 if (mddev->pers) {
5922 err = restart_array(mddev);
5923 if (err == 0) {
5924 mddev->ro = 2;
5925 set_disk_ro(mddev->gendisk, 0);
5926 }
5927 }
5928 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 }
5930
5931 /*
5932 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005933 * superblock, so we do not allow them on read-only arrays.
5934 * However non-MD ioctls (e.g. get-size) will still come through
5935 * here and hit the 'default' below, so only disallow
5936 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005938 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005939 if (mddev->ro == 2) {
5940 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005941 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5943 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005944 } else {
5945 err = -EROFS;
5946 goto abort_unlock;
5947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 }
5949
5950 switch (cmd)
5951 {
5952 case ADD_NEW_DISK:
5953 {
5954 mdu_disk_info_t info;
5955 if (copy_from_user(&info, argp, sizeof(info)))
5956 err = -EFAULT;
5957 else
5958 err = add_new_disk(mddev, &info);
5959 goto done_unlock;
5960 }
5961
5962 case HOT_REMOVE_DISK:
5963 err = hot_remove_disk(mddev, new_decode_dev(arg));
5964 goto done_unlock;
5965
5966 case HOT_ADD_DISK:
5967 err = hot_add_disk(mddev, new_decode_dev(arg));
5968 goto done_unlock;
5969
5970 case SET_DISK_FAULTY:
5971 err = set_disk_faulty(mddev, new_decode_dev(arg));
5972 goto done_unlock;
5973
5974 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005975 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 goto done_unlock;
5977
NeilBrown32a76272005-06-21 17:17:14 -07005978 case SET_BITMAP_FILE:
5979 err = set_bitmap_file(mddev, (int)arg);
5980 goto done_unlock;
5981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 err = -EINVAL;
5984 goto abort_unlock;
5985 }
5986
5987done_unlock:
5988abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005989 if (mddev->hold_active == UNTIL_IOCTL &&
5990 err != -EINVAL)
5991 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 mddev_unlock(mddev);
5993
5994 return err;
5995done:
5996 if (err)
5997 MD_BUG();
5998abort:
5999 return err;
6000}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006001#ifdef CONFIG_COMPAT
6002static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6003 unsigned int cmd, unsigned long arg)
6004{
6005 switch (cmd) {
6006 case HOT_REMOVE_DISK:
6007 case HOT_ADD_DISK:
6008 case SET_DISK_FAULTY:
6009 case SET_BITMAP_FILE:
6010 /* These take in integer arg, do not convert */
6011 break;
6012 default:
6013 arg = (unsigned long)compat_ptr(arg);
6014 break;
6015 }
6016
6017 return md_ioctl(bdev, mode, cmd, arg);
6018}
6019#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Al Viroa39907f2008-03-02 10:31:15 -05006021static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
6023 /*
6024 * Succeed if we can lock the mddev, which confirms that
6025 * it isn't being stopped right now.
6026 */
NeilBrownd3374822009-01-09 08:31:10 +11006027 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 int err;
6029
NeilBrownd3374822009-01-09 08:31:10 +11006030 if (mddev->gendisk != bdev->bd_disk) {
6031 /* we are racing with mddev_put which is discarding this
6032 * bd_disk.
6033 */
6034 mddev_put(mddev);
6035 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006036 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006037 /* Then retry the open from the top */
6038 return -ERESTARTSYS;
6039 }
6040 BUG_ON(mddev != bdev->bd_disk->private_data);
6041
NeilBrownc8c00a62009-08-10 12:50:52 +10006042 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 goto out;
6044
6045 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006046 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006047 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006049 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 out:
6051 return err;
6052}
6053
Al Viroa39907f2008-03-02 10:31:15 -05006054static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
Al Viroa39907f2008-03-02 10:31:15 -05006056 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006058 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006059 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 mddev_put(mddev);
6061
6062 return 0;
6063}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006064
6065static int md_media_changed(struct gendisk *disk)
6066{
6067 mddev_t *mddev = disk->private_data;
6068
6069 return mddev->changed;
6070}
6071
6072static int md_revalidate(struct gendisk *disk)
6073{
6074 mddev_t *mddev = disk->private_data;
6075
6076 mddev->changed = 0;
6077 return 0;
6078}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006079static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006082 .open = md_open,
6083 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006084 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006085#ifdef CONFIG_COMPAT
6086 .compat_ioctl = md_compat_ioctl,
6087#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006088 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006089 .media_changed = md_media_changed,
6090 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091};
6092
Adrian Bunk75c96f82005-05-05 16:16:09 -07006093static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094{
6095 mdk_thread_t *thread = arg;
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 /*
6098 * md_thread is a 'system-thread', it's priority should be very
6099 * high. We avoid resource deadlocks individually in each
6100 * raid personality. (RAID5 does preallocation) We also use RR and
6101 * the very same RT priority as kswapd, thus we will never get
6102 * into a priority inversion deadlock.
6103 *
6104 * we definitely have to have equal or higher priority than
6105 * bdflush, otherwise bdflush will deadlock if there are too
6106 * many dirty RAID5 blocks.
6107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
NeilBrown6985c432005-10-19 21:23:47 -07006109 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006110 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
NeilBrown93588e22005-11-15 00:09:12 -08006112 /* We need to wait INTERRUPTIBLE so that
6113 * we don't add to the load-average.
6114 * That means we need to be sure no signals are
6115 * pending
6116 */
6117 if (signal_pending(current))
6118 flush_signals(current);
6119
6120 wait_event_interruptible_timeout
6121 (thread->wqueue,
6122 test_bit(THREAD_WAKEUP, &thread->flags)
6123 || kthread_should_stop(),
6124 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
NeilBrown6c987912011-01-14 09:13:53 +11006126 clear_bit(THREAD_WAKEUP, &thread->flags);
6127 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006128 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return 0;
6132}
6133
6134void md_wakeup_thread(mdk_thread_t *thread)
6135{
6136 if (thread) {
6137 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6138 set_bit(THREAD_WAKEUP, &thread->flags);
6139 wake_up(&thread->wqueue);
6140 }
6141}
6142
6143mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6144 const char *name)
6145{
6146 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
NeilBrown9ffae0c2006-01-06 00:20:32 -08006148 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 if (!thread)
6150 return NULL;
6151
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 init_waitqueue_head(&thread->wqueue);
6153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 thread->run = run;
6155 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006156 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006157 thread->tsk = kthread_run(md_thread, thread,
6158 "%s_%s",
6159 mdname(thread->mddev),
6160 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006161 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 kfree(thread);
6163 return NULL;
6164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 return thread;
6166}
6167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168void md_unregister_thread(mdk_thread_t *thread)
6169{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006170 if (!thread)
6171 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006172 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006173
6174 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 kfree(thread);
6176}
6177
6178void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6179{
6180 if (!mddev) {
6181 MD_BUG();
6182 return;
6183 }
6184
NeilBrownb2d444d2005-11-08 21:39:31 -08006185 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006187
6188 if (mddev->external)
6189 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6192 mdname(mddev),
6193 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6194 __builtin_return_address(0),__builtin_return_address(1),
6195 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006196*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006197 if (!mddev->pers)
6198 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 if (!mddev->pers->error_handler)
6200 return;
6201 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006202 if (mddev->degraded)
6203 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006204 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6206 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6207 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006208 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006209 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006210 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211}
6212
6213/* seq_file implementation /proc/mdstat */
6214
6215static void status_unused(struct seq_file *seq)
6216{
6217 int i = 0;
6218 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
6220 seq_printf(seq, "unused devices: ");
6221
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006222 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 char b[BDEVNAME_SIZE];
6224 i++;
6225 seq_printf(seq, "%s ",
6226 bdevname(rdev->bdev,b));
6227 }
6228 if (!i)
6229 seq_printf(seq, "<none>");
6230
6231 seq_printf(seq, "\n");
6232}
6233
6234
6235static void status_resync(struct seq_file *seq, mddev_t * mddev)
6236{
NeilBrowndd71cf62009-05-07 12:49:35 +10006237 sector_t max_sectors, resync, res;
6238 unsigned long dt, db;
6239 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006240 int scale;
6241 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
NeilBrowndd71cf62009-05-07 12:49:35 +10006243 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006246 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006248 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
6250 /*
6251 * Should not happen.
6252 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006253 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 MD_BUG();
6255 return;
6256 }
NeilBrown4588b422006-03-27 01:18:04 -08006257 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006258 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006259 * u32, as those are the requirements for sector_div.
6260 * Thus 'scale' must be at least 10
6261 */
6262 scale = 10;
6263 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006264 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006265 scale++;
6266 }
6267 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006268 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006269
6270 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 {
NeilBrown4588b422006-03-27 01:18:04 -08006272 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 seq_printf(seq, "[");
6274 for (i = 0; i < x; i++)
6275 seq_printf(seq, "=");
6276 seq_printf(seq, ">");
6277 for (i = 0; i < y; i++)
6278 seq_printf(seq, ".");
6279 seq_printf(seq, "] ");
6280 }
NeilBrown4588b422006-03-27 01:18:04 -08006281 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006282 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6283 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006284 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6285 "check" :
6286 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6287 "resync" : "recovery"))),
6288 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006289 (unsigned long long) resync/2,
6290 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
6292 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 * dt: time from mark until now
6294 * db: blocks written from mark until now
6295 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006296 *
6297 * rt is a sector_t, so could be 32bit or 64bit.
6298 * So we divide before multiply in case it is 32bit and close
6299 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006300 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006301 * near the end of resync when the number of remaining sectors
6302 * is close to 'db'.
6303 * We then divide rt by 32 after multiplying by db to compensate.
6304 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 */
6306 dt = ((jiffies - mddev->resync_mark) / HZ);
6307 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006308 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6309 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310
NeilBrowndd71cf62009-05-07 12:49:35 +10006311 rt = max_sectors - resync; /* number of remaining sectors */
6312 sector_div(rt, db/32+1);
6313 rt *= dt;
6314 rt >>= 5;
6315
6316 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6317 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
NeilBrownff4e8d92006-07-10 04:44:16 -07006319 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320}
6321
6322static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6323{
6324 struct list_head *tmp;
6325 loff_t l = *pos;
6326 mddev_t *mddev;
6327
6328 if (l >= 0x10000)
6329 return NULL;
6330 if (!l--)
6331 /* header */
6332 return (void*)1;
6333
6334 spin_lock(&all_mddevs_lock);
6335 list_for_each(tmp,&all_mddevs)
6336 if (!l--) {
6337 mddev = list_entry(tmp, mddev_t, all_mddevs);
6338 mddev_get(mddev);
6339 spin_unlock(&all_mddevs_lock);
6340 return mddev;
6341 }
6342 spin_unlock(&all_mddevs_lock);
6343 if (!l--)
6344 return (void*)2;/* tail */
6345 return NULL;
6346}
6347
6348static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6349{
6350 struct list_head *tmp;
6351 mddev_t *next_mddev, *mddev = v;
6352
6353 ++*pos;
6354 if (v == (void*)2)
6355 return NULL;
6356
6357 spin_lock(&all_mddevs_lock);
6358 if (v == (void*)1)
6359 tmp = all_mddevs.next;
6360 else
6361 tmp = mddev->all_mddevs.next;
6362 if (tmp != &all_mddevs)
6363 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6364 else {
6365 next_mddev = (void*)2;
6366 *pos = 0x10000;
6367 }
6368 spin_unlock(&all_mddevs_lock);
6369
6370 if (v != (void*)1)
6371 mddev_put(mddev);
6372 return next_mddev;
6373
6374}
6375
6376static void md_seq_stop(struct seq_file *seq, void *v)
6377{
6378 mddev_t *mddev = v;
6379
6380 if (mddev && v != (void*)1 && v != (void*)2)
6381 mddev_put(mddev);
6382}
6383
NeilBrownd7603b72006-01-06 00:20:30 -08006384struct mdstat_info {
6385 int event;
6386};
6387
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388static int md_seq_show(struct seq_file *seq, void *v)
6389{
6390 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006391 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006393 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006394 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006397 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 seq_printf(seq, "Personalities : ");
6399 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006400 list_for_each_entry(pers, &pers_list, list)
6401 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
6403 spin_unlock(&pers_lock);
6404 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006405 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 return 0;
6407 }
6408 if (v == (void*)2) {
6409 status_unused(seq);
6410 return 0;
6411 }
6412
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006413 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6417 seq_printf(seq, "%s : %sactive", mdname(mddev),
6418 mddev->pers ? "" : "in");
6419 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006420 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006422 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006423 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 seq_printf(seq, " %s", mddev->pers->name);
6425 }
6426
Andre Nolldd8ac332009-03-31 14:33:13 +11006427 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006428 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 char b[BDEVNAME_SIZE];
6430 seq_printf(seq, " %s[%d]",
6431 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006432 if (test_bit(WriteMostly, &rdev->flags))
6433 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006434 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 seq_printf(seq, "(F)");
6436 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006437 } else if (rdev->raid_disk < 0)
6438 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006439 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 }
6441
6442 if (!list_empty(&mddev->disks)) {
6443 if (mddev->pers)
6444 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006445 (unsigned long long)
6446 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 else
6448 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006449 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006451 if (mddev->persistent) {
6452 if (mddev->major_version != 0 ||
6453 mddev->minor_version != 90) {
6454 seq_printf(seq," super %d.%d",
6455 mddev->major_version,
6456 mddev->minor_version);
6457 }
NeilBrowne6910632008-02-06 01:39:51 -08006458 } else if (mddev->external)
6459 seq_printf(seq, " super external:%s",
6460 mddev->metadata_type);
6461 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006462 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
6464 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006465 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006467 if (mddev->pers->sync_request) {
6468 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006469 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006470 seq_printf(seq, "\n ");
6471 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6472 seq_printf(seq, "\tresync=DELAYED\n ");
6473 else if (mddev->recovery_cp < MaxSector)
6474 seq_printf(seq, "\tresync=PENDING\n ");
6475 }
NeilBrown32a76272005-06-21 17:17:14 -07006476 } else
6477 seq_printf(seq, "\n ");
6478
6479 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006480 unsigned long chunk_kb;
6481 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006482 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006483 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006484 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6485 "%lu%s chunk",
6486 bitmap->pages - bitmap->missing_pages,
6487 bitmap->pages,
6488 (bitmap->pages - bitmap->missing_pages)
6489 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006490 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006491 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006492 if (bitmap->file) {
6493 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006494 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006495 }
NeilBrown78d742d2005-06-21 17:17:15 -07006496
NeilBrown32a76272005-06-21 17:17:14 -07006497 seq_printf(seq, "\n");
6498 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
6500
6501 seq_printf(seq, "\n");
6502 }
6503 mddev_unlock(mddev);
6504
6505 return 0;
6506}
6507
Jan Engelhardt110518b2009-05-07 12:49:37 +10006508static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 .start = md_seq_start,
6510 .next = md_seq_next,
6511 .stop = md_seq_stop,
6512 .show = md_seq_show,
6513};
6514
6515static int md_seq_open(struct inode *inode, struct file *file)
6516{
6517 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006518 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6519 if (mi == NULL)
6520 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521
6522 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006523 if (error)
6524 kfree(mi);
6525 else {
6526 struct seq_file *p = file->private_data;
6527 p->private = mi;
6528 mi->event = atomic_read(&md_event_count);
6529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 return error;
6531}
6532
NeilBrownd7603b72006-01-06 00:20:30 -08006533static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6534{
6535 struct seq_file *m = filp->private_data;
6536 struct mdstat_info *mi = m->private;
6537 int mask;
6538
6539 poll_wait(filp, &md_event_waiters, wait);
6540
6541 /* always allow read */
6542 mask = POLLIN | POLLRDNORM;
6543
6544 if (mi->event != atomic_read(&md_event_count))
6545 mask |= POLLERR | POLLPRI;
6546 return mask;
6547}
6548
Arjan van de Venfa027c22007-02-12 00:55:33 -08006549static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006550 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 .open = md_seq_open,
6552 .read = seq_read,
6553 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006554 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006555 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556};
6557
NeilBrown2604b702006-01-06 00:20:36 -08006558int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006561 list_add_tail(&p->list, &pers_list);
6562 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 spin_unlock(&pers_lock);
6564 return 0;
6565}
6566
NeilBrown2604b702006-01-06 00:20:36 -08006567int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568{
NeilBrown2604b702006-01-06 00:20:36 -08006569 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006571 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 spin_unlock(&pers_lock);
6573 return 0;
6574}
6575
NeilBrowneea1bf32009-03-31 14:27:02 +11006576static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577{
6578 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006580 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581
6582 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006583 rcu_read_lock();
6584 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006586 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6587 (int)part_stat_read(&disk->part0, sectors[1]) -
6588 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006589 /* sync IO will cause sync_io to increase before the disk_stats
6590 * as sync_io is counted when a request starts, and
6591 * disk_stats is counted when it completes.
6592 * So resync activity will cause curr_events to be smaller than
6593 * when there was no such activity.
6594 * non-sync IO will cause disk_stat to increase without
6595 * increasing sync_io so curr_events will (eventually)
6596 * be larger than it was before. Once it becomes
6597 * substantially larger, the test below will cause
6598 * the array to appear non-idle, and resync will slow
6599 * down.
6600 * If there is a lot of outstanding resync activity when
6601 * we set last_event to curr_events, then all that activity
6602 * completing might cause the array to appear non-idle
6603 * and resync will be slowed down even though there might
6604 * not have been non-resync activity. This will only
6605 * happen once though. 'last_events' will soon reflect
6606 * the state where there is little or no outstanding
6607 * resync requests, and further resync activity will
6608 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006611 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 rdev->last_events = curr_events;
6613 idle = 0;
6614 }
6615 }
NeilBrown4b809912008-07-21 17:05:25 +10006616 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 return idle;
6618}
6619
6620void md_done_sync(mddev_t *mddev, int blocks, int ok)
6621{
6622 /* another "blocks" (512byte) blocks have been synced */
6623 atomic_sub(blocks, &mddev->recovery_active);
6624 wake_up(&mddev->recovery_wait);
6625 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006626 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 md_wakeup_thread(mddev->thread);
6628 // stop recovery, signal do_sync ....
6629 }
6630}
6631
6632
NeilBrown06d91a52005-06-21 17:17:12 -07006633/* md_write_start(mddev, bi)
6634 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006635 * in superblock) before writing, schedule a superblock update
6636 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006637 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006638void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639{
Neil Brown0fd62b82008-06-28 08:31:36 +10006640 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006641 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006642 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006643
NeilBrownf91de922005-11-08 21:39:36 -08006644 BUG_ON(mddev->ro == 1);
6645 if (mddev->ro == 2) {
6646 /* need to switch to read/write */
6647 mddev->ro = 0;
6648 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6649 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006650 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006651 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006652 }
NeilBrown06d91a52005-06-21 17:17:12 -07006653 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006654 if (mddev->safemode == 1)
6655 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006656 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006657 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006658 if (mddev->in_sync) {
6659 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006660 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006661 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006662 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006663 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006664 }
NeilBrowna9701a32005-11-08 21:39:34 -08006665 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006666 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006667 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006668 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006669 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006670 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671}
6672
6673void md_write_end(mddev_t *mddev)
6674{
6675 if (atomic_dec_and_test(&mddev->writes_pending)) {
6676 if (mddev->safemode == 2)
6677 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006678 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6680 }
6681}
6682
NeilBrown2a2275d2007-01-26 00:57:11 -08006683/* md_allow_write(mddev)
6684 * Calling this ensures that the array is marked 'active' so that writes
6685 * may proceed without blocking. It is important to call this before
6686 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6687 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006688 *
6689 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6690 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006691 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006692int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006693{
6694 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006695 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006696 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006697 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006698 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006699 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006700
6701 spin_lock_irq(&mddev->write_lock);
6702 if (mddev->in_sync) {
6703 mddev->in_sync = 0;
6704 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006705 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006706 if (mddev->safemode_delay &&
6707 mddev->safemode == 0)
6708 mddev->safemode = 1;
6709 spin_unlock_irq(&mddev->write_lock);
6710 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006711 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006712 } else
6713 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006714
NeilBrown070dc6d2010-08-30 17:33:34 +10006715 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006716 return -EAGAIN;
6717 else
6718 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006719}
6720EXPORT_SYMBOL_GPL(md_allow_write);
6721
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722#define SYNC_MARKS 10
6723#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006724void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725{
6726 mddev_t *mddev2;
6727 unsigned int currspeed = 0,
6728 window;
NeilBrown57afd892005-06-21 17:17:13 -07006729 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 unsigned long mark[SYNC_MARKS];
6731 sector_t mark_cnt[SYNC_MARKS];
6732 int last_mark,m;
6733 struct list_head *tmp;
6734 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006735 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006736 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006737 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 /* just incase thread restarts... */
6740 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6741 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006742 if (mddev->ro) /* never try to sync a read-only array */
6743 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
NeilBrown61df9d92006-10-03 01:15:57 -07006745 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6746 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6747 desc = "data-check";
6748 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6749 desc = "requested-resync";
6750 else
6751 desc = "resync";
6752 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6753 desc = "reshape";
6754 else
6755 desc = "recovery";
6756
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 /* we overload curr_resync somewhat here.
6758 * 0 == not engaged in resync at all
6759 * 2 == checking that there is no conflict with another sync
6760 * 1 == like 2, but have yielded to allow conflicting resync to
6761 * commense
6762 * other == active in resync - this many blocks
6763 *
6764 * Before starting a resync we must have set curr_resync to
6765 * 2, and then checked that every "conflicting" array has curr_resync
6766 * less than ours. When we find one that is the same or higher
6767 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6768 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6769 * This will mean we have to start checking from the beginning again.
6770 *
6771 */
6772
6773 do {
6774 mddev->curr_resync = 2;
6775
6776 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006777 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006778 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006779
6780 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006782 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 if (mddev2 == mddev)
6784 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006785 if (!mddev->parallel_resync
6786 && mddev2->curr_resync
6787 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 DEFINE_WAIT(wq);
6789 if (mddev < mddev2 && mddev->curr_resync == 2) {
6790 /* arbitrarily yield */
6791 mddev->curr_resync = 1;
6792 wake_up(&resync_wait);
6793 }
6794 if (mddev > mddev2 && mddev->curr_resync == 1)
6795 /* no need to wait here, we can wait the next
6796 * time 'round when curr_resync == 2
6797 */
6798 continue;
NeilBrown97441972008-09-19 11:49:54 +10006799 /* We need to wait 'interruptible' so as not to
6800 * contribute to the load average, and not to
6801 * be caught by 'softlockup'
6802 */
6803 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006804 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006805 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006806 printk(KERN_INFO "md: delaying %s of %s"
6807 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006809 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006811 if (signal_pending(current))
6812 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 schedule();
6814 finish_wait(&resync_wait, &wq);
6815 goto try_again;
6816 }
6817 finish_wait(&resync_wait, &wq);
6818 }
6819 }
6820 } while (mddev->curr_resync < 2);
6821
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006822 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006823 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006825 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 */
6827 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006828 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006829 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006830 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6831 j = mddev->resync_min;
6832 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006833 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006834
NeilBrownccfcc3c2006-03-27 01:18:09 -08006835 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006836 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006837 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006839 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006840 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006841 rcu_read_lock();
6842 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006843 if (rdev->raid_disk >= 0 &&
6844 !test_bit(Faulty, &rdev->flags) &&
6845 !test_bit(In_sync, &rdev->flags) &&
6846 rdev->recovery_offset < j)
6847 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006848 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
NeilBrown61df9d92006-10-03 01:15:57 -07006851 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6852 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6853 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006854 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006855 "(but not more than %d KB/sec) for %s.\n",
6856 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
NeilBrowneea1bf32009-03-31 14:27:02 +11006858 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006859
NeilBrown57afd892005-06-21 17:17:13 -07006860 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 for (m = 0; m < SYNC_MARKS; m++) {
6862 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006863 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 }
6865 last_mark = 0;
6866 mddev->resync_mark = mark[last_mark];
6867 mddev->resync_mark_cnt = mark_cnt[last_mark];
6868
6869 /*
6870 * Tune reconstruction:
6871 */
6872 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05006873 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
6874 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
6876 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 last_check = 0;
6878
6879 if (j>2) {
6880 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006881 "md: resuming %s of %s from checkpoint.\n",
6882 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 mddev->curr_resync = j;
6884 }
NeilBrown75d3da42011-01-14 09:14:34 +11006885 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006888 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
NeilBrown57afd892005-06-21 17:17:13 -07006890 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006891
NeilBrown7a91ee12009-05-26 12:57:21 +10006892 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6893 ((mddev->curr_resync > mddev->curr_resync_completed &&
6894 (mddev->curr_resync - mddev->curr_resync_completed)
6895 > (max_sectors >> 4)) ||
6896 (j - mddev->curr_resync_completed)*2
6897 >= mddev->resync_max - mddev->curr_resync_completed
6898 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006899 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11006900 wait_event(mddev->recovery_wait,
6901 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006902 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006903 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006904 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006905 }
NeilBrownacb180b2009-04-14 16:28:34 +10006906
NeilBrowne62e58a2009-07-01 13:15:35 +10006907 while (j >= mddev->resync_max && !kthread_should_stop()) {
6908 /* As this condition is controlled by user-space,
6909 * we can block indefinitely, so use '_interruptible'
6910 * to avoid triggering warnings.
6911 */
6912 flush_signals(current); /* just in case */
6913 wait_event_interruptible(mddev->recovery_wait,
6914 mddev->resync_max > j
6915 || kthread_should_stop());
6916 }
NeilBrownacb180b2009-04-14 16:28:34 +10006917
6918 if (kthread_should_stop())
6919 goto interrupted;
6920
NeilBrown57afd892005-06-21 17:17:13 -07006921 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006922 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006923 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006924 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 goto out;
6926 }
NeilBrown57afd892005-06-21 17:17:13 -07006927
6928 if (!skipped) { /* actual IO requested */
6929 io_sectors += sectors;
6930 atomic_add(sectors, &mddev->recovery_active);
6931 }
6932
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 j += sectors;
6934 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006935 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006936 if (last_check == 0)
6937 /* this is the earliers that rebuilt will be
6938 * visible in /proc/mdstat
6939 */
6940 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006941
6942 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 continue;
6944
NeilBrown57afd892005-06-21 17:17:13 -07006945 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
NeilBrowndfc70642008-05-23 13:04:39 -07006947 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 break;
6949
6950 repeat:
6951 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6952 /* step marks */
6953 int next = (last_mark+1) % SYNC_MARKS;
6954
6955 mddev->resync_mark = mark[next];
6956 mddev->resync_mark_cnt = mark_cnt[next];
6957 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006958 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 last_mark = next;
6960 }
6961
6962
NeilBrownc6207272008-02-06 01:39:52 -08006963 if (kthread_should_stop())
6964 goto interrupted;
6965
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
6967 /*
6968 * this loop exits only if either when we are slower than
6969 * the 'hard' speed limit, or the system was IO-idle for
6970 * a jiffy.
6971 * the system might be non-idle CPU-wise, but we only care
6972 * about not overloading the IO subsystem. (things like an
6973 * e2fsck being done on the RAID array should execute fast)
6974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 cond_resched();
6976
NeilBrown57afd892005-06-21 17:17:13 -07006977 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6978 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
NeilBrown88202a02006-01-06 00:21:36 -08006980 if (currspeed > speed_min(mddev)) {
6981 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006982 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006983 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 goto repeat;
6985 }
6986 }
6987 }
NeilBrown61df9d92006-10-03 01:15:57 -07006988 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 /*
6990 * this also signals 'finished resyncing' to md_stop
6991 */
6992 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6994
6995 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006996 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
NeilBrowndfc70642008-05-23 13:04:39 -07006998 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006999 mddev->curr_resync > 2) {
7000 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7001 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7002 if (mddev->curr_resync >= mddev->recovery_cp) {
7003 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007004 "md: checkpointing %s of %s.\n",
7005 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007006 mddev->recovery_cp = mddev->curr_resync;
7007 }
7008 } else
7009 mddev->recovery_cp = MaxSector;
7010 } else {
7011 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7012 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007013 rcu_read_lock();
7014 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007015 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007016 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007017 !test_bit(Faulty, &rdev->flags) &&
7018 !test_bit(In_sync, &rdev->flags) &&
7019 rdev->recovery_offset < mddev->curr_resync)
7020 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007021 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 }
NeilBrown17571282006-12-10 02:20:52 -08007024 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007027 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7028 /* We completed so min/max setting can be forgotten if used. */
7029 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7030 mddev->resync_min = 0;
7031 mddev->resync_max = MaxSector;
7032 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7033 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 mddev->curr_resync = 0;
7035 wake_up(&resync_wait);
7036 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7037 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007038 return;
7039
7040 interrupted:
7041 /*
7042 * got a signal, exit.
7043 */
7044 printk(KERN_INFO
7045 "md: md_do_sync() got signal ... exiting\n");
7046 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7047 goto out;
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049}
NeilBrown29269552006-03-27 01:18:10 -08007050EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007052static int remove_and_add_spares(mddev_t *mddev)
7053{
7054 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007055 int spares = 0;
7056
NeilBrown97e4f422009-03-31 14:33:13 +11007057 mddev->curr_resync_completed = 0;
7058
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007059 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007060 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007061 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007062 (test_bit(Faulty, &rdev->flags) ||
7063 ! test_bit(In_sync, &rdev->flags)) &&
7064 atomic_read(&rdev->nr_pending)==0) {
7065 if (mddev->pers->hot_remove_disk(
7066 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007067 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007068 rdev->raid_disk = -1;
7069 }
7070 }
7071
NeilBrown53890422011-07-27 11:00:36 +10007072 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007073 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007074 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007075 !test_bit(In_sync, &rdev->flags) &&
NeilBrown42742152011-06-28 16:59:42 +10007076 !test_bit(Faulty, &rdev->flags) &&
Dan Williamse5427132008-07-28 23:28:06 -07007077 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007078 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007079 if (rdev->raid_disk < 0
7080 && !test_bit(Faulty, &rdev->flags)) {
7081 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007082 if (mddev->pers->
7083 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007084 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007085 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007086 spares++;
7087 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007088 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007089 } else
7090 break;
7091 }
NeilBrowndfc70642008-05-23 13:04:39 -07007092 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007093 }
7094 return spares;
7095}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007096
7097static void reap_sync_thread(mddev_t *mddev)
7098{
7099 mdk_rdev_t *rdev;
7100
7101 /* resync has finished, collect result */
7102 md_unregister_thread(mddev->sync_thread);
7103 mddev->sync_thread = NULL;
7104 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7105 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7106 /* success...*/
7107 /* activate any spares */
7108 if (mddev->pers->spare_active(mddev))
7109 sysfs_notify(&mddev->kobj, NULL,
7110 "degraded");
7111 }
7112 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7113 mddev->pers->finish_reshape)
7114 mddev->pers->finish_reshape(mddev);
7115 md_update_sb(mddev, 1);
7116
7117 /* if array is no-longer degraded, then any saved_raid_disk
7118 * information must be scrapped
7119 */
7120 if (!mddev->degraded)
7121 list_for_each_entry(rdev, &mddev->disks, same_set)
7122 rdev->saved_raid_disk = -1;
7123
7124 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7125 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7126 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7127 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7128 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7129 /* flag recovery needed just to double check */
7130 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7131 sysfs_notify_dirent_safe(mddev->sysfs_action);
7132 md_new_event(mddev);
7133}
7134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135/*
7136 * This routine is regularly called by all per-raid-array threads to
7137 * deal with generic issues like resync and super-block update.
7138 * Raid personalities that don't have a thread (linear/raid0) do not
7139 * need this as they never do any recovery or update the superblock.
7140 *
7141 * It does not do any resync itself, but rather "forks" off other threads
7142 * to do that as needed.
7143 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7144 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007145 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 * and wakeups up this thread which will reap the thread and finish up.
7147 * This thread also removes any faulty devices (with nr_pending == 0).
7148 *
7149 * The overall approach is:
7150 * 1/ if the superblock needs updating, update it.
7151 * 2/ If a recovery thread is running, don't do anything else.
7152 * 3/ If recovery has finished, clean up, possibly marking spares active.
7153 * 4/ If there are any faulty devices, remove them.
7154 * 5/ If array is degraded, try to add spares devices
7155 * 6/ If array has spares or is not in-sync, start a resync thread.
7156 */
7157void md_check_recovery(mddev_t *mddev)
7158{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007159 if (mddev->suspended)
7160 return;
7161
NeilBrown5f404022005-06-21 17:17:16 -07007162 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007163 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
NeilBrownfca4d842005-06-21 17:17:11 -07007165 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007166 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007167 printk(KERN_INFO "md: %s in immediate safe mode\n",
7168 mdname(mddev));
7169 mddev->safemode = 2;
7170 }
7171 flush_signals(current);
7172 }
7173
NeilBrownc89a8ee2008-08-05 15:54:13 +10007174 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007177 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007179 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007180 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007181 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7182 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 ))
7184 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007185
NeilBrowndf5b89b2006-03-27 01:18:20 -08007186 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007187 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007188
NeilBrownc89a8ee2008-08-05 15:54:13 +10007189 if (mddev->ro) {
7190 /* Only thing we do on a ro array is remove
7191 * failed devices.
7192 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007193 mdk_rdev_t *rdev;
7194 list_for_each_entry(rdev, &mddev->disks, same_set)
7195 if (rdev->raid_disk >= 0 &&
7196 !test_bit(Blocked, &rdev->flags) &&
7197 test_bit(Faulty, &rdev->flags) &&
7198 atomic_read(&rdev->nr_pending)==0) {
7199 if (mddev->pers->hot_remove_disk(
7200 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007201 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007202 rdev->raid_disk = -1;
7203 }
7204 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007205 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7206 goto unlock;
7207 }
7208
NeilBrown31a59e32008-04-30 00:52:30 -07007209 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007210 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007211 spin_lock_irq(&mddev->write_lock);
7212 if (mddev->safemode &&
7213 !atomic_read(&mddev->writes_pending) &&
7214 !mddev->in_sync &&
7215 mddev->recovery_cp == MaxSector) {
7216 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007217 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007218 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007219 }
7220 if (mddev->safemode == 1)
7221 mddev->safemode = 0;
7222 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007223 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007224 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007225 }
NeilBrownfca4d842005-06-21 17:17:11 -07007226
NeilBrown850b2b42006-10-03 01:15:46 -07007227 if (mddev->flags)
7228 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007229
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7231 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7232 /* resync/recovery still happening */
7233 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7234 goto unlock;
7235 }
7236 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007237 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 goto unlock;
7239 }
Neil Brown72a23c22008-06-28 08:31:41 +10007240 /* Set RUNNING before clearing NEEDED to avoid
7241 * any transients in the value of "sync_action".
7242 */
7243 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7244 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007245 /* Clear some bits that don't mean anything, but
7246 * might be left set
7247 */
NeilBrown24dd4692005-11-08 21:39:26 -08007248 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7249 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007251 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7252 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 /* no recovery is running.
7254 * remove any failed drives, then
7255 * add spares if possible.
7256 * Spare are also removed and re-added, to allow
7257 * the personality to fail the re-add.
7258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007260 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007261 if (mddev->pers->check_reshape == NULL ||
7262 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007263 /* Cannot proceed */
7264 goto unlock;
7265 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007266 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007267 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007268 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7269 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007270 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007271 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007272 } else if (mddev->recovery_cp < MaxSector) {
7273 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007274 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007275 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7276 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007278
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007280 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7281 /* We are adding a device or devices to an array
7282 * which has the bitmap stored on all devices.
7283 * So make sure all bitmap pages get written
7284 */
7285 bitmap_write_all(mddev->bitmap);
7286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 mddev->sync_thread = md_register_thread(md_do_sync,
7288 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007289 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 if (!mddev->sync_thread) {
7291 printk(KERN_ERR "%s: could not start resync"
7292 " thread...\n",
7293 mdname(mddev));
7294 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007295 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7296 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7297 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7298 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7299 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007300 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007302 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007303 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 }
7305 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007306 if (!mddev->sync_thread) {
7307 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7308 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7309 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007310 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007311 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 mddev_unlock(mddev);
7314 }
7315}
7316
Dan Williams6bfe0b42008-04-30 00:52:32 -07007317void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7318{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007319 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007320 wait_event_timeout(rdev->blocked_wait,
7321 !test_bit(Blocked, &rdev->flags),
7322 msecs_to_jiffies(5000));
7323 rdev_dec_pending(rdev, mddev);
7324}
7325EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7326
Adrian Bunk75c96f82005-05-05 16:16:09 -07007327static int md_notify_reboot(struct notifier_block *this,
7328 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329{
7330 struct list_head *tmp;
7331 mddev_t *mddev;
7332
7333 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7334
7335 printk(KERN_INFO "md: stopping all md devices.\n");
7336
NeilBrown29ac4aa2008-02-06 01:39:58 -08007337 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007338 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007339 /* Force a switch to readonly even array
7340 * appears to still be in use. Hence
7341 * the '100'.
7342 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007343 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007344 mddev_unlock(mddev);
7345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 /*
7347 * certain more exotic SCSI devices are known to be
7348 * volatile wrt too early system reboots. While the
7349 * right place to handle this issue is the given
7350 * driver, we do want to have a safe RAID driver ...
7351 */
7352 mdelay(1000*1);
7353 }
7354 return NOTIFY_DONE;
7355}
7356
Adrian Bunk75c96f82005-05-05 16:16:09 -07007357static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 .notifier_call = md_notify_reboot,
7359 .next = NULL,
7360 .priority = INT_MAX, /* before any real devices */
7361};
7362
7363static void md_geninit(void)
7364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7366
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007367 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368}
7369
Adrian Bunk75c96f82005-05-05 16:16:09 -07007370static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371{
Tejun Heoe804ac72010-10-15 15:36:08 +02007372 int ret = -ENOMEM;
7373
Tejun Heoada609e2011-01-25 14:35:54 +01007374 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02007375 if (!md_wq)
7376 goto err_wq;
7377
7378 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7379 if (!md_misc_wq)
7380 goto err_misc_wq;
7381
7382 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7383 goto err_md;
7384
7385 if ((ret = register_blkdev(0, "mdp")) < 0)
7386 goto err_mdp;
7387 mdp_major = ret;
7388
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007389 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007390 md_probe, NULL, NULL);
7391 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 md_probe, NULL, NULL);
7393
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007395 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
7397 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
Tejun Heoe804ac72010-10-15 15:36:08 +02007400err_mdp:
7401 unregister_blkdev(MD_MAJOR, "md");
7402err_md:
7403 destroy_workqueue(md_misc_wq);
7404err_misc_wq:
7405 destroy_workqueue(md_wq);
7406err_wq:
7407 return ret;
7408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
7410#ifndef MODULE
7411
7412/*
7413 * Searches all registered partitions for autorun RAID arrays
7414 * at boot time.
7415 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007416
7417static LIST_HEAD(all_detected_devices);
7418struct detected_devices_node {
7419 struct list_head list;
7420 dev_t dev;
7421};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422
7423void md_autodetect_dev(dev_t dev)
7424{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007425 struct detected_devices_node *node_detected_dev;
7426
7427 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7428 if (node_detected_dev) {
7429 node_detected_dev->dev = dev;
7430 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7431 } else {
7432 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7433 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435}
7436
7437
7438static void autostart_arrays(int part)
7439{
7440 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007441 struct detected_devices_node *node_detected_dev;
7442 dev_t dev;
7443 int i_scanned, i_passed;
7444
7445 i_scanned = 0;
7446 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447
7448 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7449
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007450 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7451 i_scanned++;
7452 node_detected_dev = list_entry(all_detected_devices.next,
7453 struct detected_devices_node, list);
7454 list_del(&node_detected_dev->list);
7455 dev = node_detected_dev->dev;
7456 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007457 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 if (IS_ERR(rdev))
7459 continue;
7460
NeilBrownb2d444d2005-11-08 21:39:31 -08007461 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 MD_BUG();
7463 continue;
7464 }
NeilBrownd0fae182008-03-04 14:29:31 -08007465 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007467 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007469
7470 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7471 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472
7473 autorun_devices(part);
7474}
7475
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007476#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477
7478static __exit void md_exit(void)
7479{
7480 mddev_t *mddev;
7481 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007482
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007483 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007484 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007486 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 unregister_blkdev(mdp_major, "mdp");
7488 unregister_reboot_notifier(&md_notifier);
7489 unregister_sysctl_table(raid_table_header);
7490 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007491 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007493 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007495 destroy_workqueue(md_misc_wq);
7496 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497}
7498
Dan Williams685784a2007-07-09 11:56:42 -07007499subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500module_exit(md_exit)
7501
NeilBrownf91de922005-11-08 21:39:36 -08007502static int get_ro(char *buffer, struct kernel_param *kp)
7503{
7504 return sprintf(buffer, "%d", start_readonly);
7505}
7506static int set_ro(const char *val, struct kernel_param *kp)
7507{
7508 char *e;
7509 int num = simple_strtoul(val, &e, 10);
7510 if (*val && (*e == '\0' || *e == '\n')) {
7511 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007512 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007513 }
7514 return -EINVAL;
7515}
7516
NeilBrown80ca3a42006-07-10 04:44:18 -07007517module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7518module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007519
NeilBrownefeb53c2009-01-09 08:31:10 +11007520module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007521
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522EXPORT_SYMBOL(register_md_personality);
7523EXPORT_SYMBOL(unregister_md_personality);
7524EXPORT_SYMBOL(md_error);
7525EXPORT_SYMBOL(md_done_sync);
7526EXPORT_SYMBOL(md_write_start);
7527EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528EXPORT_SYMBOL(md_register_thread);
7529EXPORT_SYMBOL(md_unregister_thread);
7530EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531EXPORT_SYMBOL(md_check_recovery);
7532MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007533MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007534MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007535MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);